kernel - Fix keyboard probe for chromebooks (2)
[dragonfly.git] / sys / dev / misc / psm / psm.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
4 * All rights reserved.
5 *
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.
11 *
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.
984263bc 22 */
984263bc
MD
23/*
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
27 *
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.
34 *
35 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
37 *
38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39 * Andrew Herbert - 12 June 1993
40 *
41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
42 * - 13 June 1993
43 *
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
45 * - 24 October 1993
46 *
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'.
91be4d71 53 * - January/February 1997. Tweaked probe logic for
984263bc
MD
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
91be4d71 56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
984263bc
MD
57 * Improved sync check logic.
58 * Vendor specific support routines.
91be4d71
AHJ
59 *
60 * $FreeBSD: src/sys/dev/atkbdc/psm.c,v 1.107 2010/09/09 07:52:15 ed Exp $
984263bc 61 */
984263bc
MD
62#include "opt_psm.h"
63
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>
fef8985e 70#include <sys/device.h>
0de295ce 71#include <sys/event.h>
984263bc
MD
72#include <sys/syslog.h>
73#include <sys/malloc.h>
984263bc 74#include <sys/rman.h>
91be4d71 75#include <sys/sysctl.h>
a6f9c039 76#include <sys/thread2.h>
984263bc
MD
77#include <sys/time.h>
78#include <sys/uio.h>
b47b3275 79#include <sys/machintr.h>
984263bc
MD
80
81#include <machine/clock.h>
82#include <machine/limits.h>
91be4d71 83#include <sys/mouse.h>
984263bc 84
1f2de5d4
MD
85#include <bus/isa/isavar.h>
86#include <dev/misc/kbd/atkbdcreg.h>
984263bc
MD
87
88/*
89 * Driver specific options: the following options may be set by
90 * `options' statements in the kernel configuration file.
91 */
92
93/* debugging */
94#ifndef PSM_DEBUG
91be4d71
AHJ
95#define PSM_DEBUG 0 /*
96 * logging: 0: none, 1: brief, 2: verbose
97 * 3: sync errors, 4: all packets
98 */
984263bc 99#endif
91be4d71
AHJ
100#define VLOG(level, args) do { \
101 if (verbose >= level) \
102 log args; \
103} while (0)
984263bc 104
91be4d71
AHJ
105#ifndef PSM_INPUT_TIMEOUT
106#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
984263bc
MD
107#endif
108
91be4d71
AHJ
109#ifndef PSM_TAP_TIMEOUT
110#define PSM_TAP_TIMEOUT 125000
111#endif
112
113#ifndef PSM_TAP_THRESHOLD
114#define PSM_TAP_THRESHOLD 25
984263bc
MD
115#endif
116
117/* end of driver specific options */
118
91be4d71
AHJ
119#define PSM_DRIVER_NAME "psm"
120#define PSMCPNP_DRIVER_NAME "psmcpnp"
984263bc
MD
121
122/* input queue */
91be4d71
AHJ
123#define PSM_BUFSIZE 960
124#define PSM_SMALLBUFSIZE 240
984263bc
MD
125
126/* operation levels */
91be4d71
AHJ
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
984263bc
MD
132
133/* Logitech PS2++ protocol */
91be4d71
AHJ
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))
984263bc
MD
138
139/* some macros */
91be4d71
AHJ
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))
984263bc
MD
143
144/* ring buffer */
145typedef struct ringbuf {
91be4d71
AHJ
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];
984263bc
MD
150} ringbuf_t;
151
91be4d71
AHJ
152/* data buffer */
153typedef struct packetbuf {
154 u_char ipacket[16]; /* interim input buffer */
155 int inputbytes; /* # of bytes in the input buffer */
156} packetbuf_t;
157
158#ifndef PSM_PACKETQUEUE
159#define PSM_PACKETQUEUE 128
160#endif
161
162enum {
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
193};
194
195typedef 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;
230
231typedef struct synapticspacket {
232 int x;
233 int y;
234} synapticspacket_t;
235
236#define SYNAPTICS_PACKETQUEUE 10
237#define SYNAPTICS_QUEUE_CURSOR(x) \
238 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
239
240typedef struct synapticsaction {
241 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
242 int queue_len;
243 int queue_cursor;
244 int window_min;
245 int start_x;
246 int start_y;
247 int avg_dx;
248 int avg_dy;
249 int squelch_x;
250 int squelch_y;
251 int fingers_nb;
252 int tap_button;
253 int in_taphold;
254 int in_vscroll;
255} synapticsaction_t;
256
984263bc
MD
257/* driver control block */
258struct psm_softc { /* Driver status information */
91be4d71
AHJ
259 int unit;
260 struct kqinfo rkq; /* Processes with registered kevents */
261 u_char state; /* Mouse driver state */
262 int config; /* driver configuration flags */
263 int flags; /* other flags */
264 KBDC kbdc; /* handle to access kbd controller */
265 struct resource *intr; /* IRQ resource */
266 void *ih; /* interrupt handle */
267 mousehw_t hw; /* hardware information */
268 synapticshw_t synhw; /* Synaptics hardware information */
269 synapticsinfo_t syninfo; /* Synaptics configuration */
270 synapticsaction_t synaction; /* Synaptics action context */
271 mousemode_t mode; /* operation mode */
272 mousemode_t dflt_mode; /* default operation mode */
273 mousestatus_t status; /* accumulated mouse movement */
274 ringbuf_t queue; /* mouse status queue */
275 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
276 int pqueue_start; /* start of data in queue */
277 int pqueue_end; /* end of data in queue */
278 int button; /* the latest button state */
279 int xold; /* previous absolute X position */
280 int yold; /* previous absolute Y position */
281 int xaverage; /* average X position */
282 int yaverage; /* average Y position */
283 int squelch; /* level to filter movement at low speed */
284 int zmax; /* maximum pressure value for touchpads */
285 int syncerrors; /* # of bytes discarded to synchronize */
286 int pkterrors; /* # of packets failed during quaranteen. */
287 struct timeval inputtimeout;
288 struct timeval lastsoftintr; /* time of last soft interrupt */
289 struct timeval lastinputerr; /* time last sync error happened */
290 struct timeval taptimeout; /* tap timeout for touchpads */
291 int watchdog; /* watchdog timer flag */
292 struct callout callout; /* watchdog timer call out */
293 struct callout softcallout; /* buffer timer call out */
294 struct cdev *dev;
295 struct cdev *bdev;
296 int lasterr;
297 int cmdcount;
298 struct sigio *async; /* Processes waiting for SIGIO */
984263bc 299};
91be4d71
AHJ
300static devclass_t psm_devclass;
301#define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
984263bc
MD
302
303/* driver state flags (state) */
91be4d71
AHJ
304#define PSM_VALID 0x80
305#define PSM_OPEN 1 /* Device is open */
306#define PSM_ASLP 2 /* Waiting for mouse data */
307#define PSM_SOFTARMED 4 /* Software interrupt armed */
308#define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
984263bc
MD
309
310/* driver configuration flags (config) */
91be4d71
AHJ
311#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
312#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
313#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
314#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
315#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
316#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
317#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
318#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
319#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
320#define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
321
322#define PSM_CONFIG_FLAGS \
323 (PSM_CONFIG_RESOLUTION | \
324 PSM_CONFIG_ACCEL | \
325 PSM_CONFIG_NOCHECKSYNC | \
326 PSM_CONFIG_SYNCHACK | \
327 PSM_CONFIG_NOIDPROBE | \
328 PSM_CONFIG_NORESET | \
329 PSM_CONFIG_FORCETAP | \
330 PSM_CONFIG_IGNPORTERROR | \
331 PSM_CONFIG_HOOKRESUME | \
332 PSM_CONFIG_INITAFTERSUSPEND)
984263bc
MD
333
334/* other flags (flags) */
91be4d71
AHJ
335#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
336
337/* Tunables */
338static int tap_enabled = -1;
339TUNABLE_INT("hw.psm.tap_enabled", &tap_enabled);
340
341static int synaptics_support = 0;
342TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support);
343
344static int verbose = PSM_DEBUG;
345TUNABLE_INT("debug.psm.loglevel", &verbose);
984263bc
MD
346
347/* for backward compatibility */
91be4d71
AHJ
348#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
349#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
350#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
984263bc
MD
351
352typedef struct old_mousehw {
91be4d71
AHJ
353 int buttons;
354 int iftype;
355 int type;
356 int hwid;
984263bc
MD
357} old_mousehw_t;
358
359typedef struct old_mousemode {
91be4d71
AHJ
360 int protocol;
361 int rate;
362 int resolution;
363 int accelfactor;
984263bc
MD
364} old_mousemode_t;
365
366/* packet formatting function */
91be4d71
AHJ
367typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
368 mousestatus_t *);
984263bc
MD
369
370/* function prototypes */
91be4d71
AHJ
371static void psmidentify(driver_t *, device_t);
372static int psmprobe(device_t);
373static int psmattach(device_t);
374static int psmdetach(device_t);
375static int psmresume(device_t);
376
377static d_open_t psmopen;
378static d_close_t psmclose;
379static d_read_t psmread;
380static d_write_t psmwrite;
381static d_ioctl_t psmioctl;
0de295ce 382static d_kqfilter_t psmkqfilter;
984263bc 383
91be4d71
AHJ
384static int enable_aux_dev(KBDC);
385static int disable_aux_dev(KBDC);
386static int get_mouse_status(KBDC, int *, int, int);
387static int get_aux_id(KBDC);
388static int set_mouse_sampling_rate(KBDC, int);
389static int set_mouse_scaling(KBDC, int);
390static int set_mouse_resolution(KBDC, int);
391static int set_mouse_mode(KBDC);
392static int get_mouse_buttons(KBDC);
393static int is_a_mouse(int);
394static void recover_from_error(KBDC);
395static int restore_controller(KBDC, int);
396static int doinitialize(struct psm_softc *, mousemode_t *);
397static int doopen(struct psm_softc *, int);
398static int reinitialize(struct psm_softc *, int);
399static char *model_name(int);
400static void psmsoftintr(void *);
401static void psmintr(void *);
402static void psmtimeout(void *);
403static void psmfilter_detach(struct knote *);
404static int psmfilter(struct knote *, long);
405static int timeelapsed(const struct timeval *, int, int,
406 const struct timeval *);
407static void dropqueue(struct psm_softc *);
408static void flushpackets(struct psm_softc *);
409static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
410 mousestatus_t *, int *, int *, int *);
411static int proc_synaptics(struct psm_softc *, packetbuf_t *,
412 mousestatus_t *, int *, int *, int *);
413static void proc_versapad(struct psm_softc *, packetbuf_t *,
414 mousestatus_t *, int *, int *, int *);
415static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
416 u_char *);
984263bc
MD
417
418/* vendor specific features */
91be4d71
AHJ
419typedef int probefunc_t(struct psm_softc *);
420
421static int mouse_id_proc1(KBDC, int, int, int *);
422static int mouse_ext_command(KBDC, int);
423
424static probefunc_t enable_groller;
425static probefunc_t enable_gmouse;
426static probefunc_t enable_aglide;
427static probefunc_t enable_kmouse;
428static probefunc_t enable_msexplorer;
429static probefunc_t enable_msintelli;
430static probefunc_t enable_4dmouse;
431static probefunc_t enable_4dplus;
432static probefunc_t enable_mmanplus;
433static probefunc_t enable_synaptics;
434static probefunc_t enable_versapad;
984263bc
MD
435
436static struct {
91be4d71
AHJ
437 int model;
438 u_char syncmask;
439 int packetsize;
440 probefunc_t *probefunc;
984263bc 441} vendortype[] = {
91be4d71
AHJ
442 /*
443 * WARNING: the order of probe is very important. Don't mess it
444 * unless you know what you are doing.
445 */
446 { MOUSE_MODEL_NET, /* Genius NetMouse */
447 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
448 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
449 0xc8, 6, enable_groller },
450 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
451 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
452 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
453 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
454 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
455 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
456 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
457 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
458 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
459 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
460 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
461 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
462 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
463 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
464 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
465 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
466 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
467 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
468 { MOUSE_MODEL_GENERIC,
469 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
984263bc 470};
b370aff7 471#define GENERIC_MOUSE_ENTRY (NELEM(vendortype) - 1)
984263bc
MD
472
473/* device driver declarateion */
474static device_method_t psm_methods[] = {
475 /* Device interface */
91be4d71 476 DEVMETHOD(device_identify, psmidentify),
984263bc
MD
477 DEVMETHOD(device_probe, psmprobe),
478 DEVMETHOD(device_attach, psmattach),
479 DEVMETHOD(device_detach, psmdetach),
480 DEVMETHOD(device_resume, psmresume),
481
d3c9c58e 482 DEVMETHOD_END
984263bc
MD
483};
484
485static driver_t psm_driver = {
91be4d71
AHJ
486 PSM_DRIVER_NAME,
487 psm_methods,
488 sizeof(struct psm_softc),
984263bc
MD
489};
490
fef8985e 491static struct dev_ops psm_ops = {
88abd8b5 492 { PSM_DRIVER_NAME, 0, 0 },
91be4d71
AHJ
493 .d_open = psmopen,
494 .d_close = psmclose,
495 .d_read = psmread,
496 .d_write = psmwrite,
497 .d_ioctl = psmioctl,
498 .d_kqfilter = psmkqfilter
984263bc
MD
499};
500
984263bc
MD
501/* device I/O routines */
502static int
503enable_aux_dev(KBDC kbdc)
504{
91be4d71 505 int res;
984263bc 506
91be4d71
AHJ
507 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
508 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
984263bc 509
91be4d71 510 return (res == PSM_ACK);
984263bc
MD
511}
512
513static int
514disable_aux_dev(KBDC kbdc)
515{
91be4d71 516 int res;
984263bc 517
91be4d71
AHJ
518 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
519 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
984263bc 520
91be4d71 521 return (res == PSM_ACK);
984263bc
MD
522}
523
524static int
525get_mouse_status(KBDC kbdc, int *status, int flag, int len)
526{
91be4d71
AHJ
527 int cmd;
528 int res;
529 int i;
530
531 switch (flag) {
532 case 0:
533 default:
534 cmd = PSMC_SEND_DEV_STATUS;
535 break;
536 case 1:
537 cmd = PSMC_SEND_DEV_DATA;
538 break;
539 }
540 empty_aux_buffer(kbdc, 5);
541 res = send_aux_command(kbdc, cmd);
542 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
543 (flag == 1) ? "DATA" : "STATUS", res));
544 if (res != PSM_ACK)
545 return (0);
546
547 for (i = 0; i < len; ++i) {
548 status[i] = read_aux_data(kbdc);
549 if (status[i] < 0)
550 break;
551 }
552
553 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
554 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
555
556 return (i);
984263bc
MD
557}
558
559static int
560get_aux_id(KBDC kbdc)
561{
91be4d71
AHJ
562 int res;
563 int id;
984263bc 564
91be4d71
AHJ
565 empty_aux_buffer(kbdc, 5);
566 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
567 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
568 if (res != PSM_ACK)
569 return (-1);
984263bc 570
91be4d71
AHJ
571 /* 10ms delay */
572 DRIVERSLEEP(10000);
984263bc 573
91be4d71
AHJ
574 id = read_aux_data(kbdc);
575 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
984263bc 576
91be4d71 577 return (id);
984263bc
MD
578}
579
580static int
581set_mouse_sampling_rate(KBDC kbdc, int rate)
582{
91be4d71 583 int res;
984263bc 584
91be4d71
AHJ
585 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
586 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
984263bc 587
91be4d71 588 return ((res == PSM_ACK) ? rate : -1);
984263bc
MD
589}
590
591static int
592set_mouse_scaling(KBDC kbdc, int scale)
593{
91be4d71
AHJ
594 int res;
595
596 switch (scale) {
597 case 1:
598 default:
599 scale = PSMC_SET_SCALING11;
600 break;
601 case 2:
602 scale = PSMC_SET_SCALING21;
603 break;
604 }
605 res = send_aux_command(kbdc, scale);
606 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
607 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
608
609 return (res == PSM_ACK);
984263bc
MD
610}
611
612/* `val' must be 0 through PSMD_MAX_RESOLUTION */
613static int
614set_mouse_resolution(KBDC kbdc, int val)
615{
91be4d71 616 int res;
984263bc 617
91be4d71
AHJ
618 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
619 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
984263bc 620
91be4d71 621 return ((res == PSM_ACK) ? val : -1);
984263bc
MD
622}
623
624/*
625 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
626 * re-enabled by calling `enable_aux_dev()'
627 */
628static int
629set_mouse_mode(KBDC kbdc)
630{
91be4d71 631 int res;
984263bc 632
91be4d71
AHJ
633 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
634 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
984263bc 635
91be4d71 636 return (res == PSM_ACK);
984263bc
MD
637}
638
639static int
640get_mouse_buttons(KBDC kbdc)
641{
91be4d71
AHJ
642 int c = 2; /* assume two buttons by default */
643 int status[3];
644
645 /*
646 * NOTE: a special sequence to obtain Logitech Mouse specific
647 * information: set resolution to 25 ppi, set scaling to 1:1, set
648 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
649 * mouse status bytes is the number of available buttons.
650 * Some manufactures also support this sequence.
651 */
652 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
653 return (c);
654 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
655 set_mouse_scaling(kbdc, 1) &&
656 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
657 return (status[1]);
658 return (c);
984263bc
MD
659}
660
661/* misc subroutines */
662/*
663 * Someday, I will get the complete list of valid pointing devices and
664 * their IDs... XXX
665 */
666static int
667is_a_mouse(int id)
668{
669#if 0
91be4d71
AHJ
670 static int valid_ids[] = {
671 PSM_MOUSE_ID, /* mouse */
672 PSM_BALLPOINT_ID, /* ballpoint device */
673 PSM_INTELLI_ID, /* Intellimouse */
674 PSM_EXPLORER_ID, /* Intellimouse Explorer */
675 -1 /* end of table */
676 };
677 int i;
678
679 for (i = 0; valid_ids[i] >= 0; ++i)
680 if (valid_ids[i] == id)
681 return (TRUE);
682 return (FALSE);
984263bc 683#else
91be4d71 684 return (TRUE);
984263bc
MD
685#endif
686}
687
688static char *
689model_name(int model)
690{
91be4d71
AHJ
691 static struct {
692 int model_code;
693 char *model_name;
694 } models[] = {
695 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
696 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
697 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
698 { MOUSE_MODEL_THINK, "ThinkingMouse" },
699 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
700 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
701 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
702 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
703 { MOUSE_MODEL_4D, "4D Mouse" },
704 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
705 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
706 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
707 { MOUSE_MODEL_UNKNOWN, "Unknown" },
708 };
709 int i;
710
711 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
712 if (models[i].model_code == model)
713 break;
714 return (models[i].model_name);
984263bc
MD
715}
716
717static void
718recover_from_error(KBDC kbdc)
719{
91be4d71
AHJ
720 /* discard anything left in the output buffer */
721 empty_both_buffers(kbdc, 10);
984263bc
MD
722
723#if 0
91be4d71
AHJ
724 /*
725 * NOTE: KBDC_RESET_KBD may not restore the communication between the
726 * keyboard and the controller.
727 */
728 reset_kbd(kbdc);
984263bc 729#else
91be4d71
AHJ
730 /*
731 * NOTE: somehow diagnostic and keyboard port test commands bring the
732 * keyboard back.
733 */
734 if (!test_controller(kbdc))
735 log(LOG_ERR, "psm: keyboard controller failed.\n");
736 /* if there isn't a keyboard in the system, the following error is OK */
737 if (test_kbd_port(kbdc) != 0)
738 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
984263bc
MD
739#endif
740}
741
742static int
743restore_controller(KBDC kbdc, int command_byte)
744{
984263bc 745 empty_both_buffers(kbdc, 10);
91be4d71
AHJ
746
747 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
748 log(LOG_ERR, "psm: failed to restore the keyboard controller "
749 "command byte.\n");
750 empty_both_buffers(kbdc, 10);
751 return (FALSE);
752 } else {
753 empty_both_buffers(kbdc, 10);
754 return (TRUE);
755 }
984263bc
MD
756}
757
91be4d71 758/*
984263bc 759 * Re-initialize the aux port and device. The aux port must be enabled
91be4d71 760 * and its interrupt must be disabled before calling this routine.
984263bc
MD
761 * The aux device will be disabled before returning.
762 * The keyboard controller must be locked via `kbdc_lock()' before
763 * calling this routine.
764 */
765static int
766doinitialize(struct psm_softc *sc, mousemode_t *mode)
767{
91be4d71
AHJ
768 KBDC kbdc = sc->kbdc;
769 int stat[3];
770 int i;
771
772 switch((i = test_aux_port(kbdc))) {
773 case 1: /* ignore these errors */
774 case 2:
775 case 3:
776 case PSM_ACK:
777 if (verbose)
778 log(LOG_DEBUG,
779 "psm%d: strange result for test aux port (%d).\n",
780 sc->unit, i);
781 /* FALLTHROUGH */
782 case 0: /* no error */
984263bc 783 break;
91be4d71
AHJ
784 case -1: /* time out */
785 default: /* error */
786 recover_from_error(kbdc);
787 if (sc->config & PSM_CONFIG_IGNPORTERROR)
788 break;
789 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
790 sc->unit, i);
791 return (FALSE);
792 }
793
794 if (sc->config & PSM_CONFIG_NORESET) {
795 /*
796 * Don't try to reset the pointing device. It may possibly
797 * be left in the unknown state, though...
798 */
799 } else {
800 /*
801 * NOTE: some controllers appears to hang the `keyboard' when
802 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
803 */
804 if (!reset_aux_dev(kbdc)) {
805 recover_from_error(kbdc);
806 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
807 sc->unit);
808 return (FALSE);
809 }
810 }
811
812 /*
813 * both the aux port and the aux device is functioning, see
814 * if the device can be enabled.
815 */
816 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
817 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
818 sc->unit);
819 return (FALSE);
820 }
821 empty_both_buffers(kbdc, 10); /* remove stray data if any */
822
823 if (sc->config & PSM_CONFIG_NOIDPROBE)
824 i = GENERIC_MOUSE_ENTRY;
825 else {
826 /* FIXME: hardware ID, mouse buttons? */
827
828 /* other parameters */
829 for (i = 0; vendortype[i].probefunc != NULL; ++i)
830 if ((*vendortype[i].probefunc)(sc)) {
831 if (verbose >= 2)
832 log(LOG_ERR, "psm%d: found %s\n",
833 sc->unit,
834 model_name(vendortype[i].model));
835 break;
836 }
837 }
838
839 sc->hw.model = vendortype[i].model;
840 sc->mode.packetsize = vendortype[i].packetsize;
841
842 /* set mouse parameters */
843 if (mode != NULL) {
844 if (mode->rate > 0)
845 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
846 if (mode->resolution >= 0)
847 mode->resolution =
848 set_mouse_resolution(kbdc, mode->resolution);
849 set_mouse_scaling(kbdc, 1);
850 set_mouse_mode(kbdc);
851 }
984263bc 852
91be4d71
AHJ
853 /* Record sync on the next data packet we see. */
854 sc->flags |= PSM_NEED_SYNCBITS;
984263bc 855
91be4d71
AHJ
856 /* just check the status of the mouse */
857 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
858 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
859 sc->unit);
860
861 return (TRUE);
984263bc
MD
862}
863
864static int
865doopen(struct psm_softc *sc, int command_byte)
866{
91be4d71
AHJ
867 int stat[3];
868
869 /*
870 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
871 * no obvious reason. Thus we check the current mode and restore the
872 * Absolute Mode if it was cleared.
873 *
874 * The previous hack at the end of psmprobe() wasn't efficient when
875 * moused(8) was restarted.
876 *
877 * A Reset (FF) or Set Defaults (F6) command would clear the
878 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
879 * doesn't show any evidence of such a command.
984263bc 880 */
91be4d71
AHJ
881 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
882 mouse_ext_command(sc->kbdc, 1);
883 get_mouse_status(sc->kbdc, stat, 0, 3);
884 if (stat[1] == 0x47 && stat[2] == 0x40) {
885 /* Set the mode byte -- request wmode where
886 * available */
887 if (sc->synhw.capExtended)
888 mouse_ext_command(sc->kbdc, 0xc1);
889 else
890 mouse_ext_command(sc->kbdc, 0xc0);
891 set_mouse_sampling_rate(sc->kbdc, 20);
892 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
893 "hopefully restored\n",
894 sc->unit));
895 }
896 }
897
898 /*
899 * A user may want to disable tap and drag gestures on a Synaptics
900 * TouchPad when it operates in Relative Mode.
984263bc 901 */
91be4d71
AHJ
902 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
903 if (tap_enabled > 0) {
904 /*
905 * Enable tap & drag gestures. We use a Mode Byte
906 * and clear the DisGest bit (see §2.5 of Synaptics
907 * TouchPad Interfacing Guide).
908 */
909 VLOG(2, (LOG_DEBUG,
910 "psm%d: enable tap and drag gestures\n",
911 sc->unit));
912 mouse_ext_command(sc->kbdc, 0x00);
913 set_mouse_sampling_rate(sc->kbdc, 20);
914 } else if (tap_enabled == 0) {
915 /*
916 * Disable tap & drag gestures. We use a Mode Byte
917 * and set the DisGest bit (see §2.5 of Synaptics
918 * TouchPad Interfacing Guide).
919 */
920 VLOG(2, (LOG_DEBUG,
921 "psm%d: disable tap and drag gestures\n",
922 sc->unit));
923 mouse_ext_command(sc->kbdc, 0x04);
924 set_mouse_sampling_rate(sc->kbdc, 20);
925 }
926 }
927
928 /* enable the mouse device */
929 if (!enable_aux_dev(sc->kbdc)) {
930 /* MOUSE ERROR: failed to enable the mouse because:
931 * 1) the mouse is faulty,
932 * 2) the mouse has been removed(!?)
933 * In the latter case, the keyboard may have hung, and need
934 * recovery procedure...
935 */
936 recover_from_error(sc->kbdc);
937#if 0
938 /* FIXME: we could reset the mouse here and try to enable
939 * it again. But it will take long time and it's not a good
940 * idea to disable the keyboard that long...
941 */
942 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
943 recover_from_error(sc->kbdc);
984263bc 944#else
91be4d71 945 {
984263bc 946#endif
91be4d71
AHJ
947 restore_controller(sc->kbdc, command_byte);
948 /* mark this device is no longer available */
949 sc->state &= ~PSM_VALID;
950 log(LOG_ERR,
951 "psm%d: failed to enable the device (doopen).\n",
952 sc->unit);
953 return (EIO);
954 }
984263bc 955 }
984263bc 956
91be4d71
AHJ
957 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
958 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
959 sc->unit);
984263bc 960
91be4d71
AHJ
961 /* enable the aux port and interrupt */
962 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 963 KBD_AUX_CONTROL_BITS,
91be4d71
AHJ
964 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
965 /* CONTROLLER ERROR */
966 disable_aux_dev(sc->kbdc);
967 restore_controller(sc->kbdc, command_byte);
968 log(LOG_ERR,
969 "psm%d: failed to enable the aux interrupt (doopen).\n",
970 sc->unit);
971 return (EIO);
972 }
973
974 /* start the watchdog timer */
975 sc->watchdog = FALSE;
976 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc);
977
978 return (0);
984263bc
MD
979}
980
981static int
982reinitialize(struct psm_softc *sc, int doinit)
983{
91be4d71
AHJ
984 int err;
985 int c;
986
987 /* don't let anybody mess with the aux device */
988 if (!kbdc_lock(sc->kbdc, TRUE))
989 return (EIO);
990
991 crit_enter();
992
993 /* block our watchdog timer */
994 sc->watchdog = FALSE;
995 callout_stop(&sc->callout);
996
997 /* save the current controller command byte */
998 empty_both_buffers(sc->kbdc, 10);
999 c = get_controller_command_byte(sc->kbdc);
1000 VLOG(2, (LOG_DEBUG,
1001 "psm%d: current command byte: %04x (reinitialize).\n",
1002 sc->unit, c));
1003
1004 /* enable the aux port but disable the aux interrupt and the keyboard */
7f3d8d55
MD
1005 if ((c == -1) ||
1006 !set_controller_command_byte(sc->kbdc,
1007 KBD_AUX_CONTROL_BITS,
1008 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1009 /* CONTROLLER ERROR */
1010 crit_exit();
1011 kbdc_lock(sc->kbdc, FALSE);
1012 log(LOG_ERR,
1013 "psm%d: unable to set the command byte (reinitialize).\n",
1014 sc->unit);
1015 return (EIO);
1016 }
1017
1018 /* flush any data */
1019 if (sc->state & PSM_VALID) {
1020 /* this may fail; but never mind... */
1021 disable_aux_dev(sc->kbdc);
1022 empty_aux_buffer(sc->kbdc, 10);
1023 }
1024 flushpackets(sc);
1025 sc->syncerrors = 0;
1026 sc->pkterrors = 0;
1027 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1028
1029 /* try to detect the aux device; are you still there? */
1030 err = 0;
1031 if (doinit) {
1032 if (doinitialize(sc, &sc->mode)) {
1033 /* yes */
1034 sc->state |= PSM_VALID;
1035 } else {
1036 /* the device has gone! */
1037 restore_controller(sc->kbdc, c);
1038 sc->state &= ~PSM_VALID;
1039 log(LOG_ERR,
1040 "psm%d: the aux device has gone! (reinitialize).\n",
1041 sc->unit);
1042 err = ENXIO;
1043 }
1044 }
a6f9c039 1045 crit_exit();
91be4d71
AHJ
1046
1047 /* restore the driver state */
1048 if ((sc->state & PSM_OPEN) && (err == 0)) {
1049 /* enable the aux device and the port again */
1050 err = doopen(sc, c);
1051 if (err != 0)
1052 log(LOG_ERR, "psm%d: failed to enable the device "
1053 "(reinitialize).\n", sc->unit);
984263bc 1054 } else {
91be4d71
AHJ
1055 /* restore the keyboard port and disable the aux port */
1056 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1057 KBD_AUX_CONTROL_BITS,
91be4d71
AHJ
1058 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1059 /* CONTROLLER ERROR */
1060 log(LOG_ERR, "psm%d: failed to disable the aux port "
1061 "(reinitialize).\n", sc->unit);
1062 err = EIO;
1063 }
1064 }
1065
1066 kbdc_lock(sc->kbdc, FALSE);
1067 return (err);
984263bc
MD
1068}
1069
1070/* psm driver entry points */
1071
91be4d71
AHJ
1072static void
1073psmidentify(driver_t *driver, device_t parent)
1074{
1075 device_t psmc;
1076 device_t psm;
1077 u_long irq;
1078 int unit;
1079
1080 unit = device_get_unit(parent);
1081
1082 /* always add at least one child */
1083 psm = BUS_ADD_CHILD(parent, parent, KBDC_RID_AUX, driver->name, unit);
1084 if (psm == NULL)
1085 return;
1086
1087 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1088 if (irq > 0)
1089 return;
1090
1091 /*
1092 * If the PS/2 mouse device has already been reported by ACPI or
1093 * PnP BIOS, obtain the IRQ resource from it.
1094 * (See psmcpnp_attach() below.)
1095 */
1096 psmc = device_find_child(device_get_parent(parent),
1097 PSMCPNP_DRIVER_NAME, unit);
1098 if (psmc == NULL)
1099 return;
1100 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1101 if (irq <= 0)
1102 return;
b47b3275 1103 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1,
bec969af 1104 machintr_legacy_intr_cpuid(irq));
91be4d71
AHJ
1105}
1106
1107#define endprobe(v) do { \
1108 if (bootverbose) \
1109 --verbose; \
7f3d8d55
MD
1110 set_controller_command_byte(sc->kbdc, \
1111 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT); \
91be4d71
AHJ
1112 kbdc_lock(sc->kbdc, FALSE); \
1113 return (v); \
1114} while (0)
984263bc
MD
1115
1116static int
1117psmprobe(device_t dev)
1118{
91be4d71
AHJ
1119 int unit = device_get_unit(dev);
1120 struct psm_softc *sc = device_get_softc(dev);
1121 uintptr_t irq;
1122 uintptr_t flags;
1123 int stat[3];
1124 int command_byte;
91be4d71 1125 int i;
984263bc
MD
1126
1127#if 0
91be4d71 1128 kbdc_debug(TRUE);
984263bc
MD
1129#endif
1130
91be4d71
AHJ
1131 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
1132 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags);
984263bc 1133
91be4d71
AHJ
1134 sc->unit = unit;
1135 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1136 sc->config = flags & PSM_CONFIG_FLAGS;
1137 /* XXX: for backward compatibility */
984263bc 1138#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
91be4d71 1139 sc->config |=
984263bc
MD
1140#ifdef PSM_RESETAFTERSUSPEND
1141 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
1142#else
1143 PSM_CONFIG_HOOKRESUME;
1144#endif
1145#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
91be4d71
AHJ
1146 sc->flags = 0;
1147 if (bootverbose)
1148 ++verbose;
1149
1150 device_set_desc(dev, "PS/2 Mouse");
1151
1152 if (!kbdc_lock(sc->kbdc, TRUE)) {
1153 kprintf("psm%d: unable to lock the controller.\n", unit);
1154 if (bootverbose)
1155 --verbose;
1156 return (ENXIO);
1157 }
1158
1159 /*
1160 * NOTE: two bits in the command byte controls the operation of the
1161 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1162 * port interrupt (IRQ 12) enable bit (bit 2).
984263bc 1163 */
91be4d71
AHJ
1164
1165 /* discard anything left after the keyboard initialization */
1166 empty_both_buffers(sc->kbdc, 10);
1167
1168 /* save the current command byte; it will be used later */
91be4d71
AHJ
1169 command_byte = get_controller_command_byte(sc->kbdc);
1170 if (verbose)
1171 kprintf("psm%d: current command byte:%04x\n", unit,
1172 command_byte);
1173 if (command_byte == -1) {
1174 /* CONTROLLER ERROR */
7f3d8d55
MD
1175 kprintf("psm%d: unable to get the current "
1176 "command byte value.\n",
91be4d71
AHJ
1177 unit);
1178 endprobe(ENXIO);
1179 }
1180
1181 /*
7f3d8d55
MD
1182 * NOTE: We cannot mess with the keyboard port, do NOT disable it
1183 * while we are probing the aux port during this routine.
1184 * Disabling the keyboard port will break some things
1185 * (Acer c720)... probably related to BIOS emulation of the
1186 * i8042.
984263bc 1187 */
91be4d71 1188 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1189 KBD_AUX_CONTROL_BITS, KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1190 /*
1191 * this is CONTROLLER ERROR; I don't know how to recover
1192 * from this error...
1193 */
91be4d71
AHJ
1194 kprintf("psm%d: unable to set the command byte.\n", unit);
1195 endprobe(ENXIO);
1196 }
7f3d8d55
MD
1197
1198 /*
1199 * NOTE: Linux doesn't send discrete aux port enablement commands,
1200 * it is unclear whether this is needed or helps or hinders
1201 * bios emulators.
1202 */
91be4d71
AHJ
1203 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1204
984263bc 1205 /*
91be4d71
AHJ
1206 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1207 * port exists and is functioning. However, some controllers appears
1208 * to respond with zero even when the aux port doesn't exist. (It may
1209 * be that this is only the case when the controller DOES have the aux
1210 * port but the port is not wired on the motherboard.) The keyboard
1211 * controllers without the port, such as the original AT, are
1212 * supporsed to return with an error code or simply time out. In any
1213 * case, we have to continue probing the port even when the controller
1214 * passes this test.
f46756f3 1215 *
91be4d71
AHJ
1216 * XXX: some controllers erroneously return the error code 1, 2 or 3
1217 * when it has the perfectly functional aux port. We have to ignore
1218 * this error code. Even if the controller HAS error with the aux
1219 * port, it will be detected later...
1220 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
984263bc 1221 */
91be4d71
AHJ
1222 switch ((i = test_aux_port(sc->kbdc))) {
1223 case 1: /* ignore these errors */
1224 case 2:
1225 case 3:
1226 case PSM_ACK:
1227 if (verbose)
1228 kprintf("psm%d: strange result for test aux port "
1229 "(%d).\n", unit, i);
1230 /* FALLTHROUGH */
1231 case 0: /* no error */
1232 break;
1233 case -1: /* time out */
1234 default: /* error */
1235 recover_from_error(sc->kbdc);
1236 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1237 break;
91be4d71 1238 if (verbose)
7f3d8d55
MD
1239 kprintf("psm%d: the aux port is not "
1240 "functioning (%d).\n",
1241 unit, i);
91be4d71
AHJ
1242 endprobe(ENXIO);
1243 }
1244
1245 if (sc->config & PSM_CONFIG_NORESET) {
1246 /*
1247 * Don't try to reset the pointing device. It may possibly be
1248 * left in the unknown state, though...
1249 */
1250 } else {
1251 /*
1252 * NOTE: some controllers appears to hang the `keyboard' when
1253 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1254 *
1255 * Attempt to reset the controller twice -- this helps
1256 * pierce through some KVM switches. The second reset
1257 * is non-fatal.
1258 */
1259 if (!reset_aux_dev(sc->kbdc)) {
1260 recover_from_error(sc->kbdc);
91be4d71
AHJ
1261 if (verbose)
1262 kprintf("psm%d: failed to reset the aux "
1263 "device.\n", unit);
1264 endprobe(ENXIO);
1265 } else if (!reset_aux_dev(sc->kbdc)) {
1266 recover_from_error(sc->kbdc);
1267 if (verbose >= 2)
1268 kprintf("psm%d: failed to reset the aux device "
1269 "(2).\n", unit);
1270 }
1271 }
1272
1273 /*
1274 * both the aux port and the aux device is functioning, see if the
1275 * device can be enabled. NOTE: when enabled, the device will start
1276 * sending data; we shall immediately disable the device once we know
1277 * the device can be enabled.
1278 */
1279 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1280 /* MOUSE ERROR */
1281 recover_from_error(sc->kbdc);
91be4d71
AHJ
1282 if (verbose)
1283 kprintf("psm%d: failed to enable the aux device.\n",
1284 unit);
1285 endprobe(ENXIO);
1286 }
1287
1288 /* save the default values after reset */
1289 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1290 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1291 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1292 } else {
1293 sc->dflt_mode.rate = sc->mode.rate = -1;
1294 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1295 }
1296
1297 /* hardware information */
1298 sc->hw.iftype = MOUSE_IF_PS2;
1299
1300 /* verify the device is a mouse */
1301 sc->hw.hwid = get_aux_id(sc->kbdc);
1302 if (!is_a_mouse(sc->hw.hwid)) {
91be4d71
AHJ
1303 if (verbose)
1304 kprintf("psm%d: unknown device type (%d).\n", unit,
1305 sc->hw.hwid);
1306 endprobe(ENXIO);
1307 }
1308 switch (sc->hw.hwid) {
1309 case PSM_BALLPOINT_ID:
1310 sc->hw.type = MOUSE_TRACKBALL;
1311 break;
1312 case PSM_MOUSE_ID:
1313 case PSM_INTELLI_ID:
1314 case PSM_EXPLORER_ID:
1315 case PSM_4DMOUSE_ID:
1316 case PSM_4DPLUS_ID:
1317 sc->hw.type = MOUSE_MOUSE;
984263bc 1318 break;
91be4d71
AHJ
1319 default:
1320 sc->hw.type = MOUSE_UNKNOWN;
1321 break;
1322 }
1323
1324 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1325 sc->hw.buttons = 2;
1326 i = GENERIC_MOUSE_ENTRY;
1327 } else {
1328 /* # of buttons */
1329 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1330
1331 /* other parameters */
1332 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1333 if ((*vendortype[i].probefunc)(sc)) {
1334 if (verbose >= 2)
1335 kprintf("psm%d: found %s\n", unit,
1336 model_name(vendortype[i].model));
1337 break;
1338 }
1339 }
1340
1341 sc->hw.model = vendortype[i].model;
1342
1343 sc->dflt_mode.level = PSM_LEVEL_BASE;
1344 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1345 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1346 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1347 sc->dflt_mode.syncmask[0] = 0;
1348 else
1349 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1350 if (sc->config & PSM_CONFIG_FORCETAP)
1351 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1352 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1353 sc->mode = sc->dflt_mode;
1354 sc->mode.packetsize = vendortype[i].packetsize;
1355
1356 /* set mouse parameters */
984263bc 1357#if 0
91be4d71
AHJ
1358 /*
1359 * A version of Logitech FirstMouse+ won't report wheel movement,
1360 * if SET_DEFAULTS is sent... Don't use this command.
1361 * This fix was found by Takashi Nishida.
1362 */
1363 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1364 if (verbose >= 2)
1365 kprintf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
984263bc 1366#endif
91be4d71
AHJ
1367 if (sc->config & PSM_CONFIG_RESOLUTION)
1368 sc->mode.resolution =
1369 set_mouse_resolution(sc->kbdc,
1370 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1371 else if (sc->mode.resolution >= 0)
1372 sc->mode.resolution =
1373 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1374 if (sc->mode.rate > 0)
1375 sc->mode.rate =
1376 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1377 set_mouse_scaling(sc->kbdc, 1);
1378
1379 /* Record sync on the next data packet we see. */
1380 sc->flags |= PSM_NEED_SYNCBITS;
1381
1382 /* just check the status of the mouse */
1383 /*
1384 * NOTE: XXX there are some arcane controller/mouse combinations out
1385 * there, which hung the controller unless there is data transmission
1386 * after ACK from the mouse.
984263bc 1387 */
91be4d71
AHJ
1388 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1389 kprintf("psm%d: failed to get status.\n", unit);
1390 else {
1391 /*
1392 * When in its native mode, some mice operate with different
1393 * default parameters than in the PS/2 compatible mode.
1394 */
1395 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1396 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1397 }
984263bc 1398
91be4d71
AHJ
1399 /* disable the aux port for now... */
1400 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1401 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1402 /*
1403 * this is CONTROLLER ERROR; I don't know the proper way to
1404 * recover from this error...
1405 */
91be4d71
AHJ
1406 kprintf("psm%d: unable to set the command byte.\n", unit);
1407 endprobe(ENXIO);
1408 }
984263bc 1409
91be4d71 1410 /* done */
91be4d71
AHJ
1411 kbdc_lock(sc->kbdc, FALSE);
1412 return (0);
984263bc
MD
1413}
1414
1415static int
1416psmattach(device_t dev)
1417{
91be4d71
AHJ
1418 int unit = device_get_unit(dev);
1419 struct psm_softc *sc = device_get_softc(dev);
1420 int error;
1421 intptr_t irq;
1422 int rid;
1423
1424 /* Setup initial state */
1425 sc->state = PSM_VALID;
1426 callout_init(&sc->callout);
1427 callout_init(&sc->softcallout);
1428
1429 /* Setup our interrupt handler */
1430 rid = KBDC_RID_AUX;
1431 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq);
920ffa6f 1432 sc->intr = bus_alloc_legacy_irq_resource(dev, &rid, irq, RF_ACTIVE);
91be4d71
AHJ
1433
1434 if (sc->intr == NULL)
1435 return (ENXIO);
1436 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr,
0e6f0e28 1437 INTR_NOPOLL, psmintr, sc, &sc->ih, NULL, NULL);
91be4d71
AHJ
1438 if (error) {
1439 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1440 return (error);
1441 }
1442
1443 /* Done */
1444 make_dev(&psm_ops, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit);
1445 make_dev(&psm_ops, PSM_MKMINOR(unit, TRUE), 0, 0, 0666, "bpsm%d", unit);
1446
1447 if (!verbose)
1448 kprintf("psm%d: model %s, device ID %d\n",
1449 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1450 else {
1451 kprintf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1452 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1453 sc->hw.hwid >> 8, sc->hw.buttons);
1454 kprintf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1455 unit, sc->config, sc->flags, sc->mode.packetsize);
1456 kprintf("psm%d: syncmask:%02x, syncbits:%02x\n",
1457 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1458 }
1459
1460 if (bootverbose)
1461 --verbose;
1462
1463 return (0);
984263bc
MD
1464}
1465
1466static int
1467psmdetach(device_t dev)
1468{
91be4d71
AHJ
1469 struct psm_softc *sc;
1470 int rid;
984263bc 1471
91be4d71
AHJ
1472 sc = device_get_softc(dev);
1473 if (sc->state & PSM_OPEN)
1474 return (EBUSY);
984263bc 1475
91be4d71
AHJ
1476 rid = KBDC_RID_AUX;
1477 bus_teardown_intr(dev, sc->intr, sc->ih);
1478 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
e4c9c0c8 1479
91be4d71
AHJ
1480 destroy_dev(sc->dev);
1481 destroy_dev(sc->bdev);
984263bc 1482
91be4d71 1483 return (0);
984263bc
MD
1484}
1485
1486static int
fef8985e 1487psmopen(struct dev_open_args *ap)
984263bc 1488{
91be4d71
AHJ
1489 cdev_t dev = ap->a_head.a_dev;
1490 int unit = PSM_UNIT(dev);
1491 struct psm_softc *sc;
1492 int command_byte;
1493 int err;
1494
1495 /* Get device data */
1496 sc = PSM_SOFTC(unit);
1497 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
1498 /* the device is no longer valid/functioning */
1499 return (ENXIO);
1500 }
1501
1502 /* Disallow multiple opens */
1503 if (sc->state & PSM_OPEN)
1504 return (EBUSY);
984263bc 1505
7e3ac54b 1506#if 0
91be4d71 1507 device_busy(devclass_get_device(psm_devclass, sc->unit));
7e3ac54b 1508#endif
984263bc 1509
91be4d71
AHJ
1510 /* Initialize state */
1511 sc->mode.level = sc->dflt_mode.level;
1512 sc->mode.protocol = sc->dflt_mode.protocol;
1513 sc->watchdog = FALSE;
1514 sc->async = NULL;
984263bc 1515
91be4d71
AHJ
1516 /* flush the event queue */
1517 sc->queue.count = 0;
1518 sc->queue.head = 0;
1519 sc->queue.tail = 0;
1520 sc->status.flags = 0;
1521 sc->status.button = 0;
1522 sc->status.obutton = 0;
1523 sc->status.dx = 0;
1524 sc->status.dy = 0;
1525 sc->status.dz = 0;
1526 sc->button = 0;
1527 sc->pqueue_start = 0;
1528 sc->pqueue_end = 0;
984263bc 1529
91be4d71
AHJ
1530 /* empty input buffer */
1531 flushpackets(sc);
1532 sc->syncerrors = 0;
1533 sc->pkterrors = 0;
1534
1535 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1536 if (!kbdc_lock(sc->kbdc, TRUE))
1537 return (EIO);
1538
1539 /* save the current controller command byte */
1540 crit_enter();
1541 command_byte = get_controller_command_byte(sc->kbdc);
1542
1543 /* enable the aux port and temporalily disable the keyboard */
7f3d8d55
MD
1544 if (command_byte == -1 ||
1545 !set_controller_command_byte(sc->kbdc,
1546 KBD_AUX_CONTROL_BITS,
1547 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1548 /* CONTROLLER ERROR; do you know how to get out of this? */
1549 kbdc_lock(sc->kbdc, FALSE);
1550 crit_exit();
1551 log(LOG_ERR,
1552 "psm%d: unable to set the command byte (psmopen).\n",
1553 sc->unit);
1554 return (EIO);
1555 }
1556 /*
1557 * Now that the keyboard controller is told not to generate
1558 * the keyboard and mouse interrupts, call `splx()' to allow
1559 * the other tty interrupts. The clock interrupt may also occur,
1560 * but timeout routines will be blocked by the poll flag set
1561 * via `kbdc_lock()'
984263bc 1562 */
91be4d71 1563 crit_exit();
984263bc 1564
91be4d71
AHJ
1565 /* enable the mouse device */
1566 err = doopen(sc, command_byte);
984263bc 1567
91be4d71
AHJ
1568 /* done */
1569 if (err == 0)
1570 sc->state |= PSM_OPEN;
1571 kbdc_lock(sc->kbdc, FALSE);
1572 return (err);
984263bc
MD
1573}
1574
1575static int
91be4d71 1576psmclose(struct dev_close_args *ap)
984263bc 1577{
91be4d71
AHJ
1578 cdev_t dev = ap->a_head.a_dev;
1579 int unit = PSM_UNIT(dev);
1580 struct psm_softc *sc = PSM_SOFTC(unit);
1581 int stat[3];
1582 int command_byte;
984263bc 1583
91be4d71
AHJ
1584 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1585 if (!kbdc_lock(sc->kbdc, TRUE))
1586 return (EIO);
984263bc 1587
91be4d71
AHJ
1588 /* save the current controller command byte */
1589 crit_enter();
1590 command_byte = get_controller_command_byte(sc->kbdc);
1591 if (command_byte == -1) {
1592 kbdc_lock(sc->kbdc, FALSE);
1593 crit_exit();
1594 return (EIO);
1595 }
1596
1597 /* disable the aux interrupt and temporalily disable the keyboard */
1598 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1599 KBD_AUX_CONTROL_BITS,
91be4d71
AHJ
1600 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1601 log(LOG_ERR,
1602 "psm%d: failed to disable the aux int (psmclose).\n",
1603 sc->unit);
1604 /* CONTROLLER ERROR;
1605 * NOTE: we shall force our way through. Because the only
1606 * ill effect we shall see is that we may not be able
1607 * to read ACK from the mouse, and it doesn't matter much
1608 * so long as the mouse will accept the DISABLE command.
1609 */
1610 }
1611 crit_exit();
1612
1613 /* stop the watchdog timer */
1614 callout_stop(&sc->callout);
1615
1616 /* remove anything left in the output buffer */
1617 empty_aux_buffer(sc->kbdc, 10);
1618
1619 /* disable the aux device, port and interrupt */
1620 if (sc->state & PSM_VALID) {
1621 if (!disable_aux_dev(sc->kbdc)) {
1622 /* MOUSE ERROR;
1623 * NOTE: we don't return (error) and continue,
1624 * pretending we have successfully disabled the device.
1625 * It's OK because the interrupt routine will discard
1626 * any data from the mouse hereafter.
1627 */
1628 log(LOG_ERR,
1629 "psm%d: failed to disable the device (psmclose).\n",
1630 sc->unit);
1631 }
1632
1633 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1634 log(LOG_DEBUG,
1635 "psm%d: failed to get status (psmclose).\n",
1636 sc->unit);
1637 }
1638
1639 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1640 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1641 /*
1642 * CONTROLLER ERROR;
1643 * we shall ignore this error; see the above comment.
1644 */
1645 log(LOG_ERR,
1646 "psm%d: failed to disable the aux port (psmclose).\n",
1647 sc->unit);
1648 }
1649
1650 /* remove anything left in the output buffer */
1651 empty_aux_buffer(sc->kbdc, 10);
1652
1653 /* close is almost always successful */
1654 sc->state &= ~PSM_OPEN;
1655 kbdc_lock(sc->kbdc, FALSE);
1656#if 0
1657 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
1658#endif
1659 return (0);
1660}
1661
1662static int
1663tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
1664 u_char *buf)
1665{
1666 static u_char butmapps2[8] = {
1667 0,
1668 MOUSE_PS2_BUTTON1DOWN,
1669 MOUSE_PS2_BUTTON2DOWN,
1670 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1671 MOUSE_PS2_BUTTON3DOWN,
1672 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1673 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1674 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
1675 MOUSE_PS2_BUTTON3DOWN,
1676 };
1677 static u_char butmapmsc[8] = {
1678 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
1679 MOUSE_MSC_BUTTON3UP,
1680 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1681 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1682 MOUSE_MSC_BUTTON3UP,
1683 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1684 MOUSE_MSC_BUTTON2UP,
1685 MOUSE_MSC_BUTTON1UP,
1686 0,
1687 };
1688 int mapped;
1689 int i;
1690
1691 if (sc->mode.level == PSM_LEVEL_BASE) {
1692 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1693 if (status->button & MOUSE_BUTTON4DOWN)
1694 mapped |= MOUSE_BUTTON1DOWN;
1695 status->button = mapped;
1696 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1697 i = imax(imin(status->dx, 255), -256);
1698 if (i < 0)
1699 buf[0] |= MOUSE_PS2_XNEG;
1700 buf[1] = i;
1701 i = imax(imin(status->dy, 255), -256);
1702 if (i < 0)
1703 buf[0] |= MOUSE_PS2_YNEG;
1704 buf[2] = i;
1705 return (MOUSE_PS2_PACKETSIZE);
1706 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1707 buf[0] = MOUSE_MSC_SYNC |
1708 butmapmsc[status->button & MOUSE_STDBUTTONS];
1709 i = imax(imin(status->dx, 255), -256);
1710 buf[1] = i >> 1;
1711 buf[3] = i - buf[1];
1712 i = imax(imin(status->dy, 255), -256);
1713 buf[2] = i >> 1;
1714 buf[4] = i - buf[2];
1715 i = imax(imin(status->dz, 127), -128);
1716 buf[5] = (i >> 1) & 0x7f;
1717 buf[6] = (i - (i >> 1)) & 0x7f;
1718 buf[7] = (~status->button >> 3) & 0x7f;
1719 return (MOUSE_SYS_PACKETSIZE);
1720 }
1721 return (pb->inputbytes);
1722}
1723
1724static int
1725psmread(struct dev_read_args *ap)
1726{
1727 cdev_t dev = ap->a_head.a_dev;
1728 struct uio *uio = ap->a_uio;
1729 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1730 u_char buf[PSM_SMALLBUFSIZE];
1731 int error = 0;
1732 int l;
1733
1734 if ((sc->state & PSM_VALID) == 0)
1735 return (EIO);
1736
1737 /* block until mouse activity occured */
1738 crit_enter();
1739 while (sc->queue.count <= 0) {
1740 if (dev != sc->bdev) {
1741 crit_exit();
1742 return (EWOULDBLOCK);
1743 }
1744 sc->state |= PSM_ASLP;
1745 error = tsleep((caddr_t) sc, PCATCH, "psmrea", 0);
1746 sc->state &= ~PSM_ASLP;
1747 if (error) {
1748 crit_exit();
1749 return (error);
1750 } else if ((sc->state & PSM_VALID) == 0) {
1751 /* the device disappeared! */
1752 crit_exit();
1753 return (EIO);
1754 }
1755 }
1756 crit_exit();
1757
1758 /* copy data to the user land */
1759 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1760 crit_enter();
1761 l = imin(sc->queue.count, uio->uio_resid);
1762 if (l > sizeof(buf))
1763 l = sizeof(buf);
1764 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1765 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1766 sizeof(sc->queue.buf) - sc->queue.head);
1767 bcopy(&sc->queue.buf[0],
1768 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1769 l - (sizeof(sc->queue.buf) - sc->queue.head));
1770 } else
1771 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1772 sc->queue.count -= l;
1773 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1774 crit_exit();
1775 error = uiomove(buf, (size_t)l, uio);
1776 if (error)
1777 break;
1778 }
1779
1780 return (error);
1781}
984263bc
MD
1782
1783static int
1784block_mouse_data(struct psm_softc *sc, int *c)
1785{
984263bc 1786
91be4d71
AHJ
1787 if (!kbdc_lock(sc->kbdc, TRUE))
1788 return (EIO);
1789
1790 crit_enter();
1791 *c = get_controller_command_byte(sc->kbdc);
7f3d8d55
MD
1792 if ((*c == -1) ||
1793 !set_controller_command_byte(sc->kbdc,
1794 KBD_AUX_CONTROL_BITS,
1795 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
91be4d71
AHJ
1796 /* this is CONTROLLER ERROR */
1797 crit_exit();
1798 kbdc_lock(sc->kbdc, FALSE);
1799 return (EIO);
1800 }
1801
1802 /*
1803 * The device may be in the middle of status data transmission.
1804 * The transmission will be interrupted, thus, incomplete status
1805 * data must be discarded. Although the aux interrupt is disabled
1806 * at the keyboard controller level, at most one aux interrupt
1807 * may have already been pending and a data byte is in the
1808 * output buffer; throw it away. Note that the second argument
1809 * to `empty_aux_buffer()' is zero, so that the call will just
1810 * flush the internal queue.
1811 * `psmintr()' will be invoked after `splx()' if an interrupt is
1812 * pending; it will see no data and returns immediately.
1813 */
1814 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1815 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1816 flushpackets(sc);
a6f9c039 1817 crit_exit();
91be4d71
AHJ
1818
1819 return (0);
1820}
1821
1822static void
1823dropqueue(struct psm_softc *sc)
1824{
1825
1826 sc->queue.count = 0;
1827 sc->queue.head = 0;
1828 sc->queue.tail = 0;
1829 if ((sc->state & PSM_SOFTARMED) != 0) {
1830 sc->state &= ~PSM_SOFTARMED;
1831 callout_stop(&sc->softcallout);
1832 }
1833 sc->pqueue_start = sc->pqueue_end;
1834}
1835
1836static void
1837flushpackets(struct psm_softc *sc)
1838{
1839
1840 dropqueue(sc);
1841 bzero(&sc->pqueue, sizeof(sc->pqueue));
984263bc
MD
1842}
1843
1844static int
1845unblock_mouse_data(struct psm_softc *sc, int c)
1846{
91be4d71 1847 int error = 0;
984263bc 1848
91be4d71
AHJ
1849 /*
1850 * We may have seen a part of status data during `set_mouse_XXX()'.
1851 * they have been queued; flush it.
1852 */
1853 empty_aux_buffer(sc->kbdc, 0);
984263bc 1854
91be4d71
AHJ
1855 /* restore ports and interrupt */
1856 if (!set_controller_command_byte(sc->kbdc,
7f3d8d55 1857 KBD_AUX_CONTROL_BITS, c & (KBD_AUX_CONTROL_BITS))) {
91be4d71
AHJ
1858 /*
1859 * CONTROLLER ERROR; this is serious, we may have
1860 * been left with the inaccessible keyboard and
1861 * the disabled mouse interrupt.
1862 */
1863 error = EIO;
1864 }
1865
1866 kbdc_lock(sc->kbdc, FALSE);
1867 return (error);
1868}
1869
1870static int
1871psmwrite(struct dev_write_args *ap)
1872{
1873 cdev_t dev = ap->a_head.a_dev;
1874 struct uio *uio = ap->a_uio;
1875 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1876 u_char buf[PSM_SMALLBUFSIZE];
1877 int error = 0, i, l;
1878
1879 if ((sc->state & PSM_VALID) == 0)
1880 return (EIO);
1881
1882 if (sc->mode.level < PSM_LEVEL_NATIVE)
1883 return (ENODEV);
1884
1885 /* copy data from the user land */
1886 while (uio->uio_resid > 0) {
1887 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
1888 error = uiomove(buf, l, uio);
1889 if (error)
1890 break;
1891 for (i = 0; i < l; i++) {
1892 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
1893 if (!write_aux_command(sc->kbdc, buf[i])) {
1894 VLOG(2, (LOG_DEBUG,
1895 "psm: cmd 0x%x failed.\n", buf[i]));
1896 return (reinitialize(sc, FALSE));
1897 }
1898 }
1899 }
1900
1901 return (error);
984263bc
MD
1902}
1903
1904static int
fef8985e 1905psmioctl(struct dev_ioctl_args *ap)
984263bc 1906{
91be4d71
AHJ
1907 cdev_t dev = ap->a_head.a_dev;
1908 caddr_t addr= ap->a_data;
1909 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1910 mousemode_t mode;
1911 mousestatus_t status;
984263bc 1912#if (defined(MOUSE_GETVARS))
91be4d71 1913 mousevar_t *var;
984263bc 1914#endif
91be4d71
AHJ
1915 mousedata_t *data;
1916 int stat[3];
1917 int command_byte;
1918 int error = 0;
1919
1920 mode.resolution = -1;
1921
1922 /* Perform IOCTL command */
1923 switch (ap->a_cmd) {
1924
1925 case OLD_MOUSE_GETHWINFO:
1926 crit_enter();
1927 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1928 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1929 ((old_mousehw_t *)addr)->type = sc->hw.type;
1930 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1931 crit_exit();
1932 break;
984263bc 1933
91be4d71
AHJ
1934 case MOUSE_GETHWINFO:
1935 crit_enter();
1936 *(mousehw_t *)addr = sc->hw;
1937 if (sc->mode.level == PSM_LEVEL_BASE)
1938 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1939 crit_exit();
1940 break;
d557216f 1941
91be4d71
AHJ
1942 case MOUSE_SYN_GETHWINFO:
1943 crit_enter();
1944 if (synaptics_support && sc->hw.model == MOUSE_MODEL_SYNAPTICS)
1945 *(synapticshw_t *)addr = sc->synhw;
1946 else
1947 error = EINVAL;
1948 crit_exit();
1949 break;
984263bc 1950
91be4d71
AHJ
1951 case OLD_MOUSE_GETMODE:
1952 crit_enter();
1953 switch (sc->mode.level) {
1954 case PSM_LEVEL_BASE:
1955 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1956 break;
1957 case PSM_LEVEL_STANDARD:
1958 ((old_mousemode_t *)addr)->protocol =
1959 MOUSE_PROTO_SYSMOUSE;
1960 break;
1961 case PSM_LEVEL_NATIVE:
1962 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1963 break;
1964 }
1965 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1966 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1967 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1968 crit_exit();
1969 break;
984263bc 1970
91be4d71
AHJ
1971 case MOUSE_GETMODE:
1972 crit_enter();
1973 *(mousemode_t *)addr = sc->mode;
1974 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
1975 ((mousemode_t *)addr)->syncmask[0] = 0;
1976 ((mousemode_t *)addr)->syncmask[1] = 0;
1977 }
1978 ((mousemode_t *)addr)->resolution =
1979 MOUSE_RES_LOW - sc->mode.resolution;
1980 switch (sc->mode.level) {
1981 case PSM_LEVEL_BASE:
1982 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1983 ((mousemode_t *)addr)->packetsize =
1984 MOUSE_PS2_PACKETSIZE;
1985 break;
1986 case PSM_LEVEL_STANDARD:
1987 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1988 ((mousemode_t *)addr)->packetsize =
1989 MOUSE_SYS_PACKETSIZE;
1990 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1991 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1992 break;
1993 case PSM_LEVEL_NATIVE:
1994 /* FIXME: this isn't quite correct... XXX */
1995 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1996 break;
1997 }
1998 crit_exit();
1999 break;
984263bc 2000
91be4d71
AHJ
2001 case OLD_MOUSE_SETMODE:
2002 case MOUSE_SETMODE:
2003 if (ap->a_cmd == OLD_MOUSE_SETMODE) {
2004 mode.rate = ((old_mousemode_t *)addr)->rate;
2005 /*
2006 * resolution old I/F new I/F
2007 * default 0 0
2008 * low 1 -2
2009 * medium low 2 -3
2010 * medium high 3 -4
2011 * high 4 -5
2012 */
2013 if (((old_mousemode_t *)addr)->resolution > 0)
2014 mode.resolution =
2015 -((old_mousemode_t *)addr)->resolution - 1;
2016 else
2017 mode.resolution = 0;
2018 mode.accelfactor =
2019 ((old_mousemode_t *)addr)->accelfactor;
2020 mode.level = -1;
2021 } else
2022 mode = *(mousemode_t *)addr;
2023
2024 /* adjust and validate parameters. */
2025 if (mode.rate > UCHAR_MAX)
2026 return (EINVAL);
2027 if (mode.rate == 0)
2028 mode.rate = sc->dflt_mode.rate;
2029 else if (mode.rate == -1)
2030 /* don't change the current setting */
2031 ;
2032 else if (mode.rate < 0)
2033 return (EINVAL);
2034 if (mode.resolution >= UCHAR_MAX)
2035 return (EINVAL);
2036 if (mode.resolution >= 200)
2037 mode.resolution = MOUSE_RES_HIGH;
2038 else if (mode.resolution >= 100)
2039 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2040 else if (mode.resolution >= 50)
2041 mode.resolution = MOUSE_RES_MEDIUMLOW;
2042 else if (mode.resolution > 0)
2043 mode.resolution = MOUSE_RES_LOW;
2044 if (mode.resolution == MOUSE_RES_DEFAULT)
2045 mode.resolution = sc->dflt_mode.resolution;
2046 else if (mode.resolution == -1)
2047 /* don't change the current setting */
2048 ;
2049 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2050 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2051 if (mode.level == -1)
2052 /* don't change the current setting */
2053 mode.level = sc->mode.level;
2054 else if ((mode.level < PSM_LEVEL_MIN) ||
2055 (mode.level > PSM_LEVEL_MAX))
2056 return (EINVAL);
2057 if (mode.accelfactor == -1)
2058 /* don't change the current setting */
2059 mode.accelfactor = sc->mode.accelfactor;
2060 else if (mode.accelfactor < 0)
2061 return (EINVAL);
2062
2063 /* don't allow anybody to poll the keyboard controller */
2064 error = block_mouse_data(sc, &command_byte);
2065 if (error)
2066 return (error);
2067
2068 /* set mouse parameters */
2069 if (mode.rate > 0)
2070 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2071 mode.rate);
2072 if (mode.resolution >= 0)
2073 mode.resolution =
2074 set_mouse_resolution(sc->kbdc, mode.resolution);
2075 set_mouse_scaling(sc->kbdc, 1);
2076 get_mouse_status(sc->kbdc, stat, 0, 3);
2077
2078 crit_enter();
2079 sc->mode.rate = mode.rate;
2080 sc->mode.resolution = mode.resolution;
2081 sc->mode.accelfactor = mode.accelfactor;
2082 sc->mode.level = mode.level;
2083 crit_exit();
2084
2085 unblock_mouse_data(sc, command_byte);
2086 break;
984263bc 2087
91be4d71
AHJ
2088 case MOUSE_GETLEVEL:
2089 *(int *)addr = sc->mode.level;
2090 break;
2091
2092 case MOUSE_SETLEVEL:
2093 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2094 (*(int *)addr > PSM_LEVEL_MAX))
2095 return (EINVAL);
2096 sc->mode.level = *(int *)addr;
2097 break;
2098
2099 case MOUSE_GETSTATUS:
2100 crit_enter();
2101 status = sc->status;
2102 sc->status.flags = 0;
2103 sc->status.obutton = sc->status.button;
2104 sc->status.button = 0;
2105 sc->status.dx = 0;
2106 sc->status.dy = 0;
2107 sc->status.dz = 0;
2108 crit_exit();
2109 *(mousestatus_t *)addr = status;
2110 break;
984263bc
MD
2111
2112#if (defined(MOUSE_GETVARS))
91be4d71
AHJ
2113 case MOUSE_GETVARS:
2114 var = (mousevar_t *)addr;
2115 bzero(var, sizeof(*var));
2116 crit_enter();
2117 var->var[0] = MOUSE_VARS_PS2_SIG;
2118 var->var[1] = sc->config;
2119 var->var[2] = sc->flags;
2120 crit_exit();
2121 break;
984263bc 2122
91be4d71
AHJ
2123 case MOUSE_SETVARS:
2124 return (ENODEV);
984263bc
MD
2125#endif /* MOUSE_GETVARS */
2126
91be4d71
AHJ
2127 case MOUSE_READSTATE:
2128 case MOUSE_READDATA:
2129 data = (mousedata_t *)addr;
c157ff7a 2130 if (data->len > NELEM(data->buf))
91be4d71
AHJ
2131 return (EINVAL);
2132
2133 error = block_mouse_data(sc, &command_byte);
2134 if (error)
2135 return (error);
2136 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2137 (ap->a_cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2138 error = EIO;
2139 unblock_mouse_data(sc, command_byte);
2140 break;
984263bc
MD
2141
2142#if (defined(MOUSE_SETRESOLUTION))
91be4d71
AHJ
2143 case MOUSE_SETRESOLUTION:
2144 mode.resolution = *(int *)addr;
2145 if (mode.resolution >= UCHAR_MAX)
2146 return (EINVAL);
2147 else if (mode.resolution >= 200)
2148 mode.resolution = MOUSE_RES_HIGH;
2149 else if (mode.resolution >= 100)
2150 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2151 else if (mode.resolution >= 50)
2152 mode.resolution = MOUSE_RES_MEDIUMLOW;
2153 else if (mode.resolution > 0)
2154 mode.resolution = MOUSE_RES_LOW;
2155 if (mode.resolution == MOUSE_RES_DEFAULT)
2156 mode.resolution = sc->dflt_mode.resolution;
2157 else if (mode.resolution == -1)
2158 mode.resolution = sc->mode.resolution;
2159 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2160 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2161
2162 error = block_mouse_data(sc, &command_byte);
2163 if (error)
2164 return (error);
2165 sc->mode.resolution =
2166 set_mouse_resolution(sc->kbdc, mode.resolution);
2167 if (sc->mode.resolution != mode.resolution)
2168 error = EIO;
2169 unblock_mouse_data(sc, command_byte);
2170 break;
984263bc
MD
2171#endif /* MOUSE_SETRESOLUTION */
2172
2173#if (defined(MOUSE_SETRATE))
91be4d71
AHJ
2174 case MOUSE_SETRATE:
2175 mode.rate = *(int *)addr;
2176 if (mode.rate > UCHAR_MAX)
2177 return (EINVAL);
2178 if (mode.rate == 0)
2179 mode.rate = sc->dflt_mode.rate;
2180 else if (mode.rate < 0)
2181 mode.rate = sc->mode.rate;
2182
2183 error = block_mouse_data(sc, &command_byte);
2184 if (error)
2185 return (error);
2186 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2187 if (sc->mode.rate != mode.rate)
2188 error = EIO;
2189 unblock_mouse_data(sc, command_byte);
2190 break;
984263bc
MD
2191#endif /* MOUSE_SETRATE */
2192
2193#if (defined(MOUSE_SETSCALING))
91be4d71
AHJ
2194 case MOUSE_SETSCALING:
2195 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2196 return (EINVAL);
2197
2198 error = block_mouse_data(sc, &command_byte);
2199 if (error)
2200 return (error);
2201 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2202 error = EIO;
2203 unblock_mouse_data(sc, command_byte);
2204 break;
984263bc
MD
2205#endif /* MOUSE_SETSCALING */
2206
2207#if (defined(MOUSE_GETHWID))
91be4d71
AHJ
2208 case MOUSE_GETHWID:
2209 error = block_mouse_data(sc, &command_byte);
2210 if (error)
2211 return (error);
2212 sc->hw.hwid &= ~0x00ff;
2213 sc->hw.hwid |= get_aux_id(sc->kbdc);
2214 *(int *)addr = sc->hw.hwid & 0x00ff;
2215 unblock_mouse_data(sc, command_byte);
2216 break;
984263bc
MD
2217#endif /* MOUSE_GETHWID */
2218
91be4d71
AHJ
2219 default:
2220 return (ENOTTY);
2221 }
984263bc 2222
91be4d71 2223 return (error);
984263bc
MD
2224}
2225
2226static void
2227psmtimeout(void *arg)
2228{
91be4d71
AHJ
2229 struct psm_softc *sc;
2230
2231 sc = (struct psm_softc *)arg;
2232 crit_enter();
2233 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2234 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2235 psmintr(sc);
2236 kbdc_lock(sc->kbdc, FALSE);
2237 }
2238 sc->watchdog = TRUE;
2239 crit_exit();
2240 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc);
2241}
2242
2243/* Add all sysctls under the debug.psm and hw.psm nodes */
2244SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2245SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2246
2247SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0,
2248 "Verbosity level");
2249
2250static int psmhz = 20;
2251SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2252 "Frequency of the softcallout (in hz)");
2253static int psmerrsecs = 2;
2254SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2255 "Number of seconds during which packets will dropped after a sync error");
2256static int psmerrusecs = 0;
2257SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2258 "Microseconds to add to psmerrsecs");
2259static int psmsecs = 0;
2260SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2261 "Max number of seconds between soft interrupts");
2262static int psmusecs = 500000;
2263SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2264 "Microseconds to add to psmsecs");
2265static int pkterrthresh = 2;
2266SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2267 "Number of error packets allowed before reinitializing the mouse");
2268
2269SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0,
2270 "Enable tap and drag gestures");
2271static int tap_threshold = PSM_TAP_THRESHOLD;
2272SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2273 "Button tap threshold");
2274static int tap_timeout = PSM_TAP_TIMEOUT;
2275SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2276 "Tap timeout for touchpads");
2277
2278static void
2279psmintr(void *arg)
2280{
2281 struct psm_softc *sc = arg;
2282 struct timeval now;
2283 int c;
2284 packetbuf_t *pb;
2285
2286
2287 /* read until there is nothing to read */
2288 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2289 pb = &sc->pqueue[sc->pqueue_end];
2290
2291 /* discard the byte if the device is not open */
2292 if ((sc->state & PSM_OPEN) == 0)
2293 continue;
2294
2295 getmicrouptime(&now);
2296 if ((pb->inputbytes > 0) &&
2297 timevalcmp(&now, &sc->inputtimeout, >)) {
2298 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2299 "resetting byte count\n"));
2300 pb->inputbytes = 0;
2301 sc->syncerrors = 0;
2302 sc->pkterrors = 0;
2303 }
2304 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2305 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2306 timevaladd(&sc->inputtimeout, &now);
2307
2308 pb->ipacket[pb->inputbytes++] = c;
2309
2310 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2311 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2312 sc->syncerrors = 0;
2313 sc->pkterrors = 0;
2314 goto next;
2315 } else {
2316 if (pb->inputbytes < sc->mode.packetsize)
2317 continue;
2318
2319 VLOG(4, (LOG_DEBUG,
2320 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2321 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2322 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2323 }
2324
2325 c = pb->ipacket[0];
2326
2327 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2328 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2329 sc->flags &= ~PSM_NEED_SYNCBITS;
2330 VLOG(2, (LOG_DEBUG,
2331 "psmintr: Sync bytes now %04x,%04x\n",
2332 sc->mode.syncmask[0], sc->mode.syncmask[0]));
2333 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2334 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
2335 "(%04x != %04x) %d cmds since last error.\n",
2336 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
2337 sc->cmdcount - sc->lasterr));
2338 sc->lasterr = sc->cmdcount;
2339 /*
2340 * The sync byte test is a weak measure of packet
2341 * validity. Conservatively discard any input yet
2342 * to be seen by userland when we detect a sync
2343 * error since there is a good chance some of
2344 * the queued packets have undetected errors.
2345 */
2346 dropqueue(sc);
2347 if (sc->syncerrors == 0)
2348 sc->pkterrors++;
2349 ++sc->syncerrors;
2350 sc->lastinputerr = now;
2351 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
2352 sc->pkterrors >= pkterrthresh) {
2353 /*
2354 * If we've failed to find a single sync byte
2355 * in 2 packets worth of data, or we've seen
2356 * persistent packet errors during the
2357 * validation period, reinitialize the mouse
2358 * in hopes of returning it to the expected
2359 * mode.
2360 */
2361 VLOG(3, (LOG_DEBUG,
2362 "psmintr: reset the mouse.\n"));
2363 reinitialize(sc, TRUE);
2364 } else if (sc->syncerrors == sc->mode.packetsize) {
2365 /*
2366 * Try a soft reset after searching for a sync
2367 * byte through a packet length of bytes.
2368 */
2369 VLOG(3, (LOG_DEBUG,
2370 "psmintr: re-enable the mouse.\n"));
2371 pb->inputbytes = 0;
2372 disable_aux_dev(sc->kbdc);
2373 enable_aux_dev(sc->kbdc);
2374 } else {
2375 VLOG(3, (LOG_DEBUG,
2376 "psmintr: discard a byte (%d)\n",
2377 sc->syncerrors));
2378 pb->inputbytes--;
2379 bcopy(&pb->ipacket[1], &pb->ipacket[0],
2380 pb->inputbytes);
2381 }
2382 continue;
2383 }
2384
2385 /*
2386 * We have what appears to be a valid packet.
2387 * Reset the error counters.
2388 */
2389 sc->syncerrors = 0;
2390
2391 /*
2392 * Drop even good packets if they occur within a timeout
2393 * period of a sync error. This allows the detection of
2394 * a change in the mouse's packet mode without exposing
2395 * erratic mouse behavior to the user. Some KVMs forget
2396 * enhanced mouse modes during switch events.
2397 */
2398 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
2399 &now)) {
2400 pb->inputbytes = 0;
2401 continue;
2402 }
2403
2404 /*
2405 * Now that we're out of the validation period, reset
2406 * the packet error count.
2407 */
2408 sc->pkterrors = 0;
2409
2410 sc->cmdcount++;
2411next:
2412 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
2413 sc->pqueue_end = 0;
2414 /*
2415 * If we've filled the queue then call the softintr ourselves,
2416 * otherwise schedule the interrupt for later.
2417 */
2418 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
2419 (sc->pqueue_end == sc->pqueue_start)) {
2420 if ((sc->state & PSM_SOFTARMED) != 0) {
2421 sc->state &= ~PSM_SOFTARMED;
2422 callout_stop(&sc->softcallout);
2423 }
2424 psmsoftintr(arg);
2425 } else if ((sc->state & PSM_SOFTARMED) == 0) {
2426 sc->state |= PSM_SOFTARMED;
2427 callout_reset(&sc->softcallout, psmhz < 1 ? 1 : (hz/psmhz),
2428 psmsoftintr, arg);
2429 }
2430 }
2431}
2432
2433static void
2434proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2435 int *x, int *y, int *z)
2436{
2437
2438 /*
2439 * PS2++ protocl packet
2440 *
2441 * b7 b6 b5 b4 b3 b2 b1 b0
2442 * byte 1: * 1 p3 p2 1 * * *
2443 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2444 *
2445 * p3-p0: packet type
2446 * c1, c2: c1 & c2 == 1, if p2 == 0
2447 * c1 & c2 == 0, if p2 == 1
2448 *
2449 * packet type: 0 (device type)
2450 * See comments in enable_mmanplus() below.
2451 *
2452 * packet type: 1 (wheel data)
2453 *
2454 * b7 b6 b5 b4 b3 b2 b1 b0
2455 * byte 3: h * B5 B4 s d2 d1 d0
2456 *
2457 * h: 1, if horizontal roller data
2458 * 0, if vertical roller data
2459 * B4, B5: button 4 and 5
2460 * s: sign bit
2461 * d2-d0: roller data
2462 *
2463 * packet type: 2 (reserved)
2464 */
2465 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
2466 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
2467 /*
2468 * the extended data packet encodes button
2469 * and wheel events
2470 */
2471 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
2472 case 1:
2473 /* wheel data packet */
2474 *x = *y = 0;
2475 if (pb->ipacket[2] & 0x80) {
2476 /* XXX horizontal roller count - ignore it */
2477 ;
2478 } else {
2479 /* vertical roller count */
2480 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
2481 (pb->ipacket[2] & 0x0f) - 16 :
2482 (pb->ipacket[2] & 0x0f);
2483 }
2484 ms->button |= (pb->ipacket[2] &
2485 MOUSE_PS2PLUS_BUTTON4DOWN) ?
2486 MOUSE_BUTTON4DOWN : 0;
2487 ms->button |= (pb->ipacket[2] &
2488 MOUSE_PS2PLUS_BUTTON5DOWN) ?
2489 MOUSE_BUTTON5DOWN : 0;
2490 break;
2491 case 2:
2492 /*
2493 * this packet type is reserved by
2494 * Logitech...
2495 */
2496 /*
2497 * IBM ScrollPoint Mouse uses this
2498 * packet type to encode both vertical
2499 * and horizontal scroll movement.
2500 */
2501 *x = *y = 0;
2502 /* horizontal count */
2503 if (pb->ipacket[2] & 0x0f)
2504 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
2505 -2 : 2;
2506 /* vertical count */
2507 if (pb->ipacket[2] & 0xf0)
2508 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
2509 -1 : 1;
2510 break;
2511 case 0:
2512 /* device type packet - shouldn't happen */
2513 /* FALLTHROUGH */
2514 default:
2515 *x = *y = 0;
2516 ms->button = ms->obutton;
2517 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
2518 "type %d: 0x%02x 0x%02x 0x%02x\n",
2519 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
2520 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
2521 break;
2522 }
2523 } else {
2524 /* preserve button states */
2525 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
2526 }
2527}
2528
2529static int
2530proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
2531 int *x, int *y, int *z)
2532{
2533 static int touchpad_buttons;
2534 static int guest_buttons;
2535 int w, x0, y0;
2536
2537 /* TouchPad PS/2 absolute mode message format
2538 *
2539 * Bits: 7 6 5 4 3 2 1 0 (LSB)
2540 * ------------------------------------------------
2541 * ipacket[0]: 1 0 W3 W2 0 W1 R L
2542 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
2543 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
2544 * ipacket[3]: 1 1 Yc Xc 0 W0 D U
2545 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
2546 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
2547 *
2548 * Legend:
2549 * L: left physical mouse button
2550 * R: right physical mouse button
2551 * D: down button
2552 * U: up button
2553 * W: "wrist" value
2554 * X: x position
2555 * Y: y position
2556 * Z: pressure
2557 *
2558 * Absolute reportable limits: 0 - 6143.
2559 * Typical bezel limits: 1472 - 5472.
2560 * Typical edge marings: 1632 - 5312.
2561 *
2562 * w = 3 Passthrough Packet
2563 *
2564 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
2565 */
2566
2567 if (!synaptics_support)
2568 return (0);
2569
2570 /* Sanity check for out of sync packets. */
2571 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
2572 (pb->ipacket[3] & 0xc8) != 0xc0)
2573 return (-1);
2574
2575 *x = *y = 0;
2576
2577 /*
2578 * Pressure value.
2579 * Interpretation:
2580 * z = 0 No finger contact
2581 * z = 10 Finger hovering near the pad
2582 * z = 30 Very light finger contact
2583 * z = 80 Normal finger contact
2584 * z = 110 Very heavy finger contact
2585 * z = 200 Finger lying flat on pad surface
2586 * z = 255 Maximum reportable Z
2587 */
2588 *z = pb->ipacket[2];
2589
2590 /*
2591 * Finger width value
2592 * Interpretation:
2593 * w = 0 Two finger on the pad (capMultiFinger needed)
2594 * w = 1 Three or more fingers (capMultiFinger needed)
2595 * w = 2 Pen (instead of finger) (capPen needed)
2596 * w = 3 Reserved (passthrough?)
2597 * w = 4-7 Finger of normal width (capPalmDetect needed)
2598 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
2599 * w = 15 Maximum reportable width (capPalmDetect needed)
2600 */
2601 /* XXX Is checking capExtended enough? */
2602 if (sc->synhw.capExtended)
2603 w = ((pb->ipacket[0] & 0x30) >> 2) |
2604 ((pb->ipacket[0] & 0x04) >> 1) |
2605 ((pb->ipacket[3] & 0x04) >> 2);
2606 else {
2607 /* Assume a finger of regular width. */
2608 w = 4;
2609 }
2610
2611 /* Handle packets from the guest device */
2612 /* XXX Documentation? */
2613 if (w == 3 && sc->synhw.capPassthrough) {
2614 *x = ((pb->ipacket[1] & 0x10) ?
2615 pb->ipacket[4] - 256 : pb->ipacket[4]);
2616 *y = ((pb->ipacket[1] & 0x20) ?
2617 pb->ipacket[5] - 256 : pb->ipacket[5]);
2618 *z = 0;
2619
2620 guest_buttons = 0;
2621 if (pb->ipacket[1] & 0x01)
2622 guest_buttons |= MOUSE_BUTTON1DOWN;
2623 if (pb->ipacket[1] & 0x04)
2624 guest_buttons |= MOUSE_BUTTON2DOWN;
2625 if (pb->ipacket[1] & 0x02)
2626 guest_buttons |= MOUSE_BUTTON3DOWN;
2627
2628 ms->button = touchpad_buttons | guest_buttons;
2629 goto SYNAPTICS_END;
2630 }
2631
2632 /* Button presses */
2633 touchpad_buttons = 0;
2634 if (pb->ipacket[0] & 0x01)
2635 touchpad_buttons |= MOUSE_BUTTON1DOWN;
2636 if (pb->ipacket[0] & 0x02)
2637 touchpad_buttons |= MOUSE_BUTTON3DOWN;
2638
2639 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
2640 if ((pb->ipacket[3] & 0x01) && (pb->ipacket[0] & 0x01) == 0)
2641 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2642 if ((pb->ipacket[3] & 0x02) && (pb->ipacket[0] & 0x02) == 0)
2643 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2644 }
2645
2646 /*
2647 * In newer pads - bit 0x02 in the third byte of
2648 * the packet indicates that we have an extended
2649 * button press.
2650 */
2651 /* XXX Documentation? */
2652 if (pb->ipacket[3] & 0x02) {
2653 /*
2654 * if directional_scrolls is not 1, we treat any of
2655 * the scrolling directions as middle-click.
2656 */
2657 if (sc->syninfo.directional_scrolls) {
2658 if (pb->ipacket[4] & 0x01)
2659 touchpad_buttons |= MOUSE_BUTTON4DOWN;
2660 if (pb->ipacket[5] & 0x01)
2661 touchpad_buttons |= MOUSE_BUTTON5DOWN;
2662 if (pb->ipacket[4] & 0x02)
2663 touchpad_buttons |= MOUSE_BUTTON6DOWN;
2664 if (pb->ipacket[5] & 0x02)
2665 touchpad_buttons |= MOUSE_BUTTON7DOWN;
2666 } else {
2667 if ((pb->ipacket[4] & 0x0F) ||
2668 (pb->ipacket[5] & 0x0F))
2669 touchpad_buttons |= MOUSE_BUTTON2DOWN;
2670 }
2671 }
2672
2673 ms->button = touchpad_buttons | guest_buttons;
2674
2675 /* Check pressure to detect a real wanted action on the
2676 * touchpad. */
2677 if (*z >= sc->syninfo.min_pressure) {
2678 synapticsaction_t *synaction;
2679 int cursor, peer, window;
2680 int dx, dy, dxp, dyp;
2681 int max_width, max_pressure;
2682 int margin_top, margin_right, margin_bottom, margin_left;
2683 int na_top, na_right, na_bottom, na_left;
2684 int window_min, window_max;
2685 int multiplicator;
2686 int weight_current, weight_previous, weight_len_squared;
2687 int div_min, div_max, div_len;
2688 int vscroll_hor_area, vscroll_ver_area;
2689
2690 int len, weight_prev_x, weight_prev_y;
2691 int div_max_x, div_max_y, div_x, div_y;
2692
2693 /* Read sysctl. */
2694 /* XXX Verify values? */
2695 max_width = sc->syninfo.max_width;
2696 max_pressure = sc->syninfo.max_pressure;
2697 margin_top = sc->syninfo.margin_top;
2698 margin_right = sc->syninfo.margin_right;
2699 margin_bottom = sc->syninfo.margin_bottom;
2700 margin_left = sc->syninfo.margin_left;
2701 na_top = sc->syninfo.na_top;
2702 na_right = sc->syninfo.na_right;
2703 na_bottom = sc->syninfo.na_bottom;
2704 na_left = sc->syninfo.na_left;
2705 window_min = sc->syninfo.window_min;
2706 window_max = sc->syninfo.window_max;
2707 multiplicator = sc->syninfo.multiplicator;
2708 weight_current = sc->syninfo.weight_current;
2709 weight_previous = sc->syninfo.weight_previous;
2710 weight_len_squared = sc->syninfo.weight_len_squared;
2711 div_min = sc->syninfo.div_min;
2712 div_max = sc->syninfo.div_max;
2713 div_len = sc->syninfo.div_len;
2714 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
2715 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
2716
2717 /* Palm detection. */
2718 if (!(
2719 (sc->synhw.capMultiFinger && (w == 0 || w == 1)) ||
2720 (sc->synhw.capPalmDetect && w >= 4 && w <= max_width) ||
2721 (!sc->synhw.capPalmDetect && *z <= max_pressure) ||
2722 (sc->synhw.capPen && w == 2))) {
2723 /*
2724 * We consider the packet irrelevant for the current
2725 * action when:
2726 * - the width isn't comprised in:
2727 * [4; max_width]
2728 * - the pressure isn't comprised in:
2729 * [min_pressure; max_pressure]
2730 * - pen aren't supported but w is 2
2731 *
2732 * Note that this doesn't terminate the current action.
2733 */
2734 VLOG(2, (LOG_DEBUG,
2735 "synaptics: palm detected! (%d)\n", w));
2736 goto SYNAPTICS_END;
2737 }
2738
2739 /* Read current absolute position. */
2740 x0 = ((pb->ipacket[3] & 0x10) << 8) |
2741 ((pb->ipacket[1] & 0x0f) << 8) |
2742 pb->ipacket[4];
2743 y0 = ((pb->ipacket[3] & 0x20) << 7) |
2744 ((pb->ipacket[1] & 0xf0) << 4) |
2745 pb->ipacket[5];
2746
2747 synaction = &(sc->synaction);
2748
2749 /*
2750 * If the action is just beginning, init the structure and
2751 * compute tap timeout.
2752 */
2753 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
2754 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
2755
2756 /* Store the first point of this action. */
2757 synaction->start_x = x0;
2758 synaction->start_y = y0;
2759 dx = dy = 0;
2760
2761 /* Initialize queue. */
2762 synaction->queue_cursor = SYNAPTICS_PACKETQUEUE;
2763 synaction->queue_len = 0;
2764 synaction->window_min = window_min;
2765
2766 /* Reset average. */
2767 synaction->avg_dx = 0;
2768 synaction->avg_dy = 0;
2769
2770 /* Reset squelch. */
2771 synaction->squelch_x = 0;
2772 synaction->squelch_y = 0;
2773
2774 /* Reset pressure peak. */
2775 sc->zmax = 0;
2776
2777 /* Reset fingers count. */
2778 synaction->fingers_nb = 0;
2779
2780 /* Reset virtual scrolling state. */
2781 synaction->in_vscroll = 0;
2782
2783 /* Compute tap timeout. */
2784 sc->taptimeout.tv_sec = tap_timeout / 1000000;
2785 sc->taptimeout.tv_usec = tap_timeout % 1000000;
2786 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
2787
2788 sc->flags |= PSM_FLAGS_FINGERDOWN;
2789 } else {
2790 /* Calculate the current delta. */
2791 cursor = synaction->queue_cursor;
2792 dx = x0 - synaction->queue[cursor].x;
2793 dy = y0 - synaction->queue[cursor].y;
2794 }
2795
2796 /* If in tap-hold, add the recorded button. */
2797 if (synaction->in_taphold)
2798 ms->button |= synaction->tap_button;
2799
2800 /*
2801 * From now on, we can use the SYNAPTICS_END label to skip
2802 * the current packet.
2803 */
2804
2805 /*
2806 * Limit the coordinates to the specified margins because
2807 * this area isn't very reliable.
2808 */
2809 if (x0 <= margin_left)
2810 x0 = margin_left;
2811 else if (x0 >= 6143 - margin_right)
2812 x0 = 6143 - margin_right;
2813 if (y0 <= margin_bottom)
2814 y0 = margin_bottom;
2815 else if (y0 >= 6143 - margin_top)
2816 y0 = 6143 - margin_top;
2817
2818 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
2819 x0, y0, *z, w));
2820
2821 /* Queue this new packet. */
2822 cursor = SYNAPTICS_QUEUE_CURSOR(synaction->queue_cursor - 1);
2823 synaction->queue[cursor].x = x0;
2824 synaction->queue[cursor].y = y0;
2825 synaction->queue_cursor = cursor;
2826 if (synaction->queue_len < SYNAPTICS_PACKETQUEUE)
2827 synaction->queue_len++;
2828 VLOG(5, (LOG_DEBUG,
2829 "synaptics: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
2830 cursor, x0, y0, dx, dy));
2831
2832 /*
2833 * For tap, we keep the maximum number of fingers and the
2834 * pressure peak. Also with multiple fingers, we increase
2835 * the minimum window.
2836 */
2837 switch (w) {
2838 case 1: /* Three or more fingers. */
2839 synaction->fingers_nb = imax(3, synaction->fingers_nb);
2840 synaction->window_min = window_max;
2841 break;
2842 case 0: /* Two fingers. */
2843 synaction->fingers_nb = imax(2, synaction->fingers_nb);
2844 synaction->window_min = window_max;
2845 break;
2846 default: /* One finger or undetectable. */
2847 synaction->fingers_nb = imax(1, synaction->fingers_nb);
2848 }
2849 sc->zmax = imax(*z, sc->zmax);
2850
2851 /* Do we have enough packets to consider this a movement? */
2852 if (synaction->queue_len < synaction->window_min)
2853 goto SYNAPTICS_END;
2854
2855 /* Is a scrolling action occuring? */
2856 if (!synaction->in_taphold && !synaction->in_vscroll) {
2857 /*
2858 * A scrolling action must not conflict with a tap
2859 * action. Here are the conditions to consider a
2860 * scrolling action:
2861 * - the action in a configurable area
2862 * - one of the following:
2863 * . the distance between the last packet and the
2864 * first should be above a configurable minimum
2865 * . tap timed out
2866 */
2867 dxp = abs(synaction->queue[synaction->queue_cursor].x -
2868 synaction->start_x);
2869 dyp = abs(synaction->queue[synaction->queue_cursor].y -
2870 synaction->start_y);
2871
2872 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, >) ||
2873 dxp >= sc->syninfo.vscroll_min_delta ||
2874 dyp >= sc->syninfo.vscroll_min_delta) {
2875 /* Check for horizontal scrolling. */
2876 if ((vscroll_hor_area > 0 &&
2877 synaction->start_y <= vscroll_hor_area) ||
2878 (vscroll_hor_area < 0 &&
2879 synaction->start_y >=
2880 6143 + vscroll_hor_area))
2881 synaction->in_vscroll += 2;
2882
2883 /* Check for vertical scrolling. */
2884 if ((vscroll_ver_area > 0 &&
2885 synaction->start_x <= vscroll_ver_area) ||
2886 (vscroll_ver_area < 0 &&
2887 synaction->start_x >=
2888 6143 + vscroll_ver_area))
2889 synaction->in_vscroll += 1;
2890
2891 /* Avoid conflicts if area overlaps. */
2892 if (synaction->in_vscroll == 3)
2893 synaction->in_vscroll =
2894 (dxp > dyp) ? 2 : 1;
2895 }
2896 VLOG(5, (LOG_DEBUG,
2897 "synaptics: virtual scrolling: %s "
2898 "(direction=%d, dxp=%d, dyp=%d)\n",
2899 synaction->in_vscroll ? "YES" : "NO",
2900 synaction->in_vscroll, dxp, dyp));
2901 }
2902
2903 weight_prev_x = weight_prev_y = weight_previous;
2904 div_max_x = div_max_y = div_max;
2905
2906 if (synaction->in_vscroll) {
2907 /* Dividers are different with virtual scrolling. */
2908 div_min = sc->syninfo.vscroll_div_min;
2909 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
2910 } else {
2911 /*
2912 * There's a lot of noise in coordinates when
2913 * the finger is on the touchpad's borders. When
2914 * using this area, we apply a special weight and
2915 * div.
2916 */
2917 if (x0 <= na_left || x0 >= 6143 - na_right) {
2918 weight_prev_x = sc->syninfo.weight_previous_na;
2919 div_max_x = sc->syninfo.div_max_na;
2920 }
2921
2922 if (y0 <= na_bottom || y0 >= 6143 - na_top) {
2923 weight_prev_y = sc->syninfo.weight_previous_na;
2924 div_max_y = sc->syninfo.div_max_na;
2925 }
2926 }
2927
2928 /*
2929 * Calculate weights for the average operands and
2930 * the divisor. Both depend on the distance between
2931 * the current packet and a previous one (based on the
2932 * window width).
2933 */
2934 window = imin(synaction->queue_len, window_max);
2935 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
2936 dxp = abs(x0 - synaction->queue[peer].x) + 1;
2937 dyp = abs(y0 - synaction->queue[peer].y) + 1;
2938 len = (dxp * dxp) + (dyp * dyp);
2939 weight_prev_x = imin(weight_prev_x,
2940 weight_len_squared * weight_prev_x / len);
2941 weight_prev_y = imin(weight_prev_y,
2942 weight_len_squared * weight_prev_y / len);
2943
2944 len = (dxp + dyp) / 2;
2945 div_x = div_len * div_max_x / len;
2946 div_x = imin(div_max_x, div_x);
2947 div_x = imax(div_min, div_x);
2948 div_y = div_len * div_max_y / len;
2949 div_y = imin(div_max_y, div_y);
2950 div_y = imax(div_min, div_y);
2951
2952 VLOG(3, (LOG_DEBUG,
2953 "synaptics: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
2954 peer, len, weight_prev_x, weight_prev_y, div_x, div_y));
2955
2956 /* Compute averages. */
2957 synaction->avg_dx =
2958 (weight_current * dx * multiplicator +
2959 weight_prev_x * synaction->avg_dx) /
2960 (weight_current + weight_prev_x);
2961
2962 synaction->avg_dy =
2963 (weight_current * dy * multiplicator +
2964 weight_prev_y * synaction->avg_dy) /
2965 (weight_current + weight_prev_y);
2966
2967 VLOG(5, (LOG_DEBUG,
2968 "synaptics: avg_dx~=%d, avg_dy~=%d\n",
2969 synaction->avg_dx / multiplicator,
2970 synaction->avg_dy / multiplicator));
2971
2972 /* Use these averages to calculate x & y. */
2973 synaction->squelch_x += synaction->avg_dx;
2974 *x = synaction->squelch_x / (div_x * multiplicator);
2975 synaction->squelch_x = synaction->squelch_x %
2976 (div_x * multiplicator);
2977
2978 synaction->squelch_y += synaction->avg_dy;
2979 *y = synaction->squelch_y / (div_y * multiplicator);
2980 synaction->squelch_y = synaction->squelch_y %
2981 (div_y * multiplicator);
2982
2983 if (synaction->in_vscroll) {
2984 switch(synaction->in_vscroll) {
2985 case 1: /* Vertical scrolling. */
2986 if (*y != 0)
2987 ms->button |= (*y > 0) ?
2988 MOUSE_BUTTON4DOWN :
2989 MOUSE_BUTTON5DOWN;
2990 break;
2991 case 2: /* Horizontal scrolling. */
2992 if (*x != 0)
2993 ms->button |= (*x > 0) ?
2994 MOUSE_BUTTON7DOWN :
2995 MOUSE_BUTTON6DOWN;
2996 break;
2997 }
2998
2999 /* The pointer is not moved. */
3000 *x = *y = 0;
3001 } else {
3002 VLOG(3, (LOG_DEBUG, "synaptics: [%d, %d] -> [%d, %d]\n",
3003 dx, dy, *x, *y));
3004 }
3005 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3006 /*
3007 * An action is currently taking place but the pressure
3008 * dropped under the minimum, putting an end to it.
3009 */
3010 synapticsaction_t *synaction;
3011 int taphold_timeout, dx, dy, tap_max_delta;
3012
3013 synaction = &(sc->synaction);
3014 dx = abs(synaction->queue[synaction->queue_cursor].x -
3015 synaction->start_x);
3016 dy = abs(synaction->queue[synaction->queue_cursor].y -
3017 synaction->start_y);
3018
3019 /* Max delta is disabled for multi-fingers tap. */
3020 if (synaction->fingers_nb > 1)
3021 tap_max_delta = imax(dx, dy);
3022 else
3023 tap_max_delta = sc->syninfo.tap_max_delta;
3024
3025 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3026
3027 /* Check for tap. */
3028 VLOG(3, (LOG_DEBUG,
3029 "synaptics: zmax=%d, dx=%d, dy=%d, "
3030 "delta=%d, fingers=%d, queue=%d\n",
3031 sc->zmax, dx, dy, tap_max_delta, synaction->fingers_nb,
3032 synaction->queue_len));
3033 if (!synaction->in_vscroll && sc->zmax >= tap_threshold &&
3034 timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=) &&
3035 dx <= tap_max_delta && dy <= tap_max_delta &&
3036 synaction->queue_len >= sc->syninfo.tap_min_queue) {
3037 /*
3038 * We have a tap if:
3039 * - the maximum pressure went over tap_threshold
3040 * - the action ended before tap_timeout
3041 *
3042 * To handle tap-hold, we must delay any button push to
3043 * the next action.
3044 */
3045 if (synaction->in_taphold) {
3046 /*
3047 * This is the second and last tap of a
3048 * double tap action, not a tap-hold.
3049 */
3050 synaction->in_taphold = 0;
3051
3052 /*
3053 * For double-tap to work:
3054 * - no button press is emitted (to
3055 * simulate a button release)
3056 * - PSM_FLAGS_FINGERDOWN is set to
3057 * force the next packet to emit a
3058 * button press)
3059 */
3060 VLOG(2, (LOG_DEBUG,
3061 "synaptics: button RELEASE: %d\n",
3062 synaction->tap_button));
3063 sc->flags |= PSM_FLAGS_FINGERDOWN;
3064 } else {
3065 /*
3066 * This is the first tap: we set the
3067 * tap-hold state and notify the button
3068 * down event.
3069 */
3070 synaction->in_taphold = 1;
3071 taphold_timeout = sc->syninfo.taphold_timeout;
3072 sc->taptimeout.tv_sec = taphold_timeout /
3073 1000000;
3074 sc->taptimeout.tv_usec = taphold_timeout %
3075 1000000;
3076 timevaladd(&sc->taptimeout, &sc->lastsoftintr);
3077
3078 switch (synaction->fingers_nb) {
3079 case 3:
3080 synaction->tap_button =
3081 MOUSE_BUTTON2DOWN;
3082 break;
3083 case 2:
3084 synaction->tap_button =
3085 MOUSE_BUTTON3DOWN;
3086 break;
3087 default:
3088 synaction->tap_button =
3089 MOUSE_BUTTON1DOWN;
3090 }
3091 VLOG(2, (LOG_DEBUG,
3092 "synaptics: button PRESS: %d\n",
3093 synaction->tap_button));
3094 ms->button |= synaction->tap_button;
3095 }
3096 } else {
3097 /*
3098 * Not enough pressure or timeout: reset
3099 * tap-hold state.
3100 */
3101 if (synaction->in_taphold) {
3102 VLOG(2, (LOG_DEBUG,
3103 "synaptics: button RELEASE: %d\n",
3104 synaction->tap_button));
3105 synaction->in_taphold = 0;
3106 } else {
3107 VLOG(2, (LOG_DEBUG,
3108 "synaptics: not a tap-hold\n"));
3109 }
3110 }
3111 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) &&
3112 sc->synaction.in_taphold) {
3113 /*
3114 * For a tap-hold to work, the button must remain down at
3115 * least until timeout (where the in_taphold flags will be
3116 * cleared) or during the next action.
3117 */
3118 if (timevalcmp(&sc->lastsoftintr, &sc->taptimeout, <=)) {
3119 ms->button |= sc->synaction.tap_button;
3120 } else {
3121 VLOG(2, (LOG_DEBUG,
3122 "synaptics: button RELEASE: %d\n",
3123 sc->synaction.tap_button));
3124 sc->synaction.in_taphold = 0;
3125 }
3126 }
3127
3128SYNAPTICS_END:
3129 /*
3130 * Use the extra buttons as a scrollwheel
3131 *
3132 * XXX X.Org uses the Z axis for vertical wheel only,
3133 * whereas moused(8) understands special values to differ
3134 * vertical and horizontal wheels.
3135 *
3136 * xf86-input-mouse needs therefore a small patch to
3137 * understand these special values. Without it, the
3138 * horizontal wheel acts as a vertical wheel in X.Org.
3139 *
3140 * That's why the horizontal wheel is disabled by
3141 * default for now.
3142 */
3143 if (ms->button & MOUSE_BUTTON4DOWN) {
3144 *z = -1;
3145 ms->button &= ~MOUSE_BUTTON4DOWN;
3146 } else if (ms->button & MOUSE_BUTTON5DOWN) {
3147 *z = 1;
3148 ms->button &= ~MOUSE_BUTTON5DOWN;
3149 } else if (ms->button & MOUSE_BUTTON6DOWN) {
3150 *z = -2;
3151 ms->button &= ~MOUSE_BUTTON6DOWN;
3152 } else if (ms->button & MOUSE_BUTTON7DOWN) {
3153 *z = 2;
3154 ms->button &= ~MOUSE_BUTTON7DOWN;
3155 } else
3156 *z = 0;
3157
3158 return (0);
3159}
3160
3161static void
3162proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3163 int *x, int *y, int *z)
3164{
3165 static int butmap_versapad[8] = {
3166 0,
3167 MOUSE_BUTTON3DOWN,
3168 0,
3169 MOUSE_BUTTON3DOWN,
3170 MOUSE_BUTTON1DOWN,
3171 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3172 MOUSE_BUTTON1DOWN,
3173 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
3174 };
3175 int c, x0, y0;
3176
3177 /* VersaPad PS/2 absolute mode message format
3178 *
3179 * [packet1] 7 6 5 4 3 2 1 0(LSB)
3180 * ipacket[0]: 1 1 0 A 1 L T R
3181 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
3182 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
3183 * ipacket[3]: 1 1 1 A 1 L T R
3184 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
3185 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
3186 *
3187 * [note]
3188 * R: right physical mouse button (1=on)
3189 * T: touch pad virtual button (1=tapping)
3190 * L: left physical mouse button (1=on)
3191 * A: position data is valid (1=valid)
3192 * H: horizontal data (12bit signed integer. H11 is sign bit.)
3193 * V: vertical data (12bit signed integer. V11 is sign bit.)
3194 * P: pressure data
3195 *
3196 * Tapping is mapped to MOUSE_BUTTON4.
3197 */
3198 c = pb->ipacket[0];
3199 *x = *y = 0;
3200 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
3201 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
3202 if (c & MOUSE_PS2VERSA_IN_USE) {
3203 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
3204 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
3205 if (x0 & 0x800)
3206 x0 -= 0x1000;
3207 if (y0 & 0x800)
3208 y0 -= 0x1000;
3209 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3210 *x = sc->xold - x0;
3211 *y = y0 - sc->yold;
3212 if (*x < 0) /* XXX */
3213 ++*x;
3214 else if (*x)
3215 --*x;
3216 if (*y < 0)
3217 ++*y;
3218 else if (*y)
3219 --*y;
3220 } else
3221 sc->flags |= PSM_FLAGS_FINGERDOWN;
3222 sc->xold = x0;
3223 sc->yold = y0;
3224 } else
3225 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
984263bc
MD
3226}
3227
3228static void
91be4d71 3229psmsoftintr(void *arg)
984263bc 3230{
91be4d71
AHJ
3231 /*
3232 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
3233 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
3234 */
3235 static int butmap[8] = {
3236 0,
3237 MOUSE_BUTTON1DOWN,
3238 MOUSE_BUTTON3DOWN,
3239 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
3240 MOUSE_BUTTON2DOWN,
3241 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
3242 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
3243 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
3244 };
3245 struct psm_softc *sc = arg;
3246 mousestatus_t ms;
3247 packetbuf_t *pb;
3248 int x, y, z, c, l;
3249
3250 getmicrouptime(&sc->lastsoftintr);
984263bc 3251
91be4d71 3252 crit_enter();
984263bc 3253
91be4d71
AHJ
3254 do {
3255 pb = &sc->pqueue[sc->pqueue_start];
3256
3257 if (sc->mode.level == PSM_LEVEL_NATIVE)
3258 goto next_native;
3259
3260 c = pb->ipacket[0];
3261 /*
3262 * A kludge for Kensington device!
3263 * The MSB of the horizontal count appears to be stored in
3264 * a strange place.
3265 */
3266 if (sc->hw.model == MOUSE_MODEL_THINK)
3267 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
3268
3269 /* ignore the overflow bits... */
3270 x = (c & MOUSE_PS2_XNEG) ?
3271 pb->ipacket[1] - 256 : pb->ipacket[1];
3272 y = (c & MOUSE_PS2_YNEG) ?
3273 pb->ipacket[2] - 256 : pb->ipacket[2];
984263bc 3274 z = 0;
91be4d71
AHJ
3275 ms.obutton = sc->button; /* previous button state */
3276 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
3277 /* `tapping' action */
3278 if (sc->config & PSM_CONFIG_FORCETAP)
3279 ms.button |= ((c & MOUSE_PS2_TAP)) ?
3280 0 : MOUSE_BUTTON4DOWN;
3281
3282 switch (sc->hw.model) {
3283
3284 case MOUSE_MODEL_EXPLORER:
3285 /*
3286 * b7 b6 b5 b4 b3 b2 b1 b0
3287 * byte 1: oy ox sy sx 1 M R L
3288 * byte 2: x x x x x x x x
3289 * byte 3: y y y y y y y y
3290 * byte 4: * * S2 S1 s d2 d1 d0
3291 *
3292 * L, M, R, S1, S2: left, middle, right and side buttons
3293 * s: wheel data sign bit
3294 * d2-d0: wheel data
3295 */
3296 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
3297 (pb->ipacket[3] & 0x0f) - 16 :
3298 (pb->ipacket[3] & 0x0f);
3299 ms.button |=
3300 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
3301 MOUSE_BUTTON4DOWN : 0;
3302 ms.button |=
3303 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
3304 MOUSE_BUTTON5DOWN : 0;
3305 break;
3306
3307 case MOUSE_MODEL_INTELLI:
3308 case MOUSE_MODEL_NET:
3309 /* wheel data is in the fourth byte */
3310 z = (char)pb->ipacket[3];
3311 /*
3312 * XXX some mice may send 7 when there is no Z movement? */
3313 if ((z >= 7) || (z <= -7))
3314 z = 0;
3315 /* some compatible mice have additional buttons */
3316 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
3317 MOUSE_BUTTON4DOWN : 0;
3318 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
3319 MOUSE_BUTTON5DOWN : 0;
3320 break;
3321
3322 case MOUSE_MODEL_MOUSEMANPLUS:
3323 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
3324 break;
3325
3326 case MOUSE_MODEL_GLIDEPOINT:
3327 /* `tapping' action */
3328 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
3329 MOUSE_BUTTON4DOWN;
3330 break;
3331
3332 case MOUSE_MODEL_NETSCROLL:
3333 /*
3334 * three addtional bytes encode buttons and
3335 * wheel events
3336 */
3337 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
3338 MOUSE_BUTTON4DOWN : 0;
3339 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
3340 MOUSE_BUTTON5DOWN : 0;
3341 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
3342 pb->ipacket[4] - 256 : pb->ipacket[4];
3343 break;
3344
3345 case MOUSE_MODEL_THINK:
3346 /* the fourth button state in the first byte */
3347 ms.button |= (c & MOUSE_PS2_TAP) ?
3348 MOUSE_BUTTON4DOWN : 0;
3349 break;
3350
3351 case MOUSE_MODEL_VERSAPAD:
3352 proc_versapad(sc, pb, &ms, &x, &y, &z);
3353 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
3354 ((y < 0) ? MOUSE_PS2_YNEG : 0);
3355 break;
3356
3357 case MOUSE_MODEL_4D:
3358 /*
3359 * b7 b6 b5 b4 b3 b2 b1 b0
3360 * byte 1: s2 d2 s1 d1 1 M R L
3361 * byte 2: sx x x x x x x x
3362 * byte 3: sy y y y y y y y
3363 *
3364 * s1: wheel 1 direction
3365 * d1: wheel 1 data
3366 * s2: wheel 2 direction
3367 * d2: wheel 2 data
3368 */
3369 x = (pb->ipacket[1] & 0x80) ?
3370 pb->ipacket[1] - 256 : pb->ipacket[1];
3371 y = (pb->ipacket[2] & 0x80) ?
3372 pb->ipacket[2] - 256 : pb->ipacket[2];
3373 switch (c & MOUSE_4D_WHEELBITS) {
3374 case 0x10:
3375 z = 1;
3376 break;
3377 case 0x30:
3378 z = -1;
3379 break;
3380 case 0x40: /* XXX 2nd wheel turning right */
3381 z = 2;
3382 break;
3383 case 0xc0: /* XXX 2nd wheel turning left */
3384 z = -2;
3385 break;
3386 }
3387 break;
3388
3389 case MOUSE_MODEL_4DPLUS:
3390 if ((x < 16 - 256) && (y < 16 - 256)) {
3391 /*
3392 * b7 b6 b5 b4 b3 b2 b1 b0
3393 * byte 1: 0 0 1 1 1 M R L
3394 * byte 2: 0 0 0 0 1 0 0 0
3395 * byte 3: 0 0 0 0 S s d1 d0
3396 *
3397 * L, M, R, S: left, middle, right,
3398 * and side buttons
3399 * s: wheel data sign bit
3400 * d1-d0: wheel data
3401 */
3402 x = y = 0;
3403 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
3404 ms.button |= MOUSE_BUTTON4DOWN;
3405 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
3406 ((pb->ipacket[2] & 0x07) - 8) :
3407 (pb->ipacket[2] & 0x07) ;
3408 } else {
3409 /* preserve previous button states */
3410 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
3411 }
3412 break;
3413
3414 case MOUSE_MODEL_SYNAPTICS:
3415 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0)
3416 goto next;
3417 break;
3418
3419 case MOUSE_MODEL_GENERIC:
984263bc 3420 default:
91be4d71 3421 break;
984263bc 3422 }
91be4d71
AHJ
3423
3424 /* scale values */
3425 if (sc->mode.accelfactor >= 1) {
3426 if (x != 0) {
3427 x = x * x / sc->mode.accelfactor;
3428 if (x == 0)
3429 x = 1;
3430 if (c & MOUSE_PS2_XNEG)
3431 x = -x;
984263bc 3432 }
91be4d71
AHJ
3433 if (y != 0) {
3434 y = y * y / sc->mode.accelfactor;
3435 if (y == 0)
3436 y = 1;
3437 if (c & MOUSE_PS2_YNEG)
3438 y = -y;
3439 }
3440 }
984263bc 3441
91be4d71
AHJ
3442 ms.dx = x;
3443 ms.dy = y;
3444 ms.dz = z;
3445 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
3446 (ms.obutton ^ ms.button);
984263bc 3447
91be4d71 3448 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
984263bc 3449
91be4d71
AHJ
3450 sc->status.flags |= ms.flags;
3451 sc->status.dx += ms.dx;
3452 sc->status.dy += ms.dy;
3453 sc->status.dz += ms.dz;
3454 sc->status.button = ms.button;
3455 sc->button = ms.button;
984263bc 3456
91be4d71 3457next_native:
984263bc
MD
3458 sc->watchdog = FALSE;
3459
91be4d71
AHJ
3460 /* queue data */
3461 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
3462 l = imin(pb->inputbytes,
3463 sizeof(sc->queue.buf) - sc->queue.tail);
3464 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
3465 if (pb->inputbytes > l)
3466 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
3467 pb->inputbytes - l);
3468 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
3469 sizeof(sc->queue.buf);
3470 sc->queue.count += pb->inputbytes;
3471 }
3472 pb->inputbytes = 0;
3473
3474next:
3475 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
3476 sc->pqueue_start = 0;
3477 } while (sc->pqueue_start != sc->pqueue_end);
3478
3479 if (sc->state & PSM_ASLP) {
3480 sc->state &= ~PSM_ASLP;
3481 wakeup(sc);
3482 }
5b22f1a7 3483 KNOTE(&sc->rkq.ki_note, 0);
91be4d71
AHJ
3484
3485 sc->state &= ~PSM_SOFTARMED;
3486 crit_exit();
984263bc
MD
3487}
3488
0de295ce 3489static struct filterops psmfiltops =
4c91dbc9 3490 { FILTEROP_ISFD, NULL, psmfilter_detach, psmfilter };
0de295ce
SG
3491
3492static int
3493psmkqfilter(struct dev_kqfilter_args *ap)
3494{
3495 cdev_t dev = ap->a_head.a_dev;
3496 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
3497 struct knote *kn = ap->a_kn;
3498 struct klist *klist;
3499
3500 ap->a_result = 0;
3501
3502 switch (kn->kn_filter) {
91be4d71
AHJ
3503 case EVFILT_READ:
3504 kn->kn_fop = &psmfiltops;
3505 kn->kn_hook = (caddr_t)sc;
3506 break;
3507 default:
3508 ap->a_result = EOPNOTSUPP;
3509 return (0);
0de295ce
SG
3510 }
3511
5b22f1a7
SG
3512 klist = &sc->rkq.ki_note;
3513 knote_insert(klist, kn);
0de295ce
SG
3514
3515 return (0);
3516}
3517
3518static void
3519psmfilter_detach(struct knote *kn)
3520{
3521 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
3522 struct klist *klist;
3523
5b22f1a7
SG
3524 klist = &sc->rkq.ki_note;
3525 knote_remove(klist, kn);
0de295ce
SG
3526}
3527
3528static int
3529psmfilter(struct knote *kn, long hint)
3530{
3531 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook;
3532 int ready = 0;
3533
3534 crit_enter();
3535 if (sc->queue.count > 0)
3536 ready = 1;
3537 crit_exit();
3538
3539 return (ready);
3540}
3541
984263bc
MD
3542/* vendor/model specific routines */
3543
3544static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
3545{
91be4d71
AHJ
3546 if (set_mouse_resolution(kbdc, res) != res)
3547 return (FALSE);
3548 if (set_mouse_scaling(kbdc, scale) &&
3549 set_mouse_scaling(kbdc, scale) &&
3550 set_mouse_scaling(kbdc, scale) &&
3551 (get_mouse_status(kbdc, status, 0, 3) >= 3))
3552 return (TRUE);
3553 return (FALSE);
984263bc
MD
3554}
3555
91be4d71 3556static int
984263bc
MD
3557mouse_ext_command(KBDC kbdc, int command)
3558{
91be4d71
AHJ
3559 int c;
3560
3561 c = (command >> 6) & 0x03;
3562 if (set_mouse_resolution(kbdc, c) != c)
3563 return (FALSE);
3564 c = (command >> 4) & 0x03;
3565 if (set_mouse_resolution(kbdc, c) != c)
3566 return (FALSE);
3567 c = (command >> 2) & 0x03;
3568 if (set_mouse_resolution(kbdc, c) != c)
3569 return (FALSE);
3570 c = (command >> 0) & 0x03;
3571 if (set_mouse_resolution(kbdc, c) != c)
3572 return (FALSE);
3573 return (TRUE);
984263bc
MD
3574}
3575
91be4d71 3576#ifdef notyet
984263bc
MD
3577/* Logitech MouseMan Cordless II */
3578static int
3579enable_lcordless(struct psm_softc *sc)
3580{
91be4d71
AHJ
3581 int status[3];
3582 int ch;
3583
3584 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
3585 return (FALSE);
3586 if (status[1] == PSMD_RES_HIGH)
3587 return (FALSE);
3588 ch = (status[0] & 0x07) - 1; /* channel # */
3589 if ((ch <= 0) || (ch > 4))
3590 return (FALSE);
3591 /*
3592 * status[1]: always one?
3593 * status[2]: battery status? (0-100)
3594 */
3595 return (TRUE);
984263bc
MD
3596}
3597#endif /* notyet */
3598
3599/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
3600static int
3601enable_groller(struct psm_softc *sc)
3602{
91be4d71
AHJ
3603 int status[3];
3604
3605 /*
3606 * The special sequence to enable the fourth button and the
3607 * roller. Immediately after this sequence check status bytes.
3608 * if the mouse is NetScroll, the second and the third bytes are
3609 * '3' and 'D'.
3610 */
3611
3612 /*
3613 * If the mouse is an ordinary PS/2 mouse, the status bytes should
3614 * look like the following.
3615 *
3616 * byte 1 bit 7 always 0
3617 * bit 6 stream mode (0)
3618 * bit 5 disabled (0)
3619 * bit 4 1:1 scaling (0)
3620 * bit 3 always 0
3621 * bit 0-2 button status
3622 * byte 2 resolution (PSMD_RES_HIGH)
3623 * byte 3 report rate (?)
3624 */
3625
3626 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3627 return (FALSE);
3628 if ((status[1] != '3') || (status[2] != 'D'))
3629 return (FALSE);
3630 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
3631 sc->hw.buttons = 4;
3632 return (TRUE);
984263bc
MD
3633}
3634
3635/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
3636static int
3637enable_gmouse(struct psm_softc *sc)
3638{
91be4d71
AHJ
3639 int status[3];
3640
3641 /*
3642 * The special sequence to enable the middle, "rubber" button.
3643 * Immediately after this sequence check status bytes.
3644 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
3645 * the second and the third bytes are '3' and 'U'.
3646 * NOTE: NetMouse reports that it has three buttons although it has
3647 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
3648 * say they have three buttons too and they do have a button on the
3649 * side...
3650 */
3651 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
3652 return (FALSE);
3653 if ((status[1] != '3') || (status[2] != 'U'))
3654 return (FALSE);
3655 return (TRUE);
984263bc
MD
3656}
3657
3658/* ALPS GlidePoint */
3659static int
3660enable_aglide(struct psm_softc *sc)
3661{
91be4d71
AHJ
3662 int status[3];
3663
3664 /*
3665 * The special sequence to obtain ALPS GlidePoint specific
3666 * information. Immediately after this sequence, status bytes will
3667 * contain something interesting.
3668 * NOTE: ALPS produces several models of GlidePoint. Some of those
3669 * do not respond to this sequence, thus, cannot be detected this way.
3670 */
3671 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
3672 return (FALSE);
3673 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
3674 return (FALSE);
3675 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
3676 return (FALSE);
3677 return (TRUE);
984263bc
MD
3678}
3679
3680/* Kensington ThinkingMouse/Trackball */
3681static int
3682enable_kmouse(struct psm_softc *sc)
3683{
91be4d71
AHJ
3684 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
3685 KBDC kbdc = sc->kbdc;
3686 int status[3];
3687 int id1;
3688 int id2;
3689 int i;
3690
3691 id1 = get_aux_id(kbdc);
3692 if (set_mouse_sampling_rate(kbdc, 10) != 10)
3693 return (FALSE);
3694 /*
3695 * The device is now in the native mode? It returns a different
3696 * ID value...
3697 */
3698 id2 = get_aux_id(kbdc);
3699 if ((id1 == id2) || (id2 != 2))
3700 return (FALSE);
3701
3702 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
3703 return (FALSE);
984263bc 3704#if PSM_DEBUG >= 2
91be4d71
AHJ
3705 /* at this point, resolution is LOW, sampling rate is 10/sec */
3706 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3707 return (FALSE);
984263bc
MD
3708#endif
3709
91be4d71
AHJ
3710 /*
3711 * The special sequence to enable the third and fourth buttons.
3712 * Otherwise they behave like the first and second buttons.
3713 */
c157ff7a 3714 for (i = 0; i < NELEM(rate); ++i)
91be4d71
AHJ
3715 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3716 return (FALSE);
3717
3718 /*
3719 * At this point, the device is using default resolution and
3720 * sampling rate for the native mode.
3721 */
3722 if (get_mouse_status(kbdc, status, 0, 3) < 3)
3723 return (FALSE);
3724 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
3725 return (FALSE);
3726
3727 /* the device appears be enabled by this sequence, diable it for now */
3728 disable_aux_dev(kbdc);
3729 empty_aux_buffer(kbdc, 5);
3730
3731 return (TRUE);
984263bc
MD
3732}
3733
3734/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
3735static int
3736enable_mmanplus(struct psm_softc *sc)
3737{
91be4d71
AHJ
3738 KBDC kbdc = sc->kbdc;
3739 int data[3];
3740
3741 /* the special sequence to enable the fourth button and the roller. */
3742 /*
3743 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
3744 * must be called exactly three times since the last RESET command
3745 * before this sequence. XXX
3746 */
3747 if (!set_mouse_scaling(kbdc, 1))
3748 return (FALSE);
3749 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
3750 return (FALSE);
3751 if (get_mouse_status(kbdc, data, 1, 3) < 3)
3752 return (FALSE);
3753
3754 /*
3755 * PS2++ protocl, packet type 0
3756 *
3757 * b7 b6 b5 b4 b3 b2 b1 b0
3758 * byte 1: * 1 p3 p2 1 * * *
3759 * byte 2: 1 1 p1 p0 m1 m0 1 0
3760 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
3761 *
3762 * p3-p0: packet type: 0
3763 * m7-m0: model ID: MouseMan+:0x50,
3764 * FirstMouse+:0x51,
3765 * ScrollPoint:0x58...
3766 */
3767 /* check constant bits */
3768 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
3769 return (FALSE);
3770 if ((data[1] & 0xc3) != 0xc2)
3771 return (FALSE);
3772 /* check d3-d0 in byte 2 */
3773 if (!MOUSE_PS2PLUS_CHECKBITS(data))
3774 return (FALSE);
3775 /* check p3-p0 */
3776 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
3777 return (FALSE);
3778
3779 sc->hw.hwid &= 0x00ff;
3780 sc->hw.hwid |= data[2] << 8; /* save model ID */
3781
3782 /*
3783 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
3784 * the wheel and the fourth button events are encoded in the
3785 * special data packet. The mouse may be put in the IntelliMouse mode
3786 * if it is initialized by the IntelliMouse's method.
3787 */
3788 return (TRUE);
984263bc
MD
3789}
3790
3791/* MS IntelliMouse Explorer */
3792static int
3793enable_msexplorer(struct psm_softc *sc)
3794{
91be4d71
AHJ
3795 static u_char rate0[] = { 200, 100, 80, };
3796 static u_char rate1[] = { 200, 200, 80, };
3797 KBDC kbdc = sc->kbdc;
3798 int id;
3799 int i;
3800
3801 /*
3802 * This is needed for at least A4Tech X-7xx mice - they do not go
3803 * straight to Explorer mode, but need to be set to Intelli mode
3804 * first.
3805 */
3806 enable_msintelli(sc);
3807
3808 /* the special sequence to enable the extra buttons and the roller. */
c157ff7a 3809 for (i = 0; i < NELEM(rate1); ++i)
91be4d71
AHJ
3810 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
3811 return (FALSE);
3812 /* the device will give the genuine ID only after the above sequence */
3813 id = get_aux_id(kbdc);
3814 if (id != PSM_EXPLORER_ID)
3815 return (FALSE);
3816
3817 sc->hw.hwid = id;
3818 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
3819
3820 /*
3821 * XXX: this is a kludge to fool some KVM switch products
3822 * which think they are clever enough to know the 4-byte IntelliMouse
3823 * protocol, and assume any other protocols use 3-byte packets.
3824 * They don't convey 4-byte data packets from the IntelliMouse Explorer
3825 * correctly to the host computer because of this!
3826 * The following sequence is actually IntelliMouse's "wake up"
3827 * sequence; it will make the KVM think the mouse is IntelliMouse
3828 * when it is in fact IntelliMouse Explorer.
3829 */
c157ff7a 3830 for (i = 0; i < NELEM(rate0); ++i)
91be4d71
AHJ
3831 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
3832 break;
3833 id = get_aux_id(kbdc);
3834
3835 return (TRUE);
984263bc
MD
3836}
3837
3838/* MS IntelliMouse */
3839static int
3840enable_msintelli(struct psm_softc *sc)
3841{
91be4d71
AHJ
3842 /*
3843 * Logitech MouseMan+ and FirstMouse+ will also respond to this
3844 * probe routine and act like IntelliMouse.
3845 */
3846
3847 static u_char rate[] = { 200, 100, 80, };
3848 KBDC kbdc = sc->kbdc;
3849 int id;
3850 int i;
3851
3852 /* the special sequence to enable the third button and the roller. */
c157ff7a 3853 for (i = 0; i < NELEM(rate); ++i)
91be4d71
AHJ
3854 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3855 return (FALSE);
3856 /* the device will give the genuine ID only after the above sequence */
3857 id = get_aux_id(kbdc);
3858 if (id != PSM_INTELLI_ID)
3859 return (FALSE);
3860
3861 sc->hw.hwid = id;
3862 sc->hw.buttons = 3;
3863
3864 return (TRUE);
984263bc
MD
3865}
3866
3867/* A4 Tech 4D Mouse */
3868static int
3869enable_4dmouse(struct psm_softc *sc)
3870{
91be4d71
AHJ
3871 /*
3872 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
3873 */
3874
3875 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3876 KBDC kbdc = sc->kbdc;
3877 int id;
3878 int i;
3879
c157ff7a 3880 for (i = 0; i < NELEM(rate); ++i)
91be4d71
AHJ
3881 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3882 return (FALSE);
3883 id = get_aux_id(kbdc);
3884 /*
3885 * WinEasy 4D, 4 Way Scroll 4D: 6
3886 * Cable-Free 4D: 8 (4DPLUS)
3887 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
3888 */
3889 if (id != PSM_4DMOUSE_ID)
3890 return (FALSE);
3891
3892 sc->hw.hwid = id;
3893 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
3894
3895 return (TRUE);
984263bc
MD
3896}
3897
3898/* A4 Tech 4D+ Mouse */
3899static int
3900enable_4dplus(struct psm_softc *sc)
3901{
91be4d71
AHJ
3902 /*
3903 * Newer wheel mice from A4 Tech seem to use this protocol.
3904 * Older models are recognized as either 4D Mouse or IntelliMouse.
3905 */
3906 KBDC kbdc = sc->kbdc;
3907 int id;
3908
3909 /*
3910 * enable_4dmouse() already issued the following ID sequence...
3911 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
3912 int i;
3913
c157ff7a 3914 for (i = 0; i < NELEM(rate); ++i)
91be4d71
AHJ
3915 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
3916 return (FALSE);
3917 */
3918
3919 id = get_aux_id(kbdc);
3920 switch (id) {
3921 case PSM_4DPLUS_ID:
3922 sc->hw.buttons = 4;
3923 break;
3924 case PSM_4DPLUS_RFSW35_ID:
3925 sc->hw.buttons = 3;
3926 break;
3927 default:
3928 return (FALSE);
3929 }
3930
3931 sc->hw.hwid = id;
3932
3933 return (TRUE);
3934}
3935
3936/* Synaptics Touchpad */
3937static int
3938synaptics_sysctl(SYSCTL_HANDLER_ARGS)
3939{
3940 int error, arg;
3941
3942 /* Read the current value. */
3943 arg = *(int *)oidp->oid_arg1;
3944 error = sysctl_handle_int(oidp, &arg, 0, req);
3945
3946 /* Sanity check. */
3947 if (error || !req->newptr)
3948 return (error);
3949
3950 /*
3951 * Check that the new value is in the concerned node's range
3952 * of values.
3953 */
3954 switch (oidp->oid_arg2) {
3955 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
3956 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
3957 if (arg < 0 || arg > 255)
3958 return (EINVAL);
3959 break;
3960 case SYNAPTICS_SYSCTL_MAX_WIDTH:
3961 if (arg < 4 || arg > 15)
3962 return (EINVAL);
3963 break;
3964 case SYNAPTICS_SYSCTL_MARGIN_TOP:
3965 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
3966 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
3967 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
3968 case SYNAPTICS_SYSCTL_NA_TOP:
3969 case SYNAPTICS_SYSCTL_NA_RIGHT:
3970 case SYNAPTICS_SYSCTL_NA_BOTTOM:
3971 case SYNAPTICS_SYSCTL_NA_LEFT:
3972 if (arg < 0 || arg > 6143)
3973 return (EINVAL);
3974 break;
3975 case SYNAPTICS_SYSCTL_WINDOW_MIN:
3976 case SYNAPTICS_SYSCTL_WINDOW_MAX:
3977 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
3978 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
3979 return (EINVAL);
3980 break;
3981 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
3982 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
3983 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
3984 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
3985 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
3986 case SYNAPTICS_SYSCTL_DIV_MIN:
3987 case SYNAPTICS_SYSCTL_DIV_MAX:
3988 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
3989 case SYNAPTICS_SYSCTL_DIV_LEN:
3990 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
3991 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
3992 if (arg < 1)
3993 return (EINVAL);
3994 break;
3995 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
3996 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
3997 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
3998 if (arg < 0)
3999 return (EINVAL);
4000 break;
4001 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
4002 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
4003 if (arg < -6143 || arg > 6143)
4004 return (EINVAL);
4005 break;
4006 default:
4007 return (EINVAL);
4008 }
4009
4010 /* Update. */
4011 *(int *)oidp->oid_arg1 = arg;
4012
4013 return (error);
4014}
4015
4016static void
4017synaptics_sysctl_create_tree(struct psm_softc *sc)
4018{
4019
4020 if (sc->syninfo.sysctl_tree != NULL)
4021 return;
4022
4023 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
4024 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
4025 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
4026 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "synaptics", CTLFLAG_RD,
4027 0, "Synaptics TouchPad");
4028
4029 /* hw.psm.synaptics.directional_scrolls. */
4030 sc->syninfo.directional_scrolls = 1;
4031 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
4032 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4033 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
4034 &sc->syninfo.directional_scrolls, 0,
4035 "Enable hardware scrolling pad (if non-zero) or register it as "
4036 "a middle-click (if 0)");
4037
4038 /* hw.psm.synaptics.min_pressure. */
4039 sc->syninfo.min_pressure = 16;
4040 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4041 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
4042 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
4043 &sc->syninfo.min_pressure, SYNAPTICS_SYSCTL_MIN_PRESSURE,
4044 synaptics_sysctl, "I",
4045 "Minimum pressure required to start an action");
4046
4047 /* hw.psm.synaptics.max_pressure. */
4048 sc->syninfo.max_pressure = 220;
4049 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
4050 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_