~jderose/ubuntu/raring/xserver-xorg-input-synaptics/clickpad

« back to all changes in this revision

Viewing changes to src/ps2comm.c

  • Committer: Package Import Robot
  • Author(s): Cyril Brulebois, Julien Cristau, Cyril Brulebois
  • Date: 2012-05-20 16:50:18 UTC
  • mfrom: (0.4.9)
  • mto: (61.1.1 quantal)
  • mto: This revision was merged to the branch mainline in revision 60.
  • Revision ID: package-import@ubuntu.com-20120520165018-ap60y9jh84nv75vp
Tags: 1.6.1-1
[ Julien Cristau ]
* The mtdev build-dep is linux-only (closes: #672572).  Thanks, Pino
  Toscano!

[ Cyril Brulebois ]
* New upstream release, from the 1.6 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
#include "ps2comm.h"
44
44
#include <xf86.h>
45
45
 
46
 
#define MAX_UNSYNC_PACKETS 10                           /* i.e. 10 to 60 bytes */
 
46
#define MAX_UNSYNC_PACKETS 10   /* i.e. 10 to 60 bytes */
47
47
/*
48
48
 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
49
49
 * section 2.3.2, which says that they should be valid regardless of the
91
91
 * Read a byte from the ps/2 port
92
92
 */
93
93
static Bool
94
 
ps2_getbyte(int fd, byte *b)
 
94
ps2_getbyte(int fd, byte * b)
95
95
{
96
96
    if (xf86WaitForInput(fd, 50000) > 0) {
97
 
        if (xf86ReadSerial(fd, b, 1) != 1) {
98
 
            PS2DBG(ErrorF("ps2_getbyte: No byte read\n"));
99
 
            return FALSE;
100
 
        }
101
 
        PS2DBG(ErrorF("ps2_getbyte: byte %02X read\n", *b));
102
 
        return TRUE;
 
97
        if (xf86ReadSerial(fd, b, 1) != 1) {
 
98
            PS2DBG(ErrorF("ps2_getbyte: No byte read\n"));
 
99
            return FALSE;
 
100
        }
 
101
        PS2DBG(ErrorF("ps2_getbyte: byte %02X read\n", *b));
 
102
        return TRUE;
103
103
    }
104
104
    PS2DBG(ErrorF("ps2_getbyte: timeout xf86WaitForInput\n"));
105
105
    return FALSE;
114
114
    byte ack;
115
115
 
116
116
    if (xf86WriteSerial(fd, &b, 1) != 1) {
117
 
        PS2DBG(ErrorF("ps2_putbyte: error xf86WriteSerial\n"));
118
 
        return FALSE;
 
117
        PS2DBG(ErrorF("ps2_putbyte: error xf86WriteSerial\n"));
 
118
        return FALSE;
119
119
    }
120
120
    PS2DBG(ErrorF("ps2_putbyte: byte %02X send\n", b));
121
121
    /* wait for an ACK */
122
122
    if (!ps2_getbyte(fd, &ack)) {
123
 
        return FALSE;
 
123
        return FALSE;
124
124
    }
125
125
    if (ack != PS2_ACK) {
126
 
        PS2DBG(ErrorF("ps2_putbyte: wrong acknowledge 0x%02x\n", ack));
127
 
        return FALSE;
 
126
        PS2DBG(ErrorF("ps2_putbyte: wrong acknowledge 0x%02x\n", ack));
 
127
        return FALSE;
128
128
    }
129
129
    return TRUE;
130
130
}
142
142
 
143
143
    /* initialize with 'inert' command */
144
144
    if (!ps2_putbyte(fd, PS2_CMD_SET_SCALING_1_1))
145
 
        return FALSE;
 
145
        return FALSE;
146
146
 
147
147
    /* send 4x 2-bits with set resolution command */
148
148
    for (i = 0; i < 4; i++) {
149
 
        if (!ps2_putbyte(fd, PS2_CMD_SET_RESOLUTION) ||
150
 
            !ps2_putbyte(fd, (cmd >> 6) & 0x3))
151
 
            return FALSE;
152
 
        cmd <<= 2;
 
149
        if (!ps2_putbyte(fd, PS2_CMD_SET_RESOLUTION) ||
 
150
            !ps2_putbyte(fd, (cmd >> 6) & 0x3))
 
151
            return FALSE;
 
152
        cmd <<= 2;
153
153
    }
154
154
    return TRUE;
155
155
}
161
161
ps2_send_cmd(int fd, byte c)
162
162
{
163
163
    PS2DBG(ErrorF("send command: 0x%02X\n", c));
164
 
    return (ps2_special_cmd(fd, c) &&
165
 
            ps2_putbyte(fd, PS2_CMD_STATUS_REQUEST));
 
164
    return (ps2_special_cmd(fd, c) && ps2_putbyte(fd, PS2_CMD_STATUS_REQUEST));
166
165
}
167
166
 
