[BACK]Return to synaptics.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / pckbport

Annotation of src/sys/dev/pckbport/synaptics.c, Revision 1.8

1.8     ! christos    1: /*     $NetBSD: synaptics.c,v 1.3.4.4 2005/11/10 14:07:24 skrll Exp $  */
1.1       christos    2:
                      3: /*
1.3       scw         4:  * Copyright (c) 2005, Steve C. Woodford
1.1       christos    5:  * Copyright (c) 2004, Ales Krenek
                      6:  * Copyright (c) 2004, Kentaro A. Kurahone
                      7:  * All rights reserved.
1.5       perry       8:  *
1.1       christos    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:
1.3       scw        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:
1.4       scw        49: #include "opt_pms.h"
                     50:
1.1       christos   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:
1.3       scw        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:
1.1       christos   89: static int pms_synaptics_send_command(pckbport_tag_t, pckbport_slot_t, u_char);
                     90: static void pms_synaptics_input(void *, int);
1.3       scw        91: static void pms_synaptics_process_packet(struct pms_softc *,
                     92:                struct synaptics_packet *);
1.2       christos   93: static void pms_sysctl_synaptics(struct sysctllog **);
1.1       christos   94: static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
                     95:
                     96: /* Controled by sysctl. */
1.3       scw        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;
1.1       christos  114:
                    115: /* Sysctl nodes. */
1.3       scw       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;
1.1       christos  133:
                    134: int
                    135: pms_synaptics_probe_init(void *vsc)
                    136: {
1.3       scw       137:        struct pms_softc *psc = vsc;
                    138:        struct synaptics_softc *sc = &psc->u.synaptics;
1.1       christos  139:        u_char cmd[2], resp[3];
1.3       scw       140:        int res, ver_minor, ver_major;
1.2       christos  141:        struct sysctllog *clog = NULL;
1.1       christos  142:
1.3       scw       143:        res = pms_synaptics_send_command(psc->sc_kbctag, psc->sc_kbcslot,
1.1       christos  144:            SYNAPTICS_IDENTIFY_TOUCHPAD);
                    145:        cmd[0] = PMS_SEND_DEV_STATUS;
1.3       scw       146:        res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
                    147:            resp, 0);
1.1       christos  148:        if (res) {
                    149: #ifdef SYNAPTICSDEBUG
1.3       scw       150:                aprint_error("%s: synaptics_probe: Identify Touchpad error.\n",
                    151:                    psc->sc_dev.dv_xname);
1.1       christos  152: #endif
1.3       scw       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);
1.1       christos  161:        }
                    162:
                    163:        if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
                    164: #ifdef SYNAPTICSDEBUG
1.3       scw       165:                printf("%s: synaptics_probe: Not synaptics.\n",
                    166:                    psc->sc_dev.dv_xname);
1.1       christos  167: #endif
1.3       scw       168:                res = 1;
                    169:                goto doreset;
1.1       christos  170:        }
                    171:
1.3       scw       172:        sc->flags = 0;
                    173:
1.1       christos  174:        /* Check for minimum version and print a nice message. */
                    175:        ver_major = resp[2] & 0x0f;
                    176:        ver_minor = resp[0];
1.3       scw       177:        aprint_normal("%s: Synaptics touchpad version %d.%d\n",
                    178:            psc->sc_dev.dv_xname, ver_major, ver_minor);
1.1       christos  179:        if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
                    180:                /* No capability query support. */
1.3       scw       181:                sc->caps = 0;
1.1       christos  182:                goto done;
                    183:        }
                    184:
                    185:        /* Query the hardware capabilities. */
1.3       scw       186:        res = pms_synaptics_send_command(psc->sc_kbctag, psc->sc_kbcslot,
1.1       christos  187:            SYNAPTICS_READ_CAPABILITIES);
                    188:        cmd[0] = PMS_SEND_DEV_STATUS;
1.3       scw       189:        res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
                    190:            resp, 0);
1.1       christos  191:        if (res) {
                    192:                /* Hmm, failed to get capabilites. */
1.3       scw       193:                aprint_error("%s: synaptics_probe: Failed to query "
                    194:                    "capabilities.\n", psc->sc_dev.dv_xname);
                    195:                goto doreset;
1.1       christos  196:        }
                    197:
1.3       scw       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) {
1.1       christos  207: #ifdef SYNAPTICSDEBUG
1.3       scw       208:                aprint_normal("%s: synaptics_probe: Capabilities 0x%04x.\n",
                    209:                    psc->sc_dev.dv_xname, sc->caps);
1.1       christos  210: #endif
1.3       scw       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;
1.1       christos  219:
                    220:                /* Ask about extra buttons to detect up/down. */
1.3       scw       221:                if (sc->caps & SYNAPTICS_CAP_EXTNUM) {
                    222:                        res = pms_synaptics_send_command(psc->sc_kbctag,
                    223:                            psc->sc_kbcslot, SYNAPTICS_EXTENDED_QUERY);
1.1       christos  224:                        cmd[0] = PMS_SEND_DEV_STATUS;
1.3       scw       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) {
1.1       christos  234:                                /* Yes. */
1.3       scw       235:                                sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
1.1       christos  236:                        }
1.3       scw       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");
1.1       christos  264:        }
                    265:
                    266: done:
1.2       christos  267:        pms_sysctl_synaptics(&clog);
1.3       scw       268:        pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
                    269:            pms_synaptics_input, psc, psc->sc_dev.dv_xname);
                    270:
                    271:        return (0);
1.1       christos  272: }
                    273:
                    274: void
                    275: pms_synaptics_enable(void *vsc)
                    276: {
1.3       scw       277:        struct pms_softc *psc = vsc;
                    278:        struct synaptics_softc *sc = &psc->u.synaptics;
1.1       christos  279:        u_char cmd[2];
                    280:        int res;
                    281:
1.3       scw       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);
1.1       christos  288:        cmd[0] = PMS_SET_SAMPLE;
                    289:        cmd[1] = 0x14; /* doit */
1.3       scw       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:        }
1.1       christos  305: }
                    306:
                    307: void
                    308: pms_synaptics_resume(void *vsc)
                    309: {
1.3       scw       310:        struct pms_softc *psc = vsc;
                    311:        unsigned char cmd[1],resp[2] = { 0,0 };
                    312:        int res;
1.1       christos  313:
                    314:        cmd[0] = PMS_RESET;
1.3       scw       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]);
1.1       christos  319: }
                    320:
1.3       scw       321: static void
                    322: pms_sysctl_synaptics(struct sysctllog **clog)
1.1       christos  323: {
                    324:        int rc, root_num;
1.6       atatat    325:        const struct sysctlnode *node;
1.1       christos  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,
1.3       scw       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"),
1.1       christos  404:            pms_sysctl_synaptics_verify, 0,
1.3       scw       405:            &synaptics_edge_right,
1.1       christos  406:            0, CTL_HW, root_num, CTL_CREATE,
                    407:            CTL_EOL)) != 0)
                    408:                goto err;
                    409:
1.3       scw       410:        synaptics_edge_right_nodenum = node->sysctl_num;
1.1       christos  411:
                    412:        if ((rc = sysctl_createv(clog, 0, NULL, &node,
                    413:            CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1.3       scw       414:            CTLTYPE_INT, "edge_top",
                    415:            SYSCTL_DESCR("Define top edge of touchpad"),
1.1       christos  416:            pms_sysctl_synaptics_verify, 0,
1.3       scw       417:            &synaptics_edge_top,
1.1       christos  418:            0, CTL_HW, root_num, CTL_CREATE,
                    419:            CTL_EOL)) != 0)
1.3       scw       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;
1.1       christos  505:
1.3       scw       506:        synaptics_scale_y_nodenum = node->sysctl_num;
1.1       christos  507:
                    508:        if ((rc = sysctl_createv(clog, 0, NULL, &node,
                    509:            CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1.3       scw       510:            CTLTYPE_INT, "max_speed_x",
                    511:            SYSCTL_DESCR("Horizontal movement maximum speed"),
1.1       christos  512:            pms_sysctl_synaptics_verify, 0,
1.3       scw       513:            &synaptics_max_speed_x,
1.1       christos  514:            0, CTL_HW, root_num, CTL_CREATE,
                    515:            CTL_EOL)) != 0)
                    516:                goto err;
                    517:
1.3       scw       518:        synaptics_max_speed_x_nodenum = node->sysctl_num;
1.1       christos  519:
                    520:        if ((rc = sysctl_createv(clog, 0, NULL, &node,
                    521:            CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1.3       scw       522:            CTLTYPE_INT, "max_speed_y",
                    523:            SYSCTL_DESCR("Vertical movement maximum speed"),
1.1       christos  524:            pms_sysctl_synaptics_verify, 0,
1.3       scw       525:            &synaptics_max_speed_y,
1.1       christos  526:            0, CTL_HW, root_num, CTL_CREATE,
                    527:            CTL_EOL)) != 0)
                    528:                goto err;
                    529:
