summaryrefslogtreecommitdiff
path: root/src/ps2comm.c
blob: 887a012d93607c90d097a250c4f2f581de896708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
 * Copyright © 1997 C. Scott Ananian
 * Copyright © 1998-2000 Bruce Kalk
 * Copyright © 2001 Stefan Gmeiner
 * Copyright © 2002 Linuxcare Inc. David Kennedy
 * Copyright © 2003 Fred Hucht <fred@thp.Uni-Duisburg.de>
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Red Hat
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  Red
 * Hat makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors:
 *      Stefan Gmeiner (riddlebox@freesurf.ch)
 *      C. Scott Ananian (cananian@alumni.priceton.edu)
 *      Bruce Kalk (kall@compass.com)
 *      Linuxcare Inc. David Kennedy (dkennedy@linuxcare.com)
 *      Fred Hucht (fred@thp.Uni-Duisburg.de)
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <xorg-server.h>
#include <xf86.h>
#include "synproto.h"
#include "synapticsstr.h"
#include "ps2comm.h"
#include "synaptics.h"

#define MAX_UNSYNC_PACKETS 10   /* i.e. 10 to 60 bytes */
/*
 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
 * section 2.3.2, which says that they should be valid regardless of the
 * actual size of the sensor.
 */
#define XMIN_NOMINAL 1472
#define XMAX_NOMINAL 5472
#define YMIN_NOMINAL 1408
#define YMAX_NOMINAL 4448

#define XMAX_VALID 6143

/* synaptics queries */
#define SYN_QUE_IDENTIFY		0x00
#define SYN_QUE_MODES			0x01
#define SYN_QUE_CAPABILITIES		0x02
#define SYN_QUE_MODEL			0x03
#define SYN_QUE_SERIAL_NUMBER_PREFIX	0x06
#define SYN_QUE_SERIAL_NUMBER_SUFFIX	0x07
#define SYN_QUE_RESOLUTION		0x08
#define SYN_QUE_EXT_CAPAB		0x09

/* status request response bits (PS2_CMD_STATUS_REQUEST) */
#define PS2_RES_REMOTE(r)	((r) & (1 << 22))
#define PS2_RES_ENABLE(r)	((r) & (1 << 21))
#define PS2_RES_SCALING(r)	((r) & (1 << 20))
#define PS2_RES_LEFT(r)		((r) & (1 << 18))
#define PS2_RES_MIDDLE(r)	((r) & (1 << 17))
#define PS2_RES_RIGHT(r)	((r) & (1 << 16))
#define PS2_RES_RESOLUTION(r)	(((r) >> 8) & 0x03)
#define PS2_RES_SAMPLE_RATE(r)	((r) & 0xff)

#ifdef DEBUG
#define PS2DBG(...) ErrorF(__VA_ARGS__)
#else
#define PS2DBG(...)
#endif

/*****************************************************************************
 *	PS/2 Utility functions.
 *     Many parts adapted from tpconfig.c by C. Scott Ananian
 ****************************************************************************/

/*
 * Read a byte from the ps/2 port
 */
static int
ps2_getbyte(int fd, byte * b)
{
    if (xf86WaitForInput(fd, 50000) > 0) {
        if (xf86ReadSerial(fd, b, 1) != 1) {
            PS2DBG("ps2_getbyte: No byte read\n");
            return FALSE;
        }
        PS2DBG("ps2_getbyte: byte %02X read\n", *b);
        return TRUE;
    }
    PS2DBG("ps2_getbyte: timeout xf86WaitForInput\n");
    return FALSE;
}

/*
 * Write a byte to the ps/2 port, wait for ACK
 */
int
ps2_putbyte(int fd, byte b)
{
    byte ack;

    if (xf86WriteSerial(fd, &b, 1) != 1) {
        PS2DBG("ps2_putbyte: error xf86WriteSerial\n");
        return FALSE;
    }
    PS2DBG("ps2_putbyte: byte %02X send\n", b);
    /* wait for an ACK */
    if (!ps2_getbyte(fd, &ack)) {
        return FALSE;
    }
    if (ack != PS2_ACK) {
        PS2DBG("ps2_putbyte: wrong acknowledge 0x%02x\n", ack);
        return FALSE;
    }
    return TRUE;
}

