Input: synaptics - adjust threshold for treating position values as negative
[pandora-kernel.git] / drivers / input / mouse / synaptics.c
1 /*
2  * Synaptics TouchPad PS/2 mouse driver
3  *
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.
7  *
8  *   2003 Peter Osterlund <petero2@telia.com>
9  *     Ported to 2.5 input device infrastructure.
10  *
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)
14  *
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)
18  *
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.
22  *
23  * Trademarks are the property of their respective owners.
24  */
25
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/dmi.h>
29 #include <linux/input/mt.h>
30 #include <linux/serio.h>
31 #include <linux/libps2.h>
32 #include <linux/slab.h>
33 #include "psmouse.h"
34 #include "synaptics.h"
35
36 /*
37  * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
38  * section 2.3.2, which says that they should be valid regardless of the
39  * actual size of the sensor.
40  * Note that newer firmware allows querying device for maximum useable
41  * coordinates.
42  */
43 #define XMIN 0
44 #define XMAX 6143
45 #define YMIN 0
46 #define YMAX 6143
47 #define XMIN_NOMINAL 1472
48 #define XMAX_NOMINAL 5472
49 #define YMIN_NOMINAL 1408
50 #define YMAX_NOMINAL 4448
51
52 /* Size in bits of absolute position values reported by the hardware */
53 #define ABS_POS_BITS 13
54
55 /*
56  * These values should represent the absolute maximum value that will
57  * be reported for a positive position value. Some Synaptics firmware
58  * uses this value to indicate a finger near the edge of the touchpad
59  * whose precise position cannot be determined.
60  *
61  * At least one touchpad is known to report positions in excess of this
62  * value which are actually negative values truncated to the 13-bit
63  * reporting range. These values have never been observed to be lower
64  * than 8184 (i.e. -8), so we treat all values greater than 8176 as
65  * negative and any other value as positive.
66  */
67 #define X_MAX_POSITIVE 8176
68 #define Y_MAX_POSITIVE 8176
69
70 /*
71  * Synaptics touchpads report the y coordinate from bottom to top, which is
72  * opposite from what userspace expects.
73  * This function is used to invert y before reporting.
74  */
75 static int synaptics_invert_y(int y)
76 {
77         return YMAX_NOMINAL + YMIN_NOMINAL - y;
78 }
79
80
81 /*****************************************************************************
82  *      Stuff we need even when we do not want native Synaptics support
83  ****************************************************************************/
84
85 /*
86  * Set the synaptics touchpad mode byte by special commands
87  */
88 static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
89 {
90         unsigned char param[1];
91
92         if (psmouse_sliced_command(psmouse, mode))
93                 return -1;
94         param[0] = SYN_PS_SET_MODE2;
95         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
96                 return -1;
97         return 0;
98 }
99
100 int synaptics_detect(struct psmouse *psmouse, bool set_properties)
101 {
102         struct ps2dev *ps2dev = &psmouse->ps2dev;
103         unsigned char param[4];
104
105         param[0] = 0;
106
107         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
108         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
109         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
110         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
111         ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
112
113         if (param[1] != 0x47)
114                 return -ENODEV;
115
116         if (set_properties) {
117                 psmouse->vendor = "Synaptics";
118                 psmouse->name = "TouchPad";
119         }
120
121         return 0;
122 }
123
124 void synaptics_reset(struct psmouse *psmouse)
125 {
126         /* reset touchpad back to relative mode, gestures enabled */
127         synaptics_mode_cmd(psmouse, 0);
128 }
129
130 #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
131
132 /*****************************************************************************
133  *      Synaptics communications functions
134  ****************************************************************************/
135
136 /*
137  * Send a command to the synpatics touchpad by special commands
138  */
139 static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
140 {
141         if (psmouse_sliced_command(psmouse, c))
142                 return -1;
143         if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
144                 return -1;
145         return 0;
146 }
147
148 /*
149  * Read the model-id bytes from the touchpad
150  * see also SYN_MODEL_* macros
151  */
152 static int synaptics_model_id(struct psmouse *psmouse)
153 {
154         struct synaptics_data *priv = psmouse->private;
155         unsigned char mi[3];
156
157         if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
158                 return -1;
159         priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
160         return 0;
161 }
162
163 /*
164  * Read the capability-bits from the touchpad
165  * see also the SYN_CAP_* macros
166  */
167 static int synaptics_capability(struct psmouse *psmouse)
168 {
169         struct synaptics_data *priv = psmouse->private;
170         unsigned char cap[3];
171
172         if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
173                 return -1;
174         priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
175         priv->ext_cap = priv->ext_cap_0c = 0;
176
177         /*
178          * Older firmwares had submodel ID fixed to 0x47
179          */
180         if (SYN_ID_FULL(priv->identity) < 0x705 &&
181             SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
182                 return -1;
183         }
184
185         /*
186          * Unless capExtended is set the rest of the flags should be ignored
187          */
188         if (!SYN_CAP_EXTENDED(priv->capabilities))
189                 priv->capabilities = 0;
190
191         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
192                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
193                         psmouse_warn(psmouse,
194                                      "device claims to have extended capabilities, but I'm not able to read them.\n");
195                 } else {
196                         priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
197
198                         /*
199                          * if nExtBtn is greater than 8 it should be considered
200                          * invalid and treated as 0
201                          */
202                         if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
203                                 priv->ext_cap &= 0xff0fff;
204                 }
205         }
206
207         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
208                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
209                         psmouse_warn(psmouse,
210                                      "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
211                 } else {
212                         priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
213                 }
214         }
215
216         return 0;
217 }
218
219 /*
220  * Identify Touchpad
221  * See also the SYN_ID_* macros
222  */
223 static int synaptics_identify(struct psmouse *psmouse)
224 {
225         struct synaptics_data *priv = psmouse->private;
226         unsigned char id[3];
227
228         if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
229                 return -1;
230         priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
231         if (SYN_ID_IS_SYNAPTICS(priv->identity))
232                 return 0;
233         return -1;
234 }
235
236 /*
237  * Read touchpad resolution and maximum reported coordinates
238  * Resolution is left zero if touchpad does not support the query
239  */
240 static int synaptics_resolution(struct psmouse *psmouse)
241 {
242         struct synaptics_data *priv = psmouse->private;
243         unsigned char resp[3];
244
245         if (SYN_ID_MAJOR(priv->identity) < 4)
246                 return 0;
247
248         if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
249                 if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
250                         priv->x_res = resp[0]; /* x resolution in units/mm */
251                         priv->y_res = resp[2]; /* y resolution in units/mm */
252                 }
253         }
254
255         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
256             SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
257                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
258                         psmouse_warn(psmouse,
259                                      "device claims to have max coordinates query, but I'm not able to read it.\n");
260                 } else {
261                         priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
262                         priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
263                 }
264         }
265
266         if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
267             SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
268                 if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
269                         psmouse_warn(psmouse,
270                                      "device claims to have min coordinates query, but I'm not able to read it.\n");
271                 } else {
272                         priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
273                         priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
274                 }
275         }
276
277         return 0;
278 }
279
280 static int synaptics_query_hardware(struct psmouse *psmouse)
281 {
282         if (synaptics_identify(psmouse))
283                 return -1;
284         if (synaptics_model_id(psmouse))
285                 return -1;
286         if (synaptics_capability(psmouse))
287                 return -1;
288         if (synaptics_resolution(psmouse))
289                 return -1;
290
291         return 0;
292 }
293
294 static int synaptics_set_absolute_mode(struct psmouse *psmouse)
295 {
296         struct synaptics_data *priv = psmouse->private;
297
298         priv->mode = SYN_BIT_ABSOLUTE_MODE;
299         if (SYN_ID_MAJOR(priv->identity) >= 4)
300                 priv->mode |= SYN_BIT_DISABLE_GESTURE;
301         if (SYN_CAP_EXTENDED(priv->capabilities))
302                 priv->mode |= SYN_BIT_W_MODE;
303
304         if (synaptics_mode_cmd(psmouse, priv->mode))
305                 return -1;
306
307         return 0;
308 }
309
310 static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
311 {
312         struct synaptics_data *priv = psmouse->private;
313
314         if (rate >= 80) {
315                 priv->mode |= SYN_BIT_HIGH_RATE;
316                 psmouse->rate = 80;
317         } else {
318                 priv->mode &= ~SYN_BIT_HIGH_RATE;
319                 psmouse->rate = 40;
320         }
321
322         synaptics_mode_cmd(psmouse, priv->mode);
323 }
324
325 static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
326 {
327         static unsigned char param = 0xc8;
328         struct synaptics_data *priv = psmouse->private;
329
330         if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
331                         SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
332                 return 0;
333
334         if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
335                 return -1;
336         if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
337                 return -1;
338
339         /* Advanced gesture mode also sends multi finger data */
340         priv->capabilities |= BIT(1);
341
342         return 0;
343 }
344
345 /*****************************************************************************
346  *      Synaptics pass-through PS/2 port support
347  ****************************************************************************/
348 static int synaptics_pt_write(struct serio *serio, unsigned char c)
349 {
350         struct psmouse *parent = serio_get_drvdata(serio->parent);
351         char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
352
353         if (psmouse_sliced_command(parent, c))
354                 return -1;
355         if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
356                 return -1;
357         return 0;
358 }
359
360 static int synaptics_pt_start(struct serio *serio)
361 {
362         struct psmouse *parent = serio_get_drvdata(serio->parent);
363         struct synaptics_data *priv = parent->private;
364
365         serio_pause_rx(parent->ps2dev.serio);
366         priv->pt_port = serio;
367         serio_continue_rx(parent->ps2dev.serio);
368
369         return 0;
370 }
371
372 static void synaptics_pt_stop(struct serio *serio)
373 {
374         struct psmouse *parent = serio_get_drvdata(serio->parent);
375         struct synaptics_data *priv = parent->private;
376
377         serio_pause_rx(parent->ps2dev.serio);
378         priv->pt_port = NULL;
379         serio_continue_rx(parent->ps2dev.serio);
380 }
381
382 static int synaptics_is_pt_packet(unsigned char *buf)
383 {
384         return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
385 }
386
387 static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
388 {
389         struct psmouse *child = serio_get_drvdata(ptport);
390
391         if (child && child->state == PSMOUSE_ACTIVATED) {
392                 serio_interrupt(ptport, packet[1], 0);
393                 serio_interrupt(ptport, packet[4], 0);
394                 serio_interrupt(ptport, packet[5], 0);
395                 if (child->pktsize == 4)
396                         serio_interrupt(ptport, packet[2], 0);
397         } else
398                 serio_interrupt(ptport, packet[1], 0);
399 }
400
401 static void synaptics_pt_activate(struct psmouse *psmouse)
402 {
403         struct synaptics_data *priv = psmouse->private;
404         struct psmouse *child = serio_get_drvdata(priv->pt_port);
405
406         /* adjust the touchpad to child's choice of protocol */
407         if (child) {
408                 if (child->pktsize == 4)
409                         priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
410                 else
411                         priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
412
413                 if (synaptics_mode_cmd(psmouse, priv->mode))
414                         psmouse_warn(psmouse,
415                                      "failed to switch guest protocol\n");
416         }
417 }
418
419 static void synaptics_pt_create(struct psmouse *psmouse)
420 {
421         struct serio *serio;
422
423         serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
424         if (!serio) {
425                 psmouse_err(psmouse,
426                             "not enough memory for pass-through port\n");
427                 return;
428         }
429
430         serio->id.type = SERIO_PS_PSTHRU;
431         strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
432         strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
433         serio->write = synaptics_pt_write;
434         serio->start = synaptics_pt_start;
435         serio->stop = synaptics_pt_stop;
436         serio->parent = psmouse->ps2dev.serio;
437
438         psmouse->pt_activate = synaptics_pt_activate;
439
440         psmouse_info(psmouse, "serio: %s port at %s\n",
441                      serio->name, psmouse->phys);
442         serio_register_port(serio);
443 }
444
445 /*****************************************************************************
446  *      Functions to interpret the absolute mode packets
447  ****************************************************************************/
448
449 static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
450                                    int sgm, int agm)
451 {
452         state->count = count;
453         state->sgm = sgm;
454         state->agm = agm;
455 }
456
457 static void synaptics_parse_agm(const unsigned char buf[],
458                                 struct synaptics_data *priv,
459                                 struct synaptics_hw_state *hw)
460 {
461         struct synaptics_hw_state *agm = &priv->agm;
462         int agm_packet_type;
463
464         agm_packet_type = (buf[5] & 0x30) >> 4;
465         switch (agm_packet_type) {
466         case 1:
467                 /* Gesture packet: (x, y, z) half resolution */
468                 agm->w = hw->w;
469                 agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
470                 agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
471                 agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
472                 break;
473
474         case 2:
475                 /* AGM-CONTACT packet: (count, sgm, agm) */
476                 synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
477                 break;
478
479         default:
480                 break;
481         }
482
483         /* Record that at least one AGM has been received since last SGM */
484         priv->agm_pending = true;
485 }
486
487 static int synaptics_parse_hw_state(const unsigned char buf[],
488                                     struct synaptics_data *priv,
489                                     struct synaptics_hw_state *hw)
490 {
491         memset(hw, 0, sizeof(struct synaptics_hw_state));
492
493         if (SYN_MODEL_NEWABS(priv->model_id)) {
494                 hw->w = (((buf[0] & 0x30) >> 2) |
495                          ((buf[0] & 0x04) >> 1) |
496                          ((buf[3] & 0x04) >> 2));
497
498                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
499                 hw->right = (buf[0] & 0x02) ? 1 : 0;
500
501                 if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
502                         /*
503                          * Clickpad's button is transmitted as middle button,
504                          * however, since it is primary button, we will report
505                          * it as BTN_LEFT.
506                          */
507                         hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
508
509                 } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
510                         hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
511                         if (hw->w == 2)
512                                 hw->scroll = (signed char)(buf[1]);
513                 }
514
515                 if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
516                         hw->up   = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
517                         hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
518                 }
519
520                 if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
521                         SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
522                     hw->w == 2) {
523                         synaptics_parse_agm(buf, priv, hw);
524                         return 1;
525                 }
526
527                 hw->x = (((buf[3] & 0x10) << 8) |
528                          ((buf[1] & 0x0f) << 8) |
529                          buf[4]);
530                 hw->y = (((buf[3] & 0x20) << 7) |
531                          ((buf[1] & 0xf0) << 4) |
532                          buf[5]);
533                 hw->z = buf[2];
534
535                 if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
536                     ((buf[0] ^ buf[3]) & 0x02)) {
537                         switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
538                         default:
539                                 /*
540                                  * if nExtBtn is greater than 8 it should be
541                                  * considered invalid and treated as 0
542                                  */
543                                 break;
544                         case 8:
545                                 hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
546                                 hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
547                         case 6:
548                                 hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
549                                 hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
550                         case 4:
551                                 hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
552                                 hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
553                         case 2:
554                                 hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
555                                 hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
556                         }
557                 }
558         } else {
559                 hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
560                 hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
561
562                 hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
563                 hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
564
565                 hw->left  = (buf[0] & 0x01) ? 1 : 0;
566                 hw->right = (buf[0] & 0x02) ? 1 : 0;
567         }
568
569         /*
570          * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
571          * is used by some firmware to indicate a finger at the edge of
572          * the touchpad whose precise position cannot be determined, so
573          * convert these values to the maximum axis value.
574          */
575         if (hw->x > X_MAX_POSITIVE)
576                 hw->x -= 1 << ABS_POS_BITS;
577         else if (hw->x == X_MAX_POSITIVE)
578                 hw->x = XMAX;
579
580         if (hw->y > Y_MAX_POSITIVE)
581                 hw->y -= 1 << ABS_POS_BITS;
582         else if (hw->y == Y_MAX_POSITIVE)
583                 hw->y = YMAX;
584
585         return 0;
586 }
587
588 static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
589                                           bool active, int x, int y)
590 {
591         input_mt_slot(dev, slot);
592         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
593         if (active) {
594                 input_report_abs(dev, ABS_MT_POSITION_X, x);
595                 input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
596         }
597 }
598
599 static void synaptics_report_semi_mt_data(struct input_dev *dev,
600                                           const struct synaptics_hw_state *a,
601                                           const struct synaptics_hw_state *b,
602                                           int num_fingers)
603 {
604         if (num_fingers >= 2) {
605                 synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
606                                               min(a->y, b->y));
607                 synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
608                                               max(a->y, b->y));
609         } else if (num_fingers == 1) {
610                 synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
611                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
612         } else {
613                 synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
614                 synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
615         }
616 }
617
618 static void synaptics_report_buttons(struct psmouse *psmouse,
619                                      const struct synaptics_hw_state *hw)
620 {
621         struct input_dev *dev = psmouse->dev;
622         struct synaptics_data *priv = psmouse->private;
623         int i;
624
625         input_report_key(dev, BTN_LEFT, hw->left);
626         input_report_key(dev, BTN_RIGHT, hw->right);
627
628         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
629                 input_report_key(dev, BTN_MIDDLE, hw->middle);
630
631         if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
632                 input_report_key(dev, BTN_FORWARD, hw->up);
633                 input_report_key(dev, BTN_BACK, hw->down);
634         }
635
636         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
637                 input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
638 }
639
640 static void synaptics_report_slot(struct input_dev *dev, int slot,
641                                   const struct synaptics_hw_state *hw)
642 {
643         input_mt_slot(dev, slot);
644         input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
645         if (!hw)
646                 return;
647
648         input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
649         input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
650         input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
651 }
652
653 static void synaptics_report_mt_data(struct psmouse *psmouse,
654                                      struct synaptics_mt_state *mt_state,
655                                      const struct synaptics_hw_state *sgm)
656 {
657         struct input_dev *dev = psmouse->dev;
658         struct synaptics_data *priv = psmouse->private;
659         struct synaptics_hw_state *agm = &priv->agm;
660         struct synaptics_mt_state *old = &priv->mt_state;
661
662         switch (mt_state->count) {
663         case 0:
664                 synaptics_report_slot(dev, 0, NULL);
665                 synaptics_report_slot(dev, 1, NULL);
666                 break;
667         case 1:
668                 if (mt_state->sgm == -1) {
669                         synaptics_report_slot(dev, 0, NULL);
670                         synaptics_report_slot(dev, 1, NULL);
671                 } else if (mt_state->sgm == 0) {
672                         synaptics_report_slot(dev, 0, sgm);
673                         synaptics_report_slot(dev, 1, NULL);
674                 } else {
675                         synaptics_report_slot(dev, 0, NULL);
676                         synaptics_report_slot(dev, 1, sgm);
677                 }
678                 break;
679         default:
680                 /*
681                  * If the finger slot contained in SGM is valid, and either
682                  * hasn't changed, or is new, then report SGM in MTB slot 0.
683                  * Otherwise, empty MTB slot 0.
684                  */
685                 if (mt_state->sgm != -1 &&
686                     (mt_state->sgm == old->sgm || old->sgm == -1))
687                         synaptics_report_slot(dev, 0, sgm);
688                 else
689                         synaptics_report_slot(dev, 0, NULL);
690
691                 /*
692                  * If the finger slot contained in AGM is valid, and either
693                  * hasn't changed, or is new, then report AGM in MTB slot 1.
694                  * Otherwise, empty MTB slot 1.
695                  */
696                 if (mt_state->agm != -1 &&
697                     (mt_state->agm == old->agm || old->agm == -1))
698                         synaptics_report_slot(dev, 1, agm);
699                 else
700                         synaptics_report_slot(dev, 1, NULL);
701                 break;
702         }
703
704         /* Don't use active slot count to generate BTN_TOOL events. */
705         input_mt_report_pointer_emulation(dev, false);
706
707         /* Send the number of fingers reported by touchpad itself. */
708         input_mt_report_finger_count(dev, mt_state->count);
709
710         synaptics_report_buttons(psmouse, sgm);
711
712         input_sync(dev);
713 }
714
715 /* Handle case where mt_state->count = 0 */
716 static void synaptics_image_sensor_0f(struct synaptics_data *priv,
717                                       struct synaptics_mt_state *mt_state)
718 {
719         synaptics_mt_state_set(mt_state, 0, -1, -1);
720         priv->mt_state_lost = false;
721 }
722
723 /* Handle case where mt_state->count = 1 */
724 static void synaptics_image_sensor_1f(struct synaptics_data *priv,
725                                       struct synaptics_mt_state *mt_state)
726 {
727         struct synaptics_hw_state *agm = &priv->agm;
728         struct synaptics_mt_state *old = &priv->mt_state;
729
730         /*
731          * If the last AGM was (0,0,0), and there is only one finger left,
732          * then we absolutely know that SGM contains slot 0, and all other
733          * fingers have been removed.
734          */
735         if (priv->agm_pending && agm->z == 0) {
736                 synaptics_mt_state_set(mt_state, 1, 0, -1);
737                 priv->mt_state_lost = false;
738                 return;
739         }
740
741         switch (old->count) {
742         case 0:
743                 synaptics_mt_state_set(mt_state, 1, 0, -1);
744                 break;
745         case 1:
746                 /*
747                  * If mt_state_lost, then the previous transition was 3->1,
748                  * and SGM now contains either slot 0 or 1, but we don't know
749                  * which.  So, we just assume that the SGM now contains slot 1.
750                  *
751                  * If pending AGM and either:
752                  *   (a) the previous SGM slot contains slot 0, or
753                  *   (b) there was no SGM slot
754                  * then, the SGM now contains slot 1
755                  *
756                  * Case (a) happens with very rapid "drum roll" gestures, where
757                  * slot 0 finger is lifted and a new slot 1 finger touches
758                  * within one reporting interval.
759                  *
760                  * Case (b) happens if initially two or more fingers tap
761                  * briefly, and all but one lift before the end of the first
762                  * reporting interval.
763                  *
764                  * (In both these cases, slot 0 will becomes empty, so SGM
765                  * contains slot 1 with the new finger)
766                  *
767                  * Else, if there was no previous SGM, it now contains slot 0.
768                  *
769                  * Otherwise, SGM still contains the same slot.
770                  */
771                 if (priv->mt_state_lost ||
772                     (priv->agm_pending && old->sgm <= 0))
773                         synaptics_mt_state_set(mt_state, 1, 1, -1);
774                 else if (old->sgm == -1)
775                         synaptics_mt_state_set(mt_state, 1, 0, -1);
776                 break;
777         case 2:
778                 /*
779                  * If mt_state_lost, we don't know which finger SGM contains.
780                  *
781                  * So, report 1 finger, but with both slots empty.
782                  * We will use slot 1 on subsequent 1->1
783                  */
784                 if (priv->mt_state_lost) {
785                         synaptics_mt_state_set(mt_state, 1, -1, -1);
786                         break;
787                 }
788                 /*
789                  * Since the last AGM was NOT (0,0,0), it was the finger in
790                  * slot 0 that has been removed.
791                  * So, SGM now contains previous AGM's slot, and AGM is now
792                  * empty.
793                  */
794                 synaptics_mt_state_set(mt_state, 1, old->agm, -1);
795                 break;
796         case 3:
797                 /*
798                  * Since last AGM was not (0,0,0), we don't know which finger
799                  * is left.
800                  *
801                  * So, report 1 finger, but with both slots empty.
802                  * We will use slot 1 on subsequent 1->1
803                  */
804                 synaptics_mt_state_set(mt_state, 1, -1, -1);
805                 priv->mt_state_lost = true;
806                 break;
807         case 4:
808         case 5:
809                 /* mt_state was updated by AGM-CONTACT packet */
810                 break;
811         }
812 }
813
814 /* Handle case where mt_state->count = 2 */
815 static void synaptics_image_sensor_2f(struct synaptics_data *priv,
816                                       struct synaptics_mt_state *mt_state)
817 {
818         struct synaptics_mt_state *old = &priv->mt_state;
819
820         switch (old->count) {
821         case 0:
822                 synaptics_mt_state_set(mt_state, 2, 0, 1);
823                 break;
824         case 1:
825                 /*
826                  * If previous SGM contained slot 1 or higher, SGM now contains
827                  * slot 0 (the newly touching finger) and AGM contains SGM's
828                  * previous slot.
829                  *
830                  * Otherwise, SGM still contains slot 0 and AGM now contains
831                  * slot 1.
832                  */
833                 if (old->sgm >= 1)
834                         synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
835                 else
836                         synaptics_mt_state_set(mt_state, 2, 0, 1);
837                 break;
838         case 2:
839                 /*
840                  * If mt_state_lost, SGM now contains either finger 1 or 2, but
841                  * we don't know which.
842                  * So, we just assume that the SGM contains slot 0 and AGM 1.
843                  */
844                 if (priv->mt_state_lost)
845                         synaptics_mt_state_set(mt_state, 2, 0, 1);
846                 /*
847                  * Otherwise, use the same mt_state, since it either hasn't
848                  * changed, or was updated by a recently received AGM-CONTACT
849                  * packet.
850                  */
851                 break;
852         case 3:
853                 /*
854                  * 3->2 transitions have two unsolvable problems:
855                  *  1) no indication is given which finger was removed
856                  *  2) no way to tell if agm packet was for finger 3
857                  *     before 3->2, or finger 2 after 3->2.
858                  *
859                  * So, report 2 fingers, but empty all slots.
860                  * We will guess slots [0,1] on subsequent 2->2.
861                  */
862                 synaptics_mt_state_set(mt_state, 2, -1, -1);
863                 priv->mt_state_lost = true;
864                 break;
865         case 4:
866         case 5:
867                 /* mt_state was updated by AGM-CONTACT packet */
868                 break;
869         }
870 }
871
872 /* Handle case where mt_state->count = 3 */
873 static void synaptics_image_sensor_3f(struct synaptics_data *priv,
874                                       struct synaptics_mt_state *mt_state)
875 {
876         struct synaptics_mt_state *old = &priv->mt_state;
877
878         switch (old->count) {
879         case 0:
880                 synaptics_mt_state_set(mt_state, 3, 0, 2);
881                 break;
882         case 1:
883                 /*
884                  * If previous SGM contained slot 2 or higher, SGM now contains
885                  * slot 0 (one of the newly touching fingers) and AGM contains
886                  * SGM's previous slot.
887                  *
888                  * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
889                  */
890                 if (old->sgm >= 2)
891                         synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
892                 else
893                         synaptics_mt_state_set(mt_state, 3, 0, 2);
894                 break;
895         case 2:
896                 /*
897                  * If the AGM previously contained slot 3 or higher, then the
898                  * newly touching finger is in the lowest available slot.
899                  *
900                  * If SGM was previously 1 or higher, then the new SGM is
901                  * now slot 0 (with a new finger), otherwise, the new finger
902                  * is now in a hidden slot between 0 and AGM's slot.
903                  *
904                  * In all such cases, the SGM now contains slot 0, and the AGM
905                  * continues to contain the same slot as before.
906                  */
907                 if (old->agm >= 3) {
908                         synaptics_mt_state_set(mt_state, 3, 0, old->agm);
909                         break;
910                 }
911
912                 /*
913                  * After some 3->1 and all 3->2 transitions, we lose track
914                  * of which slot is reported by SGM and AGM.
915                  *
916                  * For 2->3 in this state, report 3 fingers, but empty all
917                  * slots, and we will guess (0,2) on a subsequent 0->3.
918                  *
919                  * To userspace, the resulting transition will look like:
920                  *    2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
921                  */
922                 if (priv->mt_state_lost) {
923                         synaptics_mt_state_set(mt_state, 3, -1, -1);
924                         break;
925                 }
926
927                 /*
928                  * If the (SGM,AGM) really previously contained slots (0, 1),
929                  * then we cannot know what slot was just reported by the AGM,
930                  * because the 2->3 transition can occur either before or after
931                  * the AGM packet. Thus, this most recent AGM could contain
932                  * either the same old slot 1 or the new slot 2.
933                  * Subsequent AGMs will be reporting slot 2.
934                  *
935                  * To userspace, the resulting transition will look like:
936                  *    2:[0,1] -> 3:[0,-1] -> 3:[0,2]
937                  */
938                 synaptics_mt_state_set(mt_state, 3, 0, -1);
939                 break;
940         case 3:
941                 /*
942                  * If, for whatever reason, the previous agm was invalid,
943                  * Assume SGM now contains slot 0, AGM now contains slot 2.
944                  */
945                 if (old->agm <= 2)
946                         synaptics_mt_state_set(mt_state, 3, 0, 2);
947                 /*
948                  * mt_state either hasn't changed, or was updated by a recently
949                  * received AGM-CONTACT packet.
950                  */
951                 break;
952
953         case 4:
954         case 5:
955                 /* mt_state was updated by AGM-CONTACT packet */
956                 break;
957         }
958 }
959
960 /* Handle case where mt_state->count = 4, or = 5 */
961 static void synaptics_image_sensor_45f(struct synaptics_data *priv,
962                                        struct synaptics_mt_state *mt_state)
963 {
964         /* mt_state was updated correctly by AGM-CONTACT packet */
965         priv->mt_state_lost = false;
966 }
967
968 static void synaptics_image_sensor_process(struct psmouse *psmouse,
969                                            struct synaptics_hw_state *sgm)
970 {
971         struct synaptics_data *priv = psmouse->private;
972         struct synaptics_hw_state *agm = &priv->agm;
973         struct synaptics_mt_state mt_state;
974
975         /* Initialize using current mt_state (as updated by last agm) */
976         mt_state = agm->mt_state;
977
978         /*
979          * Update mt_state using the new finger count and current mt_state.
980          */
981         if (sgm->z == 0)
982                 synaptics_image_sensor_0f(priv, &mt_state);
983         else if (sgm->w >= 4)
984                 synaptics_image_sensor_1f(priv, &mt_state);
985         else if (sgm->w == 0)
986                 synaptics_image_sensor_2f(priv, &mt_state);
987         else if (sgm->w == 1 && mt_state.count <= 3)
988                 synaptics_image_sensor_3f(priv, &mt_state);
989         else
990                 synaptics_image_sensor_45f(priv, &mt_state);
991
992         /* Send resulting input events to user space */
993         synaptics_report_mt_data(psmouse, &mt_state, sgm);
994
995         /* Store updated mt_state */
996         priv->mt_state = agm->mt_state = mt_state;
997         priv->agm_pending = false;
998 }
999
1000 /*
1001  *  called for each full received packet from the touchpad
1002  */
1003 static void synaptics_process_packet(struct psmouse *psmouse)
1004 {
1005         struct input_dev *dev = psmouse->dev;
1006         struct synaptics_data *priv = psmouse->private;
1007         struct synaptics_hw_state hw;
1008         int num_fingers;
1009         int finger_width;
1010
1011         if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
1012                 return;
1013
1014         if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1015                 synaptics_image_sensor_process(psmouse, &hw);
1016                 return;
1017         }
1018
1019         if (hw.scroll) {
1020                 priv->scroll += hw.scroll;
1021
1022                 while (priv->scroll >= 4) {
1023                         input_report_key(dev, BTN_BACK, !hw.down);
1024                         input_sync(dev);
1025                         input_report_key(dev, BTN_BACK, hw.down);
1026                         input_sync(dev);
1027                         priv->scroll -= 4;
1028                 }
1029                 while (priv->scroll <= -4) {
1030                         input_report_key(dev, BTN_FORWARD, !hw.up);
1031                         input_sync(dev);
1032                         input_report_key(dev, BTN_FORWARD, hw.up);
1033                         input_sync(dev);
1034                         priv->scroll += 4;
1035                 }
1036                 return;
1037         }
1038
1039         if (hw.z > 0 && hw.x > 1) {
1040                 num_fingers = 1;
1041                 finger_width = 5;
1042                 if (SYN_CAP_EXTENDED(priv->capabilities)) {
1043                         switch (hw.w) {
1044                         case 0 ... 1:
1045                                 if (SYN_CAP_MULTIFINGER(priv->capabilities))
1046                                         num_fingers = hw.w + 2;
1047                                 break;
1048                         case 2:
1049                                 if (SYN_MODEL_PEN(priv->model_id))
1050                                         ;   /* Nothing, treat a pen as a single finger */
1051                                 break;
1052                         case 4 ... 15:
1053                                 if (SYN_CAP_PALMDETECT(priv->capabilities))
1054                                         finger_width = hw.w;
1055                                 break;
1056                         }
1057                 }
1058         } else {
1059                 num_fingers = 0;
1060                 finger_width = 0;
1061         }
1062
1063         if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
1064                 synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
1065                                               num_fingers);
1066
1067         /* Post events
1068          * BTN_TOUCH has to be first as mousedev relies on it when doing
1069          * absolute -> relative conversion
1070          */
1071         if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
1072         if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
1073
1074         if (num_fingers > 0) {
1075                 input_report_abs(dev, ABS_X, hw.x);
1076                 input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
1077         }
1078         input_report_abs(dev, ABS_PRESSURE, hw.z);
1079
1080         if (SYN_CAP_PALMDETECT(priv->capabilities))
1081                 input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
1082
1083         input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
1084         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1085                 input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
1086                 input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
1087         }
1088
1089         synaptics_report_buttons(psmouse, &hw);
1090
1091         input_sync(dev);
1092 }
1093
1094 static int synaptics_validate_byte(struct psmouse *psmouse,
1095                                    int idx, unsigned char pkt_type)
1096 {
1097         static const unsigned char newabs_mask[]        = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
1098         static const unsigned char newabs_rel_mask[]    = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
1099         static const unsigned char newabs_rslt[]        = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
1100         static const unsigned char oldabs_mask[]        = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
1101         static const unsigned char oldabs_rslt[]        = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
1102         const char *packet = psmouse->packet;
1103
1104         if (idx < 0 || idx > 4)
1105                 return 0;
1106
1107         switch (pkt_type) {
1108
1109         case SYN_NEWABS:
1110         case SYN_NEWABS_RELAXED:
1111                 return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
1112
1113         case SYN_NEWABS_STRICT:
1114                 return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
1115
1116         case SYN_OLDABS:
1117                 return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
1118
1119         default:
1120                 psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
1121                 return 0;
1122         }
1123 }
1124
1125 static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
1126 {
1127         int i;
1128
1129         for (i = 0; i < 5; i++)
1130                 if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
1131                         psmouse_info(psmouse, "using relaxed packet validation\n");
1132                         return SYN_NEWABS_RELAXED;
1133                 }
1134
1135         return SYN_NEWABS_STRICT;
1136 }
1137
1138 static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
1139 {
1140         struct synaptics_data *priv = psmouse->private;
1141
1142         if (psmouse->pktcnt >= 6) { /* Full packet received */
1143                 if (unlikely(priv->pkt_type == SYN_NEWABS))
1144                         priv->pkt_type = synaptics_detect_pkt_type(psmouse);
1145
1146                 if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
1147                     synaptics_is_pt_packet(psmouse->packet)) {
1148                         if (priv->pt_port)
1149                                 synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
1150                 } else
1151                         synaptics_process_packet(psmouse);
1152
1153                 return PSMOUSE_FULL_PACKET;
1154         }
1155
1156         return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
1157                 PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
1158 }
1159
1160 /*****************************************************************************
1161  *      Driver initialization/cleanup functions
1162  ****************************************************************************/
1163 static void set_abs_position_params(struct input_dev *dev,
1164                                     struct synaptics_data *priv, int x_code,
1165                                     int y_code)
1166 {
1167         int x_min = priv->x_min ?: XMIN_NOMINAL;
1168         int x_max = priv->x_max ?: XMAX_NOMINAL;
1169         int y_min = priv->y_min ?: YMIN_NOMINAL;
1170         int y_max = priv->y_max ?: YMAX_NOMINAL;
1171         int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
1172                         SYN_REDUCED_FILTER_FUZZ : 0;
1173
1174         input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
1175         input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
1176         input_abs_set_res(dev, x_code, priv->x_res);
1177         input_abs_set_res(dev, y_code, priv->y_res);
1178 }
1179
1180 static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
1181 {
1182         int i;
1183
1184         __set_bit(INPUT_PROP_POINTER, dev->propbit);
1185
1186         __set_bit(EV_ABS, dev->evbit);
1187         set_abs_position_params(dev, priv, ABS_X, ABS_Y);
1188         input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
1189
1190         if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
1191                 input_mt_init_slots(dev, 2);
1192                 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1193                                         ABS_MT_POSITION_Y);
1194                 /* Image sensors can report per-contact pressure */
1195                 input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
1196
1197                 /* Image sensors can signal 4 and 5 finger clicks */
1198                 __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
1199                 __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
1200         } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
1201                 /* Non-image sensors with AGM use semi-mt */
1202                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
1203                 input_mt_init_slots(dev, 2);
1204                 set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
1205                                         ABS_MT_POSITION_Y);
1206         }
1207
1208         if (SYN_CAP_PALMDETECT(priv->capabilities))
1209                 input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
1210
1211         __set_bit(EV_KEY, dev->evbit);
1212         __set_bit(BTN_TOUCH, dev->keybit);
1213         __set_bit(BTN_TOOL_FINGER, dev->keybit);
1214         __set_bit(BTN_LEFT, dev->keybit);
1215         __set_bit(BTN_RIGHT, dev->keybit);
1216
1217         if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
1218                 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
1219                 __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
1220         }
1221
1222         if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
1223                 __set_bit(BTN_MIDDLE, dev->keybit);
1224
1225         if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
1226             SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
1227                 __set_bit(BTN_FORWARD, dev->keybit);
1228                 __set_bit(BTN_BACK, dev->keybit);
1229         }
1230
1231         for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
1232                 __set_bit(BTN_0 + i, dev->keybit);
1233
1234         __clear_bit(EV_REL, dev->evbit);
1235         __clear_bit(REL_X, dev->relbit);
1236         __clear_bit(REL_Y, dev->relbit);
1237
1238         if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
1239                 __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
1240                 /* Clickpads report only left button */
1241                 __clear_bit(BTN_RIGHT, dev->keybit);
1242                 __clear_bit(BTN_MIDDLE, dev->keybit);
1243         }
1244 }
1245
1246 static void synaptics_disconnect(struct psmouse *psmouse)
1247 {
1248         synaptics_reset(psmouse);
1249         kfree(psmouse->private);
1250         psmouse->private = NULL;
1251 }
1252
1253 static int synaptics_reconnect(struct psmouse *psmouse)
1254 {
1255         struct synaptics_data *priv = psmouse->private;
1256         struct synaptics_data old_priv = *priv;
1257         int retry = 0;
1258         int error;
1259
1260         do {
1261                 psmouse_reset(psmouse);
1262                 if (retry) {
1263                         /*
1264                          * On some boxes, right after resuming, the touchpad
1265                          * needs some time to finish initializing (I assume
1266                          * it needs time to calibrate) and start responding
1267                          * to Synaptics-specific queries, so let's wait a
1268                          * bit.
1269                          */
1270                         ssleep(1);
1271                 }
1272                 error = synaptics_detect(psmouse, 0);
1273         } while (error && ++retry < 3);
1274
1275         if (error)
1276                 return -1;
1277
1278         if (retry > 1)
1279                 psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
1280
1281         if (synaptics_query_hardware(psmouse)) {
1282                 psmouse_err(psmouse, "Unable to query device.\n");
1283                 return -1;
1284         }
1285
1286         if (synaptics_set_absolute_mode(psmouse)) {
1287                 psmouse_err(psmouse, "Unable to initialize device.\n");
1288                 return -1;
1289         }
1290
1291         if (synaptics_set_advanced_gesture_mode(psmouse)) {
1292                 psmouse_err(psmouse,
1293                             "Advanced gesture mode reconnect failed.\n");
1294                 return -1;
1295         }
1296
1297         if (old_priv.identity != priv->identity ||
1298             old_priv.model_id != priv->model_id ||
1299             old_priv.capabilities != priv->capabilities ||
1300             old_priv.ext_cap != priv->ext_cap) {
1301                 psmouse_err(psmouse,
1302                             "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
1303                             old_priv.identity, priv->identity,
1304                             old_priv.model_id, priv->model_id,
1305                             old_priv.capabilities, priv->capabilities,
1306                             old_priv.ext_cap, priv->ext_cap);
1307                 return -1;
1308         }
1309
1310         return 0;
1311 }
1312
1313 static bool impaired_toshiba_kbc;
1314
1315 static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
1316 #if defined(CONFIG_DMI) && defined(CONFIG_X86)
1317         {
1318                 /* Toshiba Satellite */
1319                 .matches = {
1320                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1321                         DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
1322                 },
1323         },
1324         {
1325                 /* Toshiba Dynabook */
1326                 .matches = {
1327                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1328                         DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
1329                 },
1330         },
1331         {
1332                 /* Toshiba Portege M300 */
1333                 .matches = {
1334                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1335                         DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
1336                 },
1337
1338         },
1339         {
1340                 /* Toshiba Portege M300 */
1341                 .matches = {
1342                         DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
1343                         DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
1344                         DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
1345                 },
1346
1347         },
1348 #endif
1349         { }
1350 };
1351
1352 static bool broken_olpc_ec;
1353
1354 static const struct dmi_system_id __initconst olpc_dmi_table[] = {
1355 #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
1356         {
1357                 /* OLPC XO-1 or XO-1.5 */
1358                 .matches = {
1359                         DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
1360                         DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
1361                 },
1362         },
1363 #endif
1364         { }
1365 };
1366
1367 void __init synaptics_module_init(void)
1368 {
1369         impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
1370         broken_olpc_ec = dmi_check_system(olpc_dmi_table);
1371 }
1372
1373 int synaptics_init(struct psmouse *psmouse)
1374 {
1375         struct synaptics_data *priv;
1376
1377         /*
1378          * The OLPC XO has issues with Synaptics' absolute mode; similarly to
1379          * the HGPK, it quickly degrades and the hardware becomes jumpy and
1380          * overly sensitive.  Not only that, but the constant packet spew
1381          * (even at a lowered 40pps rate) overloads the EC such that key
1382          * presses on the keyboard are missed.  Given all of that, don't
1383          * even attempt to use Synaptics mode.  Relative mode seems to work
1384          * just fine.
1385          */
1386         if (broken_olpc_ec) {
1387                 psmouse_info(psmouse,
1388                              "OLPC XO detected, not enabling Synaptics protocol.\n");
1389                 return -ENODEV;
1390         }
1391
1392         psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
1393         if (!priv)
1394                 return -ENOMEM;
1395
1396         psmouse_reset(psmouse);
1397
1398         if (synaptics_query_hardware(psmouse)) {
1399                 psmouse_err(psmouse, "Unable to query device.\n");
1400                 goto init_fail;
1401         }
1402
1403         if (synaptics_set_absolute_mode(psmouse)) {
1404                 psmouse_err(psmouse, "Unable to initialize device.\n");
1405                 goto init_fail;
1406         }
1407
1408         if (synaptics_set_advanced_gesture_mode(psmouse)) {
1409                 psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
1410                 goto init_fail;
1411         }
1412
1413         priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
1414
1415         psmouse_info(psmouse,
1416                      "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
1417                      SYN_ID_MODEL(priv->identity),
1418                      SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
1419                      priv->model_id,
1420                      priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
1421
1422         set_input_params(psmouse->dev, priv);
1423
1424         /*
1425          * Encode touchpad model so that it can be used to set
1426          * input device->id.version and be visible to userspace.
1427          * Because version is __u16 we have to drop something.
1428          * Hardware info bits seem to be good candidates as they
1429          * are documented to be for Synaptics corp. internal use.
1430          */
1431         psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
1432                           (priv->model_id & 0x000000ff);
1433
1434         psmouse->protocol_handler = synaptics_process_byte;
1435         psmouse->set_rate = synaptics_set_rate;
1436         psmouse->disconnect = synaptics_disconnect;
1437         psmouse->reconnect = synaptics_reconnect;
1438         psmouse->cleanup = synaptics_reset;
1439         psmouse->pktsize = 6;
1440         /* Synaptics can usually stay in sync without extra help */
1441         psmouse->resync_time = 0;
1442
1443         if (SYN_CAP_PASS_THROUGH(priv->capabilities))
1444                 synaptics_pt_create(psmouse);
1445
1446         /*
1447          * Toshiba's KBC seems to have trouble handling data from
1448          * Synaptics at full rate.  Switch to a lower rate (roughly
1449          * the same rate as a standard PS/2 mouse).
1450          */
1451         if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
1452                 psmouse_info(psmouse,
1453                              "Toshiba %s detected, limiting rate to 40pps.\n",
1454                              dmi_get_system_info(DMI_PRODUCT_NAME));
1455                 psmouse->rate = 40;
1456         }
1457
1458         return 0;
1459
1460  init_fail:
1461         kfree(priv);
1462         return -1;
1463 }
1464
1465 bool synaptics_supported(void)
1466 {
1467         return true;
1468 }
1469
1470 #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
1471
1472 void __init synaptics_module_init(void)
1473 {
1474 }
1475
1476 int synaptics_init(struct psmouse *psmouse)
1477 {
1478         return -ENOSYS;
1479 }
1480
1481 bool synaptics_supported(void)
1482 {
1483         return false;
1484 }
1485
1486 #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */