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