1.3       scw       530:        synaptics_max_speed_y_nodenum = node->sysctl_num;
1.1       christos  531:
                    532:        if ((rc = sysctl_createv(clog, 0, NULL, &node,
                    533:            CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1.3       scw       534:            CTLTYPE_INT, "movement_threshold",
                    535:            SYSCTL_DESCR("Minimum reported movement threshold"),
1.1       christos  536:            pms_sysctl_synaptics_verify, 0,
1.3       scw       537:            &synaptics_movement_threshold,
1.1       christos  538:            0, CTL_HW, root_num, CTL_CREATE,
                    539:            CTL_EOL)) != 0)
                    540:                goto err;
                    541:
1.3       scw       542:        synaptics_movement_threshold_nodenum = node->sysctl_num;
1.1       christos  543:        return;
                    544:
                    545: err:
1.3       scw       546:        aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
1.1       christos  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. */
1.3       scw       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) {
1.1       christos  571:                if (t < 0)
1.3       scw       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);
1.1       christos  583:        } else
1.3       scw       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);
1.1       christos  605:
1.3       scw       606:        *(int *)rnode->sysctl_data = t;
1.1       christos  607:
1.3       scw       608:        return (0);
1.1       christos  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:
1.3       scw       618:        /*
                    619:         * Need to send 4 Set Resolution commands, with the argument
1.1       christos  620:         * encoded in the bottom most 2 bits.
                    621:         */
1.3       scw       622:        cmd[0] = PMS_SET_RES;
                    623:        cmd[1] = syn_cmd >> 6;
1.1       christos  624:        res = pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
1.3       scw       625:
                    626:        cmd[0] = PMS_SET_RES;
                    627:        cmd[1] = (syn_cmd & 0x30) >> 4;
1.1       christos  628:        res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
1.3       scw       629:
                    630:        cmd[0] = PMS_SET_RES;
                    631:        cmd[1] = (syn_cmd & 0x0c) >> 2;
1.1       christos  632:        res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
1.3       scw       633:
                    634:        cmd[0] = PMS_SET_RES;
                    635:        cmd[1] = (syn_cmd & 0x03);
1.1       christos  636:        res |= pckbport_poll_cmd(tag, slot, cmd, 2, 0, NULL, 0);
                    637:
1.3       scw       638:        return (res);
1.1       christos  639: }
                    640:
1.3       scw       641: /* Masks for the first byte of a packet */
                    642: #define PMS_LBUTMASK 0x01
                    643: #define PMS_RBUTMASK 0x02
                    644: #define PMS_MBUTMASK 0x04
1.1       christos  645:
                    646: static void
                    647: pms_synaptics_input(void *vsc, int data)
                    648: {
1.3       scw       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;
1.1       christos  654:
1.3       scw       655:        if (!psc->sc_enabled) {
1.1       christos  656:                /* Interrupts are not expected.  Discard the byte. */
                    657:                return;
                    658:        }
                    659:
                    660:        s = splclock();
1.3       scw       661:        psc->current = mono_time;
1.1       christos  662:        splx(s);
                    663:
1.3       scw       664:        if (psc->inputstate > 0) {
                    665:                timersub(&psc->current, &psc->last, &diff);
1.1       christos  666:                if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
1.3       scw       667:                        printf("%s: pms_input: unusual delay (%ld.%06ld s), "
                    668:                            "scheduling reset\n", psc->sc_dev.dv_xname,
1.1       christos  669:                            (long)diff.tv_sec, (long)diff.tv_usec);
1.3       scw       670:                        psc->inputstate = 0;
                    671:                        psc->sc_enabled = 0;
                    672:                        wakeup(&psc->sc_enabled);
1.1       christos  673:                        return;
                    674:                }
                    675:        }