/*
 * Use the Synaptics extended ps/2 syntax to write a special command byte. Needed by
 * ps2_send_cmd and ps2_set_mode.
 * special command: 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
 *                  is the command. A 0xF3 or 0xE9 must follow (see ps2_send_cmd, ps2_set_mode)
 */
static Bool
ps2_special_cmd(int fd, byte cmd)
{
    int i;

    /* initialize with 'inert' command */
    if (!ps2_putbyte(fd, PS2_CMD_SET_SCALING_1_1))
        return FALSE;

    /* send 4x 2-bits with set resolution command */
    for (i = 0; i < 4; i++) {
        if (!ps2_putbyte(fd, PS2_CMD_SET_RESOLUTION) ||
            !ps2_putbyte(fd, (cmd >> 6) & 0x3))
            return FALSE;
        cmd <<= 2;
    }
    return TRUE;
}

/*
 * Send a command to the synpatics touchpad by special commands
 */
static Bool
ps2_send_cmd(int fd, byte c)
{
    PS2DBG("send command: 0x%02X\n", c);
    return (ps2_special_cmd(fd, c) && ps2_putbyte(fd, PS2_CMD_STATUS_REQUEST));
}

/*****************************************************************************
 *	Synaptics communications functions
 ****************************************************************************/

/*
 * Set the synaptics touchpad mode byte by special commands
 */
static Bool
ps2_synaptics_set_mode(int fd, byte mode)
{
    PS2DBG("set mode byte to: 0x%02X\n", mode);
    return (ps2_special_cmd(fd, mode) &&
            ps2_putbyte(fd, PS2_CMD_SET_SAMPLE_RATE) && ps2_putbyte(fd, 0x14));
}

/*
 * reset the touchpad
 */
static Bool
ps2_synaptics_reset(int fd)
{
    byte r[2];

    xf86FlushInput(fd);
    PS2DBG("Reset the Touchpad...\n");
    if (!ps2_putbyte(fd, PS2_CMD_RESET)) {
        PS2DBG("...failed\n");
        return FALSE;
    }
    xf86WaitForInput(fd, 4000000);
    if (ps2_getbyte(fd, &r[0]) && ps2_getbyte(fd, &r[1])) {
        if (r[0] == 0xAA && r[1] == 0x00) {
            PS2DBG("...done\n");
            return TRUE;
        }
        else {
            PS2DBG("...failed. Wrong reset ack 0x%02x, 0x%02x\n", r[0], r[1]);
            return FALSE;
        }
    }
    PS2DBG("...failed\n");
    return FALSE;
}

/*
 * Read the model-id bytes from the touchpad
 * see also SYN_MODEL_* macros
 */
static Bool
ps2_synaptics_model_id(int fd, struct PS2SynapticsHwInfo *synhw)
{
    byte mi[3];

    PS2DBG("Read mode id...\n");

    synhw->model_id = 0;
    if (ps2_send_cmd(fd, SYN_QUE_MODEL) &&
        ps2_getbyte(fd, &mi[0]) &&
        ps2_getbyte(fd, &mi[1]) && ps2_getbyte(fd, &mi[2])) {
        synhw->model_id = (mi[0] << 16) | (mi[1] << 8) | mi[2];
        PS2DBG("model-id %06X\n", synhw->model_id);
        PS2DBG("...done.\n");
        return TRUE;
    }
    PS2DBG("...failed.\n");
    return FALSE;
}

/*
 * Read the capability-bits from the touchpad
 * see also the SYN_CAP_* macros
 */