168
167
/*****************************************************************************
177
176
{
178
177
    PS2DBG(ErrorF("set mode byte to: 0x%02X\n", mode));
179
178
    return (ps2_special_cmd(fd, mode) &&
180
 
            ps2_putbyte(fd, PS2_CMD_SET_SAMPLE_RATE) &&
181
 
            ps2_putbyte(fd, 0x14));
 
179
            ps2_putbyte(fd, PS2_CMD_SET_SAMPLE_RATE) && ps2_putbyte(fd, 0x14));
182
180
}
183
181
 
184
182
/*
192
190
    xf86FlushInput(fd);
193
191
    PS2DBG(ErrorF("Reset the Touchpad...\n"));
194
192
    if (!ps2_putbyte(fd, PS2_CMD_RESET)) {
195
 
        PS2DBG(ErrorF("...failed\n"));
196
 
        return FALSE;
 
193
        PS2DBG(ErrorF("...failed\n"));
 
194
        return FALSE;
197
195
    }
198
196
    xf86WaitForInput(fd, 4000000);
199
197
    if (ps2_getbyte(fd, &r[0]) && ps2_getbyte(fd, &r[1])) {
200
 
        if (r[0] == 0xAA && r[1] == 0x00) {
201
 
            PS2DBG(ErrorF("...done\n"));
202
 
            return TRUE;
203
 
        } else {
204
 
            PS2DBG(ErrorF("...failed. Wrong reset ack 0x%02x, 0x%02x\n", r[0], r[1]));
205
 
            return FALSE;
206
 
        }
 
198
        if (r[0] == 0xAA && r[1] == 0x00) {
 
199
            PS2DBG(ErrorF("...done\n"));
 
200
            return TRUE;
 
201
        }
 
202
        else {
 
203
            PS2DBG(ErrorF
 
204
                   ("...failed. Wrong reset ack 0x%02x, 0x%02x\n", r[0], r[1]));
 
205
            return FALSE;
 
206
        }
207
207
    }
208
208
    PS2DBG(ErrorF("...failed\n"));
209
209
    return FALSE;
222
222
 
223
223
    synhw->model_id = 0;
224
224
    if (ps2_send_cmd(fd, SYN_QUE_MODEL) &&
225
 
        ps2_getbyte(fd, &mi[0]) &&
226
 
        ps2_getbyte(fd, &mi[1]) &&
227
 
        ps2_getbyte(fd, &mi[2])) {
228
 
        synhw->model_id = (mi[0] << 16) | (mi[1] << 8) | mi[2];
229
 
        PS2DBG(ErrorF("model-id %06X\n", synhw->model_id));
230
 
        PS2DBG(ErrorF("...done.\n"));
231
 
        return TRUE;
 
225
        ps2_getbyte(fd, &mi[0]) &&
 
226
        ps2_getbyte(fd, &mi[1]) && ps2_getbyte(fd, &mi[2])) {
 
227
        synhw->model_id = (mi[0] << 16) | (mi[1] << 8) | mi[2];
 
228
        PS2DBG(ErrorF("model-id %06X\n", synhw->model_id));
 
229
        PS2DBG(ErrorF("...done.\n"));
 
230
        return TRUE;
232
231
    }
233
232
    PS2DBG(ErrorF("...failed.\n"));
234
233
    return FALSE;
248
247
    synhw->capabilities = 0;
249
248
    synhw->ext_cap = 0;
250
249
    if (ps2_send_cmd(fd, SYN_QUE_CAPABILITIES) &&
251
 
        ps2_getbyte(fd, &cap[0]) &&
252
 
        ps2_getbyte(fd, &cap[1]) &&
253
 
        ps2_getbyte(fd, &cap[2])) {
254
 
        synhw->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
255
 
        PS2DBG(ErrorF("capabilities %06X\n", synhw->capabilities));
256
 
        if (SYN_CAP_VALID(synhw)) {
257
 
            if (SYN_EXT_CAP_REQUESTS(synhw)) {
258
 
                if (ps2_send_cmd(fd, SYN_QUE_EXT_CAPAB) &&
259
 
                    ps2_getbyte(fd, &cap[0]) &&
260
 
                    ps2_getbyte(fd, &cap[1]) &&
261
 
                    ps2_getbyte(fd, &cap[2])) {
262
 
                    synhw->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
263
 
                    PS2DBG(ErrorF("ext-capability %06X\n", synhw->ext_cap));
264
 
                } else {
265
 
                    PS2DBG(ErrorF("synaptics says, that it has extended-capabilities, "
266
 
                                  "but I cannot read them."));
267
 
                }
268
 
            }
269
 
            PS2DBG(ErrorF("...done.\n"));
270
 
            return TRUE;
271
 
        }
 
250
        ps2_getbyte(fd, &cap[0]) &&
 
251
        ps2_getbyte(fd, &cap[1]) && ps2_getbyte(fd, &cap[2])) {
 
252
        synhw->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
 
253
        PS2DBG(ErrorF("capabilities %06X\n", synhw->capabilities));
 
254
        if (SYN_CAP_VALID(synhw)) {
 
255
            if (SYN_EXT_CAP_REQUESTS(synhw)) {
 
256
                if (ps2_send_cmd(fd, SYN_QUE_EXT_CAPAB) &&
 
257
                    ps2_getbyte(fd, &cap[0]) &&
 
258
                    ps2_getbyte(fd, &cap[1]) && ps2_getbyte(fd, &cap[2])) {
 
259
                    synhw->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
 
260
                    PS2DBG(ErrorF("ext-capability %06X\n", synhw->ext_cap));
 
261
                }
 
262
                else {
 
263
                    PS2DBG(ErrorF
 
264
                           ("synaptics says, that it has extended-capabilities, "
 
265
                            "but I cannot read them."));
 
266
                }
 
267
            }
 
268
            PS2DBG(ErrorF("...done.\n"));
 
269
            return TRUE;
 
270
        }
272
271
    }
273
272
    PS2DBG(ErrorF("...failed.\n"));
274
273
    return FALSE;
287
286
 
288
287
    synhw->identity = 0;
289
288
    if (ps2_send_cmd(fd, SYN_QUE_IDENTIFY) &&
290
 
        ps2_getbyte(fd, &id[0]) &&
291
 
        ps2_getbyte(fd, &id[1]) &&
292
 
        ps2_getbyte(fd, &id[2])) {
293
 
        synhw->identity = (id[0] << 16) | (id[1] << 8) | id[2];
294
 
        PS2DBG(ErrorF("ident %06X\n", synhw->identity));
295
 
        if (SYN_ID_IS_SYNAPTICS(synhw)) {
296
 
            PS2DBG(ErrorF("...done.\n"));
297
 
            return TRUE;
298
 
        }
 
289
        ps2_getbyte(fd, &id[0]) &&
 
290
        ps2_getbyte(fd, &id[1]) && ps2_getbyte(fd, &id[2])) {
 
291
        synhw->identity = (id[0] << 16) | (id[1] << 8) | id[2];
 
292
        PS2DBG(ErrorF("ident %06X\n", synhw->identity));
 
293
        if (SYN_ID_IS_SYNAPTICS(synhw)) {
 
294
            PS2DBG(ErrorF("...done.\n"));
 
295
            return TRUE;
 
296
        }
299
297
    }
300
298
    PS2DBG(ErrorF("...failed.\n"));
301
299
    return FALSE;
315
313
}
316
314
 
317
315
static Bool
318
 
ps2_query_is_synaptics(InputInfoPtr pInfo, int fd, struct PS2SynapticsHwInfo* synhw)
 
316
ps2_query_is_synaptics(InputInfoPtr pInfo, int fd,
 
317
                       struct PS2SynapticsHwInfo *synhw)
319
318
{
320
319
    int i;
321
320
 
322
321
    for (i = 0; i < 3; i++) {
323
 
        if (ps2_synaptics_disable_device(fd))
324
 
            break;
 
322
        if (ps2_synaptics_disable_device(fd))
 
323
            break;
325
324
    }
326
325
 
327
326
    xf86WaitForInput(fd, 20000);
328
327
    xf86FlushInput(fd);
329
328
    if (ps2_synaptics_identify(fd, synhw)) {
330
 
        return TRUE;
331
 
    } else {
332
 
        xf86IDrvMsg(pInfo, X_ERROR, "Query no Synaptics: %06X\n", synhw->identity);
333
 
        return FALSE;
 
329
        return TRUE;
 
330
    }
 
331
    else {
 
332
        xf86IDrvMsg(pInfo, X_ERROR, "Query no Synaptics: %06X\n",
 
333
                    synhw->identity);
 
334
        return FALSE;
334
335
    }
335
336
}
336
337
 
337
338
void
338
339
ps2_print_ident(InputInfoPtr pInfo, const struct PS2SynapticsHwInfo *synhw)
339
340
{
340
 
    xf86IDrvMsg(pInfo, X_PROBED, " Synaptics Touchpad, model: %d\n", SYN_ID_MODEL(synhw));
 
341
    xf86IDrvMsg(pInfo, X_PROBED, " Synaptics Touchpad, model: %d\n",
 
342
                SYN_ID_MODEL(synhw));
341
343
    xf86IDrvMsg(pInfo, X_PROBED, " Firmware: %d.%d\n", SYN_ID_MAJOR(synhw),
342
 
            SYN_ID_MINOR(synhw));
 
344
                SYN_ID_MINOR(synhw));
343
345
 
344
346
    if (SYN_MODEL_ROT180(synhw))
345
 
        xf86IDrvMsg(pInfo, X_PROBED, " 180 degree mounted touchpad\n");
 
347
        xf86IDrvMsg(pInfo, X_PROBED, " 180 degree mounted touchpad\n");
346
348
    if (SYN_MODEL_PORTRAIT(synhw))
347
 
        xf86IDrvMsg(pInfo, X_PROBED, " portrait touchpad\n");
 
349
        xf86IDrvMsg(pInfo, X_PROBED, " portrait touchpad\n");
348
350
    xf86IDrvMsg(pInfo, X_PROBED, " Sensor: %d\n", SYN_MODEL_SENSOR(synhw));
349
351
    if (SYN_MODEL_NEWABS(synhw))
350
 
        xf86IDrvMsg(pInfo, X_PROBED, " new absolute packet format\n");
 
352
        xf86IDrvMsg(pInfo, X_PROBED, " new absolute packet format\n");
351
353
    if (SYN_MODEL_PEN(synhw))
352
 
        xf86IDrvMsg(pInfo, X_PROBED, " pen detection\n");
 
354
        xf86IDrvMsg(pInfo, X_PROBED, " pen detection\n");
353
355
 
354
356
    if (SYN_CAP_EXTENDED(synhw)) {
355
 
        xf86IDrvMsg(pInfo, X_PROBED, " Touchpad has extended capability bits\n");
356
 
        if (SYN_CAP_MULTI_BUTTON_NO(synhw))
357
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> %d multi buttons, i.e. besides standard buttons\n",
358
 
                    (int)(SYN_CAP_MULTI_BUTTON_NO(synhw)));
359
 
        if (SYN_CAP_MIDDLE_BUTTON(synhw))
360
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> middle button\n");
361
 
        if (SYN_CAP_FOUR_BUTTON(synhw))
362
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> four buttons\n");
363
 
        if (SYN_CAP_MULTIFINGER(synhw))
364
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> multifinger detection\n");
365
 
        if (SYN_CAP_PALMDETECT(synhw))
366
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> palm detection\n");
367
 
        if (SYN_CAP_PASSTHROUGH(synhw))
368
 
            xf86IDrvMsg(pInfo, X_PROBED, " -> pass-through port\n");
 
357
        xf86IDrvMsg(pInfo, X_PROBED,
 
358
                    " Touchpad has extended capability bits\n");
 
359
        if (SYN_CAP_MULTI_BUTTON_NO(synhw))
 
360
            xf86IDrvMsg(pInfo, X_PROBED,
 
361
                        " -> %d multi buttons, i.e. besides standard buttons\n",
 
362
                        (int) (SYN_CAP_MULTI_BUTTON_NO(synhw)));
 
363
        if (SYN_CAP_MIDDLE_BUTTON(synhw))
 
364
            xf86IDrvMsg(pInfo, X_PROBED, " -> middle button\n");
 
365
        if (SYN_CAP_FOUR_BUTTON(synhw))
 
366
            xf86IDrvMsg(pInfo, X_PROBED, " -> four buttons\n");
 
367
        if (SYN_CAP_MULTIFINGER(synhw))
 
368
            xf86IDrvMsg(pInfo, X_PROBED, " -> multifinger detection\n");
 
369
        if (SYN_CAP_PALMDETECT(synhw))
 
370
            xf86IDrvMsg(pInfo, X_PROBED, " -> palm detection\n");
 
371
        if (SYN_CAP_PASSTHROUGH(synhw))
 
372
            xf86IDrvMsg(pInfo, X_PROBED, " -> pass-through port\n");
369
373
    }
370
374
}
371
375
 
382
386
PS2QueryHardware(InputInfoPtr pInfo)
383
387
{
384
388
    int mode;
385
 
    SynapticsPrivate *priv = (SynapticsPrivate *)pInfo->private;
 
389
    SynapticsPrivate *priv = (SynapticsPrivate *) pInfo->private;
386
390
    struct PS2SynapticsHwInfo *synhw;
387
391
 
388
392
    if (!priv->proto_data)
389
393
        priv->proto_data = calloc(1, sizeof(struct PS2SynapticsHwInfo));
390
 
    synhw = (struct PS2SynapticsHwInfo*)priv->proto_data;
 
394
    synhw = (struct PS2SynapticsHwInfo *) priv->proto_data;
391
395
 
392
396
    /* is the synaptics touchpad active? */