1.3       scw       676:        psc->last = psc->current;
1.1       christos  677:
1.3       scw       678:        switch (psc->inputstate) {
                    679:        case 0:
1.1       christos  680:                if ((data & 0xc8) != 0x80) {
1.2       christos  681: #ifdef SYNAPTICSDEBUG
1.3       scw       682:                        printf("%s: pms_input: 0x%02x out of sync\n",
                    683:                            psc->sc_dev.dv_xname, data);
1.2       christos  684: #endif
1.1       christos  685:                        return; /* not in sync yet, discard input */
                    686:                }
1.3       scw       687:                /*FALLTHROUGH*/
                    688:
                    689:        case 3:
1.5       perry     690:                if ((data & 8) == 8) {
1.7       rpaulo    691: #ifdef SYNAPTICSDEBUG
1.3       scw       692:                        printf("%s: pms_input: dropped in relative mode, "
                    693:                            "reset\n", psc->sc_dev.dv_xname);
1.2       christos  694: #endif
1.3       scw       695:                        psc->inputstate = 0;
                    696:                        psc->sc_enabled = 0;
                    697:                        wakeup(&psc->sc_enabled);
1.1       christos  698:                        return;
                    699:                }
                    700:        }
                    701:
1.3       scw       702:        psc->packet[psc->inputstate++] = data & 0xff;
                    703:        if (psc->inputstate == 6) {
                    704:                /*
                    705:                 * We have a complete packet.
                    706:                 * Extract the pertinent details.
                    707:                 */
                    708:                psc->inputstate = 0;
                    709:
                    710:                /* Absolute X/Y coordinates of finger */
                    711:                sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
                    712:                    ((psc->packet[3] & 0x10) << 8);
                    713:                sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
                    714:                    ((psc->packet[3] & 0x20) << 7);
                    715:
                    716:                /* Pressure */
                    717:                sp.sp_z = psc->packet[2];
                    718:
                    719:                /* Width of finger */
                    720:                sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
                    721:                    ((psc->packet[0] & 0x04) >> 1) +
                    722:                    ((psc->packet[3] & 0x04) >> 2);
                    723:
                    724:                /* Left/Right button handling. */
                    725:                sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
                    726:                sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
                    727:
                    728:                /* Up/Down buttons. */
                    729:                if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
                    730:                        /* Old up/down buttons. */
                    731:                        sp.sp_up = sp.sp_left ^
                    732:                            (psc->packet[3] & PMS_LBUTMASK);
                    733:                        sp.sp_down = sp.sp_right ^
                    734:                            (psc->packet[3] & PMS_RBUTMASK);
                    735:                } else
                    736:                if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
                    737:                    ((psc->packet[0] & PMS_RBUTMASK) ^
                    738:                    (psc->packet[3] & PMS_RBUTMASK))) {
                    739:                        /* New up/down button. */
                    740:                        sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
                    741:                        sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
                    742:                } else {
                    743:                        sp.sp_up = 0;
                    744:                        sp.sp_down = 0;
1.1       christos  745:                }
1.3       scw       746:
                    747:                /* Middle button. */
                    748:                if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
                    749:                        /* Old style Middle Button. */
                    750:                        sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
                    751:                            (psc->packet[3] & PMS_LBUTMASK);
                    752:                } else
                    753:                if (synaptics_up_down_emul == 1) {
                    754:                        /* Do middle button emulation using up/down buttons */
                    755:                        sp.sp_middle = sp.sp_up | sp.sp_down;
                    756:                        sp.sp_up = sp.sp_down = 0;
                    757:                } else
                    758:                        sp.sp_middle = 0;
                    759:
                    760:                /*
                    761:                 * Go process the new packet
                    762:                 */
                    763:                pms_synaptics_process_packet(psc, &sp);