static Bool
ps2_synaptics_capability(int fd, struct PS2SynapticsHwInfo *synhw)
{
    byte cap[3];

    PS2DBG("Read capabilites...\n");

    synhw->capabilities = 0;
    synhw->ext_cap = 0;
    if (ps2_send_cmd(fd, SYN_QUE_CAPABILITIES) &&
        ps2_getbyte(fd, &cap[0]) &&
        ps2_getbyte(fd, &cap[1]) && ps2_getbyte(fd, &cap[2])) {
        synhw->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
        PS2DBG("capabilities %06X\n", synhw->capabilities);
        if (SYN_CAP_VALID(synhw)) {
            if (SYN_EXT_CAP_REQUESTS(synhw)) {
                if (ps2_send_cmd(fd, SYN_QUE_EXT_CAPAB) &&
                    ps2_getbyte(fd, &cap[0]) &&
                    ps2_getbyte(fd, &cap[1]) && ps2_getbyte(fd, &cap[2])) {
                    synhw->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
                    PS2DBG("ext-capability %06X\n", synhw->ext_cap);
                }
                else {
                    PS2DBG("synaptics says, that it has extended-capabilities, "
                           "but I cannot read them.");
                }
            }
            PS2DBG("...done.\n");
            return TRUE;
        }
    }
    PS2DBG("...failed.\n");
    return FALSE;
}

/*
 * Identify Touchpad
 * See also the SYN_ID_* macros
 */
static Bool
ps2_synaptics_identify(int fd, struct PS2SynapticsHwInfo *synhw)
{
    byte id[3];

    PS2DBG("Identify Touchpad...\n");

    synhw->identity = 0;
    if (ps2_send_cmd(fd, SYN_QUE_IDENTIFY) &&
        ps2_getbyte(fd, &id[0]) &&
        ps2_getbyte(fd, &id[1]) && ps2_getbyte(fd, &id[2])) {
        synhw->identity = (id[0] << 16) | (id[1] << 8) | id[2];
        PS2DBG("ident %06X\n", synhw->identity);
        if (SYN_ID_IS_SYNAPTICS(synhw)) {
            PS2DBG("...done.\n");
            return TRUE;
        }
    }
    PS2DBG("...failed.\n");
    return FALSE;
}

static Bool
ps2_synaptics_enable_device(int fd)
{
    return ps2_putbyte(fd, PS2_CMD_ENABLE);
}

static Bool
ps2_synaptics_disable_device(int fd)
{
    xf86FlushInput(fd);
    return ps2_putbyte(fd, PS2_CMD_DISABLE);
}

static Bool
ps2_query_is_synaptics(SynapticsPrivate *priv, int fd,
                       struct PS2SynapticsHwInfo *synhw)
{
    int i;

    for (i = 0; i < 3; i++) {
        if (ps2_synaptics_disable_device(fd))
            break;
    }

    xf86WaitForInput(fd, 20000);
    xf86FlushInput(fd);
    if (ps2_synaptics_identify(fd, synhw)) {
        return TRUE;
    }
    else {
        SynapticsMsg(priv, MSG_ERROR, "Query no Synaptics: %06X\n",
                     synhw->identity);
        return FALSE;
    }
}

void
ps2_print_ident(SynapticsPrivate *priv, const struct PS2SynapticsHwInfo *synhw)
{
    SynapticsMsg(priv, MSG_INFO, " Synaptics Touchpad, model: %d\n",
                SYN_ID_MODEL(synhw));
    SynapticsMsg(priv, MSG_INFO, " Firmware: %d.%d\n", SYN_ID_MAJOR(synhw),
                SYN_ID_MINOR(synhw));

    if (SYN_MODEL_ROT180(synhw))
        SynapticsMsg(priv, MSG_INFO, " 180 degree mounted touchpad\n");
    if (SYN_MODEL_PORTRAIT(synhw))
        SynapticsMsg(priv, MSG_INFO, " portrait touchpad\n");
    SynapticsMsg(priv, MSG_INFO, " Sensor: %d\n", SYN_MODEL_SENSOR(synhw));
    if (SYN_MODEL_NEWABS(synhw))
        SynapticsMsg(priv, MSG_INFO, " new absolute packet format\n");
    if (SYN_MODEL_PEN(synhw))
        SynapticsMsg(priv, MSG_INFO, " pen detection\n");

    if (SYN_CAP_EXTENDED(synhw)) {
        SynapticsMsg(priv, MSG_INFO,
                    " Touchpad has extended capability bits\n");
        if (SYN_CAP_MULTI_BUTTON_NO(synhw))
            SynapticsMsg(priv, MSG_INFO,
                         " -> %d multi buttons, i.e. besides standard buttons\n",
                         (int) (SYN_CAP_MULTI_BUTTON_NO(synhw)));
        if (SYN_CAP_MIDDLE_BUTTON(synhw))
            SynapticsMsg(priv, MSG_INFO, " -> middle button\n");
        if (SYN_CAP_FOUR_BUTTON(synhw))
            SynapticsMsg(priv, MSG_INFO, " -> four buttons\n");
        if (SYN_CAP_MULTIFINGER(synhw))
            SynapticsMsg(priv, MSG_INFO, " -> multifinger detection\n");
        if (SYN_CAP_PALMDETECT(synhw))
            SynapticsMsg(priv, MSG_INFO, " -> palm detection\n");
        if (SYN_CAP_PASSTHROUGH(synhw))
            SynapticsMsg(priv, MSG_INFO, " -> pass-through port\n");
    }
}