393
397
    if (!ps2_query_is_synaptics(pInfo, pInfo->fd, synhw))
394
 
        return FALSE;
 
398
        return FALSE;
395
399
 
396
400
    xf86IDrvMsg(pInfo, X_PROBED, "synaptics touchpad found\n");
397
401
 
398
402
    if (!ps2_synaptics_reset(pInfo->fd))
399
 
        xf86IDrvMsg(pInfo, X_ERROR, "reset failed\n");
 
403
        xf86IDrvMsg(pInfo, X_ERROR, "reset failed\n");
400
404
 
401
405
    if (!ps2_synaptics_identify(pInfo->fd, synhw))
402
 
        return FALSE;
 
406
        return FALSE;
403
407
 
404
408
    if (!ps2_synaptics_model_id(pInfo->fd, synhw))
405
 
        return FALSE;
 
409
        return FALSE;
406
410
 
407
411
    if (!ps2_synaptics_capability(pInfo->fd, synhw))
408
 
        return FALSE;
 
412
        return FALSE;
409
413
 
410
414
    mode = SYN_BIT_ABSOLUTE_MODE | SYN_BIT_HIGH_RATE;
411
415
    if (SYN_ID_MAJOR(synhw) >= 4)
412
 
        mode |= SYN_BIT_DISABLE_GESTURE;
 
