ps2comm.c 17.7 KB
Newer Older
1 2 3 4 5 6
/*
 * 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>
7
 *
8 9 10 11 12 13 14 15 16 17
 * 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.
18
 *
19 20 21 22 23 24 25
 * 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.
26
 *
27 28 29 30 31 32
 * 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)
33 34
 */

35 36 37 38
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

39
#include <xorg-server.h>
40
#include "ps2comm.h"
41
#include "synproto.h"
42
#include "synaptics.h"
43
#include "synapticsstr.h"
44
#include <xf86.h>
45

46
#define MAX_UNSYNC_PACKETS 10				/* i.e. 10 to 60 bytes */
47 48 49 50 51 52 53 54 55 56 57
/*
 * 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
58

59
/* synaptics queries */
60 61 62 63 64 65 66 67
#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
68 69

/* status request response bits (PS2_CMD_STATUS_REQUEST) */
70 71 72 73 74 75 76 77
#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)
78

79
#ifdef DEBUG
80
#define PS2DBG(x) (x)
81
#else
82
#define PS2DBG(x)
83
#endif
84

85 86 87 88 89 90 91
struct SynapticsHwInfo {
    unsigned int model_id;		    /* Model-ID */
    unsigned int capabilities;		    /* Capabilities */
    unsigned int ext_cap;		    /* Extended Capabilities */
    unsigned int identity;		    /* Identification */
};

92 93 94 95 96
/*****************************************************************************
 *	PS/2 Utility functions.
 *     Many parts adapted from tpconfig.c by C. Scott Ananian
 ****************************************************************************/

97
/*
98 99
 * Read a byte from the ps/2 port
 */
100 101
static Bool
ps2_getbyte(int fd, byte *b)
102
{
103 104
    if (xf86WaitForInput(fd, 50000) > 0) {
	if (xf86ReadSerial(fd, b, 1) != 1) {
105
	    PS2DBG(ErrorF("ps2_getbyte: No byte read\n"));
106
	    return FALSE;
107
	}
108
	PS2DBG(ErrorF("ps2_getbyte: byte %02X read\n", *b));
109
	return TRUE;
110
    }
111
    PS2DBG(ErrorF("ps2_getbyte: timeout xf86WaitForInput\n"));
112
    return FALSE;
113 114 115
}

/*
Peter Osterlund's avatar
Peter Osterlund committed
116
 * Write a byte to the ps/2 port, wait for ACK
117
 */
118
Bool
119
ps2_putbyte(int fd, byte b)
120
{
121
    byte ack;
122

123
    if (xf86WriteSerial(fd, &b, 1) != 1) {
124
	PS2DBG(ErrorF("ps2_putbyte: error xf86WriteSerial\n"));
125
	return FALSE;
126
    }
127
    PS2DBG(ErrorF("ps2_putbyte: byte %02X send\n", b));
128
    /* wait for an ACK */
129 130
    if (!ps2_getbyte(fd, &ack)) {
	return FALSE;
131
    }
132
    if (ack != PS2_ACK) {
133
	PS2DBG(ErrorF("ps2_putbyte: wrong acknowledge 0x%02x\n", ack));
134
	return FALSE;
135
    }
136
    return TRUE;
137 138 139
}

/*
140 141
 * Use the Synaptics extended ps/2 syntax to write a special command byte. Needed by
 * ps2_send_cmd and ps2_set_mode.
142 143 144
 * 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)
 */
145 146
static Bool
ps2_special_cmd(int fd, byte cmd)
147
{
148 149 150
    int i;

    /* initialize with 'inert' command */
151 152 153 154 155 156 157 158 159 160 161
    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;
162 163 164 165
}

/*
 * Send a command to the synpatics touchpad by special commands
166
 */
167 168 169
static Bool
ps2_send_cmd(int fd, byte c)
{
170
    PS2DBG(ErrorF("send command: 0x%02X\n", c));
171 172
    return (ps2_special_cmd(fd, c) &&
	    ps2_putbyte(fd, PS2_CMD_STATUS_REQUEST));
173 174 175 176 177 178 179 180 181
}

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

/*
 * Set the synaptics touchpad mode byte by special commands
 */