1.1       christos  764:        }
1.3       scw       765: }
1.1       christos  766:
1.3       scw       767: static __inline int
                    768: synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
                    769:     int *palmp)
                    770: {
                    771:        int fingers;
                    772:
                    773:        /* Assume no palm */
                    774:        *palmp = 0;
                    775:
                    776:        /*
                    777:         * Apply some hysteresis when checking for a finger.
                    778:         * When the finger is first applied, we ignore it until the
                    779:         * pressure exceeds the 'high' threshold. The finger is considered
                    780:         * removed only when pressure falls beneath the 'low' threshold.
                    781:         */
                    782:        if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
                    783:            (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
                    784:                fingers = 1;
                    785:        else
                    786:                fingers = 0;
                    787:
                    788:        /*
                    789:         * If the pad can't do palm detection, skip the rest.
                    790:         */
                    791:        if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
                    792:                return (fingers);
                    793:
                    794:        /*
                    795:         * Palm detection
                    796:         */
                    797:        if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
                    798:            sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
                    799:                *palmp = 1;
                    800:
                    801:        if (sc->prev_fingers == 0 &&
                    802:            (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
                    803:             sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
                    804:                /*
                    805:                 * Contact area or pressure is too great to be a finger.
                    806:                 * Just ignore it for now.
                    807:                 */
                    808:                return (0);
                    809:        }
                    810:
                    811:        /*
                    812:         * Detect 2 and 3 fingers if supported, but only if multiple
                    813:         * fingers appear within the tap gesture time period.
                    814:         */
                    815:        if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
                    816:            SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) {
                    817:                switch (sp->sp_w) {
                    818:                case SYNAPTICS_WIDTH_TWO_FINGERS:
                    819:                        fingers = 2;
                    820:                        break;
                    821:
                    822:                case SYNAPTICS_WIDTH_THREE_OR_MORE:
                    823:                        fingers = 3;
                    824:                        break;
                    825:
                    826:                case SYNAPTICS_WIDTH_PEN:
                    827:                        fingers = 1;
                    828:                        break;
                    829:
                    830:                default:
                    831:                        /*
                    832:                         * The width value can report spurious single-finger
                    833:                         * events after a multi-finger event.
                    834:                         */
                    835:                        if (sc->prev_fingers > 1)
                    836:                                fingers = sc->prev_fingers;
                    837:                        else
                    838:                                fingers = 1;
                    839:                        break;
1.1       christos  840:                }
                    841:        }
1.3       scw       842:
                    843:        return (fingers);
1.1       christos  844: }
                    845:
1.3       scw       846: static __inline void
                    847: synaptics_gesture_detect(struct synaptics_softc *sc,
                    848:     struct synaptics_packet *sp, int fingers)
                    849: {
                    850:        int gesture_len, gesture_move_x, gesture_move_y, gesture_buttons;
                    851:        int set_buttons;
                    852:
                    853:        gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
                    854:        gesture_buttons = sc->gesture_buttons;
1.1       christos  855:
1.3       scw       856:        if (fingers && sc->prev_fingers == 0) {
                    857:                /*
                    858:                 * Finger was just applied.
                    859:                 * If the previous gesture was a single-click, set things
                    860:                 * up to deal with a possible drag or double-click gesture.
                    861:                 * Basically, if the finger is removed again within
                    862:                 * 'synaptics_gesture_length' packets, this is treated
                    863:                 * as a double-click. Otherwise we will emulate holding
                    864:                 * the left button down whilst dragging the mouse.
                    865:                 */
                    866:                if (SYN_IS_SINGLE_TAP(sc->gesture_type))
                    867:                        sc->gesture_type |= SYN_GESTURE_DRAG;
                    868:
                    869:                sc->gesture_start_x = sp->sp_x;
                    870:                sc->gesture_start_y = sp->sp_y;
                    871:                sc->gesture_start_packet = sc->total_packets;
                    872:        } else
                    873:        if (fingers == 0 && sc->prev_fingers != 0) {
                    874:                /*
                    875:                 * Finger was just removed.
                    876:                 * Check if the contact time and finger movement were
                    877:                 * small enough to qualify as a gesture.
                    878:                 * Ignore finger movement if multiple fingers were
                    879:                 * detected (the pad may report coordinates for any
                    880:                 * of the fingers).
                    881:                 */
                    882:                gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
                    883:                gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
                    884:
                    885:                if (gesture_len < synaptics_gesture_length &&
                    886:                    (sc->prev_fingers > 1 ||
                    887:                    (gesture_move_x < synaptics_gesture_move &&
                    888:                     gesture_move_y < synaptics_gesture_move))) {
                    889:                        /*
                    890:                         * Looking good so far.
                    891:                         */
                    892:                        if (SYN_IS_DRAG(sc->gesture_type)) {
                    893:                                /*
                    894:                                 * Promote this gesture to double-click.
                    895:                                 */
                    896:                                sc->gesture_type |= SYN_GESTURE_DOUBLE;
                    897:                                sc->gesture_type &= ~SYN_GESTURE_SINGLE;
                    898:                        } else {
                    899:                                /*
                    900:                                 * Single tap gesture. Set the tap length timer
                    901:                                 * and flag a single-click.
                    902:                                 */
                    903:                                sc->gesture_tap_packet = sc->total_packets;
                    904:                                sc->gesture_type |= SYN_GESTURE_SINGLE;
                    905:
                    906:                                /*
                    907:                                 * The gesture can be modified depending on
                    908:                                 * the number of fingers detected.
                    909:                                 *
                    910:                                 * 1: Normal left button emulation.
                    911:                                 * 2: Either middle button or right button
                    912:                                 *    depending on the value of the two_fingers
                    913:                                 *    sysctl variable.
                    914:                                 * 3: Right button.
                    915:                                 */
                    916:                                switch (sc->prev_fingers) {
                    917:                                case 2:
                    918:                                        if (synaptics_two_fingers_emul == 1)
                    919:                                                gesture_buttons |= PMS_RBUTMASK;
                    920:                                        else
                    921:                                        if (synaptics_two_fingers_emul == 2)
                    922:                                                gesture_buttons |= PMS_MBUTMASK;
                    923:                                        break;
                    924:                                case 3:
                    925:                                        gesture_buttons |= PMS_RBUTMASK;
                    926:                                        break;
                    927:                                default:
                    928:                                        gesture_buttons |= PMS_LBUTMASK;
                    929:                                        break;
                    930:                                }
1.1       christos  931:                        }
                    932:                }
                    933:
1.3       scw       934:                /*
                    935:                 * Always clear drag state when the finger is removed.
                    936:                 */
                    937:                sc->gesture_type &= ~SYN_GESTURE_DRAG;
                    938:        }
                    939:
                    940:        if (sc->gesture_type == 0) {
                    941:                /*
                    942:                 * There is no gesture in progress.
                    943:                 * Clear emulated button state.
                    944:                 */
                    945:                sc->gesture_buttons = 0;
                    946:                return;
                    947:        }
                    948:
                    949:        /*
                    950:         * A gesture is in progress.
                    951:         */
                    952:        set_buttons = 0;
                    953:
                    954:        if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
                    955:                /*
                    956:                 * Single-click.
                    957:                 * Activate the relevant button(s) until the
                    958:                 * gesture tap timer has expired.
                    959:                 */
                    960:                if (SYN_TIME(sc, sc->gesture_tap_packet) <
                    961:                    synaptics_gesture_length)
                    962:                        set_buttons = 1;
                    963:                else
                    964:                        sc->gesture_type &= ~SYN_GESTURE_SINGLE;
                    965:        } else
                    966:        if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
                    967:                /*
                    968:                 * Double-click.
                    969:                 * Activate the relevant button(s) once.
                    970:                 */
                    971:                set_buttons = 1;
                    972:                sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
                    973:        }
                    974:
                    975:        if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
                    976:                /*
                    977:                 * Single-click and drag.
                    978:                 * Maintain button state until the finger is removed.
                    979:                 */
                    980:                sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
                    981:                sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
                    982:                sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
                    983:        }
                    984:
                    985:        sc->gesture_buttons = gesture_buttons;
                    986: }
                    987:
                    988: static __inline int
                    989: synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value)
                    990: {
                    991:        int a, b, rv, count;
1.1       christos  992:
1.3       scw       993:        count = sc->total_packets;
                    994:
                    995:        /*
                    996:         * Once we've accumulated at least SYN_HIST_SIZE values, combine
                    997:         * each new value with the previous two and return the average.
                    998:         *
                    999:         * This is necessary when the touchpad is operating in 80 packets
                   1000:         * per second mode, as it performs little internal filtering on
                   1001:         * reported values.
                   1002:         *
                   1003:         * Using a rolling average helps to filter out jitter caused by
                   1004:         * tiny finger movements.
                   1005:         */
                   1006:        if (sc->movement_history >= SYN_HIST_SIZE) {
                   1007:                a = (history[(count + 0) % SYN_HIST_SIZE] +
                   1008:                    history[(count + 1) % SYN_HIST_SIZE]) / 2;
                   1009:
                   1010:                b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
                   1011:
                   1012:                rv = b - a;
                   1013:
                   1014:                /*
                   1015:                 * Don't report the movement if it's below a certain
                   1016:                 * threshold.
                   1017:                 */
                   1018:                if (abs(rv) < synaptics_movement_threshold)
                   1019:                        rv = 0;
                   1020:        } else
                   1021:                rv = 0;
                   1022:
                   1023:        /*
                   1024:         * Add the new value to the history buffer.
                   1025:         */
                   1026:        history[(count + 1) % SYN_HIST_SIZE] = value;
                   1027:
                   1028:        return (rv);
                   1029: }
                   1030:
                   1031: /* Edge detection */
                   1032: #define        SYN_EDGE_TOP            1
                   1033: #define        SYN_EDGE_BOTTOM         2
                   1034: #define        SYN_EDGE_LEFT           4
                   1035: #define        SYN_EDGE_RIGHT          8
                   1036:
                   1037: static __inline int
                   1038: synaptics_check_edge(int x, int y)
                   1039: {
                   1040:        int rv = 0;
                   1041:
                   1042:        if (x < synaptics_edge_left)
                   1043:                rv |= SYN_EDGE_LEFT;
                   1044:        else
                   1045:        if (x > synaptics_edge_right)
                   1046:                rv |= SYN_EDGE_RIGHT;
                   1047:
                   1048:        if (y < synaptics_edge_bottom)
                   1049:                rv |= SYN_EDGE_BOTTOM;
                   1050:        else
                   1051:        if (y > synaptics_edge_top)
                   1052:                rv |= SYN_EDGE_TOP;
                   1053:
                   1054:        return (rv);
                   1055: }
                   1056:
                   1057: static __inline int
                   1058: synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
                   1059: {
                   1060:
                   1061:        /*
                   1062:         * When edge motion is enabled, synaptics_edge_motion_delta is
                   1063:         * combined with the current delta, together with the direction
                   1064:         * in which to simulate the motion. The result is added to
                   1065:         * the delta derived from finger movement. This provides a smooth
                   1066:         * transition from finger movement to edge motion.
                   1067:         */
                   1068:        delta = synaptics_edge_motion_delta + (dir * delta);
                   1069:        if (delta < 0)
                   1070:                return (0);
                   1071:        if (delta > synaptics_edge_motion_delta)
                   1072:                return (synaptics_edge_motion_delta);
                   1073:        return (delta);
                   1074: }
                   1075:
                   1076: static __inline int
                   1077: synaptics_scale(int delta, int scale, int *remp)
                   1078: {
                   1079:        int rv;
                   1080:
                   1081:        /*
                   1082:         * Scale the raw delta in Synaptics coordinates (0-6143) into
                   1083:         * something more reasonable by dividing the raw delta by a
                   1084:         * scale factor. Any remainder from the previous scale result
                   1085:         * is added to the current delta before scaling.
                   1086:         * This prevents loss of resolution for very small/slow
                   1087:         * movements of the finger.
                   1088:         */
                   1089:        delta += *remp;
                   1090:        rv = delta / scale;
                   1091:        *remp = delta % scale;
                   1092:
                   1093:        return (rv);
                   1094: }
                   1095:
                   1096: static __inline void
                   1097: synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
                   1098:     int *dxp, int *dyp)
                   1099: {
                   1100:        int dx, dy, edge;
                   1101:
                   1102:        /*
                   1103:         * Compute the next values of dx and dy
                   1104:         */
                   1105:        dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x);
                   1106:        dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y);
                   1107:
                   1108:        /*
                   1109:         * If we're dealing with a drag gesture, and the finger moves to
                   1110:         * the edge of the touchpad, apply edge motion emulation if it
                   1111:         * is enabled.
                   1112:         */
                   1113:        if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
                   1114:                edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
                   1115:
                   1116:                if (edge & SYN_EDGE_LEFT)
                   1117:                        dx -= synaptics_edge_motion(sc, dx, 1);
                   1118:                if (edge & SYN_EDGE_RIGHT)
                   1119:                        dx += synaptics_edge_motion(sc, dx, -1);
                   1120:                if (edge & SYN_EDGE_BOTTOM)
                   1121:                        dy -= synaptics_edge_motion(sc, dy, 1);
                   1122:                if (edge & SYN_EDGE_TOP)
                   1123:                        dy += synaptics_edge_motion(sc, dy, -1);
                   1124:        }
                   1125:
                   1126:        /*
                   1127:         * Apply scaling to both deltas
                   1128:         */
                   1129:        dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x);
                   1130:        dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y);