416
        mode |= SYN_BIT_DISABLE_GESTURE;
413
417
    if (SYN_CAP_EXTENDED(synhw))
414
 
        mode |= SYN_BIT_W_MODE;
 
418
        mode |= SYN_BIT_W_MODE;
415
419
    if (!ps2_synaptics_set_mode(pInfo->fd, mode))
416
 
        return FALSE;
 
420
        return FALSE;
417
421
 
418
422
    ps2_synaptics_enable_device(pInfo->fd);
419
423
 
432
436
    int newabs = SYN_MODEL_NEWABS(synhw);
433
437
 
434
438
    if (newabs ? ((buf[0] & 0xC0) != 0x80) : ((buf[0] & 0xC0) != 0xC0)) {
435
 
        DBG(4, "Synaptics driver lost sync at 1st byte\n");
436
 
        return FALSE;
 
439
        DBG(4, "Synaptics driver lost sync at 1st byte\n");
 
440
        return FALSE;
437
441
    }
438
442
 
439
443
    if (!newabs && ((buf[1] & 0x60) != 0x00)) {
440
 
        DBG(4, "Synaptics driver lost sync at 2nd byte\n");
441
 
        return FALSE;
 
444
        DBG(4, "Synaptics driver lost sync at 2nd byte\n");
 
445
        return FALSE;
442
446
    }
