MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / mouse / synaptics.c
blob101b8094720a4162b7941d1a45a4018358786fa0
1 /*
2 * Synaptics TouchPad PS/2 mouse driver
4 * 2003 Dmitry Torokhov <dtor@mail.ru>
5 * Added support for pass-through port. Special thanks to Peter Berg Larsen
6 * for explaining various Synaptics quirks.
8 * 2003 Peter Osterlund <petero2@telia.com>
9 * Ported to 2.5 input device infrastructure.
11 * Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
12 * start merging tpconfig and gpm code to a xfree-input module
13 * adding some changes and extensions (ex. 3rd and 4th button)
15 * Copyright (c) 1997 C. Scott Ananian <cananian@alumni.priceton.edu>
16 * Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
17 * code for the special synaptics commands (from the tpconfig-source)
19 * This program is free software; you can redistribute it and/or modify it
20 * under the terms of the GNU General Public License version 2 as published by
21 * the Free Software Foundation.
23 * Trademarks are the property of their respective owners.
26 #include <linux/module.h>
27 #include <linux/input.h>
28 #include <linux/serio.h>
29 #include "psmouse.h"
30 #include "synaptics.h"
33 * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
34 * section 2.3.2, which says that they should be valid regardless of the
35 * actual size of the sensor.
37 #define XMIN_NOMINAL 1472
38 #define XMAX_NOMINAL 5472
39 #define YMIN_NOMINAL 1408
40 #define YMAX_NOMINAL 4448
42 /*****************************************************************************
43 * Synaptics communications functions
44 ****************************************************************************/
47 * Send a command to the synpatics touchpad by special commands
49 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
51 if (psmouse_sliced_command(psmouse, c))
52 return -1;
53 if (psmouse_command(psmouse, param, PSMOUSE_CMD_GETINFO))
54 return -1;
55 return 0;
59 * Set the synaptics touchpad mode byte by special commands
61 static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
63 unsigned char param[1];
65 if (psmouse_sliced_command(psmouse, mode))
66 return -1;
67 param[0] = SYN_PS_SET_MODE2;
68 if (psmouse_command(psmouse, param, PSMOUSE_CMD_SETRATE))
69 return -1;
70 return 0;
74 * Read the model-id bytes from the touchpad
75 * see also SYN_MODEL_* macros
77 static int synaptics_model_id(struct psmouse *psmouse)
79 struct synaptics_data *priv = psmouse->private;
80 unsigned char mi[3];
82 if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
83 return -1;
84 priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
85 return 0;
89 * Read the capability-bits from the touchpad
90 * see also the SYN_CAP_* macros
92 static int synaptics_capability(struct psmouse *psmouse)
94 struct synaptics_data *priv = psmouse->private;
95 unsigned char cap[3];
97 if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
98 return -1;
99 priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
100 priv->ext_cap = 0;
101 if (!SYN_CAP_VALID(priv->capabilities))
102 return -1;
105 * Unless capExtended is set the rest of the flags should be ignored
107 if (!SYN_CAP_EXTENDED(priv->capabilities))
108 priv->capabilities = 0;
110 if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
111 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
112 printk(KERN_ERR "Synaptics claims to have extended capabilities,"
113 " but I'm not able to read them.");
114 } else {
115 priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
118 * if nExtBtn is greater than 8 it should be considered
119 * invalid and treated as 0
121 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
122 priv->ext_cap &= 0xff0fff;
125 return 0;
129 * Identify Touchpad
130 * See also the SYN_ID_* macros
132 static int synaptics_identify(struct psmouse *psmouse)
134 struct synaptics_data *priv = psmouse->private;
135 unsigned char id[3];
137 if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
138 return -1;
139 priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
140 if (SYN_ID_IS_SYNAPTICS(priv->identity))
141 return 0;
142 return -1;
145 static void print_ident(struct synaptics_data *priv)
147 printk(KERN_INFO "Synaptics Touchpad, model: %ld\n", SYN_ID_MODEL(priv->identity));
148 printk(KERN_INFO " Firmware: %ld.%ld\n", SYN_ID_MAJOR(priv->identity),
149 SYN_ID_MINOR(priv->identity));
150 if (SYN_MODEL_ROT180(priv->model_id))
151 printk(KERN_INFO " 180 degree mounted touchpad\n");
152 if (SYN_MODEL_PORTRAIT(priv->model_id))
153 printk(KERN_INFO " portrait touchpad\n");
154 printk(KERN_INFO " Sensor: %ld\n", SYN_MODEL_SENSOR(priv->model_id));
155 if (SYN_MODEL_NEWABS(priv->model_id))
156 printk(KERN_INFO " new absolute packet format\n");
157 if (SYN_MODEL_PEN(priv->model_id))
158 printk(KERN_INFO " pen detection\n");
160 if (SYN_CAP_EXTENDED(priv->capabilities)) {
161 printk(KERN_INFO " Touchpad has extended capability bits\n");
162 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
163 printk(KERN_INFO " -> %d multi-buttons, i.e. besides standard buttons\n",
164 (int)(SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap)));
165 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
166 printk(KERN_INFO " -> middle button\n");
167 if (SYN_CAP_FOUR_BUTTON(priv->capabilities))
168 printk(KERN_INFO " -> four buttons\n");
169 if (SYN_CAP_MULTIFINGER(priv->capabilities))
170 printk(KERN_INFO " -> multifinger detection\n");
171 if (SYN_CAP_PALMDETECT(priv->capabilities))
172 printk(KERN_INFO " -> palm detection\n");
173 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
174 printk(KERN_INFO " -> pass-through port\n");
178 static int synaptics_query_hardware(struct psmouse *psmouse)
180 int retries = 0;
182 while ((retries++ < 3) && psmouse_reset(psmouse))
183 printk(KERN_ERR "synaptics reset failed\n");
185 if (synaptics_identify(psmouse))
186 return -1;
187 if (synaptics_model_id(psmouse))
188 return -1;
189 if (synaptics_capability(psmouse))
190 return -1;
192 return 0;
195 static int synaptics_set_mode(struct psmouse *psmouse, int mode)
197 struct synaptics_data *priv = psmouse->private;
199 mode |= SYN_BIT_ABSOLUTE_MODE;
200 if (psmouse_rate >= 80)
201 mode |= SYN_BIT_HIGH_RATE;
202 if (SYN_ID_MAJOR(priv->identity) >= 4)
203 mode |= SYN_BIT_DISABLE_GESTURE;
204 if (SYN_CAP_EXTENDED(priv->capabilities))
205 mode |= SYN_BIT_W_MODE;
206 if (synaptics_mode_cmd(psmouse, mode))
207 return -1;
209 return 0;
212 /*****************************************************************************
213 * Synaptics pass-through PS/2 port support
214 ****************************************************************************/
215 static int synaptics_pt_write(struct serio *serio, unsigned char c)
217 struct psmouse *parent = serio->parent->private;
218 char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
220 if (psmouse_sliced_command(parent, c))
221 return -1;
222 if (psmouse_command(parent, &rate_param, PSMOUSE_CMD_SETRATE))
223 return -1;
224 return 0;
227 static inline int synaptics_is_pt_packet(unsigned char *buf)
229 return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
232 static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
234 struct psmouse *child = ptport->private;
236 if (child && child->state == PSMOUSE_ACTIVATED) {
237 serio_interrupt(ptport, packet[1], 0, NULL);
238 serio_interrupt(ptport, packet[4], 0, NULL);
239 serio_interrupt(ptport, packet[5], 0, NULL);
240 if (child->type >= PSMOUSE_GENPS)
241 serio_interrupt(ptport, packet[2], 0, NULL);
242 } else
243 serio_interrupt(ptport, packet[1], 0, NULL);
246 static void synaptics_pt_activate(struct psmouse *psmouse)
248 struct psmouse *child = psmouse->serio->child->private;
250 /* adjust the touchpad to child's choice of protocol */
251 if (child && child->type >= PSMOUSE_GENPS) {
252 if (synaptics_set_mode(psmouse, SYN_BIT_FOUR_BYTE_CLIENT))
253 printk(KERN_INFO "synaptics: failed to enable 4-byte guest protocol\n");
257 static void synaptics_pt_create(struct psmouse *psmouse)
259 struct serio *serio;
261 serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
262 if (!serio) {
263 printk(KERN_ERR "synaptics: not enough memory to allocate pass-through port\n");
264 return;
267 memset(serio, 0, sizeof(struct serio));
269 serio->type = SERIO_PS_PSTHRU;
270 strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
271 strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
272 serio->write = synaptics_pt_write;
273 serio->parent = psmouse->serio;
275 psmouse->pt_activate = synaptics_pt_activate;
277 psmouse->serio->child = serio;
280 /*****************************************************************************
281 * Functions to interpret the absolute mode packets
282 ****************************************************************************/
284 static void synaptics_parse_hw_state(unsigned char buf[], struct synaptics_data *priv, struct synaptics_hw_state *hw)
286 memset(hw, 0, sizeof(struct synaptics_hw_state));
288 if (SYN_MODEL_NEWABS(priv->model_id)) {
289 hw->x = (((buf[3] & 0x10) << 8) |
290 ((buf[1] & 0x0f) << 8) |
291 buf[4]);
292 hw->y = (((buf[3] & 0x20) << 7) |
293 ((buf[1] & 0xf0) << 4) |
294 buf[5]);
296 hw->z = buf[2];
297 hw->w = (((buf[0] & 0x30) >> 2) |
298 ((buf[0] & 0x04) >> 1) |
299 ((buf[3] & 0x04) >> 2));
301 hw->left = (buf[0] & 0x01) ? 1 : 0;
302 hw->right = (buf[0] & 0x02) ? 1 : 0;
304 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
305 hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
307 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
308 hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
309 hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
312 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
313 ((buf[0] ^ buf[3]) & 0x02)) {
314 switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
315 default:
317 * if nExtBtn is greater than 8 it should be
318 * considered invalid and treated as 0
320 break;
321 case 8:
322 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
323 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
324 case 6:
325 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
326 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
327 case 4:
328 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
329 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
330 case 2:
331 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
332 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
335 } else {
336 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
337 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
339 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
340 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
342 hw->left = (buf[0] & 0x01) ? 1 : 0;
343 hw->right = (buf[0] & 0x02) ? 1 : 0;
348 * called for each full received packet from the touchpad
350 static void synaptics_process_packet(struct psmouse *psmouse)
352 struct input_dev *dev = &psmouse->dev;
353 struct synaptics_data *priv = psmouse->private;
354 struct synaptics_hw_state hw;
355 int num_fingers;
356 int finger_width;
357 int i;
359 synaptics_parse_hw_state(psmouse->packet, priv, &hw);
361 if (hw.z > 0) {
362 num_fingers = 1;
363 finger_width = 5;
364 if (SYN_CAP_EXTENDED(priv->capabilities)) {
365 switch (hw.w) {
366 case 0 ... 1:
367 if (SYN_CAP_MULTIFINGER(priv->capabilities))
368 num_fingers = hw.w + 2;
369 break;
370 case 2:
371 if (SYN_MODEL_PEN(priv->model_id))
372 ; /* Nothing, treat a pen as a single finger */
373 break;
374 case 4 ... 15:
375 if (SYN_CAP_PALMDETECT(priv->capabilities))
376 finger_width = hw.w;
377 break;
380 } else {
381 num_fingers = 0;
382 finger_width = 0;
385 /* Post events
386 * BTN_TOUCH has to be first as mousedev relies on it when doing
387 * absolute -> relative conversion
389 if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
390 if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
392 if (hw.z > 0) {
393 input_report_abs(dev, ABS_X, hw.x);
394 input_report_abs(dev, ABS_Y, YMAX_NOMINAL + YMIN_NOMINAL - hw.y);
396 input_report_abs(dev, ABS_PRESSURE, hw.z);
398 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
399 input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
400 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
401 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
403 input_report_key(dev, BTN_LEFT, hw.left);
404 input_report_key(dev, BTN_RIGHT, hw.right);
406 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
407 input_report_key(dev, BTN_MIDDLE, hw.middle);
409 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
410 input_report_key(dev, BTN_FORWARD, hw.up);
411 input_report_key(dev, BTN_BACK, hw.down);
414 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
415 input_report_key(dev, BTN_0 + i, hw.ext_buttons & (1 << i));
417 input_sync(dev);
420 static int synaptics_validate_byte(unsigned char packet[], int idx, unsigned char pkt_type)
422 static unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
423 static unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
424 static unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
425 static unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
426 static unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
428 if (idx < 0 || idx > 4)
429 return 0;
431 switch (pkt_type) {
432 case SYN_NEWABS:
433 case SYN_NEWABS_RELAXED:
434 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
436 case SYN_NEWABS_STRICT:
437 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
439 case SYN_OLDABS:
440 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
442 default:
443 printk(KERN_ERR "synaptics: unknown packet type %d\n", pkt_type);
444 return 0;
448 static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
450 int i;
452 for (i = 0; i < 5; i++)
453 if (!synaptics_validate_byte(psmouse->packet, i, SYN_NEWABS_STRICT)) {
454 printk(KERN_INFO "synaptics: using relaxed packet validation\n");
455 return SYN_NEWABS_RELAXED;
458 return SYN_NEWABS_STRICT;
461 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
463 struct input_dev *dev = &psmouse->dev;
464 struct synaptics_data *priv = psmouse->private;
466 input_regs(dev, regs);
468 if (psmouse->pktcnt >= 6) { /* Full packet received */
469 if (unlikely(priv->pkt_type == SYN_NEWABS))
470 priv->pkt_type = synaptics_detect_pkt_type(psmouse);
472 if (SYN_CAP_PASS_THROUGH(priv->capabilities) && synaptics_is_pt_packet(psmouse->packet)) {
473 if (psmouse->serio->child)
474 synaptics_pass_pt_packet(psmouse->serio->child, psmouse->packet);
475 } else
476 synaptics_process_packet(psmouse);
478 return PSMOUSE_FULL_PACKET;
481 return synaptics_validate_byte(psmouse->packet, psmouse->pktcnt - 1, priv->pkt_type) ?
482 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
485 /*****************************************************************************
486 * Driver initialization/cleanup functions
487 ****************************************************************************/
488 static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
490 int i;
492 set_bit(EV_ABS, dev->evbit);
493 input_set_abs_params(dev, ABS_X, XMIN_NOMINAL, XMAX_NOMINAL, 0, 0);
494 input_set_abs_params(dev, ABS_Y, YMIN_NOMINAL, YMAX_NOMINAL, 0, 0);
495 input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
496 set_bit(ABS_TOOL_WIDTH, dev->absbit);
498 set_bit(EV_KEY, dev->evbit);
499 set_bit(BTN_TOUCH, dev->keybit);
500 set_bit(BTN_TOOL_FINGER, dev->keybit);
501 set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
502 set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
504 set_bit(BTN_LEFT, dev->keybit);
505 set_bit(BTN_RIGHT, dev->keybit);
507 if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
508 set_bit(BTN_MIDDLE, dev->keybit);
510 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
511 set_bit(BTN_FORWARD, dev->keybit);
512 set_bit(BTN_BACK, dev->keybit);
515 for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
516 set_bit(BTN_0 + i, dev->keybit);
518 clear_bit(EV_REL, dev->evbit);
519 clear_bit(REL_X, dev->relbit);
520 clear_bit(REL_Y, dev->relbit);
523 void synaptics_reset(struct psmouse *psmouse)
525 /* reset touchpad back to relative mode, gestures enabled */
526 synaptics_mode_cmd(psmouse, 0);
529 static void synaptics_disconnect(struct psmouse *psmouse)
531 synaptics_reset(psmouse);
532 kfree(psmouse->private);
535 static int synaptics_reconnect(struct psmouse *psmouse)
537 struct synaptics_data *priv = psmouse->private;
538 struct synaptics_data old_priv = *priv;
540 if (!synaptics_detect(psmouse))
541 return -1;
543 if (synaptics_query_hardware(psmouse)) {
544 printk(KERN_ERR "Unable to query Synaptics hardware.\n");
545 return -1;
548 if (old_priv.identity != priv->identity ||
549 old_priv.model_id != priv->model_id ||
550 old_priv.capabilities != priv->capabilities ||
551 old_priv.ext_cap != priv->ext_cap)
552 return -1;
554 if (synaptics_set_mode(psmouse, 0)) {
555 printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
556 return -1;
559 return 0;
562 int synaptics_detect(struct psmouse *psmouse)
564 unsigned char param[4];
566 param[0] = 0;
568 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
569 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
570 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
571 psmouse_command(psmouse, param, PSMOUSE_CMD_SETRES);
572 psmouse_command(psmouse, param, PSMOUSE_CMD_GETINFO);
574 return param[1] == 0x47;
577 int synaptics_init(struct psmouse *psmouse)
579 struct synaptics_data *priv;
581 psmouse->private = priv = kmalloc(sizeof(struct synaptics_data), GFP_KERNEL);
582 if (!priv)
583 return -1;
584 memset(priv, 0, sizeof(struct synaptics_data));
586 if (synaptics_query_hardware(psmouse)) {
587 printk(KERN_ERR "Unable to query Synaptics hardware.\n");
588 goto init_fail;
591 if (synaptics_set_mode(psmouse, 0)) {
592 printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
593 goto init_fail;
596 priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
598 if (SYN_CAP_PASS_THROUGH(priv->capabilities))
599 synaptics_pt_create(psmouse);
601 print_ident(priv);
602 set_input_params(&psmouse->dev, priv);
604 psmouse->protocol_handler = synaptics_process_byte;
605 psmouse->disconnect = synaptics_disconnect;
606 psmouse->reconnect = synaptics_reconnect;
608 return 0;
610 init_fail:
611 kfree(priv);
612 return -1;