static Bool
PS2DeviceOffHook(SynapticsPrivate *priv)
{
    ps2_synaptics_reset(priv->fd);
    ps2_synaptics_enable_device(priv->fd);

    PS2ProtoDataCleanup(priv);

    return TRUE;
}

static Bool
PS2QueryHardware(SynapticsPrivate *priv, int fd)
{
    int mode;
    struct PS2SynapticsHwInfo *synhw;

    if (!priv->proto_data)
        priv->proto_data = calloc(1, sizeof(struct PS2SynapticsHwInfo));
    synhw = (struct PS2SynapticsHwInfo *) priv->proto_data;

    /* is the synaptics touchpad active? */
    if (!ps2_query_is_synaptics(priv, fd, synhw))
        return FALSE;

    SynapticsMsg(priv, MSG_INFO, "synaptics touchpad found\n");

    if (!ps2_synaptics_reset(fd))
        SynapticsMsg(priv, MSG_ERROR, "reset failed\n");

    if (!ps2_synaptics_identify(fd, synhw))
        return FALSE;

    if (!ps2_synaptics_model_id(fd, synhw))
        return FALSE;

    if (!ps2_synaptics_capability(fd, synhw))
        return FALSE;

    mode = SYN_BIT_ABSOLUTE_MODE | SYN_BIT_HIGH_RATE;
    if (SYN_ID_MAJOR(synhw) >= 4)
        mode |= SYN_BIT_DISABLE_GESTURE;
    if (SYN_CAP_EXTENDED(synhw))
        mode |= SYN_BIT_W_MODE;
    if (!ps2_synaptics_set_mode(fd, mode))
        return FALSE;

    ps2_synaptics_enable_device(fd);

    ps2_print_ident(priv, synhw);

    return TRUE;
}

/*
 * Decide if the current packet stored in priv->protoBuf is valid.
 */
static Bool
ps2_packet_ok(struct PS2SynapticsHwInfo *synhw, struct PS2CommData *comm)
{
    unsigned char *buf = comm->protoBuf;
    int newabs = SYN_MODEL_NEWABS(synhw);

    if (newabs ? ((buf[0] & 0xC0) != 0x80) : ((buf[0] & 0xC0) != 0xC0)) {
        PS2DBG("Synaptics driver lost sync at 1st byte\n");
        return FALSE;
    }

    if (!newabs && ((buf[1] & 0x60) != 0x00)) {
        PS2DBG("Synaptics driver lost sync at 2nd byte\n");
        return FALSE;
    }

    if ((newabs ? ((buf[3] & 0xC0) != 0xC0) : ((buf[3] & 0xC0) != 0x80))) {
        PS2DBG("Synaptics driver lost sync at 4th byte\n");
        return FALSE;
    }

    if (!newabs && ((buf[4] & 0x60) != 0x00)) {
        PS2DBG("Synaptics driver lost sync at 5th byte\n");
        return FALSE;
    }

    return TRUE;
}

static Bool
ps2_synaptics_get_packet(SynapticsPrivate *priv, int fd,
                         struct PS2SynapticsHwInfo *synhw,
                         struct SynapticsProtocolOperations *proto_ops,
                         struct CommData *comm)
{
    struct PS2CommData *ps2_comm_data = priv->proto_data;
    int count = 0;
    int c;
    unsigned char u;

    while ((c = XisbRead(ps2_comm_data->buffer)) >= 0) {
        u = (unsigned char) c;

        /* test if there is a reset sequence received */
        if ((c == 0x00) && (ps2_comm_data->lastByte == 0xAA)) {
            if (xf86WaitForInput(fd, 50000) == 0) {
                PS2DBG("Reset received\n");
                proto_ops->QueryHardware(priv, fd);
            }
            else
                PS2DBG("faked reset received\n");
        }
        ps2_comm_data->lastByte = u;

        /* to avoid endless loops */
        if (count++ > 30) {
            SynapticsMsg(priv, MSG_ERROR,
                         "Synaptics driver lost sync... got gigantic packet!\n");
            return FALSE;
        }

        ps2_comm_data->protoBuf[ps2_comm_data->protoBufTail++] = u;

        /* Check that we have a valid packet. If not, we are out of sync,
           so we throw away the first byte in the packet. */
        if (ps2_comm_data->protoBufTail >= 6) {
            if (!ps2_packet_ok(synhw, ps2_comm_data)) {
                int i;

                for (i = 0; i < ps2_comm_data->protoBufTail - 1; i++)
                    ps2_comm_data->protoBuf[i] = ps2_comm_data->protoBuf[i + 1];
                ps2_comm_data->protoBufTail--;
                ps2_comm_data->outOfSync++;
                if (ps2_comm_data->outOfSync > MAX_UNSYNC_PACKETS) {
                    ps2_comm_data->outOfSync = 0;
                    PS2DBG("Synaptics synchronization lost too long -> reset touchpad.\n");
                    proto_ops->QueryHardware(priv, fd);    /* including a reset */
                    continue;
                }
            }
        }

        if (ps2_comm_data->protoBufTail >= 6) {  /* Full packet received */
            if (ps2_comm_data->outOfSync > 0) {
                ps2_comm_data->outOfSync = 0;
                PS2DBG("Synaptics driver resynced.\n");
            }
            ps2_comm_data->protoBufTail = 0;
            return TRUE;
        }
    }

    return FALSE;
}

Bool
PS2ReadHwStateProto(SynapticsPrivate *priv, int fd,
                    struct SynapticsProtocolOperations *proto_ops,
                    struct CommData *comm, struct SynapticsHwState *hwRet)
{
    struct PS2CommData *ps2_comm_data = priv->proto_data;
    unsigned char *buf = ps2_comm_data->protoBuf;
    struct SynapticsHwState *hw = comm->hwState;
    SynapticsParameters *para = &priv->synpara;
    struct PS2SynapticsHwInfo *synhw;
    int newabs;
    int w, i;

    synhw = (struct PS2SynapticsHwInfo *) priv->proto_data;
    if (!synhw) {
        SynapticsMsg(priv, MSG_ERROR,
                     "PS2ReadHwState, synhw is NULL. This is a bug.\n");
        return FALSE;
    }

    newabs = SYN_MODEL_NEWABS(synhw);

    if (!ps2_synaptics_get_packet(priv, fd, synhw, proto_ops, comm))
        return FALSE;

    /* Handle normal packets */
    hw->x = hw->y = hw->z = hw->numFingers = hw->fingerWidth = 0;
    hw->left = hw->right = hw->up = hw->down = hw->middle = FALSE;
    for (i = 0; i < 8; i++)
        hw->multi[i] = FALSE;

