synaptics.c revision 1.9.6.1 1 /* $NetBSD: synaptics.c,v 1.9.6.1 2006/02/04 14:03:58 simonb Exp $ */
2
3 /*
4 * Copyright (c) 2005, Steve C. Woodford
5 * Copyright (c) 2004, Ales Krenek
6 * Copyright (c) 2004, Kentaro A. Kurahone
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
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.
22 *
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.
35 *
36 */
37
38 /*
39 * TODO:
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?)
46 * - Support pass-through mode (whatever that is; my docs are too old).
47 */
48
49 #include "opt_pms.h"
50
51 #include <sys/cdefs.h>
52
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>
59
60 #include <machine/bus.h>
61
62 #include <dev/pckbport/pckbportvar.h>
63
64 #include <dev/pckbport/synapticsreg.h>
65 #include <dev/pckbport/synapticsvar.h>
66
67 #include <dev/pckbport/pmsreg.h>
68 #include <dev/pckbport/pmsvar.h>
69
70 #include <dev/wscons/wsconsio.h>
71 #include <dev/wscons/wsmousevar.h>
72
73 /*
74 * Absolute-mode packets are decoded and passed around using
75 * the following structure.
76 */
77 struct synaptics_packet {
78 signed short sp_x; /* Unscaled absolute X/Y coordinates */
79 signed short sp_y;
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 */
87 };
88
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);
95
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;
114
115 /* Sysctl nodes. */
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;
133
134 int
135 pms_synaptics_probe_init(void *vsc)
136 {
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;
142
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,
147 resp, 0);
148 if (res) {
149 #ifdef SYNAPTICSDEBUG
150 aprint_error("%s: synaptics_probe: Identify Touchpad error.\n",
151 psc->sc_dev.dv_xname);
152 #endif
153 /*
154 * Reset device in case the probe confused it.
155 */
156 doreset:
157 cmd[0] = PMS_RESET;
158 (void) pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
159 1, 2, resp, 1);
160 return (res);
161 }
162
163 if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
164 #ifdef SYNAPTICSDEBUG
165 printf("%s: synaptics_probe: Not synaptics.\n",
166 psc->sc_dev.dv_xname);
167 #endif
168 res = 1;
169 goto doreset;
170 }
171
172 sc->flags = 0;
173
174 /* Check for minimum version and print a nice message. */
175 ver_major = resp[2] & 0x0f;
176 ver_minor = resp[0];
177 aprint_normal("%s: Synaptics touchpad version %d.%d\n",
178 psc->sc_dev.dv_xname, ver_major, ver_minor);
179 if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
180 /* No capability query support. */
181 sc->caps = 0;
182 goto done;
183 }
184
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,
190 resp, 0);
191 if (res) {
192 /* Hmm, failed to get capabilites. */
193 aprint_error("%s: synaptics_probe: Failed to query "
194 "capabilities.\n", psc->sc_dev.dv_xname);
195 goto doreset;
196 }
197
198 sc->caps = (resp[0] << 8) | resp[2];
199
200 if (sc->caps & SYNAPTICS_CAP_MBUTTON)
201 sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
202
203 if (sc->caps & SYNAPTICS_CAP_4BUTTON)
204 sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
205
206 if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
207 #ifdef SYNAPTICSDEBUG
208 aprint_normal("%s: synaptics_probe: Capabilities 0x%04x.\n",
209 psc->sc_dev.dv_xname, sc->caps);
210 #endif
211 if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH) /*XXX: Not supported*/
212 sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
213
214 if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
215 sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
216
217 if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
218 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
219
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
228 if (res == 0)
229 aprint_normal("%s: synaptics_probe: Extended "
230 "Capabilities 0x%02x.\n",
231 psc->sc_dev.dv_xname, resp[1]);
232 #endif
233 if (!res && (resp[1] >> 4) >= 2) {
234 /* Yes. */
235 sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
236 }
237 }
238 }
239
240 if (sc->flags) {
241 const char comma[] = ", ";
242 const char *sep = "";
243 aprint_normal("%s: ", psc->sc_dev.dv_xname);
244 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
245 aprint_normal("%sMiddle button", sep);
246 sep = comma;
247 }
248 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
249 aprint_normal("%sButtons 4/5", sep);
250 sep = comma;
251 }
252 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) {
253 aprint_normal("%sUp/down buttons", sep);
254 sep = comma;
255 }
256 if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) {
257 aprint_normal("%sPalm detect", sep);
258 sep = comma;
259 }
260 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER)
261 aprint_normal("%sMulti-finger", sep);
262
263 aprint_normal("\n");
264 }
265
266 done:
267 pms_sysctl_synaptics(&clog);
268 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
269 pms_synaptics_input, psc, psc->sc_dev.dv_xname);
270
271 return (0);
272 }
273
274 void
275 pms_synaptics_enable(void *vsc)
276 {
277 struct pms_softc *psc = vsc;
278 struct synaptics_softc *sc = &psc->u.synaptics;
279 u_char cmd[2];
280 int res;
281
282 /*
283 * Enable Absolute mode with W (width) reporting, and set
284 * the packet rate to maximum (80 packets per second).
285 */
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,
291 1, NULL);
292 sc->up_down = 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;
301 if (res) {
302 printf("%s: synaptics_enable: Error enabling device.\n",
303 psc->sc_dev.dv_xname);
304 }
305 }
306
307 void
308 pms_synaptics_resume(void *vsc)
309 {
310 struct pms_softc *psc = vsc;
311 unsigned char cmd[1],resp[2] = { 0,0 };
312 int res;
313
314 cmd[0] = PMS_RESET;
315 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
316 resp, 1);
317 printf("%s: pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n",
318 psc->sc_dev.dv_xname, res, resp[0], resp[1]);
319 }
320
321 static void
322 pms_sysctl_synaptics(struct sysctllog **clog)
323 {
324 int rc, root_num;
325 const struct sysctlnode *node;
326
327 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
328 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
329 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
330 goto err;
331
332 if ((rc = sysctl_createv(clog, 0, NULL, &node,
333 CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
334 SYSCTL_DESCR("Synaptics touchpad controls"),
335 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
336 goto err;
337
338 root_num = node->sysctl_num;
339
340 if ((rc = sysctl_createv(clog, 0, NULL, &node,
341 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
342 CTLTYPE_INT, "up_down_emulation",
343 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
344 pms_sysctl_synaptics_verify, 0,
345 &synaptics_up_down_emul,
346 0, CTL_HW, root_num, CTL_CREATE,
347 CTL_EOL)) != 0)
348 goto err;
349
350 synaptics_up_down_emul_nodenum = node->sysctl_num;
351
352 if ((rc = sysctl_createv(clog, 0, NULL, &node,
353 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
354 CTLTYPE_INT, "up_down_motion_delta",
355 SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
356 pms_sysctl_synaptics_verify, 0,
357 &synaptics_up_down_motion_delta,
358 0, CTL_HW, root_num, CTL_CREATE,
359 CTL_EOL)) != 0)
360 goto err;
361
362 synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
363
364 if ((rc = sysctl_createv(clog, 0, NULL, &node,
365 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
366 CTLTYPE_INT, "gesture_move",
367 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
368 pms_sysctl_synaptics_verify, 0,
369 &synaptics_gesture_move,
370 0, CTL_HW, root_num, CTL_CREATE,
371 CTL_EOL)) != 0)
372 goto err;
373
374 synaptics_gesture_move_nodenum = node->sysctl_num;
375
376 if ((rc = sysctl_createv(clog, 0, NULL, &node,
377 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
378 CTLTYPE_INT, "gesture_length",
379 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
380 pms_sysctl_synaptics_verify, 0,
381 &synaptics_gesture_length,
382 0, CTL_HW, root_num, CTL_CREATE,
383 CTL_EOL)) != 0)
384 goto err;
385
386 synaptics_gesture_length_nodenum = node->sysctl_num;
387
388 if ((rc = sysctl_createv(clog, 0, NULL, &node,
389 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
390 CTLTYPE_INT, "edge_left",
391 SYSCTL_DESCR("Define left edge of touchpad"),
392 pms_sysctl_synaptics_verify, 0,
393 &synaptics_edge_left,
394 0, CTL_HW, root_num, CTL_CREATE,
395 CTL_EOL)) != 0)
396 goto err;
397
398 synaptics_edge_left_nodenum = node->sysctl_num;
399
400 if ((rc = sysctl_createv(clog, 0, NULL, &node,
401 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
402 CTLTYPE_INT, "edge_right",
403 SYSCTL_DESCR("Define right edge of touchpad"),
404 pms_sysctl_synaptics_verify, 0,
405 &synaptics_edge_right,
406 0, CTL_HW, root_num, CTL_CREATE,
407 CTL_EOL)) != 0)
408 goto err;
409
410 synaptics_edge_right_nodenum = node->sysctl_num;
411
412 if ((rc = sysctl_createv(clog, 0, NULL, &node,
413 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
414 CTLTYPE_INT, "edge_top",
415 SYSCTL_DESCR("Define top edge of touchpad"),
416 pms_sysctl_synaptics_verify, 0,
417 &synaptics_edge_top,
418 0, CTL_HW, root_num, CTL_CREATE,
419 CTL_EOL)) != 0)
420 goto err;
421
422 synaptics_edge_top_nodenum = node->sysctl_num;
423
424 if ((rc = sysctl_createv(clog, 0, NULL, &node,
425 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
426 CTLTYPE_INT, "edge_bottom",
427 SYSCTL_DESCR("Define bottom edge of touchpad"),
428 pms_sysctl_synaptics_verify, 0,
429 &synaptics_edge_bottom,
430 0, CTL_HW, root_num, CTL_CREATE,
431 CTL_EOL)) != 0)
432 goto err;
433
434 synaptics_edge_bottom_nodenum = node->sysctl_num;
435
436 if ((rc = sysctl_createv(clog, 0, NULL, &node,
437 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
438 CTLTYPE_INT, "edge_motion_delta",
439 SYSCTL_DESCR("Define edge motion rate"),
440 pms_sysctl_synaptics_verify, 0,
441 &synaptics_edge_motion_delta,
442 0, CTL_HW, root_num, CTL_CREATE,
443 CTL_EOL)) != 0)
444 goto err;
445
446 synaptics_edge_motion_delta_nodenum = node->sysctl_num;
447
448 if ((rc = sysctl_createv(clog, 0, NULL, &node,
449 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
450 CTLTYPE_INT, "finger_high",
451 SYSCTL_DESCR("Define finger applied pressure threshold"),
452 pms_sysctl_synaptics_verify, 0,
453 &synaptics_finger_high,
454 0, CTL_HW, root_num, CTL_CREATE,
455 CTL_EOL)) != 0)
456 goto err;
457
458 synaptics_finger_high_nodenum = node->sysctl_num;
459
460 if ((rc = sysctl_createv(clog, 0, NULL, &node,
461 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
462 CTLTYPE_INT, "finger_low",
463 SYSCTL_DESCR("Define finger removed pressure threshold"),
464 pms_sysctl_synaptics_verify, 0,
465 &synaptics_finger_low,
466 0, CTL_HW, root_num, CTL_CREATE,
467 CTL_EOL)) != 0)
468 goto err;
469
470 synaptics_finger_low_nodenum = node->sysctl_num;
471
472 if ((rc = sysctl_createv(clog, 0, NULL, &node,
473 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
474 CTLTYPE_INT, "two_fingers_emulation",
475 SYSCTL_DESCR("Map two fingers to middle button"),
476 pms_sysctl_synaptics_verify, 0,
477 &synaptics_two_fingers_emul,
478 0, CTL_HW, root_num, CTL_CREATE,
479 CTL_EOL)) != 0)
480 goto err;
481
482 synaptics_two_fingers_emul_nodenum = node->sysctl_num;
483
484 if ((rc = sysctl_createv(clog, 0, NULL, &node,
485 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
486 CTLTYPE_INT, "scale_x",
487 SYSCTL_DESCR("Horizontal movement scale factor"),
488 pms_sysctl_synaptics_verify, 0,
489 &synaptics_scale_x,
490 0, CTL_HW, root_num, CTL_CREATE,
491 CTL_EOL)) != 0)
492 goto err;
493
494 synaptics_scale_x_nodenum = node->sysctl_num;
495
496 if ((rc = sysctl_createv(clog, 0, NULL, &node,
497 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
498 CTLTYPE_INT, "scale_y",
499 SYSCTL_DESCR("Vertical movement scale factor"),
500 pms_sysctl_synaptics_verify, 0,
501 &synaptics_scale_y,
502 0, CTL_HW, root_num, CTL_CREATE,
503 CTL_EOL)) != 0)
504 goto err;
505
506 synaptics_scale_y_nodenum = node->sysctl_num;
507
508 if ((rc = sysctl_createv(clog, 0, NULL, &node,
509 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
510 CTLTYPE_INT, "max_speed_x",
511 SYSCTL_DESCR("Horizontal movement maximum speed"),
512 pms_sysctl_synaptics_verify, 0,
513 &synaptics_max_speed_x,
514 0, CTL_HW, root_num, CTL_CREATE,
515 CTL_EOL)) != 0)
516 goto err;
517
518 synaptics_max_speed_x_nodenum = node->sysctl_num;
519
520 if ((rc = sysctl_createv(clog, 0, NULL, &node,
521 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
522 CTLTYPE_INT, "max_speed_y",
523 SYSCTL_DESCR("Vertical movement maximum speed"),
524 pms_sysctl_synaptics_verify, 0,
525 &synaptics_max_speed_y,
526 0, CTL_HW, root_num, CTL_CREATE,
527 CTL_EOL)) != 0)
528 goto err;
529
530 synaptics_max_speed_y_nodenum = node->sysctl_num;
531
532 if ((rc = sysctl_createv(clog, 0, NULL, &node,
533 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
534 CTLTYPE_INT, "movement_threshold",
535 SYSCTL_DESCR("Minimum reported movement threshold"),
536 pms_sysctl_synaptics_verify, 0,
537 &synaptics_movement_threshold,
538 0, CTL_HW, root_num, CTL_CREATE,
539 CTL_EOL)) != 0)
540 goto err;
541
542 synaptics_movement_threshold_nodenum = node->sysctl_num;
543 return;
544
545 err:
546 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
547 }
548
549 static int
550 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
551 {
552 int error, t;
553 struct sysctlnode node;
554
555 node = *rnode;
556 t = *(int *)rnode->sysctl_data;
557 node.sysctl_data = &t;
558 error = sysctl_lookup(SYSCTLFN_CALL(&node));
559 if (error || newp == NULL)
560 return error;
561
562 /* Sanity check the params. */
563 if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
564 node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
565 if (t < 0 || t > 2)
566 return (EINVAL);
567 } else
568 if (node.sysctl_num == synaptics_gesture_length_nodenum ||
569 node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
570 node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
571 if (t < 0)
572 return (EINVAL);
573 } else
574 if (node.sysctl_num == synaptics_edge_left_nodenum ||
575 node.sysctl_num == synaptics_edge_bottom_nodenum) {
576 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
577 return (EINVAL);
578 } else
579 if (node.sysctl_num == synaptics_edge_right_nodenum ||
580 node.sysctl_num == synaptics_edge_top_nodenum) {
581 if (t < (SYNAPTICS_EDGE_MAX / 2))
582 return (EINVAL);
583 } else
584 if (node.sysctl_num == synaptics_scale_x_nodenum ||
585 node.sysctl_num == synaptics_scale_y_nodenum) {
586 if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
587 return (EINVAL);
588 } else
589 if (node.sysctl_num == synaptics_finger_high_nodenum) {
590 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
591 t < synaptics_finger_low)
592 return (EINVAL);
593 } else
594 if (node.sysctl_num == synaptics_finger_low_nodenum) {
595 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
596 t > synaptics_finger_high)
597 return (EINVAL);
598 } else
599 if (node.sysctl_num == synaptics_gesture_move_nodenum ||
600 node.sysctl_num == synaptics_movement_threshold_nodenum) {
601 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
602 return (EINVAL);
603 } else
604 return (EINVAL);
605
606 *(int *)rnode->sysctl_data = t;
607
608 return (0);
609 }
610
611 static int
612 pms_synaptics_send_command(pckbport_tag_t tag, pckbport_slot_t slot,
613 u_char syn_cmd)
614 {
615 u_char cmd[2];
616 int res;
617
618 /*
619 * Need to send 4 Set Resolution commands, with the argument
620 * encoded in the bottom most 2 bits.
621 */
622 cmd[0] = PMS_SET_RES;
623 cmd[1] = syn_cmd >> 6;
624 res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
625
626 cmd[0] = PMS_SET_RES;
627 cmd[1] = (syn_cmd & 0x30) >> 4;
628 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
629
630 cmd[0] = PMS_SET_RES;
631 cmd[1] = (syn_cmd & 0x0c) >> 2;
632 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
633
634 cmd[0] = PMS_SET_RES;
635 cmd[1] = (syn_cmd & 0x03);
636 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
637
638 return (res);
639 }
640
641 /* Masks for the first byte of a packet */
642 #define PMS_LBUTMASK 0x01
643 #define PMS_RBUTMASK 0x02
644 #define PMS_MBUTMASK 0x04
645
646 static void
647 pms_synaptics_input(void *vsc, int data)
648 {
649 struct pms_softc *psc = vsc;
650 struct synaptics_softc *sc = &psc->u.synaptics;
651 struct timeval diff;
652 struct synaptics_packet sp;
653 int s;
654
655 if (!psc->sc_enabled) {
656 /* Interrupts are not expected. Discard the byte. */
657 return;
658 }
659
660 getmicrouptime(&psc->current);
661
662 if (psc->inputstate > 0) {
663 timersub(&psc->current, &psc->last, &diff);
664 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
665 printf("%s: pms_input: unusual delay (%ld.%06ld s), "
666 "scheduling reset\n", psc->sc_dev.dv_xname,
667 (long)diff.tv_sec, (long)diff.tv_usec);
668 psc->inputstate = 0;
669 psc->sc_enabled = 0;
670 wakeup(&psc->sc_enabled);
671 return;
672 }
673 }
674 psc->last = psc->current;
675
676 switch (psc->inputstate) {
677 case 0:
678 if ((data & 0xc8) != 0x80) {
679 #ifdef SYNAPTICSDEBUG
680 printf("%s: pms_input: 0x%02x out of sync\n",
681 psc->sc_dev.dv_xname, data);
682 #endif
683 return; /* not in sync yet, discard input */
684 }
685 /*FALLTHROUGH*/
686
687 case 3:
688 if ((data & 8) == 8) {
689 #ifdef SYNAPTICSDEBUG
690 printf("%s: pms_input: dropped in relative mode, "
691 "reset\n", psc->sc_dev.dv_xname);
692 #endif
693 psc->inputstate = 0;
694 psc->sc_enabled = 0;
695 wakeup(&psc->sc_enabled);
696 return;
697 }
698 }
699
700 psc->packet[psc->inputstate++] = data & 0xff;
701 if (psc->inputstate == 6) {
702 /*
703 * We have a complete packet.
704 * Extract the pertinent details.
705 */
706 psc->inputstate = 0;
707
708 /* Absolute X/Y coordinates of finger */
709 sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
710 ((psc->packet[3] & 0x10) << 8);
711 sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
712 ((psc->packet[3] & 0x20) << 7);
713
714 /* Pressure */
715 sp.sp_z = psc->packet[2];
716
717 /* Width of finger */
718 sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
719 ((psc->packet[0] & 0x04) >> 1) +
720 ((psc->packet[3] & 0x04) >> 2);
721
722 /* Left/Right button handling. */
723 sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
724 sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
725
726 /* Up/Down buttons. */
727 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
728 /* Old up/down buttons. */
729 sp.sp_up = sp.sp_left ^
730 (psc->packet[3] & PMS_LBUTMASK);
731 sp.sp_down = sp.sp_right ^
732 (psc->packet[3] & PMS_RBUTMASK);
733 } else
734 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
735 ((psc->packet[0] & PMS_RBUTMASK) ^
736 (psc->packet[3] & PMS_RBUTMASK))) {
737 /* New up/down button. */
738 sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
739 sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
740 } else {
741 sp.sp_up = 0;
742 sp.sp_down = 0;
743 }
744
745 /* Middle button. */
746 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
747 /* Old style Middle Button. */
748 sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
749 (psc->packet[3] & PMS_LBUTMASK);
750 } else
751 if (synaptics_up_down_emul == 1) {
752 /* Do middle button emulation using up/down buttons */
753 sp.sp_middle = sp.sp_up | sp.sp_down;
754 sp.sp_up = sp.sp_down = 0;
755 } else
756 sp.sp_middle = 0;
757
758 /*
759 * Go process the new packet
760 */
761 pms_synaptics_process_packet(psc, &sp);
762 }
763 }
764
765 static inline int
766 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
767 int *palmp)
768 {
769 int fingers;
770
771 /* Assume no palm */
772 *palmp = 0;
773
774 /*
775 * Apply some hysteresis when checking for a finger.
776 * When the finger is first applied, we ignore it until the
777 * pressure exceeds the 'high' threshold. The finger is considered
778 * removed only when pressure falls beneath the 'low' threshold.
779 */
780 if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
781 (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
782 fingers = 1;
783 else
784 fingers = 0;
785
786 /*
787 * If the pad can't do palm detection, skip the rest.
788 */
789 if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
790 return (fingers);
791
792 /*
793 * Palm detection
794 */
795 if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
796 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
797 *palmp = 1;
798
799 if (sc->prev_fingers == 0 &&
800 (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
801 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
802 /*
803 * Contact area or pressure is too great to be a finger.
804 * Just ignore it for now.
805 */
806 return (0);
807 }
808
809 /*
810 * Detect 2 and 3 fingers if supported, but only if multiple
811 * fingers appear within the tap gesture time period.
812 */
813 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
814 SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) {
815 switch (sp->sp_w) {
816 case SYNAPTICS_WIDTH_TWO_FINGERS:
817 fingers = 2;
818 break;
819
820 case SYNAPTICS_WIDTH_THREE_OR_MORE:
821 fingers = 3;
822 break;
823
824 case SYNAPTICS_WIDTH_PEN:
825 fingers = 1;
826 break;
827
828 default:
829 /*
830 * The width value can report spurious single-finger
831 * events after a multi-finger event.
832 */
833 if (sc->prev_fingers > 1)
834 fingers = sc->prev_fingers;
835 else
836 fingers = 1;
837 break;
838 }
839 }
840
841 return (fingers);
842 }
843
844 static inline void
845 synaptics_gesture_detect(struct synaptics_softc *sc,
846 struct synaptics_packet *sp, int fingers)
847 {
848 int gesture_len, gesture_move_x, gesture_move_y, gesture_buttons;
849 int set_buttons;
850
851 gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
852 gesture_buttons = sc->gesture_buttons;
853
854 if (fingers && sc->prev_fingers == 0) {
855 /*
856 * Finger was just applied.
857 * If the previous gesture was a single-click, set things
858 * up to deal with a possible drag or double-click gesture.
859 * Basically, if the finger is removed again within
860 * 'synaptics_gesture_length' packets, this is treated
861 * as a double-click. Otherwise we will emulate holding
862 * the left button down whilst dragging the mouse.
863 */
864 if (SYN_IS_SINGLE_TAP(sc->gesture_type))
865 sc->gesture_type |= SYN_GESTURE_DRAG;
866
867 sc->gesture_start_x = sp->sp_x;
868 sc->gesture_start_y = sp->sp_y;
869 sc->gesture_start_packet = sc->total_packets;
870 } else
871 if (fingers == 0 && sc->prev_fingers != 0) {
872 /*
873 * Finger was just removed.
874 * Check if the contact time and finger movement were
875 * small enough to qualify as a gesture.
876 * Ignore finger movement if multiple fingers were
877 * detected (the pad may report coordinates for any
878 * of the fingers).
879 */
880 gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
881 gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
882
883 if (gesture_len < synaptics_gesture_length &&
884 (sc->prev_fingers > 1 ||
885 (gesture_move_x < synaptics_gesture_move &&
886 gesture_move_y < synaptics_gesture_move))) {
887 /*
888 * Looking good so far.
889 */
890 if (SYN_IS_DRAG(sc->gesture_type)) {
891 /*
892 * Promote this gesture to double-click.
893 */
894 sc->gesture_type |= SYN_GESTURE_DOUBLE;
895 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
896 } else {
897 /*
898 * Single tap gesture. Set the tap length timer
899 * and flag a single-click.
900 */
901 sc->gesture_tap_packet = sc->total_packets;
902 sc->gesture_type |= SYN_GESTURE_SINGLE;
903
904 /*
905 * The gesture can be modified depending on
906 * the number of fingers detected.
907 *
908 * 1: Normal left button emulation.
909 * 2: Either middle button or right button
910 * depending on the value of the two_fingers
911 * sysctl variable.
912 * 3: Right button.
913 */
914 switch (sc->prev_fingers) {
915 case 2:
916 if (synaptics_two_fingers_emul == 1)
917 gesture_buttons |= PMS_RBUTMASK;
918 else
919 if (synaptics_two_fingers_emul == 2)
920 gesture_buttons |= PMS_MBUTMASK;
921 break;
922 case 3:
923 gesture_buttons |= PMS_RBUTMASK;
924 break;
925 default:
926 gesture_buttons |= PMS_LBUTMASK;
927 break;
928 }
929 }
930 }
931
932 /*
933 * Always clear drag state when the finger is removed.
934 */
935 sc->gesture_type &= ~SYN_GESTURE_DRAG;
936 }
937
938 if (sc->gesture_type == 0) {
939 /*
940 * There is no gesture in progress.
941 * Clear emulated button state.
942 */
943 sc->gesture_buttons = 0;
944 return;
945 }
946
947 /*
948 * A gesture is in progress.
949 */
950 set_buttons = 0;
951
952 if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
953 /*
954 * Single-click.
955 * Activate the relevant button(s) until the
956 * gesture tap timer has expired.
957 */
958 if (SYN_TIME(sc, sc->gesture_tap_packet) <
959 synaptics_gesture_length)
960 set_buttons = 1;
961 else
962 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
963 } else
964 if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
965 /*
966 * Double-click.
967 * Activate the relevant button(s) once.
968 */
969 set_buttons = 1;
970 sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
971 }
972
973 if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
974 /*
975 * Single-click and drag.
976 * Maintain button state until the finger is removed.
977 */
978 sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
979 sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
980 sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
981 }
982
983 sc->gesture_buttons = gesture_buttons;
984 }
985
986 static inline int
987 synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value)
988 {
989 int a, b, rv, count;
990
991 count = sc->total_packets;
992
993 /*
994 * Once we've accumulated at least SYN_HIST_SIZE values, combine
995 * each new value with the previous two and return the average.
996 *
997 * This is necessary when the touchpad is operating in 80 packets
998 * per second mode, as it performs little internal filtering on
999 * reported values.
1000 *
1001 * Using a rolling average helps to filter out jitter caused by
1002 * tiny finger movements.
1003 */
1004 if (sc->movement_history >= SYN_HIST_SIZE) {
1005 a = (history[(count + 0) % SYN_HIST_SIZE] +
1006 history[(count + 1) % SYN_HIST_SIZE]) / 2;
1007
1008 b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1009
1010 rv = b - a;
1011
1012 /*
1013 * Don't report the movement if it's below a certain
1014 * threshold.
1015 */
1016 if (abs(rv) < synaptics_movement_threshold)
1017 rv = 0;
1018 } else
1019 rv = 0;
1020
1021 /*
1022 * Add the new value to the history buffer.
1023 */
1024 history[(count + 1) % SYN_HIST_SIZE] = value;
1025
1026 return (rv);
1027 }
1028
1029 /* Edge detection */
1030 #define SYN_EDGE_TOP 1
1031 #define SYN_EDGE_BOTTOM 2
1032 #define SYN_EDGE_LEFT 4
1033 #define SYN_EDGE_RIGHT 8
1034
1035 static inline int
1036 synaptics_check_edge(int x, int y)
1037 {
1038 int rv = 0;
1039
1040 if (x < synaptics_edge_left)
1041 rv |= SYN_EDGE_LEFT;
1042 else
1043 if (x > synaptics_edge_right)
1044 rv |= SYN_EDGE_RIGHT;
1045
1046 if (y < synaptics_edge_bottom)
1047 rv |= SYN_EDGE_BOTTOM;
1048 else
1049 if (y > synaptics_edge_top)
1050 rv |= SYN_EDGE_TOP;
1051
1052 return (rv);
1053 }
1054
1055 static inline int
1056 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1057 {
1058
1059 /*
1060 * When edge motion is enabled, synaptics_edge_motion_delta is
1061 * combined with the current delta, together with the direction
1062 * in which to simulate the motion. The result is added to
1063 * the delta derived from finger movement. This provides a smooth
1064 * transition from finger movement to edge motion.
1065 */
1066 delta = synaptics_edge_motion_delta + (dir * delta);
1067 if (delta < 0)
1068 return (0);
1069 if (delta > synaptics_edge_motion_delta)
1070 return (synaptics_edge_motion_delta);
1071 return (delta);
1072 }
1073
1074 static inline int
1075 synaptics_scale(int delta, int scale, int *remp)
1076 {
1077 int rv;
1078
1079 /*
1080 * Scale the raw delta in Synaptics coordinates (0-6143) into
1081 * something more reasonable by dividing the raw delta by a
1082 * scale factor. Any remainder from the previous scale result
1083 * is added to the current delta before scaling.
1084 * This prevents loss of resolution for very small/slow
1085 * movements of the finger.
1086 */
1087 delta += *remp;
1088 rv = delta / scale;
1089 *remp = delta % scale;
1090
1091 return (rv);
1092 }
1093
1094 static inline void
1095 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1096 int *dxp, int *dyp)
1097 {
1098 int dx, dy, edge;
1099
1100 /*
1101 * Compute the next values of dx and dy
1102 */
1103 dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x);
1104 dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y);
1105
1106 /*
1107 * If we're dealing with a drag gesture, and the finger moves to
1108 * the edge of the touchpad, apply edge motion emulation if it
1109 * is enabled.
1110 */
1111 if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1112 edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1113
1114 if (edge & SYN_EDGE_LEFT)
1115 dx -= synaptics_edge_motion(sc, dx, 1);
1116 if (edge & SYN_EDGE_RIGHT)
1117 dx += synaptics_edge_motion(sc, dx, -1);
1118 if (edge & SYN_EDGE_BOTTOM)
1119 dy -= synaptics_edge_motion(sc, dy, 1);
1120 if (edge & SYN_EDGE_TOP)
1121 dy += synaptics_edge_motion(sc, dy, -1);
1122 }
1123
1124 /*
1125 * Apply scaling to both deltas
1126 */
1127 dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x);
1128 dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y);
1129
1130 /*
1131 * Clamp deltas to specified maximums.
1132 */
1133 if (dx > synaptics_max_speed_x)
1134 dx = synaptics_max_speed_x;
1135 if (dy > synaptics_max_speed_y)
1136 dy = synaptics_max_speed_y;
1137
1138 *dxp = dx;
1139 *dyp = dy;
1140
1141 sc->movement_history++;
1142 }
1143
1144 static void
1145 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
1146 {
1147 struct synaptics_softc *sc = &psc->u.synaptics;
1148 int dx, dy, dz;
1149 int fingers, palm, buttons, changed;
1150 int s;
1151
1152 /*
1153 * Do Z-axis emulation using up/down buttons if required.
1154 * Note that the pad will send a one second burst of packets
1155 * when an up/down button is pressed and held. At the moment
1156 * we don't deal with auto-repeat, so convert the burst into
1157 * a one-shot.
1158 */
1159 dz = 0;
1160 if (synaptics_up_down_emul == 2) {
1161 if (sc->up_down == 0) {
1162 if (sp->sp_up && sp->sp_down) {
1163 /*
1164 * Most up/down buttons will be actuated using
1165 * a rocker switch, so we should never see
1166 * them both simultaneously. But just in case,
1167 * treat this situation as a middle button
1168 * event.
1169 */
1170 sp->sp_middle = 1;
1171 } else
1172 if (sp->sp_up)
1173 dz = -synaptics_up_down_motion_delta;
1174 else
1175 if (sp->sp_down)
1176 dz = synaptics_up_down_motion_delta;
1177 }
1178
1179 sc->up_down = sp->sp_up | sp->sp_down;
1180 sp->sp_up = sp->sp_down = 0;
1181 }
1182
1183 /*
1184 * Determine whether or not a finger is on the pad.
1185 * On some pads, this will return the number of fingers
1186 * detected.
1187 */
1188 fingers = synaptics_finger_detect(sc, sp, &palm);
1189
1190 /*
1191 * Do gesture processing only if we didn't detect a palm.
1192 */
1193 if (palm == 0)
1194 synaptics_gesture_detect(sc, sp, fingers);
1195 else
1196 sc->gesture_type = sc->gesture_buttons = 0;
1197
1198 /*
1199 * Determine what buttons to report
1200 */
1201 buttons = (sp->sp_left ? 0x1 : 0) |
1202 (sp->sp_middle ? 0x2 : 0) |
1203 (sp->sp_right ? 0x4 : 0) |
1204 (sp->sp_up ? 0x8 : 0) |
1205 (sp->sp_down ? 0x10 : 0);
1206 changed = buttons ^ psc->buttons;
1207 psc->buttons = buttons;
1208
1209 sc->prev_fingers = fingers;
1210 sc->total_packets++;
1211
1212 /*
1213 * Do movement processing IFF we have a single finger and no palm.
1214 */
1215 if (fingers == 1 && palm == 0)
1216 synaptics_movement(sc, sp, &dx, &dy);
1217 else {
1218 /*
1219 * No valid finger. Therefore no movement.
1220 */
1221 sc->movement_history = 0;
1222 sc->rem_x = sc->rem_y = 0;
1223 dx = dy = 0;
1224 }
1225
1226 /*
1227 * Pass the final results up to wsmouse_input() if necessary.
1228 */
1229 if (dx || dy || dz || changed) {
1230 s = spltty();
1231 wsmouse_input(psc->sc_wsmousedev, buttons, dx, dy, dz,
1232 WSMOUSE_INPUT_DELTA);
1233 splx(s);
1234 }
1235 }
1236