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