182
static Bool
183
ps2_synaptics_set_mode(int fd, byte mode)
184
{
185
    PS2DBG(ErrorF("set mode byte to: 0x%02X\n", mode));
186 187
    return (ps2_special_cmd(fd, mode) &&
	    ps2_putbyte(fd, PS2_CMD_SET_SAMPLE_RATE) &&
188
	    ps2_putbyte(fd, 0x14));
189
}
190 191 192

/*
 * reset the touchpad
193
 */
194
static Bool
195
ps2_synaptics_reset(int fd)
196
{
197
    byte r[2];
198

199
    xf86FlushInput(fd);
200
    PS2DBG(ErrorF("Reset the Touchpad...\n"));
201
    if (!ps2_putbyte(fd, PS2_CMD_RESET)) {
202
	PS2DBG(ErrorF("...failed\n"));
203
	return FALSE;
204 205
    }
    xf86WaitForInput(fd, 4000000);
206
    if (ps2_getbyte(fd, &r[0]) && ps2_getbyte(fd, &r[1])) {
207
	if (r[0] == 0xAA && r[1] == 0x00) {
208
	    PS2DBG(ErrorF("...done\n"));
209
	    return TRUE;
210
	} else {
211
	    PS2DBG(ErrorF("...failed. Wrong reset ack 0x%02x, 0x%02x\n", r[0], r[1]));
212
	    return FALSE;
213
	}
214
    }
215
    PS2DBG(ErrorF("...failed\n"));
216
    return FALSE;
217 218
}

219
/*
220 221 222
 * Read the model-id bytes from the touchpad
 * see also SYN_MODEL_* macros
 */
223
static Bool
224
ps2_synaptics_model_id(int fd, struct SynapticsHwInfo *synhw)
225
{
226
    byte mi[3];
227

228
    PS2DBG(ErrorF("Read mode id...\n"));
229

230 231 232 233 234
    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])) {
235
	synhw->model_id = (mi[0] << 16) | (mi[1] << 8) | mi[2];
236
	PS2DBG(ErrorF("model-id %06X\n", synhw->model_id));
237
	PS2DBG(ErrorF("...done.\n"));
238
	return TRUE;
239
    }
240
    PS2DBG(ErrorF("...failed.\n"));
241
    return FALSE;
242 243 244 245 246 247
}

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

253
    PS2DBG(ErrorF("Read capabilites...\n"));
254

255
    synhw->capabilities = 0;
256
    synhw->ext_cap = 0;
257 258 259 260
    if (ps2_send_cmd(fd, SYN_QUE_CAPABILITIES) &&
	ps2_getbyte(fd, &cap[0]) &&
	ps2_getbyte(fd, &cap[1]) &&
	ps2_getbyte(fd, &cap[2])) {
261
	synhw->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
262
	PS2DBG(ErrorF("capabilities %06X\n", synhw->capabilities));
263 264
	if (SYN_CAP_VALID(synhw)) {
	    if (SYN_EXT_CAP_REQUESTS(synhw)) {
265 266 267 268
		if (ps2_send_cmd(fd, SYN_QUE_EXT_CAPAB) &&
		    ps2_getbyte(fd, &cap[0]) &&
		    ps2_getbyte(fd, &cap[1]) &&
		    ps2_getbyte(fd, &cap[2])) {
269
		    synhw->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
270
		    PS2DBG(ErrorF("ext-capability %06X\n", synhw->ext_cap));
271
		} else {
272 273
		    PS2DBG(ErrorF("synaptics says, that it has extended-capabilities, "
				  "but I cannot read them."));
274 275
		}
	    }
276
	    PS2DBG(ErrorF("...done.\n"));
277
	    return TRUE;
278
	}
279
    }
280
    PS2DBG(ErrorF("...failed.\n"));
281
    return FALSE;
282 283 284
}

/*
285
 * Identify Touchpad
286 287
 * See also the SYN_ID_* macros
 */
