Deleted Added
full compact
psm.c (334763) psm.c (343158)
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.
22 */
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'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
59 */
60
61#include <sys/cdefs.h>
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.
22 */
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'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
59 */
60
61#include <sys/cdefs.h>
62__FBSDID("$FreeBSD: stable/11/sys/dev/atkbdc/psm.c 334763 2018-06-07 07:43:58Z hselasky $");
62__FBSDID("$FreeBSD: stable/11/sys/dev/atkbdc/psm.c 343158 2019-01-18 21:12:00Z wulf $");
63
64#include "opt_isa.h"
65#include "opt_psm.h"
66#include "opt_evdev.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/kernel.h>
71#include <sys/module.h>
72#include <sys/bus.h>
73#include <sys/conf.h>
74#include <sys/filio.h>
75#include <sys/poll.h>
76#include <sys/sigio.h>
77#include <sys/signalvar.h>
78#include <sys/syslog.h>
79#include <machine/bus.h>
80#include <sys/rman.h>
81#include <sys/selinfo.h>
82#include <sys/sysctl.h>
83#include <sys/time.h>
84#include <sys/uio.h>
85
86#include <sys/limits.h>
87#include <sys/mouse.h>
88#include <machine/resource.h>
89
90#ifdef DEV_ISA
91#include <isa/isavar.h>
92#endif
93
94#ifdef EVDEV_SUPPORT
95#include <dev/evdev/evdev.h>
96#include <dev/evdev/input.h>
97#endif
98
99#include <dev/atkbdc/atkbdcreg.h>
100#include <dev/atkbdc/psm.h>
101
102/*
103 * Driver specific options: the following options may be set by
104 * `options' statements in the kernel configuration file.
105 */
106
107/* debugging */
108#ifndef PSM_DEBUG
109#define PSM_DEBUG 0 /*
110 * logging: 0: none, 1: brief, 2: verbose
111 * 3: sync errors, 4: all packets
112 */
113#endif
114#define VLOG(level, args) do { \
115 if (verbose >= level) \
116 log args; \
117} while (0)
118
119#ifndef PSM_INPUT_TIMEOUT
120#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
121#endif
122
123#ifndef PSM_TAP_TIMEOUT
124#define PSM_TAP_TIMEOUT 125000
125#endif
126
127#ifndef PSM_TAP_THRESHOLD
128#define PSM_TAP_THRESHOLD 25
129#endif
130
131/* end of driver specific options */
132
133#define PSMCPNP_DRIVER_NAME "psmcpnp"
134
135struct psmcpnp_softc {
136 enum {
137 PSMCPNP_GENERIC,
138 PSMCPNP_FORCEPAD,
63
64#include "opt_isa.h"
65#include "opt_psm.h"
66#include "opt_evdev.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/kernel.h>
71#include <sys/module.h>
72#include <sys/bus.h>
73#include <sys/conf.h>
74#include <sys/filio.h>
75#include <sys/poll.h>
76#include <sys/sigio.h>
77#include <sys/signalvar.h>
78#include <sys/syslog.h>
79#include <machine/bus.h>
80#include <sys/rman.h>
81#include <sys/selinfo.h>
82#include <sys/sysctl.h>
83#include <sys/time.h>
84#include <sys/uio.h>
85
86#include <sys/limits.h>
87#include <sys/mouse.h>
88#include <machine/resource.h>
89
90#ifdef DEV_ISA
91#include <isa/isavar.h>
92#endif
93
94#ifdef EVDEV_SUPPORT
95#include <dev/evdev/evdev.h>
96#include <dev/evdev/input.h>
97#endif
98
99#include <dev/atkbdc/atkbdcreg.h>
100#include <dev/atkbdc/psm.h>
101
102/*
103 * Driver specific options: the following options may be set by
104 * `options' statements in the kernel configuration file.
105 */
106
107/* debugging */
108#ifndef PSM_DEBUG
109#define PSM_DEBUG 0 /*
110 * logging: 0: none, 1: brief, 2: verbose
111 * 3: sync errors, 4: all packets
112 */
113#endif
114#define VLOG(level, args) do { \
115 if (verbose >= level) \
116 log args; \
117} while (0)
118
119#ifndef PSM_INPUT_TIMEOUT
120#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
121#endif
122
123#ifndef PSM_TAP_TIMEOUT
124#define PSM_TAP_TIMEOUT 125000
125#endif
126
127#ifndef PSM_TAP_THRESHOLD
128#define PSM_TAP_THRESHOLD 25
129#endif
130
131/* end of driver specific options */
132
133#define PSMCPNP_DRIVER_NAME "psmcpnp"
134
135struct psmcpnp_softc {
136 enum {
137 PSMCPNP_GENERIC,
138 PSMCPNP_FORCEPAD,
139 PSMCPNP_HPSYN81,
140 } type; /* Based on PnP ID */
141};
142
143/* input queue */
144#define PSM_BUFSIZE 960
145#define PSM_SMALLBUFSIZE 240
146
147/* operation levels */
148#define PSM_LEVEL_BASE 0
149#define PSM_LEVEL_STANDARD 1
150#define PSM_LEVEL_NATIVE 2
151#define PSM_LEVEL_MIN PSM_LEVEL_BASE
152#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
153
139 } type; /* Based on PnP ID */
140};
141
142/* input queue */
143#define PSM_BUFSIZE 960
144#define PSM_SMALLBUFSIZE 240
145
146/* operation levels */
147#define PSM_LEVEL_BASE 0
148#define PSM_LEVEL_STANDARD 1
149#define PSM_LEVEL_NATIVE 2
150#define PSM_LEVEL_MIN PSM_LEVEL_BASE
151#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
152
153/* Active PS/2 multiplexing */
154#define PSM_NOMUX (-1)
155
154/* Logitech PS2++ protocol */
155#define MOUSE_PS2PLUS_CHECKBITS(b) \
156 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
157#define MOUSE_PS2PLUS_PACKET_TYPE(b) \
158 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
159
160/* ring buffer */
161typedef struct ringbuf {
162 int count; /* # of valid elements in the buffer */
163 int head; /* head pointer */
164 int tail; /* tail poiner */
165 u_char buf[PSM_BUFSIZE];
166} ringbuf_t;
167
168/* data buffer */
169typedef struct packetbuf {
170 u_char ipacket[16]; /* interim input buffer */
171 int inputbytes; /* # of bytes in the input buffer */
172} packetbuf_t;
173
174#ifndef PSM_PACKETQUEUE
175#define PSM_PACKETQUEUE 128
176#endif
177
156/* Logitech PS2++ protocol */
157#define MOUSE_PS2PLUS_CHECKBITS(b) \
158 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
159#define MOUSE_PS2PLUS_PACKET_TYPE(b) \
160 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
161
162/* ring buffer */
163typedef struct ringbuf {
164 int count; /* # of valid elements in the buffer */
165 int head; /* head pointer */
166 int tail; /* tail poiner */
167 u_char buf[PSM_BUFSIZE];
168} ringbuf_t;
169
170/* data buffer */
171typedef struct packetbuf {
172 u_char ipacket[16]; /* interim input buffer */
173 int inputbytes; /* # of bytes in the input buffer */
174} packetbuf_t;
175
176#ifndef PSM_PACKETQUEUE
177#define PSM_PACKETQUEUE 128
178#endif
179
178/*
179 * Typical bezel limits. Taken from 'Synaptics
180 * PS/2 TouchPad Interfacing Guide' p.3.2.3.
181 */
182#define SYNAPTICS_DEFAULT_MAX_X 5472
183#define SYNAPTICS_DEFAULT_MAX_Y 4448
184#define SYNAPTICS_DEFAULT_MIN_X 1472
185#define SYNAPTICS_DEFAULT_MIN_Y 1408
186
187typedef struct synapticsinfo {
188 struct sysctl_ctx_list sysctl_ctx;
189 struct sysctl_oid *sysctl_tree;
190 int directional_scrolls;
191 int two_finger_scroll;
192 int min_pressure;
193 int max_pressure;
194 int max_width;
195 int margin_top;
196 int margin_right;
197 int margin_bottom;
198 int margin_left;
199 int na_top;
200 int na_right;
201 int na_bottom;
202 int na_left;
203 int window_min;
204 int window_max;
205 int multiplicator;
206 int weight_current;
207 int weight_previous;
208 int weight_previous_na;
209 int weight_len_squared;
210 int div_min;
211 int div_max;
212 int div_max_na;
213 int div_len;
214 int tap_max_delta;
215 int tap_min_queue;
216 int taphold_timeout;
217 int vscroll_ver_area;
218 int vscroll_hor_area;
219 int vscroll_min_delta;
220 int vscroll_div_min;
221 int vscroll_div_max;
222 int touchpad_off;
223 int softbuttons_y;
224 int softbutton2_x;
225 int softbutton3_x;
226 int max_x;
227 int max_y;
228} synapticsinfo_t;
229
230typedef struct synapticspacket {
231 int x;
232 int y;
233} synapticspacket_t;
234
235#define SYNAPTICS_PACKETQUEUE 10
236#define SYNAPTICS_QUEUE_CURSOR(x) \
237 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
238
239#define SYNAPTICS_VERSION_GE(synhw, major, minor) \
240 ((synhw).infoMajor > (major) || \
241 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
242
243typedef struct smoother {
244 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
245 int queue_len;
246 int queue_cursor;
247 int start_x;
248 int start_y;
249 int avg_dx;
250 int avg_dy;
251 int squelch_x;
252 int squelch_y;
253 int is_fuzzy;
254 int active;
255} smoother_t;
256
257typedef struct gesture {
258 int window_min;
259 int fingers_nb;
260 int tap_button;
261 int in_taphold;
262 int in_vscroll;
263 int zmax; /* maximum pressure value */
264 struct timeval taptimeout; /* tap timeout for touchpads */
265} gesture_t;
266
267enum {
268 TRACKPOINT_SYSCTL_SENSITIVITY,
269 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
270 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
271 TRACKPOINT_SYSCTL_BACKUP_RANGE,
272 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
273 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
274 TRACKPOINT_SYSCTL_UP_THRESHOLD,
275 TRACKPOINT_SYSCTL_THRESHOLD,
276 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
277 TRACKPOINT_SYSCTL_Z_TIME,
278 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
279 TRACKPOINT_SYSCTL_SKIP_BACKUPS
280};
281
282typedef struct trackpointinfo {
283 struct sysctl_ctx_list sysctl_ctx;
284 struct sysctl_oid *sysctl_tree;
285 int sensitivity;
286 int inertia;
287 int uplateau;
288 int reach;
289 int draghys;
290 int mindrag;
291 int upthresh;
292 int threshold;
293 int jenks;
294 int ztime;
295 int pts;
296 int skipback;
297} trackpointinfo_t;
298
299typedef struct finger {
300 int x;
301 int y;
302 int p;
303 int w;
304 int flags;
305} finger_t;
306#define PSM_FINGERS 2 /* # of processed fingers */
307#define PSM_FINGER_IS_PEN (1<<0)
308#define PSM_FINGER_FUZZY (1<<1)
309#define PSM_FINGER_DEFAULT_P tap_threshold
310#define PSM_FINGER_DEFAULT_W 1
311#define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
312#define PSM_FINGER_RESET(f) do { \
313 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
314} while (0)
315
316typedef struct elantechhw {
317 int hwversion;
318 int fwversion;
319 int sizex;
320 int sizey;
321 int dpmmx;
322 int dpmmy;
323 int ntracesx;
324 int ntracesy;
325 int dptracex;
326 int dptracey;
327 int issemimt;
328 int isclickpad;
329 int hascrc;
330 int hastrackpoint;
331 int haspressure;
332} elantechhw_t;
333
334/* minimum versions supported by this driver */
335#define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
336
337#define ELANTECH_MAGIC(magic) \
338 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
339 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
340
341#define ELANTECH_FW_ID 0x00
342#define ELANTECH_FW_VERSION 0x01
343#define ELANTECH_CAPABILITIES 0x02
344#define ELANTECH_SAMPLE 0x03
345#define ELANTECH_RESOLUTION 0x04
346#define ELANTECH_REG_READ 0x10
347#define ELANTECH_REG_WRITE 0x11
348#define ELANTECH_REG_RDWR 0x00
349#define ELANTECH_CUSTOM_CMD 0xf8
350
351#ifdef EVDEV_SUPPORT
352#define ELANTECH_MAX_FINGERS 5
353#else
354#define ELANTECH_MAX_FINGERS PSM_FINGERS
355#endif
356
357#define ELANTECH_FINGER_MAX_P 255
358#define ELANTECH_FINGER_MAX_W 15
359#define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
360 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
361 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
362 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
363 .w = PSM_FINGER_DEFAULT_W, \
364 .flags = 0 \
365}
366
367enum {
368 ELANTECH_PKT_NOP,
369 ELANTECH_PKT_TRACKPOINT,
370 ELANTECH_PKT_V2_COMMON,
371 ELANTECH_PKT_V2_2FINGER,
372 ELANTECH_PKT_V3,
373 ELANTECH_PKT_V4_STATUS,
374 ELANTECH_PKT_V4_HEAD,
375 ELANTECH_PKT_V4_MOTION
376};
377
378#define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
379#define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
380 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
381 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
382 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
383 (pb)->ipacket[5] == 0xff)
384#define ELANTECH_PKT_IS_V2(pb) \
385 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
386#define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
387 ((pb)->ipacket[3] & 0x09) == 0x08 : \
388 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
389#define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
390 ((pb)->ipacket[3] & 0x09) == 0x09 : \
391 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
392#define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
393 ((pb)->ipacket[3] & 0x08) == 0x00 : \
394 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
395
396typedef struct elantechaction {
397 finger_t fingers[ELANTECH_MAX_FINGERS];
398 int mask;
399 int mask_v4wait;
400} elantechaction_t;
401
402/* driver control block */
403struct psm_softc { /* Driver status information */
404 int unit;
405 struct selinfo rsel; /* Process selecting for Input */
406 u_char state; /* Mouse driver state */
407 int config; /* driver configuration flags */
408 int flags; /* other flags */
409 KBDC kbdc; /* handle to access kbd controller */
410 struct resource *intr; /* IRQ resource */
411 void *ih; /* interrupt handle */
412 mousehw_t hw; /* hardware information */
413 synapticshw_t synhw; /* Synaptics hardware information */
414 synapticsinfo_t syninfo; /* Synaptics configuration */
415 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
416 gesture_t gesture; /* Gesture context */
417 elantechhw_t elanhw; /* Elantech hardware information */
418 elantechaction_t elanaction; /* Elantech action context */
419 int tphw; /* TrackPoint hardware information */
420 trackpointinfo_t tpinfo; /* TrackPoint configuration */
421 mousemode_t mode; /* operation mode */
422 mousemode_t dflt_mode; /* default operation mode */
423 mousestatus_t status; /* accumulated mouse movement */
424 ringbuf_t queue; /* mouse status queue */
425 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
426 int pqueue_start; /* start of data in queue */
427 int pqueue_end; /* end of data in queue */
428 int button; /* the latest button state */
429 int xold; /* previous absolute X position */
430 int yold; /* previous absolute Y position */
431 int xaverage; /* average X position */
432 int yaverage; /* average Y position */
433 int squelch; /* level to filter movement at low speed */
434 int syncerrors; /* # of bytes discarded to synchronize */
435 int pkterrors; /* # of packets failed during quaranteen. */
436 int fpcount; /* forcePad valid packet counter */
437 struct timeval inputtimeout;
438 struct timeval lastsoftintr; /* time of last soft interrupt */
439 struct timeval lastinputerr; /* time last sync error happened */
440 struct timeval idletimeout;
441 packetbuf_t idlepacket; /* packet to send after idle timeout */
442 int watchdog; /* watchdog timer flag */
443 struct callout callout; /* watchdog timer call out */
444 struct callout softcallout; /* buffer timer call out */
445 struct cdev *dev;
446 struct cdev *bdev;
447 int lasterr;
448 int cmdcount;
449 struct sigio *async; /* Processes waiting for SIGIO */
450 int extended_buttons;
180typedef struct synapticsinfo {
181 struct sysctl_ctx_list sysctl_ctx;
182 struct sysctl_oid *sysctl_tree;
183 int directional_scrolls;
184 int two_finger_scroll;
185 int min_pressure;
186 int max_pressure;
187 int max_width;
188 int margin_top;
189 int margin_right;
190 int margin_bottom;
191 int margin_left;
192 int na_top;
193 int na_right;
194 int na_bottom;
195 int na_left;
196 int window_min;
197 int window_max;
198 int multiplicator;
199 int weight_current;
200 int weight_previous;
201 int weight_previous_na;
202 int weight_len_squared;
203 int div_min;
204 int div_max;
205 int div_max_na;
206 int div_len;
207 int tap_max_delta;
208 int tap_min_queue;
209 int taphold_timeout;
210 int vscroll_ver_area;
211 int vscroll_hor_area;
212 int vscroll_min_delta;
213 int vscroll_div_min;
214 int vscroll_div_max;
215 int touchpad_off;
216 int softbuttons_y;
217 int softbutton2_x;
218 int softbutton3_x;
219 int max_x;
220 int max_y;
221} synapticsinfo_t;
222
223typedef struct synapticspacket {
224 int x;
225 int y;
226} synapticspacket_t;
227
228#define SYNAPTICS_PACKETQUEUE 10
229#define SYNAPTICS_QUEUE_CURSOR(x) \
230 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
231
232#define SYNAPTICS_VERSION_GE(synhw, major, minor) \
233 ((synhw).infoMajor > (major) || \
234 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
235
236typedef struct smoother {
237 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
238 int queue_len;
239 int queue_cursor;
240 int start_x;
241 int start_y;
242 int avg_dx;
243 int avg_dy;
244 int squelch_x;
245 int squelch_y;
246 int is_fuzzy;
247 int active;
248} smoother_t;
249
250typedef struct gesture {
251 int window_min;
252 int fingers_nb;
253 int tap_button;
254 int in_taphold;
255 int in_vscroll;
256 int zmax; /* maximum pressure value */
257 struct timeval taptimeout; /* tap timeout for touchpads */
258} gesture_t;
259
260enum {
261 TRACKPOINT_SYSCTL_SENSITIVITY,
262 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
263 TRACKPOINT_SYSCTL_UPPER_PLATEAU,
264 TRACKPOINT_SYSCTL_BACKUP_RANGE,
265 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
266 TRACKPOINT_SYSCTL_MINIMUM_DRAG,
267 TRACKPOINT_SYSCTL_UP_THRESHOLD,
268 TRACKPOINT_SYSCTL_THRESHOLD,
269 TRACKPOINT_SYSCTL_JENKS_CURVATURE,
270 TRACKPOINT_SYSCTL_Z_TIME,
271 TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
272 TRACKPOINT_SYSCTL_SKIP_BACKUPS
273};
274
275typedef struct trackpointinfo {
276 struct sysctl_ctx_list sysctl_ctx;
277 struct sysctl_oid *sysctl_tree;
278 int sensitivity;
279 int inertia;
280 int uplateau;
281 int reach;
282 int draghys;
283 int mindrag;
284 int upthresh;
285 int threshold;
286 int jenks;
287 int ztime;
288 int pts;
289 int skipback;
290} trackpointinfo_t;
291
292typedef struct finger {
293 int x;
294 int y;
295 int p;
296 int w;
297 int flags;
298} finger_t;
299#define PSM_FINGERS 2 /* # of processed fingers */
300#define PSM_FINGER_IS_PEN (1<<0)
301#define PSM_FINGER_FUZZY (1<<1)
302#define PSM_FINGER_DEFAULT_P tap_threshold
303#define PSM_FINGER_DEFAULT_W 1
304#define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
305#define PSM_FINGER_RESET(f) do { \
306 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
307} while (0)
308
309typedef struct elantechhw {
310 int hwversion;
311 int fwversion;
312 int sizex;
313 int sizey;
314 int dpmmx;
315 int dpmmy;
316 int ntracesx;
317 int ntracesy;
318 int dptracex;
319 int dptracey;
320 int issemimt;
321 int isclickpad;
322 int hascrc;
323 int hastrackpoint;
324 int haspressure;
325} elantechhw_t;
326
327/* minimum versions supported by this driver */
328#define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
329
330#define ELANTECH_MAGIC(magic) \
331 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
332 ((magic)[2] == 0xc8 || (magic)[2] == 0x00))
333
334#define ELANTECH_FW_ID 0x00
335#define ELANTECH_FW_VERSION 0x01
336#define ELANTECH_CAPABILITIES 0x02
337#define ELANTECH_SAMPLE 0x03
338#define ELANTECH_RESOLUTION 0x04
339#define ELANTECH_REG_READ 0x10
340#define ELANTECH_REG_WRITE 0x11
341#define ELANTECH_REG_RDWR 0x00
342#define ELANTECH_CUSTOM_CMD 0xf8
343
344#ifdef EVDEV_SUPPORT
345#define ELANTECH_MAX_FINGERS 5
346#else
347#define ELANTECH_MAX_FINGERS PSM_FINGERS
348#endif
349
350#define ELANTECH_FINGER_MAX_P 255
351#define ELANTECH_FINGER_MAX_W 15
352#define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
353 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
354 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
355 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
356 .w = PSM_FINGER_DEFAULT_W, \
357 .flags = 0 \
358}
359
360enum {
361 ELANTECH_PKT_NOP,
362 ELANTECH_PKT_TRACKPOINT,
363 ELANTECH_PKT_V2_COMMON,
364 ELANTECH_PKT_V2_2FINGER,
365 ELANTECH_PKT_V3,
366 ELANTECH_PKT_V4_STATUS,
367 ELANTECH_PKT_V4_HEAD,
368 ELANTECH_PKT_V4_MOTION
369};
370
371#define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
372#define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
373 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
374 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
375 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
376 (pb)->ipacket[5] == 0xff)
377#define ELANTECH_PKT_IS_V2(pb) \
378 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
379#define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
380 ((pb)->ipacket[3] & 0x09) == 0x08 : \
381 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
382#define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
383 ((pb)->ipacket[3] & 0x09) == 0x09 : \
384 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
385#define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
386 ((pb)->ipacket[3] & 0x08) == 0x00 : \
387 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
388
389typedef struct elantechaction {
390 finger_t fingers[ELANTECH_MAX_FINGERS];
391 int mask;
392 int mask_v4wait;
393} elantechaction_t;
394
395/* driver control block */
396struct psm_softc { /* Driver status information */
397 int unit;
398 struct selinfo rsel; /* Process selecting for Input */
399 u_char state; /* Mouse driver state */
400 int config; /* driver configuration flags */
401 int flags; /* other flags */
402 KBDC kbdc; /* handle to access kbd controller */
403 struct resource *intr; /* IRQ resource */
404 void *ih; /* interrupt handle */
405 mousehw_t hw; /* hardware information */
406 synapticshw_t synhw; /* Synaptics hardware information */
407 synapticsinfo_t syninfo; /* Synaptics configuration */
408 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
409 gesture_t gesture; /* Gesture context */
410 elantechhw_t elanhw; /* Elantech hardware information */
411 elantechaction_t elanaction; /* Elantech action context */
412 int tphw; /* TrackPoint hardware information */
413 trackpointinfo_t tpinfo; /* TrackPoint configuration */
414 mousemode_t mode; /* operation mode */
415 mousemode_t dflt_mode; /* default operation mode */
416 mousestatus_t status; /* accumulated mouse movement */
417 ringbuf_t queue; /* mouse status queue */
418 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
419 int pqueue_start; /* start of data in queue */
420 int pqueue_end; /* end of data in queue */
421 int button; /* the latest button state */
422 int xold; /* previous absolute X position */
423 int yold; /* previous absolute Y position */
424 int xaverage; /* average X position */
425 int yaverage; /* average Y position */
426 int squelch; /* level to filter movement at low speed */
427 int syncerrors; /* # of bytes discarded to synchronize */
428 int pkterrors; /* # of packets failed during quaranteen. */
429 int fpcount; /* forcePad valid packet counter */
430 struct timeval inputtimeout;
431 struct timeval lastsoftintr; /* time of last soft interrupt */
432 struct timeval lastinputerr; /* time last sync error happened */
433 struct timeval idletimeout;
434 packetbuf_t idlepacket; /* packet to send after idle timeout */
435 int watchdog; /* watchdog timer flag */
436 struct callout callout; /* watchdog timer call out */
437 struct callout softcallout; /* buffer timer call out */
438 struct cdev *dev;
439 struct cdev *bdev;
440 int lasterr;
441 int cmdcount;
442 struct sigio *async; /* Processes waiting for SIGIO */
443 int extended_buttons;
444 int muxport; /* MUX port with attached Synaptics */
445 u_char muxsave[3]; /* 3->6 byte proto conversion buffer */
446 int muxtpbuttons; /* Touchpad button state */
447 int muxmsbuttons; /* Mouse (trackpoint) button state */
448 struct timeval muxmidtimeout; /* middle button supression timeout */
451#ifdef EVDEV_SUPPORT
452 struct evdev_dev *evdev_a; /* Absolute reporting device */
453 struct evdev_dev *evdev_r; /* Relative reporting device */
454#endif
455};
456static devclass_t psm_devclass;
457
458/* driver state flags (state) */
459#define PSM_VALID 0x80
460#define PSM_OPEN 1 /* Device is open */
461#define PSM_ASLP 2 /* Waiting for mouse data */
462#define PSM_SOFTARMED 4 /* Software interrupt armed */
463#define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
464#define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */
465#define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */
466
467/* driver configuration flags (config) */
468#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
469#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
470#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
471#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
472#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
473#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
474#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
475#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
476#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
477
478#define PSM_CONFIG_FLAGS \
479 (PSM_CONFIG_RESOLUTION | \
480 PSM_CONFIG_ACCEL | \
481 PSM_CONFIG_NOCHECKSYNC | \
482 PSM_CONFIG_NOIDPROBE | \
483 PSM_CONFIG_NORESET | \
484 PSM_CONFIG_FORCETAP | \
485 PSM_CONFIG_IGNPORTERROR | \
486 PSM_CONFIG_HOOKRESUME | \
487 PSM_CONFIG_INITAFTERSUSPEND)
488
489/* other flags (flags) */
490#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
491
492#define kbdcp(p) ((atkbdc_softc_t *)(p))
493#define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
494 & KBDC_QUIRK_KEEP_ACTIVATED)
495
496/* Tunables */
497static int tap_enabled = -1;
498static int verbose = PSM_DEBUG;
499static int synaptics_support = 0;
500static int trackpoint_support = 0;
501static int elantech_support = 0;
502
503/* for backward compatibility */
504#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
505#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
506#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
507
508typedef struct old_mousehw {
509 int buttons;
510 int iftype;
511 int type;
512 int hwid;
513} old_mousehw_t;
514
515typedef struct old_mousemode {
516 int protocol;
517 int rate;
518 int resolution;
519 int accelfactor;
520} old_mousemode_t;
521
522#define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
523enum {
524 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
525 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
526 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
527 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
528 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
529 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
530 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
531 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
532 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
533 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
534 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
535 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
536 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
537 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
538 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
539 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
540 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
541 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
542 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
543 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
544 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
545 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
546 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
547 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
548 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
549 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
550 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
551 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
552 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
553 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
554 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
555 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
556 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
557 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
558};
559
560/* packet formatting function */
561typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
562 mousestatus_t *);
563
564/* function prototypes */
565static void psmidentify(driver_t *, device_t);
566static int psmprobe(device_t);
567static int psmattach(device_t);
568static int psmdetach(device_t);
569static int psmresume(device_t);
570
571static d_open_t psm_cdev_open;
572static d_close_t psm_cdev_close;
573static d_read_t psmread;
574static d_write_t psmwrite;
575static d_ioctl_t psmioctl;
576static d_poll_t psmpoll;
577
578static int psmopen(struct psm_softc *);
579static int psmclose(struct psm_softc *);
580
581#ifdef EVDEV_SUPPORT
582static evdev_open_t psm_ev_open_r;
583static evdev_close_t psm_ev_close_r;
584static evdev_open_t psm_ev_open_a;
585static evdev_close_t psm_ev_close_a;
586#endif
587
588static int enable_aux_dev(KBDC);
589static int disable_aux_dev(KBDC);
590static int get_mouse_status(KBDC, int *, int, int);
591static int get_aux_id(KBDC);
592static int set_mouse_sampling_rate(KBDC, int);
593static int set_mouse_scaling(KBDC, int);
594static int set_mouse_resolution(KBDC, int);
595static int set_mouse_mode(KBDC);
596static int get_mouse_buttons(KBDC);
597static int is_a_mouse(int);
598static void recover_from_error(KBDC);
599static int restore_controller(KBDC, int);
600static int doinitialize(struct psm_softc *, mousemode_t *);
601static int doopen(struct psm_softc *, int);
602static int reinitialize(struct psm_softc *, int);
603static char *model_name(int);
604static void psmsoftintr(void *);
605static void psmsoftintridle(void *);
606static void psmintr(void *);
607static void psmtimeout(void *);
608static int timeelapsed(const struct timeval *, int, int,
609 const struct timeval *);
610static void dropqueue(struct psm_softc *);
611static void flushpackets(struct psm_softc *);
612static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
613 mousestatus_t *, int *, int *, int *);
614static int proc_synaptics(struct psm_softc *, packetbuf_t *,
615 mousestatus_t *, int *, int *, int *);
449#ifdef EVDEV_SUPPORT
450 struct evdev_dev *evdev_a; /* Absolute reporting device */
451 struct evdev_dev *evdev_r; /* Relative reporting device */
452#endif
453};
454static devclass_t psm_devclass;
455
456/* driver state flags (state) */
457#define PSM_VALID 0x80
458#define PSM_OPEN 1 /* Device is open */
459#define PSM_ASLP 2 /* Waiting for mouse data */
460#define PSM_SOFTARMED 4 /* Software interrupt armed */
461#define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
462#define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */
463#define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */
464
465/* driver configuration flags (config) */
466#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
467#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
468#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
469#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
470#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
471#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
472#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
473#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
474#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
475
476#define PSM_CONFIG_FLAGS \
477 (PSM_CONFIG_RESOLUTION | \
478 PSM_CONFIG_ACCEL | \
479 PSM_CONFIG_NOCHECKSYNC | \
480 PSM_CONFIG_NOIDPROBE | \
481 PSM_CONFIG_NORESET | \
482 PSM_CONFIG_FORCETAP | \
483 PSM_CONFIG_IGNPORTERROR | \
484 PSM_CONFIG_HOOKRESUME | \
485 PSM_CONFIG_INITAFTERSUSPEND)
486
487/* other flags (flags) */
488#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
489
490#define kbdcp(p) ((atkbdc_softc_t *)(p))
491#define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
492 & KBDC_QUIRK_KEEP_ACTIVATED)
493
494/* Tunables */
495static int tap_enabled = -1;
496static int verbose = PSM_DEBUG;
497static int synaptics_support = 0;
498static int trackpoint_support = 0;
499static int elantech_support = 0;
500
501/* for backward compatibility */
502#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
503#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
504#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
505
506typedef struct old_mousehw {
507 int buttons;
508 int iftype;
509 int type;
510 int hwid;
511} old_mousehw_t;
512
513typedef struct old_mousemode {
514 int protocol;
515 int rate;
516 int resolution;
517 int accelfactor;
518} old_mousemode_t;
519
520#define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
521enum {
522 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
523 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
524 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
525 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
526 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
527 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
528 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
529 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
530 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
531 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
532 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
533 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
534 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
535 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
536 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
537 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
538 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
539 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
540 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
541 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
542 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
543 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
544 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
545 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
546 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
547 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
548 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
549 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
550 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
551 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
552 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
553 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
554 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
555 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
556};
557
558/* packet formatting function */
559typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
560 mousestatus_t *);
561
562/* function prototypes */
563static void psmidentify(driver_t *, device_t);
564static int psmprobe(device_t);
565static int psmattach(device_t);
566static int psmdetach(device_t);
567static int psmresume(device_t);
568
569static d_open_t psm_cdev_open;
570static d_close_t psm_cdev_close;
571static d_read_t psmread;
572static d_write_t psmwrite;
573static d_ioctl_t psmioctl;
574static d_poll_t psmpoll;
575
576static int psmopen(struct psm_softc *);
577static int psmclose(struct psm_softc *);
578
579#ifdef EVDEV_SUPPORT
580static evdev_open_t psm_ev_open_r;
581static evdev_close_t psm_ev_close_r;
582static evdev_open_t psm_ev_open_a;
583static evdev_close_t psm_ev_close_a;
584#endif
585
586static int enable_aux_dev(KBDC);
587static int disable_aux_dev(KBDC);
588static int get_mouse_status(KBDC, int *, int, int);
589static int get_aux_id(KBDC);
590static int set_mouse_sampling_rate(KBDC, int);
591static int set_mouse_scaling(KBDC, int);
592static int set_mouse_resolution(KBDC, int);
593static int set_mouse_mode(KBDC);
594static int get_mouse_buttons(KBDC);
595static int is_a_mouse(int);
596static void recover_from_error(KBDC);
597static int restore_controller(KBDC, int);
598static int doinitialize(struct psm_softc *, mousemode_t *);
599static int doopen(struct psm_softc *, int);
600static int reinitialize(struct psm_softc *, int);
601static char *model_name(int);
602static void psmsoftintr(void *);
603static void psmsoftintridle(void *);
604static void psmintr(void *);
605static void psmtimeout(void *);
606static int timeelapsed(const struct timeval *, int, int,
607 const struct timeval *);
608static void dropqueue(struct psm_softc *);
609static void flushpackets(struct psm_softc *);
610static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
611 mousestatus_t *, int *, int *, int *);
612static int proc_synaptics(struct psm_softc *, packetbuf_t *,
613 mousestatus_t *, int *, int *, int *);
614static int proc_synaptics_mux(struct psm_softc *, packetbuf_t *);
616static void proc_versapad(struct psm_softc *, packetbuf_t *,
617 mousestatus_t *, int *, int *, int *);
618static int proc_elantech(struct psm_softc *, packetbuf_t *,
619 mousestatus_t *, int *, int *, int *);
620static int psmpalmdetect(struct psm_softc *, finger_t *, int);
621static void psmgestures(struct psm_softc *, finger_t *, int,
622 mousestatus_t *);
623static void psmsmoother(struct psm_softc *, finger_t *, int,
624 mousestatus_t *, int *, int *);
625static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
626 u_char *);
627
628/* vendor specific features */
629enum probearg { PROBE, REINIT };
630typedef int probefunc_t(struct psm_softc *, enum probearg);
631
632static int mouse_id_proc1(KBDC, int, int, int *);
633static int mouse_ext_command(KBDC, int);
634
635static probefunc_t enable_groller;
636static probefunc_t enable_gmouse;
637static probefunc_t enable_aglide;
638static probefunc_t enable_kmouse;
639static probefunc_t enable_msexplorer;
640static probefunc_t enable_msintelli;
641static probefunc_t enable_4dmouse;
642static probefunc_t enable_4dplus;
643static probefunc_t enable_mmanplus;
644static probefunc_t enable_synaptics;
615static void proc_versapad(struct psm_softc *, packetbuf_t *,
616 mousestatus_t *, int *, int *, int *);
617static int proc_elantech(struct psm_softc *, packetbuf_t *,
618 mousestatus_t *, int *, int *, int *);
619static int psmpalmdetect(struct psm_softc *, finger_t *, int);
620static void psmgestures(struct psm_softc *, finger_t *, int,
621 mousestatus_t *);
622static void psmsmoother(struct psm_softc *, finger_t *, int,
623 mousestatus_t *, int *, int *);
624static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
625 u_char *);
626
627/* vendor specific features */
628enum probearg { PROBE, REINIT };
629typedef int probefunc_t(struct psm_softc *, enum probearg);
630
631static int mouse_id_proc1(KBDC, int, int, int *);
632static int mouse_ext_command(KBDC, int);
633
634static probefunc_t enable_groller;
635static probefunc_t enable_gmouse;
636static probefunc_t enable_aglide;
637static probefunc_t enable_kmouse;
638static probefunc_t enable_msexplorer;
639static probefunc_t enable_msintelli;
640static probefunc_t enable_4dmouse;
641static probefunc_t enable_4dplus;
642static probefunc_t enable_mmanplus;
643static probefunc_t enable_synaptics;
644static probefunc_t enable_synaptics_mux;
645static probefunc_t enable_trackpoint;
646static probefunc_t enable_versapad;
647static probefunc_t enable_elantech;
648
649static void set_trackpoint_parameters(struct psm_softc *sc);
650static void synaptics_passthrough_on(struct psm_softc *sc);
651static void synaptics_passthrough_off(struct psm_softc *sc);
652static int synaptics_preferred_mode(struct psm_softc *sc);
653static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
654
655static struct {
656 int model;
657 u_char syncmask;
658 int packetsize;
659 probefunc_t *probefunc;
660} vendortype[] = {
661 /*
662 * WARNING: the order of probe is very important. Don't mess it
663 * unless you know what you are doing.
664 */
645static probefunc_t enable_trackpoint;
646static probefunc_t enable_versapad;
647static probefunc_t enable_elantech;
648
649static void set_trackpoint_parameters(struct psm_softc *sc);
650static void synaptics_passthrough_on(struct psm_softc *sc);
651static void synaptics_passthrough_off(struct psm_softc *sc);
652static int synaptics_preferred_mode(struct psm_softc *sc);
653static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
654
655static struct {
656 int model;
657 u_char syncmask;
658 int packetsize;
659 probefunc_t *probefunc;
660} vendortype[] = {
661 /*
662 * WARNING: the order of probe is very important. Don't mess it
663 * unless you know what you are doing.
664 */
665 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad on Active Mux */
666 0x00, MOUSE_PS2_PACKETSIZE, enable_synaptics_mux },
665 { MOUSE_MODEL_NET, /* Genius NetMouse */
666 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
667 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
668 0xc8, 6, enable_groller },
669 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
670 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
671 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
672 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
673 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
674 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
675 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
676 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
677 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
678 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
679 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
680 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
681 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
682 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
683 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
684 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
685 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
686 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
687 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
688 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
689 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
690 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
691 { MOUSE_MODEL_GENERIC,
692 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
693};
694#define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
695
696/* device driver declarateion */
697static device_method_t psm_methods[] = {
698 /* Device interface */
699 DEVMETHOD(device_identify, psmidentify),
700 DEVMETHOD(device_probe, psmprobe),
701 DEVMETHOD(device_attach, psmattach),
702 DEVMETHOD(device_detach, psmdetach),
703 DEVMETHOD(device_resume, psmresume),
704
705 { 0, 0 }
706};
707
708static driver_t psm_driver = {
709 PSM_DRIVER_NAME,
710 psm_methods,
711 sizeof(struct psm_softc),
712};
713
714static struct cdevsw psm_cdevsw = {
715 .d_version = D_VERSION,
716 .d_flags = D_NEEDGIANT,
717 .d_open = psm_cdev_open,
718 .d_close = psm_cdev_close,
719 .d_read = psmread,
720 .d_write = psmwrite,
721 .d_ioctl = psmioctl,
722 .d_poll = psmpoll,
723 .d_name = PSM_DRIVER_NAME,
724};
725
726#ifdef EVDEV_SUPPORT
727static const struct evdev_methods psm_ev_methods_r = {
728 .ev_open = psm_ev_open_r,
729 .ev_close = psm_ev_close_r,
730};
731static const struct evdev_methods psm_ev_methods_a = {
732 .ev_open = psm_ev_open_a,
733 .ev_close = psm_ev_close_a,
734};
735#endif
736
737/* device I/O routines */
738static int
739enable_aux_dev(KBDC kbdc)
740{
741 int res;
742
743 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
744 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
745
746 return (res == PSM_ACK);
747}
748
749static int
750disable_aux_dev(KBDC kbdc)
751{
752 int res;
753
754 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
755 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
756
757 return (res == PSM_ACK);
758}
759
760static int
761get_mouse_status(KBDC kbdc, int *status, int flag, int len)
762{
763 int cmd;
764 int res;
765 int i;
766
767 switch (flag) {
768 case 0:
769 default:
770 cmd = PSMC_SEND_DEV_STATUS;
771 break;
772 case 1:
773 cmd = PSMC_SEND_DEV_DATA;
774 break;
775 }
776 empty_aux_buffer(kbdc, 5);
777 res = send_aux_command(kbdc, cmd);
778 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
779 (flag == 1) ? "DATA" : "STATUS", res));
780 if (res != PSM_ACK)
781 return (0);
782
783 for (i = 0; i < len; ++i) {
784 status[i] = read_aux_data(kbdc);
785 if (status[i] < 0)
786 break;
787 }
788
789 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
790 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
791
792 return (i);
793}
794
795static int
796get_aux_id(KBDC kbdc)
797{
798 int res;
799 int id;
800
801 empty_aux_buffer(kbdc, 5);
802 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
803 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
804 if (res != PSM_ACK)
805 return (-1);
806
807 /* 10ms delay */
808 DELAY(10000);
809
810 id = read_aux_data(kbdc);
811 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
812
813 return (id);
814}
815
816static int
817set_mouse_sampling_rate(KBDC kbdc, int rate)
818{
819 int res;
820
821 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
822 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
823
824 return ((res == PSM_ACK) ? rate : -1);
825}
826
827static int
828set_mouse_scaling(KBDC kbdc, int scale)
829{
830 int res;
831
832 switch (scale) {
833 case 1:
834 default:
835 scale = PSMC_SET_SCALING11;
836 break;
837 case 2:
838 scale = PSMC_SET_SCALING21;
839 break;
840 }
841 res = send_aux_command(kbdc, scale);
842 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
843 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
844
845 return (res == PSM_ACK);
846}
847
848/* `val' must be 0 through PSMD_MAX_RESOLUTION */
849static int
850set_mouse_resolution(KBDC kbdc, int val)
851{
852 int res;
853
854 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
855 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
856
857 return ((res == PSM_ACK) ? val : -1);
858}
859
860/*
861 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
862 * re-enabled by calling `enable_aux_dev()'
863 */
864static int
865set_mouse_mode(KBDC kbdc)
866{
867 int res;
868
869 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
870 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
871
872 return (res == PSM_ACK);
873}
874
875static int
876get_mouse_buttons(KBDC kbdc)
877{
878 int c = 2; /* assume two buttons by default */
879 int status[3];
880
881 /*
882 * NOTE: a special sequence to obtain Logitech Mouse specific
883 * information: set resolution to 25 ppi, set scaling to 1:1, set
884 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
885 * mouse status bytes is the number of available buttons.
886 * Some manufactures also support this sequence.
887 */
888 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
889 return (c);
890 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
891 set_mouse_scaling(kbdc, 1) &&
892 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
893 return (status[1]);
894 return (c);
895}
896
897/* misc subroutines */
898/*
899 * Someday, I will get the complete list of valid pointing devices and
900 * their IDs... XXX
901 */
902static int
903is_a_mouse(int id)
904{
905#if 0
906 static int valid_ids[] = {
907 PSM_MOUSE_ID, /* mouse */
908 PSM_BALLPOINT_ID, /* ballpoint device */
909 PSM_INTELLI_ID, /* Intellimouse */
910 PSM_EXPLORER_ID, /* Intellimouse Explorer */
911 -1 /* end of table */
912 };
913 int i;
914
915 for (i = 0; valid_ids[i] >= 0; ++i)
916 if (valid_ids[i] == id)
917 return (TRUE);
918 return (FALSE);
919#else
920 return (TRUE);
921#endif
922}
923
924static char *
925model_name(int model)
926{
927 static struct {
928 int model_code;
929 char *model_name;
930 } models[] = {
931 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
932 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
933 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
934 { MOUSE_MODEL_THINK, "ThinkingMouse" },
935 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
936 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
937 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
938 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
939 { MOUSE_MODEL_4D, "4D Mouse" },
940 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
941 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
942 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
943 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
944 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
945 { MOUSE_MODEL_UNKNOWN, "Unknown" },
946 };
947 int i;
948
949 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
950 if (models[i].model_code == model)
951 break;
952 return (models[i].model_name);
953}
954
955static void
956recover_from_error(KBDC kbdc)
957{
958 /* discard anything left in the output buffer */
959 empty_both_buffers(kbdc, 10);
960
961#if 0
962 /*
963 * NOTE: KBDC_RESET_KBD may not restore the communication between the
964 * keyboard and the controller.
965 */
966 reset_kbd(kbdc);
967#else
968 /*
969 * NOTE: somehow diagnostic and keyboard port test commands bring the
970 * keyboard back.
971 */
972 if (!test_controller(kbdc))
973 log(LOG_ERR, "psm: keyboard controller failed.\n");
974 /* if there isn't a keyboard in the system, the following error is OK */
975 if (test_kbd_port(kbdc) != 0)
976 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
977#endif
978}
979
980static int
981restore_controller(KBDC kbdc, int command_byte)
982{
983 empty_both_buffers(kbdc, 10);
984
985 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
986 log(LOG_ERR, "psm: failed to restore the keyboard controller "
987 "command byte.\n");
988 empty_both_buffers(kbdc, 10);
989 return (FALSE);
990 } else {
991 empty_both_buffers(kbdc, 10);
992 return (TRUE);
993 }
994}
995
996/*
997 * Re-initialize the aux port and device. The aux port must be enabled
998 * and its interrupt must be disabled before calling this routine.
999 * The aux device will be disabled before returning.
1000 * The keyboard controller must be locked via `kbdc_lock()' before
1001 * calling this routine.
1002 */
1003static int
1004doinitialize(struct psm_softc *sc, mousemode_t *mode)
1005{
1006 KBDC kbdc = sc->kbdc;
1007 int stat[3];
1008 int i;
1009
1010 switch((i = test_aux_port(kbdc))) {
1011 case 1: /* ignore these errors */
1012 case 2:
1013 case 3:
1014 case PSM_ACK:
1015 if (verbose)
1016 log(LOG_DEBUG,
1017 "psm%d: strange result for test aux port (%d).\n",
1018 sc->unit, i);
1019 /* FALLTHROUGH */
1020 case 0: /* no error */
1021 break;
1022 case -1: /* time out */
1023 default: /* error */
1024 recover_from_error(kbdc);
1025 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1026 break;
1027 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
1028 sc->unit, i);
1029 return (FALSE);
1030 }
1031
1032 if (sc->config & PSM_CONFIG_NORESET) {
1033 /*
1034 * Don't try to reset the pointing device. It may possibly
1035 * be left in the unknown state, though...
1036 */
1037 } else {
1038 /*
1039 * NOTE: some controllers appears to hang the `keyboard' when
1040 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1041 */
1042 if (!reset_aux_dev(kbdc)) {
1043 recover_from_error(kbdc);
1044 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
1045 sc->unit);
1046 return (FALSE);
1047 }
1048 }
1049
1050 /*
1051 * both the aux port and the aux device is functioning, see
1052 * if the device can be enabled.
1053 */
1054 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
1055 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1056 sc->unit);
1057 return (FALSE);
1058 }
1059 empty_both_buffers(kbdc, 10); /* remove stray data if any */
1060
1061 /* Re-enable the mouse. */
1062 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1063 if (vendortype[i].model == sc->hw.model)
1064 (*vendortype[i].probefunc)(sc, REINIT);
1065
1066 /* set mouse parameters */
1067 if (mode != (mousemode_t *)NULL) {
1068 if (mode->rate > 0)
1069 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1070 if (mode->resolution >= 0)
1071 mode->resolution =
1072 set_mouse_resolution(kbdc, mode->resolution);
1073 set_mouse_scaling(kbdc, 1);
1074 set_mouse_mode(kbdc);
1075 }
1076
1077 /* Record sync on the next data packet we see. */
1078 sc->flags |= PSM_NEED_SYNCBITS;
1079
1080 /* just check the status of the mouse */
1081 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1082 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1083 sc->unit);
1084
1085 return (TRUE);
1086}
1087
1088static int
1089doopen(struct psm_softc *sc, int command_byte)
1090{
1091 int stat[3];
667 { MOUSE_MODEL_NET, /* Genius NetMouse */
668 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
669 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
670 0xc8, 6, enable_groller },
671 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
672 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
673 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
674 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
675 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
676 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
677 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
678 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
679 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
680 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
681 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
682 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
683 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
684 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
685 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
686 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
687 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
688 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
689 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
690 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
691 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
692 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
693 { MOUSE_MODEL_GENERIC,
694 0xc0, MOUSE_PS2_PACKETSIZE, NULL },
695};
696#define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
697
698/* device driver declarateion */
699static device_method_t psm_methods[] = {
700 /* Device interface */
701 DEVMETHOD(device_identify, psmidentify),
702 DEVMETHOD(device_probe, psmprobe),
703 DEVMETHOD(device_attach, psmattach),
704 DEVMETHOD(device_detach, psmdetach),
705 DEVMETHOD(device_resume, psmresume),
706
707 { 0, 0 }
708};
709
710static driver_t psm_driver = {
711 PSM_DRIVER_NAME,
712 psm_methods,
713 sizeof(struct psm_softc),
714};
715
716static struct cdevsw psm_cdevsw = {
717 .d_version = D_VERSION,
718 .d_flags = D_NEEDGIANT,
719 .d_open = psm_cdev_open,
720 .d_close = psm_cdev_close,
721 .d_read = psmread,
722 .d_write = psmwrite,
723 .d_ioctl = psmioctl,
724 .d_poll = psmpoll,
725 .d_name = PSM_DRIVER_NAME,
726};
727
728#ifdef EVDEV_SUPPORT
729static const struct evdev_methods psm_ev_methods_r = {
730 .ev_open = psm_ev_open_r,
731 .ev_close = psm_ev_close_r,
732};
733static const struct evdev_methods psm_ev_methods_a = {
734 .ev_open = psm_ev_open_a,
735 .ev_close = psm_ev_close_a,
736};
737#endif
738
739/* device I/O routines */
740static int
741enable_aux_dev(KBDC kbdc)
742{
743 int res;
744
745 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
746 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
747
748 return (res == PSM_ACK);
749}
750
751static int
752disable_aux_dev(KBDC kbdc)
753{
754 int res;
755
756 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
757 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
758
759 return (res == PSM_ACK);
760}
761
762static int
763get_mouse_status(KBDC kbdc, int *status, int flag, int len)
764{
765 int cmd;
766 int res;
767 int i;
768
769 switch (flag) {
770 case 0:
771 default:
772 cmd = PSMC_SEND_DEV_STATUS;
773 break;
774 case 1:
775 cmd = PSMC_SEND_DEV_DATA;
776 break;
777 }
778 empty_aux_buffer(kbdc, 5);
779 res = send_aux_command(kbdc, cmd);
780 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
781 (flag == 1) ? "DATA" : "STATUS", res));
782 if (res != PSM_ACK)
783 return (0);
784
785 for (i = 0; i < len; ++i) {
786 status[i] = read_aux_data(kbdc);
787 if (status[i] < 0)
788 break;
789 }
790
791 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
792 (flag == 1) ? "data" : "status", status[0], status[1], status[2]));
793
794 return (i);
795}
796
797static int
798get_aux_id(KBDC kbdc)
799{
800 int res;
801 int id;
802
803 empty_aux_buffer(kbdc, 5);
804 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
805 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
806 if (res != PSM_ACK)
807 return (-1);
808
809 /* 10ms delay */
810 DELAY(10000);
811
812 id = read_aux_data(kbdc);
813 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
814
815 return (id);
816}
817
818static int
819set_mouse_sampling_rate(KBDC kbdc, int rate)
820{
821 int res;
822
823 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
824 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
825
826 return ((res == PSM_ACK) ? rate : -1);
827}
828
829static int
830set_mouse_scaling(KBDC kbdc, int scale)
831{
832 int res;
833
834 switch (scale) {
835 case 1:
836 default:
837 scale = PSMC_SET_SCALING11;
838 break;
839 case 2:
840 scale = PSMC_SET_SCALING21;
841 break;
842 }
843 res = send_aux_command(kbdc, scale);
844 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
845 (scale == PSMC_SET_SCALING21) ? "21" : "11", res));
846
847 return (res == PSM_ACK);
848}
849
850/* `val' must be 0 through PSMD_MAX_RESOLUTION */
851static int
852set_mouse_resolution(KBDC kbdc, int val)
853{
854 int res;
855
856 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
857 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
858
859 return ((res == PSM_ACK) ? val : -1);
860}
861
862/*
863 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
864 * re-enabled by calling `enable_aux_dev()'
865 */
866static int
867set_mouse_mode(KBDC kbdc)
868{
869 int res;
870
871 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
872 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
873
874 return (res == PSM_ACK);
875}
876
877static int
878get_mouse_buttons(KBDC kbdc)
879{
880 int c = 2; /* assume two buttons by default */
881 int status[3];
882
883 /*
884 * NOTE: a special sequence to obtain Logitech Mouse specific
885 * information: set resolution to 25 ppi, set scaling to 1:1, set
886 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
887 * mouse status bytes is the number of available buttons.
888 * Some manufactures also support this sequence.
889 */
890 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
891 return (c);
892 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
893 set_mouse_scaling(kbdc, 1) &&
894 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
895 return (status[1]);
896 return (c);
897}
898
899/* misc subroutines */
900/*
901 * Someday, I will get the complete list of valid pointing devices and
902 * their IDs... XXX
903 */
904static int
905is_a_mouse(int id)
906{
907#if 0
908 static int valid_ids[] = {
909 PSM_MOUSE_ID, /* mouse */
910 PSM_BALLPOINT_ID, /* ballpoint device */
911 PSM_INTELLI_ID, /* Intellimouse */
912 PSM_EXPLORER_ID, /* Intellimouse Explorer */
913 -1 /* end of table */
914 };
915 int i;
916
917 for (i = 0; valid_ids[i] >= 0; ++i)
918 if (valid_ids[i] == id)
919 return (TRUE);
920 return (FALSE);
921#else
922 return (TRUE);
923#endif
924}
925
926static char *
927model_name(int model)
928{
929 static struct {
930 int model_code;
931 char *model_name;
932 } models[] = {
933 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
934 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
935 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
936 { MOUSE_MODEL_THINK, "ThinkingMouse" },
937 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
938 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
939 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
940 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
941 { MOUSE_MODEL_4D, "4D Mouse" },
942 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
943 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
944 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
945 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
946 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
947 { MOUSE_MODEL_UNKNOWN, "Unknown" },
948 };
949 int i;
950
951 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
952 if (models[i].model_code == model)
953 break;
954 return (models[i].model_name);
955}
956
957static void
958recover_from_error(KBDC kbdc)
959{
960 /* discard anything left in the output buffer */
961 empty_both_buffers(kbdc, 10);
962
963#if 0
964 /*
965 * NOTE: KBDC_RESET_KBD may not restore the communication between the
966 * keyboard and the controller.
967 */
968 reset_kbd(kbdc);
969#else
970 /*
971 * NOTE: somehow diagnostic and keyboard port test commands bring the
972 * keyboard back.
973 */
974 if (!test_controller(kbdc))
975 log(LOG_ERR, "psm: keyboard controller failed.\n");
976 /* if there isn't a keyboard in the system, the following error is OK */
977 if (test_kbd_port(kbdc) != 0)
978 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n"));
979#endif
980}
981
982static int
983restore_controller(KBDC kbdc, int command_byte)
984{
985 empty_both_buffers(kbdc, 10);
986
987 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
988 log(LOG_ERR, "psm: failed to restore the keyboard controller "
989 "command byte.\n");
990 empty_both_buffers(kbdc, 10);
991 return (FALSE);
992 } else {
993 empty_both_buffers(kbdc, 10);
994 return (TRUE);
995 }
996}
997
998/*
999 * Re-initialize the aux port and device. The aux port must be enabled
1000 * and its interrupt must be disabled before calling this routine.
1001 * The aux device will be disabled before returning.
1002 * The keyboard controller must be locked via `kbdc_lock()' before
1003 * calling this routine.
1004 */
1005static int
1006doinitialize(struct psm_softc *sc, mousemode_t *mode)
1007{
1008 KBDC kbdc = sc->kbdc;
1009 int stat[3];
1010 int i;
1011
1012 switch((i = test_aux_port(kbdc))) {
1013 case 1: /* ignore these errors */
1014 case 2:
1015 case 3:
1016 case PSM_ACK:
1017 if (verbose)
1018 log(LOG_DEBUG,
1019 "psm%d: strange result for test aux port (%d).\n",
1020 sc->unit, i);
1021 /* FALLTHROUGH */
1022 case 0: /* no error */
1023 break;
1024 case -1: /* time out */
1025 default: /* error */
1026 recover_from_error(kbdc);
1027 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1028 break;
1029 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
1030 sc->unit, i);
1031 return (FALSE);
1032 }
1033
1034 if (sc->config & PSM_CONFIG_NORESET) {
1035 /*
1036 * Don't try to reset the pointing device. It may possibly
1037 * be left in the unknown state, though...
1038 */
1039 } else {
1040 /*
1041 * NOTE: some controllers appears to hang the `keyboard' when
1042 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1043 */
1044 if (!reset_aux_dev(kbdc)) {
1045 recover_from_error(kbdc);
1046 log(LOG_ERR, "psm%d: failed to reset the aux device.\n",
1047 sc->unit);
1048 return (FALSE);
1049 }
1050 }
1051
1052 /*
1053 * both the aux port and the aux device is functioning, see
1054 * if the device can be enabled.
1055 */
1056 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
1057 log(LOG_ERR, "psm%d: failed to enable the aux device.\n",
1058 sc->unit);
1059 return (FALSE);
1060 }
1061 empty_both_buffers(kbdc, 10); /* remove stray data if any */
1062
1063 /* Re-enable the mouse. */
1064 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1065 if (vendortype[i].model == sc->hw.model)
1066 (*vendortype[i].probefunc)(sc, REINIT);
1067
1068 /* set mouse parameters */
1069 if (mode != (mousemode_t *)NULL) {
1070 if (mode->rate > 0)
1071 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
1072 if (mode->resolution >= 0)
1073 mode->resolution =
1074 set_mouse_resolution(kbdc, mode->resolution);
1075 set_mouse_scaling(kbdc, 1);
1076 set_mouse_mode(kbdc);
1077 }
1078
1079 /* Record sync on the next data packet we see. */
1080 sc->flags |= PSM_NEED_SYNCBITS;
1081
1082 /* just check the status of the mouse */
1083 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
1084 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
1085 sc->unit);
1086
1087 return (TRUE);
1088}
1089
1090static int
1091doopen(struct psm_softc *sc, int command_byte)
1092{
1093 int stat[3];
1094 int mux_enabled = FALSE;
1092
1093 /*
1094 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1095 * no obvious reason. Thus we check the current mode and restore the
1096 * Absolute Mode if it was cleared.
1097 *
1098 * The previous hack at the end of psmprobe() wasn't efficient when
1099 * moused(8) was restarted.
1100 *
1101 * A Reset (FF) or Set Defaults (F6) command would clear the
1102 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1103 * doesn't show any evidence of such a command.
1104 */
1105 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1095
1096 /*
1097 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with
1098 * no obvious reason. Thus we check the current mode and restore the
1099 * Absolute Mode if it was cleared.
1100 *
1101 * The previous hack at the end of psmprobe() wasn't efficient when
1102 * moused(8) was restarted.
1103 *
1104 * A Reset (FF) or Set Defaults (F6) command would clear the
1105 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5
1106 * doesn't show any evidence of such a command.
1107 */
1108 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) {
1109 if (sc->muxport != PSM_NOMUX) {
1110 mux_enabled = enable_aux_mux(sc->kbdc) >= 0;
1111 if (mux_enabled)
1112 set_active_aux_mux_port(sc->kbdc, sc->muxport);
1113 else
1114 log(LOG_ERR, "psm%d: failed to enable "
1115 "active multiplexing mode.\n",
1116 sc->unit);
1117 }
1106 mouse_ext_command(sc->kbdc, 1);
1107 get_mouse_status(sc->kbdc, stat, 0, 3);
1108 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1118 mouse_ext_command(sc->kbdc, 1);
1119 get_mouse_status(sc->kbdc, stat, 0, 3);
1120 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) ||
1109 stat[1] == 0x46 || stat[1] == 0x47) &&
1121 stat[1] == 0x47) &&
1110 stat[2] == 0x40) {
1111 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1112 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1113 "hopefully restored\n",
1114 sc->unit));
1115 }
1122 stat[2] == 0x40) {
1123 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1124 VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode "
1125 "hopefully restored\n",
1126 sc->unit));
1127 }
1128 if (mux_enabled)
1129 disable_aux_mux(sc->kbdc);
1116 }
1117
1118 /*
1119 * A user may want to disable tap and drag gestures on a Synaptics
1120 * TouchPad when it operates in Relative Mode.
1121 */
1122 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1123 if (tap_enabled > 0) {
1124 VLOG(2, (LOG_DEBUG,
1125 "psm%d: enable tap and drag gestures\n",
1126 sc->unit));
1127 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1128 } else if (tap_enabled == 0) {
1129 VLOG(2, (LOG_DEBUG,
1130 "psm%d: disable tap and drag gestures\n",
1131 sc->unit));
1132 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1133 }
1134 }
1135
1136 /* enable the mouse device */
1137 if (!enable_aux_dev(sc->kbdc)) {
1138 /* MOUSE ERROR: failed to enable the mouse because:
1139 * 1) the mouse is faulty,
1140 * 2) the mouse has been removed(!?)
1141 * In the latter case, the keyboard may have hung, and need
1142 * recovery procedure...
1143 */
1144 recover_from_error(sc->kbdc);
1145#if 0
1146 /* FIXME: we could reset the mouse here and try to enable
1147 * it again. But it will take long time and it's not a good
1148 * idea to disable the keyboard that long...
1149 */
1150 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1151 recover_from_error(sc->kbdc);
1152#else
1153 {
1154#endif
1155 restore_controller(sc->kbdc, command_byte);
1156 /* mark this device is no longer available */
1157 sc->state &= ~PSM_VALID;
1158 log(LOG_ERR,
1159 "psm%d: failed to enable the device (doopen).\n",
1160 sc->unit);
1161 return (EIO);
1162 }
1163 }
1164
1165 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1166 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1167 sc->unit);
1168
1169 /* enable the aux port and interrupt */
1170 if (!set_controller_command_byte(sc->kbdc,
1171 kbdc_get_device_mask(sc->kbdc),
1172 (command_byte & KBD_KBD_CONTROL_BITS) |
1173 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1174 /* CONTROLLER ERROR */
1175 disable_aux_dev(sc->kbdc);
1176 restore_controller(sc->kbdc, command_byte);
1177 log(LOG_ERR,
1178 "psm%d: failed to enable the aux interrupt (doopen).\n",
1179 sc->unit);
1180 return (EIO);
1181 }
1182
1183 /* start the watchdog timer */
1184 sc->watchdog = FALSE;
1185 callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1186
1187 return (0);
1188}
1189
1190static int
1191reinitialize(struct psm_softc *sc, int doinit)
1192{
1193 int err;
1194 int c;
1195 int s;
1196
1197 /* don't let anybody mess with the aux device */
1198 if (!kbdc_lock(sc->kbdc, TRUE))
1199 return (EIO);
1200 s = spltty();
1201
1202 /* block our watchdog timer */
1203 sc->watchdog = FALSE;
1204 callout_stop(&sc->callout);
1205
1206 /* save the current controller command byte */
1207 empty_both_buffers(sc->kbdc, 10);
1208 c = get_controller_command_byte(sc->kbdc);
1209 VLOG(2, (LOG_DEBUG,
1210 "psm%d: current command byte: %04x (reinitialize).\n",
1211 sc->unit, c));
1212
1213 /* enable the aux port but disable the aux interrupt and the keyboard */
1214 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1215 kbdc_get_device_mask(sc->kbdc),
1216 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1217 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1218 /* CONTROLLER ERROR */
1219 splx(s);
1220 kbdc_lock(sc->kbdc, FALSE);
1221 log(LOG_ERR,
1222 "psm%d: unable to set the command byte (reinitialize).\n",
1223 sc->unit);
1224 return (EIO);
1225 }
1226
1227 /* flush any data */
1228 if (sc->state & PSM_VALID) {
1229 /* this may fail; but never mind... */
1230 disable_aux_dev(sc->kbdc);
1231 empty_aux_buffer(sc->kbdc, 10);
1232 }
1233 flushpackets(sc);
1234 sc->syncerrors = 0;
1235 sc->pkterrors = 0;
1236 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1237
1238 /* try to detect the aux device; are you still there? */
1239 err = 0;
1240 if (doinit) {
1241 if (doinitialize(sc, &sc->mode)) {
1242 /* yes */
1243 sc->state |= PSM_VALID;
1244 } else {
1245 /* the device has gone! */
1246 restore_controller(sc->kbdc, c);
1247 sc->state &= ~PSM_VALID;
1248 log(LOG_ERR,
1249 "psm%d: the aux device has gone! (reinitialize).\n",
1250 sc->unit);
1251 err = ENXIO;
1252 }
1253 }
1254 splx(s);
1255
1256 /* restore the driver state */
1257 if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) &&
1258 (err == 0)) {
1259 /* enable the aux device and the port again */
1260 err = doopen(sc, c);
1261 if (err != 0)
1262 log(LOG_ERR, "psm%d: failed to enable the device "
1263 "(reinitialize).\n", sc->unit);
1264 } else {
1265 /* restore the keyboard port and disable the aux port */
1266 if (!set_controller_command_byte(sc->kbdc,
1267 kbdc_get_device_mask(sc->kbdc),
1268 (c & KBD_KBD_CONTROL_BITS) |
1269 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1270 /* CONTROLLER ERROR */
1271 log(LOG_ERR, "psm%d: failed to disable the aux port "
1272 "(reinitialize).\n", sc->unit);
1273 err = EIO;
1274 }
1275 }
1276
1277 kbdc_lock(sc->kbdc, FALSE);
1278 return (err);
1279}
1280
1281/* psm driver entry points */
1282
1283static void
1284psmidentify(driver_t *driver, device_t parent)
1285{
1286 device_t psmc;
1287 device_t psm;
1288 u_long irq;
1289 int unit;
1290
1291 unit = device_get_unit(parent);
1292
1293 /* always add at least one child */
1294 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1295 if (psm == NULL)
1296 return;
1297
1298 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1299 if (irq > 0)
1300 return;
1301
1302 /*
1303 * If the PS/2 mouse device has already been reported by ACPI or
1304 * PnP BIOS, obtain the IRQ resource from it.
1305 * (See psmcpnp_attach() below.)
1306 */
1307 psmc = device_find_child(device_get_parent(parent),
1308 PSMCPNP_DRIVER_NAME, unit);
1309 if (psmc == NULL)
1310 return;
1311 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1312 if (irq <= 0)
1313 return;
1314 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1315 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1316}
1317
1318#define endprobe(v) do { \
1319 if (bootverbose) \
1320 --verbose; \
1321 kbdc_set_device_mask(sc->kbdc, mask); \
1322 kbdc_lock(sc->kbdc, FALSE); \
1323 return (v); \
1324} while (0)
1325
1326static int
1327psmprobe(device_t dev)
1328{
1329 int unit = device_get_unit(dev);
1330 struct psm_softc *sc = device_get_softc(dev);
1331 int stat[3];
1332 int command_byte;
1333 int mask;
1334 int rid;
1335 int i;
1336
1337#if 0
1338 kbdc_debug(TRUE);
1339#endif
1340
1341 /* see if IRQ is available */
1342 rid = KBDC_RID_AUX;
1343 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1344 if (sc->intr == NULL) {
1345 if (bootverbose)
1346 device_printf(dev, "unable to allocate IRQ\n");
1347 return (ENXIO);
1348 }
1349 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1350
1351 sc->unit = unit;
1352 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1353 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1354 /* XXX: for backward compatibility */
1355#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1356 sc->config |=
1357#ifdef PSM_RESETAFTERSUSPEND
1358 PSM_CONFIG_INITAFTERSUSPEND;
1359#else
1360 PSM_CONFIG_HOOKRESUME;
1361#endif
1362#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1363 sc->flags = 0;
1130 }
1131
1132 /*
1133 * A user may want to disable tap and drag gestures on a Synaptics
1134 * TouchPad when it operates in Relative Mode.
1135 */
1136 if (sc->hw.model == MOUSE_MODEL_GENERIC) {
1137 if (tap_enabled > 0) {
1138 VLOG(2, (LOG_DEBUG,
1139 "psm%d: enable tap and drag gestures\n",
1140 sc->unit));
1141 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1142 } else if (tap_enabled == 0) {
1143 VLOG(2, (LOG_DEBUG,
1144 "psm%d: disable tap and drag gestures\n",
1145 sc->unit));
1146 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
1147 }
1148 }
1149
1150 /* enable the mouse device */
1151 if (!enable_aux_dev(sc->kbdc)) {
1152 /* MOUSE ERROR: failed to enable the mouse because:
1153 * 1) the mouse is faulty,
1154 * 2) the mouse has been removed(!?)
1155 * In the latter case, the keyboard may have hung, and need
1156 * recovery procedure...
1157 */
1158 recover_from_error(sc->kbdc);
1159#if 0
1160 /* FIXME: we could reset the mouse here and try to enable
1161 * it again. But it will take long time and it's not a good
1162 * idea to disable the keyboard that long...
1163 */
1164 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
1165 recover_from_error(sc->kbdc);
1166#else
1167 {
1168#endif
1169 restore_controller(sc->kbdc, command_byte);
1170 /* mark this device is no longer available */
1171 sc->state &= ~PSM_VALID;
1172 log(LOG_ERR,
1173 "psm%d: failed to enable the device (doopen).\n",
1174 sc->unit);
1175 return (EIO);
1176 }
1177 }
1178
1179 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1180 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n",
1181 sc->unit);
1182
1183 /* enable the aux port and interrupt */
1184 if (!set_controller_command_byte(sc->kbdc,
1185 kbdc_get_device_mask(sc->kbdc),
1186 (command_byte & KBD_KBD_CONTROL_BITS) |
1187 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
1188 /* CONTROLLER ERROR */
1189 disable_aux_dev(sc->kbdc);
1190 restore_controller(sc->kbdc, command_byte);
1191 log(LOG_ERR,
1192 "psm%d: failed to enable the aux interrupt (doopen).\n",
1193 sc->unit);
1194 return (EIO);
1195 }
1196
1197 /* start the watchdog timer */
1198 sc->watchdog = FALSE;
1199 callout_reset(&sc->callout, hz * 2, psmtimeout, sc);
1200
1201 return (0);
1202}
1203
1204static int
1205reinitialize(struct psm_softc *sc, int doinit)
1206{
1207 int err;
1208 int c;
1209 int s;
1210
1211 /* don't let anybody mess with the aux device */
1212 if (!kbdc_lock(sc->kbdc, TRUE))
1213 return (EIO);
1214 s = spltty();
1215
1216 /* block our watchdog timer */
1217 sc->watchdog = FALSE;
1218 callout_stop(&sc->callout);
1219
1220 /* save the current controller command byte */
1221 empty_both_buffers(sc->kbdc, 10);
1222 c = get_controller_command_byte(sc->kbdc);
1223 VLOG(2, (LOG_DEBUG,
1224 "psm%d: current command byte: %04x (reinitialize).\n",
1225 sc->unit, c));
1226
1227 /* enable the aux port but disable the aux interrupt and the keyboard */
1228 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
1229 kbdc_get_device_mask(sc->kbdc),
1230 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1231 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1232 /* CONTROLLER ERROR */
1233 splx(s);
1234 kbdc_lock(sc->kbdc, FALSE);
1235 log(LOG_ERR,
1236 "psm%d: unable to set the command byte (reinitialize).\n",
1237 sc->unit);
1238 return (EIO);
1239 }
1240
1241 /* flush any data */
1242 if (sc->state & PSM_VALID) {
1243 /* this may fail; but never mind... */
1244 disable_aux_dev(sc->kbdc);
1245 empty_aux_buffer(sc->kbdc, 10);
1246 }
1247 flushpackets(sc);
1248 sc->syncerrors = 0;
1249 sc->pkterrors = 0;
1250 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr));
1251
1252 /* try to detect the aux device; are you still there? */
1253 err = 0;
1254 if (doinit) {
1255 if (doinitialize(sc, &sc->mode)) {
1256 /* yes */
1257 sc->state |= PSM_VALID;
1258 } else {
1259 /* the device has gone! */
1260 restore_controller(sc->kbdc, c);
1261 sc->state &= ~PSM_VALID;
1262 log(LOG_ERR,
1263 "psm%d: the aux device has gone! (reinitialize).\n",
1264 sc->unit);
1265 err = ENXIO;
1266 }
1267 }
1268 splx(s);
1269
1270 /* restore the driver state */
1271 if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) &&
1272 (err == 0)) {
1273 /* enable the aux device and the port again */
1274 err = doopen(sc, c);
1275 if (err != 0)
1276 log(LOG_ERR, "psm%d: failed to enable the device "
1277 "(reinitialize).\n", sc->unit);
1278 } else {
1279 /* restore the keyboard port and disable the aux port */
1280 if (!set_controller_command_byte(sc->kbdc,
1281 kbdc_get_device_mask(sc->kbdc),
1282 (c & KBD_KBD_CONTROL_BITS) |
1283 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1284 /* CONTROLLER ERROR */
1285 log(LOG_ERR, "psm%d: failed to disable the aux port "
1286 "(reinitialize).\n", sc->unit);
1287 err = EIO;
1288 }
1289 }
1290
1291 kbdc_lock(sc->kbdc, FALSE);
1292 return (err);
1293}
1294
1295/* psm driver entry points */
1296
1297static void
1298psmidentify(driver_t *driver, device_t parent)
1299{
1300 device_t psmc;
1301 device_t psm;
1302 u_long irq;
1303 int unit;
1304
1305 unit = device_get_unit(parent);
1306
1307 /* always add at least one child */
1308 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
1309 if (psm == NULL)
1310 return;
1311
1312 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
1313 if (irq > 0)
1314 return;
1315
1316 /*
1317 * If the PS/2 mouse device has already been reported by ACPI or
1318 * PnP BIOS, obtain the IRQ resource from it.
1319 * (See psmcpnp_attach() below.)
1320 */
1321 psmc = device_find_child(device_get_parent(parent),
1322 PSMCPNP_DRIVER_NAME, unit);
1323 if (psmc == NULL)
1324 return;
1325 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
1326 if (irq <= 0)
1327 return;
1328 bus_delete_resource(psmc, SYS_RES_IRQ, 0);
1329 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
1330}
1331
1332#define endprobe(v) do { \
1333 if (bootverbose) \
1334 --verbose; \
1335 kbdc_set_device_mask(sc->kbdc, mask); \
1336 kbdc_lock(sc->kbdc, FALSE); \
1337 return (v); \
1338} while (0)
1339
1340static int
1341psmprobe(device_t dev)
1342{
1343 int unit = device_get_unit(dev);
1344 struct psm_softc *sc = device_get_softc(dev);
1345 int stat[3];
1346 int command_byte;
1347 int mask;
1348 int rid;
1349 int i;
1350
1351#if 0
1352 kbdc_debug(TRUE);
1353#endif
1354
1355 /* see if IRQ is available */
1356 rid = KBDC_RID_AUX;
1357 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1358 if (sc->intr == NULL) {
1359 if (bootverbose)
1360 device_printf(dev, "unable to allocate IRQ\n");
1361 return (ENXIO);
1362 }
1363 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1364
1365 sc->unit = unit;
1366 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
1367 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
1368 /* XXX: for backward compatibility */
1369#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
1370 sc->config |=
1371#ifdef PSM_RESETAFTERSUSPEND
1372 PSM_CONFIG_INITAFTERSUSPEND;
1373#else
1374 PSM_CONFIG_HOOKRESUME;
1375#endif
1376#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
1377 sc->flags = 0;
1378 sc->muxport = PSM_NOMUX;
1364 if (bootverbose)
1365 ++verbose;
1366
1367 device_set_desc(dev, "PS/2 Mouse");
1368
1369 if (!kbdc_lock(sc->kbdc, TRUE)) {
1370 printf("psm%d: unable to lock the controller.\n", unit);
1371 if (bootverbose)
1372 --verbose;
1373 return (ENXIO);
1374 }
1375
1376 /*
1377 * NOTE: two bits in the command byte controls the operation of the
1378 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1379 * port interrupt (IRQ 12) enable bit (bit 2).
1380 */
1381
1382 /* discard anything left after the keyboard initialization */
1383 empty_both_buffers(sc->kbdc, 10);
1384
1385 /* save the current command byte; it will be used later */
1386 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1387 command_byte = get_controller_command_byte(sc->kbdc);
1388 if (verbose)
1389 printf("psm%d: current command byte:%04x\n", unit,
1390 command_byte);
1391 if (command_byte == -1) {
1392 /* CONTROLLER ERROR */
1393 printf("psm%d: unable to get the current command byte value.\n",
1394 unit);
1395 endprobe(ENXIO);
1396 }
1397
1398 /*
1399 * disable the keyboard port while probing the aux port, which must be
1400 * enabled during this routine
1401 */
1402 if (!set_controller_command_byte(sc->kbdc,
1403 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1404 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1405 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1406 /*
1407 * this is CONTROLLER ERROR; I don't know how to recover
1408 * from this error...
1409 */
1410 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1411 restore_controller(sc->kbdc, command_byte);
1412 printf("psm%d: unable to set the command byte.\n", unit);
1413 endprobe(ENXIO);
1414 }
1415 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1416
1417 /*
1418 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1419 * port exists and is functioning. However, some controllers appears
1420 * to respond with zero even when the aux port doesn't exist. (It may
1421 * be that this is only the case when the controller DOES have the aux
1422 * port but the port is not wired on the motherboard.) The keyboard
1423 * controllers without the port, such as the original AT, are
1424 * supposed to return with an error code or simply time out. In any
1425 * case, we have to continue probing the port even when the controller
1426 * passes this test.
1427 *
1428 * XXX: some controllers erroneously return the error code 1, 2 or 3
1429 * when it has a perfectly functional aux port. We have to ignore
1430 * this error code. Even if the controller HAS error with the aux
1431 * port, it will be detected later...
1432 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1433 */
1434 switch ((i = test_aux_port(sc->kbdc))) {
1435 case 1: /* ignore these errors */
1436 case 2:
1437 case 3:
1438 case PSM_ACK:
1439 if (verbose)
1440 printf("psm%d: strange result for test aux port "
1441 "(%d).\n", unit, i);
1442 /* FALLTHROUGH */
1443 case 0: /* no error */
1444 break;
1445 case -1: /* time out */
1446 default: /* error */
1447 recover_from_error(sc->kbdc);
1448 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1449 break;
1450 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1451 restore_controller(sc->kbdc, command_byte);
1452 if (verbose)
1453 printf("psm%d: the aux port is not functioning (%d).\n",
1454 unit, i);
1455 endprobe(ENXIO);
1456 }
1457
1458 if (sc->config & PSM_CONFIG_NORESET) {
1459 /*
1460 * Don't try to reset the pointing device. It may possibly be
1461 * left in an unknown state, though...
1462 */
1463 } else {
1464 /*
1465 * NOTE: some controllers appears to hang the `keyboard' when
1466 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1467 *
1468 * Attempt to reset the controller twice -- this helps
1469 * pierce through some KVM switches. The second reset
1470 * is non-fatal.
1471 */
1472 if (!reset_aux_dev(sc->kbdc)) {
1473 recover_from_error(sc->kbdc);
1474 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1475 restore_controller(sc->kbdc, command_byte);
1476 if (verbose)
1477 printf("psm%d: failed to reset the aux "
1478 "device.\n", unit);
1479 endprobe(ENXIO);
1480 } else if (!reset_aux_dev(sc->kbdc)) {
1481 recover_from_error(sc->kbdc);
1482 if (verbose >= 2)
1483 printf("psm%d: failed to reset the aux device "
1484 "(2).\n", unit);
1485 }
1486 }
1487
1488 /*
1489 * both the aux port and the aux device are functioning, see if the
1490 * device can be enabled. NOTE: when enabled, the device will start
1491 * sending data; we shall immediately disable the device once we know
1492 * the device can be enabled.
1493 */
1494 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1495 /* MOUSE ERROR */
1496 recover_from_error(sc->kbdc);
1497 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1498 restore_controller(sc->kbdc, command_byte);
1499 if (verbose)
1500 printf("psm%d: failed to enable the aux device.\n",
1501 unit);
1502 endprobe(ENXIO);
1503 }
1504
1505 /* save the default values after reset */
1506 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1507 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1508 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1509 } else {
1510 sc->dflt_mode.rate = sc->mode.rate = -1;
1511 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1512 }
1513
1514 /* hardware information */
1515 sc->hw.iftype = MOUSE_IF_PS2;
1516
1517 /* verify the device is a mouse */
1518 sc->hw.hwid = get_aux_id(sc->kbdc);
1519 if (!is_a_mouse(sc->hw.hwid)) {
1520 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1521 restore_controller(sc->kbdc, command_byte);
1522 if (verbose)
1523 printf("psm%d: unknown device type (%d).\n", unit,
1524 sc->hw.hwid);
1525 endprobe(ENXIO);
1526 }
1527 switch (sc->hw.hwid) {
1528 case PSM_BALLPOINT_ID:
1529 sc->hw.type = MOUSE_TRACKBALL;
1530 break;
1531 case PSM_MOUSE_ID:
1532 case PSM_INTELLI_ID:
1533 case PSM_EXPLORER_ID:
1534 case PSM_4DMOUSE_ID:
1535 case PSM_4DPLUS_ID:
1536 sc->hw.type = MOUSE_MOUSE;
1537 break;
1538 default:
1539 sc->hw.type = MOUSE_UNKNOWN;
1540 break;
1541 }
1542
1543 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1544 sc->hw.buttons = 2;
1545 i = GENERIC_MOUSE_ENTRY;
1546 } else {
1547 /* # of buttons */
1548 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1549
1550 /* other parameters */
1551 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1552 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1553 if (verbose >= 2)
1554 printf("psm%d: found %s\n", unit,
1555 model_name(vendortype[i].model));
1556 break;
1557 }
1558 }
1559
1560 sc->hw.model = vendortype[i].model;
1561
1562 sc->dflt_mode.level = PSM_LEVEL_BASE;
1563 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1564 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1565 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1566 sc->dflt_mode.syncmask[0] = 0;
1567 else
1568 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1569 if (sc->config & PSM_CONFIG_FORCETAP)
1570 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1571 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1572 sc->mode = sc->dflt_mode;
1573 sc->mode.packetsize = vendortype[i].packetsize;
1574
1575 /* set mouse parameters */
1576#if 0
1577 /*
1578 * A version of Logitech FirstMouse+ won't report wheel movement,
1579 * if SET_DEFAULTS is sent... Don't use this command.
1580 * This fix was found by Takashi Nishida.
1581 */
1582 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1583 if (verbose >= 2)
1584 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1585#endif
1586 if (sc->config & PSM_CONFIG_RESOLUTION)
1587 sc->mode.resolution =
1588 set_mouse_resolution(sc->kbdc,
1589 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1590 else if (sc->mode.resolution >= 0)
1591 sc->mode.resolution =
1592 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1593 if (sc->mode.rate > 0)
1594 sc->mode.rate =
1595 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1596 set_mouse_scaling(sc->kbdc, 1);
1597
1598 /* Record sync on the next data packet we see. */
1599 sc->flags |= PSM_NEED_SYNCBITS;
1600
1601 /* just check the status of the mouse */
1602 /*
1603 * NOTE: XXX there are some arcane controller/mouse combinations out
1604 * there, which hung the controller unless there is data transmission
1605 * after ACK from the mouse.
1606 */
1607 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1608 printf("psm%d: failed to get status.\n", unit);
1609 else {
1610 /*
1611 * When in its native mode, some mice operate with different
1612 * default parameters than in the PS/2 compatible mode.
1613 */
1614 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1615 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1616 }
1617
1618 /* disable the aux port for now... */
1619 if (!set_controller_command_byte(sc->kbdc,
1620 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1621 (command_byte & KBD_KBD_CONTROL_BITS) |
1622 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1623 /*
1624 * this is CONTROLLER ERROR; I don't know the proper way to
1625 * recover from this error...
1626 */
1627 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1628 restore_controller(sc->kbdc, command_byte);
1629 printf("psm%d: unable to set the command byte.\n", unit);
1630 endprobe(ENXIO);
1631 }
1632
1633 /* done */
1634 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1635 kbdc_lock(sc->kbdc, FALSE);
1636 return (0);
1637}
1638
1639#ifdef EVDEV_SUPPORT
1640/* Values are taken from Linux drivers for userland software compatibility */
1641#define PS2_MOUSE_VENDOR 0x0002
1642#define PS2_MOUSE_GENERIC_PRODUCT 0x0001
1643#define PS2_MOUSE_SYNAPTICS_NAME "SynPS/2 Synaptics TouchPad"
1644#define PS2_MOUSE_SYNAPTICS_PRODUCT 0x0007
1645#define PS2_MOUSE_TRACKPOINT_NAME "TPPS/2 IBM TrackPoint"
1646#define PS2_MOUSE_TRACKPOINT_PRODUCT 0x000A
1647#define PS2_MOUSE_ELANTECH_NAME "ETPS/2 Elantech Touchpad"
1648#define PS2_MOUSE_ELANTECH_ST_NAME "ETPS/2 Elantech TrackPoint"
1649#define PS2_MOUSE_ELANTECH_PRODUCT 0x000E
1650
1651#define ABSINFO_END { ABS_CNT, 0, 0, 0 }
1652
1653static void
1654psm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4])
1655{
1656 size_t i;
1657
1658 for (i = 0; info[i][0] != ABS_CNT; i++)
1659 evdev_support_abs(evdev, info[i][0], 0, info[i][1], info[i][2],
1660 0, 0, info[i][3]);
1661}
1662
1663static void
1664psm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f)
1665{
1666 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1667
1668 evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id);
1669 evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id);
1670 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x);
1671 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y);
1672 evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p);
1673}
1674
1675static void
1676psm_push_st_finger(struct psm_softc *sc, const finger_t *f)
1677{
1678 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1679
1680 evdev_push_abs(sc->evdev_a, ABS_X, f->x);
1681 evdev_push_abs(sc->evdev_a, ABS_Y, y);
1682 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p);
1683 if (sc->synhw.capPalmDetect)
1684 evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w);
1685}
1686
1687static void
1688psm_release_mt_slot(struct evdev_dev *evdev, int32_t slot)
1689{
1690
1691 evdev_push_abs(evdev, ABS_MT_SLOT, slot);
1692 evdev_push_abs(evdev, ABS_MT_TRACKING_ID, -1);
1693}
1694
1695static int
1696psm_register(device_t dev, int model_code)
1697{
1698 struct psm_softc *sc = device_get_softc(dev);
1699 struct evdev_dev *evdev_r;
1700 int error, i, nbuttons, nwheels, product;
1701 bool is_pointing_stick;
1702 const char *name;
1703
1704 name = model_name(model_code);
1705 nbuttons = sc->hw.buttons;
1706 product = PS2_MOUSE_GENERIC_PRODUCT;
1707 nwheels = 0;
1708 is_pointing_stick = false;
1709
1710 switch (model_code) {
1711 case MOUSE_MODEL_TRACKPOINT:
1712 name = PS2_MOUSE_TRACKPOINT_NAME;
1713 product = PS2_MOUSE_TRACKPOINT_PRODUCT;
1714 nbuttons = 3;
1715 is_pointing_stick = true;
1716 break;
1717
1718 case MOUSE_MODEL_ELANTECH:
1719 name = PS2_MOUSE_ELANTECH_ST_NAME;
1720 product = PS2_MOUSE_ELANTECH_PRODUCT;
1721 nbuttons = 3;
1722 is_pointing_stick = true;
1723 break;
1724
1725 case MOUSE_MODEL_MOUSEMANPLUS:
1726 case MOUSE_MODEL_4D:
1727 nwheels = 2;
1728 break;
1729
1730 case MOUSE_MODEL_EXPLORER:
1731 case MOUSE_MODEL_INTELLI:
1732 case MOUSE_MODEL_NET:
1733 case MOUSE_MODEL_NETSCROLL:
1734 case MOUSE_MODEL_4DPLUS:
1735 nwheels = 1;
1736 break;
1737 }
1738
1739 evdev_r = evdev_alloc();
1740 evdev_set_name(evdev_r, name);
1741 evdev_set_phys(evdev_r, device_get_nameunit(dev));
1742 evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0);
1743 evdev_set_methods(evdev_r, sc, &psm_ev_methods_r);
1744
1745 evdev_support_prop(evdev_r, INPUT_PROP_POINTER);
1746 if (is_pointing_stick)
1747 evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK);
1748 evdev_support_event(evdev_r, EV_SYN);
1749 evdev_support_event(evdev_r, EV_KEY);
1750 evdev_support_event(evdev_r, EV_REL);
1751 evdev_support_rel(evdev_r, REL_X);
1752 evdev_support_rel(evdev_r, REL_Y);
1753 switch (nwheels) {
1754 case 2:
1755 evdev_support_rel(evdev_r, REL_HWHEEL);
1756 /* FALLTHROUGH */
1757 case 1:
1758 evdev_support_rel(evdev_r, REL_WHEEL);
1759 }
1760 for (i = 0; i < nbuttons; i++)
1761 evdev_support_key(evdev_r, BTN_MOUSE + i);
1762
1763 error = evdev_register_mtx(evdev_r, &Giant);
1764 if (error)
1765 evdev_free(evdev_r);
1766 else
1767 sc->evdev_r = evdev_r;
1768 return (error);
1769}
1770
1771static int
1772psm_register_synaptics(device_t dev)
1773{
1774 struct psm_softc *sc = device_get_softc(dev);
1775 const uint16_t synaptics_absinfo_st[][4] = {
1776 { ABS_X, sc->synhw.minimumXCoord,
1777 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1778 { ABS_Y, sc->synhw.minimumYCoord,
1779 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1780 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1781 ABSINFO_END,
1782 };
1783 const uint16_t synaptics_absinfo_mt[][4] = {
1784 { ABS_MT_SLOT, 0, PSM_FINGERS-1, 0},
1785 { ABS_MT_TRACKING_ID, -1, PSM_FINGERS-1, 0},
1786 { ABS_MT_POSITION_X, sc->synhw.minimumXCoord,
1787 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1788 { ABS_MT_POSITION_Y, sc->synhw.minimumYCoord,
1789 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1790 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1791 ABSINFO_END,
1792 };
1793 struct evdev_dev *evdev_a;
1794 int error, i, guest_model;
1795
1796 evdev_a = evdev_alloc();
1797 evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME);
1798 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1799 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1800 PS2_MOUSE_SYNAPTICS_PRODUCT, 0);
1801 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1802
1803 evdev_support_event(evdev_a, EV_SYN);
1804 evdev_support_event(evdev_a, EV_KEY);
1805 evdev_support_event(evdev_a, EV_ABS);
1806 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1807 if (sc->synhw.capAdvancedGestures)
1808 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1809 if (sc->synhw.capClickPad)
1810 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1811 evdev_support_key(evdev_a, BTN_TOUCH);
1812 evdev_support_nfingers(evdev_a, 3);
1813 psm_support_abs_bulk(evdev_a, synaptics_absinfo_st);
1814 if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1815 psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt);
1816 if (sc->synhw.capPalmDetect)
1817 evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 0, 15, 0, 0, 0);
1818 evdev_support_key(evdev_a, BTN_LEFT);
1819 if (!sc->synhw.capClickPad) {
1820 evdev_support_key(evdev_a, BTN_RIGHT);
1821 if (sc->synhw.capExtended && sc->synhw.capMiddle)
1822 evdev_support_key(evdev_a, BTN_MIDDLE);
1823 }
1824 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
1825 evdev_support_key(evdev_a, BTN_BACK);
1826 evdev_support_key(evdev_a, BTN_FORWARD);
1827 }
1828 if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0))
1829 for (i = 0; i < sc->synhw.nExtendedButtons; i++)
1830 evdev_support_key(evdev_a, BTN_0 + i);
1831
1832 error = evdev_register_mtx(evdev_a, &Giant);
1379 if (bootverbose)
1380 ++verbose;
1381
1382 device_set_desc(dev, "PS/2 Mouse");
1383
1384 if (!kbdc_lock(sc->kbdc, TRUE)) {
1385 printf("psm%d: unable to lock the controller.\n", unit);
1386 if (bootverbose)
1387 --verbose;
1388 return (ENXIO);
1389 }
1390
1391 /*
1392 * NOTE: two bits in the command byte controls the operation of the
1393 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
1394 * port interrupt (IRQ 12) enable bit (bit 2).
1395 */
1396
1397 /* discard anything left after the keyboard initialization */
1398 empty_both_buffers(sc->kbdc, 10);
1399
1400 /* save the current command byte; it will be used later */
1401 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
1402 command_byte = get_controller_command_byte(sc->kbdc);
1403 if (verbose)
1404 printf("psm%d: current command byte:%04x\n", unit,
1405 command_byte);
1406 if (command_byte == -1) {
1407 /* CONTROLLER ERROR */
1408 printf("psm%d: unable to get the current command byte value.\n",
1409 unit);
1410 endprobe(ENXIO);
1411 }
1412
1413 /*
1414 * disable the keyboard port while probing the aux port, which must be
1415 * enabled during this routine
1416 */
1417 if (!set_controller_command_byte(sc->kbdc,
1418 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1419 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
1420 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1421 /*
1422 * this is CONTROLLER ERROR; I don't know how to recover
1423 * from this error...
1424 */
1425 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1426 restore_controller(sc->kbdc, command_byte);
1427 printf("psm%d: unable to set the command byte.\n", unit);
1428 endprobe(ENXIO);
1429 }
1430 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
1431
1432 /*
1433 * NOTE: `test_aux_port()' is designed to return with zero if the aux
1434 * port exists and is functioning. However, some controllers appears
1435 * to respond with zero even when the aux port doesn't exist. (It may
1436 * be that this is only the case when the controller DOES have the aux
1437 * port but the port is not wired on the motherboard.) The keyboard
1438 * controllers without the port, such as the original AT, are
1439 * supposed to return with an error code or simply time out. In any
1440 * case, we have to continue probing the port even when the controller
1441 * passes this test.
1442 *
1443 * XXX: some controllers erroneously return the error code 1, 2 or 3
1444 * when it has a perfectly functional aux port. We have to ignore
1445 * this error code. Even if the controller HAS error with the aux
1446 * port, it will be detected later...
1447 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1448 */
1449 switch ((i = test_aux_port(sc->kbdc))) {
1450 case 1: /* ignore these errors */
1451 case 2:
1452 case 3:
1453 case PSM_ACK:
1454 if (verbose)
1455 printf("psm%d: strange result for test aux port "
1456 "(%d).\n", unit, i);
1457 /* FALLTHROUGH */
1458 case 0: /* no error */
1459 break;
1460 case -1: /* time out */
1461 default: /* error */
1462 recover_from_error(sc->kbdc);
1463 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1464 break;
1465 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1466 restore_controller(sc->kbdc, command_byte);
1467 if (verbose)
1468 printf("psm%d: the aux port is not functioning (%d).\n",
1469 unit, i);
1470 endprobe(ENXIO);
1471 }
1472
1473 if (sc->config & PSM_CONFIG_NORESET) {
1474 /*
1475 * Don't try to reset the pointing device. It may possibly be
1476 * left in an unknown state, though...
1477 */
1478 } else {
1479 /*
1480 * NOTE: some controllers appears to hang the `keyboard' when
1481 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
1482 *
1483 * Attempt to reset the controller twice -- this helps
1484 * pierce through some KVM switches. The second reset
1485 * is non-fatal.
1486 */
1487 if (!reset_aux_dev(sc->kbdc)) {
1488 recover_from_error(sc->kbdc);
1489 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1490 restore_controller(sc->kbdc, command_byte);
1491 if (verbose)
1492 printf("psm%d: failed to reset the aux "
1493 "device.\n", unit);
1494 endprobe(ENXIO);
1495 } else if (!reset_aux_dev(sc->kbdc)) {
1496 recover_from_error(sc->kbdc);
1497 if (verbose >= 2)
1498 printf("psm%d: failed to reset the aux device "
1499 "(2).\n", unit);
1500 }
1501 }
1502
1503 /*
1504 * both the aux port and the aux device are functioning, see if the
1505 * device can be enabled. NOTE: when enabled, the device will start
1506 * sending data; we shall immediately disable the device once we know
1507 * the device can be enabled.
1508 */
1509 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1510 /* MOUSE ERROR */
1511 recover_from_error(sc->kbdc);
1512 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1513 restore_controller(sc->kbdc, command_byte);
1514 if (verbose)
1515 printf("psm%d: failed to enable the aux device.\n",
1516 unit);
1517 endprobe(ENXIO);
1518 }
1519
1520 /* save the default values after reset */
1521 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1522 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1523 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1524 } else {
1525 sc->dflt_mode.rate = sc->mode.rate = -1;
1526 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1527 }
1528
1529 /* hardware information */
1530 sc->hw.iftype = MOUSE_IF_PS2;
1531
1532 /* verify the device is a mouse */
1533 sc->hw.hwid = get_aux_id(sc->kbdc);
1534 if (!is_a_mouse(sc->hw.hwid)) {
1535 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1536 restore_controller(sc->kbdc, command_byte);
1537 if (verbose)
1538 printf("psm%d: unknown device type (%d).\n", unit,
1539 sc->hw.hwid);
1540 endprobe(ENXIO);
1541 }
1542 switch (sc->hw.hwid) {
1543 case PSM_BALLPOINT_ID:
1544 sc->hw.type = MOUSE_TRACKBALL;
1545 break;
1546 case PSM_MOUSE_ID:
1547 case PSM_INTELLI_ID:
1548 case PSM_EXPLORER_ID:
1549 case PSM_4DMOUSE_ID:
1550 case PSM_4DPLUS_ID:
1551 sc->hw.type = MOUSE_MOUSE;
1552 break;
1553 default:
1554 sc->hw.type = MOUSE_UNKNOWN;
1555 break;
1556 }
1557
1558 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1559 sc->hw.buttons = 2;
1560 i = GENERIC_MOUSE_ENTRY;
1561 } else {
1562 /* # of buttons */
1563 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1564
1565 /* other parameters */
1566 for (i = 0; vendortype[i].probefunc != NULL; ++i)
1567 if ((*vendortype[i].probefunc)(sc, PROBE)) {
1568 if (verbose >= 2)
1569 printf("psm%d: found %s\n", unit,
1570 model_name(vendortype[i].model));
1571 break;
1572 }
1573 }
1574
1575 sc->hw.model = vendortype[i].model;
1576
1577 sc->dflt_mode.level = PSM_LEVEL_BASE;
1578 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1579 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1580 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1581 sc->dflt_mode.syncmask[0] = 0;
1582 else
1583 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1584 if (sc->config & PSM_CONFIG_FORCETAP)
1585 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1586 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1587 sc->mode = sc->dflt_mode;
1588 sc->mode.packetsize = vendortype[i].packetsize;
1589
1590 /* set mouse parameters */
1591#if 0
1592 /*
1593 * A version of Logitech FirstMouse+ won't report wheel movement,
1594 * if SET_DEFAULTS is sent... Don't use this command.
1595 * This fix was found by Takashi Nishida.
1596 */
1597 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1598 if (verbose >= 2)
1599 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1600#endif
1601 if (sc->config & PSM_CONFIG_RESOLUTION)
1602 sc->mode.resolution =
1603 set_mouse_resolution(sc->kbdc,
1604 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1605 else if (sc->mode.resolution >= 0)
1606 sc->mode.resolution =
1607 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1608 if (sc->mode.rate > 0)
1609 sc->mode.rate =
1610 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1611 set_mouse_scaling(sc->kbdc, 1);
1612
1613 /* Record sync on the next data packet we see. */
1614 sc->flags |= PSM_NEED_SYNCBITS;
1615
1616 /* just check the status of the mouse */
1617 /*
1618 * NOTE: XXX there are some arcane controller/mouse combinations out
1619 * there, which hung the controller unless there is data transmission
1620 * after ACK from the mouse.
1621 */
1622 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1623 printf("psm%d: failed to get status.\n", unit);
1624 else {
1625 /*
1626 * When in its native mode, some mice operate with different
1627 * default parameters than in the PS/2 compatible mode.
1628 */
1629 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1630 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1631 }
1632
1633 /* disable the aux port for now... */
1634 if (!set_controller_command_byte(sc->kbdc,
1635 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1636 (command_byte & KBD_KBD_CONTROL_BITS) |
1637 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1638 /*
1639 * this is CONTROLLER ERROR; I don't know the proper way to
1640 * recover from this error...
1641 */
1642 if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc))
1643 restore_controller(sc->kbdc, command_byte);
1644 printf("psm%d: unable to set the command byte.\n", unit);
1645 endprobe(ENXIO);
1646 }
1647
1648 /* done */
1649 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1650 kbdc_lock(sc->kbdc, FALSE);
1651 return (0);
1652}
1653
1654#ifdef EVDEV_SUPPORT
1655/* Values are taken from Linux drivers for userland software compatibility */
1656#define PS2_MOUSE_VENDOR 0x0002
1657#define PS2_MOUSE_GENERIC_PRODUCT 0x0001
1658#define PS2_MOUSE_SYNAPTICS_NAME "SynPS/2 Synaptics TouchPad"
1659#define PS2_MOUSE_SYNAPTICS_PRODUCT 0x0007
1660#define PS2_MOUSE_TRACKPOINT_NAME "TPPS/2 IBM TrackPoint"
1661#define PS2_MOUSE_TRACKPOINT_PRODUCT 0x000A
1662#define PS2_MOUSE_ELANTECH_NAME "ETPS/2 Elantech Touchpad"
1663#define PS2_MOUSE_ELANTECH_ST_NAME "ETPS/2 Elantech TrackPoint"
1664#define PS2_MOUSE_ELANTECH_PRODUCT 0x000E
1665
1666#define ABSINFO_END { ABS_CNT, 0, 0, 0 }
1667
1668static void
1669psm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4])
1670{
1671 size_t i;
1672
1673 for (i = 0; info[i][0] != ABS_CNT; i++)
1674 evdev_support_abs(evdev, info[i][0], 0, info[i][1], info[i][2],
1675 0, 0, info[i][3]);
1676}
1677
1678static void
1679psm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f)
1680{
1681 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1682
1683 evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id);
1684 evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id);
1685 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x);
1686 evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y);
1687 evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p);
1688}
1689
1690static void
1691psm_push_st_finger(struct psm_softc *sc, const finger_t *f)
1692{
1693 int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y;
1694
1695 evdev_push_abs(sc->evdev_a, ABS_X, f->x);
1696 evdev_push_abs(sc->evdev_a, ABS_Y, y);
1697 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p);
1698 if (sc->synhw.capPalmDetect)
1699 evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w);
1700}
1701
1702static void
1703psm_release_mt_slot(struct evdev_dev *evdev, int32_t slot)
1704{
1705
1706 evdev_push_abs(evdev, ABS_MT_SLOT, slot);
1707 evdev_push_abs(evdev, ABS_MT_TRACKING_ID, -1);
1708}
1709
1710static int
1711psm_register(device_t dev, int model_code)
1712{
1713 struct psm_softc *sc = device_get_softc(dev);
1714 struct evdev_dev *evdev_r;
1715 int error, i, nbuttons, nwheels, product;
1716 bool is_pointing_stick;
1717 const char *name;
1718
1719 name = model_name(model_code);
1720 nbuttons = sc->hw.buttons;
1721 product = PS2_MOUSE_GENERIC_PRODUCT;
1722 nwheels = 0;
1723 is_pointing_stick = false;
1724
1725 switch (model_code) {
1726 case MOUSE_MODEL_TRACKPOINT:
1727 name = PS2_MOUSE_TRACKPOINT_NAME;
1728 product = PS2_MOUSE_TRACKPOINT_PRODUCT;
1729 nbuttons = 3;
1730 is_pointing_stick = true;
1731 break;
1732
1733 case MOUSE_MODEL_ELANTECH:
1734 name = PS2_MOUSE_ELANTECH_ST_NAME;
1735 product = PS2_MOUSE_ELANTECH_PRODUCT;
1736 nbuttons = 3;
1737 is_pointing_stick = true;
1738 break;
1739
1740 case MOUSE_MODEL_MOUSEMANPLUS:
1741 case MOUSE_MODEL_4D:
1742 nwheels = 2;
1743 break;
1744
1745 case MOUSE_MODEL_EXPLORER:
1746 case MOUSE_MODEL_INTELLI:
1747 case MOUSE_MODEL_NET:
1748 case MOUSE_MODEL_NETSCROLL:
1749 case MOUSE_MODEL_4DPLUS:
1750 nwheels = 1;
1751 break;
1752 }
1753
1754 evdev_r = evdev_alloc();
1755 evdev_set_name(evdev_r, name);
1756 evdev_set_phys(evdev_r, device_get_nameunit(dev));
1757 evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0);
1758 evdev_set_methods(evdev_r, sc, &psm_ev_methods_r);
1759
1760 evdev_support_prop(evdev_r, INPUT_PROP_POINTER);
1761 if (is_pointing_stick)
1762 evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK);
1763 evdev_support_event(evdev_r, EV_SYN);
1764 evdev_support_event(evdev_r, EV_KEY);
1765 evdev_support_event(evdev_r, EV_REL);
1766 evdev_support_rel(evdev_r, REL_X);
1767 evdev_support_rel(evdev_r, REL_Y);
1768 switch (nwheels) {
1769 case 2:
1770 evdev_support_rel(evdev_r, REL_HWHEEL);
1771 /* FALLTHROUGH */
1772 case 1:
1773 evdev_support_rel(evdev_r, REL_WHEEL);
1774 }
1775 for (i = 0; i < nbuttons; i++)
1776 evdev_support_key(evdev_r, BTN_MOUSE + i);
1777
1778 error = evdev_register_mtx(evdev_r, &Giant);
1779 if (error)
1780 evdev_free(evdev_r);
1781 else
1782 sc->evdev_r = evdev_r;
1783 return (error);
1784}
1785
1786static int
1787psm_register_synaptics(device_t dev)
1788{
1789 struct psm_softc *sc = device_get_softc(dev);
1790 const uint16_t synaptics_absinfo_st[][4] = {
1791 { ABS_X, sc->synhw.minimumXCoord,
1792 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1793 { ABS_Y, sc->synhw.minimumYCoord,
1794 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1795 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1796 ABSINFO_END,
1797 };
1798 const uint16_t synaptics_absinfo_mt[][4] = {
1799 { ABS_MT_SLOT, 0, PSM_FINGERS-1, 0},
1800 { ABS_MT_TRACKING_ID, -1, PSM_FINGERS-1, 0},
1801 { ABS_MT_POSITION_X, sc->synhw.minimumXCoord,
1802 sc->synhw.maximumXCoord, sc->synhw.infoXupmm },
1803 { ABS_MT_POSITION_Y, sc->synhw.minimumYCoord,
1804 sc->synhw.maximumYCoord, sc->synhw.infoYupmm },
1805 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1806 ABSINFO_END,
1807 };
1808 struct evdev_dev *evdev_a;
1809 int error, i, guest_model;
1810
1811 evdev_a = evdev_alloc();
1812 evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME);
1813 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1814 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1815 PS2_MOUSE_SYNAPTICS_PRODUCT, 0);
1816 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1817
1818 evdev_support_event(evdev_a, EV_SYN);
1819 evdev_support_event(evdev_a, EV_KEY);
1820 evdev_support_event(evdev_a, EV_ABS);
1821 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1822 if (sc->synhw.capAdvancedGestures)
1823 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1824 if (sc->synhw.capClickPad)
1825 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1826 evdev_support_key(evdev_a, BTN_TOUCH);
1827 evdev_support_nfingers(evdev_a, 3);
1828 psm_support_abs_bulk(evdev_a, synaptics_absinfo_st);
1829 if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV)
1830 psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt);
1831 if (sc->synhw.capPalmDetect)
1832 evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 0, 15, 0, 0, 0);
1833 evdev_support_key(evdev_a, BTN_LEFT);
1834 if (!sc->synhw.capClickPad) {
1835 evdev_support_key(evdev_a, BTN_RIGHT);
1836 if (sc->synhw.capExtended && sc->synhw.capMiddle)
1837 evdev_support_key(evdev_a, BTN_MIDDLE);
1838 }
1839 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
1840 evdev_support_key(evdev_a, BTN_BACK);
1841 evdev_support_key(evdev_a, BTN_FORWARD);
1842 }
1843 if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0))
1844 for (i = 0; i < sc->synhw.nExtendedButtons; i++)
1845 evdev_support_key(evdev_a, BTN_0 + i);
1846
1847 error = evdev_register_mtx(evdev_a, &Giant);
1833 if (!error && sc->synhw.capPassthrough) {
1848 if (!error && (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX)) {
1834 guest_model = sc->tpinfo.sysctl_tree != NULL ?
1835 MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC;
1836 error = psm_register(dev, guest_model);
1837 }
1838 if (error)
1839 evdev_free(evdev_a);
1840 else
1841 sc->evdev_a = evdev_a;
1842 return (error);
1843}
1844
1845static int
1846psm_register_elantech(device_t dev)
1847{
1848 struct psm_softc *sc = device_get_softc(dev);
1849 const uint16_t elantech_absinfo[][4] = {
1850 { ABS_X, 0, sc->elanhw.sizex,
1851 sc->elanhw.dpmmx },
1852 { ABS_Y, 0, sc->elanhw.sizey,
1853 sc->elanhw.dpmmy },
1854 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1855 { ABS_TOOL_WIDTH, 0, ELANTECH_FINGER_MAX_W, 0 },
1856 { ABS_MT_SLOT, 0, ELANTECH_MAX_FINGERS - 1, 0 },
1857 { ABS_MT_TRACKING_ID, -1, ELANTECH_MAX_FINGERS - 1, 0 },
1858 { ABS_MT_POSITION_X, 0, sc->elanhw.sizex,
1859 sc->elanhw.dpmmx },
1860 { ABS_MT_POSITION_Y, 0, sc->elanhw.sizey,
1861 sc->elanhw.dpmmy },
1862 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1863 { ABS_MT_TOUCH_MAJOR, 0, ELANTECH_FINGER_MAX_W *
1864 sc->elanhw.dptracex, 0 },
1865 ABSINFO_END,
1866 };
1867 struct evdev_dev *evdev_a;
1868 int error;
1869
1870 evdev_a = evdev_alloc();
1871 evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME);
1872 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1873 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1874 PS2_MOUSE_ELANTECH_PRODUCT, 0);
1875 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1876
1877 evdev_support_event(evdev_a, EV_SYN);
1878 evdev_support_event(evdev_a, EV_KEY);
1879 evdev_support_event(evdev_a, EV_ABS);
1880 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1881 if (sc->elanhw.issemimt)
1882 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1883 if (sc->elanhw.isclickpad)
1884 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1885 evdev_support_key(evdev_a, BTN_TOUCH);
1886 evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS);
1887 evdev_support_key(evdev_a, BTN_LEFT);
1888 if (!sc->elanhw.isclickpad)
1889 evdev_support_key(evdev_a, BTN_RIGHT);
1890 psm_support_abs_bulk(evdev_a, elantech_absinfo);
1891
1892 error = evdev_register_mtx(evdev_a, &Giant);
1893 if (!error && sc->elanhw.hastrackpoint)
1894 error = psm_register(dev, MOUSE_MODEL_ELANTECH);
1895 if (error)
1896 evdev_free(evdev_a);
1897 else
1898 sc->evdev_a = evdev_a;
1899 return (error);
1900}
1901#endif
1902
1903static int
1904psmattach(device_t dev)
1905{
1906 int unit = device_get_unit(dev);
1907 struct psm_softc *sc = device_get_softc(dev);
1908 int error;
1909 int rid;
1910
1911 /* Setup initial state */
1912 sc->state = PSM_VALID;
1913 callout_init(&sc->callout, 0);
1914 callout_init(&sc->softcallout, 0);
1915
1916 /* Setup our interrupt handler */
1917 rid = KBDC_RID_AUX;
1918 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1919 if (sc->intr == NULL)
1920 return (ENXIO);
1921 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1922 &sc->ih);
1923 if (error) {
1924 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1925 return (error);
1926 }
1927
1928 /* Done */
1929 sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1930 sc->dev->si_drv1 = sc;
1931 sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1932 sc->bdev->si_drv1 = sc;
1933
1934#ifdef EVDEV_SUPPORT
1935 switch (sc->hw.model) {
1936 case MOUSE_MODEL_SYNAPTICS:
1937 error = psm_register_synaptics(dev);
1938 break;
1939
1940 case MOUSE_MODEL_ELANTECH:
1941 error = psm_register_elantech(dev);
1942 break;
1943
1944 default:
1945 error = psm_register(dev, sc->hw.model);
1946 }
1947
1948 if (error)
1949 return (error);
1950#endif
1951
1952 /* Some touchpad devices need full reinitialization after suspend. */
1953 switch (sc->hw.model) {
1954 case MOUSE_MODEL_SYNAPTICS:
1955 case MOUSE_MODEL_GLIDEPOINT:
1956 case MOUSE_MODEL_VERSAPAD:
1957 case MOUSE_MODEL_ELANTECH:
1958 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1959 break;
1960 default:
1961 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1962 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1963 break;
1964 }
1965
1966 /* Elantech trackpad`s sync bit differs from touchpad`s one */
1967 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
1968 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) {
1969 sc->config |= PSM_CONFIG_NOCHECKSYNC;
1970 sc->flags &= ~PSM_NEED_SYNCBITS;
1971 }
1972
1973 if (!verbose)
1974 printf("psm%d: model %s, device ID %d\n",
1975 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1976 else {
1977 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1978 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1979 sc->hw.hwid >> 8, sc->hw.buttons);
1980 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1981 unit, sc->config, sc->flags, sc->mode.packetsize);
1982 printf("psm%d: syncmask:%02x, syncbits:%02x%s\n",
1983 unit, sc->mode.syncmask[0], sc->mode.syncmask[1],
1984 sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : "");
1985 }
1986
1987 if (bootverbose)
1988 --verbose;
1989
1990 return (0);
1991}
1992
1993static int
1994psmdetach(device_t dev)
1995{
1996 struct psm_softc *sc;
1997 int rid;
1998
1999 sc = device_get_softc(dev);
2000 if (sc->state & PSM_OPEN)
2001 return (EBUSY);
2002
2003#ifdef EVDEV_SUPPORT
2004 evdev_free(sc->evdev_r);
2005 evdev_free(sc->evdev_a);
2006#endif
2007
2008 rid = KBDC_RID_AUX;
2009 bus_teardown_intr(dev, sc->intr, sc->ih);
2010 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2011
2012 destroy_dev(sc->dev);
2013 destroy_dev(sc->bdev);
2014
2015 callout_drain(&sc->callout);
2016 callout_drain(&sc->softcallout);
2017
2018 return (0);
2019}
2020
2021#ifdef EVDEV_SUPPORT
2022static int
2023psm_ev_open_r(struct evdev_dev *evdev, void *ev_softc)
2024{
2025 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2026 int err = 0;
2027
2028 /* Get device data */
2029 if ((sc->state & PSM_VALID) == 0) {
2030 /* the device is no longer valid/functioning */
2031 return (ENXIO);
2032 }
2033
2034 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A)))
2035 err = psmopen(sc);
2036
2037 if (err == 0)
2038 sc->state |= PSM_EV_OPEN_R;
2039
2040 return (err);
2041}
2042
2043static void
2044psm_ev_close_r(struct evdev_dev *evdev, void *ev_softc)
2045{
2046 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2047
2048 sc->state &= ~PSM_EV_OPEN_R;
2049
2050 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A))
2051 return;
2052
2053 if (sc->state & PSM_VALID)
2054 psmclose(sc);
2055}
2056
2057static int
2058psm_ev_open_a(struct evdev_dev *evdev, void *ev_softc)
2059{
2060 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2061 int err = 0;
2062
2063 /* Get device data */
2064 if ((sc->state & PSM_VALID) == 0) {
2065 /* the device is no longer valid/functioning */
2066 return (ENXIO);
2067 }
2068
2069 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R)))
2070 err = psmopen(sc);
2071
2072 if (err == 0)
2073 sc->state |= PSM_EV_OPEN_A;
2074
2075 return (err);
2076}
2077
2078static void
2079psm_ev_close_a(struct evdev_dev *evdev, void *ev_softc)
2080{
2081 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2082
2083 sc->state &= ~PSM_EV_OPEN_A;
2084
2085 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R))
2086 return;
2087
2088 if (sc->state & PSM_VALID)
2089 psmclose(sc);
2090}
2091#endif
2092
2093static int
2094psm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td)
2095{
2096 struct psm_softc *sc;
2097 int err = 0;
2098
2099 /* Get device data */
2100 sc = dev->si_drv1;
2101 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2102 /* the device is no longer valid/functioning */
2103 return (ENXIO);
2104 }
2105
2106 /* Disallow multiple opens */
2107 if (sc->state & PSM_OPEN)
2108 return (EBUSY);
2109
2110 device_busy(devclass_get_device(psm_devclass, sc->unit));
2111
2112#ifdef EVDEV_SUPPORT
2113 /* Already opened by evdev */
2114 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2115#endif
2116 err = psmopen(sc);
2117
2118 if (err == 0)
2119 sc->state |= PSM_OPEN;
2120 else
2121 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2122
2123 return (err);
2124}
2125
2126static int
2127psm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
2128{
2129 struct psm_softc *sc;
2130 int err = 0;
2131
2132 /* Get device data */
2133 sc = dev->si_drv1;
2134 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2135 /* the device is no longer valid/functioning */
2136 return (ENXIO);
2137 }
2138
2139#ifdef EVDEV_SUPPORT
2140 /* Still opened by evdev */
2141 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2142#endif
2143 err = psmclose(sc);
2144
2145 if (err == 0) {
2146 sc->state &= ~PSM_OPEN;
2147 /* clean up and sigio requests */
2148 if (sc->async != NULL) {
2149 funsetown(&sc->async);
2150 sc->async = NULL;
2151 }
2152 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2153 }
2154
2155 return (err);
2156}
2157
2158static int
2159psmopen(struct psm_softc *sc)
2160{
2161 int command_byte;
2162 int err;
2163 int s;
2164
2165 /* Initialize state */
2166 sc->mode.level = sc->dflt_mode.level;
2167 sc->mode.protocol = sc->dflt_mode.protocol;
2168 sc->watchdog = FALSE;
2169 sc->async = NULL;
2170
2171 /* flush the event queue */
2172 sc->queue.count = 0;
2173 sc->queue.head = 0;
2174 sc->queue.tail = 0;
2175 sc->status.flags = 0;
2176 sc->status.button = 0;
2177 sc->status.obutton = 0;
2178 sc->status.dx = 0;
2179 sc->status.dy = 0;
2180 sc->status.dz = 0;
2181 sc->button = 0;
2182 sc->pqueue_start = 0;
2183 sc->pqueue_end = 0;
2184
2185 /* empty input buffer */
2186 flushpackets(sc);
2187 sc->syncerrors = 0;
2188 sc->pkterrors = 0;
2189
2190 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2191 if (!kbdc_lock(sc->kbdc, TRUE))
2192 return (EIO);
2193
2194 /* save the current controller command byte */
2195 s = spltty();
2196 command_byte = get_controller_command_byte(sc->kbdc);
2197
2198 /* enable the aux port and temporalily disable the keyboard */
2199 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
2200 kbdc_get_device_mask(sc->kbdc),
2201 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2202 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2203 /* CONTROLLER ERROR; do you know how to get out of this? */
2204 kbdc_lock(sc->kbdc, FALSE);
2205 splx(s);
2206 log(LOG_ERR,
2207 "psm%d: unable to set the command byte (psmopen).\n",
2208 sc->unit);
2209 return (EIO);
2210 }
2211 /*
2212 * Now that the keyboard controller is told not to generate
2213 * the keyboard and mouse interrupts, call `splx()' to allow
2214 * the other tty interrupts. The clock interrupt may also occur,
2215 * but timeout routines will be blocked by the poll flag set
2216 * via `kbdc_lock()'
2217 */
2218 splx(s);
2219
2220 /* enable the mouse device */
2221 err = doopen(sc, command_byte);
2222
2223 /* done */
2224 kbdc_lock(sc->kbdc, FALSE);
2225 return (err);
2226}
2227
2228static int
2229psmclose(struct psm_softc *sc)
2230{
2231 int stat[3];
2232 int command_byte;
2233 int s;
2234
2235 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2236 if (!kbdc_lock(sc->kbdc, TRUE))
2237 return (EIO);
2238
2239 /* save the current controller command byte */
2240 s = spltty();
2241 command_byte = get_controller_command_byte(sc->kbdc);
2242 if (command_byte == -1) {
2243 kbdc_lock(sc->kbdc, FALSE);
2244 splx(s);
2245 return (EIO);
2246 }
2247
2248 /* disable the aux interrupt and temporalily disable the keyboard */
2249 if (!set_controller_command_byte(sc->kbdc,
2250 kbdc_get_device_mask(sc->kbdc),
2251 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2252 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2253 log(LOG_ERR,
2254 "psm%d: failed to disable the aux int (psmclose).\n",
2255 sc->unit);
2256 /* CONTROLLER ERROR;
2257 * NOTE: we shall force our way through. Because the only
2258 * ill effect we shall see is that we may not be able
2259 * to read ACK from the mouse, and it doesn't matter much
2260 * so long as the mouse will accept the DISABLE command.
2261 */
2262 }
2263 splx(s);
2264
2265 /* stop the watchdog timer */
2266 callout_stop(&sc->callout);
2267
2268 /* remove anything left in the output buffer */
2269 empty_aux_buffer(sc->kbdc, 10);
2270
2271 /* disable the aux device, port and interrupt */
2272 if (sc->state & PSM_VALID) {
2273 if (!disable_aux_dev(sc->kbdc)) {
2274 /* MOUSE ERROR;
2275 * NOTE: we don't return (error) and continue,
2276 * pretending we have successfully disabled the device.
2277 * It's OK because the interrupt routine will discard
2278 * any data from the mouse hereafter.
2279 */
2280 log(LOG_ERR,
2281 "psm%d: failed to disable the device (psmclose).\n",
2282 sc->unit);
2283 }
2284
2285 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
2286 log(LOG_DEBUG,
2287 "psm%d: failed to get status (psmclose).\n",
2288 sc->unit);
2289 }
2290
2291 if (!set_controller_command_byte(sc->kbdc,
2292 kbdc_get_device_mask(sc->kbdc),
2293 (command_byte & KBD_KBD_CONTROL_BITS) |
2294 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2295 /*
2296 * CONTROLLER ERROR;
2297 * we shall ignore this error; see the above comment.
2298 */
2299 log(LOG_ERR,
2300 "psm%d: failed to disable the aux port (psmclose).\n",
2301 sc->unit);
2302 }
2303
2304 /* remove anything left in the output buffer */
2305 empty_aux_buffer(sc->kbdc, 10);
2306
2307 /* close is almost always successful */
2308 kbdc_lock(sc->kbdc, FALSE);
2309 return (0);
2310}
2311
2312static int
2313tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
2314 u_char *buf)
2315{
2316 static u_char butmapps2[8] = {
2317 0,
2318 MOUSE_PS2_BUTTON1DOWN,
2319 MOUSE_PS2_BUTTON2DOWN,
2320 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
2321 MOUSE_PS2_BUTTON3DOWN,
2322 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
2323 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
2324 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
2325 MOUSE_PS2_BUTTON3DOWN,
2326 };
2327 static u_char butmapmsc[8] = {
2328 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
2329 MOUSE_MSC_BUTTON3UP,
2330 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
2331 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
2332 MOUSE_MSC_BUTTON3UP,
2333 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
2334 MOUSE_MSC_BUTTON2UP,
2335 MOUSE_MSC_BUTTON1UP,
2336 0,
2337 };
2338 int mapped;
2339 int i;
2340
2341 if (sc->mode.level == PSM_LEVEL_BASE) {
2342 mapped = status->button & ~MOUSE_BUTTON4DOWN;
2343 if (status->button & MOUSE_BUTTON4DOWN)
2344 mapped |= MOUSE_BUTTON1DOWN;
2345 status->button = mapped;
2346 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
2347 i = imax(imin(status->dx, 255), -256);
2348 if (i < 0)
2349 buf[0] |= MOUSE_PS2_XNEG;
2350 buf[1] = i;
2351 i = imax(imin(status->dy, 255), -256);
2352 if (i < 0)
2353 buf[0] |= MOUSE_PS2_YNEG;
2354 buf[2] = i;
2355 return (MOUSE_PS2_PACKETSIZE);
2356 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
2357 buf[0] = MOUSE_MSC_SYNC |
2358 butmapmsc[status->button & MOUSE_STDBUTTONS];
2359 i = imax(imin(status->dx, 255), -256);
2360 buf[1] = i >> 1;
2361 buf[3] = i - buf[1];
2362 i = imax(imin(status->dy, 255), -256);
2363 buf[2] = i >> 1;
2364 buf[4] = i - buf[2];
2365 i = imax(imin(status->dz, 127), -128);
2366 buf[5] = (i >> 1) & 0x7f;
2367 buf[6] = (i - (i >> 1)) & 0x7f;
2368 buf[7] = (~status->button >> 3) & 0x7f;
2369 return (MOUSE_SYS_PACKETSIZE);
2370 }
2371 return (pb->inputbytes);
2372}
2373
2374static int
2375psmread(struct cdev *dev, struct uio *uio, int flag)
2376{
2377 struct psm_softc *sc = dev->si_drv1;
2378 u_char buf[PSM_SMALLBUFSIZE];
2379 int error = 0;
2380 int s;
2381 int l;
2382
2383 if ((sc->state & PSM_VALID) == 0)
2384 return (EIO);
2385
2386 /* block until mouse activity occurred */
2387 s = spltty();
2388 while (sc->queue.count <= 0) {
2389 if (dev != sc->bdev) {
2390 splx(s);
2391 return (EWOULDBLOCK);
2392 }
2393 sc->state |= PSM_ASLP;
2394 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
2395 sc->state &= ~PSM_ASLP;
2396 if (error) {
2397 splx(s);
2398 return (error);
2399 } else if ((sc->state & PSM_VALID) == 0) {
2400 /* the device disappeared! */
2401 splx(s);
2402 return (EIO);
2403 }
2404 }
2405 splx(s);
2406
2407 /* copy data to the user land */
2408 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
2409 s = spltty();
2410 l = imin(sc->queue.count, uio->uio_resid);
2411 if (l > sizeof(buf))
2412 l = sizeof(buf);
2413 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
2414 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
2415 sizeof(sc->queue.buf) - sc->queue.head);
2416 bcopy(&sc->queue.buf[0],
2417 &buf[sizeof(sc->queue.buf) - sc->queue.head],
2418 l - (sizeof(sc->queue.buf) - sc->queue.head));
2419 } else
2420 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
2421 sc->queue.count -= l;
2422 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
2423 splx(s);
2424 error = uiomove(buf, l, uio);
2425 if (error)
2426 break;
2427 }
2428
2429 return (error);
2430}
2431
2432static int
2433block_mouse_data(struct psm_softc *sc, int *c)
2434{
2435 int s;
2436
2437 if (!kbdc_lock(sc->kbdc, TRUE))
2438 return (EIO);
2439
2440 s = spltty();
2441 *c = get_controller_command_byte(sc->kbdc);
2442 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
2443 kbdc_get_device_mask(sc->kbdc),
2444 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2445 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2446 /* this is CONTROLLER ERROR */
2447 splx(s);
2448 kbdc_lock(sc->kbdc, FALSE);
2449 return (EIO);
2450 }
2451
2452 /*
2453 * The device may be in the middle of status data transmission.
2454 * The transmission will be interrupted, thus, incomplete status
2455 * data must be discarded. Although the aux interrupt is disabled
2456 * at the keyboard controller level, at most one aux interrupt
2457 * may have already been pending and a data byte is in the
2458 * output buffer; throw it away. Note that the second argument
2459 * to `empty_aux_buffer()' is zero, so that the call will just
2460 * flush the internal queue.
2461 * `psmintr()' will be invoked after `splx()' if an interrupt is
2462 * pending; it will see no data and returns immediately.
2463 */
2464 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2465 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2466 flushpackets(sc);
2467 splx(s);
2468
2469 return (0);
2470}
2471
2472static void
2473dropqueue(struct psm_softc *sc)
2474{
2475
2476 sc->queue.count = 0;
2477 sc->queue.head = 0;
2478 sc->queue.tail = 0;
2479 if ((sc->state & PSM_SOFTARMED) != 0) {
2480 sc->state &= ~PSM_SOFTARMED;
2481 callout_stop(&sc->softcallout);
2482 }
2483 sc->pqueue_start = sc->pqueue_end;
2484}
2485
2486static void
2487flushpackets(struct psm_softc *sc)
2488{
2489
2490 dropqueue(sc);
2491 bzero(&sc->pqueue, sizeof(sc->pqueue));
2492}
2493
2494static int
2495unblock_mouse_data(struct psm_softc *sc, int c)
2496{
2497 int error = 0;
2498
2499 /*
2500 * We may have seen a part of status data during `set_mouse_XXX()'.
2501 * they have been queued; flush it.
2502 */
2503 empty_aux_buffer(sc->kbdc, 0);
2504
2505 /* restore ports and interrupt */
2506 if (!set_controller_command_byte(sc->kbdc,
2507 kbdc_get_device_mask(sc->kbdc),
2508 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2509 /*
2510 * CONTROLLER ERROR; this is serious, we may have
2511 * been left with the inaccessible keyboard and
2512 * the disabled mouse interrupt.
2513 */
2514 error = EIO;
2515 }
2516
2517 kbdc_lock(sc->kbdc, FALSE);
2518 return (error);
2519}
2520
2521static int
2522psmwrite(struct cdev *dev, struct uio *uio, int flag)
2523{
2524 struct psm_softc *sc = dev->si_drv1;
2525 u_char buf[PSM_SMALLBUFSIZE];
2526 int error = 0, i, l;
2527
2528 if ((sc->state & PSM_VALID) == 0)
2529 return (EIO);
2530
2531 if (sc->mode.level < PSM_LEVEL_NATIVE)
2532 return (ENODEV);
2533
2534 /* copy data from the user land */
2535 while (uio->uio_resid > 0) {
2536 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2537 error = uiomove(buf, l, uio);
2538 if (error)
2539 break;
2540 for (i = 0; i < l; i++) {
2541 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2542 if (!write_aux_command(sc->kbdc, buf[i])) {
2543 VLOG(2, (LOG_DEBUG,
2544 "psm: cmd 0x%x failed.\n", buf[i]));
2545 return (reinitialize(sc, FALSE));
2546 }
2547 }
2548 }
2549
2550 return (error);
2551}
2552
2553static int
2554psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2555 struct thread *td)
2556{
2557 struct psm_softc *sc = dev->si_drv1;
2558 mousemode_t mode;
2559 mousestatus_t status;
2560#if (defined(MOUSE_GETVARS))
2561 mousevar_t *var;
2562#endif
2563 mousedata_t *data;
2564 int stat[3];
2565 int command_byte;
2566 int error = 0;
2567 int s;
2568
2569 /* Perform IOCTL command */
2570 switch (cmd) {
2571
2572 case OLD_MOUSE_GETHWINFO:
2573 s = spltty();
2574 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2575 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2576 ((old_mousehw_t *)addr)->type = sc->hw.type;
2577 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2578 splx(s);
2579 break;
2580
2581 case MOUSE_GETHWINFO:
2582 s = spltty();
2583 *(mousehw_t *)addr = sc->hw;
2584 if (sc->mode.level == PSM_LEVEL_BASE)
2585 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2586 splx(s);
2587 break;
2588
2589 case MOUSE_SYN_GETHWINFO:
2590 s = spltty();
2591 if (sc->synhw.infoMajor >= 4)
2592 *(synapticshw_t *)addr = sc->synhw;
2593 else
2594 error = EINVAL;
2595 splx(s);
2596 break;
2597
2598 case OLD_MOUSE_GETMODE:
2599 s = spltty();
2600 switch (sc->mode.level) {
2601 case PSM_LEVEL_BASE:
2602 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2603 break;
2604 case PSM_LEVEL_STANDARD:
2605 ((old_mousemode_t *)addr)->protocol =
2606 MOUSE_PROTO_SYSMOUSE;
2607 break;
2608 case PSM_LEVEL_NATIVE:
2609 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2610 break;
2611 }
2612 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2613 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2614 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2615 splx(s);
2616 break;
2617
2618 case MOUSE_GETMODE:
2619 s = spltty();
2620 *(mousemode_t *)addr = sc->mode;
2621 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2622 ((mousemode_t *)addr)->syncmask[0] = 0;
2623 ((mousemode_t *)addr)->syncmask[1] = 0;
2624 }
2625 ((mousemode_t *)addr)->resolution =
2626 MOUSE_RES_LOW - sc->mode.resolution;
2627 switch (sc->mode.level) {
2628 case PSM_LEVEL_BASE:
2629 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2630 ((mousemode_t *)addr)->packetsize =
2631 MOUSE_PS2_PACKETSIZE;
2632 break;
2633 case PSM_LEVEL_STANDARD:
2634 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2635 ((mousemode_t *)addr)->packetsize =
2636 MOUSE_SYS_PACKETSIZE;
2637 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2638 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2639 break;
2640 case PSM_LEVEL_NATIVE:
2641 /* FIXME: this isn't quite correct... XXX */
2642 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2643 break;
2644 }
2645 splx(s);
2646 break;
2647
2648 case OLD_MOUSE_SETMODE:
2649 case MOUSE_SETMODE:
2650 if (cmd == OLD_MOUSE_SETMODE) {
2651 mode.rate = ((old_mousemode_t *)addr)->rate;
2652 /*
2653 * resolution old I/F new I/F
2654 * default 0 0
2655 * low 1 -2
2656 * medium low 2 -3
2657 * medium high 3 -4
2658 * high 4 -5
2659 */
2660 if (((old_mousemode_t *)addr)->resolution > 0)
2661 mode.resolution =
2662 -((old_mousemode_t *)addr)->resolution - 1;
2663 else
2664 mode.resolution = 0;
2665 mode.accelfactor =
2666 ((old_mousemode_t *)addr)->accelfactor;
2667 mode.level = -1;
2668 } else
2669 mode = *(mousemode_t *)addr;
2670
2671 /* adjust and validate parameters. */
2672 if (mode.rate > UCHAR_MAX)
2673 return (EINVAL);
2674 if (mode.rate == 0)
2675 mode.rate = sc->dflt_mode.rate;
2676 else if (mode.rate == -1)
2677 /* don't change the current setting */
2678 ;
2679 else if (mode.rate < 0)
2680 return (EINVAL);
2681 if (mode.resolution >= UCHAR_MAX)
2682 return (EINVAL);
2683 if (mode.resolution >= 200)
2684 mode.resolution = MOUSE_RES_HIGH;
2685 else if (mode.resolution >= 100)
2686 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2687 else if (mode.resolution >= 50)
2688 mode.resolution = MOUSE_RES_MEDIUMLOW;
2689 else if (mode.resolution > 0)
2690 mode.resolution = MOUSE_RES_LOW;
2691 if (mode.resolution == MOUSE_RES_DEFAULT)
2692 mode.resolution = sc->dflt_mode.resolution;
2693 else if (mode.resolution == -1)
2694 /* don't change the current setting */
2695 ;
2696 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2697 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2698 if (mode.level == -1)
2699 /* don't change the current setting */
2700 mode.level = sc->mode.level;
2701 else if ((mode.level < PSM_LEVEL_MIN) ||
2702 (mode.level > PSM_LEVEL_MAX))
2703 return (EINVAL);
2704 if (mode.accelfactor == -1)
2705 /* don't change the current setting */
2706 mode.accelfactor = sc->mode.accelfactor;
2707 else if (mode.accelfactor < 0)
2708 return (EINVAL);
2709
2710 /* don't allow anybody to poll the keyboard controller */
2711 error = block_mouse_data(sc, &command_byte);
2712 if (error)
2713 return (error);
2714
2715 /* set mouse parameters */
2716 if (mode.rate > 0)
2717 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2718 mode.rate);
2719 if (mode.resolution >= 0)
2720 mode.resolution =
2721 set_mouse_resolution(sc->kbdc, mode.resolution);
2722 set_mouse_scaling(sc->kbdc, 1);
2723 get_mouse_status(sc->kbdc, stat, 0, 3);
2724
2725 s = spltty();
2726 sc->mode.rate = mode.rate;
2727 sc->mode.resolution = mode.resolution;
2728 sc->mode.accelfactor = mode.accelfactor;
2729 sc->mode.level = mode.level;
2730 splx(s);
2731
2732 unblock_mouse_data(sc, command_byte);
2733 break;
2734
2735 case MOUSE_GETLEVEL:
2736 *(int *)addr = sc->mode.level;
2737 break;
2738
2739 case MOUSE_SETLEVEL:
2740 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2741 (*(int *)addr > PSM_LEVEL_MAX))
2742 return (EINVAL);
2743 sc->mode.level = *(int *)addr;
2744 break;
2745
2746 case MOUSE_GETSTATUS:
2747 s = spltty();
2748 status = sc->status;
2749 sc->status.flags = 0;
2750 sc->status.obutton = sc->status.button;
2751 sc->status.button = 0;
2752 sc->status.dx = 0;
2753 sc->status.dy = 0;
2754 sc->status.dz = 0;
2755 splx(s);
2756 *(mousestatus_t *)addr = status;
2757 break;
2758
2759#if (defined(MOUSE_GETVARS))
2760 case MOUSE_GETVARS:
2761 var = (mousevar_t *)addr;
2762 bzero(var, sizeof(*var));
2763 s = spltty();
2764 var->var[0] = MOUSE_VARS_PS2_SIG;
2765 var->var[1] = sc->config;
2766 var->var[2] = sc->flags;
2767 splx(s);
2768 break;
2769
2770 case MOUSE_SETVARS:
2771 return (ENODEV);
2772#endif /* MOUSE_GETVARS */
2773
2774 case MOUSE_READSTATE:
2775 case MOUSE_READDATA:
2776 data = (mousedata_t *)addr;
2777 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2778 return (EINVAL);
2779
2780 error = block_mouse_data(sc, &command_byte);
2781 if (error)
2782 return (error);
2783 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2784 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2785 error = EIO;
2786 unblock_mouse_data(sc, command_byte);
2787 break;
2788
2789#if (defined(MOUSE_SETRESOLUTION))
2790 case MOUSE_SETRESOLUTION:
2791 mode.resolution = *(int *)addr;
2792 if (mode.resolution >= UCHAR_MAX)
2793 return (EINVAL);
2794 else if (mode.resolution >= 200)
2795 mode.resolution = MOUSE_RES_HIGH;
2796 else if (mode.resolution >= 100)
2797 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2798 else if (mode.resolution >= 50)
2799 mode.resolution = MOUSE_RES_MEDIUMLOW;
2800 else if (mode.resolution > 0)
2801 mode.resolution = MOUSE_RES_LOW;
2802 if (mode.resolution == MOUSE_RES_DEFAULT)
2803 mode.resolution = sc->dflt_mode.resolution;
2804 else if (mode.resolution == -1)
2805 mode.resolution = sc->mode.resolution;
2806 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2807 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2808
2809 error = block_mouse_data(sc, &command_byte);
2810 if (error)
2811 return (error);
2812 sc->mode.resolution =
2813 set_mouse_resolution(sc->kbdc, mode.resolution);
2814 if (sc->mode.resolution != mode.resolution)
2815 error = EIO;
2816 unblock_mouse_data(sc, command_byte);
2817 break;
2818#endif /* MOUSE_SETRESOLUTION */
2819
2820#if (defined(MOUSE_SETRATE))
2821 case MOUSE_SETRATE:
2822 mode.rate = *(int *)addr;
2823 if (mode.rate > UCHAR_MAX)
2824 return (EINVAL);
2825 if (mode.rate == 0)
2826 mode.rate = sc->dflt_mode.rate;
2827 else if (mode.rate < 0)
2828 mode.rate = sc->mode.rate;
2829
2830 error = block_mouse_data(sc, &command_byte);
2831 if (error)
2832 return (error);
2833 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2834 if (sc->mode.rate != mode.rate)
2835 error = EIO;
2836 unblock_mouse_data(sc, command_byte);
2837 break;
2838#endif /* MOUSE_SETRATE */
2839
2840#if (defined(MOUSE_SETSCALING))
2841 case MOUSE_SETSCALING:
2842 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2843 return (EINVAL);
2844
2845 error = block_mouse_data(sc, &command_byte);
2846 if (error)
2847 return (error);
2848 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2849 error = EIO;
2850 unblock_mouse_data(sc, command_byte);
2851 break;
2852#endif /* MOUSE_SETSCALING */
2853
2854#if (defined(MOUSE_GETHWID))
2855 case MOUSE_GETHWID:
2856 error = block_mouse_data(sc, &command_byte);
2857 if (error)
2858 return (error);
2859 sc->hw.hwid &= ~0x00ff;
2860 sc->hw.hwid |= get_aux_id(sc->kbdc);
2861 *(int *)addr = sc->hw.hwid & 0x00ff;
2862 unblock_mouse_data(sc, command_byte);
2863 break;
2864#endif /* MOUSE_GETHWID */
2865
2866 case FIONBIO:
2867 case FIOASYNC:
2868 break;
2869 case FIOSETOWN:
2870 error = fsetown(*(int *)addr, &sc->async);
2871 break;
2872 case FIOGETOWN:
2873 *(int *) addr = fgetown(&sc->async);
2874 break;
2875 default:
2876 return (ENOTTY);
2877 }
2878
2879 return (error);
2880}
2881
2882static void
2883psmtimeout(void *arg)
2884{
2885 struct psm_softc *sc;
2886 int s;
2887
2888 sc = (struct psm_softc *)arg;
2889 s = spltty();
2890 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2891 VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2892 psmintr(sc);
2893 kbdc_lock(sc->kbdc, FALSE);
2894 }
2895 sc->watchdog = TRUE;
2896 splx(s);
2897 callout_reset(&sc->callout, hz, psmtimeout, sc);
2898}
2899
2900/* Add all sysctls under the debug.psm and hw.psm nodes */
2901static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2902static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2903
2904SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2905 "Verbosity level");
2906
2907static int psmhz = 20;
2908SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2909 "Frequency of the softcallout (in hz)");
2910static int psmerrsecs = 2;
2911SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2912 "Number of seconds during which packets will dropped after a sync error");
2913static int psmerrusecs = 0;
2914SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2915 "Microseconds to add to psmerrsecs");
2916static int psmsecs = 0;
2917SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2918 "Max number of seconds between soft interrupts");
2919static int psmusecs = 500000;
2920SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2921 "Microseconds to add to psmsecs");
2922static int pkterrthresh = 2;
2923SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2924 "Number of error packets allowed before reinitializing the mouse");
2925
2926SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2927 "Enable tap and drag gestures");
2928static int tap_threshold = PSM_TAP_THRESHOLD;
2929SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2930 "Button tap threshold");
2931static int tap_timeout = PSM_TAP_TIMEOUT;
2932SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2933 "Tap timeout for touchpads");
2934
2935/* Tunables */
2936SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2937 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2938
2939SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2940 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2941
2942SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2943 &elantech_support, 0, "Enable support for Elantech touchpads");
2944
2945static void
2946psmintr(void *arg)
2947{
2948 struct psm_softc *sc = arg;
2949 struct timeval now;
2950 int c;
2951 packetbuf_t *pb;
2952
1849 guest_model = sc->tpinfo.sysctl_tree != NULL ?
1850 MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC;
1851 error = psm_register(dev, guest_model);
1852 }
1853 if (error)
1854 evdev_free(evdev_a);
1855 else
1856 sc->evdev_a = evdev_a;
1857 return (error);
1858}
1859
1860static int
1861psm_register_elantech(device_t dev)
1862{
1863 struct psm_softc *sc = device_get_softc(dev);
1864 const uint16_t elantech_absinfo[][4] = {
1865 { ABS_X, 0, sc->elanhw.sizex,
1866 sc->elanhw.dpmmx },
1867 { ABS_Y, 0, sc->elanhw.sizey,
1868 sc->elanhw.dpmmy },
1869 { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1870 { ABS_TOOL_WIDTH, 0, ELANTECH_FINGER_MAX_W, 0 },
1871 { ABS_MT_SLOT, 0, ELANTECH_MAX_FINGERS - 1, 0 },
1872 { ABS_MT_TRACKING_ID, -1, ELANTECH_MAX_FINGERS - 1, 0 },
1873 { ABS_MT_POSITION_X, 0, sc->elanhw.sizex,
1874 sc->elanhw.dpmmx },
1875 { ABS_MT_POSITION_Y, 0, sc->elanhw.sizey,
1876 sc->elanhw.dpmmy },
1877 { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 },
1878 { ABS_MT_TOUCH_MAJOR, 0, ELANTECH_FINGER_MAX_W *
1879 sc->elanhw.dptracex, 0 },
1880 ABSINFO_END,
1881 };
1882 struct evdev_dev *evdev_a;
1883 int error;
1884
1885 evdev_a = evdev_alloc();
1886 evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME);
1887 evdev_set_phys(evdev_a, device_get_nameunit(dev));
1888 evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR,
1889 PS2_MOUSE_ELANTECH_PRODUCT, 0);
1890 evdev_set_methods(evdev_a, sc, &psm_ev_methods_a);
1891
1892 evdev_support_event(evdev_a, EV_SYN);
1893 evdev_support_event(evdev_a, EV_KEY);
1894 evdev_support_event(evdev_a, EV_ABS);
1895 evdev_support_prop(evdev_a, INPUT_PROP_POINTER);
1896 if (sc->elanhw.issemimt)
1897 evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT);
1898 if (sc->elanhw.isclickpad)
1899 evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD);
1900 evdev_support_key(evdev_a, BTN_TOUCH);
1901 evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS);
1902 evdev_support_key(evdev_a, BTN_LEFT);
1903 if (!sc->elanhw.isclickpad)
1904 evdev_support_key(evdev_a, BTN_RIGHT);
1905 psm_support_abs_bulk(evdev_a, elantech_absinfo);
1906
1907 error = evdev_register_mtx(evdev_a, &Giant);
1908 if (!error && sc->elanhw.hastrackpoint)
1909 error = psm_register(dev, MOUSE_MODEL_ELANTECH);
1910 if (error)
1911 evdev_free(evdev_a);
1912 else
1913 sc->evdev_a = evdev_a;
1914 return (error);
1915}
1916#endif
1917
1918static int
1919psmattach(device_t dev)
1920{
1921 int unit = device_get_unit(dev);
1922 struct psm_softc *sc = device_get_softc(dev);
1923 int error;
1924 int rid;
1925
1926 /* Setup initial state */
1927 sc->state = PSM_VALID;
1928 callout_init(&sc->callout, 0);
1929 callout_init(&sc->softcallout, 0);
1930
1931 /* Setup our interrupt handler */
1932 rid = KBDC_RID_AUX;
1933 sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
1934 if (sc->intr == NULL)
1935 return (ENXIO);
1936 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc,
1937 &sc->ih);
1938 if (error) {
1939 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1940 return (error);
1941 }
1942
1943 /* Done */
1944 sc->dev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "psm%d", unit);
1945 sc->dev->si_drv1 = sc;
1946 sc->bdev = make_dev(&psm_cdevsw, 0, 0, 0, 0666, "bpsm%d", unit);
1947 sc->bdev->si_drv1 = sc;
1948
1949#ifdef EVDEV_SUPPORT
1950 switch (sc->hw.model) {
1951 case MOUSE_MODEL_SYNAPTICS:
1952 error = psm_register_synaptics(dev);
1953 break;
1954
1955 case MOUSE_MODEL_ELANTECH:
1956 error = psm_register_elantech(dev);
1957 break;
1958
1959 default:
1960 error = psm_register(dev, sc->hw.model);
1961 }
1962
1963 if (error)
1964 return (error);
1965#endif
1966
1967 /* Some touchpad devices need full reinitialization after suspend. */
1968 switch (sc->hw.model) {
1969 case MOUSE_MODEL_SYNAPTICS:
1970 case MOUSE_MODEL_GLIDEPOINT:
1971 case MOUSE_MODEL_VERSAPAD:
1972 case MOUSE_MODEL_ELANTECH:
1973 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1974 break;
1975 default:
1976 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0)
1977 sc->config |= PSM_CONFIG_INITAFTERSUSPEND;
1978 break;
1979 }
1980
1981 /* Elantech trackpad`s sync bit differs from touchpad`s one */
1982 if (sc->hw.model == MOUSE_MODEL_ELANTECH &&
1983 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) {
1984 sc->config |= PSM_CONFIG_NOCHECKSYNC;
1985 sc->flags &= ~PSM_NEED_SYNCBITS;
1986 }
1987
1988 if (!verbose)
1989 printf("psm%d: model %s, device ID %d\n",
1990 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1991 else {
1992 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1993 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff,
1994 sc->hw.hwid >> 8, sc->hw.buttons);
1995 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1996 unit, sc->config, sc->flags, sc->mode.packetsize);
1997 printf("psm%d: syncmask:%02x, syncbits:%02x%s\n",
1998 unit, sc->mode.syncmask[0], sc->mode.syncmask[1],
1999 sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : "");
2000 }
2001
2002 if (bootverbose)
2003 --verbose;
2004
2005 return (0);
2006}
2007
2008static int
2009psmdetach(device_t dev)
2010{
2011 struct psm_softc *sc;
2012 int rid;
2013
2014 sc = device_get_softc(dev);
2015 if (sc->state & PSM_OPEN)
2016 return (EBUSY);
2017
2018#ifdef EVDEV_SUPPORT
2019 evdev_free(sc->evdev_r);
2020 evdev_free(sc->evdev_a);
2021#endif
2022
2023 rid = KBDC_RID_AUX;
2024 bus_teardown_intr(dev, sc->intr, sc->ih);
2025 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
2026
2027 destroy_dev(sc->dev);
2028 destroy_dev(sc->bdev);
2029
2030 callout_drain(&sc->callout);
2031 callout_drain(&sc->softcallout);
2032
2033 return (0);
2034}
2035
2036#ifdef EVDEV_SUPPORT
2037static int
2038psm_ev_open_r(struct evdev_dev *evdev, void *ev_softc)
2039{
2040 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2041 int err = 0;
2042
2043 /* Get device data */
2044 if ((sc->state & PSM_VALID) == 0) {
2045 /* the device is no longer valid/functioning */
2046 return (ENXIO);
2047 }
2048
2049 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A)))
2050 err = psmopen(sc);
2051
2052 if (err == 0)
2053 sc->state |= PSM_EV_OPEN_R;
2054
2055 return (err);
2056}
2057
2058static void
2059psm_ev_close_r(struct evdev_dev *evdev, void *ev_softc)
2060{
2061 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2062
2063 sc->state &= ~PSM_EV_OPEN_R;
2064
2065 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A))
2066 return;
2067
2068 if (sc->state & PSM_VALID)
2069 psmclose(sc);
2070}
2071
2072static int
2073psm_ev_open_a(struct evdev_dev *evdev, void *ev_softc)
2074{
2075 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2076 int err = 0;
2077
2078 /* Get device data */
2079 if ((sc->state & PSM_VALID) == 0) {
2080 /* the device is no longer valid/functioning */
2081 return (ENXIO);
2082 }
2083
2084 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R)))
2085 err = psmopen(sc);
2086
2087 if (err == 0)
2088 sc->state |= PSM_EV_OPEN_A;
2089
2090 return (err);
2091}
2092
2093static void
2094psm_ev_close_a(struct evdev_dev *evdev, void *ev_softc)
2095{
2096 struct psm_softc *sc = (struct psm_softc *)ev_softc;
2097
2098 sc->state &= ~PSM_EV_OPEN_A;
2099
2100 if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R))
2101 return;
2102
2103 if (sc->state & PSM_VALID)
2104 psmclose(sc);
2105}
2106#endif
2107
2108static int
2109psm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td)
2110{
2111 struct psm_softc *sc;
2112 int err = 0;
2113
2114 /* Get device data */
2115 sc = dev->si_drv1;
2116 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2117 /* the device is no longer valid/functioning */
2118 return (ENXIO);
2119 }
2120
2121 /* Disallow multiple opens */
2122 if (sc->state & PSM_OPEN)
2123 return (EBUSY);
2124
2125 device_busy(devclass_get_device(psm_devclass, sc->unit));
2126
2127#ifdef EVDEV_SUPPORT
2128 /* Already opened by evdev */
2129 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2130#endif
2131 err = psmopen(sc);
2132
2133 if (err == 0)
2134 sc->state |= PSM_OPEN;
2135 else
2136 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2137
2138 return (err);
2139}
2140
2141static int
2142psm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
2143{
2144 struct psm_softc *sc;
2145 int err = 0;
2146
2147 /* Get device data */
2148 sc = dev->si_drv1;
2149 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) {
2150 /* the device is no longer valid/functioning */
2151 return (ENXIO);
2152 }
2153
2154#ifdef EVDEV_SUPPORT
2155 /* Still opened by evdev */
2156 if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2157#endif
2158 err = psmclose(sc);
2159
2160 if (err == 0) {
2161 sc->state &= ~PSM_OPEN;
2162 /* clean up and sigio requests */
2163 if (sc->async != NULL) {
2164 funsetown(&sc->async);
2165 sc->async = NULL;
2166 }
2167 device_unbusy(devclass_get_device(psm_devclass, sc->unit));
2168 }
2169
2170 return (err);
2171}
2172
2173static int
2174psmopen(struct psm_softc *sc)
2175{
2176 int command_byte;
2177 int err;
2178 int s;
2179
2180 /* Initialize state */
2181 sc->mode.level = sc->dflt_mode.level;
2182 sc->mode.protocol = sc->dflt_mode.protocol;
2183 sc->watchdog = FALSE;
2184 sc->async = NULL;
2185
2186 /* flush the event queue */
2187 sc->queue.count = 0;
2188 sc->queue.head = 0;
2189 sc->queue.tail = 0;
2190 sc->status.flags = 0;
2191 sc->status.button = 0;
2192 sc->status.obutton = 0;
2193 sc->status.dx = 0;
2194 sc->status.dy = 0;
2195 sc->status.dz = 0;
2196 sc->button = 0;
2197 sc->pqueue_start = 0;
2198 sc->pqueue_end = 0;
2199
2200 /* empty input buffer */
2201 flushpackets(sc);
2202 sc->syncerrors = 0;
2203 sc->pkterrors = 0;
2204
2205 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2206 if (!kbdc_lock(sc->kbdc, TRUE))
2207 return (EIO);
2208
2209 /* save the current controller command byte */
2210 s = spltty();
2211 command_byte = get_controller_command_byte(sc->kbdc);
2212
2213 /* enable the aux port and temporalily disable the keyboard */
2214 if (command_byte == -1 || !set_controller_command_byte(sc->kbdc,
2215 kbdc_get_device_mask(sc->kbdc),
2216 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2217 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2218 /* CONTROLLER ERROR; do you know how to get out of this? */
2219 kbdc_lock(sc->kbdc, FALSE);
2220 splx(s);
2221 log(LOG_ERR,
2222 "psm%d: unable to set the command byte (psmopen).\n",
2223 sc->unit);
2224 return (EIO);
2225 }
2226 /*
2227 * Now that the keyboard controller is told not to generate
2228 * the keyboard and mouse interrupts, call `splx()' to allow
2229 * the other tty interrupts. The clock interrupt may also occur,
2230 * but timeout routines will be blocked by the poll flag set
2231 * via `kbdc_lock()'
2232 */
2233 splx(s);
2234
2235 /* enable the mouse device */
2236 err = doopen(sc, command_byte);
2237
2238 /* done */
2239 kbdc_lock(sc->kbdc, FALSE);
2240 return (err);
2241}
2242
2243static int
2244psmclose(struct psm_softc *sc)
2245{
2246 int stat[3];
2247 int command_byte;
2248 int s;
2249
2250 /* don't let timeout routines in the keyboard driver to poll the kbdc */
2251 if (!kbdc_lock(sc->kbdc, TRUE))
2252 return (EIO);
2253
2254 /* save the current controller command byte */
2255 s = spltty();
2256 command_byte = get_controller_command_byte(sc->kbdc);
2257 if (command_byte == -1) {
2258 kbdc_lock(sc->kbdc, FALSE);
2259 splx(s);
2260 return (EIO);
2261 }
2262
2263 /* disable the aux interrupt and temporalily disable the keyboard */
2264 if (!set_controller_command_byte(sc->kbdc,
2265 kbdc_get_device_mask(sc->kbdc),
2266 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2267 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2268 log(LOG_ERR,
2269 "psm%d: failed to disable the aux int (psmclose).\n",
2270 sc->unit);
2271 /* CONTROLLER ERROR;
2272 * NOTE: we shall force our way through. Because the only
2273 * ill effect we shall see is that we may not be able
2274 * to read ACK from the mouse, and it doesn't matter much
2275 * so long as the mouse will accept the DISABLE command.
2276 */
2277 }
2278 splx(s);
2279
2280 /* stop the watchdog timer */
2281 callout_stop(&sc->callout);
2282
2283 /* remove anything left in the output buffer */
2284 empty_aux_buffer(sc->kbdc, 10);
2285
2286 /* disable the aux device, port and interrupt */
2287 if (sc->state & PSM_VALID) {
2288 if (!disable_aux_dev(sc->kbdc)) {
2289 /* MOUSE ERROR;
2290 * NOTE: we don't return (error) and continue,
2291 * pretending we have successfully disabled the device.
2292 * It's OK because the interrupt routine will discard
2293 * any data from the mouse hereafter.
2294 */
2295 log(LOG_ERR,
2296 "psm%d: failed to disable the device (psmclose).\n",
2297 sc->unit);
2298 }
2299
2300 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
2301 log(LOG_DEBUG,
2302 "psm%d: failed to get status (psmclose).\n",
2303 sc->unit);
2304 }
2305
2306 if (!set_controller_command_byte(sc->kbdc,
2307 kbdc_get_device_mask(sc->kbdc),
2308 (command_byte & KBD_KBD_CONTROL_BITS) |
2309 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2310 /*
2311 * CONTROLLER ERROR;
2312 * we shall ignore this error; see the above comment.
2313 */
2314 log(LOG_ERR,
2315 "psm%d: failed to disable the aux port (psmclose).\n",
2316 sc->unit);
2317 }
2318
2319 /* remove anything left in the output buffer */
2320 empty_aux_buffer(sc->kbdc, 10);
2321
2322 /* close is almost always successful */
2323 kbdc_lock(sc->kbdc, FALSE);
2324 return (0);
2325}
2326
2327static int
2328tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status,
2329 u_char *buf)
2330{
2331 static u_char butmapps2[8] = {
2332 0,
2333 MOUSE_PS2_BUTTON1DOWN,
2334 MOUSE_PS2_BUTTON2DOWN,
2335 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
2336 MOUSE_PS2_BUTTON3DOWN,
2337 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
2338 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
2339 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN |
2340 MOUSE_PS2_BUTTON3DOWN,
2341 };
2342 static u_char butmapmsc[8] = {
2343 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP |
2344 MOUSE_MSC_BUTTON3UP,
2345 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
2346 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
2347 MOUSE_MSC_BUTTON3UP,
2348 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
2349 MOUSE_MSC_BUTTON2UP,
2350 MOUSE_MSC_BUTTON1UP,
2351 0,
2352 };
2353 int mapped;
2354 int i;
2355
2356 if (sc->mode.level == PSM_LEVEL_BASE) {
2357 mapped = status->button & ~MOUSE_BUTTON4DOWN;
2358 if (status->button & MOUSE_BUTTON4DOWN)
2359 mapped |= MOUSE_BUTTON1DOWN;
2360 status->button = mapped;
2361 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
2362 i = imax(imin(status->dx, 255), -256);
2363 if (i < 0)
2364 buf[0] |= MOUSE_PS2_XNEG;
2365 buf[1] = i;
2366 i = imax(imin(status->dy, 255), -256);
2367 if (i < 0)
2368 buf[0] |= MOUSE_PS2_YNEG;
2369 buf[2] = i;
2370 return (MOUSE_PS2_PACKETSIZE);
2371 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
2372 buf[0] = MOUSE_MSC_SYNC |
2373 butmapmsc[status->button & MOUSE_STDBUTTONS];
2374 i = imax(imin(status->dx, 255), -256);
2375 buf[1] = i >> 1;
2376 buf[3] = i - buf[1];
2377 i = imax(imin(status->dy, 255), -256);
2378 buf[2] = i >> 1;
2379 buf[4] = i - buf[2];
2380 i = imax(imin(status->dz, 127), -128);
2381 buf[5] = (i >> 1) & 0x7f;
2382 buf[6] = (i - (i >> 1)) & 0x7f;
2383 buf[7] = (~status->button >> 3) & 0x7f;
2384 return (MOUSE_SYS_PACKETSIZE);
2385 }
2386 return (pb->inputbytes);
2387}
2388
2389static int
2390psmread(struct cdev *dev, struct uio *uio, int flag)
2391{
2392 struct psm_softc *sc = dev->si_drv1;
2393 u_char buf[PSM_SMALLBUFSIZE];
2394 int error = 0;
2395 int s;
2396 int l;
2397
2398 if ((sc->state & PSM_VALID) == 0)
2399 return (EIO);
2400
2401 /* block until mouse activity occurred */
2402 s = spltty();
2403 while (sc->queue.count <= 0) {
2404 if (dev != sc->bdev) {
2405 splx(s);
2406 return (EWOULDBLOCK);
2407 }
2408 sc->state |= PSM_ASLP;
2409 error = tsleep(sc, PZERO | PCATCH, "psmrea", 0);
2410 sc->state &= ~PSM_ASLP;
2411 if (error) {
2412 splx(s);
2413 return (error);
2414 } else if ((sc->state & PSM_VALID) == 0) {
2415 /* the device disappeared! */
2416 splx(s);
2417 return (EIO);
2418 }
2419 }
2420 splx(s);
2421
2422 /* copy data to the user land */
2423 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
2424 s = spltty();
2425 l = imin(sc->queue.count, uio->uio_resid);
2426 if (l > sizeof(buf))
2427 l = sizeof(buf);
2428 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
2429 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
2430 sizeof(sc->queue.buf) - sc->queue.head);
2431 bcopy(&sc->queue.buf[0],
2432 &buf[sizeof(sc->queue.buf) - sc->queue.head],
2433 l - (sizeof(sc->queue.buf) - sc->queue.head));
2434 } else
2435 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
2436 sc->queue.count -= l;
2437 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
2438 splx(s);
2439 error = uiomove(buf, l, uio);
2440 if (error)
2441 break;
2442 }
2443
2444 return (error);
2445}
2446
2447static int
2448block_mouse_data(struct psm_softc *sc, int *c)
2449{
2450 int s;
2451
2452 if (!kbdc_lock(sc->kbdc, TRUE))
2453 return (EIO);
2454
2455 s = spltty();
2456 *c = get_controller_command_byte(sc->kbdc);
2457 if ((*c == -1) || !set_controller_command_byte(sc->kbdc,
2458 kbdc_get_device_mask(sc->kbdc),
2459 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT |
2460 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
2461 /* this is CONTROLLER ERROR */
2462 splx(s);
2463 kbdc_lock(sc->kbdc, FALSE);
2464 return (EIO);
2465 }
2466
2467 /*
2468 * The device may be in the middle of status data transmission.
2469 * The transmission will be interrupted, thus, incomplete status
2470 * data must be discarded. Although the aux interrupt is disabled
2471 * at the keyboard controller level, at most one aux interrupt
2472 * may have already been pending and a data byte is in the
2473 * output buffer; throw it away. Note that the second argument
2474 * to `empty_aux_buffer()' is zero, so that the call will just
2475 * flush the internal queue.
2476 * `psmintr()' will be invoked after `splx()' if an interrupt is
2477 * pending; it will see no data and returns immediately.
2478 */
2479 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
2480 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
2481 flushpackets(sc);
2482 splx(s);
2483
2484 return (0);
2485}
2486
2487static void
2488dropqueue(struct psm_softc *sc)
2489{
2490
2491 sc->queue.count = 0;
2492 sc->queue.head = 0;
2493 sc->queue.tail = 0;
2494 if ((sc->state & PSM_SOFTARMED) != 0) {
2495 sc->state &= ~PSM_SOFTARMED;
2496 callout_stop(&sc->softcallout);
2497 }
2498 sc->pqueue_start = sc->pqueue_end;
2499}
2500
2501static void
2502flushpackets(struct psm_softc *sc)
2503{
2504
2505 dropqueue(sc);
2506 bzero(&sc->pqueue, sizeof(sc->pqueue));
2507}
2508
2509static int
2510unblock_mouse_data(struct psm_softc *sc, int c)
2511{
2512 int error = 0;
2513
2514 /*
2515 * We may have seen a part of status data during `set_mouse_XXX()'.
2516 * they have been queued; flush it.
2517 */
2518 empty_aux_buffer(sc->kbdc, 0);
2519
2520 /* restore ports and interrupt */
2521 if (!set_controller_command_byte(sc->kbdc,
2522 kbdc_get_device_mask(sc->kbdc),
2523 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
2524 /*
2525 * CONTROLLER ERROR; this is serious, we may have
2526 * been left with the inaccessible keyboard and
2527 * the disabled mouse interrupt.
2528 */
2529 error = EIO;
2530 }
2531
2532 kbdc_lock(sc->kbdc, FALSE);
2533 return (error);
2534}
2535
2536static int
2537psmwrite(struct cdev *dev, struct uio *uio, int flag)
2538{
2539 struct psm_softc *sc = dev->si_drv1;
2540 u_char buf[PSM_SMALLBUFSIZE];
2541 int error = 0, i, l;
2542
2543 if ((sc->state & PSM_VALID) == 0)
2544 return (EIO);
2545
2546 if (sc->mode.level < PSM_LEVEL_NATIVE)
2547 return (ENODEV);
2548
2549 /* copy data from the user land */
2550 while (uio->uio_resid > 0) {
2551 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid);
2552 error = uiomove(buf, l, uio);
2553 if (error)
2554 break;
2555 for (i = 0; i < l; i++) {
2556 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i]));
2557 if (!write_aux_command(sc->kbdc, buf[i])) {
2558 VLOG(2, (LOG_DEBUG,
2559 "psm: cmd 0x%x failed.\n", buf[i]));
2560 return (reinitialize(sc, FALSE));
2561 }
2562 }
2563 }
2564
2565 return (error);
2566}
2567
2568static int
2569psmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2570 struct thread *td)
2571{
2572 struct psm_softc *sc = dev->si_drv1;
2573 mousemode_t mode;
2574 mousestatus_t status;
2575#if (defined(MOUSE_GETVARS))
2576 mousevar_t *var;
2577#endif
2578 mousedata_t *data;
2579 int stat[3];
2580 int command_byte;
2581 int error = 0;
2582 int s;
2583
2584 /* Perform IOCTL command */
2585 switch (cmd) {
2586
2587 case OLD_MOUSE_GETHWINFO:
2588 s = spltty();
2589 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
2590 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
2591 ((old_mousehw_t *)addr)->type = sc->hw.type;
2592 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
2593 splx(s);
2594 break;
2595
2596 case MOUSE_GETHWINFO:
2597 s = spltty();
2598 *(mousehw_t *)addr = sc->hw;
2599 if (sc->mode.level == PSM_LEVEL_BASE)
2600 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
2601 splx(s);
2602 break;
2603
2604 case MOUSE_SYN_GETHWINFO:
2605 s = spltty();
2606 if (sc->synhw.infoMajor >= 4)
2607 *(synapticshw_t *)addr = sc->synhw;
2608 else
2609 error = EINVAL;
2610 splx(s);
2611 break;
2612
2613 case OLD_MOUSE_GETMODE:
2614 s = spltty();
2615 switch (sc->mode.level) {
2616 case PSM_LEVEL_BASE:
2617 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2618 break;
2619 case PSM_LEVEL_STANDARD:
2620 ((old_mousemode_t *)addr)->protocol =
2621 MOUSE_PROTO_SYSMOUSE;
2622 break;
2623 case PSM_LEVEL_NATIVE:
2624 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2625 break;
2626 }
2627 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
2628 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
2629 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
2630 splx(s);
2631 break;
2632
2633 case MOUSE_GETMODE:
2634 s = spltty();
2635 *(mousemode_t *)addr = sc->mode;
2636 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2637 ((mousemode_t *)addr)->syncmask[0] = 0;
2638 ((mousemode_t *)addr)->syncmask[1] = 0;
2639 }
2640 ((mousemode_t *)addr)->resolution =
2641 MOUSE_RES_LOW - sc->mode.resolution;
2642 switch (sc->mode.level) {
2643 case PSM_LEVEL_BASE:
2644 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2645 ((mousemode_t *)addr)->packetsize =
2646 MOUSE_PS2_PACKETSIZE;
2647 break;
2648 case PSM_LEVEL_STANDARD:
2649 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
2650 ((mousemode_t *)addr)->packetsize =
2651 MOUSE_SYS_PACKETSIZE;
2652 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
2653 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
2654 break;
2655 case PSM_LEVEL_NATIVE:
2656 /* FIXME: this isn't quite correct... XXX */
2657 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
2658 break;
2659 }
2660 splx(s);
2661 break;
2662
2663 case OLD_MOUSE_SETMODE:
2664 case MOUSE_SETMODE:
2665 if (cmd == OLD_MOUSE_SETMODE) {
2666 mode.rate = ((old_mousemode_t *)addr)->rate;
2667 /*
2668 * resolution old I/F new I/F
2669 * default 0 0
2670 * low 1 -2
2671 * medium low 2 -3
2672 * medium high 3 -4
2673 * high 4 -5
2674 */
2675 if (((old_mousemode_t *)addr)->resolution > 0)
2676 mode.resolution =
2677 -((old_mousemode_t *)addr)->resolution - 1;
2678 else
2679 mode.resolution = 0;
2680 mode.accelfactor =
2681 ((old_mousemode_t *)addr)->accelfactor;
2682 mode.level = -1;
2683 } else
2684 mode = *(mousemode_t *)addr;
2685
2686 /* adjust and validate parameters. */
2687 if (mode.rate > UCHAR_MAX)
2688 return (EINVAL);
2689 if (mode.rate == 0)
2690 mode.rate = sc->dflt_mode.rate;
2691 else if (mode.rate == -1)
2692 /* don't change the current setting */
2693 ;
2694 else if (mode.rate < 0)
2695 return (EINVAL);
2696 if (mode.resolution >= UCHAR_MAX)
2697 return (EINVAL);
2698 if (mode.resolution >= 200)
2699 mode.resolution = MOUSE_RES_HIGH;
2700 else if (mode.resolution >= 100)
2701 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2702 else if (mode.resolution >= 50)
2703 mode.resolution = MOUSE_RES_MEDIUMLOW;
2704 else if (mode.resolution > 0)
2705 mode.resolution = MOUSE_RES_LOW;
2706 if (mode.resolution == MOUSE_RES_DEFAULT)
2707 mode.resolution = sc->dflt_mode.resolution;
2708 else if (mode.resolution == -1)
2709 /* don't change the current setting */
2710 ;
2711 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2712 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2713 if (mode.level == -1)
2714 /* don't change the current setting */
2715 mode.level = sc->mode.level;
2716 else if ((mode.level < PSM_LEVEL_MIN) ||
2717 (mode.level > PSM_LEVEL_MAX))
2718 return (EINVAL);
2719 if (mode.accelfactor == -1)
2720 /* don't change the current setting */
2721 mode.accelfactor = sc->mode.accelfactor;
2722 else if (mode.accelfactor < 0)
2723 return (EINVAL);
2724
2725 /* don't allow anybody to poll the keyboard controller */
2726 error = block_mouse_data(sc, &command_byte);
2727 if (error)
2728 return (error);
2729
2730 /* set mouse parameters */
2731 if (mode.rate > 0)
2732 mode.rate = set_mouse_sampling_rate(sc->kbdc,
2733 mode.rate);
2734 if (mode.resolution >= 0)
2735 mode.resolution =
2736 set_mouse_resolution(sc->kbdc, mode.resolution);
2737 set_mouse_scaling(sc->kbdc, 1);
2738 get_mouse_status(sc->kbdc, stat, 0, 3);
2739
2740 s = spltty();
2741 sc->mode.rate = mode.rate;
2742 sc->mode.resolution = mode.resolution;
2743 sc->mode.accelfactor = mode.accelfactor;
2744 sc->mode.level = mode.level;
2745 splx(s);
2746
2747 unblock_mouse_data(sc, command_byte);
2748 break;
2749
2750 case MOUSE_GETLEVEL:
2751 *(int *)addr = sc->mode.level;
2752 break;
2753
2754 case MOUSE_SETLEVEL:
2755 if ((*(int *)addr < PSM_LEVEL_MIN) ||
2756 (*(int *)addr > PSM_LEVEL_MAX))
2757 return (EINVAL);
2758 sc->mode.level = *(int *)addr;
2759 break;
2760
2761 case MOUSE_GETSTATUS:
2762 s = spltty();
2763 status = sc->status;
2764 sc->status.flags = 0;
2765 sc->status.obutton = sc->status.button;
2766 sc->status.button = 0;
2767 sc->status.dx = 0;
2768 sc->status.dy = 0;
2769 sc->status.dz = 0;
2770 splx(s);
2771 *(mousestatus_t *)addr = status;
2772 break;
2773
2774#if (defined(MOUSE_GETVARS))
2775 case MOUSE_GETVARS:
2776 var = (mousevar_t *)addr;
2777 bzero(var, sizeof(*var));
2778 s = spltty();
2779 var->var[0] = MOUSE_VARS_PS2_SIG;
2780 var->var[1] = sc->config;
2781 var->var[2] = sc->flags;
2782 splx(s);
2783 break;
2784
2785 case MOUSE_SETVARS:
2786 return (ENODEV);
2787#endif /* MOUSE_GETVARS */
2788
2789 case MOUSE_READSTATE:
2790 case MOUSE_READDATA:
2791 data = (mousedata_t *)addr;
2792 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
2793 return (EINVAL);
2794
2795 error = block_mouse_data(sc, &command_byte);
2796 if (error)
2797 return (error);
2798 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
2799 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
2800 error = EIO;
2801 unblock_mouse_data(sc, command_byte);
2802 break;
2803
2804#if (defined(MOUSE_SETRESOLUTION))
2805 case MOUSE_SETRESOLUTION:
2806 mode.resolution = *(int *)addr;
2807 if (mode.resolution >= UCHAR_MAX)
2808 return (EINVAL);
2809 else if (mode.resolution >= 200)
2810 mode.resolution = MOUSE_RES_HIGH;
2811 else if (mode.resolution >= 100)
2812 mode.resolution = MOUSE_RES_MEDIUMHIGH;
2813 else if (mode.resolution >= 50)
2814 mode.resolution = MOUSE_RES_MEDIUMLOW;
2815 else if (mode.resolution > 0)
2816 mode.resolution = MOUSE_RES_LOW;
2817 if (mode.resolution == MOUSE_RES_DEFAULT)
2818 mode.resolution = sc->dflt_mode.resolution;
2819 else if (mode.resolution == -1)
2820 mode.resolution = sc->mode.resolution;
2821 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
2822 mode.resolution = MOUSE_RES_LOW - mode.resolution;
2823
2824 error = block_mouse_data(sc, &command_byte);
2825 if (error)
2826 return (error);
2827 sc->mode.resolution =
2828 set_mouse_resolution(sc->kbdc, mode.resolution);
2829 if (sc->mode.resolution != mode.resolution)
2830 error = EIO;
2831 unblock_mouse_data(sc, command_byte);
2832 break;
2833#endif /* MOUSE_SETRESOLUTION */
2834
2835#if (defined(MOUSE_SETRATE))
2836 case MOUSE_SETRATE:
2837 mode.rate = *(int *)addr;
2838 if (mode.rate > UCHAR_MAX)
2839 return (EINVAL);
2840 if (mode.rate == 0)
2841 mode.rate = sc->dflt_mode.rate;
2842 else if (mode.rate < 0)
2843 mode.rate = sc->mode.rate;
2844
2845 error = block_mouse_data(sc, &command_byte);
2846 if (error)
2847 return (error);
2848 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
2849 if (sc->mode.rate != mode.rate)
2850 error = EIO;
2851 unblock_mouse_data(sc, command_byte);
2852 break;
2853#endif /* MOUSE_SETRATE */
2854
2855#if (defined(MOUSE_SETSCALING))
2856 case MOUSE_SETSCALING:
2857 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
2858 return (EINVAL);
2859
2860 error = block_mouse_data(sc, &command_byte);
2861 if (error)
2862 return (error);
2863 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
2864 error = EIO;
2865 unblock_mouse_data(sc, command_byte);
2866 break;
2867#endif /* MOUSE_SETSCALING */
2868
2869#if (defined(MOUSE_GETHWID))
2870 case MOUSE_GETHWID:
2871 error = block_mouse_data(sc, &command_byte);
2872 if (error)
2873 return (error);
2874 sc->hw.hwid &= ~0x00ff;
2875 sc->hw.hwid |= get_aux_id(sc->kbdc);
2876 *(int *)addr = sc->hw.hwid & 0x00ff;
2877 unblock_mouse_data(sc, command_byte);
2878 break;
2879#endif /* MOUSE_GETHWID */
2880
2881 case FIONBIO:
2882 case FIOASYNC:
2883 break;
2884 case FIOSETOWN:
2885 error = fsetown(*(int *)addr, &sc->async);
2886 break;
2887 case FIOGETOWN:
2888 *(int *) addr = fgetown(&sc->async);
2889 break;
2890 default:
2891 return (ENOTTY);
2892 }
2893
2894 return (error);
2895}
2896
2897static void
2898psmtimeout(void *arg)
2899{
2900 struct psm_softc *sc;
2901 int s;
2902
2903 sc = (struct psm_softc *)arg;
2904 s = spltty();
2905 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
2906 VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit));
2907 psmintr(sc);
2908 kbdc_lock(sc->kbdc, FALSE);
2909 }
2910 sc->watchdog = TRUE;
2911 splx(s);
2912 callout_reset(&sc->callout, hz, psmtimeout, sc);
2913}
2914
2915/* Add all sysctls under the debug.psm and hw.psm nodes */
2916static SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2917static SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse");
2918
2919SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0,
2920 "Verbosity level");
2921
2922static int psmhz = 20;
2923SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0,
2924 "Frequency of the softcallout (in hz)");
2925static int psmerrsecs = 2;
2926SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0,
2927 "Number of seconds during which packets will dropped after a sync error");
2928static int psmerrusecs = 0;
2929SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0,
2930 "Microseconds to add to psmerrsecs");
2931static int psmsecs = 0;
2932SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0,
2933 "Max number of seconds between soft interrupts");
2934static int psmusecs = 500000;
2935SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0,
2936 "Microseconds to add to psmsecs");
2937static int pkterrthresh = 2;
2938SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0,
2939 "Number of error packets allowed before reinitializing the mouse");
2940
2941SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0,
2942 "Enable tap and drag gestures");
2943static int tap_threshold = PSM_TAP_THRESHOLD;
2944SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0,
2945 "Button tap threshold");
2946static int tap_timeout = PSM_TAP_TIMEOUT;
2947SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0,
2948 "Tap timeout for touchpads");
2949
2950/* Tunables */
2951SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN,
2952 &synaptics_support, 0, "Enable support for Synaptics touchpads");
2953
2954SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN,
2955 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint");
2956
2957SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN,
2958 &elantech_support, 0, "Enable support for Elantech touchpads");
2959
2960static void
2961psmintr(void *arg)
2962{
2963 struct psm_softc *sc = arg;
2964 struct timeval now;
2965 int c;
2966 packetbuf_t *pb;
2967
2968 if (aux_mux_is_enabled(sc->kbdc))
2969 VLOG(2, (LOG_DEBUG, "psmintr: active multiplexing mode is not "
2970 "supported!\n"));
2953
2954 /* read until there is nothing to read */
2955 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2956 pb = &sc->pqueue[sc->pqueue_end];
2957
2958 /* discard the byte if the device is not open */
2959 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2960 continue;
2961
2962 getmicrouptime(&now);
2963 if ((pb->inputbytes > 0) &&
2964 timevalcmp(&now, &sc->inputtimeout, >)) {
2965 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2966 "resetting byte count\n"));
2967 pb->inputbytes = 0;
2968 sc->syncerrors = 0;
2969 sc->pkterrors = 0;
2970 }
2971 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2972 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2973 timevaladd(&sc->inputtimeout, &now);
2974
2975 pb->ipacket[pb->inputbytes++] = c;
2976
2977 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2978 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2979 sc->syncerrors = 0;
2980 sc->pkterrors = 0;
2981 goto next;
2982 } else {
2983 if (pb->inputbytes < sc->mode.packetsize)
2984 continue;
2985
2986 VLOG(4, (LOG_DEBUG,
2987 "psmintr: %02x %02x %02x %02x %02x %02x\n",
2988 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
2989 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
2990 }
2991
2992 c = pb->ipacket[0];
2993
2994 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
2995 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
2996 sc->flags &= ~PSM_NEED_SYNCBITS;
2997 VLOG(2, (LOG_DEBUG,
2998 "psmintr: Sync bytes now %04x,%04x\n",
2999 sc->mode.syncmask[0], sc->mode.syncmask[1]));
3000 } else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 &&
3001 (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
3002 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
3003 "(%04x != %04x) %d cmds since last error.\n",
3004 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
3005 sc->cmdcount - sc->lasterr));
3006 sc->lasterr = sc->cmdcount;
3007 /*
3008 * The sync byte test is a weak measure of packet
3009 * validity. Conservatively discard any input yet
3010 * to be seen by userland when we detect a sync
3011 * error since there is a good chance some of
3012 * the queued packets have undetected errors.
3013 */
3014 dropqueue(sc);
3015 if (sc->syncerrors == 0)
3016 sc->pkterrors++;
3017 ++sc->syncerrors;
3018 sc->lastinputerr = now;
3019 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
3020 sc->pkterrors >= pkterrthresh) {
3021 /*
3022 * If we've failed to find a single sync byte
3023 * in 2 packets worth of data, or we've seen
3024 * persistent packet errors during the
3025 * validation period, reinitialize the mouse
3026 * in hopes of returning it to the expected
3027 * mode.
3028 */
3029 VLOG(3, (LOG_DEBUG,
3030 "psmintr: reset the mouse.\n"));
3031 reinitialize(sc, TRUE);
3032 } else if (sc->syncerrors == sc->mode.packetsize) {
3033 /*
3034 * Try a soft reset after searching for a sync
3035 * byte through a packet length of bytes.
3036 */
3037 VLOG(3, (LOG_DEBUG,
3038 "psmintr: re-enable the mouse.\n"));
3039 pb->inputbytes = 0;
3040 disable_aux_dev(sc->kbdc);
3041 enable_aux_dev(sc->kbdc);
3042 } else {
3043 VLOG(3, (LOG_DEBUG,
3044 "psmintr: discard a byte (%d)\n",
3045 sc->syncerrors));
3046 pb->inputbytes--;
3047 bcopy(&pb->ipacket[1], &pb->ipacket[0],
3048 pb->inputbytes);
3049 }
3050 continue;
3051 }
3052
3053 /*
3054 * We have what appears to be a valid packet.
3055 * Reset the error counters.
3056 */
3057 sc->syncerrors = 0;
3058
3059 /*
3060 * Drop even good packets if they occur within a timeout
3061 * period of a sync error. This allows the detection of
3062 * a change in the mouse's packet mode without exposing
3063 * erratic mouse behavior to the user. Some KVMs forget
3064 * enhanced mouse modes during switch events.
3065 */
3066 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
3067 &now)) {
3068 pb->inputbytes = 0;
3069 continue;
3070 }
3071
3072 /*
3073 * Now that we're out of the validation period, reset
3074 * the packet error count.
3075 */
3076 sc->pkterrors = 0;
3077
3078 sc->cmdcount++;
3079next:
3080 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
3081 sc->pqueue_end = 0;
3082 /*
3083 * If we've filled the queue then call the softintr ourselves,
3084 * otherwise schedule the interrupt for later.
3085 */
3086 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
3087 (sc->pqueue_end == sc->pqueue_start)) {
3088 if ((sc->state & PSM_SOFTARMED) != 0) {
3089 sc->state &= ~PSM_SOFTARMED;
3090 callout_stop(&sc->softcallout);
3091 }
3092 psmsoftintr(arg);
3093 } else if ((sc->state & PSM_SOFTARMED) == 0) {
3094 sc->state |= PSM_SOFTARMED;
3095 callout_reset(&sc->softcallout,
3096 psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
3097 }
3098 }
3099}
3100
3101static void
3102proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3103 int *x, int *y, int *z)
3104{
3105
3106 /*
3107 * PS2++ protocol packet
3108 *
3109 * b7 b6 b5 b4 b3 b2 b1 b0
3110 * byte 1: * 1 p3 p2 1 * * *
3111 * byte 2: c1 c2 p1 p0 d1 d0 1 0
3112 *
3113 * p3-p0: packet type
3114 * c1, c2: c1 & c2 == 1, if p2 == 0
3115 * c1 & c2 == 0, if p2 == 1
3116 *
3117 * packet type: 0 (device type)
3118 * See comments in enable_mmanplus() below.
3119 *
3120 * packet type: 1 (wheel data)
3121 *
3122 * b7 b6 b5 b4 b3 b2 b1 b0
3123 * byte 3: h * B5 B4 s d2 d1 d0
3124 *
3125 * h: 1, if horizontal roller data
3126 * 0, if vertical roller data
3127 * B4, B5: button 4 and 5
3128 * s: sign bit
3129 * d2-d0: roller data
3130 *
3131 * packet type: 2 (reserved)
3132 */
3133 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
3134 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
3135 /*
3136 * the extended data packet encodes button
3137 * and wheel events
3138 */
3139 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
3140 case 1:
3141 /* wheel data packet */
3142 *x = *y = 0;
3143 if (pb->ipacket[2] & 0x80) {
3144 /* XXX horizontal roller count - ignore it */
3145 ;
3146 } else {
3147 /* vertical roller count */
3148 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
3149 (pb->ipacket[2] & 0x0f) - 16 :
3150 (pb->ipacket[2] & 0x0f);
3151 }
3152 ms->button |= (pb->ipacket[2] &
3153 MOUSE_PS2PLUS_BUTTON4DOWN) ?
3154 MOUSE_BUTTON4DOWN : 0;
3155 ms->button |= (pb->ipacket[2] &
3156 MOUSE_PS2PLUS_BUTTON5DOWN) ?
3157 MOUSE_BUTTON5DOWN : 0;
3158 break;
3159 case 2:
3160 /*
3161 * this packet type is reserved by
3162 * Logitech...
3163 */
3164 /*
3165 * IBM ScrollPoint Mouse uses this
3166 * packet type to encode both vertical
3167 * and horizontal scroll movement.
3168 */
3169 *x = *y = 0;
3170 /* horizontal count */
3171 if (pb->ipacket[2] & 0x0f)
3172 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
3173 -2 : 2;
3174 /* vertical count */
3175 if (pb->ipacket[2] & 0xf0)
3176 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
3177 -1 : 1;
3178 break;
3179 case 0:
3180 /* device type packet - shouldn't happen */
3181 /* FALLTHROUGH */
3182 default:
3183 *x = *y = 0;
3184 ms->button = ms->obutton;
3185 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
3186 "type %d: 0x%02x 0x%02x 0x%02x\n",
3187 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
3188 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
3189 break;
3190 }
3191 } else {
3192 /* preserve button states */
3193 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
3194 }
3195}
3196
3197static int
3198proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3199 int *x, int *y, int *z)
3200{
3201 static int touchpad_buttons;
3202 static int guest_buttons;
3203 static finger_t f[PSM_FINGERS];
3204 int w, id, nfingers, ewcode, extended_buttons, clickpad_pressed;
3205
3206 extended_buttons = 0;
3207
3208 /* TouchPad PS/2 absolute mode message format with capFourButtons:
3209 *
3210 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3211 * ------------------------------------------------
3212 * ipacket[0]: 1 0 W3 W2 0 W1 R L
3213 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
3214 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
3215 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L
3216 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3217 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3218 *
3219 * Legend:
3220 * L: left physical mouse button
3221 * R: right physical mouse button
3222 * D: down button
3223 * U: up button
3224 * W: "wrist" value
3225 * X: x position
3226 * Y: y position
3227 * Z: pressure
3228 *
3229 * Without capFourButtons but with nExtendeButtons and/or capMiddle
3230 *
3231 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3232 * ------------------------------------------------------
3233 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
3234 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
3235 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
3236 *
3237 * Legend:
3238 * M: Middle physical mouse button
3239 * E: Extended mouse buttons reported instead of low bits of X and Y
3240 * b1-b8: Extended mouse buttons
3241 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
3242 * 4 and 5, for reading X and Y value they should be zeroed.
3243 *
3244 * Absolute reportable limits: 0 - 6143.
3245 * Typical bezel limits: 1472 - 5472.
3246 * Typical edge marings: 1632 - 5312.
3247 *
3248 * w = 3 Passthrough Packet
3249 *
3250 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
3251 */
3252
3253 if (!synaptics_support)
3254 return (0);
3255
3256 /* Sanity check for out of sync packets. */
3257 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
3258 (pb->ipacket[3] & 0xc8) != 0xc0)
3259 return (-1);
3260
3261 *x = *y = 0;
3262 ms->button = ms->obutton;
3263
3264 /*
3265 * Pressure value.
3266 * Interpretation:
3267 * z = 0 No finger contact
3268 * z = 10 Finger hovering near the pad
3269 * z = 30 Very light finger contact
3270 * z = 80 Normal finger contact
3271 * z = 110 Very heavy finger contact
3272 * z = 200 Finger lying flat on pad surface
3273 * z = 255 Maximum reportable Z
3274 */
3275 *z = pb->ipacket[2];
3276
3277 /*
3278 * Finger width value
3279 * Interpretation:
3280 * w = 0 Two finger on the pad (capMultiFinger needed)
3281 * w = 1 Three or more fingers (capMultiFinger needed)
3282 * w = 2 Pen (instead of finger) (capPen needed)
3283 * w = 3 Reserved (passthrough?)
3284 * w = 4-7 Finger of normal width (capPalmDetect needed)
3285 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
3286 * w = 15 Maximum reportable width (capPalmDetect needed)
3287 */
3288 /* XXX Is checking capExtended enough? */
3289 if (sc->synhw.capExtended)
3290 w = ((pb->ipacket[0] & 0x30) >> 2) |
3291 ((pb->ipacket[0] & 0x04) >> 1) |
3292 ((pb->ipacket[3] & 0x04) >> 2);
3293 else {
3294 /* Assume a finger of regular width. */
3295 w = 4;
3296 }
3297
3298 switch (w) {
3299 case 3:
3300 /*
3301 * Handle packets from the guest device. See:
3302 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
3303 */
2971
2972 /* read until there is nothing to read */
2973 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
2974 pb = &sc->pqueue[sc->pqueue_end];
2975
2976 /* discard the byte if the device is not open */
2977 if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)))
2978 continue;
2979
2980 getmicrouptime(&now);
2981 if ((pb->inputbytes > 0) &&
2982 timevalcmp(&now, &sc->inputtimeout, >)) {
2983 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; "
2984 "resetting byte count\n"));
2985 pb->inputbytes = 0;
2986 sc->syncerrors = 0;
2987 sc->pkterrors = 0;
2988 }
2989 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000;
2990 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000;
2991 timevaladd(&sc->inputtimeout, &now);
2992
2993 pb->ipacket[pb->inputbytes++] = c;
2994
2995 if (sc->mode.level == PSM_LEVEL_NATIVE) {
2996 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0]));
2997 sc->syncerrors = 0;
2998 sc->pkterrors = 0;
2999 goto next;
3000 } else {
3001 if (pb->inputbytes < sc->mode.packetsize)
3002 continue;
3003
3004 VLOG(4, (LOG_DEBUG,
3005 "psmintr: %02x %02x %02x %02x %02x %02x\n",
3006 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3007 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3008 }
3009
3010 c = pb->ipacket[0];
3011
3012 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) {
3013 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]);
3014 sc->flags &= ~PSM_NEED_SYNCBITS;
3015 VLOG(2, (LOG_DEBUG,
3016 "psmintr: Sync bytes now %04x,%04x\n",
3017 sc->mode.syncmask[0], sc->mode.syncmask[1]));
3018 } else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 &&
3019 (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
3020 VLOG(3, (LOG_DEBUG, "psmintr: out of sync "
3021 "(%04x != %04x) %d cmds since last error.\n",
3022 c & sc->mode.syncmask[0], sc->mode.syncmask[1],
3023 sc->cmdcount - sc->lasterr));
3024 sc->lasterr = sc->cmdcount;
3025 /*
3026 * The sync byte test is a weak measure of packet
3027 * validity. Conservatively discard any input yet
3028 * to be seen by userland when we detect a sync
3029 * error since there is a good chance some of
3030 * the queued packets have undetected errors.
3031 */
3032 dropqueue(sc);
3033 if (sc->syncerrors == 0)
3034 sc->pkterrors++;
3035 ++sc->syncerrors;
3036 sc->lastinputerr = now;
3037 if (sc->syncerrors >= sc->mode.packetsize * 2 ||
3038 sc->pkterrors >= pkterrthresh) {
3039 /*
3040 * If we've failed to find a single sync byte
3041 * in 2 packets worth of data, or we've seen
3042 * persistent packet errors during the
3043 * validation period, reinitialize the mouse
3044 * in hopes of returning it to the expected
3045 * mode.
3046 */
3047 VLOG(3, (LOG_DEBUG,
3048 "psmintr: reset the mouse.\n"));
3049 reinitialize(sc, TRUE);
3050 } else if (sc->syncerrors == sc->mode.packetsize) {
3051 /*
3052 * Try a soft reset after searching for a sync
3053 * byte through a packet length of bytes.
3054 */
3055 VLOG(3, (LOG_DEBUG,
3056 "psmintr: re-enable the mouse.\n"));
3057 pb->inputbytes = 0;
3058 disable_aux_dev(sc->kbdc);
3059 enable_aux_dev(sc->kbdc);
3060 } else {
3061 VLOG(3, (LOG_DEBUG,
3062 "psmintr: discard a byte (%d)\n",
3063 sc->syncerrors));
3064 pb->inputbytes--;
3065 bcopy(&pb->ipacket[1], &pb->ipacket[0],
3066 pb->inputbytes);
3067 }
3068 continue;
3069 }
3070
3071 /*
3072 * We have what appears to be a valid packet.
3073 * Reset the error counters.
3074 */
3075 sc->syncerrors = 0;
3076
3077 /*
3078 * Drop even good packets if they occur within a timeout
3079 * period of a sync error. This allows the detection of
3080 * a change in the mouse's packet mode without exposing
3081 * erratic mouse behavior to the user. Some KVMs forget
3082 * enhanced mouse modes during switch events.
3083 */
3084 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs,
3085 &now)) {
3086 pb->inputbytes = 0;
3087 continue;
3088 }
3089
3090 /*
3091 * Now that we're out of the validation period, reset
3092 * the packet error count.
3093 */
3094 sc->pkterrors = 0;
3095
3096 sc->cmdcount++;
3097next:
3098 if (++sc->pqueue_end >= PSM_PACKETQUEUE)
3099 sc->pqueue_end = 0;
3100 /*
3101 * If we've filled the queue then call the softintr ourselves,
3102 * otherwise schedule the interrupt for later.
3103 */
3104 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) ||
3105 (sc->pqueue_end == sc->pqueue_start)) {
3106 if ((sc->state & PSM_SOFTARMED) != 0) {
3107 sc->state &= ~PSM_SOFTARMED;
3108 callout_stop(&sc->softcallout);
3109 }
3110 psmsoftintr(arg);
3111 } else if ((sc->state & PSM_SOFTARMED) == 0) {
3112 sc->state |= PSM_SOFTARMED;
3113 callout_reset(&sc->softcallout,
3114 psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg);
3115 }
3116 }
3117}
3118
3119static void
3120proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3121 int *x, int *y, int *z)
3122{
3123
3124 /*
3125 * PS2++ protocol packet
3126 *
3127 * b7 b6 b5 b4 b3 b2 b1 b0
3128 * byte 1: * 1 p3 p2 1 * * *
3129 * byte 2: c1 c2 p1 p0 d1 d0 1 0
3130 *
3131 * p3-p0: packet type
3132 * c1, c2: c1 & c2 == 1, if p2 == 0
3133 * c1 & c2 == 0, if p2 == 1
3134 *
3135 * packet type: 0 (device type)
3136 * See comments in enable_mmanplus() below.
3137 *
3138 * packet type: 1 (wheel data)
3139 *
3140 * b7 b6 b5 b4 b3 b2 b1 b0
3141 * byte 3: h * B5 B4 s d2 d1 d0
3142 *
3143 * h: 1, if horizontal roller data
3144 * 0, if vertical roller data
3145 * B4, B5: button 4 and 5
3146 * s: sign bit
3147 * d2-d0: roller data
3148 *
3149 * packet type: 2 (reserved)
3150 */
3151 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) &&
3152 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) {
3153 /*
3154 * the extended data packet encodes button
3155 * and wheel events
3156 */
3157 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) {
3158 case 1:
3159 /* wheel data packet */
3160 *x = *y = 0;
3161 if (pb->ipacket[2] & 0x80) {
3162 /* XXX horizontal roller count - ignore it */
3163 ;
3164 } else {
3165 /* vertical roller count */
3166 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ?
3167 (pb->ipacket[2] & 0x0f) - 16 :
3168 (pb->ipacket[2] & 0x0f);
3169 }
3170 ms->button |= (pb->ipacket[2] &
3171 MOUSE_PS2PLUS_BUTTON4DOWN) ?
3172 MOUSE_BUTTON4DOWN : 0;
3173 ms->button |= (pb->ipacket[2] &
3174 MOUSE_PS2PLUS_BUTTON5DOWN) ?
3175 MOUSE_BUTTON5DOWN : 0;
3176 break;
3177 case 2:
3178 /*
3179 * this packet type is reserved by
3180 * Logitech...
3181 */
3182 /*
3183 * IBM ScrollPoint Mouse uses this
3184 * packet type to encode both vertical
3185 * and horizontal scroll movement.
3186 */
3187 *x = *y = 0;
3188 /* horizontal count */
3189 if (pb->ipacket[2] & 0x0f)
3190 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ?
3191 -2 : 2;
3192 /* vertical count */
3193 if (pb->ipacket[2] & 0xf0)
3194 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ?
3195 -1 : 1;
3196 break;
3197 case 0:
3198 /* device type packet - shouldn't happen */
3199 /* FALLTHROUGH */
3200 default:
3201 *x = *y = 0;
3202 ms->button = ms->obutton;
3203 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet "
3204 "type %d: 0x%02x 0x%02x 0x%02x\n",
3205 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket),
3206 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2]));
3207 break;
3208 }
3209 } else {
3210 /* preserve button states */
3211 ms->button |= ms->obutton & MOUSE_EXTBUTTONS;
3212 }
3213}
3214
3215static int
3216proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
3217 int *x, int *y, int *z)
3218{
3219 static int touchpad_buttons;
3220 static int guest_buttons;
3221 static finger_t f[PSM_FINGERS];
3222 int w, id, nfingers, ewcode, extended_buttons, clickpad_pressed;
3223
3224 extended_buttons = 0;
3225
3226 /* TouchPad PS/2 absolute mode message format with capFourButtons:
3227 *
3228 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3229 * ------------------------------------------------
3230 * ipacket[0]: 1 0 W3 W2 0 W1 R L
3231 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8
3232 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0
3233 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L
3234 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
3235 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
3236 *
3237 * Legend:
3238 * L: left physical mouse button
3239 * R: right physical mouse button
3240 * D: down button
3241 * U: up button
3242 * W: "wrist" value
3243 * X: x position
3244 * Y: y position
3245 * Z: pressure
3246 *
3247 * Without capFourButtons but with nExtendeButtons and/or capMiddle
3248 *
3249 * Bits: 7 6 5 4 3 2 1 0 (LSB)
3250 * ------------------------------------------------------
3251 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L
3252 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1
3253 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2
3254 *
3255 * Legend:
3256 * M: Middle physical mouse button
3257 * E: Extended mouse buttons reported instead of low bits of X and Y
3258 * b1-b8: Extended mouse buttons
3259 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet
3260 * 4 and 5, for reading X and Y value they should be zeroed.
3261 *
3262 * Absolute reportable limits: 0 - 6143.
3263 * Typical bezel limits: 1472 - 5472.
3264 * Typical edge marings: 1632 - 5312.
3265 *
3266 * w = 3 Passthrough Packet
3267 *
3268 * Byte 2,5,6 == Byte 1,2,3 of "Guest"
3269 */
3270
3271 if (!synaptics_support)
3272 return (0);
3273
3274 /* Sanity check for out of sync packets. */
3275 if ((pb->ipacket[0] & 0xc8) != 0x80 ||
3276 (pb->ipacket[3] & 0xc8) != 0xc0)
3277 return (-1);
3278
3279 *x = *y = 0;
3280 ms->button = ms->obutton;
3281
3282 /*
3283 * Pressure value.
3284 * Interpretation:
3285 * z = 0 No finger contact
3286 * z = 10 Finger hovering near the pad
3287 * z = 30 Very light finger contact
3288 * z = 80 Normal finger contact
3289 * z = 110 Very heavy finger contact
3290 * z = 200 Finger lying flat on pad surface
3291 * z = 255 Maximum reportable Z
3292 */
3293 *z = pb->ipacket[2];
3294
3295 /*
3296 * Finger width value
3297 * Interpretation:
3298 * w = 0 Two finger on the pad (capMultiFinger needed)
3299 * w = 1 Three or more fingers (capMultiFinger needed)
3300 * w = 2 Pen (instead of finger) (capPen needed)
3301 * w = 3 Reserved (passthrough?)
3302 * w = 4-7 Finger of normal width (capPalmDetect needed)
3303 * w = 8-14 Very wide finger or palm (capPalmDetect needed)
3304 * w = 15 Maximum reportable width (capPalmDetect needed)
3305 */
3306 /* XXX Is checking capExtended enough? */
3307 if (sc->synhw.capExtended)
3308 w = ((pb->ipacket[0] & 0x30) >> 2) |
3309 ((pb->ipacket[0] & 0x04) >> 1) |
3310 ((pb->ipacket[3] & 0x04) >> 2);
3311 else {
3312 /* Assume a finger of regular width. */
3313 w = 4;
3314 }
3315
3316 switch (w) {
3317 case 3:
3318 /*
3319 * Handle packets from the guest device. See:
3320 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1
3321 */
3304 if (sc->synhw.capPassthrough) {
3322 if (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX) {
3305 *x = ((pb->ipacket[1] & 0x10) ?
3306 pb->ipacket[4] - 256 : pb->ipacket[4]);
3307 *y = ((pb->ipacket[1] & 0x20) ?
3308 pb->ipacket[5] - 256 : pb->ipacket[5]);
3309 *z = 0;
3310
3311 guest_buttons = 0;
3312 if (pb->ipacket[1] & 0x01)
3313 guest_buttons |= MOUSE_BUTTON1DOWN;
3314 if (pb->ipacket[1] & 0x04)
3315 guest_buttons |= MOUSE_BUTTON2DOWN;
3316 if (pb->ipacket[1] & 0x02)
3317 guest_buttons |= MOUSE_BUTTON3DOWN;
3318#ifdef EVDEV_SUPPORT
3319 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3320 evdev_push_rel(sc->evdev_r, REL_X, *x);
3321 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
3322 evdev_push_mouse_btn(sc->evdev_r,
3323 guest_buttons);
3324 evdev_sync(sc->evdev_r);
3325 }
3326#endif
3327 ms->button = touchpad_buttons | guest_buttons |
3328 sc->extended_buttons;
3329 }
3330 goto SYNAPTICS_END;
3331
3332 case 2:
3333 /* Handle Extended W mode packets */
3334 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
3335#if PSM_FINGERS > 1
3336 switch (ewcode) {
3337 case 1:
3338 /* Secondary finger */
3339 if (sc->synhw.capAdvancedGestures)
3340 f[1] = (finger_t) {
3341 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3342 pb->ipacket[1]) << 1,
3343 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3344 pb->ipacket[2]) << 1,
3345 .p = ((pb->ipacket[3] & 0x30) |
3346 (pb->ipacket[5] & 0x0f)) << 1,
3347 .w = PSM_FINGER_DEFAULT_W,
3348 .flags = PSM_FINGER_FUZZY,
3349 };
3350 else if (sc->synhw.capReportsV)
3351 f[1] = (finger_t) {
3352 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3353 (pb->ipacket[1] & 0xfe)) << 1,
3354 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3355 (pb->ipacket[2] & 0xfe)) << 1,
3356 .p = ((pb->ipacket[3] & 0x30) |
3357 (pb->ipacket[5] & 0x0e)) << 1,
3358 .w = (((pb->ipacket[5] & 0x01) << 2) |
3359 ((pb->ipacket[2] & 0x01) << 1) |
3360 (pb->ipacket[1] & 0x01)) + 8,
3361 .flags = PSM_FINGER_FUZZY,
3362 };
3363 default:
3364 break;
3365 }
3366#endif
3367 goto SYNAPTICS_END;
3368
3369 case 1:
3370 case 0:
3371 nfingers = w + 2;
3372 break;
3373
3374 default:
3375 nfingers = 1;
3376 }
3377
3378 if (sc->syninfo.touchpad_off)
3379 goto SYNAPTICS_END;
3380
3381 /* Button presses */
3382 touchpad_buttons = 0;
3383 if (pb->ipacket[0] & 0x01)
3384 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3385 if (pb->ipacket[0] & 0x02)
3386 touchpad_buttons |= MOUSE_BUTTON3DOWN;
3387
3388 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3389 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
3390 touchpad_buttons |= MOUSE_BUTTON4DOWN;
3391 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
3392 touchpad_buttons |= MOUSE_BUTTON5DOWN;
3393 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
3394 !sc->synhw.capClickPad) {
3395 /* Middle Button */
3396 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
3397 touchpad_buttons |= MOUSE_BUTTON2DOWN;
3398 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
3399 /* Extended Buttons */
3400 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
3401 if (sc->syninfo.directional_scrolls) {
3402 if (pb->ipacket[4] & 0x01)
3403 extended_buttons |= MOUSE_BUTTON4DOWN;
3404 if (pb->ipacket[5] & 0x01)
3405 extended_buttons |= MOUSE_BUTTON5DOWN;
3406 if (pb->ipacket[4] & 0x02)
3407 extended_buttons |= MOUSE_BUTTON6DOWN;
3408 if (pb->ipacket[5] & 0x02)
3409 extended_buttons |= MOUSE_BUTTON7DOWN;
3410 } else {
3411 if (pb->ipacket[4] & 0x01)
3412 extended_buttons |= MOUSE_BUTTON1DOWN;
3413 if (pb->ipacket[5] & 0x01)
3414 extended_buttons |= MOUSE_BUTTON3DOWN;
3415 if (pb->ipacket[4] & 0x02)
3416 extended_buttons |= MOUSE_BUTTON2DOWN;
3417 sc->extended_buttons = extended_buttons;
3418 }
3419
3420 /*
3421 * Zero out bits used by extended buttons to avoid
3422 * misinterpretation of the data absolute position.
3423 *
3424 * The bits represented by
3425 *
3426 * (nExtendedButtons + 1) >> 1
3427 *
3428 * will be masked out in both bytes.
3429 * The mask for n bits is computed with the formula
3430 *
3431 * (1 << n) - 1
3432 */
3433 int maskedbits = 0;
3434 int mask = 0;
3435 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
3436 mask = (1 << maskedbits) - 1;
3437#ifdef EVDEV_SUPPORT
3438 int i;
3439 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3440 if (sc->synhw.capPassthrough) {
3441 evdev_push_mouse_btn(sc->evdev_r,
3442 extended_buttons);
3443 evdev_sync(sc->evdev_r);
3444 }
3445 for (i = 0; i < maskedbits; i++) {
3446 evdev_push_key(sc->evdev_a,
3447 BTN_0 + i * 2,
3448 pb->ipacket[4] & (1 << i));
3449 evdev_push_key(sc->evdev_a,
3450 BTN_0 + i * 2 + 1,
3451 pb->ipacket[5] & (1 << i));
3452 }
3453 }
3454#endif
3455 pb->ipacket[4] &= ~(mask);
3456 pb->ipacket[5] &= ~(mask);
3457 } else if (!sc->syninfo.directional_scrolls &&
3458 !sc->gesture.in_vscroll) {
3459 /*
3460 * Keep reporting MOUSE DOWN until we get a new packet
3461 * indicating otherwise.
3462 */
3463 extended_buttons |= sc->extended_buttons;
3464 }
3465 }
3466
3467 if (sc->synhw.capReportsV && nfingers > 1)
3468 f[0] = (finger_t) {
3469 .x = ((pb->ipacket[3] & 0x10) << 8) |
3470 ((pb->ipacket[1] & 0x0f) << 8) |
3471 (pb->ipacket[4] & 0xfd),
3472 .y = ((pb->ipacket[3] & 0x20) << 7) |
3473 ((pb->ipacket[1] & 0xf0) << 4) |
3474 (pb->ipacket[5] & 0xfd),
3475 .p = *z & 0xfe,
3476 .w = (((pb->ipacket[2] & 0x01) << 2) |
3477 (pb->ipacket[5] & 0x02) |
3478 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
3479 .flags = PSM_FINGER_FUZZY,
3480 };
3481 else
3482 f[0] = (finger_t) {
3483 .x = ((pb->ipacket[3] & 0x10) << 8) |
3484 ((pb->ipacket[1] & 0x0f) << 8) |
3485 pb->ipacket[4],
3486 .y = ((pb->ipacket[3] & 0x20) << 7) |
3487 ((pb->ipacket[1] & 0xf0) << 4) |
3488 pb->ipacket[5],
3489 .p = *z,
3490 .w = w,
3491 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3492 };
3493
3494 /* Ignore hovering and unmeasurable touches */
3495 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3496 nfingers = 0;
3497
3498 /* Handle ClickPad */
3499 if (sc->synhw.capClickPad) {
3500 clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01;
3501 if (sc->synhw.forcePad) {
3502 /*
3503 * Forcepads erroneously report button click if there
3504 * are 2 or more fingers on the touchpad breaking
3505 * multifinger gestures. To workaround this start
3506 * reporting a click only after 4 consecutive single
3507 * touch packets has been received.
3508 * Skip these packets in case more contacts appear.
3509 */
3510 switch (nfingers) {
3511 case 0:
3512 sc->fpcount = 0;
3513 break;
3514 case 1:
3515 if (clickpad_pressed && sc->fpcount < INT_MAX)
3516 ++sc->fpcount;
3517 /* FALLTHROUGH */
3518 default:
3519 if (!clickpad_pressed)
3520 sc->fpcount = 0;
3521 if (sc->fpcount >= sc->syninfo.window_min)
3522 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3523 }
3524 } else if (clickpad_pressed)
3525 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3526 }
3527
3528 for (id = 0; id < PSM_FINGERS; id++)
3529 if (id >= nfingers)
3530 PSM_FINGER_RESET(f[id]);
3531
3532#ifdef EVDEV_SUPPORT
3533 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3534 for (id = 0; id < PSM_FINGERS; id++) {
3535 if (PSM_FINGER_IS_SET(f[id]))
3536 psm_push_mt_finger(sc, id, &f[id]);
3537 else
3538 psm_release_mt_slot(sc->evdev_a, id);
3539 }
3540 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
3541 evdev_push_nfingers(sc->evdev_a, nfingers);
3542 if (nfingers > 0)
3543 psm_push_st_finger(sc, &f[0]);
3544 else
3545 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
3546 evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons);
3547 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3548 evdev_push_key(sc->evdev_a, BTN_FORWARD,
3549 touchpad_buttons & MOUSE_BUTTON4DOWN);
3550 evdev_push_key(sc->evdev_a, BTN_BACK,
3551 touchpad_buttons & MOUSE_BUTTON5DOWN);
3552 }
3553 evdev_sync(sc->evdev_a);
3554 }
3555#endif
3556
3557 ms->button = touchpad_buttons;
3558
3559 psmgestures(sc, &f[0], nfingers, ms);
3560 for (id = 0; id < PSM_FINGERS; id++)
3561 psmsmoother(sc, &f[id], id, ms, x, y);
3562
3563 /* Palm detection doesn't terminate the current action. */
3564 if (psmpalmdetect(sc, &f[0], nfingers)) {
3565 *x = *y = *z = 0;
3566 ms->button = ms->obutton;
3567 return (0);
3568 }
3569
3570 ms->button |= extended_buttons | guest_buttons;
3571
3572SYNAPTICS_END:
3573 /*
3574 * Use the extra buttons as a scrollwheel
3575 *
3576 * XXX X.Org uses the Z axis for vertical wheel only,
3577 * whereas moused(8) understands special values to differ
3578 * vertical and horizontal wheels.
3579 *
3580 * xf86-input-mouse needs therefore a small patch to
3581 * understand these special values. Without it, the
3582 * horizontal wheel acts as a vertical wheel in X.Org.
3583 *
3584 * That's why the horizontal wheel is disabled by
3585 * default for now.
3586 */
3587 if (ms->button & MOUSE_BUTTON4DOWN)
3588 *z = -1;
3589 else if (ms->button & MOUSE_BUTTON5DOWN)
3590 *z = 1;
3591 else if (ms->button & MOUSE_BUTTON6DOWN)
3592 *z = -2;
3593 else if (ms->button & MOUSE_BUTTON7DOWN)
3594 *z = 2;
3595 else
3596 *z = 0;
3597 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3598 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3599
3600 return (0);
3601}
3602
3603static int
3323 *x = ((pb->ipacket[1] & 0x10) ?
3324 pb->ipacket[4] - 256 : pb->ipacket[4]);
3325 *y = ((pb->ipacket[1] & 0x20) ?
3326 pb->ipacket[5] - 256 : pb->ipacket[5]);
3327 *z = 0;
3328
3329 guest_buttons = 0;
3330 if (pb->ipacket[1] & 0x01)
3331 guest_buttons |= MOUSE_BUTTON1DOWN;
3332 if (pb->ipacket[1] & 0x04)
3333 guest_buttons |= MOUSE_BUTTON2DOWN;
3334 if (pb->ipacket[1] & 0x02)
3335 guest_buttons |= MOUSE_BUTTON3DOWN;
3336#ifdef EVDEV_SUPPORT
3337 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3338 evdev_push_rel(sc->evdev_r, REL_X, *x);
3339 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
3340 evdev_push_mouse_btn(sc->evdev_r,
3341 guest_buttons);
3342 evdev_sync(sc->evdev_r);
3343 }
3344#endif
3345 ms->button = touchpad_buttons | guest_buttons |
3346 sc->extended_buttons;
3347 }
3348 goto SYNAPTICS_END;
3349
3350 case 2:
3351 /* Handle Extended W mode packets */
3352 ewcode = (pb->ipacket[5] & 0xf0) >> 4;
3353#if PSM_FINGERS > 1
3354 switch (ewcode) {
3355 case 1:
3356 /* Secondary finger */
3357 if (sc->synhw.capAdvancedGestures)
3358 f[1] = (finger_t) {
3359 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3360 pb->ipacket[1]) << 1,
3361 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3362 pb->ipacket[2]) << 1,
3363 .p = ((pb->ipacket[3] & 0x30) |
3364 (pb->ipacket[5] & 0x0f)) << 1,
3365 .w = PSM_FINGER_DEFAULT_W,
3366 .flags = PSM_FINGER_FUZZY,
3367 };
3368 else if (sc->synhw.capReportsV)
3369 f[1] = (finger_t) {
3370 .x = (((pb->ipacket[4] & 0x0f) << 8) |
3371 (pb->ipacket[1] & 0xfe)) << 1,
3372 .y = (((pb->ipacket[4] & 0xf0) << 4) |
3373 (pb->ipacket[2] & 0xfe)) << 1,
3374 .p = ((pb->ipacket[3] & 0x30) |
3375 (pb->ipacket[5] & 0x0e)) << 1,
3376 .w = (((pb->ipacket[5] & 0x01) << 2) |
3377 ((pb->ipacket[2] & 0x01) << 1) |
3378 (pb->ipacket[1] & 0x01)) + 8,
3379 .flags = PSM_FINGER_FUZZY,
3380 };
3381 default:
3382 break;
3383 }
3384#endif
3385 goto SYNAPTICS_END;
3386
3387 case 1:
3388 case 0:
3389 nfingers = w + 2;
3390 break;
3391
3392 default:
3393 nfingers = 1;
3394 }
3395
3396 if (sc->syninfo.touchpad_off)
3397 goto SYNAPTICS_END;
3398
3399 /* Button presses */
3400 touchpad_buttons = 0;
3401 if (pb->ipacket[0] & 0x01)
3402 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3403 if (pb->ipacket[0] & 0x02)
3404 touchpad_buttons |= MOUSE_BUTTON3DOWN;
3405
3406 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3407 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01)
3408 touchpad_buttons |= MOUSE_BUTTON4DOWN;
3409 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02)
3410 touchpad_buttons |= MOUSE_BUTTON5DOWN;
3411 } else if (sc->synhw.capExtended && sc->synhw.capMiddle &&
3412 !sc->synhw.capClickPad) {
3413 /* Middle Button */
3414 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)
3415 touchpad_buttons |= MOUSE_BUTTON2DOWN;
3416 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) {
3417 /* Extended Buttons */
3418 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) {
3419 if (sc->syninfo.directional_scrolls) {
3420 if (pb->ipacket[4] & 0x01)
3421 extended_buttons |= MOUSE_BUTTON4DOWN;
3422 if (pb->ipacket[5] & 0x01)
3423 extended_buttons |= MOUSE_BUTTON5DOWN;
3424 if (pb->ipacket[4] & 0x02)
3425 extended_buttons |= MOUSE_BUTTON6DOWN;
3426 if (pb->ipacket[5] & 0x02)
3427 extended_buttons |= MOUSE_BUTTON7DOWN;
3428 } else {
3429 if (pb->ipacket[4] & 0x01)
3430 extended_buttons |= MOUSE_BUTTON1DOWN;
3431 if (pb->ipacket[5] & 0x01)
3432 extended_buttons |= MOUSE_BUTTON3DOWN;
3433 if (pb->ipacket[4] & 0x02)
3434 extended_buttons |= MOUSE_BUTTON2DOWN;
3435 sc->extended_buttons = extended_buttons;
3436 }
3437
3438 /*
3439 * Zero out bits used by extended buttons to avoid
3440 * misinterpretation of the data absolute position.
3441 *
3442 * The bits represented by
3443 *
3444 * (nExtendedButtons + 1) >> 1
3445 *
3446 * will be masked out in both bytes.
3447 * The mask for n bits is computed with the formula
3448 *
3449 * (1 << n) - 1
3450 */
3451 int maskedbits = 0;
3452 int mask = 0;
3453 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1;
3454 mask = (1 << maskedbits) - 1;
3455#ifdef EVDEV_SUPPORT
3456 int i;
3457 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3458 if (sc->synhw.capPassthrough) {
3459 evdev_push_mouse_btn(sc->evdev_r,
3460 extended_buttons);
3461 evdev_sync(sc->evdev_r);
3462 }
3463 for (i = 0; i < maskedbits; i++) {
3464 evdev_push_key(sc->evdev_a,
3465 BTN_0 + i * 2,
3466 pb->ipacket[4] & (1 << i));
3467 evdev_push_key(sc->evdev_a,
3468 BTN_0 + i * 2 + 1,
3469 pb->ipacket[5] & (1 << i));
3470 }
3471 }
3472#endif
3473 pb->ipacket[4] &= ~(mask);
3474 pb->ipacket[5] &= ~(mask);
3475 } else if (!sc->syninfo.directional_scrolls &&
3476 !sc->gesture.in_vscroll) {
3477 /*
3478 * Keep reporting MOUSE DOWN until we get a new packet
3479 * indicating otherwise.
3480 */
3481 extended_buttons |= sc->extended_buttons;
3482 }
3483 }
3484
3485 if (sc->synhw.capReportsV && nfingers > 1)
3486 f[0] = (finger_t) {
3487 .x = ((pb->ipacket[3] & 0x10) << 8) |
3488 ((pb->ipacket[1] & 0x0f) << 8) |
3489 (pb->ipacket[4] & 0xfd),
3490 .y = ((pb->ipacket[3] & 0x20) << 7) |
3491 ((pb->ipacket[1] & 0xf0) << 4) |
3492 (pb->ipacket[5] & 0xfd),
3493 .p = *z & 0xfe,
3494 .w = (((pb->ipacket[2] & 0x01) << 2) |
3495 (pb->ipacket[5] & 0x02) |
3496 ((pb->ipacket[4] & 0x02) >> 1)) + 8,
3497 .flags = PSM_FINGER_FUZZY,
3498 };
3499 else
3500 f[0] = (finger_t) {
3501 .x = ((pb->ipacket[3] & 0x10) << 8) |
3502 ((pb->ipacket[1] & 0x0f) << 8) |
3503 pb->ipacket[4],
3504 .y = ((pb->ipacket[3] & 0x20) << 7) |
3505 ((pb->ipacket[1] & 0xf0) << 4) |
3506 pb->ipacket[5],
3507 .p = *z,
3508 .w = w,
3509 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0,
3510 };
3511
3512 /* Ignore hovering and unmeasurable touches */
3513 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2)
3514 nfingers = 0;
3515
3516 /* Handle ClickPad */
3517 if (sc->synhw.capClickPad) {
3518 clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01;
3519 if (sc->synhw.forcePad) {
3520 /*
3521 * Forcepads erroneously report button click if there
3522 * are 2 or more fingers on the touchpad breaking
3523 * multifinger gestures. To workaround this start
3524 * reporting a click only after 4 consecutive single
3525 * touch packets has been received.
3526 * Skip these packets in case more contacts appear.
3527 */
3528 switch (nfingers) {
3529 case 0:
3530 sc->fpcount = 0;
3531 break;
3532 case 1:
3533 if (clickpad_pressed && sc->fpcount < INT_MAX)
3534 ++sc->fpcount;
3535 /* FALLTHROUGH */
3536 default:
3537 if (!clickpad_pressed)
3538 sc->fpcount = 0;
3539 if (sc->fpcount >= sc->syninfo.window_min)
3540 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3541 }
3542 } else if (clickpad_pressed)
3543 touchpad_buttons |= MOUSE_BUTTON1DOWN;
3544 }
3545
3546 for (id = 0; id < PSM_FINGERS; id++)
3547 if (id >= nfingers)
3548 PSM_FINGER_RESET(f[id]);
3549
3550#ifdef EVDEV_SUPPORT
3551 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
3552 for (id = 0; id < PSM_FINGERS; id++) {
3553 if (PSM_FINGER_IS_SET(f[id]))
3554 psm_push_mt_finger(sc, id, &f[id]);
3555 else
3556 psm_release_mt_slot(sc->evdev_a, id);
3557 }
3558 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
3559 evdev_push_nfingers(sc->evdev_a, nfingers);
3560 if (nfingers > 0)
3561 psm_push_st_finger(sc, &f[0]);
3562 else
3563 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
3564 evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons);
3565 if (sc->synhw.capExtended && sc->synhw.capFourButtons) {
3566 evdev_push_key(sc->evdev_a, BTN_FORWARD,
3567 touchpad_buttons & MOUSE_BUTTON4DOWN);
3568 evdev_push_key(sc->evdev_a, BTN_BACK,
3569 touchpad_buttons & MOUSE_BUTTON5DOWN);
3570 }
3571 evdev_sync(sc->evdev_a);
3572 }
3573#endif
3574
3575 ms->button = touchpad_buttons;
3576
3577 psmgestures(sc, &f[0], nfingers, ms);
3578 for (id = 0; id < PSM_FINGERS; id++)
3579 psmsmoother(sc, &f[id], id, ms, x, y);
3580
3581 /* Palm detection doesn't terminate the current action. */
3582 if (psmpalmdetect(sc, &f[0], nfingers)) {
3583 *x = *y = *z = 0;
3584 ms->button = ms->obutton;
3585 return (0);
3586 }
3587
3588 ms->button |= extended_buttons | guest_buttons;
3589
3590SYNAPTICS_END:
3591 /*
3592 * Use the extra buttons as a scrollwheel
3593 *
3594 * XXX X.Org uses the Z axis for vertical wheel only,
3595 * whereas moused(8) understands special values to differ
3596 * vertical and horizontal wheels.
3597 *
3598 * xf86-input-mouse needs therefore a small patch to
3599 * understand these special values. Without it, the
3600 * horizontal wheel acts as a vertical wheel in X.Org.
3601 *
3602 * That's why the horizontal wheel is disabled by
3603 * default for now.
3604 */
3605 if (ms->button & MOUSE_BUTTON4DOWN)
3606 *z = -1;
3607 else if (ms->button & MOUSE_BUTTON5DOWN)
3608 *z = 1;
3609 else if (ms->button & MOUSE_BUTTON6DOWN)
3610 *z = -2;
3611 else if (ms->button & MOUSE_BUTTON7DOWN)
3612 *z = 2;
3613 else
3614 *z = 0;
3615 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
3616 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
3617
3618 return (0);
3619}
3620
3621static int
3622proc_synaptics_mux(struct psm_softc *sc, packetbuf_t *pb)
3623{
3624 int butt;
3625
3626 /*
3627 * Convert 3-byte interleaved mixture of Synaptics and generic mouse
3628 * packets into plain 6-byte Synaptics packet protocol.
3629 * While in hidden multiplexing mode KBC does some editing of the
3630 * packet stream. It remembers the button bits from the last packet
3631 * received from each device, and replaces the button bits of every
3632 * packet with the logical OR of all devices��� most recent button bits.
3633 * This button crosstalk should be filtered out as Synaptics and
3634 * generic mouse encode middle button presses in a different way.
3635 */
3636 switch (pb->ipacket[0] & 0xc0) {
3637 case 0x80: /* First 3 bytes of Synaptics packet */
3638 bcopy(pb->ipacket, sc->muxsave, 3);
3639 /* Compute middle mouse button supression timeout. */
3640 sc->muxmidtimeout.tv_sec = 0;
3641 sc->muxmidtimeout.tv_usec = 50000; /* ~2-3 ints */
3642 timevaladd(&sc->muxmidtimeout, &sc->lastsoftintr);
3643 return (1);
3644
3645 case 0xc0: /* Second 3 bytes of Synaptics packet */
3646 /* Join two 3-bytes absolute packets */
3647 bcopy(pb->ipacket, pb->ipacket + 3, 3);
3648 bcopy(sc->muxsave, pb->ipacket, 3);
3649 /* Prefer trackpoint buttons over touchpad's */
3650 pb->ipacket[0] &= ~(0x08 | sc->muxmsbuttons);
3651 pb->ipacket[3] &= ~(0x08 | sc->muxmsbuttons);
3652 butt = (pb->ipacket[3] & 0x03) << 2 | (pb->ipacket[0] & 0x03);
3653 /* Add hysteresis to remove spurious middle button events */
3654 if (butt != sc->muxtpbuttons && sc->fpcount < 1) {
3655 pb->ipacket[0] &= 0xfc;
3656 pb->ipacket[0] |= sc->muxtpbuttons & 0x03;
3657 pb->ipacket[3] &= 0xfc;
3658 pb->ipacket[3] |= sc->muxtpbuttons >> 2 & 0x03;
3659 ++sc->fpcount;
3660 } else {
3661 sc->fpcount = 0;
3662 sc->muxtpbuttons = butt;
3663 }
3664 /* Filter out impossible w induced by middle trackpoint btn */
3665 if (sc->synhw.capExtended && !sc->synhw.capPassthrough &&
3666 (pb->ipacket[0] & 0x34) == 0x04 &&
3667 (pb->ipacket[3] & 0x04) == 0x04) {
3668 pb->ipacket[0] &= 0xfb;
3669 pb->ipacket[3] &= 0xfb;
3670 }
3671 sc->muxsave[0] &= 0x30;
3672 break;
3673
3674 default: /* Generic mouse (Trackpoint) packet */
3675 /* Filter out middle button events induced by some w values */
3676 if (sc->muxmsbuttons & 0x03 || pb->ipacket[0] & 0x03 ||
3677 (timevalcmp(&sc->lastsoftintr, &sc->muxmidtimeout, <=) &&
3678 (sc->muxsave[0] & 0x30 || sc->muxsave[2] > 8)))
3679 pb->ipacket[0] &= 0xfb;
3680 sc->muxmsbuttons = pb->ipacket[0] & 0x07;
3681 /* Convert to Synaptics pass-through protocol */
3682 pb->ipacket[4] = pb->ipacket[1];
3683 pb->ipacket[5] = pb->ipacket[2];
3684 pb->ipacket[1] = pb->ipacket[0];
3685 pb->ipacket[2] = 0;
3686 pb->ipacket[0] = 0x84 | (sc->muxtpbuttons & 0x03);
3687 pb->ipacket[3] = 0xc4 | (sc->muxtpbuttons >> 2 & 0x03);
3688 }
3689
3690 VLOG(4, (LOG_DEBUG, "synaptics: %02x %02x %02x %02x %02x %02x\n",
3691 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
3692 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
3693
3694 pb->inputbytes = MOUSE_SYNAPTICS_PACKETSIZE;
3695 return (0);
3696}
3697
3698static int
3604psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3605{
3606 if (!(
3607 ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) &&
3608 !sc->synhw.capReportsV && nfingers > 1) ||
3609 (sc->synhw.capReportsV && nfingers > 2) ||
3610 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3611 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3612 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3613 /*
3614 * We consider the packet irrelevant for the current
3615 * action when:
3616 * - the width isn't comprised in:
3617 * [1; max_width]
3618 * - the pressure isn't comprised in:
3619 * [min_pressure; max_pressure]
3620 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3621 */
3622 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w));
3623 return (1);
3624 }
3625 return (0);
3626}
3627
3628static void
3629psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3630 mousestatus_t *ms)
3631{
3632 smoother_t *smoother;
3633 gesture_t *gest;
3634 finger_t *f;
3635 int y_ok, center_button, center_x, right_button, right_x, i;
3636
3637 f = &fingers[0];
3638 smoother = &sc->smoother[0];
3639 gest = &sc->gesture;
3640
3641 /* Find first active finger. */
3642 if (nfingers > 0) {
3643 for (i = 0; i < PSM_FINGERS; i++) {
3644 if (PSM_FINGER_IS_SET(fingers[i])) {
3645 f = &fingers[i];
3646 smoother = &sc->smoother[i];
3647 break;
3648 }
3649 }
3650 }
3651
3652 /*
3653 * Check pressure to detect a real wanted action on the
3654 * touchpad.
3655 */
3656 if (f->p >= sc->syninfo.min_pressure) {
3657 int x0, y0;
3658 int dxp, dyp;
3659 int start_x, start_y;
3660 int queue_len;
3661 int margin_top, margin_right, margin_bottom, margin_left;
3662 int window_min, window_max;
3663 int vscroll_hor_area, vscroll_ver_area;
3664 int two_finger_scroll;
3665 int max_x, max_y;
3666
3667 /* Read sysctl. */
3668 /* XXX Verify values? */
3669 margin_top = sc->syninfo.margin_top;
3670 margin_right = sc->syninfo.margin_right;
3671 margin_bottom = sc->syninfo.margin_bottom;
3672 margin_left = sc->syninfo.margin_left;
3673 window_min = sc->syninfo.window_min;
3674 window_max = sc->syninfo.window_max;
3675 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3676 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3677 two_finger_scroll = sc->syninfo.two_finger_scroll;
3678 max_x = sc->syninfo.max_x;
3679 max_y = sc->syninfo.max_y;
3680
3681 /* Read current absolute position. */
3682 x0 = f->x;
3683 y0 = f->y;
3684
3685 /*
3686 * Limit the coordinates to the specified margins because
3687 * this area isn't very reliable.
3688 */
3689 if (x0 <= margin_left)
3690 x0 = margin_left;
3691 else if (x0 >= max_x - margin_right)
3692 x0 = max_x - margin_right;
3693 if (y0 <= margin_bottom)
3694 y0 = margin_bottom;
3695 else if (y0 >= max_y - margin_top)
3696 y0 = max_y - margin_top;
3697
3698 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3699 x0, y0, f->p, f->w));
3700
3701 /*
3702 * If the action is just beginning, init the structure and
3703 * compute tap timeout.
3704 */
3705 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3706 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3707
3708 /* Initialize queue. */
3709 gest->window_min = window_min;
3710
3711 /* Reset pressure peak. */
3712 gest->zmax = 0;
3713
3714 /* Reset fingers count. */
3715 gest->fingers_nb = 0;
3716
3717 /* Reset virtual scrolling state. */
3718 gest->in_vscroll = 0;
3719
3720 /* Compute tap timeout. */
3721 gest->taptimeout.tv_sec = tap_timeout / 1000000;
3722 gest->taptimeout.tv_usec = tap_timeout % 1000000;
3723 timevaladd(&gest->taptimeout, &sc->lastsoftintr);
3724
3725 sc->flags |= PSM_FLAGS_FINGERDOWN;
3726
3727 /* Smoother has not been reset yet */
3728 queue_len = 1;
3729 start_x = x0;
3730 start_y = y0;
3731 } else {
3732 queue_len = smoother->queue_len + 1;
3733 start_x = smoother->start_x;
3734 start_y = smoother->start_y;
3735 }
3736
3737 /* Process ClickPad softbuttons */
3738 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3739 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3740 start_y < sc->syninfo.softbuttons_y :
3741 start_y > max_y + sc->syninfo.softbuttons_y;
3742
3743 center_button = MOUSE_BUTTON2DOWN;
3744 center_x = sc->syninfo.softbutton2_x;
3745 right_button = MOUSE_BUTTON3DOWN;
3746 right_x = sc->syninfo.softbutton3_x;
3747
3748 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3749 center_button = MOUSE_BUTTON3DOWN;
3750 center_x = sc->syninfo.softbutton3_x;
3751 right_button = MOUSE_BUTTON2DOWN;
3752 right_x = sc->syninfo.softbutton2_x;
3753 }
3754
3755 if (right_x > 0 && start_x > right_x && y_ok)
3756 ms->button = (ms->button &
3757 ~MOUSE_BUTTON1DOWN) | right_button;
3758 else if (center_x > 0 && start_x > center_x && y_ok)
3759 ms->button = (ms->button &
3760 ~MOUSE_BUTTON1DOWN) | center_button;
3761 }
3762
3763 /* If in tap-hold, add the recorded button. */
3764 if (gest->in_taphold)
3765 ms->button |= gest->tap_button;
3766
3767 /*
3768 * For tap, we keep the maximum number of fingers and the
3769 * pressure peak. Also with multiple fingers, we increase
3770 * the minimum window.
3771 */
3772 if (nfingers > 1)
3773 gest->window_min = window_max;
3774 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3775 gest->zmax = imax(f->p, gest->zmax);
3776
3777 /* Do we have enough packets to consider this a gesture? */
3778 if (queue_len < gest->window_min)
3779 return;
3780
3781 /* Is a scrolling action occurring? */
3782 if (!gest->in_taphold && !ms->button &&
3783 (!gest->in_vscroll || two_finger_scroll)) {
3784 /*
3785 * A scrolling action must not conflict with a tap
3786 * action. Here are the conditions to consider a
3787 * scrolling action:
3788 * - the action in a configurable area
3789 * - one of the following:
3790 * . the distance between the last packet and the
3791 * first should be above a configurable minimum
3792 * . tap timed out
3793 */
3794 dxp = abs(x0 - start_x);
3795 dyp = abs(y0 - start_y);
3796
3797 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3798 dxp >= sc->syninfo.vscroll_min_delta ||
3799 dyp >= sc->syninfo.vscroll_min_delta) {
3800 /*
3801 * Handle two finger scrolling.
3802 * Note that we don't rely on fingers_nb
3803 * as that keeps the maximum number of fingers.
3804 */
3805 if (two_finger_scroll) {
3806 if (nfingers == 2) {
3807 gest->in_vscroll +=
3808 dyp ? 2 : 0;
3809 gest->in_vscroll +=
3810 dxp ? 1 : 0;
3811 }
3812 } else {
3813 /* Check for horizontal scrolling. */
3814 if ((vscroll_hor_area > 0 &&
3815 start_y <= vscroll_hor_area) ||
3816 (vscroll_hor_area < 0 &&
3817 start_y >=
3818 max_y + vscroll_hor_area))
3819 gest->in_vscroll += 2;
3820
3821 /* Check for vertical scrolling. */
3822 if ((vscroll_ver_area > 0 &&
3823 start_x <= vscroll_ver_area) ||
3824 (vscroll_ver_area < 0 &&
3825 start_x >=
3826 max_x + vscroll_ver_area))
3827 gest->in_vscroll += 1;
3828 }
3829
3830 /* Avoid conflicts if area overlaps. */
3831 if (gest->in_vscroll >= 3)
3832 gest->in_vscroll =
3833 (dxp > dyp) ? 2 : 1;
3834 }
3835 }
3836 /*
3837 * Reset two finger scrolling when the number of fingers
3838 * is different from two or any button is pressed.
3839 */
3840 if (two_finger_scroll && gest->in_vscroll != 0 &&
3841 (nfingers != 2 || ms->button))
3842 gest->in_vscroll = 0;
3843
3844 VLOG(5, (LOG_DEBUG,
3845 "synaptics: virtual scrolling: %s "
3846 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3847 gest->in_vscroll ? "YES" : "NO",
3848 gest->in_vscroll, dxp, dyp,
3849 gest->fingers_nb));
3850
3851 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3852 /*
3853 * An action is currently taking place but the pressure
3854 * dropped under the minimum, putting an end to it.
3855 */
3856 int taphold_timeout, dx, dy, tap_max_delta;
3857
3858 dx = abs(smoother->queue[smoother->queue_cursor].x -
3859 smoother->start_x);
3860 dy = abs(smoother->queue[smoother->queue_cursor].y -
3861 smoother->start_y);
3862
3863 /* Max delta is disabled for multi-fingers tap. */
3864 if (gest->fingers_nb > 1)
3865 tap_max_delta = imax(dx, dy);
3866 else
3867 tap_max_delta = sc->syninfo.tap_max_delta;
3868
3869 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3870
3871 /* Check for tap. */
3872 VLOG(3, (LOG_DEBUG,
3873 "synaptics: zmax=%d, dx=%d, dy=%d, "
3874 "delta=%d, fingers=%d, queue=%d\n",
3875 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
3876 smoother->queue_len));
3877 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
3878 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
3879 dx <= tap_max_delta && dy <= tap_max_delta &&
3880 smoother->queue_len >= sc->syninfo.tap_min_queue) {
3881 /*
3882 * We have a tap if:
3883 * - the maximum pressure went over tap_threshold
3884 * - the action ended before tap_timeout
3885 *
3886 * To handle tap-hold, we must delay any button push to
3887 * the next action.
3888 */
3889 if (gest->in_taphold) {
3890 /*
3891 * This is the second and last tap of a
3892 * double tap action, not a tap-hold.
3893 */
3894 gest->in_taphold = 0;
3895
3896 /*
3897 * For double-tap to work:
3898 * - no button press is emitted (to
3899 * simulate a button release)
3900 * - PSM_FLAGS_FINGERDOWN is set to
3901 * force the next packet to emit a
3902 * button press)
3903 */
3904 VLOG(2, (LOG_DEBUG,
3905 "synaptics: button RELEASE: %d\n",
3906 gest->tap_button));
3907 sc->flags |= PSM_FLAGS_FINGERDOWN;
3908
3909 /* Schedule button press on next interrupt */
3910 sc->idletimeout.tv_sec = psmhz > 1 ?
3911 0 : 1;
3912 sc->idletimeout.tv_usec = psmhz > 1 ?
3913 1000000 / psmhz : 0;
3914 } else {
3915 /*
3916 * This is the first tap: we set the
3917 * tap-hold state and notify the button
3918 * down event.
3919 */
3920 gest->in_taphold = 1;
3921 taphold_timeout = sc->syninfo.taphold_timeout;
3922 gest->taptimeout.tv_sec = taphold_timeout /
3923 1000000;
3924 gest->taptimeout.tv_usec = taphold_timeout %
3925 1000000;
3926 sc->idletimeout = gest->taptimeout;
3927 timevaladd(&gest->taptimeout,
3928 &sc->lastsoftintr);
3929
3930 switch (gest->fingers_nb) {
3931 case 3:
3932 gest->tap_button =
3933 MOUSE_BUTTON2DOWN;
3934 break;
3935 case 2:
3936 gest->tap_button =
3937 MOUSE_BUTTON3DOWN;
3938 break;
3939 default:
3940 gest->tap_button =
3941 MOUSE_BUTTON1DOWN;
3942 }
3943 VLOG(2, (LOG_DEBUG,
3944 "synaptics: button PRESS: %d\n",
3945 gest->tap_button));
3946 ms->button |= gest->tap_button;
3947 }
3948 } else {
3949 /*
3950 * Not enough pressure or timeout: reset
3951 * tap-hold state.
3952 */
3953 if (gest->in_taphold) {
3954 VLOG(2, (LOG_DEBUG,
3955 "synaptics: button RELEASE: %d\n",
3956 gest->tap_button));
3957 gest->in_taphold = 0;
3958 } else {
3959 VLOG(2, (LOG_DEBUG,
3960 "synaptics: not a tap-hold\n"));
3961 }
3962 }
3963 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
3964 /*
3965 * For a tap-hold to work, the button must remain down at
3966 * least until timeout (where the in_taphold flags will be
3967 * cleared) or during the next action.
3968 */
3969 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
3970 ms->button |= gest->tap_button;
3971 } else {
3972 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
3973 gest->tap_button));
3974 gest->in_taphold = 0;
3975 }
3976 }
3977
3978 return;
3979}
3980
3981static void
3982psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
3983 mousestatus_t *ms, int *x, int *y)
3984{
3985 smoother_t *smoother = &sc->smoother[smoother_id];
3986 gesture_t *gest = &(sc->gesture);
3987
3988 /*
3989 * Check pressure to detect a real wanted action on the
3990 * touchpad.
3991 */
3992 if (f->p >= sc->syninfo.min_pressure) {
3993 int x0, y0;
3994 int cursor, peer, window;
3995 int dx, dy, dxp, dyp;
3996 int max_width, max_pressure;
3997 int margin_top, margin_right, margin_bottom, margin_left;
3998 int na_top, na_right, na_bottom, na_left;
3999 int window_min, window_max;
4000 int multiplicator;
4001 int weight_current, weight_previous, weight_len_squared;
4002 int div_min, div_max, div_len;
4003 int vscroll_hor_area, vscroll_ver_area;
4004 int two_finger_scroll;
4005 int max_x, max_y;
4006 int len, weight_prev_x, weight_prev_y;
4007 int div_max_x, div_max_y, div_x, div_y;
4008 int is_fuzzy;
4009
4010 /* Read sysctl. */
4011 /* XXX Verify values? */
4012 max_width = sc->syninfo.max_width;
4013 max_pressure = sc->syninfo.max_pressure;
4014 margin_top = sc->syninfo.margin_top;
4015 margin_right = sc->syninfo.margin_right;
4016 margin_bottom = sc->syninfo.margin_bottom;
4017 margin_left = sc->syninfo.margin_left;
4018 na_top = sc->syninfo.na_top;
4019 na_right = sc->syninfo.na_right;
4020 na_bottom = sc->syninfo.na_bottom;
4021 na_left = sc->syninfo.na_left;
4022 window_min = sc->syninfo.window_min;
4023 window_max = sc->syninfo.window_max;
4024 multiplicator = sc->syninfo.multiplicator;
4025 weight_current = sc->syninfo.weight_current;
4026 weight_previous = sc->syninfo.weight_previous;
4027 weight_len_squared = sc->syninfo.weight_len_squared;
4028 div_min = sc->syninfo.div_min;
4029 div_max = sc->syninfo.div_max;
4030 div_len = sc->syninfo.div_len;
4031 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
4032 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
4033 two_finger_scroll = sc->syninfo.two_finger_scroll;
4034 max_x = sc->syninfo.max_x;
4035 max_y = sc->syninfo.max_y;
4036
4037 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
4038
4039 /* Read current absolute position. */
4040 x0 = f->x;
4041 y0 = f->y;
4042
4043 /*
4044 * Limit the coordinates to the specified margins because
4045 * this area isn't very reliable.
4046 */
4047 if (x0 <= margin_left)
4048 x0 = margin_left;
4049 else if (x0 >= max_x - margin_right)
4050 x0 = max_x - margin_right;
4051 if (y0 <= margin_bottom)
4052 y0 = margin_bottom;
4053 else if (y0 >= max_y - margin_top)
4054 y0 = max_y - margin_top;
4055
4056 /* If the action is just beginning, init the structure. */
4057 if (smoother->active == 0) {
4058 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
4059
4060 /* Store the first point of this action. */
4061 smoother->start_x = x0;
4062 smoother->start_y = y0;
4063 dx = dy = 0;
4064
4065 /* Initialize queue. */
4066 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
4067 smoother->queue_len = 0;
4068
4069 /* Reset average. */
4070 smoother->avg_dx = 0;
4071 smoother->avg_dy = 0;
4072
4073 /* Reset squelch. */
4074 smoother->squelch_x = 0;
4075 smoother->squelch_y = 0;
4076
4077 /* Activate queue */
4078 smoother->active = 1;
4079 } else {
4080 /* Calculate the current delta. */
4081 cursor = smoother->queue_cursor;
4082 dx = x0 - smoother->queue[cursor].x;
4083 dy = y0 - smoother->queue[cursor].y;
4084 }
4085
4086 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
4087 smoother_id, x0, y0, f->p, f->w));
4088
4089 /* Queue this new packet. */
4090 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
4091 smoother->queue[cursor].x = x0;
4092 smoother->queue[cursor].y = y0;
4093 smoother->queue_cursor = cursor;
4094 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
4095 smoother->queue_len++;
4096 VLOG(5, (LOG_DEBUG,
4097 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
4098 smoother_id, cursor, x0, y0, dx, dy));
4099
4100 /* Do we have enough packets to consider this a movement? */
4101 if (smoother->queue_len < gest->window_min)
4102 return;
4103
4104 weight_prev_x = weight_prev_y = weight_previous;
4105 div_max_x = div_max_y = div_max;
4106
4107 if (gest->in_vscroll) {
4108 /* Dividers are different with virtual scrolling. */
4109 div_min = sc->syninfo.vscroll_div_min;
4110 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
4111 } else {
4112 /*
4113 * There's a lot of noise in coordinates when
4114 * the finger is on the touchpad's borders. When
4115 * using this area, we apply a special weight and
4116 * div.
4117 */
4118 if (x0 <= na_left || x0 >= max_x - na_right) {
4119 weight_prev_x = sc->syninfo.weight_previous_na;
4120 div_max_x = sc->syninfo.div_max_na;
4121 }
4122
4123 if (y0 <= na_bottom || y0 >= max_y - na_top) {
4124 weight_prev_y = sc->syninfo.weight_previous_na;
4125 div_max_y = sc->syninfo.div_max_na;
4126 }
4127 }
4128
4129 /*
4130 * Calculate weights for the average operands and
4131 * the divisor. Both depend on the distance between
4132 * the current packet and a previous one (based on the
4133 * window width).
4134 */
4135 window = imin(smoother->queue_len, window_max);
4136 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
4137 dxp = abs(x0 - smoother->queue[peer].x) + 1;
4138 dyp = abs(y0 - smoother->queue[peer].y) + 1;
4139 len = (dxp * dxp) + (dyp * dyp);
4140 weight_prev_x = imin(weight_prev_x,
4141 weight_len_squared * weight_prev_x / len);
4142 weight_prev_y = imin(weight_prev_y,
4143 weight_len_squared * weight_prev_y / len);
4144
4145 len = (dxp + dyp) / 2;
4146 div_x = div_len * div_max_x / len;
4147 div_x = imin(div_max_x, div_x);
4148 div_x = imax(div_min, div_x);
4149 div_y = div_len * div_max_y / len;
4150 div_y = imin(div_max_y, div_y);
4151 div_y = imax(div_min, div_y);
4152
4153 VLOG(3, (LOG_DEBUG,
4154 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
4155 smoother_id, peer, len, weight_prev_x, weight_prev_y,
4156 div_x, div_y));
4157
4158 /* Compute averages. */
4159 smoother->avg_dx =
4160 (weight_current * dx * multiplicator +
4161 weight_prev_x * smoother->avg_dx) /
4162 (weight_current + weight_prev_x);
4163
4164 smoother->avg_dy =
4165 (weight_current * dy * multiplicator +
4166 weight_prev_y * smoother->avg_dy) /
4167 (weight_current + weight_prev_y);
4168
4169 VLOG(5, (LOG_DEBUG,
4170 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
4171 smoother->avg_dx / multiplicator,
4172 smoother->avg_dy / multiplicator));
4173
4174 /* Use these averages to calculate x & y. */
4175 smoother->squelch_x += smoother->avg_dx;
4176 dxp = smoother->squelch_x / (div_x * multiplicator);
4177 smoother->squelch_x = smoother->squelch_x %
4178 (div_x * multiplicator);
4179
4180 smoother->squelch_y += smoother->avg_dy;
4181 dyp = smoother->squelch_y / (div_y * multiplicator);
4182 smoother->squelch_y = smoother->squelch_y %
4183 (div_y * multiplicator);
4184
4185 switch(gest->in_vscroll) {
4186 case 0: /* Pointer movement. */
4187 /* On real<->fuzzy finger switch the x/y pos jumps */
4188 if (is_fuzzy == smoother->is_fuzzy) {
4189 *x += dxp;
4190 *y += dyp;
4191 }
4192
4193 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
4194 smoother_id, dx, dy, dxp, dyp));
4195 break;
4196 case 1: /* Vertical scrolling. */
4197 if (dyp != 0)
4198 ms->button |= (dyp > 0) ?
4199 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
4200 break;
4201 case 2: /* Horizontal scrolling. */
4202 if (dxp != 0)
4203 ms->button |= (dxp > 0) ?
4204 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
4205 break;
4206 }
4207
4208 smoother->is_fuzzy = is_fuzzy;
4209
4210 } else {
4211 /*
4212 * Deactivate queue. Note: We can not just reset queue here
4213 * as these values are still used by gesture processor.
4214 * So postpone reset till next touch.
4215 */
4216 smoother->active = 0;
4217 }
4218}
4219
4220static int
4221proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4222 int *x, int *y, int *z)
4223{
4224 static int touchpad_button, trackpoint_button;
4225 finger_t fn, f[ELANTECH_MAX_FINGERS];
4226 int pkt, id, scale, i, nfingers, mask;
4227
4228 if (!elantech_support)
4229 return (0);
4230
4231 /* Determine packet format and do a sanity check for out of sync packets. */
4232 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
4233 pkt = ELANTECH_PKT_NOP;
4234 else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb))
4235 pkt = ELANTECH_PKT_TRACKPOINT;
4236 else
4237 switch (sc->elanhw.hwversion) {
4238 case 2:
4239 if (!ELANTECH_PKT_IS_V2(pb))
4240 return (-1);
4241
4242 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
4243 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
4244 break;
4245 case 3:
4246 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
4247 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
4248 return (-1);
4249
4250 pkt = ELANTECH_PKT_V3;
4251 break;
4252 case 4:
4253 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
4254 return (-1);
4255
4256 switch (pb->ipacket[3] & 0x03) {
4257 case 0x00:
4258 pkt = ELANTECH_PKT_V4_STATUS;
4259 break;
4260 case 0x01:
4261 pkt = ELANTECH_PKT_V4_HEAD;
4262 break;
4263 case 0x02:
4264 pkt = ELANTECH_PKT_V4_MOTION;
4265 break;
4266 default:
4267 return (-1);
4268 }
4269 break;
4270 default:
4271 return (-1);
4272 }
4273
4274 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
4275
4276 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4277 PSM_FINGER_RESET(f[id]);
4278
4279 *x = *y = *z = 0;
4280 ms->button = ms->obutton;
4281
4282 if (sc->syninfo.touchpad_off)
4283 return (0);
4284
4285 /* Common legend
4286 * L: Left mouse button pressed
4287 * R: Right mouse button pressed
4288 * N: number of fingers on touchpad
4289 * X: absolute x value (horizontal)
4290 * Y: absolute y value (vertical)
4291 * W; width of the finger touch
4292 * P: pressure
4293 */
4294 switch (pkt) {
4295 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
4296 /* 7 6 5 4 3 2 1 0 (LSB)
4297 * -------------------------------------------
4298 * ipacket[0]: N1 N0 W3 W2 . . R L
4299 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4300 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4301 * ipacket[3]: N4 VF W1 W0 . . . B2
4302 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4303 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4304 * -------------------------------------------
4305 * N4: set if more than 3 fingers (only in 3 fingers mode)
4306 * VF: a kind of flag? (only on EF123, 0 when finger
4307 * is over one of the buttons, 1 otherwise)
4308 * B2: (on EF113 only, 0 otherwise), one button pressed
4309 * P & W is not reported on EF113 touchpads
4310 */
4311 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4312 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
4313 nfingers = 4;
4314
4315 if (nfingers == 0) {
4316 mask = (1 << nfingers) - 1; /* = 0x00 */
4317 break;
4318 }
4319
4320 /* Map 3-rd and 4-th fingers to first finger */
4321 mask = (1 << 1) - 1; /* = 0x01 */
4322 f[0] = ELANTECH_FINGER_SET_XYP(pb);
4323 if (sc->elanhw.haspressure) {
4324 f[0].w = ((pb->ipacket[0] & 0x30) >> 2) |
4325 ((pb->ipacket[3] & 0x30) >> 4);
4326 } else {
4327 f[0].p = PSM_FINGER_DEFAULT_P;
4328 f[0].w = PSM_FINGER_DEFAULT_W;
4329 }
4330
4331 /*
4332 * HW v2 dont report exact finger positions when 3 or more
4333 * fingers are on touchpad.
4334 */
4335 if (nfingers > 2)
4336 f[0].flags = PSM_FINGER_FUZZY;
4337
4338 break;
4339
4340 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
4341 /* 7 6 5 4 3 2 1 0 (LSB)
4342 * -------------------------------------------
4343 * ipacket[0]: N1 N0 AY8 AX8 . . R L
4344 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
4345 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
4346 * ipacket[3]: . . BY8 BX8 . . . .
4347 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
4348 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
4349 * -------------------------------------------
4350 * AX: lower-left finger absolute x value
4351 * AY: lower-left finger absolute y value
4352 * BX: upper-right finger absolute x value
4353 * BY: upper-right finger absolute y value
4354 */
4355 nfingers = 2;
4356 mask = (1 << nfingers) - 1;
4357
4358 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
4359 f[id] = (finger_t) {
4360 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
4361 pb->ipacket[id * 3 + 1]) << 2,
4362 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
4363 pb->ipacket[id * 3 + 2]) << 2,
4364 .p = PSM_FINGER_DEFAULT_P,
4365 .w = PSM_FINGER_DEFAULT_W,
4366 /* HW ver.2 sends bounding box */
4367 .flags = PSM_FINGER_FUZZY
4368 };
4369 break;
4370
4371 case ELANTECH_PKT_V3: /* HW Version 3 */
4372 /* 7 6 5 4 3 2 1 0 (LSB)
4373 * -------------------------------------------
4374 * ipacket[0]: N1 N0 W3 W2 0 1 R L
4375 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4376 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4377 * ipacket[3]: 0 0 W1 W0 0 0 1 0
4378 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4379 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4380 * -------------------------------------------
4381 */
4382 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4383 /* Map 3-rd finger to first finger */
4384 id = nfingers > 2 ? 0 : nfingers - 1;
4385 mask = (1 << (id + 1)) - 1;
4386
4387 if (nfingers == 0)
4388 break;
4389
4390 fn = ELANTECH_FINGER_SET_XYP(pb);
4391 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
4392 ((pb->ipacket[3] & 0x30) >> 4);
4393
4394 /*
4395 * HW v3 dont report exact finger positions when 3 or more
4396 * fingers are on touchpad.
4397 */
4398 if (nfingers > 1)
4399 fn.flags = PSM_FINGER_FUZZY;
4400
4401 if (nfingers == 2) {
4402 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
4403 sc->elanaction.fingers[0] = fn;
4404 return (0);
4405 } else
4406 f[0] = sc->elanaction.fingers[0];
4407 }
4408 f[id] = fn;
4409 break;
4410
4411 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
4412 /* 7 6 5 4 3 2 1 0 (LSB)
4413 * -------------------------------------------
4414 * ipacket[0]: . . . . 0 1 R L
4415 * ipacket[1]: . . . F4 F3 F2 F1 F0
4416 * ipacket[2]: . . . . . . . .
4417 * ipacket[3]: . . . 1 0 0 0 0
4418 * ipacket[4]: PL . . . . . . .
4419 * ipacket[5]: . . . . . . . .
4420 * -------------------------------------------
4421 * Fn: finger n is on touchpad
4422 * PL: palm
4423 * HV ver4 sends a status packet to indicate that the numbers
4424 * or identities of the fingers has been changed
4425 */
4426
4427 mask = pb->ipacket[1] & 0x1f;
4428 nfingers = bitcount(mask);
4429
4430 if (sc->elanaction.mask_v4wait != 0)
4431 VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet"
4432 " when not all previous head packets received\n"));
4433
4434 /* Bitmap of fingers to receive before gesture processing */
4435 sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask;
4436
4437 /* Skip "new finger is on touchpad" packets */
4438 if (sc->elanaction.mask_v4wait) {
4439 sc->elanaction.mask = mask;
4440 return (0);
4441 }
4442
4443 break;
4444
4445 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
4446 /* 7 6 5 4 3 2 1 0 (LSB)
4447 * -------------------------------------------
4448 * ipacket[0]: W3 W2 W1 W0 0 1 R L
4449 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4450 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4451 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
4452 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4453 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4454 * -------------------------------------------
4455 * ID: finger id
4456 * HW ver 4 sends head packets in two cases:
4457 * 1. One finger touch and movement.
4458 * 2. Next after status packet to tell new finger positions.
4459 */
4460 mask = sc->elanaction.mask;
4461 nfingers = bitcount(mask);
4462 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
4463 fn = ELANTECH_FINGER_SET_XYP(pb);
4464 fn.w =(pb->ipacket[0] & 0xf0) >> 4;
4465
4466 if (id < 0)
4467 return (0);
4468
4469 /* Packet is finger position update. Report it */
4470 if (sc->elanaction.mask_v4wait == 0) {
4471 if (id < ELANTECH_MAX_FINGERS)
4472 f[id] = fn;
4473 break;
4474 }
4475
4476 /* Remove finger from waiting bitmap and store into context */
4477 sc->elanaction.mask_v4wait &= ~(1 << id);
4478 if (id < ELANTECH_MAX_FINGERS)
4479 sc->elanaction.fingers[id] = fn;
4480
4481 /* Wait for other fingers if needed */
4482 if (sc->elanaction.mask_v4wait != 0)
4483 return (0);
4484
4485 /* All new fingers are received. Report them from context */
4486 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4487 if (sc->elanaction.mask & (1 << id))
4488 f[id] = sc->elanaction.fingers[id];
4489
4490 break;
4491
4492 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
4493 /* 7 6 5 4 3 2 1 0 (LSB)
4494 * -------------------------------------------
4495 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
4496 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4497 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4498 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
4499 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4500 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4501 * -------------------------------------------
4502 * OF: delta overflows (> 127 or < -128), in this case
4503 * firmware sends us (delta x / 5) and (delta y / 5)
4504 * ID: finger id
4505 * DX: delta x (two's complement)
4506 * XY: delta y (two's complement)
4507 * byte 0 ~ 2 for one finger
4508 * byte 3 ~ 5 for another finger
4509 */
4510 mask = sc->elanaction.mask;
4511 nfingers = bitcount(mask);
4512
4513 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
4514 for (i = 0; i <= 3; i += 3) {
4515 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
4516 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
4517 continue;
4518
4519 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
4520 f[id] = sc->elanaction.fingers[id];
4521 f[id].x += imax(-f[id].x,
4522 (signed char)pb->ipacket[i+1] * scale);
4523 f[id].y += imax(-f[id].y,
4524 (signed char)pb->ipacket[i+2] * scale);
4525 } else {
4526 VLOG(3, (LOG_DEBUG, "elantech: "
4527 "HW v4 motion packet skipped\n"));
4528 }
4529 }
4530
4531 break;
4532
4533 case ELANTECH_PKT_TRACKPOINT:
4534 /* 7 6 5 4 3 2 1 0 (LSB)
4535 * -------------------------------------------
4536 * ipacket[0]: 0 0 SX SY 0 M R L
4537 * ipacket[1]: ~SX 0 0 0 0 0 0 0
4538 * ipacket[2]: ~SY 0 0 0 0 0 0 0
4539 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
4540 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
4541 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4542 * -------------------------------------------
4543 * X and Y are written in two's complement spread
4544 * over 9 bits with SX/SY the relative top bit and
4545 * X7..X0 and Y7..Y0 the lower bits.
4546 */
4547 *x = (pb->ipacket[0] & 0x20) ?
4548 pb->ipacket[4] - 256 : pb->ipacket[4];
4549 *y = (pb->ipacket[0] & 0x10) ?
4550 pb->ipacket[5] - 256 : pb->ipacket[5];
4551
4552 trackpoint_button =
4553 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4554 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
4555 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
4556#ifdef EVDEV_SUPPORT
4557 evdev_push_rel(sc->evdev_r, REL_X, *x);
4558 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
4559 evdev_push_mouse_btn(sc->evdev_r, trackpoint_button);
4560 evdev_sync(sc->evdev_r);
4561#endif
4562 ms->button = touchpad_button | trackpoint_button;
4563 return (0);
4564
4565 case ELANTECH_PKT_NOP:
4566 return (0);
4567
4568 default:
4569 return (-1);
4570 }
4571
4572 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4573 if (PSM_FINGER_IS_SET(f[id]))
4574 VLOG(2, (LOG_DEBUG, "elantech: "
4575 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
4576 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
4577
4578 /* Touchpad button presses */
4579 if (sc->elanhw.isclickpad) {
4580 touchpad_button =
4581 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4582 } else {
4583 touchpad_button =
4584 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4585 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4586 }
4587
4588#ifdef EVDEV_SUPPORT
4589 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
4590 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4591 if (PSM_FINGER_IS_SET(f[id])) {
4592 psm_push_mt_finger(sc, id, &f[id]);
4593 /* Convert touch width to surface units */
4594 evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR,
4595 f[id].w * sc->elanhw.dptracex);
4596 }
4597 if (sc->elanaction.mask & (1 << id) &&
4598 !(mask & (1 << id)))
4599 psm_release_mt_slot(sc->evdev_a, id);
4600 }
4601 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
4602 evdev_push_nfingers(sc->evdev_a, nfingers);
4603 if (nfingers > 0) {
4604 if (PSM_FINGER_IS_SET(f[0]))
4605 psm_push_st_finger(sc, &f[0]);
4606 } else
4607 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
4608 evdev_push_mouse_btn(sc->evdev_a, touchpad_button);
4609 evdev_sync(sc->evdev_a);
4610 }
4611#endif
4612
4613 ms->button = touchpad_button | trackpoint_button;
4614
4615 /* Send finger 1 position to gesture processor */
4616 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4617 nfingers == 0)
4618 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4619 /* Send fingers positions to movement smoothers */
4620 for (id = 0; id < PSM_FINGERS; id++)
4621 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4622 psmsmoother(sc, &f[id], id, ms, x, y);
4623
4624 /* Store current finger positions in action context */
4625 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4626 if (PSM_FINGER_IS_SET(f[id]))
4627 sc->elanaction.fingers[id] = f[id];
4628 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4629 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4630 }
4631 sc->elanaction.mask = mask;
4632
4633 /* Palm detection doesn't terminate the current action. */
4634 if (psmpalmdetect(sc, &f[0], nfingers)) {
4635 *x = *y = *z = 0;
4636 ms->button = ms->obutton;
4637 return (0);
4638 }
4639
4640 /* Use the extra buttons as a scrollwheel */
4641 if (ms->button & MOUSE_BUTTON4DOWN)
4642 *z = -1;
4643 else if (ms->button & MOUSE_BUTTON5DOWN)
4644 *z = 1;
4645 else if (ms->button & MOUSE_BUTTON6DOWN)
4646 *z = -2;
4647 else if (ms->button & MOUSE_BUTTON7DOWN)
4648 *z = 2;
4649 else
4650 *z = 0;
4651 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4652 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4653
4654 return (0);
4655}
4656
4657static void
4658proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4659 int *x, int *y, int *z)
4660{
4661 static int butmap_versapad[8] = {
4662 0,
4663 MOUSE_BUTTON3DOWN,
4664 0,
4665 MOUSE_BUTTON3DOWN,
4666 MOUSE_BUTTON1DOWN,
4667 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4668 MOUSE_BUTTON1DOWN,
4669 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4670 };
4671 int c, x0, y0;
4672
4673 /* VersaPad PS/2 absolute mode message format
4674 *
4675 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4676 * ipacket[0]: 1 1 0 A 1 L T R
4677 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4678 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4679 * ipacket[3]: 1 1 1 A 1 L T R
4680 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4681 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4682 *
4683 * [note]
4684 * R: right physical mouse button (1=on)
4685 * T: touch pad virtual button (1=tapping)
4686 * L: left physical mouse button (1=on)
4687 * A: position data is valid (1=valid)
4688 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4689 * V: vertical data (12bit signed integer. V11 is sign bit.)
4690 * P: pressure data
4691 *
4692 * Tapping is mapped to MOUSE_BUTTON4.
4693 */
4694 c = pb->ipacket[0];
4695 *x = *y = 0;
4696 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4697 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4698 if (c & MOUSE_PS2VERSA_IN_USE) {
4699 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4700 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4701 if (x0 & 0x800)
4702 x0 -= 0x1000;
4703 if (y0 & 0x800)
4704 y0 -= 0x1000;
4705 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4706 *x = sc->xold - x0;
4707 *y = y0 - sc->yold;
4708 if (*x < 0) /* XXX */
4709 ++*x;
4710 else if (*x)
4711 --*x;
4712 if (*y < 0)
4713 ++*y;
4714 else if (*y)
4715 --*y;
4716 } else
4717 sc->flags |= PSM_FLAGS_FINGERDOWN;
4718 sc->xold = x0;
4719 sc->yold = y0;
4720 } else
4721 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4722}
4723
4724static void
4725psmsoftintridle(void *arg)
4726{
4727 struct psm_softc *sc = arg;
4728 packetbuf_t *pb;
4729
4730 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4731 * invoked from psmintr soon with pqueue filled with real data */
4732 if (sc->pqueue_start == sc->pqueue_end &&
4733 sc->idlepacket.inputbytes > 0) {
4734 /* Grow circular queue backwards to avoid race with psmintr */
4735 if (--sc->pqueue_start < 0)
4736 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4737
4738 pb = &sc->pqueue[sc->pqueue_start];
4739 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4740 VLOG(4, (LOG_DEBUG,
4741 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4742 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4743 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4744
4745 psmsoftintr(arg);
4746 }
4747}
4748
4749static void
4750psmsoftintr(void *arg)
4751{
4752 /*
4753 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4754 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4755 */
4756 static int butmap[8] = {
4757 0,
4758 MOUSE_BUTTON1DOWN,
4759 MOUSE_BUTTON3DOWN,
4760 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4761 MOUSE_BUTTON2DOWN,
4762 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4763 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4764 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4765 };
4766 struct psm_softc *sc = arg;
4767 mousestatus_t ms;
4768 packetbuf_t *pb;
4769 int x, y, z, c, l, s;
4770
4771 getmicrouptime(&sc->lastsoftintr);
4772
4773 s = spltty();
4774
4775 do {
4776 pb = &sc->pqueue[sc->pqueue_start];
4777
4778 if (sc->mode.level == PSM_LEVEL_NATIVE)
4779 goto next_native;
4780
4781 c = pb->ipacket[0];
4782 /*
4783 * A kludge for Kensington device!
4784 * The MSB of the horizontal count appears to be stored in
4785 * a strange place.
4786 */
4787 if (sc->hw.model == MOUSE_MODEL_THINK)
4788 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4789
4790 /* ignore the overflow bits... */
4791 x = (c & MOUSE_PS2_XNEG) ?
4792 pb->ipacket[1] - 256 : pb->ipacket[1];
4793 y = (c & MOUSE_PS2_YNEG) ?
4794 pb->ipacket[2] - 256 : pb->ipacket[2];
4795 z = 0;
4796 ms.obutton = sc->button; /* previous button state */
4797 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4798 /* `tapping' action */
4799 if (sc->config & PSM_CONFIG_FORCETAP)
4800 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4801 0 : MOUSE_BUTTON4DOWN;
4802 timevalclear(&sc->idletimeout);
4803 sc->idlepacket.inputbytes = 0;
4804
4805 switch (sc->hw.model) {
4806
4807 case MOUSE_MODEL_EXPLORER:
4808 /*
4809 * b7 b6 b5 b4 b3 b2 b1 b0
4810 * byte 1: oy ox sy sx 1 M R L
4811 * byte 2: x x x x x x x x
4812 * byte 3: y y y y y y y y
4813 * byte 4: * * S2 S1 s d2 d1 d0
4814 *
4815 * L, M, R, S1, S2: left, middle, right and side buttons
4816 * s: wheel data sign bit
4817 * d2-d0: wheel data
4818 */
4819 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4820 (pb->ipacket[3] & 0x0f) - 16 :
4821 (pb->ipacket[3] & 0x0f);
4822 ms.button |=
4823 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4824 MOUSE_BUTTON4DOWN : 0;
4825 ms.button |=
4826 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
4827 MOUSE_BUTTON5DOWN : 0;
4828 break;
4829
4830 case MOUSE_MODEL_INTELLI:
4831 case MOUSE_MODEL_NET:
4832 /* wheel data is in the fourth byte */
4833 z = (char)pb->ipacket[3];
4834 /*
4835 * XXX some mice may send 7 when there is no Z movement? */
4836 if ((z >= 7) || (z <= -7))
4837 z = 0;
4838 /* some compatible mice have additional buttons */
4839 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
4840 MOUSE_BUTTON4DOWN : 0;
4841 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
4842 MOUSE_BUTTON5DOWN : 0;
4843 break;
4844
4845 case MOUSE_MODEL_MOUSEMANPLUS:
4846 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
4847 break;
4848
4849 case MOUSE_MODEL_GLIDEPOINT:
4850 /* `tapping' action */
4851 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
4852 MOUSE_BUTTON4DOWN;
4853 break;
4854
4855 case MOUSE_MODEL_NETSCROLL:
4856 /*
4857 * three additional bytes encode buttons and
4858 * wheel events
4859 */
4860 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
4861 MOUSE_BUTTON4DOWN : 0;
4862 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
4863 MOUSE_BUTTON5DOWN : 0;
4864 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
4865 pb->ipacket[4] - 256 : pb->ipacket[4];
4866 break;
4867
4868 case MOUSE_MODEL_THINK:
4869 /* the fourth button state in the first byte */
4870 ms.button |= (c & MOUSE_PS2_TAP) ?
4871 MOUSE_BUTTON4DOWN : 0;
4872 break;
4873
4874 case MOUSE_MODEL_VERSAPAD:
4875 proc_versapad(sc, pb, &ms, &x, &y, &z);
4876 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
4877 ((y < 0) ? MOUSE_PS2_YNEG : 0);
4878 break;
4879
4880 case MOUSE_MODEL_4D:
4881 /*
4882 * b7 b6 b5 b4 b3 b2 b1 b0
4883 * byte 1: s2 d2 s1 d1 1 M R L
4884 * byte 2: sx x x x x x x x
4885 * byte 3: sy y y y y y y y
4886 *
4887 * s1: wheel 1 direction
4888 * d1: wheel 1 data
4889 * s2: wheel 2 direction
4890 * d2: wheel 2 data
4891 */
4892 x = (pb->ipacket[1] & 0x80) ?
4893 pb->ipacket[1] - 256 : pb->ipacket[1];
4894 y = (pb->ipacket[2] & 0x80) ?
4895 pb->ipacket[2] - 256 : pb->ipacket[2];
4896 switch (c & MOUSE_4D_WHEELBITS) {
4897 case 0x10:
4898 z = 1;
4899 break;
4900 case 0x30:
4901 z = -1;
4902 break;
4903 case 0x40: /* XXX 2nd wheel turning right */
4904 z = 2;
4905 break;
4906 case 0xc0: /* XXX 2nd wheel turning left */
4907 z = -2;
4908 break;
4909 }
4910 break;
4911
4912 case MOUSE_MODEL_4DPLUS:
4913 if ((x < 16 - 256) && (y < 16 - 256)) {
4914 /*
4915 * b7 b6 b5 b4 b3 b2 b1 b0
4916 * byte 1: 0 0 1 1 1 M R L
4917 * byte 2: 0 0 0 0 1 0 0 0
4918 * byte 3: 0 0 0 0 S s d1 d0
4919 *
4920 * L, M, R, S: left, middle, right,
4921 * and side buttons
4922 * s: wheel data sign bit
4923 * d1-d0: wheel data
4924 */
4925 x = y = 0;
4926 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
4927 ms.button |= MOUSE_BUTTON4DOWN;
4928 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
4929 ((pb->ipacket[2] & 0x07) - 8) :
4930 (pb->ipacket[2] & 0x07) ;
4931 } else {
4932 /* preserve previous button states */
4933 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
4934 }
4935 break;
4936
4937 case MOUSE_MODEL_SYNAPTICS:
3699psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers)
3700{
3701 if (!(
3702 ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) &&
3703 !sc->synhw.capReportsV && nfingers > 1) ||
3704 (sc->synhw.capReportsV && nfingers > 2) ||
3705 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) ||
3706 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) ||
3707 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) {
3708 /*
3709 * We consider the packet irrelevant for the current
3710 * action when:
3711 * - the width isn't comprised in:
3712 * [1; max_width]
3713 * - the pressure isn't comprised in:
3714 * [min_pressure; max_pressure]
3715 * - pen aren't supported but PSM_FINGER_IS_PEN is set
3716 */
3717 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w));
3718 return (1);
3719 }
3720 return (0);
3721}
3722
3723static void
3724psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers,
3725 mousestatus_t *ms)
3726{
3727 smoother_t *smoother;
3728 gesture_t *gest;
3729 finger_t *f;
3730 int y_ok, center_button, center_x, right_button, right_x, i;
3731
3732 f = &fingers[0];
3733 smoother = &sc->smoother[0];
3734 gest = &sc->gesture;
3735
3736 /* Find first active finger. */
3737 if (nfingers > 0) {
3738 for (i = 0; i < PSM_FINGERS; i++) {
3739 if (PSM_FINGER_IS_SET(fingers[i])) {
3740 f = &fingers[i];
3741 smoother = &sc->smoother[i];
3742 break;
3743 }
3744 }
3745 }
3746
3747 /*
3748 * Check pressure to detect a real wanted action on the
3749 * touchpad.
3750 */
3751 if (f->p >= sc->syninfo.min_pressure) {
3752 int x0, y0;
3753 int dxp, dyp;
3754 int start_x, start_y;
3755 int queue_len;
3756 int margin_top, margin_right, margin_bottom, margin_left;
3757 int window_min, window_max;
3758 int vscroll_hor_area, vscroll_ver_area;
3759 int two_finger_scroll;
3760 int max_x, max_y;
3761
3762 /* Read sysctl. */
3763 /* XXX Verify values? */
3764 margin_top = sc->syninfo.margin_top;
3765 margin_right = sc->syninfo.margin_right;
3766 margin_bottom = sc->syninfo.margin_bottom;
3767 margin_left = sc->syninfo.margin_left;
3768 window_min = sc->syninfo.window_min;
3769 window_max = sc->syninfo.window_max;
3770 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
3771 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
3772 two_finger_scroll = sc->syninfo.two_finger_scroll;
3773 max_x = sc->syninfo.max_x;
3774 max_y = sc->syninfo.max_y;
3775
3776 /* Read current absolute position. */
3777 x0 = f->x;
3778 y0 = f->y;
3779
3780 /*
3781 * Limit the coordinates to the specified margins because
3782 * this area isn't very reliable.
3783 */
3784 if (x0 <= margin_left)
3785 x0 = margin_left;
3786 else if (x0 >= max_x - margin_right)
3787 x0 = max_x - margin_right;
3788 if (y0 <= margin_bottom)
3789 y0 = margin_bottom;
3790 else if (y0 >= max_y - margin_top)
3791 y0 = max_y - margin_top;
3792
3793 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n",
3794 x0, y0, f->p, f->w));
3795
3796 /*
3797 * If the action is just beginning, init the structure and
3798 * compute tap timeout.
3799 */
3800 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) {
3801 VLOG(3, (LOG_DEBUG, "synaptics: ----\n"));
3802
3803 /* Initialize queue. */
3804 gest->window_min = window_min;
3805
3806 /* Reset pressure peak. */
3807 gest->zmax = 0;
3808
3809 /* Reset fingers count. */
3810 gest->fingers_nb = 0;
3811
3812 /* Reset virtual scrolling state. */
3813 gest->in_vscroll = 0;
3814
3815 /* Compute tap timeout. */
3816 gest->taptimeout.tv_sec = tap_timeout / 1000000;
3817 gest->taptimeout.tv_usec = tap_timeout % 1000000;
3818 timevaladd(&gest->taptimeout, &sc->lastsoftintr);
3819
3820 sc->flags |= PSM_FLAGS_FINGERDOWN;
3821
3822 /* Smoother has not been reset yet */
3823 queue_len = 1;
3824 start_x = x0;
3825 start_y = y0;
3826 } else {
3827 queue_len = smoother->queue_len + 1;
3828 start_x = smoother->start_x;
3829 start_y = smoother->start_y;
3830 }
3831
3832 /* Process ClickPad softbuttons */
3833 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) {
3834 y_ok = sc->syninfo.softbuttons_y >= 0 ?
3835 start_y < sc->syninfo.softbuttons_y :
3836 start_y > max_y + sc->syninfo.softbuttons_y;
3837
3838 center_button = MOUSE_BUTTON2DOWN;
3839 center_x = sc->syninfo.softbutton2_x;
3840 right_button = MOUSE_BUTTON3DOWN;
3841 right_x = sc->syninfo.softbutton3_x;
3842
3843 if (center_x > 0 && right_x > 0 && center_x > right_x) {
3844 center_button = MOUSE_BUTTON3DOWN;
3845 center_x = sc->syninfo.softbutton3_x;
3846 right_button = MOUSE_BUTTON2DOWN;
3847 right_x = sc->syninfo.softbutton2_x;
3848 }
3849
3850 if (right_x > 0 && start_x > right_x && y_ok)
3851 ms->button = (ms->button &
3852 ~MOUSE_BUTTON1DOWN) | right_button;
3853 else if (center_x > 0 && start_x > center_x && y_ok)
3854 ms->button = (ms->button &
3855 ~MOUSE_BUTTON1DOWN) | center_button;
3856 }
3857
3858 /* If in tap-hold, add the recorded button. */
3859 if (gest->in_taphold)
3860 ms->button |= gest->tap_button;
3861
3862 /*
3863 * For tap, we keep the maximum number of fingers and the
3864 * pressure peak. Also with multiple fingers, we increase
3865 * the minimum window.
3866 */
3867 if (nfingers > 1)
3868 gest->window_min = window_max;
3869 gest->fingers_nb = imax(nfingers, gest->fingers_nb);
3870 gest->zmax = imax(f->p, gest->zmax);
3871
3872 /* Do we have enough packets to consider this a gesture? */
3873 if (queue_len < gest->window_min)
3874 return;
3875
3876 /* Is a scrolling action occurring? */
3877 if (!gest->in_taphold && !ms->button &&
3878 (!gest->in_vscroll || two_finger_scroll)) {
3879 /*
3880 * A scrolling action must not conflict with a tap
3881 * action. Here are the conditions to consider a
3882 * scrolling action:
3883 * - the action in a configurable area
3884 * - one of the following:
3885 * . the distance between the last packet and the
3886 * first should be above a configurable minimum
3887 * . tap timed out
3888 */
3889 dxp = abs(x0 - start_x);
3890 dyp = abs(y0 - start_y);
3891
3892 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) ||
3893 dxp >= sc->syninfo.vscroll_min_delta ||
3894 dyp >= sc->syninfo.vscroll_min_delta) {
3895 /*
3896 * Handle two finger scrolling.
3897 * Note that we don't rely on fingers_nb
3898 * as that keeps the maximum number of fingers.
3899 */
3900 if (two_finger_scroll) {
3901 if (nfingers == 2) {
3902 gest->in_vscroll +=
3903 dyp ? 2 : 0;
3904 gest->in_vscroll +=
3905 dxp ? 1 : 0;
3906 }
3907 } else {
3908 /* Check for horizontal scrolling. */
3909 if ((vscroll_hor_area > 0 &&
3910 start_y <= vscroll_hor_area) ||
3911 (vscroll_hor_area < 0 &&
3912 start_y >=
3913 max_y + vscroll_hor_area))
3914 gest->in_vscroll += 2;
3915
3916 /* Check for vertical scrolling. */
3917 if ((vscroll_ver_area > 0 &&
3918 start_x <= vscroll_ver_area) ||
3919 (vscroll_ver_area < 0 &&
3920 start_x >=
3921 max_x + vscroll_ver_area))
3922 gest->in_vscroll += 1;
3923 }
3924
3925 /* Avoid conflicts if area overlaps. */
3926 if (gest->in_vscroll >= 3)
3927 gest->in_vscroll =
3928 (dxp > dyp) ? 2 : 1;
3929 }
3930 }
3931 /*
3932 * Reset two finger scrolling when the number of fingers
3933 * is different from two or any button is pressed.
3934 */
3935 if (two_finger_scroll && gest->in_vscroll != 0 &&
3936 (nfingers != 2 || ms->button))
3937 gest->in_vscroll = 0;
3938
3939 VLOG(5, (LOG_DEBUG,
3940 "synaptics: virtual scrolling: %s "
3941 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n",
3942 gest->in_vscroll ? "YES" : "NO",
3943 gest->in_vscroll, dxp, dyp,
3944 gest->fingers_nb));
3945
3946 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) {
3947 /*
3948 * An action is currently taking place but the pressure
3949 * dropped under the minimum, putting an end to it.
3950 */
3951 int taphold_timeout, dx, dy, tap_max_delta;
3952
3953 dx = abs(smoother->queue[smoother->queue_cursor].x -
3954 smoother->start_x);
3955 dy = abs(smoother->queue[smoother->queue_cursor].y -
3956 smoother->start_y);
3957
3958 /* Max delta is disabled for multi-fingers tap. */
3959 if (gest->fingers_nb > 1)
3960 tap_max_delta = imax(dx, dy);
3961 else
3962 tap_max_delta = sc->syninfo.tap_max_delta;
3963
3964 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
3965
3966 /* Check for tap. */
3967 VLOG(3, (LOG_DEBUG,
3968 "synaptics: zmax=%d, dx=%d, dy=%d, "
3969 "delta=%d, fingers=%d, queue=%d\n",
3970 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb,
3971 smoother->queue_len));
3972 if (!gest->in_vscroll && gest->zmax >= tap_threshold &&
3973 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) &&
3974 dx <= tap_max_delta && dy <= tap_max_delta &&
3975 smoother->queue_len >= sc->syninfo.tap_min_queue) {
3976 /*
3977 * We have a tap if:
3978 * - the maximum pressure went over tap_threshold
3979 * - the action ended before tap_timeout
3980 *
3981 * To handle tap-hold, we must delay any button push to
3982 * the next action.
3983 */
3984 if (gest->in_taphold) {
3985 /*
3986 * This is the second and last tap of a
3987 * double tap action, not a tap-hold.
3988 */
3989 gest->in_taphold = 0;
3990
3991 /*
3992 * For double-tap to work:
3993 * - no button press is emitted (to
3994 * simulate a button release)
3995 * - PSM_FLAGS_FINGERDOWN is set to
3996 * force the next packet to emit a
3997 * button press)
3998 */
3999 VLOG(2, (LOG_DEBUG,
4000 "synaptics: button RELEASE: %d\n",
4001 gest->tap_button));
4002 sc->flags |= PSM_FLAGS_FINGERDOWN;
4003
4004 /* Schedule button press on next interrupt */
4005 sc->idletimeout.tv_sec = psmhz > 1 ?
4006 0 : 1;
4007 sc->idletimeout.tv_usec = psmhz > 1 ?
4008 1000000 / psmhz : 0;
4009 } else {
4010 /*
4011 * This is the first tap: we set the
4012 * tap-hold state and notify the button
4013 * down event.
4014 */
4015 gest->in_taphold = 1;
4016 taphold_timeout = sc->syninfo.taphold_timeout;
4017 gest->taptimeout.tv_sec = taphold_timeout /
4018 1000000;
4019 gest->taptimeout.tv_usec = taphold_timeout %
4020 1000000;
4021 sc->idletimeout = gest->taptimeout;
4022 timevaladd(&gest->taptimeout,
4023 &sc->lastsoftintr);
4024
4025 switch (gest->fingers_nb) {
4026 case 3:
4027 gest->tap_button =
4028 MOUSE_BUTTON2DOWN;
4029 break;
4030 case 2:
4031 gest->tap_button =
4032 MOUSE_BUTTON3DOWN;
4033 break;
4034 default:
4035 gest->tap_button =
4036 MOUSE_BUTTON1DOWN;
4037 }
4038 VLOG(2, (LOG_DEBUG,
4039 "synaptics: button PRESS: %d\n",
4040 gest->tap_button));
4041 ms->button |= gest->tap_button;
4042 }
4043 } else {
4044 /*
4045 * Not enough pressure or timeout: reset
4046 * tap-hold state.
4047 */
4048 if (gest->in_taphold) {
4049 VLOG(2, (LOG_DEBUG,
4050 "synaptics: button RELEASE: %d\n",
4051 gest->tap_button));
4052 gest->in_taphold = 0;
4053 } else {
4054 VLOG(2, (LOG_DEBUG,
4055 "synaptics: not a tap-hold\n"));
4056 }
4057 }
4058 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) {
4059 /*
4060 * For a tap-hold to work, the button must remain down at
4061 * least until timeout (where the in_taphold flags will be
4062 * cleared) or during the next action.
4063 */
4064 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) {
4065 ms->button |= gest->tap_button;
4066 } else {
4067 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n",
4068 gest->tap_button));
4069 gest->in_taphold = 0;
4070 }
4071 }
4072
4073 return;
4074}
4075
4076static void
4077psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id,
4078 mousestatus_t *ms, int *x, int *y)
4079{
4080 smoother_t *smoother = &sc->smoother[smoother_id];
4081 gesture_t *gest = &(sc->gesture);
4082
4083 /*
4084 * Check pressure to detect a real wanted action on the
4085 * touchpad.
4086 */
4087 if (f->p >= sc->syninfo.min_pressure) {
4088 int x0, y0;
4089 int cursor, peer, window;
4090 int dx, dy, dxp, dyp;
4091 int max_width, max_pressure;
4092 int margin_top, margin_right, margin_bottom, margin_left;
4093 int na_top, na_right, na_bottom, na_left;
4094 int window_min, window_max;
4095 int multiplicator;
4096 int weight_current, weight_previous, weight_len_squared;
4097 int div_min, div_max, div_len;
4098 int vscroll_hor_area, vscroll_ver_area;
4099 int two_finger_scroll;
4100 int max_x, max_y;
4101 int len, weight_prev_x, weight_prev_y;
4102 int div_max_x, div_max_y, div_x, div_y;
4103 int is_fuzzy;
4104
4105 /* Read sysctl. */
4106 /* XXX Verify values? */
4107 max_width = sc->syninfo.max_width;
4108 max_pressure = sc->syninfo.max_pressure;
4109 margin_top = sc->syninfo.margin_top;
4110 margin_right = sc->syninfo.margin_right;
4111 margin_bottom = sc->syninfo.margin_bottom;
4112 margin_left = sc->syninfo.margin_left;
4113 na_top = sc->syninfo.na_top;
4114 na_right = sc->syninfo.na_right;
4115 na_bottom = sc->syninfo.na_bottom;
4116 na_left = sc->syninfo.na_left;
4117 window_min = sc->syninfo.window_min;
4118 window_max = sc->syninfo.window_max;
4119 multiplicator = sc->syninfo.multiplicator;
4120 weight_current = sc->syninfo.weight_current;
4121 weight_previous = sc->syninfo.weight_previous;
4122 weight_len_squared = sc->syninfo.weight_len_squared;
4123 div_min = sc->syninfo.div_min;
4124 div_max = sc->syninfo.div_max;
4125 div_len = sc->syninfo.div_len;
4126 vscroll_hor_area = sc->syninfo.vscroll_hor_area;
4127 vscroll_ver_area = sc->syninfo.vscroll_ver_area;
4128 two_finger_scroll = sc->syninfo.two_finger_scroll;
4129 max_x = sc->syninfo.max_x;
4130 max_y = sc->syninfo.max_y;
4131
4132 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0;
4133
4134 /* Read current absolute position. */
4135 x0 = f->x;
4136 y0 = f->y;
4137
4138 /*
4139 * Limit the coordinates to the specified margins because
4140 * this area isn't very reliable.
4141 */
4142 if (x0 <= margin_left)
4143 x0 = margin_left;
4144 else if (x0 >= max_x - margin_right)
4145 x0 = max_x - margin_right;
4146 if (y0 <= margin_bottom)
4147 y0 = margin_bottom;
4148 else if (y0 >= max_y - margin_top)
4149 y0 = max_y - margin_top;
4150
4151 /* If the action is just beginning, init the structure. */
4152 if (smoother->active == 0) {
4153 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id));
4154
4155 /* Store the first point of this action. */
4156 smoother->start_x = x0;
4157 smoother->start_y = y0;
4158 dx = dy = 0;
4159
4160 /* Initialize queue. */
4161 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE;
4162 smoother->queue_len = 0;
4163
4164 /* Reset average. */
4165 smoother->avg_dx = 0;
4166 smoother->avg_dy = 0;
4167
4168 /* Reset squelch. */
4169 smoother->squelch_x = 0;
4170 smoother->squelch_y = 0;
4171
4172 /* Activate queue */
4173 smoother->active = 1;
4174 } else {
4175 /* Calculate the current delta. */
4176 cursor = smoother->queue_cursor;
4177 dx = x0 - smoother->queue[cursor].x;
4178 dy = y0 - smoother->queue[cursor].y;
4179 }
4180
4181 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n",
4182 smoother_id, x0, y0, f->p, f->w));
4183
4184 /* Queue this new packet. */
4185 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1);
4186 smoother->queue[cursor].x = x0;
4187 smoother->queue[cursor].y = y0;
4188 smoother->queue_cursor = cursor;
4189 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE)
4190 smoother->queue_len++;
4191 VLOG(5, (LOG_DEBUG,
4192 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n",
4193 smoother_id, cursor, x0, y0, dx, dy));
4194
4195 /* Do we have enough packets to consider this a movement? */
4196 if (smoother->queue_len < gest->window_min)
4197 return;
4198
4199 weight_prev_x = weight_prev_y = weight_previous;
4200 div_max_x = div_max_y = div_max;
4201
4202 if (gest->in_vscroll) {
4203 /* Dividers are different with virtual scrolling. */
4204 div_min = sc->syninfo.vscroll_div_min;
4205 div_max_x = div_max_y = sc->syninfo.vscroll_div_max;
4206 } else {
4207 /*
4208 * There's a lot of noise in coordinates when
4209 * the finger is on the touchpad's borders. When
4210 * using this area, we apply a special weight and
4211 * div.
4212 */
4213 if (x0 <= na_left || x0 >= max_x - na_right) {
4214 weight_prev_x = sc->syninfo.weight_previous_na;
4215 div_max_x = sc->syninfo.div_max_na;
4216 }
4217
4218 if (y0 <= na_bottom || y0 >= max_y - na_top) {
4219 weight_prev_y = sc->syninfo.weight_previous_na;
4220 div_max_y = sc->syninfo.div_max_na;
4221 }
4222 }
4223
4224 /*
4225 * Calculate weights for the average operands and
4226 * the divisor. Both depend on the distance between
4227 * the current packet and a previous one (based on the
4228 * window width).
4229 */
4230 window = imin(smoother->queue_len, window_max);
4231 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1);
4232 dxp = abs(x0 - smoother->queue[peer].x) + 1;
4233 dyp = abs(y0 - smoother->queue[peer].y) + 1;
4234 len = (dxp * dxp) + (dyp * dyp);
4235 weight_prev_x = imin(weight_prev_x,
4236 weight_len_squared * weight_prev_x / len);
4237 weight_prev_y = imin(weight_prev_y,
4238 weight_len_squared * weight_prev_y / len);
4239
4240 len = (dxp + dyp) / 2;
4241 div_x = div_len * div_max_x / len;
4242 div_x = imin(div_max_x, div_x);
4243 div_x = imax(div_min, div_x);
4244 div_y = div_len * div_max_y / len;
4245 div_y = imin(div_max_y, div_y);
4246 div_y = imax(div_min, div_y);
4247
4248 VLOG(3, (LOG_DEBUG,
4249 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n",
4250 smoother_id, peer, len, weight_prev_x, weight_prev_y,
4251 div_x, div_y));
4252
4253 /* Compute averages. */
4254 smoother->avg_dx =
4255 (weight_current * dx * multiplicator +
4256 weight_prev_x * smoother->avg_dx) /
4257 (weight_current + weight_prev_x);
4258
4259 smoother->avg_dy =
4260 (weight_current * dy * multiplicator +
4261 weight_prev_y * smoother->avg_dy) /
4262 (weight_current + weight_prev_y);
4263
4264 VLOG(5, (LOG_DEBUG,
4265 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id,
4266 smoother->avg_dx / multiplicator,
4267 smoother->avg_dy / multiplicator));
4268
4269 /* Use these averages to calculate x & y. */
4270 smoother->squelch_x += smoother->avg_dx;
4271 dxp = smoother->squelch_x / (div_x * multiplicator);
4272 smoother->squelch_x = smoother->squelch_x %
4273 (div_x * multiplicator);
4274
4275 smoother->squelch_y += smoother->avg_dy;
4276 dyp = smoother->squelch_y / (div_y * multiplicator);
4277 smoother->squelch_y = smoother->squelch_y %
4278 (div_y * multiplicator);
4279
4280 switch(gest->in_vscroll) {
4281 case 0: /* Pointer movement. */
4282 /* On real<->fuzzy finger switch the x/y pos jumps */
4283 if (is_fuzzy == smoother->is_fuzzy) {
4284 *x += dxp;
4285 *y += dyp;
4286 }
4287
4288 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n",
4289 smoother_id, dx, dy, dxp, dyp));
4290 break;
4291 case 1: /* Vertical scrolling. */
4292 if (dyp != 0)
4293 ms->button |= (dyp > 0) ?
4294 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN;
4295 break;
4296 case 2: /* Horizontal scrolling. */
4297 if (dxp != 0)
4298 ms->button |= (dxp > 0) ?
4299 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN;
4300 break;
4301 }
4302
4303 smoother->is_fuzzy = is_fuzzy;
4304
4305 } else {
4306 /*
4307 * Deactivate queue. Note: We can not just reset queue here
4308 * as these values are still used by gesture processor.
4309 * So postpone reset till next touch.
4310 */
4311 smoother->active = 0;
4312 }
4313}
4314
4315static int
4316proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4317 int *x, int *y, int *z)
4318{
4319 static int touchpad_button, trackpoint_button;
4320 finger_t fn, f[ELANTECH_MAX_FINGERS];
4321 int pkt, id, scale, i, nfingers, mask;
4322
4323 if (!elantech_support)
4324 return (0);
4325
4326 /* Determine packet format and do a sanity check for out of sync packets. */
4327 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion))
4328 pkt = ELANTECH_PKT_NOP;
4329 else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb))
4330 pkt = ELANTECH_PKT_TRACKPOINT;
4331 else
4332 switch (sc->elanhw.hwversion) {
4333 case 2:
4334 if (!ELANTECH_PKT_IS_V2(pb))
4335 return (-1);
4336
4337 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ?
4338 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON;
4339 break;
4340 case 3:
4341 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) &&
4342 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc))
4343 return (-1);
4344
4345 pkt = ELANTECH_PKT_V3;
4346 break;
4347 case 4:
4348 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc))
4349 return (-1);
4350
4351 switch (pb->ipacket[3] & 0x03) {
4352 case 0x00:
4353 pkt = ELANTECH_PKT_V4_STATUS;
4354 break;
4355 case 0x01:
4356 pkt = ELANTECH_PKT_V4_HEAD;
4357 break;
4358 case 0x02:
4359 pkt = ELANTECH_PKT_V4_MOTION;
4360 break;
4361 default:
4362 return (-1);
4363 }
4364 break;
4365 default:
4366 return (-1);
4367 }
4368
4369 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt));
4370
4371 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4372 PSM_FINGER_RESET(f[id]);
4373
4374 *x = *y = *z = 0;
4375 ms->button = ms->obutton;
4376
4377 if (sc->syninfo.touchpad_off)
4378 return (0);
4379
4380 /* Common legend
4381 * L: Left mouse button pressed
4382 * R: Right mouse button pressed
4383 * N: number of fingers on touchpad
4384 * X: absolute x value (horizontal)
4385 * Y: absolute y value (vertical)
4386 * W; width of the finger touch
4387 * P: pressure
4388 */
4389 switch (pkt) {
4390 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */
4391 /* 7 6 5 4 3 2 1 0 (LSB)
4392 * -------------------------------------------
4393 * ipacket[0]: N1 N0 W3 W2 . . R L
4394 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4395 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4396 * ipacket[3]: N4 VF W1 W0 . . . B2
4397 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4398 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4399 * -------------------------------------------
4400 * N4: set if more than 3 fingers (only in 3 fingers mode)
4401 * VF: a kind of flag? (only on EF123, 0 when finger
4402 * is over one of the buttons, 1 otherwise)
4403 * B2: (on EF113 only, 0 otherwise), one button pressed
4404 * P & W is not reported on EF113 touchpads
4405 */
4406 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4407 if (nfingers == 3 && (pb->ipacket[3] & 0x80))
4408 nfingers = 4;
4409
4410 if (nfingers == 0) {
4411 mask = (1 << nfingers) - 1; /* = 0x00 */
4412 break;
4413 }
4414
4415 /* Map 3-rd and 4-th fingers to first finger */
4416 mask = (1 << 1) - 1; /* = 0x01 */
4417 f[0] = ELANTECH_FINGER_SET_XYP(pb);
4418 if (sc->elanhw.haspressure) {
4419 f[0].w = ((pb->ipacket[0] & 0x30) >> 2) |
4420 ((pb->ipacket[3] & 0x30) >> 4);
4421 } else {
4422 f[0].p = PSM_FINGER_DEFAULT_P;
4423 f[0].w = PSM_FINGER_DEFAULT_W;
4424 }
4425
4426 /*
4427 * HW v2 dont report exact finger positions when 3 or more
4428 * fingers are on touchpad.
4429 */
4430 if (nfingers > 2)
4431 f[0].flags = PSM_FINGER_FUZZY;
4432
4433 break;
4434
4435 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */
4436 /* 7 6 5 4 3 2 1 0 (LSB)
4437 * -------------------------------------------
4438 * ipacket[0]: N1 N0 AY8 AX8 . . R L
4439 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0
4440 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0
4441 * ipacket[3]: . . BY8 BX8 . . . .
4442 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0
4443 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0
4444 * -------------------------------------------
4445 * AX: lower-left finger absolute x value
4446 * AY: lower-left finger absolute y value
4447 * BX: upper-right finger absolute x value
4448 * BY: upper-right finger absolute y value
4449 */
4450 nfingers = 2;
4451 mask = (1 << nfingers) - 1;
4452
4453 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++)
4454 f[id] = (finger_t) {
4455 .x = (((pb->ipacket[id * 3] & 0x10) << 4) |
4456 pb->ipacket[id * 3 + 1]) << 2,
4457 .y = (((pb->ipacket[id * 3] & 0x20) << 3) |
4458 pb->ipacket[id * 3 + 2]) << 2,
4459 .p = PSM_FINGER_DEFAULT_P,
4460 .w = PSM_FINGER_DEFAULT_W,
4461 /* HW ver.2 sends bounding box */
4462 .flags = PSM_FINGER_FUZZY
4463 };
4464 break;
4465
4466 case ELANTECH_PKT_V3: /* HW Version 3 */
4467 /* 7 6 5 4 3 2 1 0 (LSB)
4468 * -------------------------------------------
4469 * ipacket[0]: N1 N0 W3 W2 0 1 R L
4470 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4471 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4472 * ipacket[3]: 0 0 W1 W0 0 0 1 0
4473 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4474 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4475 * -------------------------------------------
4476 */
4477 nfingers = (pb->ipacket[0] & 0xc0) >> 6;
4478 /* Map 3-rd finger to first finger */
4479 id = nfingers > 2 ? 0 : nfingers - 1;
4480 mask = (1 << (id + 1)) - 1;
4481
4482 if (nfingers == 0)
4483 break;
4484
4485 fn = ELANTECH_FINGER_SET_XYP(pb);
4486 fn.w = ((pb->ipacket[0] & 0x30) >> 2) |
4487 ((pb->ipacket[3] & 0x30) >> 4);
4488
4489 /*
4490 * HW v3 dont report exact finger positions when 3 or more
4491 * fingers are on touchpad.
4492 */
4493 if (nfingers > 1)
4494 fn.flags = PSM_FINGER_FUZZY;
4495
4496 if (nfingers == 2) {
4497 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) {
4498 sc->elanaction.fingers[0] = fn;
4499 return (0);
4500 } else
4501 f[0] = sc->elanaction.fingers[0];
4502 }
4503 f[id] = fn;
4504 break;
4505
4506 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */
4507 /* 7 6 5 4 3 2 1 0 (LSB)
4508 * -------------------------------------------
4509 * ipacket[0]: . . . . 0 1 R L
4510 * ipacket[1]: . . . F4 F3 F2 F1 F0
4511 * ipacket[2]: . . . . . . . .
4512 * ipacket[3]: . . . 1 0 0 0 0
4513 * ipacket[4]: PL . . . . . . .
4514 * ipacket[5]: . . . . . . . .
4515 * -------------------------------------------
4516 * Fn: finger n is on touchpad
4517 * PL: palm
4518 * HV ver4 sends a status packet to indicate that the numbers
4519 * or identities of the fingers has been changed
4520 */
4521
4522 mask = pb->ipacket[1] & 0x1f;
4523 nfingers = bitcount(mask);
4524
4525 if (sc->elanaction.mask_v4wait != 0)
4526 VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet"
4527 " when not all previous head packets received\n"));
4528
4529 /* Bitmap of fingers to receive before gesture processing */
4530 sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask;
4531
4532 /* Skip "new finger is on touchpad" packets */
4533 if (sc->elanaction.mask_v4wait) {
4534 sc->elanaction.mask = mask;
4535 return (0);
4536 }
4537
4538 break;
4539
4540 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */
4541 /* 7 6 5 4 3 2 1 0 (LSB)
4542 * -------------------------------------------
4543 * ipacket[0]: W3 W2 W1 W0 0 1 R L
4544 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8
4545 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0
4546 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1
4547 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8
4548 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4549 * -------------------------------------------
4550 * ID: finger id
4551 * HW ver 4 sends head packets in two cases:
4552 * 1. One finger touch and movement.
4553 * 2. Next after status packet to tell new finger positions.
4554 */
4555 mask = sc->elanaction.mask;
4556 nfingers = bitcount(mask);
4557 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1;
4558 fn = ELANTECH_FINGER_SET_XYP(pb);
4559 fn.w =(pb->ipacket[0] & 0xf0) >> 4;
4560
4561 if (id < 0)
4562 return (0);
4563
4564 /* Packet is finger position update. Report it */
4565 if (sc->elanaction.mask_v4wait == 0) {
4566 if (id < ELANTECH_MAX_FINGERS)
4567 f[id] = fn;
4568 break;
4569 }
4570
4571 /* Remove finger from waiting bitmap and store into context */
4572 sc->elanaction.mask_v4wait &= ~(1 << id);
4573 if (id < ELANTECH_MAX_FINGERS)
4574 sc->elanaction.fingers[id] = fn;
4575
4576 /* Wait for other fingers if needed */
4577 if (sc->elanaction.mask_v4wait != 0)
4578 return (0);
4579
4580 /* All new fingers are received. Report them from context */
4581 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4582 if (sc->elanaction.mask & (1 << id))
4583 f[id] = sc->elanaction.fingers[id];
4584
4585 break;
4586
4587 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */
4588 /* 7 6 5 4 3 2 1 0 (LSB)
4589 * -------------------------------------------
4590 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L
4591 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4592 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4593 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0
4594 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0
4595 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0
4596 * -------------------------------------------
4597 * OF: delta overflows (> 127 or < -128), in this case
4598 * firmware sends us (delta x / 5) and (delta y / 5)
4599 * ID: finger id
4600 * DX: delta x (two's complement)
4601 * XY: delta y (two's complement)
4602 * byte 0 ~ 2 for one finger
4603 * byte 3 ~ 5 for another finger
4604 */
4605 mask = sc->elanaction.mask;
4606 nfingers = bitcount(mask);
4607
4608 scale = (pb->ipacket[0] & 0x10) ? 5 : 1;
4609 for (i = 0; i <= 3; i += 3) {
4610 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1;
4611 if (id < 0 || id >= ELANTECH_MAX_FINGERS)
4612 continue;
4613
4614 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) {
4615 f[id] = sc->elanaction.fingers[id];
4616 f[id].x += imax(-f[id].x,
4617 (signed char)pb->ipacket[i+1] * scale);
4618 f[id].y += imax(-f[id].y,
4619 (signed char)pb->ipacket[i+2] * scale);
4620 } else {
4621 VLOG(3, (LOG_DEBUG, "elantech: "
4622 "HW v4 motion packet skipped\n"));
4623 }
4624 }
4625
4626 break;
4627
4628 case ELANTECH_PKT_TRACKPOINT:
4629 /* 7 6 5 4 3 2 1 0 (LSB)
4630 * -------------------------------------------
4631 * ipacket[0]: 0 0 SX SY 0 M R L
4632 * ipacket[1]: ~SX 0 0 0 0 0 0 0
4633 * ipacket[2]: ~SY 0 0 0 0 0 0 0
4634 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0
4635 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0
4636 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
4637 * -------------------------------------------
4638 * X and Y are written in two's complement spread
4639 * over 9 bits with SX/SY the relative top bit and
4640 * X7..X0 and Y7..Y0 the lower bits.
4641 */
4642 *x = (pb->ipacket[0] & 0x20) ?
4643 pb->ipacket[4] - 256 : pb->ipacket[4];
4644 *y = (pb->ipacket[0] & 0x10) ?
4645 pb->ipacket[5] - 256 : pb->ipacket[5];
4646
4647 trackpoint_button =
4648 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4649 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) |
4650 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0);
4651#ifdef EVDEV_SUPPORT
4652 evdev_push_rel(sc->evdev_r, REL_X, *x);
4653 evdev_push_rel(sc->evdev_r, REL_Y, -*y);
4654 evdev_push_mouse_btn(sc->evdev_r, trackpoint_button);
4655 evdev_sync(sc->evdev_r);
4656#endif
4657 ms->button = touchpad_button | trackpoint_button;
4658 return (0);
4659
4660 case ELANTECH_PKT_NOP:
4661 return (0);
4662
4663 default:
4664 return (-1);
4665 }
4666
4667 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
4668 if (PSM_FINGER_IS_SET(f[id]))
4669 VLOG(2, (LOG_DEBUG, "elantech: "
4670 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1,
4671 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags));
4672
4673 /* Touchpad button presses */
4674 if (sc->elanhw.isclickpad) {
4675 touchpad_button =
4676 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0);
4677 } else {
4678 touchpad_button =
4679 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) |
4680 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0);
4681 }
4682
4683#ifdef EVDEV_SUPPORT
4684 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
4685 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4686 if (PSM_FINGER_IS_SET(f[id])) {
4687 psm_push_mt_finger(sc, id, &f[id]);
4688 /* Convert touch width to surface units */
4689 evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR,
4690 f[id].w * sc->elanhw.dptracex);
4691 }
4692 if (sc->elanaction.mask & (1 << id) &&
4693 !(mask & (1 << id)))
4694 psm_release_mt_slot(sc->evdev_a, id);
4695 }
4696 evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0);
4697 evdev_push_nfingers(sc->evdev_a, nfingers);
4698 if (nfingers > 0) {
4699 if (PSM_FINGER_IS_SET(f[0]))
4700 psm_push_st_finger(sc, &f[0]);
4701 } else
4702 evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0);
4703 evdev_push_mouse_btn(sc->evdev_a, touchpad_button);
4704 evdev_sync(sc->evdev_a);
4705 }
4706#endif
4707
4708 ms->button = touchpad_button | trackpoint_button;
4709
4710 /* Send finger 1 position to gesture processor */
4711 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) ||
4712 nfingers == 0)
4713 psmgestures(sc, &f[0], imin(nfingers, 3), ms);
4714 /* Send fingers positions to movement smoothers */
4715 for (id = 0; id < PSM_FINGERS; id++)
4716 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id)))
4717 psmsmoother(sc, &f[id], id, ms, x, y);
4718
4719 /* Store current finger positions in action context */
4720 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) {
4721 if (PSM_FINGER_IS_SET(f[id]))
4722 sc->elanaction.fingers[id] = f[id];
4723 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id)))
4724 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
4725 }
4726 sc->elanaction.mask = mask;
4727
4728 /* Palm detection doesn't terminate the current action. */
4729 if (psmpalmdetect(sc, &f[0], nfingers)) {
4730 *x = *y = *z = 0;
4731 ms->button = ms->obutton;
4732 return (0);
4733 }
4734
4735 /* Use the extra buttons as a scrollwheel */
4736 if (ms->button & MOUSE_BUTTON4DOWN)
4737 *z = -1;
4738 else if (ms->button & MOUSE_BUTTON5DOWN)
4739 *z = 1;
4740 else if (ms->button & MOUSE_BUTTON6DOWN)
4741 *z = -2;
4742 else if (ms->button & MOUSE_BUTTON7DOWN)
4743 *z = 2;
4744 else
4745 *z = 0;
4746 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN |
4747 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN);
4748
4749 return (0);
4750}
4751
4752static void
4753proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms,
4754 int *x, int *y, int *z)
4755{
4756 static int butmap_versapad[8] = {
4757 0,
4758 MOUSE_BUTTON3DOWN,
4759 0,
4760 MOUSE_BUTTON3DOWN,
4761 MOUSE_BUTTON1DOWN,
4762 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4763 MOUSE_BUTTON1DOWN,
4764 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
4765 };
4766 int c, x0, y0;
4767
4768 /* VersaPad PS/2 absolute mode message format
4769 *
4770 * [packet1] 7 6 5 4 3 2 1 0(LSB)
4771 * ipacket[0]: 1 1 0 A 1 L T R
4772 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
4773 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
4774 * ipacket[3]: 1 1 1 A 1 L T R
4775 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
4776 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
4777 *
4778 * [note]
4779 * R: right physical mouse button (1=on)
4780 * T: touch pad virtual button (1=tapping)
4781 * L: left physical mouse button (1=on)
4782 * A: position data is valid (1=valid)
4783 * H: horizontal data (12bit signed integer. H11 is sign bit.)
4784 * V: vertical data (12bit signed integer. V11 is sign bit.)
4785 * P: pressure data
4786 *
4787 * Tapping is mapped to MOUSE_BUTTON4.
4788 */
4789 c = pb->ipacket[0];
4790 *x = *y = 0;
4791 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
4792 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
4793 if (c & MOUSE_PS2VERSA_IN_USE) {
4794 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8);
4795 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4);
4796 if (x0 & 0x800)
4797 x0 -= 0x1000;
4798 if (y0 & 0x800)
4799 y0 -= 0x1000;
4800 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
4801 *x = sc->xold - x0;
4802 *y = y0 - sc->yold;
4803 if (*x < 0) /* XXX */
4804 ++*x;
4805 else if (*x)
4806 --*x;
4807 if (*y < 0)
4808 ++*y;
4809 else if (*y)
4810 --*y;
4811 } else
4812 sc->flags |= PSM_FLAGS_FINGERDOWN;
4813 sc->xold = x0;
4814 sc->yold = y0;
4815 } else
4816 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
4817}
4818
4819static void
4820psmsoftintridle(void *arg)
4821{
4822 struct psm_softc *sc = arg;
4823 packetbuf_t *pb;
4824
4825 /* Invoke soft handler only when pqueue is empty. Otherwise it will be
4826 * invoked from psmintr soon with pqueue filled with real data */
4827 if (sc->pqueue_start == sc->pqueue_end &&
4828 sc->idlepacket.inputbytes > 0) {
4829 /* Grow circular queue backwards to avoid race with psmintr */
4830 if (--sc->pqueue_start < 0)
4831 sc->pqueue_start = PSM_PACKETQUEUE - 1;
4832
4833 pb = &sc->pqueue[sc->pqueue_start];
4834 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t));
4835 VLOG(4, (LOG_DEBUG,
4836 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n",
4837 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2],
4838 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5]));
4839
4840 psmsoftintr(arg);
4841 }
4842}
4843
4844static void
4845psmsoftintr(void *arg)
4846{
4847 /*
4848 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
4849 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
4850 */
4851 static int butmap[8] = {
4852 0,
4853 MOUSE_BUTTON1DOWN,
4854 MOUSE_BUTTON3DOWN,
4855 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
4856 MOUSE_BUTTON2DOWN,
4857 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
4858 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
4859 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
4860 };
4861 struct psm_softc *sc = arg;
4862 mousestatus_t ms;
4863 packetbuf_t *pb;
4864 int x, y, z, c, l, s;
4865
4866 getmicrouptime(&sc->lastsoftintr);
4867
4868 s = spltty();
4869
4870 do {
4871 pb = &sc->pqueue[sc->pqueue_start];
4872
4873 if (sc->mode.level == PSM_LEVEL_NATIVE)
4874 goto next_native;
4875
4876 c = pb->ipacket[0];
4877 /*
4878 * A kludge for Kensington device!
4879 * The MSB of the horizontal count appears to be stored in
4880 * a strange place.
4881 */
4882 if (sc->hw.model == MOUSE_MODEL_THINK)
4883 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
4884
4885 /* ignore the overflow bits... */
4886 x = (c & MOUSE_PS2_XNEG) ?
4887 pb->ipacket[1] - 256 : pb->ipacket[1];
4888 y = (c & MOUSE_PS2_YNEG) ?
4889 pb->ipacket[2] - 256 : pb->ipacket[2];
4890 z = 0;
4891 ms.obutton = sc->button; /* previous button state */
4892 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
4893 /* `tapping' action */
4894 if (sc->config & PSM_CONFIG_FORCETAP)
4895 ms.button |= ((c & MOUSE_PS2_TAP)) ?
4896 0 : MOUSE_BUTTON4DOWN;
4897 timevalclear(&sc->idletimeout);
4898 sc->idlepacket.inputbytes = 0;
4899
4900 switch (sc->hw.model) {
4901
4902 case MOUSE_MODEL_EXPLORER:
4903 /*
4904 * b7 b6 b5 b4 b3 b2 b1 b0
4905 * byte 1: oy ox sy sx 1 M R L
4906 * byte 2: x x x x x x x x
4907 * byte 3: y y y y y y y y
4908 * byte 4: * * S2 S1 s d2 d1 d0
4909 *
4910 * L, M, R, S1, S2: left, middle, right and side buttons
4911 * s: wheel data sign bit
4912 * d2-d0: wheel data
4913 */
4914 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ?
4915 (pb->ipacket[3] & 0x0f) - 16 :
4916 (pb->ipacket[3] & 0x0f);
4917 ms.button |=
4918 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ?
4919 MOUSE_BUTTON4DOWN : 0;
4920 ms.button |=
4921 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ?
4922 MOUSE_BUTTON5DOWN : 0;
4923 break;
4924
4925 case MOUSE_MODEL_INTELLI:
4926 case MOUSE_MODEL_NET:
4927 /* wheel data is in the fourth byte */
4928 z = (char)pb->ipacket[3];
4929 /*
4930 * XXX some mice may send 7 when there is no Z movement? */
4931 if ((z >= 7) || (z <= -7))
4932 z = 0;
4933 /* some compatible mice have additional buttons */
4934 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ?
4935 MOUSE_BUTTON4DOWN : 0;
4936 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ?
4937 MOUSE_BUTTON5DOWN : 0;
4938 break;
4939
4940 case MOUSE_MODEL_MOUSEMANPLUS:
4941 proc_mmanplus(sc, pb, &ms, &x, &y, &z);
4942 break;
4943
4944 case MOUSE_MODEL_GLIDEPOINT:
4945 /* `tapping' action */
4946 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 :
4947 MOUSE_BUTTON4DOWN;
4948 break;
4949
4950 case MOUSE_MODEL_NETSCROLL:
4951 /*
4952 * three additional bytes encode buttons and
4953 * wheel events
4954 */
4955 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ?
4956 MOUSE_BUTTON4DOWN : 0;
4957 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ?
4958 MOUSE_BUTTON5DOWN : 0;
4959 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ?
4960 pb->ipacket[4] - 256 : pb->ipacket[4];
4961 break;
4962
4963 case MOUSE_MODEL_THINK:
4964 /* the fourth button state in the first byte */
4965 ms.button |= (c & MOUSE_PS2_TAP) ?
4966 MOUSE_BUTTON4DOWN : 0;
4967 break;
4968
4969 case MOUSE_MODEL_VERSAPAD:
4970 proc_versapad(sc, pb, &ms, &x, &y, &z);
4971 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) |
4972 ((y < 0) ? MOUSE_PS2_YNEG : 0);
4973 break;
4974
4975 case MOUSE_MODEL_4D:
4976 /*
4977 * b7 b6 b5 b4 b3 b2 b1 b0
4978 * byte 1: s2 d2 s1 d1 1 M R L
4979 * byte 2: sx x x x x x x x
4980 * byte 3: sy y y y y y y y
4981 *
4982 * s1: wheel 1 direction
4983 * d1: wheel 1 data
4984 * s2: wheel 2 direction
4985 * d2: wheel 2 data
4986 */
4987 x = (pb->ipacket[1] & 0x80) ?
4988 pb->ipacket[1] - 256 : pb->ipacket[1];
4989 y = (pb->ipacket[2] & 0x80) ?
4990 pb->ipacket[2] - 256 : pb->ipacket[2];
4991 switch (c & MOUSE_4D_WHEELBITS) {
4992 case 0x10:
4993 z = 1;
4994 break;
4995 case 0x30:
4996 z = -1;
4997 break;
4998 case 0x40: /* XXX 2nd wheel turning right */
4999 z = 2;
5000 break;
5001 case 0xc0: /* XXX 2nd wheel turning left */
5002 z = -2;
5003 break;
5004 }
5005 break;
5006
5007 case MOUSE_MODEL_4DPLUS:
5008 if ((x < 16 - 256) && (y < 16 - 256)) {
5009 /*
5010 * b7 b6 b5 b4 b3 b2 b1 b0
5011 * byte 1: 0 0 1 1 1 M R L
5012 * byte 2: 0 0 0 0 1 0 0 0
5013 * byte 3: 0 0 0 0 S s d1 d0
5014 *
5015 * L, M, R, S: left, middle, right,
5016 * and side buttons
5017 * s: wheel data sign bit
5018 * d1-d0: wheel data
5019 */
5020 x = y = 0;
5021 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
5022 ms.button |= MOUSE_BUTTON4DOWN;
5023 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ?
5024 ((pb->ipacket[2] & 0x07) - 8) :
5025 (pb->ipacket[2] & 0x07) ;
5026 } else {
5027 /* preserve previous button states */
5028 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
5029 }
5030 break;
5031
5032 case MOUSE_MODEL_SYNAPTICS:
5033 if (pb->inputbytes == MOUSE_PS2_PACKETSIZE)
5034 if (proc_synaptics_mux(sc, pb))
5035 goto next;
5036
4938 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
4939 VLOG(3, (LOG_DEBUG, "synaptics: "
4940 "packet rejected\n"));
4941 goto next;
4942 }
4943 break;
4944
4945 case MOUSE_MODEL_ELANTECH:
4946 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) {
4947 VLOG(3, (LOG_DEBUG, "elantech: "
4948 "packet rejected\n"));
4949 goto next;
4950 }
4951 break;
4952
4953 case MOUSE_MODEL_TRACKPOINT:
4954 case MOUSE_MODEL_GENERIC:
4955 default:
4956 break;
4957 }
4958
4959#ifdef EVDEV_SUPPORT
4960 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE &&
4961 sc->hw.model != MOUSE_MODEL_ELANTECH &&
4962 sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
4963 evdev_push_rel(sc->evdev_r, REL_X, x);
4964 evdev_push_rel(sc->evdev_r, REL_Y, -y);
4965
4966 switch (sc->hw.model) {
4967 case MOUSE_MODEL_EXPLORER:
4968 case MOUSE_MODEL_INTELLI:
4969 case MOUSE_MODEL_NET:
4970 case MOUSE_MODEL_NETSCROLL:
4971 case MOUSE_MODEL_4DPLUS:
4972 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
4973 break;
4974 case MOUSE_MODEL_MOUSEMANPLUS:
4975 case MOUSE_MODEL_4D:
4976 switch (z) {
4977 case 1:
4978 case -1:
4979 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
4980 break;
4981 case 2:
4982 case -2:
4983 evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2);
4984 break;
4985 }
4986 break;
4987 }
4988
4989 evdev_push_mouse_btn(sc->evdev_r, ms.button);
4990 evdev_sync(sc->evdev_r);
4991 }
4992#endif
4993
4994 /* scale values */
4995 if (sc->mode.accelfactor >= 1) {
4996 if (x != 0) {
4997 x = x * x / sc->mode.accelfactor;
4998 if (x == 0)
4999 x = 1;
5000 if (c & MOUSE_PS2_XNEG)
5001 x = -x;
5002 }
5003 if (y != 0) {
5004 y = y * y / sc->mode.accelfactor;
5005 if (y == 0)
5006 y = 1;
5007 if (c & MOUSE_PS2_YNEG)
5008 y = -y;
5009 }
5010 }
5011
5012 /* Store last packet for reinjection if it has not been set already */
5013 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
5014 sc->idlepacket = *pb;
5015
5016 ms.dx = x;
5017 ms.dy = y;
5018 ms.dz = z;
5019 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
5020 (ms.obutton ^ ms.button);
5021
5022 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
5023
5024 sc->status.flags |= ms.flags;
5025 sc->status.dx += ms.dx;
5026 sc->status.dy += ms.dy;
5027 sc->status.dz += ms.dz;
5028 sc->status.button = ms.button;
5029 sc->button = ms.button;
5030
5031next_native:
5032 sc->watchdog = FALSE;
5033
5034 /* queue data */
5035 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
5036 l = imin(pb->inputbytes,
5037 sizeof(sc->queue.buf) - sc->queue.tail);
5038 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
5039 if (pb->inputbytes > l)
5040 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
5041 pb->inputbytes - l);
5042 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
5043 sizeof(sc->queue.buf);
5044 sc->queue.count += pb->inputbytes;
5045 }
5046
5047next:
5048 pb->inputbytes = 0;
5049 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
5050 sc->pqueue_start = 0;
5051 } while (sc->pqueue_start != sc->pqueue_end);
5052
5053 if (sc->state & PSM_ASLP) {
5054 sc->state &= ~PSM_ASLP;
5055 wakeup(sc);
5056 }
5057 selwakeuppri(&sc->rsel, PZERO);
5058 if (sc->async != NULL) {
5059 pgsigio(&sc->async, SIGIO, 0);
5060 }
5061 sc->state &= ~PSM_SOFTARMED;
5062
5063 /* schedule injection of predefined packet after idletimeout
5064 * if no data packets have been received from psmintr */
5065 if (timevalisset(&sc->idletimeout)) {
5066 sc->state |= PSM_SOFTARMED;
5067 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
5068 psmsoftintridle, sc);
5069 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
5070 tvtohz(&sc->idletimeout)));
5071 }
5072 splx(s);
5073}
5074
5075static int
5076psmpoll(struct cdev *dev, int events, struct thread *td)
5077{
5078 struct psm_softc *sc = dev->si_drv1;
5079 int s;
5080 int revents = 0;
5081
5082 /* Return true if a mouse event available */
5083 s = spltty();
5084 if (events & (POLLIN | POLLRDNORM)) {
5085 if (sc->queue.count > 0)
5086 revents |= events & (POLLIN | POLLRDNORM);
5087 else
5088 selrecord(td, &sc->rsel);
5089 }
5090 splx(s);
5091
5092 return (revents);
5093}
5094
5095/* vendor/model specific routines */
5096
5097static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
5098{
5099 if (set_mouse_resolution(kbdc, res) != res)
5100 return (FALSE);
5101 if (set_mouse_scaling(kbdc, scale) &&
5102 set_mouse_scaling(kbdc, scale) &&
5103 set_mouse_scaling(kbdc, scale) &&
5104 (get_mouse_status(kbdc, status, 0, 3) >= 3))
5105 return (TRUE);
5106 return (FALSE);
5107}
5108
5109static int
5110mouse_ext_command(KBDC kbdc, int command)
5111{
5112 int c;
5113
5114 c = (command >> 6) & 0x03;
5115 if (set_mouse_resolution(kbdc, c) != c)
5116 return (FALSE);
5117 c = (command >> 4) & 0x03;
5118 if (set_mouse_resolution(kbdc, c) != c)
5119 return (FALSE);
5120 c = (command >> 2) & 0x03;
5121 if (set_mouse_resolution(kbdc, c) != c)
5122 return (FALSE);
5123 c = (command >> 0) & 0x03;
5124 if (set_mouse_resolution(kbdc, c) != c)
5125 return (FALSE);
5126 return (TRUE);
5127}
5128
5129#ifdef notyet
5130/* Logitech MouseMan Cordless II */
5131static int
5132enable_lcordless(struct psm_softc *sc, enum probearg arg)
5133{
5134 KBDC kbdc = sc->kbdc;
5135 int status[3];
5136 int ch;
5137
5138 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
5139 return (FALSE);
5140 if (status[1] == PSMD_RES_HIGH)
5141 return (FALSE);
5142 ch = (status[0] & 0x07) - 1; /* channel # */
5143 if ((ch <= 0) || (ch > 4))
5144 return (FALSE);
5145 /*
5146 * status[1]: always one?
5147 * status[2]: battery status? (0-100)
5148 */
5149 return (TRUE);
5150}
5151#endif /* notyet */
5152
5153/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
5154static int
5155enable_groller(struct psm_softc *sc, enum probearg arg)
5156{
5157 KBDC kbdc = sc->kbdc;
5158 int status[3];
5159
5160 /*
5161 * The special sequence to enable the fourth button and the
5162 * roller. Immediately after this sequence check status bytes.
5163 * if the mouse is NetScroll, the second and the third bytes are
5164 * '3' and 'D'.
5165 */
5166
5167 /*
5168 * If the mouse is an ordinary PS/2 mouse, the status bytes should
5169 * look like the following.
5170 *
5171 * byte 1 bit 7 always 0
5172 * bit 6 stream mode (0)
5173 * bit 5 disabled (0)
5174 * bit 4 1:1 scaling (0)
5175 * bit 3 always 0
5176 * bit 0-2 button status
5177 * byte 2 resolution (PSMD_RES_HIGH)
5178 * byte 3 report rate (?)
5179 */
5180
5181 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5182 return (FALSE);
5183 if ((status[1] != '3') || (status[2] != 'D'))
5184 return (FALSE);
5185 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
5186 if (arg == PROBE)
5187 sc->hw.buttons = 4;
5188 return (TRUE);
5189}
5190
5191/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
5192static int
5193enable_gmouse(struct psm_softc *sc, enum probearg arg)
5194{
5195 KBDC kbdc = sc->kbdc;
5196 int status[3];
5197
5198 /*
5199 * The special sequence to enable the middle, "rubber" button.
5200 * Immediately after this sequence check status bytes.
5201 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
5202 * the second and the third bytes are '3' and 'U'.
5203 * NOTE: NetMouse reports that it has three buttons although it has
5204 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
5205 * say they have three buttons too and they do have a button on the
5206 * side...
5207 */
5208 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5209 return (FALSE);
5210 if ((status[1] != '3') || (status[2] != 'U'))
5211 return (FALSE);
5212 return (TRUE);
5213}
5214
5215/* ALPS GlidePoint */
5216static int
5217enable_aglide(struct psm_softc *sc, enum probearg arg)
5218{
5219 KBDC kbdc = sc->kbdc;
5220 int status[3];
5221
5222 /*
5223 * The special sequence to obtain ALPS GlidePoint specific
5224 * information. Immediately after this sequence, status bytes will
5225 * contain something interesting.
5226 * NOTE: ALPS produces several models of GlidePoint. Some of those
5227 * do not respond to this sequence, thus, cannot be detected this way.
5228 */
5229 if (set_mouse_sampling_rate(kbdc, 100) != 100)
5230 return (FALSE);
5231 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
5232 return (FALSE);
5233 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
5234 return (FALSE);
5235 return (TRUE);
5236}
5237
5238/* Kensington ThinkingMouse/Trackball */
5239static int
5240enable_kmouse(struct psm_softc *sc, enum probearg arg)
5241{
5242 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
5243 KBDC kbdc = sc->kbdc;
5244 int status[3];
5245 int id1;
5246 int id2;
5247 int i;
5248
5249 id1 = get_aux_id(kbdc);
5250 if (set_mouse_sampling_rate(kbdc, 10) != 10)
5251 return (FALSE);
5252 /*
5253 * The device is now in the native mode? It returns a different
5254 * ID value...
5255 */
5256 id2 = get_aux_id(kbdc);
5257 if ((id1 == id2) || (id2 != 2))
5258 return (FALSE);
5259
5260 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
5261 return (FALSE);
5262#if PSM_DEBUG >= 2
5263 /* at this point, resolution is LOW, sampling rate is 10/sec */
5264 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5265 return (FALSE);
5266#endif
5267
5268 /*
5269 * The special sequence to enable the third and fourth buttons.
5270 * Otherwise they behave like the first and second buttons.
5271 */
5272 for (i = 0; i < nitems(rate); ++i)
5273 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5274 return (FALSE);
5275
5276 /*
5277 * At this point, the device is using default resolution and
5278 * sampling rate for the native mode.
5279 */
5280 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5281 return (FALSE);
5282 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
5283 return (FALSE);
5284
5285 /* the device appears be enabled by this sequence, diable it for now */
5286 disable_aux_dev(kbdc);
5287 empty_aux_buffer(kbdc, 5);
5288
5289 return (TRUE);
5290}
5291
5292/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
5293static int
5294enable_mmanplus(struct psm_softc *sc, enum probearg arg)
5295{
5296 KBDC kbdc = sc->kbdc;
5297 int data[3];
5298
5299 /* the special sequence to enable the fourth button and the roller. */
5300 /*
5301 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
5302 * must be called exactly three times since the last RESET command
5303 * before this sequence. XXX
5304 */
5305 if (!set_mouse_scaling(kbdc, 1))
5306 return (FALSE);
5307 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
5308 return (FALSE);
5309 if (get_mouse_status(kbdc, data, 1, 3) < 3)
5310 return (FALSE);
5311
5312 /*
5313 * PS2++ protocol, packet type 0
5314 *
5315 * b7 b6 b5 b4 b3 b2 b1 b0
5316 * byte 1: * 1 p3 p2 1 * * *
5317 * byte 2: 1 1 p1 p0 m1 m0 1 0
5318 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
5319 *
5320 * p3-p0: packet type: 0
5321 * m7-m0: model ID: MouseMan+:0x50,
5322 * FirstMouse+:0x51,
5323 * ScrollPoint:0x58...
5324 */
5325 /* check constant bits */
5326 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
5327 return (FALSE);
5328 if ((data[1] & 0xc3) != 0xc2)
5329 return (FALSE);
5330 /* check d3-d0 in byte 2 */
5331 if (!MOUSE_PS2PLUS_CHECKBITS(data))
5332 return (FALSE);
5333 /* check p3-p0 */
5334 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
5335 return (FALSE);
5336
5337 if (arg == PROBE) {
5338 sc->hw.hwid &= 0x00ff;
5339 sc->hw.hwid |= data[2] << 8; /* save model ID */
5340 }
5341
5342 /*
5343 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
5344 * the wheel and the fourth button events are encoded in the
5345 * special data packet. The mouse may be put in the IntelliMouse mode
5346 * if it is initialized by the IntelliMouse's method.
5347 */
5348 return (TRUE);
5349}
5350
5351/* MS IntelliMouse Explorer */
5352static int
5353enable_msexplorer(struct psm_softc *sc, enum probearg arg)
5354{
5355 KBDC kbdc = sc->kbdc;
5356 static u_char rate0[] = { 200, 100, 80, };
5357 static u_char rate1[] = { 200, 200, 80, };
5358 int id;
5359 int i;
5360
5361 /*
5362 * This is needed for at least A4Tech X-7xx mice - they do not go
5363 * straight to Explorer mode, but need to be set to Intelli mode
5364 * first.
5365 */
5366 enable_msintelli(sc, arg);
5367
5368 /* the special sequence to enable the extra buttons and the roller. */
5369 for (i = 0; i < nitems(rate1); ++i)
5370 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
5371 return (FALSE);
5372 /* the device will give the genuine ID only after the above sequence */
5373 id = get_aux_id(kbdc);
5374 if (id != PSM_EXPLORER_ID)
5375 return (FALSE);
5376
5377 if (arg == PROBE) {
5378 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
5379 sc->hw.hwid = id;
5380 }
5381
5382 /*
5383 * XXX: this is a kludge to fool some KVM switch products
5384 * which think they are clever enough to know the 4-byte IntelliMouse
5385 * protocol, and assume any other protocols use 3-byte packets.
5386 * They don't convey 4-byte data packets from the IntelliMouse Explorer
5387 * correctly to the host computer because of this!
5388 * The following sequence is actually IntelliMouse's "wake up"
5389 * sequence; it will make the KVM think the mouse is IntelliMouse
5390 * when it is in fact IntelliMouse Explorer.
5391 */
5392 for (i = 0; i < nitems(rate0); ++i)
5393 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
5394 break;
5395 get_aux_id(kbdc);
5396
5397 return (TRUE);
5398}
5399
5400/*
5401 * MS IntelliMouse
5402 * Logitech MouseMan+ and FirstMouse+ will also respond to this
5403 * probe routine and act like IntelliMouse.
5404 */
5405static int
5406enable_msintelli(struct psm_softc *sc, enum probearg arg)
5407{
5408 KBDC kbdc = sc->kbdc;
5409 static u_char rate[] = { 200, 100, 80, };
5410 int id;
5411 int i;
5412
5413 /* the special sequence to enable the third button and the roller. */
5414 for (i = 0; i < nitems(rate); ++i)
5415 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5416 return (FALSE);
5417 /* the device will give the genuine ID only after the above sequence */
5418 id = get_aux_id(kbdc);
5419 if (id != PSM_INTELLI_ID)
5420 return (FALSE);
5421
5422 if (arg == PROBE) {
5423 sc->hw.buttons = 3;
5424 sc->hw.hwid = id;
5425 }
5426
5427 return (TRUE);
5428}
5429
5430/*
5431 * A4 Tech 4D Mouse
5432 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
5433 */
5434static int
5435enable_4dmouse(struct psm_softc *sc, enum probearg arg)
5436{
5437 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5438 KBDC kbdc = sc->kbdc;
5439 int id;
5440 int i;
5441
5442 for (i = 0; i < nitems(rate); ++i)
5443 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5444 return (FALSE);
5445 id = get_aux_id(kbdc);
5446 /*
5447 * WinEasy 4D, 4 Way Scroll 4D: 6
5448 * Cable-Free 4D: 8 (4DPLUS)
5449 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
5450 */
5451 if (id != PSM_4DMOUSE_ID)
5452 return (FALSE);
5453
5454 if (arg == PROBE) {
5455 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
5456 sc->hw.hwid = id;
5457 }
5458
5459 return (TRUE);
5460}
5461
5462/*
5463 * A4 Tech 4D+ Mouse
5464 * Newer wheel mice from A4 Tech seem to use this protocol.
5465 * Older models are recognized as either 4D Mouse or IntelliMouse.
5466 */
5467static int
5468enable_4dplus(struct psm_softc *sc, enum probearg arg)
5469{
5470 KBDC kbdc = sc->kbdc;
5471 int id;
5472
5473 /*
5474 * enable_4dmouse() already issued the following ID sequence...
5475 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5476 int i;
5477
5478 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
5479 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5480 return (FALSE);
5481 */
5482
5483 id = get_aux_id(kbdc);
5484 switch (id) {
5485 case PSM_4DPLUS_ID:
5486 break;
5487 case PSM_4DPLUS_RFSW35_ID:
5488 break;
5489 default:
5490 return (FALSE);
5491 }
5492
5493 if (arg == PROBE) {
5494 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
5495 sc->hw.hwid = id;
5496 }
5497
5498 return (TRUE);
5499}
5500
5501/* Synaptics Touchpad */
5502static int
5503synaptics_sysctl(SYSCTL_HANDLER_ARGS)
5504{
5505 struct psm_softc *sc;
5506 int error, arg;
5507
5508 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
5509 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X)
5510 return (EINVAL);
5511
5512 sc = oidp->oid_arg1;
5513
5514 /* Read the current value. */
5515 arg = *(int *)((char *)sc + oidp->oid_arg2);
5516 error = sysctl_handle_int(oidp, &arg, 0, req);
5517
5518 /* Sanity check. */
5519 if (error || !req->newptr)
5520 return (error);
5521
5522 /*
5523 * Check that the new value is in the concerned node's range
5524 * of values.
5525 */
5526 switch (oidp->oid_arg2) {
5527 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
5528 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
5529 if (arg < 0 || arg > 255)
5530 return (EINVAL);
5531 break;
5532 case SYNAPTICS_SYSCTL_MAX_WIDTH:
5533 if (arg < 4 || arg > 15)
5534 return (EINVAL);
5535 break;
5536 case SYNAPTICS_SYSCTL_MARGIN_TOP:
5537 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
5538 case SYNAPTICS_SYSCTL_NA_TOP:
5539 case SYNAPTICS_SYSCTL_NA_BOTTOM:
5540 if (arg < 0 || arg > sc->synhw.maximumYCoord)
5541 return (EINVAL);
5542 break;
5543 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
5544 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
5545 /* Softbuttons is clickpad only feature */
5546 if (!sc->synhw.capClickPad && arg != 0)
5547 return (EINVAL);
5548 /* FALLTHROUGH */
5549 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
5550 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
5551 case SYNAPTICS_SYSCTL_NA_RIGHT:
5552 case SYNAPTICS_SYSCTL_NA_LEFT:
5553 if (arg < 0 || arg > sc->synhw.maximumXCoord)
5554 return (EINVAL);
5555 break;
5556 case SYNAPTICS_SYSCTL_WINDOW_MIN:
5557 case SYNAPTICS_SYSCTL_WINDOW_MAX:
5558 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
5559 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
5560 return (EINVAL);
5561 break;
5562 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
5563 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
5564 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
5565 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
5566 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
5567 case SYNAPTICS_SYSCTL_DIV_MIN:
5568 case SYNAPTICS_SYSCTL_DIV_MAX:
5569 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
5570 case SYNAPTICS_SYSCTL_DIV_LEN:
5571 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
5572 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
5573 if (arg < 1)
5574 return (EINVAL);
5575 break;
5576 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
5577 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
5578 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
5579 if (arg < 0)
5580 return (EINVAL);
5581 break;
5582 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
5583 if (arg < -sc->synhw.maximumXCoord ||
5584 arg > sc->synhw.maximumXCoord)
5585 return (EINVAL);
5586 break;
5587 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
5588 /* Softbuttons is clickpad only feature */
5589 if (!sc->synhw.capClickPad && arg != 0)
5590 return (EINVAL);
5591 /* FALLTHROUGH */
5592 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
5593 if (arg < -sc->synhw.maximumYCoord ||
5594 arg > sc->synhw.maximumYCoord)
5595 return (EINVAL);
5596 break;
5597 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
5598 if (arg < 0 || arg > 1)
5599 return (EINVAL);
5600 break;
5601 default:
5602 return (EINVAL);
5603 }
5604
5605 /* Update. */
5606 *(int *)((char *)sc + oidp->oid_arg2) = arg;
5607
5608 return (error);
5609}
5610
5611static void
5612synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
5613{
5614 /*
5615 * Set predefined sizes for softbuttons.
5616 * Values are taken to match HP Pavilion dv6 clickpad drawings
5617 * with thin middle softbutton placed on separator
5618 */
5619
5620 /* hw.psm.synaptics.softbuttons_y */
5621 sc->syninfo.softbuttons_y = 1700;
5622 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5623 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5624 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5625 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
5626 synaptics_sysctl, "I",
5627 "Vertical size of softbuttons area");
5628
5629 /* hw.psm.synaptics.softbutton2_x */
5630 sc->syninfo.softbutton2_x = 3100;
5631 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5632 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5633 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5634 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
5635 synaptics_sysctl, "I",
5636 "Horisontal position of 2-nd softbutton left edge (0-disable)");
5637
5638 /* hw.psm.synaptics.softbutton3_x */
5639 sc->syninfo.softbutton3_x = 3900;
5640 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5641 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5642 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5643 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
5644 synaptics_sysctl, "I",
5645 "Horisontal position of 3-rd softbutton left edge (0-disable)");
5646}
5647
5648static void
5649synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5650 const char *descr)
5651{
5652
5653 if (sc->syninfo.sysctl_tree != NULL)
5654 return;
5655
5656 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5657 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5658 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5659 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5660 0, descr);
5661
5662 /* hw.psm.synaptics.directional_scrolls. */
5663 sc->syninfo.directional_scrolls = 0;
5664 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5665 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5666 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5667 &sc->syninfo.directional_scrolls, 0,
5668 "Enable hardware scrolling pad (if non-zero) or register it as "
5669 "extended buttons (if 0)");
5670
5671 /* hw.psm.synaptics.max_x. */
5672 sc->syninfo.max_x = 6143;
5673 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5674 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5675 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5676 &sc->syninfo.max_x, 0,
5677 "Horizontal reporting range");
5678
5679 /* hw.psm.synaptics.max_y. */
5680 sc->syninfo.max_y = 6143;
5681 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5682 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5683 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5684 &sc->syninfo.max_y, 0,
5685 "Vertical reporting range");
5686
5687 /*
5688 * Turn off two finger scroll if we have a
5689 * physical area reserved for scrolling or when
5690 * there's no multi finger support.
5691 */
5692 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5693 sc->synhw.capAdvancedGestures == 0))
5694 sc->syninfo.two_finger_scroll = 0;
5695 else
5696 sc->syninfo.two_finger_scroll = 1;
5697 /* hw.psm.synaptics.two_finger_scroll. */
5698 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5699 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5700 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5701 &sc->syninfo.two_finger_scroll, 0,
5702 "Enable two finger scrolling");
5703
5704 /* hw.psm.synaptics.min_pressure. */
5705 sc->syninfo.min_pressure = 32;
5706 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5707 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5708 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5709 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5710 synaptics_sysctl, "I",
5711 "Minimum pressure required to start an action");
5712
5713 /* hw.psm.synaptics.max_pressure. */
5714 sc->syninfo.max_pressure = 220;
5715 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5716 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5717 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5718 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5719 synaptics_sysctl, "I",
5720 "Maximum pressure to detect palm");
5721
5722 /* hw.psm.synaptics.max_width. */
5723 sc->syninfo.max_width = 10;
5724 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5725 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5726 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5727 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5728 synaptics_sysctl, "I",
5729 "Maximum finger width to detect palm");
5730
5731 /* hw.psm.synaptics.top_margin. */
5732 sc->syninfo.margin_top = 200;
5733 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5734 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5735 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5736 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5737 synaptics_sysctl, "I",
5738 "Top margin");
5739
5740 /* hw.psm.synaptics.right_margin. */
5741 sc->syninfo.margin_right = 200;
5742 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5743 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5744 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5745 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5746 synaptics_sysctl, "I",
5747 "Right margin");
5748
5749 /* hw.psm.synaptics.bottom_margin. */
5750 sc->syninfo.margin_bottom = 200;
5751 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5752 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5753 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5754 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5755 synaptics_sysctl, "I",
5756 "Bottom margin");
5757
5758 /* hw.psm.synaptics.left_margin. */
5759 sc->syninfo.margin_left = 200;
5760 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5761 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5762 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5763 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5764 synaptics_sysctl, "I",
5765 "Left margin");
5766
5767 /* hw.psm.synaptics.na_top. */
5768 sc->syninfo.na_top = 1783;
5769 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5770 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5771 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5772 sc, SYNAPTICS_SYSCTL_NA_TOP,
5773 synaptics_sysctl, "I",
5774 "Top noisy area, where weight_previous_na is used instead "
5775 "of weight_previous");
5776
5777 /* hw.psm.synaptics.na_right. */
5778 sc->syninfo.na_right = 563;
5779 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5780 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5781 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5782 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5783 synaptics_sysctl, "I",
5784 "Right noisy area, where weight_previous_na is used instead "
5785 "of weight_previous");
5786
5787 /* hw.psm.synaptics.na_bottom. */
5788 sc->syninfo.na_bottom = 1408;
5789 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5790 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5791 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5792 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5793 synaptics_sysctl, "I",
5794 "Bottom noisy area, where weight_previous_na is used instead "
5795 "of weight_previous");
5796
5797 /* hw.psm.synaptics.na_left. */
5798 sc->syninfo.na_left = 1600;
5799 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5800 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5801 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5802 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5803 synaptics_sysctl, "I",
5804 "Left noisy area, where weight_previous_na is used instead "
5805 "of weight_previous");
5806
5807 /* hw.psm.synaptics.window_min. */
5808 sc->syninfo.window_min = 4;
5809 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5810 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5811 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5812 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5813 synaptics_sysctl, "I",
5814 "Minimum window size to start an action");
5815
5816 /* hw.psm.synaptics.window_max. */
5817 sc->syninfo.window_max = 10;
5818 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5819 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5820 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5821 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
5822 synaptics_sysctl, "I",
5823 "Maximum window size");
5824
5825 /* hw.psm.synaptics.multiplicator. */
5826 sc->syninfo.multiplicator = 10000;
5827 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5828 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5829 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5830 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
5831 synaptics_sysctl, "I",
5832 "Multiplicator to increase precision in averages and divisions");
5833
5834 /* hw.psm.synaptics.weight_current. */
5835 sc->syninfo.weight_current = 3;
5836 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5837 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5838 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5839 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
5840 synaptics_sysctl, "I",
5841 "Weight of the current movement in the new average");
5842
5843 /* hw.psm.synaptics.weight_previous. */
5844 sc->syninfo.weight_previous = 6;
5845 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5846 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5847 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5848 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
5849 synaptics_sysctl, "I",
5850 "Weight of the previous average");
5851
5852 /* hw.psm.synaptics.weight_previous_na. */
5853 sc->syninfo.weight_previous_na = 20;
5854 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5855 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5856 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5857 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
5858 synaptics_sysctl, "I",
5859 "Weight of the previous average (inside the noisy area)");
5860
5861 /* hw.psm.synaptics.weight_len_squared. */
5862 sc->syninfo.weight_len_squared = 2000;
5863 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5864 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5865 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5866 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
5867 synaptics_sysctl, "I",
5868 "Length (squared) of segments where weight_previous "
5869 "starts to decrease");
5870
5871 /* hw.psm.synaptics.div_min. */
5872 sc->syninfo.div_min = 9;
5873 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5874 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5875 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5876 sc, SYNAPTICS_SYSCTL_DIV_MIN,
5877 synaptics_sysctl, "I",
5878 "Divisor for fast movements");
5879
5880 /* hw.psm.synaptics.div_max. */
5881 sc->syninfo.div_max = 17;
5882 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5883 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5884 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5885 sc, SYNAPTICS_SYSCTL_DIV_MAX,
5886 synaptics_sysctl, "I",
5887 "Divisor for slow movements");
5888
5889 /* hw.psm.synaptics.div_max_na. */
5890 sc->syninfo.div_max_na = 30;
5891 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5892 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5893 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5894 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
5895 synaptics_sysctl, "I",
5896 "Divisor with slow movements (inside the noisy area)");
5897
5898 /* hw.psm.synaptics.div_len. */
5899 sc->syninfo.div_len = 100;
5900 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5901 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5902 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5903 sc, SYNAPTICS_SYSCTL_DIV_LEN,
5904 synaptics_sysctl, "I",
5905 "Length of segments where div_max starts to decrease");
5906
5907 /* hw.psm.synaptics.tap_max_delta. */
5908 sc->syninfo.tap_max_delta = 80;
5909 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5910 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5911 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5912 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
5913 synaptics_sysctl, "I",
5914 "Length of segments above which a tap is ignored");
5915
5916 /* hw.psm.synaptics.tap_min_queue. */
5917 sc->syninfo.tap_min_queue = 2;
5918 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5919 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5920 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5921 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
5922 synaptics_sysctl, "I",
5923 "Number of packets required to consider a tap");
5924
5925 /* hw.psm.synaptics.taphold_timeout. */
5926 sc->gesture.in_taphold = 0;
5927 sc->syninfo.taphold_timeout = tap_timeout;
5928 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5929 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5930 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5931 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
5932 synaptics_sysctl, "I",
5933 "Maximum elapsed time between two taps to consider a tap-hold "
5934 "action");
5935
5936 /* hw.psm.synaptics.vscroll_hor_area. */
5937 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
5938 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5939 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5940 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5941 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
5942 synaptics_sysctl, "I",
5943 "Area reserved for horizontal virtual scrolling");
5944
5945 /* hw.psm.synaptics.vscroll_ver_area. */
5946 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
5947 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5948 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5949 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5950 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
5951 synaptics_sysctl, "I",
5952 "Area reserved for vertical virtual scrolling");
5953
5954 /* hw.psm.synaptics.vscroll_min_delta. */
5955 sc->syninfo.vscroll_min_delta = 50;
5956 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5957 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5958 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5959 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
5960 synaptics_sysctl, "I",
5961 "Minimum movement to consider virtual scrolling");
5962
5963 /* hw.psm.synaptics.vscroll_div_min. */
5964 sc->syninfo.vscroll_div_min = 100;
5965 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5966 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5967 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5968 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
5969 synaptics_sysctl, "I",
5970 "Divisor for fast scrolling");
5971
5972 /* hw.psm.synaptics.vscroll_div_min. */
5973 sc->syninfo.vscroll_div_max = 150;
5974 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5975 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5976 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5977 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
5978 synaptics_sysctl, "I",
5979 "Divisor for slow scrolling");
5980
5981 /* hw.psm.synaptics.touchpad_off. */
5982 sc->syninfo.touchpad_off = 0;
5983 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5984 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5985 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5986 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
5987 synaptics_sysctl, "I",
5988 "Turn off touchpad");
5989
5990 sc->syninfo.softbuttons_y = 0;
5991 sc->syninfo.softbutton2_x = 0;
5992 sc->syninfo.softbutton3_x = 0;
5993
5994 /* skip softbuttons sysctl on not clickpads */
5995 if (sc->synhw.capClickPad)
5996 synaptics_sysctl_create_softbuttons_tree(sc);
5997}
5998
5999static int
6000synaptics_preferred_mode(struct psm_softc *sc) {
6001 int mode_byte;
6002
6003 /* Check if we are in relative mode */
6004 if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
6005 if (tap_enabled == 0)
6006 /*
6007 * Disable tap & drag gestures. We use a Mode Byte
6008 * and set the DisGest bit (see ��2.5 of Synaptics
6009 * TouchPad Interfacing Guide).
6010 */
6011 return (0x04);
6012 else
6013 /*
6014 * Enable tap & drag gestures. We use a Mode Byte
6015 * and clear the DisGest bit (see ��2.5 of Synaptics
6016 * TouchPad Interfacing Guide).
6017 */
6018 return (0x00);
6019 }
6020
6021 mode_byte = 0xc4;
6022
6023 /* request wmode where available */
6024 if (sc->synhw.capExtended)
6025 mode_byte |= 1;
6026
6027 return mode_byte;
6028}
6029
6030static void
6031synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
6032 mouse_ext_command(sc->kbdc, mode_byte);
6033
6034 /* "Commit" the Set Mode Byte command sent above. */
6035 set_mouse_sampling_rate(sc->kbdc, 20);
6036
6037 /*
6038 * Enable advanced gestures mode if supported and we are not entering
6039 * passthrough or relative mode.
6040 */
6041 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
6042 sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
6043 mouse_ext_command(sc->kbdc, 3);
6044 set_mouse_sampling_rate(sc->kbdc, 0xc8);
6045 }
6046}
6047
5037 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) {
5038 VLOG(3, (LOG_DEBUG, "synaptics: "
5039 "packet rejected\n"));
5040 goto next;
5041 }
5042 break;
5043
5044 case MOUSE_MODEL_ELANTECH:
5045 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) {
5046 VLOG(3, (LOG_DEBUG, "elantech: "
5047 "packet rejected\n"));
5048 goto next;
5049 }
5050 break;
5051
5052 case MOUSE_MODEL_TRACKPOINT:
5053 case MOUSE_MODEL_GENERIC:
5054 default:
5055 break;
5056 }
5057
5058#ifdef EVDEV_SUPPORT
5059 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE &&
5060 sc->hw.model != MOUSE_MODEL_ELANTECH &&
5061 sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
5062 evdev_push_rel(sc->evdev_r, REL_X, x);
5063 evdev_push_rel(sc->evdev_r, REL_Y, -y);
5064
5065 switch (sc->hw.model) {
5066 case MOUSE_MODEL_EXPLORER:
5067 case MOUSE_MODEL_INTELLI:
5068 case MOUSE_MODEL_NET:
5069 case MOUSE_MODEL_NETSCROLL:
5070 case MOUSE_MODEL_4DPLUS:
5071 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5072 break;
5073 case MOUSE_MODEL_MOUSEMANPLUS:
5074 case MOUSE_MODEL_4D:
5075 switch (z) {
5076 case 1:
5077 case -1:
5078 evdev_push_rel(sc->evdev_r, REL_WHEEL, -z);
5079 break;
5080 case 2:
5081 case -2:
5082 evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2);
5083 break;
5084 }
5085 break;
5086 }
5087
5088 evdev_push_mouse_btn(sc->evdev_r, ms.button);
5089 evdev_sync(sc->evdev_r);
5090 }
5091#endif
5092
5093 /* scale values */
5094 if (sc->mode.accelfactor >= 1) {
5095 if (x != 0) {
5096 x = x * x / sc->mode.accelfactor;
5097 if (x == 0)
5098 x = 1;
5099 if (c & MOUSE_PS2_XNEG)
5100 x = -x;
5101 }
5102 if (y != 0) {
5103 y = y * y / sc->mode.accelfactor;
5104 if (y == 0)
5105 y = 1;
5106 if (c & MOUSE_PS2_YNEG)
5107 y = -y;
5108 }
5109 }
5110
5111 /* Store last packet for reinjection if it has not been set already */
5112 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0)
5113 sc->idlepacket = *pb;
5114
5115 ms.dx = x;
5116 ms.dy = y;
5117 ms.dz = z;
5118 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) |
5119 (ms.obutton ^ ms.button);
5120
5121 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket);
5122
5123 sc->status.flags |= ms.flags;
5124 sc->status.dx += ms.dx;
5125 sc->status.dy += ms.dy;
5126 sc->status.dz += ms.dz;
5127 sc->status.button = ms.button;
5128 sc->button = ms.button;
5129
5130next_native:
5131 sc->watchdog = FALSE;
5132
5133 /* queue data */
5134 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) {
5135 l = imin(pb->inputbytes,
5136 sizeof(sc->queue.buf) - sc->queue.tail);
5137 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
5138 if (pb->inputbytes > l)
5139 bcopy(&pb->ipacket[l], &sc->queue.buf[0],
5140 pb->inputbytes - l);
5141 sc->queue.tail = (sc->queue.tail + pb->inputbytes) %
5142 sizeof(sc->queue.buf);
5143 sc->queue.count += pb->inputbytes;
5144 }
5145
5146next:
5147 pb->inputbytes = 0;
5148 if (++sc->pqueue_start >= PSM_PACKETQUEUE)
5149 sc->pqueue_start = 0;
5150 } while (sc->pqueue_start != sc->pqueue_end);
5151
5152 if (sc->state & PSM_ASLP) {
5153 sc->state &= ~PSM_ASLP;
5154 wakeup(sc);
5155 }
5156 selwakeuppri(&sc->rsel, PZERO);
5157 if (sc->async != NULL) {
5158 pgsigio(&sc->async, SIGIO, 0);
5159 }
5160 sc->state &= ~PSM_SOFTARMED;
5161
5162 /* schedule injection of predefined packet after idletimeout
5163 * if no data packets have been received from psmintr */
5164 if (timevalisset(&sc->idletimeout)) {
5165 sc->state |= PSM_SOFTARMED;
5166 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout),
5167 psmsoftintridle, sc);
5168 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n",
5169 tvtohz(&sc->idletimeout)));
5170 }
5171 splx(s);
5172}
5173
5174static int
5175psmpoll(struct cdev *dev, int events, struct thread *td)
5176{
5177 struct psm_softc *sc = dev->si_drv1;
5178 int s;
5179 int revents = 0;
5180
5181 /* Return true if a mouse event available */
5182 s = spltty();
5183 if (events & (POLLIN | POLLRDNORM)) {
5184 if (sc->queue.count > 0)
5185 revents |= events & (POLLIN | POLLRDNORM);
5186 else
5187 selrecord(td, &sc->rsel);
5188 }
5189 splx(s);
5190
5191 return (revents);
5192}
5193
5194/* vendor/model specific routines */
5195
5196static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
5197{
5198 if (set_mouse_resolution(kbdc, res) != res)
5199 return (FALSE);
5200 if (set_mouse_scaling(kbdc, scale) &&
5201 set_mouse_scaling(kbdc, scale) &&
5202 set_mouse_scaling(kbdc, scale) &&
5203 (get_mouse_status(kbdc, status, 0, 3) >= 3))
5204 return (TRUE);
5205 return (FALSE);
5206}
5207
5208static int
5209mouse_ext_command(KBDC kbdc, int command)
5210{
5211 int c;
5212
5213 c = (command >> 6) & 0x03;
5214 if (set_mouse_resolution(kbdc, c) != c)
5215 return (FALSE);
5216 c = (command >> 4) & 0x03;
5217 if (set_mouse_resolution(kbdc, c) != c)
5218 return (FALSE);
5219 c = (command >> 2) & 0x03;
5220 if (set_mouse_resolution(kbdc, c) != c)
5221 return (FALSE);
5222 c = (command >> 0) & 0x03;
5223 if (set_mouse_resolution(kbdc, c) != c)
5224 return (FALSE);
5225 return (TRUE);
5226}
5227
5228#ifdef notyet
5229/* Logitech MouseMan Cordless II */
5230static int
5231enable_lcordless(struct psm_softc *sc, enum probearg arg)
5232{
5233 KBDC kbdc = sc->kbdc;
5234 int status[3];
5235 int ch;
5236
5237 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status))
5238 return (FALSE);
5239 if (status[1] == PSMD_RES_HIGH)
5240 return (FALSE);
5241 ch = (status[0] & 0x07) - 1; /* channel # */
5242 if ((ch <= 0) || (ch > 4))
5243 return (FALSE);
5244 /*
5245 * status[1]: always one?
5246 * status[2]: battery status? (0-100)
5247 */
5248 return (TRUE);
5249}
5250#endif /* notyet */
5251
5252/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
5253static int
5254enable_groller(struct psm_softc *sc, enum probearg arg)
5255{
5256 KBDC kbdc = sc->kbdc;
5257 int status[3];
5258
5259 /*
5260 * The special sequence to enable the fourth button and the
5261 * roller. Immediately after this sequence check status bytes.
5262 * if the mouse is NetScroll, the second and the third bytes are
5263 * '3' and 'D'.
5264 */
5265
5266 /*
5267 * If the mouse is an ordinary PS/2 mouse, the status bytes should
5268 * look like the following.
5269 *
5270 * byte 1 bit 7 always 0
5271 * bit 6 stream mode (0)
5272 * bit 5 disabled (0)
5273 * bit 4 1:1 scaling (0)
5274 * bit 3 always 0
5275 * bit 0-2 button status
5276 * byte 2 resolution (PSMD_RES_HIGH)
5277 * byte 3 report rate (?)
5278 */
5279
5280 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5281 return (FALSE);
5282 if ((status[1] != '3') || (status[2] != 'D'))
5283 return (FALSE);
5284 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
5285 if (arg == PROBE)
5286 sc->hw.buttons = 4;
5287 return (TRUE);
5288}
5289
5290/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
5291static int
5292enable_gmouse(struct psm_softc *sc, enum probearg arg)
5293{
5294 KBDC kbdc = sc->kbdc;
5295 int status[3];
5296
5297 /*
5298 * The special sequence to enable the middle, "rubber" button.
5299 * Immediately after this sequence check status bytes.
5300 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
5301 * the second and the third bytes are '3' and 'U'.
5302 * NOTE: NetMouse reports that it has three buttons although it has
5303 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
5304 * say they have three buttons too and they do have a button on the
5305 * side...
5306 */
5307 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status))
5308 return (FALSE);
5309 if ((status[1] != '3') || (status[2] != 'U'))
5310 return (FALSE);
5311 return (TRUE);
5312}
5313
5314/* ALPS GlidePoint */
5315static int
5316enable_aglide(struct psm_softc *sc, enum probearg arg)
5317{
5318 KBDC kbdc = sc->kbdc;
5319 int status[3];
5320
5321 /*
5322 * The special sequence to obtain ALPS GlidePoint specific
5323 * information. Immediately after this sequence, status bytes will
5324 * contain something interesting.
5325 * NOTE: ALPS produces several models of GlidePoint. Some of those
5326 * do not respond to this sequence, thus, cannot be detected this way.
5327 */
5328 if (set_mouse_sampling_rate(kbdc, 100) != 100)
5329 return (FALSE);
5330 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status))
5331 return (FALSE);
5332 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
5333 return (FALSE);
5334 return (TRUE);
5335}
5336
5337/* Kensington ThinkingMouse/Trackball */
5338static int
5339enable_kmouse(struct psm_softc *sc, enum probearg arg)
5340{
5341 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
5342 KBDC kbdc = sc->kbdc;
5343 int status[3];
5344 int id1;
5345 int id2;
5346 int i;
5347
5348 id1 = get_aux_id(kbdc);
5349 if (set_mouse_sampling_rate(kbdc, 10) != 10)
5350 return (FALSE);
5351 /*
5352 * The device is now in the native mode? It returns a different
5353 * ID value...
5354 */
5355 id2 = get_aux_id(kbdc);
5356 if ((id1 == id2) || (id2 != 2))
5357 return (FALSE);
5358
5359 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
5360 return (FALSE);
5361#if PSM_DEBUG >= 2
5362 /* at this point, resolution is LOW, sampling rate is 10/sec */
5363 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5364 return (FALSE);
5365#endif
5366
5367 /*
5368 * The special sequence to enable the third and fourth buttons.
5369 * Otherwise they behave like the first and second buttons.
5370 */
5371 for (i = 0; i < nitems(rate); ++i)
5372 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5373 return (FALSE);
5374
5375 /*
5376 * At this point, the device is using default resolution and
5377 * sampling rate for the native mode.
5378 */
5379 if (get_mouse_status(kbdc, status, 0, 3) < 3)
5380 return (FALSE);
5381 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
5382 return (FALSE);
5383
5384 /* the device appears be enabled by this sequence, diable it for now */
5385 disable_aux_dev(kbdc);
5386 empty_aux_buffer(kbdc, 5);
5387
5388 return (TRUE);
5389}
5390
5391/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
5392static int
5393enable_mmanplus(struct psm_softc *sc, enum probearg arg)
5394{
5395 KBDC kbdc = sc->kbdc;
5396 int data[3];
5397
5398 /* the special sequence to enable the fourth button and the roller. */
5399 /*
5400 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
5401 * must be called exactly three times since the last RESET command
5402 * before this sequence. XXX
5403 */
5404 if (!set_mouse_scaling(kbdc, 1))
5405 return (FALSE);
5406 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
5407 return (FALSE);
5408 if (get_mouse_status(kbdc, data, 1, 3) < 3)
5409 return (FALSE);
5410
5411 /*
5412 * PS2++ protocol, packet type 0
5413 *
5414 * b7 b6 b5 b4 b3 b2 b1 b0
5415 * byte 1: * 1 p3 p2 1 * * *
5416 * byte 2: 1 1 p1 p0 m1 m0 1 0
5417 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
5418 *
5419 * p3-p0: packet type: 0
5420 * m7-m0: model ID: MouseMan+:0x50,
5421 * FirstMouse+:0x51,
5422 * ScrollPoint:0x58...
5423 */
5424 /* check constant bits */
5425 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
5426 return (FALSE);
5427 if ((data[1] & 0xc3) != 0xc2)
5428 return (FALSE);
5429 /* check d3-d0 in byte 2 */
5430 if (!MOUSE_PS2PLUS_CHECKBITS(data))
5431 return (FALSE);
5432 /* check p3-p0 */
5433 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
5434 return (FALSE);
5435
5436 if (arg == PROBE) {
5437 sc->hw.hwid &= 0x00ff;
5438 sc->hw.hwid |= data[2] << 8; /* save model ID */
5439 }
5440
5441 /*
5442 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
5443 * the wheel and the fourth button events are encoded in the
5444 * special data packet. The mouse may be put in the IntelliMouse mode
5445 * if it is initialized by the IntelliMouse's method.
5446 */
5447 return (TRUE);
5448}
5449
5450/* MS IntelliMouse Explorer */
5451static int
5452enable_msexplorer(struct psm_softc *sc, enum probearg arg)
5453{
5454 KBDC kbdc = sc->kbdc;
5455 static u_char rate0[] = { 200, 100, 80, };
5456 static u_char rate1[] = { 200, 200, 80, };
5457 int id;
5458 int i;
5459
5460 /*
5461 * This is needed for at least A4Tech X-7xx mice - they do not go
5462 * straight to Explorer mode, but need to be set to Intelli mode
5463 * first.
5464 */
5465 enable_msintelli(sc, arg);
5466
5467 /* the special sequence to enable the extra buttons and the roller. */
5468 for (i = 0; i < nitems(rate1); ++i)
5469 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
5470 return (FALSE);
5471 /* the device will give the genuine ID only after the above sequence */
5472 id = get_aux_id(kbdc);
5473 if (id != PSM_EXPLORER_ID)
5474 return (FALSE);
5475
5476 if (arg == PROBE) {
5477 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
5478 sc->hw.hwid = id;
5479 }
5480
5481 /*
5482 * XXX: this is a kludge to fool some KVM switch products
5483 * which think they are clever enough to know the 4-byte IntelliMouse
5484 * protocol, and assume any other protocols use 3-byte packets.
5485 * They don't convey 4-byte data packets from the IntelliMouse Explorer
5486 * correctly to the host computer because of this!
5487 * The following sequence is actually IntelliMouse's "wake up"
5488 * sequence; it will make the KVM think the mouse is IntelliMouse
5489 * when it is in fact IntelliMouse Explorer.
5490 */
5491 for (i = 0; i < nitems(rate0); ++i)
5492 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
5493 break;
5494 get_aux_id(kbdc);
5495
5496 return (TRUE);
5497}
5498
5499/*
5500 * MS IntelliMouse
5501 * Logitech MouseMan+ and FirstMouse+ will also respond to this
5502 * probe routine and act like IntelliMouse.
5503 */
5504static int
5505enable_msintelli(struct psm_softc *sc, enum probearg arg)
5506{
5507 KBDC kbdc = sc->kbdc;
5508 static u_char rate[] = { 200, 100, 80, };
5509 int id;
5510 int i;
5511
5512 /* the special sequence to enable the third button and the roller. */
5513 for (i = 0; i < nitems(rate); ++i)
5514 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5515 return (FALSE);
5516 /* the device will give the genuine ID only after the above sequence */
5517 id = get_aux_id(kbdc);
5518 if (id != PSM_INTELLI_ID)
5519 return (FALSE);
5520
5521 if (arg == PROBE) {
5522 sc->hw.buttons = 3;
5523 sc->hw.hwid = id;
5524 }
5525
5526 return (TRUE);
5527}
5528
5529/*
5530 * A4 Tech 4D Mouse
5531 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
5532 */
5533static int
5534enable_4dmouse(struct psm_softc *sc, enum probearg arg)
5535{
5536 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5537 KBDC kbdc = sc->kbdc;
5538 int id;
5539 int i;
5540
5541 for (i = 0; i < nitems(rate); ++i)
5542 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5543 return (FALSE);
5544 id = get_aux_id(kbdc);
5545 /*
5546 * WinEasy 4D, 4 Way Scroll 4D: 6
5547 * Cable-Free 4D: 8 (4DPLUS)
5548 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
5549 */
5550 if (id != PSM_4DMOUSE_ID)
5551 return (FALSE);
5552
5553 if (arg == PROBE) {
5554 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
5555 sc->hw.hwid = id;
5556 }
5557
5558 return (TRUE);
5559}
5560
5561/*
5562 * A4 Tech 4D+ Mouse
5563 * Newer wheel mice from A4 Tech seem to use this protocol.
5564 * Older models are recognized as either 4D Mouse or IntelliMouse.
5565 */
5566static int
5567enable_4dplus(struct psm_softc *sc, enum probearg arg)
5568{
5569 KBDC kbdc = sc->kbdc;
5570 int id;
5571
5572 /*
5573 * enable_4dmouse() already issued the following ID sequence...
5574 static u_char rate[] = { 200, 100, 80, 60, 40, 20 };
5575 int i;
5576
5577 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i)
5578 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
5579 return (FALSE);
5580 */
5581
5582 id = get_aux_id(kbdc);
5583 switch (id) {
5584 case PSM_4DPLUS_ID:
5585 break;
5586 case PSM_4DPLUS_RFSW35_ID:
5587 break;
5588 default:
5589 return (FALSE);
5590 }
5591
5592 if (arg == PROBE) {
5593 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3;
5594 sc->hw.hwid = id;
5595 }
5596
5597 return (TRUE);
5598}
5599
5600/* Synaptics Touchpad */
5601static int
5602synaptics_sysctl(SYSCTL_HANDLER_ARGS)
5603{
5604 struct psm_softc *sc;
5605 int error, arg;
5606
5607 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 ||
5608 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X)
5609 return (EINVAL);
5610
5611 sc = oidp->oid_arg1;
5612
5613 /* Read the current value. */
5614 arg = *(int *)((char *)sc + oidp->oid_arg2);
5615 error = sysctl_handle_int(oidp, &arg, 0, req);
5616
5617 /* Sanity check. */
5618 if (error || !req->newptr)
5619 return (error);
5620
5621 /*
5622 * Check that the new value is in the concerned node's range
5623 * of values.
5624 */
5625 switch (oidp->oid_arg2) {
5626 case SYNAPTICS_SYSCTL_MIN_PRESSURE:
5627 case SYNAPTICS_SYSCTL_MAX_PRESSURE:
5628 if (arg < 0 || arg > 255)
5629 return (EINVAL);
5630 break;
5631 case SYNAPTICS_SYSCTL_MAX_WIDTH:
5632 if (arg < 4 || arg > 15)
5633 return (EINVAL);
5634 break;
5635 case SYNAPTICS_SYSCTL_MARGIN_TOP:
5636 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM:
5637 case SYNAPTICS_SYSCTL_NA_TOP:
5638 case SYNAPTICS_SYSCTL_NA_BOTTOM:
5639 if (arg < 0 || arg > sc->synhw.maximumYCoord)
5640 return (EINVAL);
5641 break;
5642 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X:
5643 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X:
5644 /* Softbuttons is clickpad only feature */
5645 if (!sc->synhw.capClickPad && arg != 0)
5646 return (EINVAL);
5647 /* FALLTHROUGH */
5648 case SYNAPTICS_SYSCTL_MARGIN_RIGHT:
5649 case SYNAPTICS_SYSCTL_MARGIN_LEFT:
5650 case SYNAPTICS_SYSCTL_NA_RIGHT:
5651 case SYNAPTICS_SYSCTL_NA_LEFT:
5652 if (arg < 0 || arg > sc->synhw.maximumXCoord)
5653 return (EINVAL);
5654 break;
5655 case SYNAPTICS_SYSCTL_WINDOW_MIN:
5656 case SYNAPTICS_SYSCTL_WINDOW_MAX:
5657 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE:
5658 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE)
5659 return (EINVAL);
5660 break;
5661 case SYNAPTICS_SYSCTL_MULTIPLICATOR:
5662 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT:
5663 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS:
5664 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA:
5665 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED:
5666 case SYNAPTICS_SYSCTL_DIV_MIN:
5667 case SYNAPTICS_SYSCTL_DIV_MAX:
5668 case SYNAPTICS_SYSCTL_DIV_MAX_NA:
5669 case SYNAPTICS_SYSCTL_DIV_LEN:
5670 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN:
5671 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX:
5672 if (arg < 1)
5673 return (EINVAL);
5674 break;
5675 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA:
5676 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT:
5677 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA:
5678 if (arg < 0)
5679 return (EINVAL);
5680 break;
5681 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA:
5682 if (arg < -sc->synhw.maximumXCoord ||
5683 arg > sc->synhw.maximumXCoord)
5684 return (EINVAL);
5685 break;
5686 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y:
5687 /* Softbuttons is clickpad only feature */
5688 if (!sc->synhw.capClickPad && arg != 0)
5689 return (EINVAL);
5690 /* FALLTHROUGH */
5691 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA:
5692 if (arg < -sc->synhw.maximumYCoord ||
5693 arg > sc->synhw.maximumYCoord)
5694 return (EINVAL);
5695 break;
5696 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF:
5697 if (arg < 0 || arg > 1)
5698 return (EINVAL);
5699 break;
5700 default:
5701 return (EINVAL);
5702 }
5703
5704 /* Update. */
5705 *(int *)((char *)sc + oidp->oid_arg2) = arg;
5706
5707 return (error);
5708}
5709
5710static void
5711synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc)
5712{
5713 /*
5714 * Set predefined sizes for softbuttons.
5715 * Values are taken to match HP Pavilion dv6 clickpad drawings
5716 * with thin middle softbutton placed on separator
5717 */
5718
5719 /* hw.psm.synaptics.softbuttons_y */
5720 sc->syninfo.softbuttons_y = 1700;
5721 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5722 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5723 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5724 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y,
5725 synaptics_sysctl, "I",
5726 "Vertical size of softbuttons area");
5727
5728 /* hw.psm.synaptics.softbutton2_x */
5729 sc->syninfo.softbutton2_x = 3100;
5730 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5731 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5732 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5733 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X,
5734 synaptics_sysctl, "I",
5735 "Horisontal position of 2-nd softbutton left edge (0-disable)");
5736
5737 /* hw.psm.synaptics.softbutton3_x */
5738 sc->syninfo.softbutton3_x = 3900;
5739 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5740 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5741 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5742 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X,
5743 synaptics_sysctl, "I",
5744 "Horisontal position of 3-rd softbutton left edge (0-disable)");
5745}
5746
5747static void
5748synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name,
5749 const char *descr)
5750{
5751
5752 if (sc->syninfo.sysctl_tree != NULL)
5753 return;
5754
5755 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */
5756 sysctl_ctx_init(&sc->syninfo.sysctl_ctx);
5757 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx,
5758 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD,
5759 0, descr);
5760
5761 /* hw.psm.synaptics.directional_scrolls. */
5762 sc->syninfo.directional_scrolls = 0;
5763 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5764 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5765 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY,
5766 &sc->syninfo.directional_scrolls, 0,
5767 "Enable hardware scrolling pad (if non-zero) or register it as "
5768 "extended buttons (if 0)");
5769
5770 /* hw.psm.synaptics.max_x. */
5771 sc->syninfo.max_x = 6143;
5772 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5773 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5774 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY,
5775 &sc->syninfo.max_x, 0,
5776 "Horizontal reporting range");
5777
5778 /* hw.psm.synaptics.max_y. */
5779 sc->syninfo.max_y = 6143;
5780 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5781 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5782 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY,
5783 &sc->syninfo.max_y, 0,
5784 "Vertical reporting range");
5785
5786 /*
5787 * Turn off two finger scroll if we have a
5788 * physical area reserved for scrolling or when
5789 * there's no multi finger support.
5790 */
5791 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 &&
5792 sc->synhw.capAdvancedGestures == 0))
5793 sc->syninfo.two_finger_scroll = 0;
5794 else
5795 sc->syninfo.two_finger_scroll = 1;
5796 /* hw.psm.synaptics.two_finger_scroll. */
5797 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx,
5798 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5799 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY,
5800 &sc->syninfo.two_finger_scroll, 0,
5801 "Enable two finger scrolling");
5802
5803 /* hw.psm.synaptics.min_pressure. */
5804 sc->syninfo.min_pressure = 32;
5805 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5806 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5807 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5808 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE,
5809 synaptics_sysctl, "I",
5810 "Minimum pressure required to start an action");
5811
5812 /* hw.psm.synaptics.max_pressure. */
5813 sc->syninfo.max_pressure = 220;
5814 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5815 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5816 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5817 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE,
5818 synaptics_sysctl, "I",
5819 "Maximum pressure to detect palm");
5820
5821 /* hw.psm.synaptics.max_width. */
5822 sc->syninfo.max_width = 10;
5823 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5824 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5825 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5826 sc, SYNAPTICS_SYSCTL_MAX_WIDTH,
5827 synaptics_sysctl, "I",
5828 "Maximum finger width to detect palm");
5829
5830 /* hw.psm.synaptics.top_margin. */
5831 sc->syninfo.margin_top = 200;
5832 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5833 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5834 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5835 sc, SYNAPTICS_SYSCTL_MARGIN_TOP,
5836 synaptics_sysctl, "I",
5837 "Top margin");
5838
5839 /* hw.psm.synaptics.right_margin. */
5840 sc->syninfo.margin_right = 200;
5841 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5842 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5843 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5844 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT,
5845 synaptics_sysctl, "I",
5846 "Right margin");
5847
5848 /* hw.psm.synaptics.bottom_margin. */
5849 sc->syninfo.margin_bottom = 200;
5850 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5851 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5852 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5853 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM,
5854 synaptics_sysctl, "I",
5855 "Bottom margin");
5856
5857 /* hw.psm.synaptics.left_margin. */
5858 sc->syninfo.margin_left = 200;
5859 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5860 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5861 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5862 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT,
5863 synaptics_sysctl, "I",
5864 "Left margin");
5865
5866 /* hw.psm.synaptics.na_top. */
5867 sc->syninfo.na_top = 1783;
5868 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5869 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5870 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5871 sc, SYNAPTICS_SYSCTL_NA_TOP,
5872 synaptics_sysctl, "I",
5873 "Top noisy area, where weight_previous_na is used instead "
5874 "of weight_previous");
5875
5876 /* hw.psm.synaptics.na_right. */
5877 sc->syninfo.na_right = 563;
5878 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5879 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5880 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5881 sc, SYNAPTICS_SYSCTL_NA_RIGHT,
5882 synaptics_sysctl, "I",
5883 "Right noisy area, where weight_previous_na is used instead "
5884 "of weight_previous");
5885
5886 /* hw.psm.synaptics.na_bottom. */
5887 sc->syninfo.na_bottom = 1408;
5888 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5889 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5890 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5891 sc, SYNAPTICS_SYSCTL_NA_BOTTOM,
5892 synaptics_sysctl, "I",
5893 "Bottom noisy area, where weight_previous_na is used instead "
5894 "of weight_previous");
5895
5896 /* hw.psm.synaptics.na_left. */
5897 sc->syninfo.na_left = 1600;
5898 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5899 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5900 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5901 sc, SYNAPTICS_SYSCTL_NA_LEFT,
5902 synaptics_sysctl, "I",
5903 "Left noisy area, where weight_previous_na is used instead "
5904 "of weight_previous");
5905
5906 /* hw.psm.synaptics.window_min. */
5907 sc->syninfo.window_min = 4;
5908 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5909 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5910 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5911 sc, SYNAPTICS_SYSCTL_WINDOW_MIN,
5912 synaptics_sysctl, "I",
5913 "Minimum window size to start an action");
5914
5915 /* hw.psm.synaptics.window_max. */
5916 sc->syninfo.window_max = 10;
5917 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5918 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5919 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5920 sc, SYNAPTICS_SYSCTL_WINDOW_MAX,
5921 synaptics_sysctl, "I",
5922 "Maximum window size");
5923
5924 /* hw.psm.synaptics.multiplicator. */
5925 sc->syninfo.multiplicator = 10000;
5926 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5927 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5928 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5929 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR,
5930 synaptics_sysctl, "I",
5931 "Multiplicator to increase precision in averages and divisions");
5932
5933 /* hw.psm.synaptics.weight_current. */
5934 sc->syninfo.weight_current = 3;
5935 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5936 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5937 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5938 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT,
5939 synaptics_sysctl, "I",
5940 "Weight of the current movement in the new average");
5941
5942 /* hw.psm.synaptics.weight_previous. */
5943 sc->syninfo.weight_previous = 6;
5944 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5945 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5946 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5947 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS,
5948 synaptics_sysctl, "I",
5949 "Weight of the previous average");
5950
5951 /* hw.psm.synaptics.weight_previous_na. */
5952 sc->syninfo.weight_previous_na = 20;
5953 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5954 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5955 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5956 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA,
5957 synaptics_sysctl, "I",
5958 "Weight of the previous average (inside the noisy area)");
5959
5960 /* hw.psm.synaptics.weight_len_squared. */
5961 sc->syninfo.weight_len_squared = 2000;
5962 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5963 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5964 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5965 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED,
5966 synaptics_sysctl, "I",
5967 "Length (squared) of segments where weight_previous "
5968 "starts to decrease");
5969
5970 /* hw.psm.synaptics.div_min. */
5971 sc->syninfo.div_min = 9;
5972 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5973 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5974 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5975 sc, SYNAPTICS_SYSCTL_DIV_MIN,
5976 synaptics_sysctl, "I",
5977 "Divisor for fast movements");
5978
5979 /* hw.psm.synaptics.div_max. */
5980 sc->syninfo.div_max = 17;
5981 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5982 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5983 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5984 sc, SYNAPTICS_SYSCTL_DIV_MAX,
5985 synaptics_sysctl, "I",
5986 "Divisor for slow movements");
5987
5988 /* hw.psm.synaptics.div_max_na. */
5989 sc->syninfo.div_max_na = 30;
5990 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
5991 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
5992 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
5993 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA,
5994 synaptics_sysctl, "I",
5995 "Divisor with slow movements (inside the noisy area)");
5996
5997 /* hw.psm.synaptics.div_len. */
5998 sc->syninfo.div_len = 100;
5999 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6000 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6001 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6002 sc, SYNAPTICS_SYSCTL_DIV_LEN,
6003 synaptics_sysctl, "I",
6004 "Length of segments where div_max starts to decrease");
6005
6006 /* hw.psm.synaptics.tap_max_delta. */
6007 sc->syninfo.tap_max_delta = 80;
6008 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6009 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6010 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6011 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA,
6012 synaptics_sysctl, "I",
6013 "Length of segments above which a tap is ignored");
6014
6015 /* hw.psm.synaptics.tap_min_queue. */
6016 sc->syninfo.tap_min_queue = 2;
6017 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6018 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6019 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6020 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE,
6021 synaptics_sysctl, "I",
6022 "Number of packets required to consider a tap");
6023
6024 /* hw.psm.synaptics.taphold_timeout. */
6025 sc->gesture.in_taphold = 0;
6026 sc->syninfo.taphold_timeout = tap_timeout;
6027 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6028 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6029 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6030 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT,
6031 synaptics_sysctl, "I",
6032 "Maximum elapsed time between two taps to consider a tap-hold "
6033 "action");
6034
6035 /* hw.psm.synaptics.vscroll_hor_area. */
6036 sc->syninfo.vscroll_hor_area = 0; /* 1300 */
6037 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6038 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6039 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6040 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA,
6041 synaptics_sysctl, "I",
6042 "Area reserved for horizontal virtual scrolling");
6043
6044 /* hw.psm.synaptics.vscroll_ver_area. */
6045 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right;
6046 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6047 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6048 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6049 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA,
6050 synaptics_sysctl, "I",
6051 "Area reserved for vertical virtual scrolling");
6052
6053 /* hw.psm.synaptics.vscroll_min_delta. */
6054 sc->syninfo.vscroll_min_delta = 50;
6055 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6056 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6057 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6058 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA,
6059 synaptics_sysctl, "I",
6060 "Minimum movement to consider virtual scrolling");
6061
6062 /* hw.psm.synaptics.vscroll_div_min. */
6063 sc->syninfo.vscroll_div_min = 100;
6064 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6065 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6066 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6067 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN,
6068 synaptics_sysctl, "I",
6069 "Divisor for fast scrolling");
6070
6071 /* hw.psm.synaptics.vscroll_div_min. */
6072 sc->syninfo.vscroll_div_max = 150;
6073 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6074 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6075 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6076 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX,
6077 synaptics_sysctl, "I",
6078 "Divisor for slow scrolling");
6079
6080 /* hw.psm.synaptics.touchpad_off. */
6081 sc->syninfo.touchpad_off = 0;
6082 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx,
6083 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO,
6084 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6085 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF,
6086 synaptics_sysctl, "I",
6087 "Turn off touchpad");
6088
6089 sc->syninfo.softbuttons_y = 0;
6090 sc->syninfo.softbutton2_x = 0;
6091 sc->syninfo.softbutton3_x = 0;
6092
6093 /* skip softbuttons sysctl on not clickpads */
6094 if (sc->synhw.capClickPad)
6095 synaptics_sysctl_create_softbuttons_tree(sc);
6096}
6097
6098static int
6099synaptics_preferred_mode(struct psm_softc *sc) {
6100 int mode_byte;
6101
6102 /* Check if we are in relative mode */
6103 if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) {
6104 if (tap_enabled == 0)
6105 /*
6106 * Disable tap & drag gestures. We use a Mode Byte
6107 * and set the DisGest bit (see ��2.5 of Synaptics
6108 * TouchPad Interfacing Guide).
6109 */
6110 return (0x04);
6111 else
6112 /*
6113 * Enable tap & drag gestures. We use a Mode Byte
6114 * and clear the DisGest bit (see ��2.5 of Synaptics
6115 * TouchPad Interfacing Guide).
6116 */
6117 return (0x00);
6118 }
6119
6120 mode_byte = 0xc4;
6121
6122 /* request wmode where available */
6123 if (sc->synhw.capExtended)
6124 mode_byte |= 1;
6125
6126 return mode_byte;
6127}
6128
6129static void
6130synaptics_set_mode(struct psm_softc *sc, int mode_byte) {
6131 mouse_ext_command(sc->kbdc, mode_byte);
6132
6133 /* "Commit" the Set Mode Byte command sent above. */
6134 set_mouse_sampling_rate(sc->kbdc, 20);
6135
6136 /*
6137 * Enable advanced gestures mode if supported and we are not entering
6138 * passthrough or relative mode.
6139 */
6140 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) &&
6141 sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) {
6142 mouse_ext_command(sc->kbdc, 3);
6143 set_mouse_sampling_rate(sc->kbdc, 0xc8);
6144 }
6145}
6146
6147/*
6148 * AUX MUX detection code should be placed at very beginning of probe sequence
6149 * at least before 4-byte protocol mouse probes e.g. MS IntelliMouse probe as
6150 * latter can trigger switching the MUX to incompatible state.
6151 */
6048static int
6152static int
6153enable_synaptics_mux(struct psm_softc *sc, enum probearg arg)
6154{
6155 KBDC kbdc = sc->kbdc;
6156 int port, version;
6157 int probe = FALSE;
6158 int active_ports_count = 0;
6159 int active_ports_mask = 0;
6160
6161 version = enable_aux_mux(kbdc);
6162 if (version == -1)
6163 return (FALSE);
6164
6165 for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6166 VLOG(3, (LOG_DEBUG, "aux_mux: ping port %d\n", port));
6167 set_active_aux_mux_port(kbdc, port);
6168 if (enable_aux_dev(kbdc) && disable_aux_dev(kbdc)) {
6169 active_ports_count++;
6170 active_ports_mask |= 1 << port;
6171 }
6172 }
6173
6174 if (verbose >= 2)
6175 printf("Active Multiplexing PS/2 controller v%d.%d with %d "
6176 "active port(s)\n", version >> 4 & 0x0f, version & 0x0f,
6177 active_ports_count);
6178
6179 /* psm has a special support for GenMouse + SynTouchpad combination */
6180 if (active_ports_count >= 2) {
6181 for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) {
6182 if ((active_ports_mask & 1 << port) == 0)
6183 continue;
6184 VLOG(3, (LOG_DEBUG, "aux_mux: probe port %d\n", port));
6185 set_active_aux_mux_port(kbdc, port);
6186 probe = enable_synaptics(sc, arg);
6187 if (probe) {
6188 if (arg == PROBE)
6189 sc->muxport = port;
6190 break;
6191 }
6192 }
6193 }
6194
6195 /* IRQ handler does not support active multiplexing mode */
6196 disable_aux_mux(kbdc);
6197
6198 return (probe);
6199}
6200
6201static int
6049enable_synaptics(struct psm_softc *sc, enum probearg arg)
6050{
6051 device_t psmcpnp;
6052 struct psmcpnp_softc *psmcpnp_sc;
6053 KBDC kbdc = sc->kbdc;
6054 synapticshw_t synhw;
6055 int status[3];
6202enable_synaptics(struct psm_softc *sc, enum probearg arg)
6203{
6204 device_t psmcpnp;
6205 struct psmcpnp_softc *psmcpnp_sc;
6206 KBDC kbdc = sc->kbdc;
6207 synapticshw_t synhw;
6208 int status[3];
6056 int buttons, middle_byte;
6209 int buttons;
6057
6058 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
6059
6060 /*
6061 * Just to be on the safe side: this avoids troubles with
6062 * following mouse_ext_command() when the previous command
6063 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
6064 * Synaptics Touchpad behaviour.
6065 */
6066 set_mouse_scaling(kbdc, 1);
6067
6068 /* Identify the Touchpad version. */
6069 if (mouse_ext_command(kbdc, 0) == 0)
6070 return (FALSE);
6071 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6072 return (FALSE);
6210
6211 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n"));
6212
6213 /*
6214 * Just to be on the safe side: this avoids troubles with
6215 * following mouse_ext_command() when the previous command
6216 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on
6217 * Synaptics Touchpad behaviour.
6218 */
6219 set_mouse_scaling(kbdc, 1);
6220
6221 /* Identify the Touchpad version. */
6222 if (mouse_ext_command(kbdc, 0) == 0)
6223 return (FALSE);
6224 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6225 return (FALSE);
6073 middle_byte = status[1];
6074 if (middle_byte != 0x46 && middle_byte != 0x47)
6226 if (status[1] != 0x47)
6075 return (FALSE);
6076
6077 bzero(&synhw, sizeof(synhw));
6078 synhw.infoMinor = status[0];
6079 synhw.infoMajor = status[2] & 0x0f;
6080
6081 if (verbose >= 2)
6082 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
6083 synhw.infoMinor);
6084
6227 return (FALSE);
6228
6229 bzero(&synhw, sizeof(synhw));
6230 synhw.infoMinor = status[0];
6231 synhw.infoMajor = status[2] & 0x0f;
6232
6233 if (verbose >= 2)
6234 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor,
6235 synhw.infoMinor);
6236
6085 /*
6086 * Most synaptics touchpads return 0x47 in middle byte in responce to
6087 * identify command as stated in p.4.4 of "Synaptics PS/2 TouchPad
6088 * Interfacing Guide" and we only support v4.0 or better. But some
6089 * devices return 0x46 here and have a different numbering scheme.
6090 * In the case of 0x46, we allow versions as low as v2.0
6091 */
6092 if ((middle_byte == 0x47 && synhw.infoMajor < 4) ||
6093 (middle_byte == 0x46 && synhw.infoMajor < 2)) {
6237 if (synhw.infoMajor < 4) {
6094 printf(" Unsupported (pre-v4) Touchpad detected\n");
6095 return (FALSE);
6096 }
6097
6098 /* Get the Touchpad model information. */
6099 if (mouse_ext_command(kbdc, 3) == 0)
6100 return (FALSE);
6101 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6102 return (FALSE);
6103 if ((status[1] & 0x01) != 0) {
6104 printf(" Failed to read model information\n");
6105 return (FALSE);
6106 }
6107
6108 synhw.infoRot180 = (status[0] & 0x80) != 0;
6109 synhw.infoPortrait = (status[0] & 0x40) != 0;
6110 synhw.infoSensor = status[0] & 0x3f;
6111 synhw.infoHardware = (status[1] & 0xfe) >> 1;
6112 synhw.infoNewAbs = (status[2] & 0x80) != 0;
6113 synhw.capPen = (status[2] & 0x40) != 0;
6114 synhw.infoSimplC = (status[2] & 0x20) != 0;
6115 synhw.infoGeometry = status[2] & 0x0f;
6116
6117 if (verbose >= 2) {
6118 printf(" Model information:\n");
6119 printf(" infoRot180: %d\n", synhw.infoRot180);
6120 printf(" infoPortrait: %d\n", synhw.infoPortrait);
6121 printf(" infoSensor: %d\n", synhw.infoSensor);
6122 printf(" infoHardware: %d\n", synhw.infoHardware);
6123 printf(" infoNewAbs: %d\n", synhw.infoNewAbs);
6124 printf(" capPen: %d\n", synhw.capPen);
6125 printf(" infoSimplC: %d\n", synhw.infoSimplC);
6126 printf(" infoGeometry: %d\n", synhw.infoGeometry);
6127 }
6128
6129 /* Read the extended capability bits. */
6130 if (mouse_ext_command(kbdc, 2) == 0)
6131 return (FALSE);
6132 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6133 return (FALSE);
6238 printf(" Unsupported (pre-v4) Touchpad detected\n");
6239 return (FALSE);
6240 }
6241
6242 /* Get the Touchpad model information. */
6243 if (mouse_ext_command(kbdc, 3) == 0)
6244 return (FALSE);
6245 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6246 return (FALSE);
6247 if ((status[1] & 0x01) != 0) {
6248 printf(" Failed to read model information\n");
6249 return (FALSE);
6250 }
6251
6252 synhw.infoRot180 = (status[0] & 0x80) != 0;
6253 synhw.infoPortrait = (status[0] & 0x40) != 0;
6254 synhw.infoSensor = status[0] & 0x3f;
6255 synhw.infoHardware = (status[1] & 0xfe) >> 1;
6256 synhw.infoNewAbs = (status[2] & 0x80) != 0;
6257 synhw.capPen = (status[2] & 0x40) != 0;
6258 synhw.infoSimplC = (status[2] & 0x20) != 0;
6259 synhw.infoGeometry = status[2] & 0x0f;
6260
6261 if (verbose >= 2) {
6262 printf(" Model information:\n");
6263 printf(" infoRot180: %d\n", synhw.infoRot180);
6264 printf(" infoPortrait: %d\n", synhw.infoPortrait);
6265 printf(" infoSensor: %d\n", synhw.infoSensor);
6266 printf(" infoHardware: %d\n", synhw.infoHardware);
6267 printf(" infoNewAbs: %d\n", synhw.infoNewAbs);
6268 printf(" capPen: %d\n", synhw.capPen);
6269 printf(" infoSimplC: %d\n", synhw.infoSimplC);
6270 printf(" infoGeometry: %d\n", synhw.infoGeometry);
6271 }
6272
6273 /* Read the extended capability bits. */
6274 if (mouse_ext_command(kbdc, 2) == 0)
6275 return (FALSE);
6276 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6277 return (FALSE);
6134 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != middle_byte) {
6278 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6135 printf(" Failed to read extended capability bits\n");
6136 return (FALSE);
6137 }
6138
6139 psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME),
6140 sc->unit);
6141 psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL;
6142
6279 printf(" Failed to read extended capability bits\n");
6280 return (FALSE);
6281 }
6282
6283 psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME),
6284 sc->unit);
6285 psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL;
6286
6143 /*
6144 * Set conservative defaults for 0x46 middle byte touchpads
6145 * as ExtendedQueries return bogus data.
6146 */
6147 if (middle_byte == 0x46) {
6148 synhw.capExtended = 1;
6149 synhw.capPalmDetect = 1;
6150 synhw.capPassthrough = 1;
6151 synhw.capMultiFinger = 1;
6152 synhw.maximumXCoord = SYNAPTICS_DEFAULT_MAX_X;
6153 synhw.maximumYCoord = SYNAPTICS_DEFAULT_MAX_Y;
6154 synhw.minimumXCoord = SYNAPTICS_DEFAULT_MIN_X;
6155 synhw.minimumYCoord = SYNAPTICS_DEFAULT_MIN_Y;
6156 /* Enable multitouch mode for HW v8.1 devices */
6157 if (psmcpnp_sc != NULL &&
6158 psmcpnp_sc->type == PSMCPNP_HPSYN81)
6159 synhw.capReportsV = 1;
6160 } else
6161 synhw.capExtended = (status[0] & 0x80) != 0;
6162
6163 /* Set the different capabilities when they exist. */
6164 buttons = 0;
6287 /* Set the different capabilities when they exist. */
6288 buttons = 0;
6165 if (synhw.capExtended && middle_byte == 0x47) {
6289 synhw.capExtended = (status[0] & 0x80) != 0;
6290 if (synhw.capExtended) {
6166 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
6167 synhw.capMiddle = (status[0] & 0x04) != 0;
6168 synhw.capPassthrough = (status[2] & 0x80) != 0;
6169 synhw.capLowPower = (status[2] & 0x40) != 0;
6170 synhw.capMultiFingerReport =
6171 (status[2] & 0x20) != 0;
6172 synhw.capSleep = (status[2] & 0x10) != 0;
6173 synhw.capFourButtons = (status[2] & 0x08) != 0;
6174 synhw.capBallistics = (status[2] & 0x04) != 0;
6175 synhw.capMultiFinger = (status[2] & 0x02) != 0;
6176 synhw.capPalmDetect = (status[2] & 0x01) != 0;
6177
6178 if (!set_mouse_scaling(kbdc, 1))
6179 return (FALSE);
6180 if (mouse_ext_command(kbdc, 0x08) == 0)
6181 return (FALSE);
6182 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6183 return (FALSE);
6184
6185 if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) {
6186 synhw.infoXupmm = status[0];
6187 synhw.infoYupmm = status[2];
6188 }
6189
6190 if (verbose >= 2) {
6191 printf(" Extended capabilities:\n");
6192 printf(" capExtended: %d\n", synhw.capExtended);
6193 printf(" capMiddle: %d\n", synhw.capMiddle);
6194 printf(" nExtendedQueries: %d\n",
6195 synhw.nExtendedQueries);
6196 printf(" capPassthrough: %d\n", synhw.capPassthrough);
6197 printf(" capLowPower: %d\n", synhw.capLowPower);
6198 printf(" capMultiFingerReport: %d\n",
6199 synhw.capMultiFingerReport);
6200 printf(" capSleep: %d\n", synhw.capSleep);
6201 printf(" capFourButtons: %d\n", synhw.capFourButtons);
6202 printf(" capBallistics: %d\n", synhw.capBallistics);
6203 printf(" capMultiFinger: %d\n", synhw.capMultiFinger);
6204 printf(" capPalmDetect: %d\n", synhw.capPalmDetect);
6205 printf(" infoXupmm: %d\n", synhw.infoXupmm);
6206 printf(" infoYupmm: %d\n", synhw.infoYupmm);
6207 }
6208
6209 /*
6210 * If nExtendedQueries is 1 or greater, then the TouchPad
6211 * supports this number of extended queries. We can load
6212 * more information about buttons using query 0x09.
6213 */
6214 if (synhw.nExtendedQueries >= 1) {
6215 if (!set_mouse_scaling(kbdc, 1))
6216 return (FALSE);
6217 if (mouse_ext_command(kbdc, 0x09) == 0)
6218 return (FALSE);
6219 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6220 return (FALSE);
6221 synhw.verticalScroll = (status[0] & 0x01) != 0;
6222 synhw.horizontalScroll = (status[0] & 0x02) != 0;
6223 synhw.verticalWheel = (status[0] & 0x08) != 0;
6224 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
6225 synhw.capEWmode = (status[0] & 0x04) != 0;
6226 if (verbose >= 2) {
6227 printf(" Extended model ID:\n");
6228 printf(" verticalScroll: %d\n",
6229 synhw.verticalScroll);
6230 printf(" horizontalScroll: %d\n",
6231 synhw.horizontalScroll);
6232 printf(" verticalWheel: %d\n",
6233 synhw.verticalWheel);
6234 printf(" nExtendedButtons: %d\n",
6235 synhw.nExtendedButtons);
6236 printf(" capEWmode: %d\n",
6237 synhw.capEWmode);
6238 }
6239 /*
6240 * Add the number of extended buttons to the total
6241 * button support count, including the middle button
6242 * if capMiddle support bit is set.
6243 */
6244 buttons = synhw.nExtendedButtons + synhw.capMiddle;
6245 } else
6246 /*
6247 * If the capFourButtons support bit is set,
6248 * add a fourth button to the total button count.
6249 */
6250 buttons = synhw.capFourButtons ? 1 : 0;
6251
6252 /* Read the continued capabilities bits. */
6253 if (synhw.nExtendedQueries >= 4) {
6254 if (!set_mouse_scaling(kbdc, 1))
6255 return (FALSE);
6256 if (mouse_ext_command(kbdc, 0x0c) == 0)
6257 return (FALSE);
6258 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6259 return (FALSE);
6260
6261 synhw.capClickPad = (status[1] & 0x01) << 1;
6262 synhw.capClickPad |= (status[0] & 0x10) != 0;
6263 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
6264 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
6265 synhw.capReportsV = (status[1] & 0x08) != 0;
6266 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
6267 synhw.capReportsMin = (status[1] & 0x20) != 0;
6268 synhw.capInterTouch = (status[1] & 0x40) != 0;
6269 synhw.capReportsMax = (status[0] & 0x02) != 0;
6270 synhw.capClearPad = (status[0] & 0x04) != 0;
6271 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
6272 synhw.capCoveredPad = (status[0] & 0x80) != 0;
6273
6274 if (synhw.capReportsMax) {
6275 if (!set_mouse_scaling(kbdc, 1))
6276 return (FALSE);
6277 if (mouse_ext_command(kbdc, 0x0d) == 0)
6278 return (FALSE);
6279 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6280 return (FALSE);
6281
6282 synhw.maximumXCoord = (status[0] << 5) |
6283 ((status[1] & 0x0f) << 1);
6284 synhw.maximumYCoord = (status[2] << 5) |
6285 ((status[1] & 0xf0) >> 3);
6286 } else {
6291 synhw.nExtendedQueries = (status[0] & 0x70) >> 4;
6292 synhw.capMiddle = (status[0] & 0x04) != 0;
6293 synhw.capPassthrough = (status[2] & 0x80) != 0;
6294 synhw.capLowPower = (status[2] & 0x40) != 0;
6295 synhw.capMultiFingerReport =
6296 (status[2] & 0x20) != 0;
6297 synhw.capSleep = (status[2] & 0x10) != 0;
6298 synhw.capFourButtons = (status[2] & 0x08) != 0;
6299 synhw.capBallistics = (status[2] & 0x04) != 0;
6300 synhw.capMultiFinger = (status[2] & 0x02) != 0;
6301 synhw.capPalmDetect = (status[2] & 0x01) != 0;
6302
6303 if (!set_mouse_scaling(kbdc, 1))
6304 return (FALSE);
6305 if (mouse_ext_command(kbdc, 0x08) == 0)
6306 return (FALSE);
6307 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6308 return (FALSE);
6309
6310 if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) {
6311 synhw.infoXupmm = status[0];
6312 synhw.infoYupmm = status[2];
6313 }
6314
6315 if (verbose >= 2) {
6316 printf(" Extended capabilities:\n");
6317 printf(" capExtended: %d\n", synhw.capExtended);
6318 printf(" capMiddle: %d\n", synhw.capMiddle);
6319 printf(" nExtendedQueries: %d\n",
6320 synhw.nExtendedQueries);
6321 printf(" capPassthrough: %d\n", synhw.capPassthrough);
6322 printf(" capLowPower: %d\n", synhw.capLowPower);
6323 printf(" capMultiFingerReport: %d\n",
6324 synhw.capMultiFingerReport);
6325 printf(" capSleep: %d\n", synhw.capSleep);
6326 printf(" capFourButtons: %d\n", synhw.capFourButtons);
6327 printf(" capBallistics: %d\n", synhw.capBallistics);
6328 printf(" capMultiFinger: %d\n", synhw.capMultiFinger);
6329 printf(" capPalmDetect: %d\n", synhw.capPalmDetect);
6330 printf(" infoXupmm: %d\n", synhw.infoXupmm);
6331 printf(" infoYupmm: %d\n", synhw.infoYupmm);
6332 }
6333
6334 /*
6335 * If nExtendedQueries is 1 or greater, then the TouchPad
6336 * supports this number of extended queries. We can load
6337 * more information about buttons using query 0x09.
6338 */
6339 if (synhw.nExtendedQueries >= 1) {
6340 if (!set_mouse_scaling(kbdc, 1))
6341 return (FALSE);
6342 if (mouse_ext_command(kbdc, 0x09) == 0)
6343 return (FALSE);
6344 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6345 return (FALSE);
6346 synhw.verticalScroll = (status[0] & 0x01) != 0;
6347 synhw.horizontalScroll = (status[0] & 0x02) != 0;
6348 synhw.verticalWheel = (status[0] & 0x08) != 0;
6349 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4;
6350 synhw.capEWmode = (status[0] & 0x04) != 0;
6351 if (verbose >= 2) {
6352 printf(" Extended model ID:\n");
6353 printf(" verticalScroll: %d\n",
6354 synhw.verticalScroll);
6355 printf(" horizontalScroll: %d\n",
6356 synhw.horizontalScroll);
6357 printf(" verticalWheel: %d\n",
6358 synhw.verticalWheel);
6359 printf(" nExtendedButtons: %d\n",
6360 synhw.nExtendedButtons);
6361 printf(" capEWmode: %d\n",
6362 synhw.capEWmode);
6363 }
6364 /*
6365 * Add the number of extended buttons to the total
6366 * button support count, including the middle button
6367 * if capMiddle support bit is set.
6368 */
6369 buttons = synhw.nExtendedButtons + synhw.capMiddle;
6370 } else
6371 /*
6372 * If the capFourButtons support bit is set,
6373 * add a fourth button to the total button count.
6374 */
6375 buttons = synhw.capFourButtons ? 1 : 0;
6376
6377 /* Read the continued capabilities bits. */
6378 if (synhw.nExtendedQueries >= 4) {
6379 if (!set_mouse_scaling(kbdc, 1))
6380 return (FALSE);
6381 if (mouse_ext_command(kbdc, 0x0c) == 0)
6382 return (FALSE);
6383 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6384 return (FALSE);
6385
6386 synhw.capClickPad = (status[1] & 0x01) << 1;
6387 synhw.capClickPad |= (status[0] & 0x10) != 0;
6388 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0;
6389 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0;
6390 synhw.capReportsV = (status[1] & 0x08) != 0;
6391 synhw.capUniformClickPad = (status[1] & 0x10) != 0;
6392 synhw.capReportsMin = (status[1] & 0x20) != 0;
6393 synhw.capInterTouch = (status[1] & 0x40) != 0;
6394 synhw.capReportsMax = (status[0] & 0x02) != 0;
6395 synhw.capClearPad = (status[0] & 0x04) != 0;
6396 synhw.capAdvancedGestures = (status[0] & 0x08) != 0;
6397 synhw.capCoveredPad = (status[0] & 0x80) != 0;
6398
6399 if (synhw.capReportsMax) {
6400 if (!set_mouse_scaling(kbdc, 1))
6401 return (FALSE);
6402 if (mouse_ext_command(kbdc, 0x0d) == 0)
6403 return (FALSE);
6404 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6405 return (FALSE);
6406
6407 synhw.maximumXCoord = (status[0] << 5) |
6408 ((status[1] & 0x0f) << 1);
6409 synhw.maximumYCoord = (status[2] << 5) |
6410 ((status[1] & 0xf0) >> 3);
6411 } else {
6287 synhw.maximumXCoord = SYNAPTICS_DEFAULT_MAX_X;
6288 synhw.maximumYCoord = SYNAPTICS_DEFAULT_MAX_Y;
6412 /*
6413 * Typical bezel limits. Taken from 'Synaptics
6414 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
6415 */
6416 synhw.maximumXCoord = 5472;
6417 synhw.maximumYCoord = 4448;
6289 }
6290
6291 if (synhw.capReportsMin) {
6292 if (!set_mouse_scaling(kbdc, 1))
6293 return (FALSE);
6294 if (mouse_ext_command(kbdc, 0x0f) == 0)
6295 return (FALSE);
6296 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6297 return (FALSE);
6298
6299 synhw.minimumXCoord = (status[0] << 5) |
6300 ((status[1] & 0x0f) << 1);
6301 synhw.minimumYCoord = (status[2] << 5) |
6302 ((status[1] & 0xf0) >> 3);
6303 } else {
6418 }
6419
6420 if (synhw.capReportsMin) {
6421 if (!set_mouse_scaling(kbdc, 1))
6422 return (FALSE);
6423 if (mouse_ext_command(kbdc, 0x0f) == 0)
6424 return (FALSE);
6425 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6426 return (FALSE);
6427
6428 synhw.minimumXCoord = (status[0] << 5) |
6429 ((status[1] & 0x0f) << 1);
6430 synhw.minimumYCoord = (status[2] << 5) |
6431 ((status[1] & 0xf0) >> 3);
6432 } else {
6304 synhw.minimumXCoord = SYNAPTICS_DEFAULT_MIN_X;
6305 synhw.minimumYCoord = SYNAPTICS_DEFAULT_MIN_Y;
6433 /*
6434 * Typical bezel limits. Taken from 'Synaptics
6435 * PS/2 * TouchPad Interfacing Guide' p.3.2.3.
6436 */
6437 synhw.minimumXCoord = 1472;
6438 synhw.minimumYCoord = 1408;
6306 }
6307
6308 /*
6309 * ClickPad properties are not exported through PS/2
6310 * protocol. Detection is based on controller's PnP ID.
6311 */
6312 if (synhw.capClickPad && psmcpnp_sc != NULL) {
6313 switch (psmcpnp_sc->type) {
6314 case PSMCPNP_FORCEPAD:
6315 synhw.forcePad = 1;
6316 break;
6317 default:
6318 break;
6319 }
6320 }
6321
6322 if (verbose >= 2) {
6323 printf(" Continued capabilities:\n");
6324 printf(" capClickPad: %d\n",
6325 synhw.capClickPad);
6326 printf(" capDeluxeLEDs: %d\n",
6327 synhw.capDeluxeLEDs);
6328 printf(" noAbsoluteFilter: %d\n",
6329 synhw.noAbsoluteFilter);
6330 printf(" capReportsV: %d\n",
6331 synhw.capReportsV);
6332 printf(" capUniformClickPad: %d\n",
6333 synhw.capUniformClickPad);
6334 printf(" capReportsMin: %d\n",
6335 synhw.capReportsMin);
6336 printf(" capInterTouch: %d\n",
6337 synhw.capInterTouch);
6338 printf(" capReportsMax: %d\n",
6339 synhw.capReportsMax);
6340 printf(" capClearPad: %d\n",
6341 synhw.capClearPad);
6342 printf(" capAdvancedGestures: %d\n",
6343 synhw.capAdvancedGestures);
6344 printf(" capCoveredPad: %d\n",
6345 synhw.capCoveredPad);
6346 if (synhw.capReportsMax) {
6347 printf(" maximumXCoord: %d\n",
6348 synhw.maximumXCoord);
6349 printf(" maximumYCoord: %d\n",
6350 synhw.maximumYCoord);
6351 }
6352 if (synhw.capReportsMin) {
6353 printf(" minimumXCoord: %d\n",
6354 synhw.minimumXCoord);
6355 printf(" minimumYCoord: %d\n",
6356 synhw.minimumYCoord);
6357 }
6358 if (synhw.capClickPad) {
6359 printf(" forcePad: %d\n",
6360 synhw.forcePad);
6361 }
6362 }
6363 buttons += synhw.capClickPad;
6364 }
6365 }
6366
6367 if (verbose >= 2) {
6368 if (synhw.capExtended)
6369 printf(" Additional Buttons: %d\n", buttons);
6370 else
6371 printf(" No extended capabilities\n");
6372 }
6373
6374 /*
6375 * Add the default number of 3 buttons to the total
6376 * count of supported buttons reported above.
6377 */
6378 buttons += 3;
6379
6380 /*
6381 * Read the mode byte.
6382 *
6383 * XXX: Note the Synaptics documentation also defines the first
6384 * byte of the response to this query to be a constant 0x3b, this
6385 * does not appear to be true for Touchpads with guest devices.
6386 */
6387 if (mouse_ext_command(kbdc, 1) == 0)
6388 return (FALSE);
6389 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6390 return (FALSE);
6439 }
6440
6441 /*
6442 * ClickPad properties are not exported through PS/2
6443 * protocol. Detection is based on controller's PnP ID.
6444 */
6445 if (synhw.capClickPad && psmcpnp_sc != NULL) {
6446 switch (psmcpnp_sc->type) {
6447 case PSMCPNP_FORCEPAD:
6448 synhw.forcePad = 1;
6449 break;
6450 default:
6451 break;
6452 }
6453 }
6454
6455 if (verbose >= 2) {
6456 printf(" Continued capabilities:\n");
6457 printf(" capClickPad: %d\n",
6458 synhw.capClickPad);
6459 printf(" capDeluxeLEDs: %d\n",
6460 synhw.capDeluxeLEDs);
6461 printf(" noAbsoluteFilter: %d\n",
6462 synhw.noAbsoluteFilter);
6463 printf(" capReportsV: %d\n",
6464 synhw.capReportsV);
6465 printf(" capUniformClickPad: %d\n",
6466 synhw.capUniformClickPad);
6467 printf(" capReportsMin: %d\n",
6468 synhw.capReportsMin);
6469 printf(" capInterTouch: %d\n",
6470 synhw.capInterTouch);
6471 printf(" capReportsMax: %d\n",
6472 synhw.capReportsMax);
6473 printf(" capClearPad: %d\n",
6474 synhw.capClearPad);
6475 printf(" capAdvancedGestures: %d\n",
6476 synhw.capAdvancedGestures);
6477 printf(" capCoveredPad: %d\n",
6478 synhw.capCoveredPad);
6479 if (synhw.capReportsMax) {
6480 printf(" maximumXCoord: %d\n",
6481 synhw.maximumXCoord);
6482 printf(" maximumYCoord: %d\n",
6483 synhw.maximumYCoord);
6484 }
6485 if (synhw.capReportsMin) {
6486 printf(" minimumXCoord: %d\n",
6487 synhw.minimumXCoord);
6488 printf(" minimumYCoord: %d\n",
6489 synhw.minimumYCoord);
6490 }
6491 if (synhw.capClickPad) {
6492 printf(" forcePad: %d\n",
6493 synhw.forcePad);
6494 }
6495 }
6496 buttons += synhw.capClickPad;
6497 }
6498 }
6499
6500 if (verbose >= 2) {
6501 if (synhw.capExtended)
6502 printf(" Additional Buttons: %d\n", buttons);
6503 else
6504 printf(" No extended capabilities\n");
6505 }
6506
6507 /*
6508 * Add the default number of 3 buttons to the total
6509 * count of supported buttons reported above.
6510 */
6511 buttons += 3;
6512
6513 /*
6514 * Read the mode byte.
6515 *
6516 * XXX: Note the Synaptics documentation also defines the first
6517 * byte of the response to this query to be a constant 0x3b, this
6518 * does not appear to be true for Touchpads with guest devices.
6519 */
6520 if (mouse_ext_command(kbdc, 1) == 0)
6521 return (FALSE);
6522 if (get_mouse_status(kbdc, status, 0, 3) != 3)
6523 return (FALSE);
6391 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != middle_byte) {
6524 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) {
6392 printf(" Failed to read mode byte\n");
6393 return (FALSE);
6394 }
6395
6396 if (arg == PROBE)
6397 sc->synhw = synhw;
6398 if (!synaptics_support)
6399 return (FALSE);
6400
6401 /* Set mouse type just now for synaptics_set_mode() */
6402 sc->hw.model = MOUSE_MODEL_SYNAPTICS;
6403
6404 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6405
6406 if (trackpoint_support && synhw.capPassthrough) {
6407 enable_trackpoint(sc, arg);
6408 }
6409
6410 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
6411
6412 if (arg == PROBE) {
6413 /* Create sysctl tree. */
6414 synaptics_sysctl_create_tree(sc, "synaptics",
6415 "Synaptics TouchPad");
6416 sc->hw.buttons = buttons;
6417 }
6418
6419 return (TRUE);
6420}
6421
6422static void
6423synaptics_passthrough_on(struct psm_softc *sc)
6424{
6425 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
6426 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
6427}
6428
6429static void
6430synaptics_passthrough_off(struct psm_softc *sc)
6431{
6432 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
6433 set_mouse_scaling(sc->kbdc, 2);
6434 set_mouse_scaling(sc->kbdc, 1);
6435 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6436}
6437
6438/* IBM/Lenovo TrackPoint */
6439static int
6440trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
6441{
6442 const int seq[] = { 0xe2, cmd, loc, val };
6443 int i;
6444
6445 if (sc->synhw.capPassthrough)
6446 synaptics_passthrough_on(sc);
6447
6448 for (i = 0; i < nitems(seq); i++) {
6449 if (sc->synhw.capPassthrough &&
6450 (seq[i] == 0xff || seq[i] == 0xe7))
6451 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
6452 synaptics_passthrough_off(sc);
6453 return (EIO);
6454 }
6455 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
6456 if (sc->synhw.capPassthrough)
6457 synaptics_passthrough_off(sc);
6458 return (EIO);
6459 }
6460 }
6461
6462 if (sc->synhw.capPassthrough)
6463 synaptics_passthrough_off(sc);
6464
6465 return (0);
6466}
6467
6468#define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
6469#define TPMASK 0
6470#define TPLOC 1
6471#define TPINFO 2
6472
6473static int
6474trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
6475{
6476 static const int data[][3] = {
6477 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
6478 { 0x00, 0x4d, PSM_TPINFO(inertia) },
6479 { 0x00, 0x60, PSM_TPINFO(uplateau) },
6480 { 0x00, 0x57, PSM_TPINFO(reach) },
6481 { 0x00, 0x58, PSM_TPINFO(draghys) },
6482 { 0x00, 0x59, PSM_TPINFO(mindrag) },
6483 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
6484 { 0x00, 0x5c, PSM_TPINFO(threshold) },
6485 { 0x00, 0x5d, PSM_TPINFO(jenks) },
6486 { 0x00, 0x5e, PSM_TPINFO(ztime) },
6487 { 0x01, 0x2c, PSM_TPINFO(pts) },
6488 { 0x08, 0x2d, PSM_TPINFO(skipback) }
6489 };
6490 struct psm_softc *sc;
6491 int error, newval, *oldvalp;
6492 const int *tp;
6493
6494 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
6495 return (EINVAL);
6496 sc = arg1;
6497 tp = data[arg2];
6498 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
6499 newval = *oldvalp;
6500 error = sysctl_handle_int(oidp, &newval, 0, req);
6501 if (error != 0)
6502 return (error);
6503 if (newval == *oldvalp)
6504 return (0);
6505 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
6506 return (EINVAL);
6507 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
6508 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
6509 if (error != 0)
6510 return (error);
6511 *oldvalp = newval;
6512
6513 return (0);
6514}
6515
6516static void
6517trackpoint_sysctl_create_tree(struct psm_softc *sc)
6518{
6519
6520 if (sc->tpinfo.sysctl_tree != NULL)
6521 return;
6522
6523 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
6524 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
6525 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
6526 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
6527 0, "IBM/Lenovo TrackPoint");
6528
6529 /* hw.psm.trackpoint.sensitivity */
6530 sc->tpinfo.sensitivity = 0x80;
6531 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6532 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6533 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6534 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
6535 trackpoint_sysctl, "I",
6536 "Sensitivity");
6537
6538 /* hw.psm.trackpoint.negative_inertia */
6539 sc->tpinfo.inertia = 0x06;
6540 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6541 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6542 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6543 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
6544 trackpoint_sysctl, "I",
6545 "Negative inertia factor");
6546
6547 /* hw.psm.trackpoint.upper_plateau */
6548 sc->tpinfo.uplateau = 0x61;
6549 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6550 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6551 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6552 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
6553 trackpoint_sysctl, "I",
6554 "Transfer function upper plateau speed");
6555
6556 /* hw.psm.trackpoint.backup_range */
6557 sc->tpinfo.reach = 0x0a;
6558 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6559 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6560 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6561 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
6562 trackpoint_sysctl, "I",
6563 "Backup range");
6564
6565 /* hw.psm.trackpoint.drag_hysteresis */
6566 sc->tpinfo.draghys = 0xff;
6567 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6568 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6569 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6570 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
6571 trackpoint_sysctl, "I",
6572 "Drag hysteresis");
6573
6574 /* hw.psm.trackpoint.minimum_drag */
6575 sc->tpinfo.mindrag = 0x14;
6576 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6577 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6578 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6579 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
6580 trackpoint_sysctl, "I",
6581 "Minimum drag");
6582
6583 /* hw.psm.trackpoint.up_threshold */
6584 sc->tpinfo.upthresh = 0xff;
6585 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6586 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6587 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6588 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
6589 trackpoint_sysctl, "I",
6590 "Up threshold for release");
6591
6592 /* hw.psm.trackpoint.threshold */
6593 sc->tpinfo.threshold = 0x08;
6594 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6595 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6596 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6597 sc, TRACKPOINT_SYSCTL_THRESHOLD,
6598 trackpoint_sysctl, "I",
6599 "Threshold");
6600
6601 /* hw.psm.trackpoint.jenks_curvature */
6602 sc->tpinfo.jenks = 0x87;
6603 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6604 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6605 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6606 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
6607 trackpoint_sysctl, "I",
6608 "Jenks curvature");
6609
6610 /* hw.psm.trackpoint.z_time */
6611 sc->tpinfo.ztime = 0x26;
6612 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6613 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6614 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6615 sc, TRACKPOINT_SYSCTL_Z_TIME,
6616 trackpoint_sysctl, "I",
6617 "Z time constant");
6618
6619 /* hw.psm.trackpoint.press_to_select */
6620 sc->tpinfo.pts = 0x00;
6621 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6622 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6623 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6624 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
6625 trackpoint_sysctl, "I",
6626 "Press to Select");
6627
6628 /* hw.psm.trackpoint.skip_backups */
6629 sc->tpinfo.skipback = 0x00;
6630 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6631 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6632 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6633 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
6634 trackpoint_sysctl, "I",
6635 "Skip backups from drags");
6636}
6637
6638static void
6639set_trackpoint_parameters(struct psm_softc *sc)
6640{
6641 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
6642 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
6643 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
6644 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
6645 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
6646 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
6647 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
6648 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
6649 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
6650 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
6651 if (sc->tpinfo.pts == 0x01)
6652 trackpoint_command(sc, 0x47, 0x2c, 0x01);
6653 if (sc->tpinfo.skipback == 0x01)
6654 trackpoint_command(sc, 0x47, 0x2d, 0x08);
6655}
6656
6657static int
6658enable_trackpoint(struct psm_softc *sc, enum probearg arg)
6659{
6660 KBDC kbdc = sc->kbdc;
6661 int id;
6662
6663 /*
6664 * If called from enable_synaptics(), make sure that passthrough
6665 * mode is enabled so we can reach the trackpoint.
6666 * However, passthrough mode must be disabled before setting the
6667 * trackpoint parameters, as rackpoint_command() enables and disables
6668 * passthrough mode on its own.
6669 */
6670 if (sc->synhw.capPassthrough)
6671 synaptics_passthrough_on(sc);
6672
6673 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
6674 read_aux_data(kbdc) != 0x01)
6675 goto no_trackpoint;
6676 id = read_aux_data(kbdc);
6677 if (id < 0x01)
6678 goto no_trackpoint;
6679 if (arg == PROBE)
6680 sc->tphw = id;
6681 if (!trackpoint_support)
6682 goto no_trackpoint;
6683
6684 if (sc->synhw.capPassthrough)
6685 synaptics_passthrough_off(sc);
6686
6687 if (arg == PROBE) {
6688 trackpoint_sysctl_create_tree(sc);
6689 /*
6690 * Don't overwrite hwid and buttons when we are
6691 * a guest device.
6692 */
6693 if (!sc->synhw.capPassthrough) {
6694 sc->hw.hwid = id;
6695 sc->hw.buttons = 3;
6696 }
6697 }
6698
6699 set_trackpoint_parameters(sc);
6700
6701 return (TRUE);
6702
6703no_trackpoint:
6704 if (sc->synhw.capPassthrough)
6705 synaptics_passthrough_off(sc);
6706
6707 return (FALSE);
6708}
6709
6710/* Interlink electronics VersaPad */
6711static int
6712enable_versapad(struct psm_softc *sc, enum probearg arg)
6713{
6714 KBDC kbdc = sc->kbdc;
6715 int data[3];
6716
6717 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
6718 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
6719 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6720 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6721 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6722 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6723 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
6724 return (FALSE);
6725 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
6726 return (FALSE);
6727 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6728
6729 return (TRUE); /* PS/2 absolute mode */
6730}
6731
6732/* Elantech Touchpad */
6733static int
6734elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6735{
6736 int res, readcmd, retidx;
6737 int resp[3];
6738
6739 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6740 retidx = hwversion == 4 ? 1 : 0;
6741
6742 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6743 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6744 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6745 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6746 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6747
6748 if (res == 0)
6749 *val = resp[retidx];
6750
6751 return (res);
6752}
6753
6754static int
6755elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
6756{
6757 int res, writecmd;
6758
6759 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
6760
6761 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6762 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6763 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6764 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6765 if (hwversion == 4) {
6766 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6767 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6768 }
6769 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6770 res |= send_aux_command(kbdc, val) != PSM_ACK;
6771 res |= set_mouse_scaling(kbdc, 1) == 0;
6772
6773 return (res);
6774}
6775
6776static int
6777elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
6778{
6779 int res;
6780
6781 if (hwversion == 2) {
6782 res = set_mouse_scaling(kbdc, 1) == 0;
6783 res |= mouse_ext_command(kbdc, cmd) == 0;
6784 } else {
6785 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6786 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
6787 }
6788 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6789
6790 return (res);
6791}
6792
6793static int
6794elantech_init(KBDC kbdc, elantechhw_t *elanhw)
6795{
6796 int i, val, res, hwversion, reg10;
6797
6798 /* set absolute mode */
6799 hwversion = elanhw->hwversion;
6800 reg10 = -1;
6801 switch (hwversion) {
6802 case 2:
6803 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
6804 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6805 if (res)
6806 break;
6807 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
6808 break;
6809 case 3:
6810 reg10 = 0x0b;
6811 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6812 break;
6813 case 4:
6814 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
6815 break;
6816 default:
6817 res = 1;
6818 }
6819
6820 /* Read back reg 0x10 to ensure hardware is ready. */
6821 if (res == 0 && reg10 >= 0) {
6822 for (i = 0; i < 5; i++) {
6823 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
6824 break;
6825 DELAY(2000);
6826 }
6827 if (i == 5)
6828 res = 1;
6829 }
6830
6831 if (res)
6832 printf("couldn't set absolute mode\n");
6833
6834 return (res);
6835}
6836
6837static void
6838elantech_init_synaptics(struct psm_softc *sc)
6839{
6840
6841 /* Set capabilites required by movement smother */
6842 sc->synhw.infoMajor = sc->elanhw.hwversion;
6843 sc->synhw.infoMinor = sc->elanhw.fwversion;
6844 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
6845 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
6846 sc->synhw.verticalScroll = 0;
6847 sc->synhw.nExtendedQueries = 4;
6848 sc->synhw.capExtended = 1;
6849 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
6850 sc->synhw.capClickPad = sc->elanhw.isclickpad;
6851 sc->synhw.capMultiFinger = 1;
6852 if (sc->elanhw.issemimt)
6853 sc->synhw.capAdvancedGestures = 1;
6854 else
6855 sc->synhw.capReportsV = 1;
6856 sc->synhw.capPalmDetect = 1;
6857 sc->synhw.capPen = 0;
6858 sc->synhw.capReportsMax = 1;
6859 sc->synhw.maximumXCoord = sc->elanhw.sizex;
6860 sc->synhw.maximumYCoord = sc->elanhw.sizey;
6861 sc->synhw.capReportsMin = 1;
6862 sc->synhw.minimumXCoord = 0;
6863 sc->synhw.minimumYCoord = 0;
6864
6865 if (sc->syninfo.sysctl_tree == NULL) {
6866 synaptics_sysctl_create_tree(sc, "elantech",
6867 "Elantech Touchpad");
6868
6869 /*
6870 * Adjust synaptic smoother tunables
6871 * 1. Disable finger detection pressure threshold. Unlike
6872 * synaptics we assume the finger is acting when packet with
6873 * its X&Y arrives not when pressure exceedes some threshold
6874 * 2. Disable unrelated features like margins and noisy areas
6875 * 3. Disable virtual scroll areas as 2nd finger is preferable
6876 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
6877 * softbuttons
6878 * 5. Scale down divisors and movement lengths by a factor of 3
6879 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
6880 */
6881
6882 /* Set reporting range to be equal touchpad size */
6883 sc->syninfo.max_x = sc->elanhw.sizex;
6884 sc->syninfo.max_y = sc->elanhw.sizey;
6885
6886 /* Disable finger detection pressure threshold */
6887 sc->syninfo.min_pressure = 1;
6888
6889 /* Adjust palm width to nearly match synaptics w=10 */
6890 sc->syninfo.max_width = 7;
6891
6892 /* Elans often report double & triple taps as single event */
6893 sc->syninfo.tap_min_queue = 1;
6894
6895 /* Use full area of touchpad */
6896 sc->syninfo.margin_top = 0;
6897 sc->syninfo.margin_right = 0;
6898 sc->syninfo.margin_bottom = 0;
6899 sc->syninfo.margin_left = 0;
6900
6901 /* Disable noisy area */
6902 sc->syninfo.na_top = 0;
6903 sc->syninfo.na_right = 0;
6904 sc->syninfo.na_bottom = 0;
6905 sc->syninfo.na_left = 0;
6906
6907 /* Tune divisors and movement lengths */
6908 sc->syninfo.weight_len_squared = 200;
6909 sc->syninfo.div_min = 3;
6910 sc->syninfo.div_max = 6;
6911 sc->syninfo.div_max_na = 10;
6912 sc->syninfo.div_len = 30;
6913 sc->syninfo.tap_max_delta = 25;
6914
6915 /* Disable virtual scrolling areas and tune its divisors */
6916 sc->syninfo.vscroll_hor_area = 0;
6917 sc->syninfo.vscroll_ver_area = 0;
6918 sc->syninfo.vscroll_min_delta = 15;
6919 sc->syninfo.vscroll_div_min = 30;
6920 sc->syninfo.vscroll_div_max = 50;
6921
6922 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
6923 if (sc->elanhw.isclickpad) {
6924 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
6925 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
6926 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
6927 }
6928 }
6929
6930 return;
6931}
6932
6933static int
6934enable_elantech(struct psm_softc *sc, enum probearg arg)
6935{
6936 static const int ic2hw[] =
6937 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
6938 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 };
6939 static const int fw_sizes[][3] = {
6940 /* FW.vers MaxX MaxY */
6941 { 0x020030, 1152, 768 },
6942 { 0x020800, 1152, 768 },
6943 { 0x020b00, 1152, 768 },
6944 { 0x040215, 900, 500 },
6945 { 0x040216, 819, 405 },
6946 { 0x040219, 900, 500 },
6947 };
6948 elantechhw_t elanhw;
6949 int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy;
6950 KBDC kbdc = sc->kbdc;
6951
6952 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
6953
6954 set_mouse_scaling(kbdc, 1);
6955 set_mouse_scaling(kbdc, 1);
6956 set_mouse_scaling(kbdc, 1);
6957 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
6958 return (FALSE);
6959
6960 if (!ELANTECH_MAGIC(resp))
6961 return (FALSE);
6962
6963 /* Identify the Touchpad version. */
6964 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
6965 return (FALSE);
6966
6967 bzero(&elanhw, sizeof(elanhw));
6968
6969 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
6970 icversion = resp[0] & 0x0f;
6971 hwversion = ic2hw[icversion];
6972
6973 if (verbose >= 2)
6974 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
6975 hwversion, elanhw.fwversion);
6976
6977 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
6978 printf (" Unsupported touchpad hardware (v1)\n");
6979 return (FALSE);
6980 }
6981 if (hwversion == 0) {
6982 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
6983 elanhw.fwversion);
6984 return (FALSE);
6985 }
6986
6987 /* Get the Touchpad model information. */
6988 elanhw.hwversion = hwversion;
6989 elanhw.issemimt = hwversion == 2;
6990 elanhw.isclickpad = (resp[1] & 0x10) != 0;
6991 elanhw.hascrc = (resp[1] & 0x40) != 0;
6992 elanhw.haspressure = elanhw.fwversion >= 0x020800;
6993
6994 /* Read the capability bits. */
6995 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
6996 printf(" Failed to read capability bits\n");
6997 return (FALSE);
6998 }
6999
7000 elanhw.ntracesx = imax(resp[1], 3);
7001 elanhw.ntracesy = imax(resp[2], 3);
7002 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
7003
7004 /* Get the touchpad resolution */
7005 switch (hwversion) {
7006 case 4:
7007 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
7008 == 0) {
7009 dpix = (resp[1] & 0x0f) * 10 + 790;
7010 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
7011 elanhw.dpmmx = (dpix * 10 + 5) / 254;
7012 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
7013 break;
7014 }
7015 /* FALLTHROUGH */
7016 case 2:
7017 case 3:
7018 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
7019 break;
7020 }
7021
7022 if (!elantech_support)
7023 return (FALSE);
7024
7025 if (elantech_init(kbdc, &elanhw)) {
7026 printf("couldn't initialize elantech touchpad\n");
7027 return (FALSE);
7028 }
7029
7030 /*
7031 * Get the touchpad reporting range.
7032 * On HW v.3 touchpads it should be done after switching hardware
7033 * to real resolution mode (by setting bit 3 of reg10)
7034 */
7035 elanhw.dptracex = elanhw.dptracey = 64;
7036 for (i = 0; i < nitems(fw_sizes); i++) {
7037 if (elanhw.fwversion == fw_sizes[i][0]) {
7038 elanhw.sizex = fw_sizes[i][1];
7039 elanhw.sizey = fw_sizes[i][2];
7040 goto found;
7041 }
7042 }
7043 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
7044 printf(" Failed to read touchpad size\n");
7045 elanhw.sizex = 10000; /* Arbitrary high values to */
7046 elanhw.sizey = 10000; /* prevent clipping in smoother */
7047 } else if (hwversion == 2) {
7048 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
7049 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
7050 elanhw.dptracex = resp[1] / 2;
7051 elanhw.dptracey = resp[2] / 2;
7052 }
7053 xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2;
7054 elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex;
7055 elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey;
7056 } else {
7057 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
7058 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
7059 xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1;
7060 elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr);
7061 elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr);
7062 }
7063found:
7064 if (verbose >= 2) {
7065 printf(" Model information:\n");
7066 printf(" MaxX: %d\n", elanhw.sizex);
7067 printf(" MaxY: %d\n", elanhw.sizey);
7068 printf(" DpmmX: %d\n", elanhw.dpmmx);
7069 printf(" DpmmY: %d\n", elanhw.dpmmy);
7070 printf(" TracesX: %d\n", elanhw.ntracesx);
7071 printf(" TracesY: %d\n", elanhw.ntracesy);
7072 printf(" DptraceX: %d\n", elanhw.dptracex);
7073 printf(" DptraceY: %d\n", elanhw.dptracey);
7074 printf(" SemiMT: %d\n", elanhw.issemimt);
7075 printf(" Clickpad: %d\n", elanhw.isclickpad);
7076 printf(" Trackpoint: %d\n", elanhw.hastrackpoint);
7077 printf(" CRC: %d\n", elanhw.hascrc);
7078 printf(" Pressure: %d\n", elanhw.haspressure);
7079 }
7080
7081 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
7082
7083 if (arg == PROBE) {
7084 sc->elanhw = elanhw;
7085 sc->hw.buttons = 3;
7086
7087 /* Initialize synaptics movement smoother */
7088 elantech_init_synaptics(sc);
7089
7090 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
7091 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
7092 }
7093
7094 return (TRUE);
7095}
7096
7097/*
7098 * Return true if 'now' is earlier than (start + (secs.usecs)).
7099 * Now may be NULL and the function will fetch the current time from
7100 * getmicrouptime(), or a cached 'now' can be passed in.
7101 * All values should be numbers derived from getmicrouptime().
7102 */
7103static int
7104timeelapsed(start, secs, usecs, now)
7105 const struct timeval *start, *now;
7106 int secs, usecs;
7107{
7108 struct timeval snow, tv;
7109
7110 /* if there is no 'now' passed in, the get it as a convience. */
7111 if (now == NULL) {
7112 getmicrouptime(&snow);
7113 now = &snow;
7114 }
7115
7116 tv.tv_sec = secs;
7117 tv.tv_usec = usecs;
7118 timevaladd(&tv, start);
7119 return (timevalcmp(&tv, now, <));
7120}
7121
7122static int
7123psmresume(device_t dev)
7124{
7125 struct psm_softc *sc = device_get_softc(dev);
7126 int unit = device_get_unit(dev);
7127 int err;
7128
7129 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
7130
7131 if ((sc->config &
7132 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
7133 return (0);
7134
7135 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
7136
7137 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
7138 /*
7139 * Release the blocked process; it must be notified that
7140 * the device cannot be accessed anymore.
7141 */
7142 sc->state &= ~PSM_ASLP;
7143 wakeup(sc);
7144 }
7145
7146 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
7147
7148 return (err);
7149}
7150
7151DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
7152#ifdef EVDEV_SUPPORT
7153MODULE_DEPEND(psm, evdev, 1, 1, 1);
7154#endif
7155
7156#ifdef DEV_ISA
7157
7158/*
7159 * This sucks up assignments from PNPBIOS and ACPI.
7160 */
7161
7162/*
7163 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
7164 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
7165 * can be probed and attached only after the AT keyboard controller is
7166 * attached, we shall quietly reserve the IRQ resource for later use.
7167 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
7168 * copy the IRQ resource to the PS/2 mouse device instance hanging
7169 * under the keyboard controller, then probe and attach it.
7170 */
7171
7172static devclass_t psmcpnp_devclass;
7173
7174static device_probe_t psmcpnp_probe;
7175static device_attach_t psmcpnp_attach;
7176
7177static device_method_t psmcpnp_methods[] = {
7178 DEVMETHOD(device_probe, psmcpnp_probe),
7179 DEVMETHOD(device_attach, psmcpnp_attach),
7180
7181 { 0, 0 }
7182};
7183
7184static driver_t psmcpnp_driver = {
7185 PSMCPNP_DRIVER_NAME,
7186 psmcpnp_methods,
7187 sizeof(struct psmcpnp_softc),
7188};
7189
7190static struct isa_pnp_id psmcpnp_ids[] = {
7191 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
7192 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
7193 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
7194 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
7195 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
7196 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
7197 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
7198 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
7199 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
7200 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
7201 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
7202 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
7203 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
7204 { 0 }
7205};
7206
7207/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7208static struct isa_pnp_id forcepad_ids[] = {
7209 { 0x0d302e4f, "HP PS/2 forcepad port" }, /* SYN300D, EB 1040 */
7210 { 0x14302e4f, "HP PS/2 forcepad port" }, /* SYN3014, EB 1040 */
7211 { 0 }
7212};
7213
6525 printf(" Failed to read mode byte\n");
6526 return (FALSE);
6527 }
6528
6529 if (arg == PROBE)
6530 sc->synhw = synhw;
6531 if (!synaptics_support)
6532 return (FALSE);
6533
6534 /* Set mouse type just now for synaptics_set_mode() */
6535 sc->hw.model = MOUSE_MODEL_SYNAPTICS;
6536
6537 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6538
6539 if (trackpoint_support && synhw.capPassthrough) {
6540 enable_trackpoint(sc, arg);
6541 }
6542
6543 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons));
6544
6545 if (arg == PROBE) {
6546 /* Create sysctl tree. */
6547 synaptics_sysctl_create_tree(sc, "synaptics",
6548 "Synaptics TouchPad");
6549 sc->hw.buttons = buttons;
6550 }
6551
6552 return (TRUE);
6553}
6554
6555static void
6556synaptics_passthrough_on(struct psm_softc *sc)
6557{
6558 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n"));
6559 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5));
6560}
6561
6562static void
6563synaptics_passthrough_off(struct psm_softc *sc)
6564{
6565 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n"));
6566 set_mouse_scaling(sc->kbdc, 2);
6567 set_mouse_scaling(sc->kbdc, 1);
6568 synaptics_set_mode(sc, synaptics_preferred_mode(sc));
6569}
6570
6571/* IBM/Lenovo TrackPoint */
6572static int
6573trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val)
6574{
6575 const int seq[] = { 0xe2, cmd, loc, val };
6576 int i;
6577
6578 if (sc->synhw.capPassthrough)
6579 synaptics_passthrough_on(sc);
6580
6581 for (i = 0; i < nitems(seq); i++) {
6582 if (sc->synhw.capPassthrough &&
6583 (seq[i] == 0xff || seq[i] == 0xe7))
6584 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) {
6585 synaptics_passthrough_off(sc);
6586 return (EIO);
6587 }
6588 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) {
6589 if (sc->synhw.capPassthrough)
6590 synaptics_passthrough_off(sc);
6591 return (EIO);
6592 }
6593 }
6594
6595 if (sc->synhw.capPassthrough)
6596 synaptics_passthrough_off(sc);
6597
6598 return (0);
6599}
6600
6601#define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x)
6602#define TPMASK 0
6603#define TPLOC 1
6604#define TPINFO 2
6605
6606static int
6607trackpoint_sysctl(SYSCTL_HANDLER_ARGS)
6608{
6609 static const int data[][3] = {
6610 { 0x00, 0x4a, PSM_TPINFO(sensitivity) },
6611 { 0x00, 0x4d, PSM_TPINFO(inertia) },
6612 { 0x00, 0x60, PSM_TPINFO(uplateau) },
6613 { 0x00, 0x57, PSM_TPINFO(reach) },
6614 { 0x00, 0x58, PSM_TPINFO(draghys) },
6615 { 0x00, 0x59, PSM_TPINFO(mindrag) },
6616 { 0x00, 0x5a, PSM_TPINFO(upthresh) },
6617 { 0x00, 0x5c, PSM_TPINFO(threshold) },
6618 { 0x00, 0x5d, PSM_TPINFO(jenks) },
6619 { 0x00, 0x5e, PSM_TPINFO(ztime) },
6620 { 0x01, 0x2c, PSM_TPINFO(pts) },
6621 { 0x08, 0x2d, PSM_TPINFO(skipback) }
6622 };
6623 struct psm_softc *sc;
6624 int error, newval, *oldvalp;
6625 const int *tp;
6626
6627 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data))
6628 return (EINVAL);
6629 sc = arg1;
6630 tp = data[arg2];
6631 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]);
6632 newval = *oldvalp;
6633 error = sysctl_handle_int(oidp, &newval, 0, req);
6634 if (error != 0)
6635 return (error);
6636 if (newval == *oldvalp)
6637 return (0);
6638 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1))
6639 return (EINVAL);
6640 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47,
6641 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]);
6642 if (error != 0)
6643 return (error);
6644 *oldvalp = newval;
6645
6646 return (0);
6647}
6648
6649static void
6650trackpoint_sysctl_create_tree(struct psm_softc *sc)
6651{
6652
6653 if (sc->tpinfo.sysctl_tree != NULL)
6654 return;
6655
6656 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */
6657 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx);
6658 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx,
6659 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD,
6660 0, "IBM/Lenovo TrackPoint");
6661
6662 /* hw.psm.trackpoint.sensitivity */
6663 sc->tpinfo.sensitivity = 0x80;
6664 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6665 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6666 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6667 sc, TRACKPOINT_SYSCTL_SENSITIVITY,
6668 trackpoint_sysctl, "I",
6669 "Sensitivity");
6670
6671 /* hw.psm.trackpoint.negative_inertia */
6672 sc->tpinfo.inertia = 0x06;
6673 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6674 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6675 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6676 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
6677 trackpoint_sysctl, "I",
6678 "Negative inertia factor");
6679
6680 /* hw.psm.trackpoint.upper_plateau */
6681 sc->tpinfo.uplateau = 0x61;
6682 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6683 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6684 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6685 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU,
6686 trackpoint_sysctl, "I",
6687 "Transfer function upper plateau speed");
6688
6689 /* hw.psm.trackpoint.backup_range */
6690 sc->tpinfo.reach = 0x0a;
6691 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6692 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6693 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6694 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE,
6695 trackpoint_sysctl, "I",
6696 "Backup range");
6697
6698 /* hw.psm.trackpoint.drag_hysteresis */
6699 sc->tpinfo.draghys = 0xff;
6700 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6701 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6702 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6703 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
6704 trackpoint_sysctl, "I",
6705 "Drag hysteresis");
6706
6707 /* hw.psm.trackpoint.minimum_drag */
6708 sc->tpinfo.mindrag = 0x14;
6709 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6710 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6711 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6712 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG,
6713 trackpoint_sysctl, "I",
6714 "Minimum drag");
6715
6716 /* hw.psm.trackpoint.up_threshold */
6717 sc->tpinfo.upthresh = 0xff;
6718 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6719 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6720 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6721 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD,
6722 trackpoint_sysctl, "I",
6723 "Up threshold for release");
6724
6725 /* hw.psm.trackpoint.threshold */
6726 sc->tpinfo.threshold = 0x08;
6727 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6728 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6729 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6730 sc, TRACKPOINT_SYSCTL_THRESHOLD,
6731 trackpoint_sysctl, "I",
6732 "Threshold");
6733
6734 /* hw.psm.trackpoint.jenks_curvature */
6735 sc->tpinfo.jenks = 0x87;
6736 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6737 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6738 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6739 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE,
6740 trackpoint_sysctl, "I",
6741 "Jenks curvature");
6742
6743 /* hw.psm.trackpoint.z_time */
6744 sc->tpinfo.ztime = 0x26;
6745 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6746 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6747 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6748 sc, TRACKPOINT_SYSCTL_Z_TIME,
6749 trackpoint_sysctl, "I",
6750 "Z time constant");
6751
6752 /* hw.psm.trackpoint.press_to_select */
6753 sc->tpinfo.pts = 0x00;
6754 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6755 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6756 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6757 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
6758 trackpoint_sysctl, "I",
6759 "Press to Select");
6760
6761 /* hw.psm.trackpoint.skip_backups */
6762 sc->tpinfo.skipback = 0x00;
6763 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx,
6764 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO,
6765 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY,
6766 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS,
6767 trackpoint_sysctl, "I",
6768 "Skip backups from drags");
6769}
6770
6771static void
6772set_trackpoint_parameters(struct psm_softc *sc)
6773{
6774 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity);
6775 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau);
6776 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia);
6777 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach);
6778 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys);
6779 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag);
6780 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh);
6781 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold);
6782 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks);
6783 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime);
6784 if (sc->tpinfo.pts == 0x01)
6785 trackpoint_command(sc, 0x47, 0x2c, 0x01);
6786 if (sc->tpinfo.skipback == 0x01)
6787 trackpoint_command(sc, 0x47, 0x2d, 0x08);
6788}
6789
6790static int
6791enable_trackpoint(struct psm_softc *sc, enum probearg arg)
6792{
6793 KBDC kbdc = sc->kbdc;
6794 int id;
6795
6796 /*
6797 * If called from enable_synaptics(), make sure that passthrough
6798 * mode is enabled so we can reach the trackpoint.
6799 * However, passthrough mode must be disabled before setting the
6800 * trackpoint parameters, as rackpoint_command() enables and disables
6801 * passthrough mode on its own.
6802 */
6803 if (sc->synhw.capPassthrough)
6804 synaptics_passthrough_on(sc);
6805
6806 if (send_aux_command(kbdc, 0xe1) != PSM_ACK ||
6807 read_aux_data(kbdc) != 0x01)
6808 goto no_trackpoint;
6809 id = read_aux_data(kbdc);
6810 if (id < 0x01)
6811 goto no_trackpoint;
6812 if (arg == PROBE)
6813 sc->tphw = id;
6814 if (!trackpoint_support)
6815 goto no_trackpoint;
6816
6817 if (sc->synhw.capPassthrough)
6818 synaptics_passthrough_off(sc);
6819
6820 if (arg == PROBE) {
6821 trackpoint_sysctl_create_tree(sc);
6822 /*
6823 * Don't overwrite hwid and buttons when we are
6824 * a guest device.
6825 */
6826 if (!sc->synhw.capPassthrough) {
6827 sc->hw.hwid = id;
6828 sc->hw.buttons = 3;
6829 }
6830 }
6831
6832 set_trackpoint_parameters(sc);
6833
6834 return (TRUE);
6835
6836no_trackpoint:
6837 if (sc->synhw.capPassthrough)
6838 synaptics_passthrough_off(sc);
6839
6840 return (FALSE);
6841}
6842
6843/* Interlink electronics VersaPad */
6844static int
6845enable_versapad(struct psm_softc *sc, enum probearg arg)
6846{
6847 KBDC kbdc = sc->kbdc;
6848 int data[3];
6849
6850 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
6851 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
6852 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6853 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6854 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6855 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6856 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
6857 return (FALSE);
6858 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
6859 return (FALSE);
6860 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
6861
6862 return (TRUE); /* PS/2 absolute mode */
6863}
6864
6865/* Elantech Touchpad */
6866static int
6867elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val)
6868{
6869 int res, readcmd, retidx;
6870 int resp[3];
6871
6872 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR;
6873 retidx = hwversion == 4 ? 1 : 0;
6874
6875 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6876 res |= send_aux_command(kbdc, readcmd) != PSM_ACK;
6877 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6878 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6879 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6880
6881 if (res == 0)
6882 *val = resp[retidx];
6883
6884 return (res);
6885}
6886
6887static int
6888elantech_write_1(KBDC kbdc, int hwversion, int reg, int val)
6889{
6890 int res, writecmd;
6891
6892 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR;
6893
6894 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6895 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6896 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6897 res |= send_aux_command(kbdc, reg) != PSM_ACK;
6898 if (hwversion == 4) {
6899 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6900 res |= send_aux_command(kbdc, writecmd) != PSM_ACK;
6901 }
6902 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6903 res |= send_aux_command(kbdc, val) != PSM_ACK;
6904 res |= set_mouse_scaling(kbdc, 1) == 0;
6905
6906 return (res);
6907}
6908
6909static int
6910elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp)
6911{
6912 int res;
6913
6914 if (hwversion == 2) {
6915 res = set_mouse_scaling(kbdc, 1) == 0;
6916 res |= mouse_ext_command(kbdc, cmd) == 0;
6917 } else {
6918 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK;
6919 res |= send_aux_command(kbdc, cmd) != PSM_ACK;
6920 }
6921 res |= get_mouse_status(kbdc, resp, 0, 3) != 3;
6922
6923 return (res);
6924}
6925
6926static int
6927elantech_init(KBDC kbdc, elantechhw_t *elanhw)
6928{
6929 int i, val, res, hwversion, reg10;
6930
6931 /* set absolute mode */
6932 hwversion = elanhw->hwversion;
6933 reg10 = -1;
6934 switch (hwversion) {
6935 case 2:
6936 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4;
6937 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6938 if (res)
6939 break;
6940 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A);
6941 break;
6942 case 3:
6943 reg10 = 0x0b;
6944 res = elantech_write_1(kbdc, hwversion, 0x10, reg10);
6945 break;
6946 case 4:
6947 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01);
6948 break;
6949 default:
6950 res = 1;
6951 }
6952
6953 /* Read back reg 0x10 to ensure hardware is ready. */
6954 if (res == 0 && reg10 >= 0) {
6955 for (i = 0; i < 5; i++) {
6956 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0)
6957 break;
6958 DELAY(2000);
6959 }
6960 if (i == 5)
6961 res = 1;
6962 }
6963
6964 if (res)
6965 printf("couldn't set absolute mode\n");
6966
6967 return (res);
6968}
6969
6970static void
6971elantech_init_synaptics(struct psm_softc *sc)
6972{
6973
6974 /* Set capabilites required by movement smother */
6975 sc->synhw.infoMajor = sc->elanhw.hwversion;
6976 sc->synhw.infoMinor = sc->elanhw.fwversion;
6977 sc->synhw.infoXupmm = sc->elanhw.dpmmx;
6978 sc->synhw.infoYupmm = sc->elanhw.dpmmy;
6979 sc->synhw.verticalScroll = 0;
6980 sc->synhw.nExtendedQueries = 4;
6981 sc->synhw.capExtended = 1;
6982 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint;
6983 sc->synhw.capClickPad = sc->elanhw.isclickpad;
6984 sc->synhw.capMultiFinger = 1;
6985 if (sc->elanhw.issemimt)
6986 sc->synhw.capAdvancedGestures = 1;
6987 else
6988 sc->synhw.capReportsV = 1;
6989 sc->synhw.capPalmDetect = 1;
6990 sc->synhw.capPen = 0;
6991 sc->synhw.capReportsMax = 1;
6992 sc->synhw.maximumXCoord = sc->elanhw.sizex;
6993 sc->synhw.maximumYCoord = sc->elanhw.sizey;
6994 sc->synhw.capReportsMin = 1;
6995 sc->synhw.minimumXCoord = 0;
6996 sc->synhw.minimumYCoord = 0;
6997
6998 if (sc->syninfo.sysctl_tree == NULL) {
6999 synaptics_sysctl_create_tree(sc, "elantech",
7000 "Elantech Touchpad");
7001
7002 /*
7003 * Adjust synaptic smoother tunables
7004 * 1. Disable finger detection pressure threshold. Unlike
7005 * synaptics we assume the finger is acting when packet with
7006 * its X&Y arrives not when pressure exceedes some threshold
7007 * 2. Disable unrelated features like margins and noisy areas
7008 * 3. Disable virtual scroll areas as 2nd finger is preferable
7009 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized
7010 * softbuttons
7011 * 5. Scale down divisors and movement lengths by a factor of 3
7012 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio
7013 */
7014
7015 /* Set reporting range to be equal touchpad size */
7016 sc->syninfo.max_x = sc->elanhw.sizex;
7017 sc->syninfo.max_y = sc->elanhw.sizey;
7018
7019 /* Disable finger detection pressure threshold */
7020 sc->syninfo.min_pressure = 1;
7021
7022 /* Adjust palm width to nearly match synaptics w=10 */
7023 sc->syninfo.max_width = 7;
7024
7025 /* Elans often report double & triple taps as single event */
7026 sc->syninfo.tap_min_queue = 1;
7027
7028 /* Use full area of touchpad */
7029 sc->syninfo.margin_top = 0;
7030 sc->syninfo.margin_right = 0;
7031 sc->syninfo.margin_bottom = 0;
7032 sc->syninfo.margin_left = 0;
7033
7034 /* Disable noisy area */
7035 sc->syninfo.na_top = 0;
7036 sc->syninfo.na_right = 0;
7037 sc->syninfo.na_bottom = 0;
7038 sc->syninfo.na_left = 0;
7039
7040 /* Tune divisors and movement lengths */
7041 sc->syninfo.weight_len_squared = 200;
7042 sc->syninfo.div_min = 3;
7043 sc->syninfo.div_max = 6;
7044 sc->syninfo.div_max_na = 10;
7045 sc->syninfo.div_len = 30;
7046 sc->syninfo.tap_max_delta = 25;
7047
7048 /* Disable virtual scrolling areas and tune its divisors */
7049 sc->syninfo.vscroll_hor_area = 0;
7050 sc->syninfo.vscroll_ver_area = 0;
7051 sc->syninfo.vscroll_min_delta = 15;
7052 sc->syninfo.vscroll_div_min = 30;
7053 sc->syninfo.vscroll_div_max = 50;
7054
7055 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */
7056 if (sc->elanhw.isclickpad) {
7057 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4;
7058 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25;
7059 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25;
7060 }
7061 }
7062
7063 return;
7064}
7065
7066static int
7067enable_elantech(struct psm_softc *sc, enum probearg arg)
7068{
7069 static const int ic2hw[] =
7070 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */
7071 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 };
7072 static const int fw_sizes[][3] = {
7073 /* FW.vers MaxX MaxY */
7074 { 0x020030, 1152, 768 },
7075 { 0x020800, 1152, 768 },
7076 { 0x020b00, 1152, 768 },
7077 { 0x040215, 900, 500 },
7078 { 0x040216, 819, 405 },
7079 { 0x040219, 900, 500 },
7080 };
7081 elantechhw_t elanhw;
7082 int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy;
7083 KBDC kbdc = sc->kbdc;
7084
7085 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n"));
7086
7087 set_mouse_scaling(kbdc, 1);
7088 set_mouse_scaling(kbdc, 1);
7089 set_mouse_scaling(kbdc, 1);
7090 if (get_mouse_status(kbdc, resp, 0, 3) != 3)
7091 return (FALSE);
7092
7093 if (!ELANTECH_MAGIC(resp))
7094 return (FALSE);
7095
7096 /* Identify the Touchpad version. */
7097 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp))
7098 return (FALSE);
7099
7100 bzero(&elanhw, sizeof(elanhw));
7101
7102 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2];
7103 icversion = resp[0] & 0x0f;
7104 hwversion = ic2hw[icversion];
7105
7106 if (verbose >= 2)
7107 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n",
7108 hwversion, elanhw.fwversion);
7109
7110 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) {
7111 printf (" Unsupported touchpad hardware (v1)\n");
7112 return (FALSE);
7113 }
7114 if (hwversion == 0) {
7115 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n",
7116 elanhw.fwversion);
7117 return (FALSE);
7118 }
7119
7120 /* Get the Touchpad model information. */
7121 elanhw.hwversion = hwversion;
7122 elanhw.issemimt = hwversion == 2;
7123 elanhw.isclickpad = (resp[1] & 0x10) != 0;
7124 elanhw.hascrc = (resp[1] & 0x40) != 0;
7125 elanhw.haspressure = elanhw.fwversion >= 0x020800;
7126
7127 /* Read the capability bits. */
7128 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) {
7129 printf(" Failed to read capability bits\n");
7130 return (FALSE);
7131 }
7132
7133 elanhw.ntracesx = imax(resp[1], 3);
7134 elanhw.ntracesy = imax(resp[2], 3);
7135 elanhw.hastrackpoint = (resp[0] & 0x80) != 0;
7136
7137 /* Get the touchpad resolution */
7138 switch (hwversion) {
7139 case 4:
7140 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp)
7141 == 0) {
7142 dpix = (resp[1] & 0x0f) * 10 + 790;
7143 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790;
7144 elanhw.dpmmx = (dpix * 10 + 5) / 254;
7145 elanhw.dpmmy = (dpiy * 10 + 5) / 254;
7146 break;
7147 }
7148 /* FALLTHROUGH */
7149 case 2:
7150 case 3:
7151 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */
7152 break;
7153 }
7154
7155 if (!elantech_support)
7156 return (FALSE);
7157
7158 if (elantech_init(kbdc, &elanhw)) {
7159 printf("couldn't initialize elantech touchpad\n");
7160 return (FALSE);
7161 }
7162
7163 /*
7164 * Get the touchpad reporting range.
7165 * On HW v.3 touchpads it should be done after switching hardware
7166 * to real resolution mode (by setting bit 3 of reg10)
7167 */
7168 elanhw.dptracex = elanhw.dptracey = 64;
7169 for (i = 0; i < nitems(fw_sizes); i++) {
7170 if (elanhw.fwversion == fw_sizes[i][0]) {
7171 elanhw.sizex = fw_sizes[i][1];
7172 elanhw.sizey = fw_sizes[i][2];
7173 goto found;
7174 }
7175 }
7176 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) {
7177 printf(" Failed to read touchpad size\n");
7178 elanhw.sizex = 10000; /* Arbitrary high values to */
7179 elanhw.sizey = 10000; /* prevent clipping in smoother */
7180 } else if (hwversion == 2) {
7181 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) &&
7182 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) {
7183 elanhw.dptracex = resp[1] / 2;
7184 elanhw.dptracey = resp[2] / 2;
7185 }
7186 xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2;
7187 elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex;
7188 elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey;
7189 } else {
7190 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1];
7191 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2];
7192 xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1;
7193 elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr);
7194 elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr);
7195 }
7196found:
7197 if (verbose >= 2) {
7198 printf(" Model information:\n");
7199 printf(" MaxX: %d\n", elanhw.sizex);
7200 printf(" MaxY: %d\n", elanhw.sizey);
7201 printf(" DpmmX: %d\n", elanhw.dpmmx);
7202 printf(" DpmmY: %d\n", elanhw.dpmmy);
7203 printf(" TracesX: %d\n", elanhw.ntracesx);
7204 printf(" TracesY: %d\n", elanhw.ntracesy);
7205 printf(" DptraceX: %d\n", elanhw.dptracex);
7206 printf(" DptraceY: %d\n", elanhw.dptracey);
7207 printf(" SemiMT: %d\n", elanhw.issemimt);
7208 printf(" Clickpad: %d\n", elanhw.isclickpad);
7209 printf(" Trackpoint: %d\n", elanhw.hastrackpoint);
7210 printf(" CRC: %d\n", elanhw.hascrc);
7211 printf(" Pressure: %d\n", elanhw.haspressure);
7212 }
7213
7214 VLOG(3, (LOG_DEBUG, "elantech: END init\n"));
7215
7216 if (arg == PROBE) {
7217 sc->elanhw = elanhw;
7218 sc->hw.buttons = 3;
7219
7220 /* Initialize synaptics movement smoother */
7221 elantech_init_synaptics(sc);
7222
7223 for (id = 0; id < ELANTECH_MAX_FINGERS; id++)
7224 PSM_FINGER_RESET(sc->elanaction.fingers[id]);
7225 }
7226
7227 return (TRUE);
7228}
7229
7230/*
7231 * Return true if 'now' is earlier than (start + (secs.usecs)).
7232 * Now may be NULL and the function will fetch the current time from
7233 * getmicrouptime(), or a cached 'now' can be passed in.
7234 * All values should be numbers derived from getmicrouptime().
7235 */
7236static int
7237timeelapsed(start, secs, usecs, now)
7238 const struct timeval *start, *now;
7239 int secs, usecs;
7240{
7241 struct timeval snow, tv;
7242
7243 /* if there is no 'now' passed in, the get it as a convience. */
7244 if (now == NULL) {
7245 getmicrouptime(&snow);
7246 now = &snow;
7247 }
7248
7249 tv.tv_sec = secs;
7250 tv.tv_usec = usecs;
7251 timevaladd(&tv, start);
7252 return (timevalcmp(&tv, now, <));
7253}
7254
7255static int
7256psmresume(device_t dev)
7257{
7258 struct psm_softc *sc = device_get_softc(dev);
7259 int unit = device_get_unit(dev);
7260 int err;
7261
7262 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit));
7263
7264 if ((sc->config &
7265 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0)
7266 return (0);
7267
7268 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
7269
7270 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
7271 /*
7272 * Release the blocked process; it must be notified that
7273 * the device cannot be accessed anymore.
7274 */
7275 sc->state &= ~PSM_ASLP;
7276 wakeup(sc);
7277 }
7278
7279 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit));
7280
7281 return (err);
7282}
7283
7284DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
7285#ifdef EVDEV_SUPPORT
7286MODULE_DEPEND(psm, evdev, 1, 1, 1);
7287#endif
7288
7289#ifdef DEV_ISA
7290
7291/*
7292 * This sucks up assignments from PNPBIOS and ACPI.
7293 */
7294
7295/*
7296 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
7297 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
7298 * can be probed and attached only after the AT keyboard controller is
7299 * attached, we shall quietly reserve the IRQ resource for later use.
7300 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
7301 * copy the IRQ resource to the PS/2 mouse device instance hanging
7302 * under the keyboard controller, then probe and attach it.
7303 */
7304
7305static devclass_t psmcpnp_devclass;
7306
7307static device_probe_t psmcpnp_probe;
7308static device_attach_t psmcpnp_attach;
7309
7310static device_method_t psmcpnp_methods[] = {
7311 DEVMETHOD(device_probe, psmcpnp_probe),
7312 DEVMETHOD(device_attach, psmcpnp_attach),
7313
7314 { 0, 0 }
7315};
7316
7317static driver_t psmcpnp_driver = {
7318 PSMCPNP_DRIVER_NAME,
7319 psmcpnp_methods,
7320 sizeof(struct psmcpnp_softc),
7321};
7322
7323static struct isa_pnp_id psmcpnp_ids[] = {
7324 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
7325 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */
7326 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */
7327 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
7328 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
7329 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
7330 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */
7331 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
7332 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
7333 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
7334 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
7335 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
7336 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
7337 { 0 }
7338};
7339
7340/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */
7341static struct isa_pnp_id forcepad_ids[] = {
7342 { 0x0d302e4f, "HP PS/2 forcepad port" }, /* SYN300D, EB 1040 */
7343 { 0x14302e4f, "HP PS/2 forcepad port" }, /* SYN3014, EB 1040 */
7344 { 0 }
7345};
7346
7214/* List of HW v8.1 synaptics touchpads erroneously detected as HW v2.0 */
7215static struct isa_pnp_id hpsyn81_ids[] = {
7216 { 0x9e012e4f, "HP PS/2 trackpad port" }, /* SYN019E, EB 9470 */
7217 { 0 }
7218};
7219
7220static int
7221create_a_copy(device_t atkbdc, device_t me)
7222{
7223 device_t psm;
7224 u_long irq;
7225
7226 /* find the PS/2 mouse device instance under the keyboard controller */
7227 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
7228 device_get_unit(atkbdc));
7229 if (psm == NULL)
7230 return (ENXIO);
7231 if (device_get_state(psm) != DS_NOTPRESENT)
7232 return (0);
7233
7234 /* move our resource to the found device */
7235 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
7236 bus_delete_resource(me, SYS_RES_IRQ, 0);
7237 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
7238
7239 /* ...then probe and attach it */
7240 return (device_probe_and_attach(psm));
7241}
7242
7243static int
7244psmcpnp_probe(device_t dev)
7245{
7246 struct psmcpnp_softc *sc = device_get_softc(dev);
7247 struct resource *res;
7248 u_long irq;
7249 int rid;
7250
7251 if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0)
7252 sc->type = PSMCPNP_FORCEPAD;
7347static int
7348create_a_copy(device_t atkbdc, device_t me)
7349{
7350 device_t psm;
7351 u_long irq;
7352
7353 /* find the PS/2 mouse device instance under the keyboard controller */
7354 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
7355 device_get_unit(atkbdc));
7356 if (psm == NULL)
7357 return (ENXIO);
7358 if (device_get_state(psm) != DS_NOTPRESENT)
7359 return (0);
7360
7361 /* move our resource to the found device */
7362 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
7363 bus_delete_resource(me, SYS_RES_IRQ, 0);
7364 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
7365
7366 /* ...then probe and attach it */
7367 return (device_probe_and_attach(psm));
7368}
7369
7370static int
7371psmcpnp_probe(device_t dev)
7372{
7373 struct psmcpnp_softc *sc = device_get_softc(dev);
7374 struct resource *res;
7375 u_long irq;
7376 int rid;
7377
7378 if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0)
7379 sc->type = PSMCPNP_FORCEPAD;
7253 else if(ISA_PNP_PROBE(device_get_parent(dev), dev, hpsyn81_ids) == 0)
7254 sc->type = PSMCPNP_HPSYN81;
7255 else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0)
7256 sc->type = PSMCPNP_GENERIC;
7257 else
7258 return (ENXIO);
7259
7260 /*
7261 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
7262 * to the PS/2 mouse device node. But, some buggy PnP BIOS
7263 * declares the PS/2 mouse device node without an IRQ resource!
7264 * If this happens, we shall refer to device hints.
7265 * If we still don't find it there, use a hardcoded value... XXX
7266 */
7267 rid = 0;
7268 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
7269 if (irq <= 0) {
7270 if (resource_long_value(PSM_DRIVER_NAME,
7271 device_get_unit(dev),"irq", &irq) != 0)
7272 irq = 12; /* XXX */
7273 device_printf(dev, "irq resource info is missing; "
7274 "assuming irq %ld\n", irq);
7275 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
7276 }
7277 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
7278 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
7279
7280 /* keep quiet */
7281 if (!bootverbose)
7282 device_quiet(dev);
7283
7284 return ((res == NULL) ? ENXIO : 0);
7285}
7286
7287static int
7288psmcpnp_attach(device_t dev)
7289{
7290 device_t atkbdc;
7291
7292 /* find the keyboard controller, which may be on acpi* or isa* bus */
7293 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
7294 device_get_unit(dev));
7295 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
7296 create_a_copy(atkbdc, dev);
7297
7298 return (0);
7299}
7300
7301DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7302DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7303
7304#endif /* DEV_ISA */
7380 else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0)
7381 sc->type = PSMCPNP_GENERIC;
7382 else
7383 return (ENXIO);
7384
7385 /*
7386 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
7387 * to the PS/2 mouse device node. But, some buggy PnP BIOS
7388 * declares the PS/2 mouse device node without an IRQ resource!
7389 * If this happens, we shall refer to device hints.
7390 * If we still don't find it there, use a hardcoded value... XXX
7391 */
7392 rid = 0;
7393 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
7394 if (irq <= 0) {
7395 if (resource_long_value(PSM_DRIVER_NAME,
7396 device_get_unit(dev),"irq", &irq) != 0)
7397 irq = 12; /* XXX */
7398 device_printf(dev, "irq resource info is missing; "
7399 "assuming irq %ld\n", irq);
7400 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
7401 }
7402 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0);
7403 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
7404
7405 /* keep quiet */
7406 if (!bootverbose)
7407 device_quiet(dev);
7408
7409 return ((res == NULL) ? ENXIO : 0);
7410}
7411
7412static int
7413psmcpnp_attach(device_t dev)
7414{
7415 device_t atkbdc;
7416
7417 /* find the keyboard controller, which may be on acpi* or isa* bus */
7418 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
7419 device_get_unit(dev));
7420 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED))
7421 create_a_copy(atkbdc, dev);
7422
7423 return (0);
7424}
7425
7426DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7427DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
7428
7429#endif /* DEV_ISA */