synaptics.c revision 1.3 1 /* $NetBSD: synaptics.c,v 1.3 2005/01/02 22:37:12 scw 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 <sys/cdefs.h>
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/ioctl.h>
55 #include <sys/sysctl.h>
56 #include <sys/kernel.h>
57
58 #include <machine/bus.h>
59
60 #include <dev/pckbport/pckbportvar.h>
61
62 #include <dev/pckbport/synapticsreg.h>
63 #include <dev/pckbport/synapticsvar.h>
64
65 #include <dev/pckbport/pmsreg.h>
66 #include <dev/pckbport/pmsvar.h>
67
68 #include <dev/wscons/wsconsio.h>
69 #include <dev/wscons/wsmousevar.h>
70
71 /*
72 * Absolute-mode packets are decoded and passed around using
73 * the following structure.
74 */
75 struct synaptics_packet {
76 signed short sp_x; /* Unscaled absolute X/Y coordinates */
77 signed short sp_y;
78 u_char sp_z; /* Z (pressure) */
79 u_char sp_w; /* W (contact patch width) */
80 char sp_left; /* Left mouse button status */
81 char sp_right; /* Right mouse button status */
82 char sp_middle; /* Middle button status (possibly emulated) */
83 char sp_up; /* Up button status */
84 char sp_down; /* Down button status */
85 };
86
87 static int pms_synaptics_send_command(pckbport_tag_t, pckbport_slot_t, u_char);
88 static void pms_synaptics_input(void *, int);
89 static void pms_synaptics_process_packet(struct pms_softc *,
90 struct synaptics_packet *);
91 static void pms_sysctl_synaptics(struct sysctllog **);
92 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
93
94 /* Controled by sysctl. */
95 static int synaptics_up_down_emul = 2;
96 static int synaptics_up_down_motion_delta = 1;
97 static int synaptics_gesture_move = 200;
98 static int synaptics_gesture_length = 20;
99 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
100 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
101 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
102 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
103 static int synaptics_edge_motion_delta = 32;
104 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
105 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
106 static int synaptics_two_fingers_emul = 0;
107 static int synaptics_scale_x = 16;
108 static int synaptics_scale_y = 16;
109 static int synaptics_max_speed_x = 32;
110 static int synaptics_max_speed_y = 32;
111 static int synaptics_movement_threshold = 4;
112
113 /* Sysctl nodes. */
114 static int synaptics_up_down_emul_nodenum;
115 static int synaptics_up_down_motion_delta_nodenum;
116 static int synaptics_gesture_move_nodenum;
117 static int synaptics_gesture_length_nodenum;
118 static int synaptics_edge_left_nodenum;
119 static int synaptics_edge_right_nodenum;
120 static int synaptics_edge_top_nodenum;
121 static int synaptics_edge_bottom_nodenum;
122 static int synaptics_edge_motion_delta_nodenum;
123 static int synaptics_finger_high_nodenum;
124 static int synaptics_finger_low_nodenum;
125 static int synaptics_two_fingers_emul_nodenum;
126 static int synaptics_scale_x_nodenum;
127 static int synaptics_scale_y_nodenum;
128 static int synaptics_max_speed_x_nodenum;
129 static int synaptics_max_speed_y_nodenum;
130 static int synaptics_movement_threshold_nodenum;
131
132 int
133 pms_synaptics_probe_init(void *vsc)
134 {
135 struct pms_softc *psc = vsc;
136 struct synaptics_softc *sc = &psc->u.synaptics;
137 u_char cmd[2], resp[3];
138 int res, ver_minor, ver_major;
139 struct sysctllog *clog = NULL;
140
141 res = pms_synaptics_send_command(psc->sc_kbctag, psc->sc_kbcslot,
142 SYNAPTICS_IDENTIFY_TOUCHPAD);
143 cmd[0] = PMS_SEND_DEV_STATUS;
144 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
145 resp, 0);
146 if (res) {
147 #ifdef SYNAPTICSDEBUG
148 aprint_error("%s: synaptics_probe: Identify Touchpad error.\n",
149 psc->sc_dev.dv_xname);
150 #endif
151 /*
152 * Reset device in case the probe confused it.
153 */
154 doreset:
155 cmd[0] = PMS_RESET;
156 (void) pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
157 1, 2, resp, 1);
158 return (res);
159 }
160
161 if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
162 #ifdef SYNAPTICSDEBUG
163 printf("%s: synaptics_probe: Not synaptics.\n",
164 psc->sc_dev.dv_xname);
165 #endif
166 res = 1;
167 goto doreset;
168 }
169
170 sc->flags = 0;
171
172 /* Check for minimum version and print a nice message. */
173 ver_major = resp[2] & 0x0f;
174 ver_minor = resp[0];
175 aprint_normal("%s: Synaptics touchpad version %d.%d\n",
176 psc->sc_dev.dv_xname, ver_major, ver_minor);
177 if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
178 /* No capability query support. */
179 sc->caps = 0;
180 goto done;
181 }
182
183 /* Query the hardware capabilities. */
184 res = pms_synaptics_send_command(psc->sc_kbctag, psc->sc_kbcslot,
185 SYNAPTICS_READ_CAPABILITIES);
186 cmd[0] = PMS_SEND_DEV_STATUS;
187 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
188 resp, 0);
189 if (res) {
190 /* Hmm, failed to get capabilites. */
191 aprint_error("%s: synaptics_probe: Failed to query "
192 "capabilities.\n", psc->sc_dev.dv_xname);
193 goto doreset;
194 }
195
196 sc->caps = (resp[0] << 8) | resp[2];
197
198 if (sc->caps & SYNAPTICS_CAP_MBUTTON)
199 sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
200
201 if (sc->caps & SYNAPTICS_CAP_4BUTTON)
202 sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
203
204 if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
205 #ifdef SYNAPTICSDEBUG
206 aprint_normal("%s: synaptics_probe: Capabilities 0x%04x.\n",
207 psc->sc_dev.dv_xname, sc->caps);
208 #endif
209 if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH) /*XXX: Not supported*/
210 sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
211
212 if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
213 sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
214
215 if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
216 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
217
218 /* Ask about extra buttons to detect up/down. */
219 if (sc->caps & SYNAPTICS_CAP_EXTNUM) {
220 res = pms_synaptics_send_command(psc->sc_kbctag,
221 psc->sc_kbcslot, SYNAPTICS_EXTENDED_QUERY);
222 cmd[0] = PMS_SEND_DEV_STATUS;
223 res |= pckbport_poll_cmd(psc->sc_kbctag,
224 psc->sc_kbcslot, cmd, 1, 3, resp, 0);
225 #ifdef SYNAPTICSDEBUG
226 if (res == 0)
227 aprint_normal("%s: synaptics_probe: Extended "
228 "Capabilities 0x%02x.\n",
229 psc->sc_dev.dv_xname, resp[1]);
230 #endif
231 if (!res && (resp[1] >> 4) >= 2) {
232 /* Yes. */
233 sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
234 }
235 }
236 }
237
238 if (sc->flags) {
239 const char comma[] = ", ";
240 const char *sep = "";
241 aprint_normal("%s: ", psc->sc_dev.dv_xname);
242 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
243 aprint_normal("%sMiddle button", sep);
244 sep = comma;
245 }
246 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
247 aprint_normal("%sButtons 4/5", sep);
248 sep = comma;
249 }
250 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) {
251 aprint_normal("%sUp/down buttons", sep);
252 sep = comma;
253 }
254 if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) {
255 aprint_normal("%sPalm detect", sep);
256 sep = comma;
257 }
258 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER)
259 aprint_normal("%sMulti-finger", sep);
260
261 aprint_normal("\n");
262 }
263
264 done:
265 pms_sysctl_synaptics(&clog);
266 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
267 pms_synaptics_input, psc, psc->sc_dev.dv_xname);
268
269 return (0);
270 }
271
272 void
273 pms_synaptics_enable(void *vsc)
274 {
275 struct pms_softc *psc = vsc;
276 struct synaptics_softc *sc = &psc->u.synaptics;
277 u_char cmd[2];
278 int res;
279
280 /*
281 * Enable Absolute mode with W (width) reporting, and set
282 * the packet rate to maximum (80 packets per second).
283 */
284 res = pms_synaptics_send_command(psc->sc_kbctag, psc->sc_kbcslot,
285 SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE);
286 cmd[0] = PMS_SET_SAMPLE;
287 cmd[1] = 0x14; /* doit */
288 res |= pckbport_enqueue_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 2, 0,
289 1, NULL);
290 sc->up_down = 0;
291 sc->prev_fingers = 0;
292 sc->gesture_start_x = sc->gesture_start_y = 0;
293 sc->gesture_start_packet = 0;
294 sc->gesture_tap_packet = 0;
295 sc->gesture_type = 0;
296 sc->gesture_buttons = 0;
297 sc->rem_x = sc->rem_y = 0;
298 sc->movement_history = 0;
299 if (res) {
300 printf("%s: synaptics_enable: Error enabling device.\n",
301 psc->sc_dev.dv_xname);
302 }
303 }
304
305 void
306 pms_synaptics_resume(void *vsc)
307 {
308 struct pms_softc *psc = vsc;
309 unsigned char cmd[1],resp[2] = { 0,0 };
310 int res;
311
312 cmd[0] = PMS_RESET;
313 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
314 resp, 1);
315 printf("%s: pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n",
316 psc->sc_dev.dv_xname, res, resp[0], resp[1]);
317 }
318
319 static void
320 pms_sysctl_synaptics(struct sysctllog **clog)
321 {
322 int rc, root_num;
323 struct sysctlnode *node;
324
325 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
326 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
327 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
328 goto err;
329
330 if ((rc = sysctl_createv(clog, 0, NULL, &node,
331 CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
332 SYSCTL_DESCR("Synaptics touchpad controls"),
333 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
334 goto err;
335
336 root_num = node->sysctl_num;
337
338 if ((rc = sysctl_createv(clog, 0, NULL, &node,
339 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
340 CTLTYPE_INT, "up_down_emulation",
341 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
342 pms_sysctl_synaptics_verify, 0,
343 &synaptics_up_down_emul,
344 0, CTL_HW, root_num, CTL_CREATE,
345 CTL_EOL)) != 0)
346 goto err;
347
348 synaptics_up_down_emul_nodenum = node->sysctl_num;
349
350 if ((rc = sysctl_createv(clog, 0, NULL, &node,
351 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
352 CTLTYPE_INT, "up_down_motion_delta",
353 SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
354 pms_sysctl_synaptics_verify, 0,
355 &synaptics_up_down_motion_delta,
356 0, CTL_HW, root_num, CTL_CREATE,
357 CTL_EOL)) != 0)
358 goto err;
359
360 synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
361
362 if ((rc = sysctl_createv(clog, 0, NULL, &node,
363 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
364 CTLTYPE_INT, "gesture_move",
365 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
366 pms_sysctl_synaptics_verify, 0,
367 &synaptics_gesture_move,
368 0, CTL_HW, root_num, CTL_CREATE,
369 CTL_EOL)) != 0)
370 goto err;
371
372 synaptics_gesture_move_nodenum = node->sysctl_num;
373
374 if ((rc = sysctl_createv(clog, 0, NULL, &node,
375 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
376 CTLTYPE_INT, "gesture_length",
377 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
378 pms_sysctl_synaptics_verify, 0,
379 &synaptics_gesture_length,
380 0, CTL_HW, root_num, CTL_CREATE,
381 CTL_EOL)) != 0)
382 goto err;
383
384 synaptics_gesture_length_nodenum = node->sysctl_num;
385
386 if ((rc = sysctl_createv(clog, 0, NULL, &node,
387 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
388 CTLTYPE_INT, "edge_left",
389 SYSCTL_DESCR("Define left edge of touchpad"),
390 pms_sysctl_synaptics_verify, 0,
391 &synaptics_edge_left,
392 0, CTL_HW, root_num, CTL_CREATE,
393 CTL_EOL)) != 0)
394 goto err;
395
396 synaptics_edge_left_nodenum = node->sysctl_num;
397
398 if ((rc = sysctl_createv(clog, 0, NULL, &node,
399 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
400 CTLTYPE_INT, "edge_right",
401 SYSCTL_DESCR("Define right edge of touchpad"),
402 pms_sysctl_synaptics_verify, 0,
403 &synaptics_edge_right,
404 0, CTL_HW, root_num, CTL_CREATE,
405 CTL_EOL)) != 0)
406 goto err;
407
408 synaptics_edge_right_nodenum = node->sysctl_num;
409
410 if ((rc = sysctl_createv(clog, 0, NULL, &node,
411 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
412 CTLTYPE_INT, "edge_top",
413 SYSCTL_DESCR("Define top edge of touchpad"),
414 pms_sysctl_synaptics_verify, 0,
415 &synaptics_edge_top,
416 0, CTL_HW, root_num, CTL_CREATE,
417 CTL_EOL)) != 0)
418 goto err;
419
420 synaptics_edge_top_nodenum = node->sysctl_num;
421
422 if ((rc = sysctl_createv(clog, 0, NULL, &node,
423 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
424 CTLTYPE_INT, "edge_bottom",
425 SYSCTL_DESCR("Define bottom edge of touchpad"),
426 pms_sysctl_synaptics_verify, 0,
427 &synaptics_edge_bottom,
428 0, CTL_HW, root_num, CTL_CREATE,
429 CTL_EOL)) != 0)
430 goto err;
431
432 synaptics_edge_bottom_nodenum = node->sysctl_num;
433
434 if ((rc = sysctl_createv(clog, 0, NULL, &node,
435 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
436 CTLTYPE_INT, "edge_motion_delta",
437 SYSCTL_DESCR("Define edge motion rate"),
438 pms_sysctl_synaptics_verify, 0,
439 &synaptics_edge_motion_delta,
440 0, CTL_HW, root_num, CTL_CREATE,
441 CTL_EOL)) != 0)
442 goto err;
443
444 synaptics_edge_motion_delta_nodenum = node->sysctl_num;
445
446 if ((rc = sysctl_createv(clog, 0, NULL, &node,
447 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
448 CTLTYPE_INT, "finger_high",
449 SYSCTL_DESCR("Define finger applied pressure threshold"),
450 pms_sysctl_synaptics_verify, 0,
451 &synaptics_finger_high,
452 0, CTL_HW, root_num, CTL_CREATE,
453 CTL_EOL)) != 0)
454 goto err;
455
456 synaptics_finger_high_nodenum = node->sysctl_num;
457
458 if ((rc = sysctl_createv(clog, 0, NULL, &node,
459 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
460 CTLTYPE_INT, "finger_low",
461 SYSCTL_DESCR("Define finger removed pressure threshold"),
462 pms_sysctl_synaptics_verify, 0,
463 &synaptics_finger_low,
464 0, CTL_HW, root_num, CTL_CREATE,
465 CTL_EOL)) != 0)
466 goto err;
467
468 synaptics_finger_low_nodenum = node->sysctl_num;
469
470 if ((rc = sysctl_createv(clog, 0, NULL, &node,
471 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
472 CTLTYPE_INT, "two_fingers_emulation",
473 SYSCTL_DESCR("Map two fingers to middle button"),
474 pms_sysctl_synaptics_verify, 0,
475 &synaptics_two_fingers_emul,
476 0, CTL_HW, root_num, CTL_CREATE,
477 CTL_EOL)) != 0)
478 goto err;
479
480 synaptics_two_fingers_emul_nodenum = node->sysctl_num;
481
482 if ((rc = sysctl_createv(clog, 0, NULL, &node,
483 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
484 CTLTYPE_INT, "scale_x",
485 SYSCTL_DESCR("Horizontal movement scale factor"),
486 pms_sysctl_synaptics_verify, 0,
487 &synaptics_scale_x,
488 0, CTL_HW, root_num, CTL_CREATE,
489 CTL_EOL)) != 0)
490 goto err;
491
492 synaptics_scale_x_nodenum = node->sysctl_num;
493
494 if ((rc = sysctl_createv(clog, 0, NULL, &node,
495 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
496 CTLTYPE_INT, "scale_y",
497 SYSCTL_DESCR("Vertical movement scale factor"),
498 pms_sysctl_synaptics_verify, 0,
499 &synaptics_scale_y,
500 0, CTL_HW, root_num, CTL_CREATE,
501 CTL_EOL)) != 0)
502 goto err;
503
504 synaptics_scale_y_nodenum = node->sysctl_num;
505
506 if ((rc = sysctl_createv(clog, 0, NULL, &node,
507 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
508 CTLTYPE_INT, "max_speed_x",
509 SYSCTL_DESCR("Horizontal movement maximum speed"),
510 pms_sysctl_synaptics_verify, 0,
511 &synaptics_max_speed_x,
512 0, CTL_HW, root_num, CTL_CREATE,
513 CTL_EOL)) != 0)
514 goto err;
515
516 synaptics_max_speed_x_nodenum = node->sysctl_num;
517
518 if ((rc = sysctl_createv(clog, 0, NULL, &node,
519 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
520 CTLTYPE_INT, "max_speed_y",
521 SYSCTL_DESCR("Vertical movement maximum speed"),
522 pms_sysctl_synaptics_verify, 0,
523 &synaptics_max_speed_y,
524 0, CTL_HW, root_num, CTL_CREATE,
525 CTL_EOL)) != 0)
526 goto err;
527
528 synaptics_max_speed_y_nodenum = node->sysctl_num;
529
530 if ((rc = sysctl_createv(clog, 0, NULL, &node,
531 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
532 CTLTYPE_INT, "movement_threshold",
533 SYSCTL_DESCR("Minimum reported movement threshold"),
534 pms_sysctl_synaptics_verify, 0,
535 &synaptics_movement_threshold,
536 0, CTL_HW, root_num, CTL_CREATE,
537 CTL_EOL)) != 0)
538 goto err;
539
540 synaptics_movement_threshold_nodenum = node->sysctl_num;
541 return;
542
543 err:
544 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
545 }
546
547 static int
548 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
549 {
550 int error, t;
551 struct sysctlnode node;
552
553 node = *rnode;
554 t = *(int *)rnode->sysctl_data;
555 node.sysctl_data = &t;
556 error = sysctl_lookup(SYSCTLFN_CALL(&node));
557 if (error || newp == NULL)
558 return error;
559
560 /* Sanity check the params. */
561 if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
562 node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
563 if (t < 0 || t > 2)
564 return (EINVAL);
565 } else
566 if (node.sysctl_num == synaptics_gesture_length_nodenum ||
567 node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
568 node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
569 if (t < 0)
570 return (EINVAL);
571 } else
572 if (node.sysctl_num == synaptics_edge_left_nodenum ||
573 node.sysctl_num == synaptics_edge_bottom_nodenum) {
574 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
575 return (EINVAL);
576 } else
577 if (node.sysctl_num == synaptics_edge_right_nodenum ||
578 node.sysctl_num == synaptics_edge_top_nodenum) {
579 if (t < (SYNAPTICS_EDGE_MAX / 2))
580 return (EINVAL);
581 } else
582 if (node.sysctl_num == synaptics_scale_x_nodenum ||
583 node.sysctl_num == synaptics_scale_y_nodenum) {
584 if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
585 return (EINVAL);
586 } else
587 if (node.sysctl_num == synaptics_finger_high_nodenum) {
588 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
589 t < synaptics_finger_low)
590 return (EINVAL);
591 } else
592 if (node.sysctl_num == synaptics_finger_low_nodenum) {
593 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
594 t > synaptics_finger_high)
595 return (EINVAL);
596 } else
597 if (node.sysctl_num == synaptics_gesture_move_nodenum ||
598 node.sysctl_num == synaptics_movement_threshold_nodenum) {
599 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
600 return (EINVAL);
601 } else
602 return (EINVAL);
603
604 *(int *)rnode->sysctl_data = t;
605
606 return (0);
607 }
608
609 static int
610 pms_synaptics_send_command(pckbport_tag_t tag, pckbport_slot_t slot,
611 u_char syn_cmd)
612 {
613 u_char cmd[2];
614 int res;
615
616 /*
617 * Need to send 4 Set Resolution commands, with the argument
618 * encoded in the bottom most 2 bits.
619 */
620 cmd[0] = PMS_SET_RES;
621 cmd[1] = syn_cmd >> 6;
622 res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
623
624 cmd[0] = PMS_SET_RES;
625 cmd[1] = (syn_cmd & 0x30) >> 4;
626 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
627
628 cmd[0] = PMS_SET_RES;
629 cmd[1] = (syn_cmd & 0x0c) >> 2;
630 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
631
632 cmd[0] = PMS_SET_RES;
633 cmd[1] = (syn_cmd & 0x03);
634 res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
635
636 return (res);
637 }
638
639 /* Masks for the first byte of a packet */
640 #define PMS_LBUTMASK 0x01
641 #define PMS_RBUTMASK 0x02
642 #define PMS_MBUTMASK 0x04
643
644 static void
645 pms_synaptics_input(void *vsc, int data)
646 {
647 struct pms_softc *psc = vsc;
648 struct synaptics_softc *sc = &psc->u.synaptics;
649 struct timeval diff;
650 struct synaptics_packet sp;
651 int s;
652
653 if (!psc->sc_enabled) {
654 /* Interrupts are not expected. Discard the byte. */
655 return;
656 }
657
658 s = splclock();
659 psc->current = mono_time;
660 splx(s);
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 SYNAPTICS_DEBUG
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