288
static Bool
289
ps2_synaptics_identify(int fd, struct SynapticsHwInfo *synhw)
290
{
291
    byte id[3];
292

293
    PS2DBG(ErrorF("Identify Touchpad...\n"));
294

295
    synhw->identity = 0;
296 297 298 299
    if (ps2_send_cmd(fd, SYN_QUE_IDENTIFY) &&
	ps2_getbyte(fd, &id[0]) &&
	ps2_getbyte(fd, &id[1]) &&
	ps2_getbyte(fd, &id[2])) {
300
	synhw->identity = (id[0] << 16) | (id[1] << 8) | id[2];
301
	PS2DBG(ErrorF("ident %06X\n", synhw->identity));
302
	if (SYN_ID_IS_SYNAPTICS(synhw)) {
303
	    PS2DBG(ErrorF("...done.\n"));
304
	    return TRUE;
305
	}
306
    }
307
    PS2DBG(ErrorF("...failed.\n"));
308
    return FALSE;
309 310
}

311
static Bool
312
ps2_synaptics_enable_device(int fd)
313
{
314
    return ps2_putbyte(fd, PS2_CMD_ENABLE);
315 316
}

317
static Bool
318
ps2_synaptics_disable_device(int fd)
319
{
320 321
    xf86FlushInput(fd);
    return ps2_putbyte(fd, PS2_CMD_DISABLE);
322 323
}

324
static Bool
325
ps2_query_is_synaptics(int fd, struct SynapticsHwInfo* synhw)
326
{
327 328 329
    int i;

    for (i = 0; i < 3; i++) {
330
	if (ps2_synaptics_disable_device(fd))
331 332 333 334 335
	    break;
    }

    xf86WaitForInput(fd, 20000);
    xf86FlushInput(fd);
336
    if (ps2_synaptics_identify(fd, synhw)) {
337 338
	return TRUE;
    } else {
339
	xf86Msg(X_ERROR, "Query no Synaptics: %06X\n", synhw->identity);
340 341
	return FALSE;
    }
342
}
343

344
void
345
ps2_print_ident(const struct SynapticsHwInfo *synhw)
346
{
347 348 349
    xf86Msg(X_PROBED, " Synaptics Touchpad, model: %d\n", SYN_ID_MODEL(synhw));
    xf86Msg(X_PROBED, " Firmware: %d.%d\n", SYN_ID_MAJOR(synhw),
	    SYN_ID_MINOR(synhw));
350

351
    if (SYN_MODEL_ROT180(synhw))
352
	xf86Msg(X_PROBED, " 180 degree mounted touchpad\n");
353
    if (SYN_MODEL_PORTRAIT(synhw))
354
	xf86Msg(X_PROBED, " portrait touchpad\n");
355 356
    xf86Msg(X_PROBED, " Sensor: %d\n", SYN_MODEL_SENSOR(synhw));
    if (SYN_MODEL_NEWABS(synhw))
357
	xf86Msg(X_PROBED, " new absolute packet format\n");
358
    if (SYN_MODEL_PEN(synhw))
359 360
	xf86Msg(X_PROBED, " pen detection\n");

361
    if (SYN_CAP_EXTENDED(synhw)) {
362
	xf86Msg(X_PROBED, " Touchpad has extended capability bits\n");
363
	if (SYN_CAP_MULTI_BUTTON_NO(synhw))
364
	    xf86Msg(X_PROBED, " -> %d multi buttons, i.e. besides standard buttons\n",
365 366
		    (int)(SYN_CAP_MULTI_BUTTON_NO(synhw)));
	if (SYN_CAP_MIDDLE_BUTTON(synhw))
367
	    xf86Msg(X_PROBED, " -> middle button\n");
368
	if (SYN_CAP_FOUR_BUTTON(synhw))
369
	    xf86Msg(X_PROBED, " -> four buttons\n");
370
	if (SYN_CAP_MULTIFINGER(synhw))
371
	    xf86Msg(X_PROBED, " -> multifinger detection\n");
372
	if (SYN_CAP_PALMDETECT(synhw))
373
	    xf86Msg(X_PROBED, " -> palm detection\n");
374
	if (SYN_CAP_PASSTHROUGH(synhw))
375 376 377 378
	    xf86Msg(X_PROBED, " -> pass-through port\n");
    }
}

379
static void
380
PS2DeviceOffHook(InputInfoPtr pInfo)
381
{
382 383
    ps2_synaptics_reset(pInfo->fd);
    ps2_synaptics_enable_device(pInfo->fd);
384 385
}