443
447
 
444
448
    if ((newabs ? ((buf[3] & 0xC0) != 0xC0) : ((buf[3] & 0xC0) != 0x80))) {
445
 
        DBG(4, "Synaptics driver lost sync at 4th byte\n");
446
 
        return FALSE;
 
449
        DBG(4, "Synaptics driver lost sync at 4th byte\n");
 
450
        return FALSE;
447
451
    }
448
452
 
449
453
    if (!newabs && ((buf[4] & 0x60) != 0x00)) {
450
 
        DBG(4, "Synaptics driver lost sync at 5th byte\n");
451
 
        return FALSE;
 
454
        DBG(4, "Synaptics driver lost sync at 5th byte\n");
 
455
        return FALSE;
452
456
    }
453
457
 
454
458
    return TRUE;
456
460
 
457
461
static Bool
458
462
ps2_synaptics_get_packet(InputInfoPtr pInfo, struct PS2SynapticsHwInfo *synhw,
459
 
                         struct SynapticsProtocolOperations *proto_ops,
460
 
                         struct CommData *comm)
 
463
                         struct SynapticsProtocolOperations *proto_ops,
 
464
                         struct CommData *comm)
461
465
{
462
466
    int count = 0;
463
467
    int c;
464
468
    unsigned char u;
465
469
 
466
470
    while ((c = XisbRead(comm->buffer)) >= 0) {
467
 
        u = (unsigned char)c;
468
 
 
469
 
        /* test if there is a reset sequence received */
470
 
        if ((c == 0x00) && (comm->lastByte == 0xAA)) {
471
 
            if (xf86WaitForInput(pInfo->fd, 50000) == 0) {
472
 
                DBG(7, "Reset received\n");
473
 
                proto_ops->QueryHardware(pInfo);
474
 
            } else
475
 
                DBG(3, "faked reset received\n");
476
 
        }
477
 
        comm->lastByte = u;
478
 
 
479
 
        /* to avoid endless loops */
480
 
        if (count++ > 30) {
481
 
            xf86IDrvMsg(pInfo, X_ERROR, "Synaptics driver lost sync... got gigantic packet!\n");
482
 
            return FALSE;
483
 
        }
484
 
 
485
 
        comm->protoBuf[comm->protoBufTail++] = u;
486
 
 
487
 
        /* Check that we have a valid packet. If not, we are out of sync,
488
 
           so we throw away the first byte in the packet.*/
489
 
        if (comm->protoBufTail >= 6) {
490
 
            if (!ps2_packet_ok(synhw, comm)) {
491
 
                int i;
492
 
                for (i = 0; i < comm->protoBufTail - 1; i++)
493
 
                    comm->protoBuf[i] = comm->protoBuf[i + 1];
494
 
                comm->protoBufTail--;
495
 
                comm->outOfSync++;
496
 
                if (comm->outOfSync > MAX_UNSYNC_PACKETS) {
497
 
                    comm->outOfSync = 0;
498
 
                    DBG(3, "Synaptics synchronization lost too long -> reset touchpad.\n");
499
 
                    proto_ops->QueryHardware(pInfo); /* including a reset */
500
 
                    continue;
501
 
                }
502
 
            }
503
 
        }
504
 
 
505
 
        if (comm->protoBufTail >= 6) { /* Full packet received */
506
 
            if (comm->outOfSync > 0) {
507
 
                comm->outOfSync = 0;
508
 
                DBG(4, "Synaptics driver resynced.\n");
509
 
            }
510
 
            comm->protoBufTail = 0;
511
 
            return TRUE;
512
 
        }
 
471
        u = (unsigned char) c;
 
472
 
 
473
        /* test if there is a reset sequence received */
 
474
        if ((c == 0x00) && (comm->lastByte == 0xAA)) {
 
475
            if (xf86WaitForInput(pInfo->fd, 50000) == 0) {
 
476
                DBG(7, "Reset received\n");
 
477
                proto_ops->QueryHardware(pInfo);
 
478
            }
 
479
            else
 
480
                DBG(3, "faked reset received\n");
 
481
        }
 
482
        comm->lastByte = u;
 
483
 
 
484
        /* to avoid endless loops */
 
485
        if (count++ > 30) {
 
486
            xf86IDrvMsg(pInfo, X_ERROR,
 
487
                        "Synaptics driver lost sync... got gigantic packet!\n");
 
488
            return FALSE;
 
489
        }
 
490
 
 
491
        comm->protoBuf[comm->protoBufTail++] = u;
 
492
 
 
493
        /* Check that we have a valid packet. If not, we are out of sync,
 
494
           so we throw away the first byte in the packet. */
 
495
        if (comm->protoBufTail >= 6) {
 
496
            if (!ps2_packet_ok(synhw, comm)) {
 
497
                int i;
 
498
 
 
499
                for (i = 0; i < comm->protoBufTail - 1; i++)
 
500
                    comm->protoBuf[i] = comm->protoBuf[i + 1];
 
501
                comm->protoBufTail--;
 
502
                comm->outOfSync++;
 
503
                if (comm->outOfSync > MAX_UNSYNC_PACKETS) {
 
504
                    comm->outOfSync = 0;
 
505
                    DBG(3,
 
506
                        "Synaptics synchronization lost too long -> reset touchpad.\n");
 
507
                    proto_ops->QueryHardware(pInfo);    /* including a reset */
 
508
                    continue;
 
509
                }
 
510
            }
 
511
        }
 
512
 
 
513
        if (comm->protoBufTail >= 6) {  /* Full packet received */
 
514
            if (comm->outOfSync > 0) {
 
515
                comm->outOfSync = 0;
 
516
                DBG(4, "Synaptics driver resynced.\n");
 
517
            }
 
518
            comm->protoBufTail = 0;
 
519
            return TRUE;
 
520
        }
513
521
    }
514
522
 
515
523
    return FALSE;
517
525
 
518
526
Bool
519
527
PS2ReadHwStateProto(InputInfoPtr pInfo,
520
 
               struct SynapticsProtocolOperations *proto_ops,
521
 
               struct CommData *comm, struct SynapticsHwState *hwRet)
 
528
                    struct SynapticsProtocolOperations *proto_ops,
 
529
                    struct CommData *comm, struct SynapticsHwState *hwRet)