    if (newabs) {               /* newer protos... */
        PS2DBG("using new protocols\n");
        hw->x = (((buf[3] & 0x10) << 8) | ((buf[1] & 0x0f) << 8) | buf[4]);
        hw->y = (((buf[3] & 0x20) << 7) | ((buf[1] & 0xf0) << 4) | buf[5]);

        hw->z = buf[2];
        w = (((buf[0] & 0x30) >> 2) |
             ((buf[0] & 0x04) >> 1) | ((buf[3] & 0x04) >> 2));

        hw->left = (buf[0] & 0x01) ? 1 : 0;
        hw->right = (buf[0] & 0x02) ? 1 : 0;

        if (SYN_CAP_EXTENDED(synhw)) {
            if (SYN_CAP_MIDDLE_BUTTON(synhw)) {
                hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
            }
            if (SYN_CAP_FOUR_BUTTON(synhw)) {
                hw->up = ((buf[3] & 0x01)) ? 1 : 0;
                if (hw->left)
                    hw->up = !hw->up;
                hw->down = ((buf[3] & 0x02)) ? 1 : 0;
                if (hw->right)
                    hw->down = !hw->down;
            }
            if (SYN_CAP_MULTI_BUTTON_NO(synhw)) {
                if ((buf[3] & 2) ? !hw->right : hw->right) {
                    switch (SYN_CAP_MULTI_BUTTON_NO(synhw) & ~0x01) {
                    default:
                        break;
                    case 8:
                        hw->multi[7] = ((buf[5] & 0x08)) ? 1 : 0;
                        hw->multi[6] = ((buf[4] & 0x08)) ? 1 : 0;
                    case 6:
                        hw->multi[5] = ((buf[5] & 0x04)) ? 1 : 0;
                        hw->multi[4] = ((buf[4] & 0x04)) ? 1 : 0;
                    case 4:
                        hw->multi[3] = ((buf[5] & 0x02)) ? 1 : 0;
                        hw->multi[2] = ((buf[4] & 0x02)) ? 1 : 0;
                    case 2:
                        hw->multi[1] = ((buf[5] & 0x01)) ? 1 : 0;
                        hw->multi[0] = ((buf[4] & 0x01)) ? 1 : 0;
                    }
                }
            }
        }
    }
    else {                      /* old proto... */
        PS2DBG("using old protocol\n");
        hw->x = (((buf[1] & 0x1F) << 8) | buf[2]);
        hw->y = (((buf[4] & 0x1F) << 8) | buf[5]);

        hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
        w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));

        hw->left = (buf[0] & 0x01) ? 1 : 0;
        hw->right = (buf[0] & 0x02) ? 1 : 0;
    }

    hw->y = YMAX_NOMINAL + YMIN_NOMINAL - hw->y;

    if (hw->z >= para->finger_high) {
        int w_ok = 0;

        /*
         * Use capability bits to decide if the w value is valid.
         * If not, set it to 5, which corresponds to a finger of
         * normal width.
         */
        if (SYN_CAP_EXTENDED(synhw)) {
            if ((w >= 0) && (w <= 1)) {
                w_ok = SYN_CAP_MULTIFINGER(synhw);
            }
            else if (w == 2) {
                w_ok = SYN_MODEL_PEN(synhw);
            }
            else if ((w >= 4) && (w <= 15)) {
                w_ok = SYN_CAP_PALMDETECT(synhw);
            }
        }
        if (!w_ok)
            w = 5;

        switch (w) {
        case 0:
            hw->numFingers = 2;
            hw->fingerWidth = 5;
            break;
        case 1:
            hw->numFingers = 3;
            hw->fingerWidth = 5;
            break;
        default:
            hw->numFingers = 1;
            hw->fingerWidth = w;
            break;
        }
    }
    hw->millis = GetTimeInMillis();
    SynapticsCopyHwState(hwRet, hw);
    return TRUE;
}

static Bool
PS2ReadHwState(SynapticsPrivate *priv, int fd,
               struct CommData *comm, struct SynapticsHwState *hwRet)
{
    return PS2ReadHwStateProto(priv, fd, &psaux_proto_operations, comm, hwRet);
}

Bool
PS2ProtoDataInit(SynapticsPrivate *priv)
{
    struct PS2CommData *data;

    data = calloc(1, sizeof(*data));
    if (data) {
        data->buffer = XisbNew(priv->fd, INPUT_BUFFER_SIZE);
        if (!data->buffer) {
            free(data);
            data = NULL;
        }
    }
    priv->proto_data = data;

    return (priv->proto_data != NULL);
}

void
PS2ProtoDataCleanup(SynapticsPrivate *priv)
{
    struct PS2CommData *data = priv->proto_data;

    XisbFree(data->buffer);

    free(data);
    priv->proto_data = NULL;
}


Bool
PS2DeviceOnHook(SynapticsPrivate *priv, SynapticsParameters *para)
{
    return PS2ProtoDataInit(priv);
}

struct SynapticsProtocolOperations psaux_proto_operations = {
    PS2DeviceOnHook,
    PS2DeviceOffHook,
    PS2QueryHardware,
    PS2ReadHwState,
    NULL,
    NULL
};