386
static Bool
387
PS2QueryHardware(InputInfoPtr pInfo)
388 389
{
    int mode;
390
    SynapticsPrivate *priv = (SynapticsPrivate *)pInfo->private;
391 392 393
    struct SynapticsHwInfo *synhw;

    if (!priv->proto_data)
394
        priv->proto_data = calloc(1, sizeof(struct SynapticsHwInfo));
395
    synhw = (struct SynapticsHwInfo*)priv->proto_data;
396 397

    /* is the synaptics touchpad active? */
398
    if (!ps2_query_is_synaptics(pInfo->fd, synhw))
399 400
	return FALSE;

401
    xf86Msg(X_PROBED, "%s synaptics touchpad found\n", pInfo->name);
402

403 404
    if (!ps2_synaptics_reset(pInfo->fd))
	xf86Msg(X_ERROR, "%s reset failed\n", pInfo->name);
405

406
    if (!ps2_synaptics_identify(pInfo->fd, synhw))
407 408
	return FALSE;

409
    if (!ps2_synaptics_model_id(pInfo->fd, synhw))
410 411
	return FALSE;

412
    if (!ps2_synaptics_capability(pInfo->fd, synhw))
413 414 415
	return FALSE;

    mode = SYN_BIT_ABSOLUTE_MODE | SYN_BIT_HIGH_RATE;
416
    if (SYN_ID_MAJOR(synhw) >= 4)
417
	mode |= SYN_BIT_DISABLE_GESTURE;
418
    if (SYN_CAP_EXTENDED(synhw))
419
	mode |= SYN_BIT_W_MODE;
420
    if (!ps2_synaptics_set_mode(pInfo->fd, mode))
421 422
	return FALSE;

423
    ps2_synaptics_enable_device(pInfo->fd);
424

425
    ps2_print_ident(synhw);
426 427 428 429

    return TRUE;
}

430 431 432 433
/*
 * Decide if the current packet stored in priv->protoBuf is valid.
 */
static Bool
434
ps2_packet_ok(struct SynapticsHwInfo *synhw, struct CommData *comm)
435
{
436
    unsigned char *buf = comm->protoBuf;
437
    int newabs = SYN_MODEL_NEWABS(synhw);
438 439

    if (newabs ? ((buf[0] & 0xC0) != 0x80) : ((buf[0] & 0xC0) != 0xC0)) {
440
	DBG(4, "Synaptics driver lost sync at 1st byte\n");
441 442 443 444
	return FALSE;
    }

    if (!newabs && ((buf[1] & 0x60) != 0x00)) {
445
	DBG(4, "Synaptics driver lost sync at 2nd byte\n");
446 447 448 449
	return FALSE;
    }

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

    if (!newabs && ((buf[4] & 0x60) != 0x00)) {
455
	DBG(4, "Synaptics driver lost sync at 5th byte\n");
456 457 458 459 460 461 462
	return FALSE;
    }

    return TRUE;
}

static Bool
463
ps2_synaptics_get_packet(InputInfoPtr pInfo, struct SynapticsHwInfo *synhw,
464
			 struct SynapticsProtocolOperations *proto_ops,
465
			 struct CommData *comm)