522
530
{
523
531
    unsigned char *buf = comm->protoBuf;
524
532
    struct SynapticsHwState *hw = comm->hwState;
525
 
    SynapticsPrivate *priv = (SynapticsPrivate *)pInfo->private;
 
533
    SynapticsPrivate *priv = (SynapticsPrivate *) pInfo->private;
526
534
    SynapticsParameters *para = &priv->synpara;
527
535
    struct PS2SynapticsHwInfo *synhw;
528
536
    int newabs;
529
537
    int w, i;
530
538
 
531
 
    synhw = (struct PS2SynapticsHwInfo*)priv->proto_data;
532
 
    if (!synhw)
533
 
    {
 
539
    synhw = (struct PS2SynapticsHwInfo *) priv->proto_data;
 
540
    if (!synhw) {
534
541
        xf86IDrvMsg(pInfo, X_ERROR,
535
542
                    "PS2ReadHwState, synhw is NULL. This is a bug.\n");
536
543
        return FALSE;
539
546
    newabs = SYN_MODEL_NEWABS(synhw);
540
547
 
541
548
    if (!ps2_synaptics_get_packet(pInfo, synhw, proto_ops, comm))
542
 
        return FALSE;
 
549
        return FALSE;
543
550
 
544
551
    /* Handle normal packets */
545
552
    hw->x = hw->y = hw->z = hw->numFingers = hw->fingerWidth = 0;
546
553
    hw->left = hw->right = hw->up = hw->down = hw->middle = FALSE;
547
554
    for (i = 0; i < 8; i++)
548
 
        hw->multi[i] = FALSE;
549
 
 
550
 
    if (newabs) {                           /* newer protos...*/
551
 
        DBG(7, "using new protocols\n");
552
 
        hw->x = (((buf[3] & 0x10) << 8) |
553
 
                 ((buf[1] & 0x0f) << 8) |
554
 
                 buf[4]);
555
 
        hw->y = (((buf[3] & 0x20) << 7) |
556
 
                 ((buf[1] & 0xf0) << 4) |
557
 
                 buf[5]);
558
 
 
559
 
        hw->z = buf[2];
560
 
        w = (((buf[0] & 0x30) >> 2) |
561
 
             ((buf[0] & 0x04) >> 1) |
562
 
             ((buf[3] & 0x04) >> 2));
563
 
 
564
 
        hw->left  = (buf[0] & 0x01) ? 1 : 0;
565
 
        hw->right = (buf[0] & 0x02) ? 1 : 0;
566
 
 
567
 
        if (SYN_CAP_EXTENDED(synhw)) {
568
 
            if (SYN_CAP_MIDDLE_BUTTON(synhw)) {
569
 
                hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
570
 
            }
571
 
            if (SYN_CAP_FOUR_BUTTON(synhw)) {
572
 
                hw->up = ((buf[3] & 0x01)) ? 1 : 0;
573
 
                if (hw->left)
574
 
                    hw->up = !hw->up;
575
 
                hw->down = ((buf[3] & 0x02)) ? 1 : 0;
576
 
                if (hw->right)
577
 
                    hw->down = !hw->down;
578
 
            }
579
 
            if (SYN_CAP_MULTI_BUTTON_NO(synhw)) {
580
 
                if ((buf[3] & 2) ? !hw->right : hw->right) {
581
 
                    switch (SYN_CAP_MULTI_BUTTON_NO(synhw) & ~0x01) {
582
 
                    default:
583
 
                        break;
584
 
                    case 8:
585
 
                        hw->multi[7] = ((buf[5] & 0x08)) ? 1 : 0;
586
 
                        hw->multi[6] = ((buf[4] & 0x08)) ? 1 : 0;
587
 
                    case 6:
588
 
                        hw->multi[5] = ((buf[5] & 0x04)) ? 1 : 0;
589
 
                        hw->multi[4] = ((buf[4] & 0x04)) ? 1 : 0;
590
 
                    case 4:
591
 
                        hw->multi[3] = ((buf[5] & 0x02)) ? 1 : 0;
592
 
                        hw->multi[2] = ((buf[4] & 0x02)) ? 1 : 0;
593
 
                    case 2:
594
 
                        hw->multi[1] = ((buf[5] & 0x01)) ? 1 : 0;
595
 
                        hw->multi[0] = ((buf[4] & 0x01)) ? 1 : 0;
596
 
                    }
597
 
                }
598
 
            }
599
 
        }
600
 
    } else {                        /* old proto...*/
601
 
        DBG(7, "using old protocol\n");
602
 
        hw->x = (((buf[1] & 0x1F) << 8) |
603
 
                 buf[2]);
604
 
        hw->y = (((buf[4] & 0x1F) << 8) |
605
 
                 buf[5]);
606
 
 
607
 
        hw->z = (((buf[0] & 0x30) << 2) |
608
 
                 (buf[3] & 0x3F));
609
 
        w = (((buf[1] & 0x80) >> 4) |
610
 
             ((buf[0] & 0x04) >> 1));
611
 
 
612
 
        hw->left  = (buf[0] & 0x01) ? 1 : 0;
613
 
        hw->right = (buf[0] & 0x02) ? 1 : 0;
 
555
        hw->multi[i] = FALSE;
 
556
 
 
557
    if (newabs) {               /* newer protos... */
 
558
        DBG(7, "using new protocols\n");
 
559
        hw->x = (((buf[3] & 0x10) << 8) | ((buf[1] & 0x0f) << 8) | buf[4]);
 
560
        hw->y = (((buf[3] & 0x20) << 7) | ((buf[1] & 0xf0) << 4) | buf[5]);
 
561
 
 
562
        hw->z = buf[2];
 
563
        w = (((buf[0] & 0x30) >> 2) |
 
564
             ((buf[0] & 0x04) >> 1) | ((buf[3] & 0x04) >> 2));
 
565
 
 
566
        hw->left = (buf[0] & 0x01) ? 1 : 0;
 
567
        hw->right = (buf[0] & 0x02) ? 1 : 0;
 
568
 
 
569
        if (SYN_CAP_EXTENDED(synhw)) {
 
570
            if (SYN_CAP_MIDDLE_BUTTON(synhw)) {
 
571
                hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
 
572
            }
 
573
            if (SYN_CAP_FOUR_BUTTON(synhw)) {
 
574
                hw->up = ((buf[3] & 0x01)) ? 1 : 0;
 
575
                if (hw->left)
 
576
                    hw->up = !hw->up;
 
577
                hw->down = ((buf[3] & 0x02)) ? 1 : 0;
 
578
                if (hw->right)
 
579
                    hw->down = !hw->down;
 
580
            }
 
581
            if (SYN_CAP_MULTI_BUTTON_NO(synhw)) {
 
582
                if ((buf[3] & 2) ? !hw->right : hw->right) {
 
583
                    switch (SYN_CAP_MULTI_BUTTON_NO(synhw) & ~0x01) {
 
584
                    default:
 
585
                        break;
 
586
                    case 8:
 
587
                        hw->multi[7] = ((buf[5] & 0x08)) ? 1 : 0;
 
588
                        hw->multi[6] = ((buf[4] & 0x08)) ? 1 : 0;
 
589
                    case 6:
 
590
                        hw->multi[5] = ((buf[5] & 0x04)) ? 1 : 0;
 
591
                        hw->multi[4] = ((buf[4] & 0x04)) ? 1 : 0;
 
592
                    case 4:
 
593
                        hw->multi[3] = ((buf[5] & 0x02)) ? 1 : 0;
 
594
                        hw->multi[2] = ((buf[4] & 0x02)) ? 1 : 0;
 
595
                    case 2:
 
596
                        hw->multi[1] = ((buf[5] & 0x01)) ? 1 : 0;
 
597
                        hw->multi[0] = ((buf[4] & 0x01)) ? 1 : 0;
 
598
                    }
 
599
                }
 
600
            }
 
601
        }
 
602
    }
 
603
    else {                      /* old proto... */
 
604
        DBG(7, "using old protocol\n");
 
605
        hw->x = (((buf[1] & 0x1F) << 8) | buf[2]);
 
606
        hw->y = (((buf[4] & 0x1F) << 8) | buf[5]);
 
607
 
 
608
        hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
 
609
        w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
 
610
 
 
611
        hw->left = (buf[0] & 0x01) ? 1 : 0;
 
612
        hw->right = (buf[0] & 0x02) ? 1 : 0;
614
613
    }
615
614
 
616
615
    hw->y = YMAX_NOMINAL + YMIN_NOMINAL - hw->y;
617
616
 
618
617
    if (hw->z >= para->finger_high) {
619
 
        int w_ok = 0;
620
 
        /*
621
 
         * Use capability bits to decide if the w value is valid.
622
 
         * If not, set it to 5, which corresponds to a finger of
623
 
         * normal width.
624
 
         */
625
 
        if (SYN_CAP_EXTENDED(synhw)) {
626
 
            if ((w >= 0) && (w <= 1)) {
627
 
                w_ok = SYN_CAP_MULTIFINGER(synhw);
628
 
            } else if (w == 2) {
629
 
                w_ok = SYN_MODEL_PEN(synhw);
630
 
            } else if ((w >= 4) && (w <= 15)) {
631
 
                w_ok = SYN_CAP_PALMDETECT(synhw);
632
 
            }
633
 
        }
634
 
        if (!w_ok)
635
 
            w = 5;
636
 
 
637
 
        switch (w) {
638
 
        case 0:
639
 
            hw->numFingers = 2;
640
 
            hw->fingerWidth = 5;
641
 
            break;
642
 
        case 1:
643
 
            hw->numFingers = 3;
644
 
            hw->fingerWidth = 5;
645
 
            break;
646
 
        default:
647
 
            hw->numFingers = 1;
648
 
            hw->fingerWidth = w;
649
 
            break;
650
 
        }
 
618
        int w_ok = 0;
 
619
 
 
620
        /*
 
621
         * Use capability bits to decide if the w value is valid.
 
622
         * If not, set it to 5, which corresponds to a finger of
 
623
         * normal width.
 
624
         */
 
625
        if (SYN_CAP_EXTENDED(synhw)) {
 
626
            if ((w >= 0) && (w <= 1)) {
 
627
                w_ok = SYN_CAP_MULTIFINGER(synhw);
 
628
            }
 
629
            else if (w == 2) {
 
630
                w_ok = SYN_MODEL_PEN(synhw);
 
631
            }
 
632
            else if ((w >= 4) && (w <= 15)) {
 
633
                w_ok = SYN_CAP_PALMDETECT(synhw);
 
634
            }
 
635
        }
 
636
        if (!w_ok)
 
637
            w = 5;
 
638
 
 
639
        switch (w) {
 
640
        case 0:
 
641
            hw->numFingers = 2;
 
642
            hw->fingerWidth = 5;
 
643
            break;
 
644
        case 1:
 
645
            hw->numFingers = 3;
 
646
            hw->fingerWidth = 5;
 
647
            break;
 
648
        default:
 
649
            hw->numFingers = 1;
 
650
            hw->fingerWidth = w;
 
651
            break;
 
652
        }
651
653
    }
652
654
    hw->millis = GetTimeInMillis();
653
655
    SynapticsCopyHwState(hwRet, hw);