1 /* $NetBSD: synaptics.c,v 1.20 2008/03/15 18:46:22 cube Exp $ */
4 * Copyright (c) 2005, Steve C. Woodford
5 * Copyright (c) 2004, Ales Krenek
6 * Copyright (c) 2004, Kentaro A. Kurahone
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the authors nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
40 * - Make the sysctl values per-instance instead of global.
41 * - Consider setting initial scaling factors at runtime according
42 * to the values returned by the 'Read Resolutions' command.
43 * - Support the serial protocol (we only support PS/2 for now)
44 * - Support auto-repeat for up/down button Z-axis emulation.
45 * - Maybe add some more gestures (can we use Palm support somehow?)
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.20 2008/03/15 18:46:22 cube Exp $");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/sysctl.h>
58 #include <sys/kernel.h>
62 #include <dev/pckbport/pckbportvar.h>
64 #include <dev/pckbport/synapticsreg.h>
65 #include <dev/pckbport/synapticsvar.h>
67 #include <dev/pckbport/pmsreg.h>
68 #include <dev/pckbport/pmsvar.h>
70 #include <dev/wscons/wsconsio.h>
71 #include <dev/wscons/wsmousevar.h>
74 * Absolute-mode packets are decoded and passed around using
75 * the following structure.
77 struct synaptics_packet
{
78 signed short sp_x
; /* Unscaled absolute X/Y coordinates */
80 u_char sp_z
; /* Z (pressure) */
81 u_char sp_w
; /* W (contact patch width) */
82 char sp_left
; /* Left mouse button status */
83 char sp_right
; /* Right mouse button status */
84 char sp_middle
; /* Middle button status (possibly emulated) */
85 char sp_up
; /* Up button status */
86 char sp_down
; /* Down button status */
89 static int pms_synaptics_send_command(pckbport_tag_t
, pckbport_slot_t
, u_char
);
90 static void pms_synaptics_input(void *, int);
91 static void pms_synaptics_process_packet(struct pms_softc
*,
92 struct synaptics_packet
*);
93 static void pms_sysctl_synaptics(struct sysctllog
**);
94 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS
);
96 /* Controled by sysctl. */
97 static int synaptics_up_down_emul
= 2;
98 static int synaptics_up_down_motion_delta
= 1;
99 static int synaptics_gesture_move
= 200;
100 static int synaptics_gesture_length
= 20;
101 static int synaptics_edge_left
= SYNAPTICS_EDGE_LEFT
;
102 static int synaptics_edge_right
= SYNAPTICS_EDGE_RIGHT
;
103 static int synaptics_edge_top
= SYNAPTICS_EDGE_TOP
;
104 static int synaptics_edge_bottom
= SYNAPTICS_EDGE_BOTTOM
;
105 static int synaptics_edge_motion_delta
= 32;
106 static u_int synaptics_finger_high
= SYNAPTICS_FINGER_LIGHT
+ 5;
107 static u_int synaptics_finger_low
= SYNAPTICS_FINGER_LIGHT
- 10;
108 static int synaptics_two_fingers_emul
= 0;
109 static int synaptics_scale_x
= 16;
110 static int synaptics_scale_y
= 16;
111 static int synaptics_max_speed_x
= 32;
112 static int synaptics_max_speed_y
= 32;
113 static int synaptics_movement_threshold
= 4;
116 static int synaptics_up_down_emul_nodenum
;
117 static int synaptics_up_down_motion_delta_nodenum
;
118 static int synaptics_gesture_move_nodenum
;
119 static int synaptics_gesture_length_nodenum
;
120 static int synaptics_edge_left_nodenum
;
121 static int synaptics_edge_right_nodenum
;
122 static int synaptics_edge_top_nodenum
;
123 static int synaptics_edge_bottom_nodenum
;
124 static int synaptics_edge_motion_delta_nodenum
;
125 static int synaptics_finger_high_nodenum
;
126 static int synaptics_finger_low_nodenum
;
127 static int synaptics_two_fingers_emul_nodenum
;
128 static int synaptics_scale_x_nodenum
;
129 static int synaptics_scale_y_nodenum
;
130 static int synaptics_max_speed_x_nodenum
;
131 static int synaptics_max_speed_y_nodenum
;
132 static int synaptics_movement_threshold_nodenum
;
135 pms_synaptics_probe_init(void *vsc
)
137 struct pms_softc
*psc
= vsc
;
138 struct synaptics_softc
*sc
= &psc
->u
.synaptics
;
139 u_char cmd
[2], resp
[3];
140 int res
, ver_minor
, ver_major
;
141 struct sysctllog
*clog
= NULL
;
143 res
= pms_synaptics_send_command(psc
->sc_kbctag
, psc
->sc_kbcslot
,
144 SYNAPTICS_IDENTIFY_TOUCHPAD
);
145 cmd
[0] = PMS_SEND_DEV_STATUS
;
146 res
|= pckbport_poll_cmd(psc
->sc_kbctag
, psc
->sc_kbcslot
, cmd
, 1, 3,
149 #ifdef SYNAPTICSDEBUG
150 aprint_normal_dev(psc
->sc_dev
,
151 "synaptics_probe: Identify Touchpad error.\n");
154 * Reset device in case the probe confused it.
158 (void) pckbport_poll_cmd(psc
->sc_kbctag
, psc
->sc_kbcslot
, cmd
,
163 if (resp
[1] != SYNAPTICS_MAGIC_BYTE
) {
164 #ifdef SYNAPTICSDEBUG
165 aprint_normal_dev(psc
->sc_dev
,
166 "synaptics_probe: Not synaptics.\n");
174 /* Check for minimum version and print a nice message. */
175 ver_major
= resp
[2] & 0x0f;
177 aprint_normal_dev(psc
->sc_dev
, "Synaptics touchpad version %d.%d\n",
178 ver_major
, ver_minor
);
179 if (ver_major
* 10 + ver_minor
< SYNAPTICS_MIN_VERSION
) {
180 /* No capability query support. */
185 /* Query the hardware capabilities. */
186 res
= pms_synaptics_send_command(psc
->sc_kbctag
, psc
->sc_kbcslot
,
187 SYNAPTICS_READ_CAPABILITIES
);
188 cmd
[0] = PMS_SEND_DEV_STATUS
;
189 res
|= pckbport_poll_cmd(psc
->sc_kbctag
, psc
->sc_kbcslot
, cmd
, 1, 3,
192 /* Hmm, failed to get capabilites. */
193 aprint_error_dev(psc
->sc_dev
,
194 "synaptics_probe: Failed to query capabilities.\n");
198 sc
->caps
= (resp
[0] << 8) | resp
[2];
200 if (sc
->caps
& SYNAPTICS_CAP_MBUTTON
)
201 sc
->flags
|= SYN_FLAG_HAS_MIDDLE_BUTTON
;
203 if (sc
->caps
& SYNAPTICS_CAP_4BUTTON
)
204 sc
->flags
|= SYN_FLAG_HAS_BUTTONS_4_5
;
206 if (sc
->caps
& SYNAPTICS_CAP_EXTENDED
) {
207 #ifdef SYNAPTICSDEBUG
208 aprint_normal_dev(psc
->sc_dev
,
209 "synaptics_probe: Capabilities 0x%04x.\n", sc
->caps
);
211 if (sc
->caps
& SYNAPTICS_CAP_PASSTHROUGH
)
212 sc
->flags
|= SYN_FLAG_HAS_PASSTHROUGH
;
214 if (sc
->caps
& SYNAPTICS_CAP_PALMDETECT
)
215 sc
->flags
|= SYN_FLAG_HAS_PALM_DETECT
;
217 if (sc
->caps
& SYNAPTICS_CAP_MULTIDETECT
)
218 sc
->flags
|= SYN_FLAG_HAS_MULTI_FINGER
;
220 /* Ask about extra buttons to detect up/down. */
221 if (sc
->caps
& SYNAPTICS_CAP_EXTNUM
) {
222 res
= pms_synaptics_send_command(psc
->sc_kbctag
,
223 psc
->sc_kbcslot
, SYNAPTICS_EXTENDED_QUERY
);
224 cmd
[0] = PMS_SEND_DEV_STATUS
;
225 res
|= pckbport_poll_cmd(psc
->sc_kbctag
,
226 psc
->sc_kbcslot
, cmd
, 1, 3, resp
, 0);
227 #ifdef SYNAPTICSDEBUG
229 aprint_normal_dev(psc
->sc_dev
,
230 "synaptics_probe: Extended "
231 "Capabilities 0x%02x.\n", resp
[1]);
233 if (!res
&& (resp
[1] >> 4) >= 2) {
235 sc
->flags
|= SYN_FLAG_HAS_UP_DOWN_BUTTONS
;
241 const char comma
[] = ", ";
242 const char *sep
= "";
243 aprint_normal_dev(psc
->sc_dev
, "");
244 if (sc
->flags
& SYN_FLAG_HAS_MIDDLE_BUTTON
) {
245 aprint_normal("%sMiddle button", sep
);
248 if (sc
->flags
& SYN_FLAG_HAS_BUTTONS_4_5
) {
249 aprint_normal("%sButtons 4/5", sep
);
252 if (sc
->flags
& SYN_FLAG_HAS_UP_DOWN_BUTTONS
) {
253 aprint_normal("%sUp/down buttons", sep
);
256 if (sc
->flags
& SYN_FLAG_HAS_PALM_DETECT
) {
257 aprint_normal("%sPalm detect", sep
);
260 if (sc
->flags
& SYN_FLAG_HAS_MULTI_FINGER
)
261 aprint_normal("%sMulti-finger", sep
);
267 pms_sysctl_synaptics(&clog
);
268 pckbport_set_inputhandler(psc
->sc_kbctag
, psc
->sc_kbcslot
,
269 pms_synaptics_input
, psc
, device_xname(psc
->sc_dev
));
275 pms_synaptics_enable(void *vsc
)
277 struct pms_softc
*psc
= vsc
;
278 struct synaptics_softc
*sc
= &psc
->u
.synaptics
;
283 * Enable Absolute mode with W (width) reporting, and set
284 * the packet rate to maximum (80 packets per second).
286 res
= pms_synaptics_send_command(psc
->sc_kbctag
, psc
->sc_kbcslot
,
287 SYNAPTICS_MODE_ABSOLUTE
| SYNAPTICS_MODE_W
| SYNAPTICS_MODE_RATE
);
288 cmd
[0] = PMS_SET_SAMPLE
;
289 cmd
[1] = 0x14; /* doit */
290 res
|= pckbport_enqueue_cmd(psc
->sc_kbctag
, psc
->sc_kbcslot
, cmd
, 2, 0,
293 sc
->prev_fingers
= 0;
294 sc
->gesture_start_x
= sc
->gesture_start_y
= 0;
295 sc
->gesture_start_packet
= 0;
296 sc
->gesture_tap_packet
= 0;
297 sc
->gesture_type
= 0;
298 sc
->gesture_buttons
= 0;
299 sc
->rem_x
= sc
->rem_y
= 0;
300 sc
->movement_history
= 0;
302 aprint_error_dev(psc
->sc_dev
,
303 "synaptics_enable: Error enabling device.\n");
308 pms_synaptics_resume(void *vsc
)
310 struct pms_softc
*psc
= vsc
;
311 unsigned char cmd
[1],resp
[2] = { 0,0 };
315 res
= pckbport_poll_cmd(psc
->sc_kbctag
, psc
->sc_kbcslot
, cmd
, 1, 2,
317 aprint_debug_dev(psc
->sc_dev
,
318 "pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n",
319 res
, resp
[0], resp
[1]);
323 pms_sysctl_synaptics(struct sysctllog
**clog
)
326 const struct sysctlnode
*node
;
328 if ((rc
= sysctl_createv(clog
, 0, NULL
, NULL
,
329 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "hw", NULL
,
330 NULL
, 0, NULL
, 0, CTL_HW
, CTL_EOL
)) != 0)
333 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
334 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "synaptics",
335 SYSCTL_DESCR("Synaptics touchpad controls"),
336 NULL
, 0, NULL
, 0, CTL_HW
, CTL_CREATE
, CTL_EOL
)) != 0)
339 root_num
= node
->sysctl_num
;
341 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
342 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
343 CTLTYPE_INT
, "up_down_emulation",
344 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
345 pms_sysctl_synaptics_verify
, 0,
346 &synaptics_up_down_emul
,
347 0, CTL_HW
, root_num
, CTL_CREATE
,
351 synaptics_up_down_emul_nodenum
= node
->sysctl_num
;
353 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
354 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
355 CTLTYPE_INT
, "up_down_motion_delta",
356 SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
357 pms_sysctl_synaptics_verify
, 0,
358 &synaptics_up_down_motion_delta
,
359 0, CTL_HW
, root_num
, CTL_CREATE
,
363 synaptics_up_down_motion_delta_nodenum
= node
->sysctl_num
;
365 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
366 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
367 CTLTYPE_INT
, "gesture_move",
368 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
369 pms_sysctl_synaptics_verify
, 0,
370 &synaptics_gesture_move
,
371 0, CTL_HW
, root_num
, CTL_CREATE
,
375 synaptics_gesture_move_nodenum
= node
->sysctl_num
;
377 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
378 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
379 CTLTYPE_INT
, "gesture_length",
380 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
381 pms_sysctl_synaptics_verify
, 0,
382 &synaptics_gesture_length
,
383 0, CTL_HW
, root_num
, CTL_CREATE
,
387 synaptics_gesture_length_nodenum
= node
->sysctl_num
;
389 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
390 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
391 CTLTYPE_INT
, "edge_left",
392 SYSCTL_DESCR("Define left edge of touchpad"),
393 pms_sysctl_synaptics_verify
, 0,
394 &synaptics_edge_left
,
395 0, CTL_HW
, root_num
, CTL_CREATE
,
399 synaptics_edge_left_nodenum
= node
->sysctl_num
;
401 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
402 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
403 CTLTYPE_INT
, "edge_right",
404 SYSCTL_DESCR("Define right edge of touchpad"),
405 pms_sysctl_synaptics_verify
, 0,
406 &synaptics_edge_right
,
407 0, CTL_HW
, root_num
, CTL_CREATE
,
411 synaptics_edge_right_nodenum
= node
->sysctl_num
;
413 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
414 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
415 CTLTYPE_INT
, "edge_top",
416 SYSCTL_DESCR("Define top edge of touchpad"),
417 pms_sysctl_synaptics_verify
, 0,
419 0, CTL_HW
, root_num
, CTL_CREATE
,
423 synaptics_edge_top_nodenum
= node
->sysctl_num
;
425 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
426 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
427 CTLTYPE_INT
, "edge_bottom",
428 SYSCTL_DESCR("Define bottom edge of touchpad"),
429 pms_sysctl_synaptics_verify
, 0,
430 &synaptics_edge_bottom
,
431 0, CTL_HW
, root_num
, CTL_CREATE
,
435 synaptics_edge_bottom_nodenum
= node
->sysctl_num
;
437 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
438 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
439 CTLTYPE_INT
, "edge_motion_delta",
440 SYSCTL_DESCR("Define edge motion rate"),
441 pms_sysctl_synaptics_verify
, 0,
442 &synaptics_edge_motion_delta
,
443 0, CTL_HW
, root_num
, CTL_CREATE
,
447 synaptics_edge_motion_delta_nodenum
= node
->sysctl_num
;
449 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
450 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
451 CTLTYPE_INT
, "finger_high",
452 SYSCTL_DESCR("Define finger applied pressure threshold"),
453 pms_sysctl_synaptics_verify
, 0,
454 &synaptics_finger_high
,
455 0, CTL_HW
, root_num
, CTL_CREATE
,
459 synaptics_finger_high_nodenum
= node
->sysctl_num
;
461 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
462 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
463 CTLTYPE_INT
, "finger_low",
464 SYSCTL_DESCR("Define finger removed pressure threshold"),
465 pms_sysctl_synaptics_verify
, 0,
466 &synaptics_finger_low
,
467 0, CTL_HW
, root_num
, CTL_CREATE
,
471 synaptics_finger_low_nodenum
= node
->sysctl_num
;
473 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
474 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
475 CTLTYPE_INT
, "two_fingers_emulation",
476 SYSCTL_DESCR("Map two fingers to middle button"),
477 pms_sysctl_synaptics_verify
, 0,
478 &synaptics_two_fingers_emul
,
479 0, CTL_HW
, root_num
, CTL_CREATE
,
483 synaptics_two_fingers_emul_nodenum
= node
->sysctl_num
;
485 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
486 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
487 CTLTYPE_INT
, "scale_x",
488 SYSCTL_DESCR("Horizontal movement scale factor"),
489 pms_sysctl_synaptics_verify
, 0,
491 0, CTL_HW
, root_num
, CTL_CREATE
,
495 synaptics_scale_x_nodenum
= node
->sysctl_num
;
497 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
498 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
499 CTLTYPE_INT
, "scale_y",
500 SYSCTL_DESCR("Vertical movement scale factor"),
501 pms_sysctl_synaptics_verify
, 0,
503 0, CTL_HW
, root_num
, CTL_CREATE
,
507 synaptics_scale_y_nodenum
= node
->sysctl_num
;
509 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
510 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
511 CTLTYPE_INT
, "max_speed_x",
512 SYSCTL_DESCR("Horizontal movement maximum speed"),
513 pms_sysctl_synaptics_verify
, 0,
514 &synaptics_max_speed_x
,
515 0, CTL_HW
, root_num
, CTL_CREATE
,
519 synaptics_max_speed_x_nodenum
= node
->sysctl_num
;
521 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
522 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
523 CTLTYPE_INT
, "max_speed_y",
524 SYSCTL_DESCR("Vertical movement maximum speed"),
525 pms_sysctl_synaptics_verify
, 0,
526 &synaptics_max_speed_y
,
527 0, CTL_HW
, root_num
, CTL_CREATE
,
531 synaptics_max_speed_y_nodenum
= node
->sysctl_num
;
533 if ((rc
= sysctl_createv(clog
, 0, NULL
, &node
,
534 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
535 CTLTYPE_INT
, "movement_threshold",
536 SYSCTL_DESCR("Minimum reported movement threshold"),
537 pms_sysctl_synaptics_verify
, 0,
538 &synaptics_movement_threshold
,
539 0, CTL_HW
, root_num
, CTL_CREATE
,
543 synaptics_movement_threshold_nodenum
= node
->sysctl_num
;
547 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__
, rc
);
551 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS
)
554 struct sysctlnode node
;
557 t
= *(int *)rnode
->sysctl_data
;
558 node
.sysctl_data
= &t
;
559 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
560 if (error
|| newp
== NULL
)
563 /* Sanity check the params. */
564 if (node
.sysctl_num
== synaptics_up_down_emul_nodenum
||
565 node
.sysctl_num
== synaptics_two_fingers_emul_nodenum
) {
569 if (node
.sysctl_num
== synaptics_gesture_length_nodenum
||
570 node
.sysctl_num
== synaptics_edge_motion_delta_nodenum
||
571 node
.sysctl_num
== synaptics_up_down_motion_delta_nodenum
) {
575 if (node
.sysctl_num
== synaptics_edge_left_nodenum
||
576 node
.sysctl_num
== synaptics_edge_bottom_nodenum
) {
577 if (t
< 0 || t
> (SYNAPTICS_EDGE_MAX
/ 2))
580 if (node
.sysctl_num
== synaptics_edge_right_nodenum
||
581 node
.sysctl_num
== synaptics_edge_top_nodenum
) {
582 if (t
< (SYNAPTICS_EDGE_MAX
/ 2))
585 if (node
.sysctl_num
== synaptics_scale_x_nodenum
||
586 node
.sysctl_num
== synaptics_scale_y_nodenum
) {
587 if (t
< 1 || t
> (SYNAPTICS_EDGE_MAX
/ 4))
590 if (node
.sysctl_num
== synaptics_finger_high_nodenum
) {
591 if (t
< 0 || t
> SYNAPTICS_FINGER_PALM
||
592 t
< synaptics_finger_low
)
595 if (node
.sysctl_num
== synaptics_finger_low_nodenum
) {
596 if (t
< 0 || t
> SYNAPTICS_FINGER_PALM
||
597 t
> synaptics_finger_high
)
600 if (node
.sysctl_num
== synaptics_gesture_move_nodenum
||
601 node
.sysctl_num
== synaptics_movement_threshold_nodenum
) {
602 if (t
< 0 || t
> (SYNAPTICS_EDGE_MAX
/ 4))
607 *(int *)rnode
->sysctl_data
= t
;
613 pms_synaptics_send_command(pckbport_tag_t tag
, pckbport_slot_t slot
,
620 * Need to send 4 Set Resolution commands, with the argument
621 * encoded in the bottom most 2 bits.
623 cmd
[0] = PMS_SET_RES
;
624 cmd
[1] = syn_cmd
>> 6;
625 res
= pckbport_poll_cmd(tag
, slot
, cmd
, 2, 0, NULL
, 0);
627 cmd
[0] = PMS_SET_RES
;
628 cmd
[1] = (syn_cmd
& 0x30) >> 4;
629 res
|= pckbport_poll_cmd(tag
, slot
, cmd
, 2, 0, NULL
, 0);
631 cmd
[0] = PMS_SET_RES
;
632 cmd
[1] = (syn_cmd
& 0x0c) >> 2;
633 res
|= pckbport_poll_cmd(tag
, slot
, cmd
, 2, 0, NULL
, 0);
635 cmd
[0] = PMS_SET_RES
;
636 cmd
[1] = (syn_cmd
& 0x03);
637 res
|= pckbport_poll_cmd(tag
, slot
, cmd
, 2, 0, NULL
, 0);
642 /* Masks for the first byte of a packet */
643 #define PMS_LBUTMASK 0x01
644 #define PMS_RBUTMASK 0x02
645 #define PMS_MBUTMASK 0x04
648 pms_synaptics_parse(struct pms_softc
*psc
)
650 struct synaptics_softc
*sc
= &psc
->u
.synaptics
;
651 struct synaptics_packet sp
;
653 /* Absolute X/Y coordinates of finger */
654 sp
.sp_x
= psc
->packet
[4] + ((psc
->packet
[1] & 0x0f) << 8) +
655 ((psc
->packet
[3] & 0x10) << 8);
656 sp
.sp_y
= psc
->packet
[5] + ((psc
->packet
[1] & 0xf0) << 4) +
657 ((psc
->packet
[3] & 0x20) << 7);
660 sp
.sp_z
= psc
->packet
[2];
662 /* Width of finger */
663 sp
.sp_w
= ((psc
->packet
[0] & 0x30) >> 2) +
664 ((psc
->packet
[0] & 0x04) >> 1) +
665 ((psc
->packet
[3] & 0x04) >> 2);
667 /* Left/Right button handling. */
668 sp
.sp_left
= psc
->packet
[0] & PMS_LBUTMASK
;
669 sp
.sp_right
= psc
->packet
[0] & PMS_RBUTMASK
;
671 /* Up/Down buttons. */
672 if (sc
->flags
& SYN_FLAG_HAS_BUTTONS_4_5
) {
673 /* Old up/down buttons. */
674 sp
.sp_up
= sp
.sp_left
^
675 (psc
->packet
[3] & PMS_LBUTMASK
);
676 sp
.sp_down
= sp
.sp_right
^
677 (psc
->packet
[3] & PMS_RBUTMASK
);
679 if (sc
->flags
& SYN_FLAG_HAS_UP_DOWN_BUTTONS
&&
680 ((psc
->packet
[0] & PMS_RBUTMASK
) ^
681 (psc
->packet
[3] & PMS_RBUTMASK
))) {
682 /* New up/down button. */
683 sp
.sp_up
= psc
->packet
[4] & SYN_1BUTMASK
;
684 sp
.sp_down
= psc
->packet
[5] & SYN_2BUTMASK
;
691 if (sc
->flags
& SYN_FLAG_HAS_MIDDLE_BUTTON
) {
692 /* Old style Middle Button. */
693 sp
.sp_middle
= (psc
->packet
[0] & PMS_LBUTMASK
) ^
694 (psc
->packet
[3] & PMS_LBUTMASK
);
696 if (synaptics_up_down_emul
== 1) {
697 /* Do middle button emulation using up/down buttons */
698 sp
.sp_middle
= sp
.sp_up
| sp
.sp_down
;
699 sp
.sp_up
= sp
.sp_down
= 0;
703 pms_synaptics_process_packet(psc
, &sp
);
707 pms_synaptics_passthrough(struct pms_softc
*psc
)
710 int buttons
, changed
;
713 buttons
= ((psc
->packet
[1] & PMS_LBUTMASK
) ? 0x20 : 0) |
714 ((psc
->packet
[1] & PMS_MBUTMASK
) ? 0x40 : 0) |
715 ((psc
->packet
[1] & PMS_RBUTMASK
) ? 0x80 : 0);
731 changed
= buttons
^ (psc
->buttons
& 0xe0);
732 psc
->buttons
^= changed
;
734 if (dx
|| dy
|| dz
|| changed
) {
735 buttons
= (psc
->buttons
& 0x1f) | ((psc
->buttons
>> 5) & 0x7);
737 wsmouse_input(psc
->sc_wsmousedev
,
738 buttons
, dx
, dy
, dz
, 0,
739 WSMOUSE_INPUT_DELTA
);
745 pms_synaptics_input(void *vsc
, int data
)
747 struct pms_softc
*psc
= vsc
;
750 if (!psc
->sc_enabled
) {
751 /* Interrupts are not expected. Discard the byte. */
755 getmicrouptime(&psc
->current
);
757 if (psc
->inputstate
> 0) {
758 timersub(&psc
->current
, &psc
->last
, &diff
);
759 if (diff
.tv_sec
> 0 || diff
.tv_usec
>= 40000) {
760 aprint_debug_dev(psc
->sc_dev
,
761 "pms_input: unusual delay (%ld.%06ld s), "
762 "scheduling reset\n",
763 (long)diff
.tv_sec
, (long)diff
.tv_usec
);
766 wakeup(&psc
->sc_enabled
);
770 psc
->last
= psc
->current
;
772 switch (psc
->inputstate
) {
774 if ((data
& 0xc8) != 0x80) {
775 #ifdef SYNAPTICSDEBUG
776 aprint_normal_dev(psc
->sc_dev
,
777 "pms_input: 0x%02x out of sync\n", data
);
779 return; /* not in sync yet, discard input */
784 if ((data
& 8) == 8) {
785 #ifdef SYNAPTICSDEBUG
786 aprint_normal_dev(psc
->sc_dev
,
787 "pms_input: dropped in relative mode, reset\n");
791 wakeup(&psc
->sc_enabled
);
796 psc
->packet
[psc
->inputstate
++] = data
& 0xff;
797 if (psc
->inputstate
== 6) {
799 * We have a complete packet.
800 * Extract the pertinent details.
804 if ((psc
->packet
[0] & 0xfc) == 0x84 &&
805 (psc
->packet
[3] & 0xcc) == 0xc4) {
806 /* PS/2 passthrough */
807 pms_synaptics_passthrough(psc
);
809 pms_synaptics_parse(psc
);
815 synaptics_finger_detect(struct synaptics_softc
*sc
, struct synaptics_packet
*sp
,
824 * Apply some hysteresis when checking for a finger.
825 * When the finger is first applied, we ignore it until the
826 * pressure exceeds the 'high' threshold. The finger is considered
827 * removed only when pressure falls beneath the 'low' threshold.
829 if ((sc
->prev_fingers
== 0 && sp
->sp_z
> synaptics_finger_high
) ||
830 (sc
->prev_fingers
!= 0 && sp
->sp_z
> synaptics_finger_low
))
836 * If the pad can't do palm detection, skip the rest.
838 if (fingers
== 0 || (sc
->flags
& SYN_FLAG_HAS_PALM_DETECT
) == 0)
844 if (sp
->sp_z
> SYNAPTICS_FINGER_FLAT
&&
845 sp
->sp_w
>= SYNAPTICS_WIDTH_PALM_MIN
)
848 if (sc
->prev_fingers
== 0 &&
849 (sp
->sp_z
> SYNAPTICS_FINGER_FLAT
||
850 sp
->sp_w
>= SYNAPTICS_WIDTH_PALM_MIN
)) {
852 * Contact area or pressure is too great to be a finger.
853 * Just ignore it for now.
859 * Detect 2 and 3 fingers if supported, but only if multiple
860 * fingers appear within the tap gesture time period.
862 if (sc
->flags
& SYN_FLAG_HAS_MULTI_FINGER
&&
863 SYN_TIME(sc
, sc
->gesture_start_packet
) < synaptics_gesture_length
) {
865 case SYNAPTICS_WIDTH_TWO_FINGERS
:
869 case SYNAPTICS_WIDTH_THREE_OR_MORE
:
873 case SYNAPTICS_WIDTH_PEN
:
879 * The width value can report spurious single-finger
880 * events after a multi-finger event.
882 if (sc
->prev_fingers
> 1)
883 fingers
= sc
->prev_fingers
;
894 synaptics_gesture_detect(struct synaptics_softc
*sc
,
895 struct synaptics_packet
*sp
, int fingers
)
897 int gesture_len
, gesture_move_x
, gesture_move_y
, gesture_buttons
;
900 gesture_len
= SYN_TIME(sc
, sc
->gesture_start_packet
);
901 gesture_buttons
= sc
->gesture_buttons
;
903 if (fingers
&& sc
->prev_fingers
== 0) {
905 * Finger was just applied.
906 * If the previous gesture was a single-click, set things
907 * up to deal with a possible drag or double-click gesture.
908 * Basically, if the finger is removed again within
909 * 'synaptics_gesture_length' packets, this is treated
910 * as a double-click. Otherwise we will emulate holding
911 * the left button down whilst dragging the mouse.
913 if (SYN_IS_SINGLE_TAP(sc
->gesture_type
))
914 sc
->gesture_type
|= SYN_GESTURE_DRAG
;
916 sc
->gesture_start_x
= sp
->sp_x
;
917 sc
->gesture_start_y
= sp
->sp_y
;
918 sc
->gesture_start_packet
= sc
->total_packets
;
920 if (fingers
== 0 && sc
->prev_fingers
!= 0) {
922 * Finger was just removed.
923 * Check if the contact time and finger movement were
924 * small enough to qualify as a gesture.
925 * Ignore finger movement if multiple fingers were
926 * detected (the pad may report coordinates for any
929 gesture_move_x
= abs(sc
->gesture_start_x
- sp
->sp_x
);
930 gesture_move_y
= abs(sc
->gesture_start_y
- sp
->sp_y
);
932 if (gesture_len
< synaptics_gesture_length
&&
933 (sc
->prev_fingers
> 1 ||
934 (gesture_move_x
< synaptics_gesture_move
&&
935 gesture_move_y
< synaptics_gesture_move
))) {
937 * Looking good so far.
939 if (SYN_IS_DRAG(sc
->gesture_type
)) {
941 * Promote this gesture to double-click.
943 sc
->gesture_type
|= SYN_GESTURE_DOUBLE
;
944 sc
->gesture_type
&= ~SYN_GESTURE_SINGLE
;
947 * Single tap gesture. Set the tap length timer
948 * and flag a single-click.
950 sc
->gesture_tap_packet
= sc
->total_packets
;
951 sc
->gesture_type
|= SYN_GESTURE_SINGLE
;
954 * The gesture can be modified depending on
955 * the number of fingers detected.
957 * 1: Normal left button emulation.
958 * 2: Either middle button or right button
959 * depending on the value of the two_fingers
963 switch (sc
->prev_fingers
) {
965 if (synaptics_two_fingers_emul
== 1)
966 gesture_buttons
|= PMS_RBUTMASK
;
968 if (synaptics_two_fingers_emul
== 2)
969 gesture_buttons
|= PMS_MBUTMASK
;
972 gesture_buttons
|= PMS_RBUTMASK
;
975 gesture_buttons
|= PMS_LBUTMASK
;
982 * Always clear drag state when the finger is removed.
984 sc
->gesture_type
&= ~SYN_GESTURE_DRAG
;
987 if (sc
->gesture_type
== 0) {
989 * There is no gesture in progress.
990 * Clear emulated button state.
992 sc
->gesture_buttons
= 0;
997 * A gesture is in progress.
1001 if (SYN_IS_SINGLE_TAP(sc
->gesture_type
)) {
1004 * Activate the relevant button(s) until the
1005 * gesture tap timer has expired.
1007 if (SYN_TIME(sc
, sc
->gesture_tap_packet
) <
1008 synaptics_gesture_length
)
1011 sc
->gesture_type
&= ~SYN_GESTURE_SINGLE
;
1013 if (SYN_IS_DOUBLE_TAP(sc
->gesture_type
) && sc
->prev_fingers
== 0) {
1016 * Activate the relevant button(s) once.
1019 sc
->gesture_type
&= ~SYN_GESTURE_DOUBLE
;
1022 if (set_buttons
|| SYN_IS_DRAG(sc
->gesture_type
)) {
1024 * Single-click and drag.
1025 * Maintain button state until the finger is removed.
1027 sp
->sp_left
|= gesture_buttons
& PMS_LBUTMASK
;
1028 sp
->sp_right
|= gesture_buttons
& PMS_RBUTMASK
;
1029 sp
->sp_middle
|= gesture_buttons
& PMS_MBUTMASK
;
1032 sc
->gesture_buttons
= gesture_buttons
;
1036 synaptics_filter_policy(struct synaptics_softc
*sc
, int *history
, int value
)
1038 int a
, b
, rv
, count
;
1040 count
= sc
->total_packets
;
1043 * Once we've accumulated at least SYN_HIST_SIZE values, combine
1044 * each new value with the previous two and return the average.
1046 * This is necessary when the touchpad is operating in 80 packets
1047 * per second mode, as it performs little internal filtering on
1050 * Using a rolling average helps to filter out jitter caused by
1051 * tiny finger movements.
1053 if (sc
->movement_history
>= SYN_HIST_SIZE
) {
1054 a
= (history
[(count
+ 0) % SYN_HIST_SIZE
] +
1055 history
[(count
+ 1) % SYN_HIST_SIZE
]) / 2;
1057 b
= (value
+ history
[(count
+ 0) % SYN_HIST_SIZE
]) / 2;
1062 * Don't report the movement if it's below a certain
1065 if (abs(rv
) < synaptics_movement_threshold
)
1071 * Add the new value to the history buffer.
1073 history
[(count
+ 1) % SYN_HIST_SIZE
] = value
;
1078 /* Edge detection */
1079 #define SYN_EDGE_TOP 1
1080 #define SYN_EDGE_BOTTOM 2
1081 #define SYN_EDGE_LEFT 4
1082 #define SYN_EDGE_RIGHT 8
1085 synaptics_check_edge(int x
, int y
)
1089 if (x
< synaptics_edge_left
)
1090 rv
|= SYN_EDGE_LEFT
;
1092 if (x
> synaptics_edge_right
)
1093 rv
|= SYN_EDGE_RIGHT
;
1095 if (y
< synaptics_edge_bottom
)
1096 rv
|= SYN_EDGE_BOTTOM
;
1098 if (y
> synaptics_edge_top
)
1105 synaptics_edge_motion(struct synaptics_softc
*sc
, int delta
, int dir
)
1109 * When edge motion is enabled, synaptics_edge_motion_delta is
1110 * combined with the current delta, together with the direction
1111 * in which to simulate the motion. The result is added to
1112 * the delta derived from finger movement. This provides a smooth
1113 * transition from finger movement to edge motion.
1115 delta
= synaptics_edge_motion_delta
+ (dir
* delta
);
1118 if (delta
> synaptics_edge_motion_delta
)
1119 return (synaptics_edge_motion_delta
);
1124 synaptics_scale(int delta
, int scale
, int *remp
)
1129 * Scale the raw delta in Synaptics coordinates (0-6143) into
1130 * something more reasonable by dividing the raw delta by a
1131 * scale factor. Any remainder from the previous scale result
1132 * is added to the current delta before scaling.
1133 * This prevents loss of resolution for very small/slow
1134 * movements of the finger.
1138 *remp
= delta
% scale
;
1144 synaptics_movement(struct synaptics_softc
*sc
, struct synaptics_packet
*sp
,
1150 * Compute the next values of dx and dy
1152 dx
= synaptics_filter_policy(sc
, sc
->history_x
, sp
->sp_x
);
1153 dy
= synaptics_filter_policy(sc
, sc
->history_y
, sp
->sp_y
);
1156 * If we're dealing with a drag gesture, and the finger moves to
1157 * the edge of the touchpad, apply edge motion emulation if it
1160 if (synaptics_edge_motion_delta
&& SYN_IS_DRAG(sc
->gesture_type
)) {
1161 edge
= synaptics_check_edge(sp
->sp_x
, sp
->sp_y
);
1163 if (edge
& SYN_EDGE_LEFT
)
1164 dx
-= synaptics_edge_motion(sc
, dx
, 1);
1165 if (edge
& SYN_EDGE_RIGHT
)
1166 dx
+= synaptics_edge_motion(sc
, dx
, -1);
1167 if (edge
& SYN_EDGE_BOTTOM
)
1168 dy
-= synaptics_edge_motion(sc
, dy
, 1);
1169 if (edge
& SYN_EDGE_TOP
)
1170 dy
+= synaptics_edge_motion(sc
, dy
, -1);
1174 * Apply scaling to both deltas
1176 dx
= synaptics_scale(dx
, synaptics_scale_x
, &sc
->rem_x
);
1177 dy
= synaptics_scale(dy
, synaptics_scale_y
, &sc
->rem_y
);
1180 * Clamp deltas to specified maximums.
1182 if (dx
> synaptics_max_speed_x
)
1183 dx
= synaptics_max_speed_x
;
1184 if (dy
> synaptics_max_speed_y
)
1185 dy
= synaptics_max_speed_y
;
1190 sc
->movement_history
++;
1194 pms_synaptics_process_packet(struct pms_softc
*psc
, struct synaptics_packet
*sp
)
1196 struct synaptics_softc
*sc
= &psc
->u
.synaptics
;
1198 int fingers
, palm
, buttons
, changed
;
1202 * Do Z-axis emulation using up/down buttons if required.
1203 * Note that the pad will send a one second burst of packets
1204 * when an up/down button is pressed and held. At the moment
1205 * we don't deal with auto-repeat, so convert the burst into
1209 if (synaptics_up_down_emul
== 2) {
1210 if (sc
->up_down
== 0) {
1211 if (sp
->sp_up
&& sp
->sp_down
) {
1213 * Most up/down buttons will be actuated using
1214 * a rocker switch, so we should never see
1215 * them both simultaneously. But just in case,
1216 * treat this situation as a middle button
1222 dz
= -synaptics_up_down_motion_delta
;
1225 dz
= synaptics_up_down_motion_delta
;
1228 sc
->up_down
= sp
->sp_up
| sp
->sp_down
;
1229 sp
->sp_up
= sp
->sp_down
= 0;
1233 * Determine whether or not a finger is on the pad.
1234 * On some pads, this will return the number of fingers
1237 fingers
= synaptics_finger_detect(sc
, sp
, &palm
);
1240 * Do gesture processing only if we didn't detect a palm.
1243 synaptics_gesture_detect(sc
, sp
, fingers
);
1245 sc
->gesture_type
= sc
->gesture_buttons
= 0;
1248 * Determine what buttons to report
1250 buttons
= (sp
->sp_left
? 0x1 : 0) |
1251 (sp
->sp_middle
? 0x2 : 0) |
1252 (sp
->sp_right
? 0x4 : 0) |
1253 (sp
->sp_up
? 0x8 : 0) |
1254 (sp
->sp_down
? 0x10 : 0);
1255 changed
= buttons
^ (psc
->buttons
& 0x1f);
1256 psc
->buttons
^= changed
;
1258 sc
->prev_fingers
= fingers
;
1259 sc
->total_packets
++;
1262 * Do movement processing IFF we have a single finger and no palm.
1264 if (fingers
== 1 && palm
== 0)
1265 synaptics_movement(sc
, sp
, &dx
, &dy
);
1268 * No valid finger. Therefore no movement.
1270 sc
->movement_history
= 0;
1271 sc
->rem_x
= sc
->rem_y
= 0;
1276 * Pass the final results up to wsmouse_input() if necessary.
1278 if (dx
|| dy
|| dz
|| changed
) {
1279 buttons
= (psc
->buttons
& 0x1f) | ((psc
->buttons
>> 5) & 0x7);
1281 wsmouse_input(psc
->sc_wsmousedev
,
1284 WSMOUSE_INPUT_DELTA
);