1.1       christos 1131:
1.3       scw      1132:        /*
                   1133:         * Clamp deltas to specified maximums.
                   1134:         */
                   1135:        if (dx > synaptics_max_speed_x)
                   1136:                dx = synaptics_max_speed_x;
                   1137:        if (dy > synaptics_max_speed_y)
                   1138:                dy = synaptics_max_speed_y;
1.1       christos 1139:
1.3       scw      1140:        *dxp = dx;
                   1141:        *dyp = dy;
1.1       christos 1142:
1.3       scw      1143:        sc->movement_history++;
                   1144: }
1.1       christos 1145:
1.3       scw      1146: static void
                   1147: pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
                   1148: {
                   1149:        struct synaptics_softc *sc = &psc->u.synaptics;
                   1150:        int dx, dy, dz;
                   1151:        int fingers, palm, buttons, changed;
                   1152:        int s;
1.1       christos 1153:
1.3       scw      1154:        /*
                   1155:         * Do Z-axis emulation using up/down buttons if required.
                   1156:         * Note that the pad will send a one second burst of packets
                   1157:         * when an up/down button is pressed and held. At the moment
                   1158:         * we don't deal with auto-repeat, so convert the burst into
                   1159:         * a one-shot.
                   1160:         */
                   1161:        dz = 0;
                   1162:        if (synaptics_up_down_emul == 2) {
                   1163:                if (sc->up_down == 0) {
                   1164:                        if (sp->sp_up && sp->sp_down) {
                   1165:                                /*
                   1166:                                 * Most up/down buttons will be actuated using
                   1167:                                 * a rocker switch, so we should never see
                   1168:                                 * them both simultaneously. But just in case,
                   1169:                                 * treat this situation as a middle button
                   1170:                                 * event.
                   1171:                                 */
                   1172:                                sp->sp_middle = 1;
                   1173:                        } else
                   1174:                        if (sp->sp_up)
                   1175:                                dz = -synaptics_up_down_motion_delta;
                   1176:                        else
                   1177:                        if (sp->sp_down)
                   1178:                                dz = synaptics_up_down_motion_delta;
1.1       christos 1179:                }
                   1180:
1.3       scw      1181:                sc->up_down = sp->sp_up | sp->sp_down;
                   1182:                sp->sp_up = sp->sp_down = 0;
                   1183:        }
                   1184:
                   1185:        /*
                   1186:         * Determine whether or not a finger is on the pad.
                   1187:         * On some pads, this will return the number of fingers
                   1188:         * detected.
                   1189:         */
                   1190:        fingers = synaptics_finger_detect(sc, sp, &palm);
                   1191:
                   1192:        /*
                   1193:         * Do gesture processing only if we didn't detect a palm.
                   1194:         */
                   1195:        if (palm == 0)
                   1196:                synaptics_gesture_detect(sc, sp, fingers);
                   1197:        else
                   1198:                sc->gesture_type = sc->gesture_buttons = 0;
1.1       christos 1199:
1.3       scw      1200:        /*
                   1201:         * Determine what buttons to report
                   1202:         */
                   1203:        buttons = (sp->sp_left ? 0x1 : 0) |
                   1204:            (sp->sp_middle ? 0x2 : 0) |
                   1205:            (sp->sp_right ? 0x4 : 0) |
                   1206:            (sp->sp_up ? 0x8 : 0) |
                   1207:            (sp->sp_down ? 0x10 : 0);
                   1208:        changed = buttons ^ psc->buttons;
                   1209:        psc->buttons = buttons;
1.1       christos 1210:
1.3       scw      1211:        sc->prev_fingers = fingers;
                   1212:        sc->total_packets++;
1.1       christos 1213:
1.3       scw      1214:        /*
                   1215:         * Do movement processing IFF we have a single finger and no palm.
                   1216:         */
                   1217:        if (fingers == 1 && palm == 0)
                   1218:                synaptics_movement(sc, sp, &dx, &dy);
                   1219:        else {
                   1220:                /*
                   1221:                 * No valid finger. Therefore no movement.
                   1222:                 */
                   1223:                sc->movement_history = 0;
                   1224:                sc->rem_x = sc->rem_y = 0;
                   1225:                dx = dy = 0;
                   1226:        }
1.1       christos 1227:
1.3       scw      1228:        /*
                   1229:         * Pass the final results up to wsmouse_input() if necessary.
                   1230:         */
                   1231:        if (dx || dy || dz || changed) {
                   1232:                s = spltty();
                   1233:                wsmouse_input(psc->sc_wsmousedev, buttons, dx, dy, dz,
                   1234:                    WSMOUSE_INPUT_DELTA);
                   1235:                splx(s);
1.1       christos 1236:        }
                   1237: }

CVSweb <webmaster@jp.NetBSD.org>