466 467 468 469 470
{
    int count = 0;
    int c;
    unsigned char u;

471
    while ((c = XisbRead(comm->buffer)) >= 0) {
472 473 474
	u = (unsigned char)c;

	/* test if there is a reset sequence received */
475
	if ((c == 0x00) && (comm->lastByte == 0xAA)) {
476
	    if (xf86WaitForInput(pInfo->fd, 50000) == 0) {
477
		DBG(7, "Reset received\n");
478
		proto_ops->QueryHardware(pInfo);
479
	    } else
480
		DBG(3, "faked reset received\n");
481
	}
482
	comm->lastByte = u;
483 484 485

	/* to avoid endless loops */
	if (count++ > 30) {
486
	    xf86Msg(X_ERROR, "Synaptics driver lost sync... got gigantic packet!\n");
487 488 489
	    return FALSE;
	}

490
	comm->protoBuf[comm->protoBufTail++] = u;
491 492 493

	/* Check that we have a valid packet. If not, we are out of sync,
	   so we throw away the first byte in the packet.*/
494
	if (comm->protoBufTail >= 6) {
495
	    if (!ps2_packet_ok(synhw, comm)) {
496
		int i;
497 498 499 500 501 502
		for (i = 0; i < comm->protoBufTail - 1; i++)
		    comm->protoBuf[i] = comm->protoBuf[i + 1];
		comm->protoBufTail--;
		comm->outOfSync++;
		if (comm->outOfSync > MAX_UNSYNC_PACKETS) {
		    comm->outOfSync = 0;
503
		    DBG(3, "Synaptics synchronization lost too long -> reset touchpad.\n");
504
		    proto_ops->QueryHardware(pInfo); /* including a reset */
505 506 507 508 509
		    continue;
		}
	    }
	}

510 511 512
	if (comm->protoBufTail >= 6) { /* Full packet received */
	    if (comm->outOfSync > 0) {
		comm->outOfSync = 0;
513
		DBG(4, "Synaptics driver resynced.\n");
514
	    }
515
	    comm->protoBufTail = 0;
516 517 518 519 520 521 522 523
	    return TRUE;
	}
    }

    return FALSE;
}

static Bool
524
PS2ReadHwState(InputInfoPtr pInfo,
525
	       struct SynapticsProtocolOperations *proto_ops,
526
	       struct CommData *comm, struct SynapticsHwState *hwRet)
527
{
528 529
    unsigned char *buf = comm->protoBuf;
    struct SynapticsHwState *hw = &(comm->hwState);
530
    SynapticsPrivate *priv = (SynapticsPrivate *)pInfo->private;
531
    SynapticsParameters *para = &priv->synpara;
532 533
    struct SynapticsHwInfo *synhw;
    int newabs;
534 535
    int w, i;

536 537 538 539 540
    synhw = (struct SynapticsHwInfo*)priv->proto_data;
    if (!synhw)
    {
        xf86Msg(X_ERROR,
                "%s: PS2ReadHwState, synhw is NULL. This is a bug.\n",
541
                pInfo->name);
542 543 544 545 546
        return FALSE;
    }

    newabs = SYN_MODEL_NEWABS(synhw);

547
    if (!ps2_synaptics_get_packet(pInfo, synhw, proto_ops, comm))
548 549 550 551 552 553 554 555 556
	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...*/
557
	DBG(7, "using new protocols\n");
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
	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;

573 574
	if (SYN_CAP_EXTENDED(synhw)) {
	    if (SYN_CAP_MIDDLE_BUTTON(synhw)) {
575 576
		hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
	    }
577
	    if (SYN_CAP_FOUR_BUTTON(synhw)) {
578 579 580 581 582 583 584
		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;
	    }
585
	    if (SYN_CAP_MULTI_BUTTON_NO(synhw)) {
586
		if ((buf[3] & 2) ? !hw->right : hw->right) {
587
		    switch (SYN_CAP_MULTI_BUTTON_NO(synhw) & ~0x01) {
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		    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...*/
607
	DBG(7, "using old protocol\n");
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	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;

624
    if (hw->z >= para->finger_high) {
625 626 627 628 629 630
	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.
	 */
631
	if (SYN_CAP_EXTENDED(synhw)) {
632
	    if ((w >= 0) && (w <= 1)) {
633
		w_ok = SYN_CAP_MULTIFINGER(synhw);
634
	    } else if (w == 2) {
635
		w_ok = SYN_MODEL_PEN(synhw);
636
	    } else if ((w >= 4) && (w <= 15)) {
637
		w_ok = SYN_CAP_PALMDETECT(synhw);
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
	    }
	}
	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;
	}
    }

    *hwRet = *hw;
    return TRUE;
}

663
static Bool
664
PS2AutoDevProbe(InputInfoPtr pInfo)
665 666 667 668
{
    return FALSE;
}

669
struct SynapticsProtocolOperations psaux_proto_operations = {
670
    NULL,
671
    PS2DeviceOffHook,
672
    PS2QueryHardware,
673
    PS2ReadHwState,
674
    PS2AutoDevProbe,
675
    SynapticsDefaultDimensions
676
};