Input: sentelic - improve packet debugging information
[linux-flexiantxendom0.git] / drivers / input / mouse / sentelic.c
1 /*-
2  * Finger Sensing Pad PS/2 mouse driver.
3  *
4  * Copyright (C) 2005-2007 Asia Vital Components Co., Ltd.
5  * Copyright (C) 2005-2012 Tai-hwa Liang, Sentelic Corporation.
6  *
7  *   This program is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU General Public License
9  *   as published by the Free Software Foundation; either version 2
10  *   of the License, or (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <linux/input/mt.h>
25 #include <linux/ctype.h>
26 #include <linux/libps2.h>
27 #include <linux/serio.h>
28 #include <linux/jiffies.h>
29 #include <linux/slab.h>
30
31 #include "psmouse.h"
32 #include "sentelic.h"
33
34 /*
35  * Timeout for FSP PS/2 command only (in milliseconds).
36  */
37 #define FSP_CMD_TIMEOUT         200
38 #define FSP_CMD_TIMEOUT2        30
39
40 #define GET_ABS_X(packet)       ((packet[1] << 2) | ((packet[3] >> 2) & 0x03))
41 #define GET_ABS_Y(packet)       ((packet[2] << 2) | (packet[3] & 0x03))
42
43 /** Driver version. */
44 static const char fsp_drv_ver[] = "1.0.0-K";
45
46 /*
47  * Make sure that the value being sent to FSP will not conflict with
48  * possible sample rate values.
49  */
50 static unsigned char fsp_test_swap_cmd(unsigned char reg_val)
51 {
52         switch (reg_val) {
53         case 10: case 20: case 40: case 60: case 80: case 100: case 200:
54                 /*
55                  * The requested value being sent to FSP matched to possible
56                  * sample rates, swap the given value such that the hardware
57                  * wouldn't get confused.
58                  */
59                 return (reg_val >> 4) | (reg_val << 4);
60         default:
61                 return reg_val; /* swap isn't necessary */
62         }
63 }
64
65 /*
66  * Make sure that the value being sent to FSP will not conflict with certain
67  * commands.
68  */
69 static unsigned char fsp_test_invert_cmd(unsigned char reg_val)
70 {
71         switch (reg_val) {
72         case 0xe9: case 0xee: case 0xf2: case 0xff:
73                 /*
74                  * The requested value being sent to FSP matched to certain
75                  * commands, inverse the given value such that the hardware
76                  * wouldn't get confused.
77                  */
78                 return ~reg_val;
79         default:
80                 return reg_val; /* inversion isn't necessary */
81         }
82 }
83
84 static int fsp_reg_read(struct psmouse *psmouse, int reg_addr, int *reg_val)
85 {
86         struct ps2dev *ps2dev = &psmouse->ps2dev;
87         unsigned char param[3];
88         unsigned char addr;
89         int rc = -1;
90
91         /*
92          * We need to shut off the device and switch it into command
93          * mode so we don't confuse our protocol handler. We don't need
94          * to do that for writes because sysfs set helper does this for
95          * us.
96          */
97         psmouse_deactivate(psmouse);
98
99         ps2_begin_command(ps2dev);
100
101         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
102                 goto out;
103
104         /* should return 0xfe(request for resending) */
105         ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
106         /* should return 0xfc(failed) */
107         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
108
109         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
110                 goto out;
111
112         if ((addr = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
113                 ps2_sendbyte(ps2dev, 0x68, FSP_CMD_TIMEOUT2);
114         } else if ((addr = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
115                 /* swapping is required */
116                 ps2_sendbyte(ps2dev, 0xcc, FSP_CMD_TIMEOUT2);
117                 /* expect 0xfe */
118         } else {
119                 /* swapping isn't necessary */
120                 ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
121                 /* expect 0xfe */
122         }
123         /* should return 0xfc(failed) */
124         ps2_sendbyte(ps2dev, addr, FSP_CMD_TIMEOUT);
125
126         if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) < 0)
127                 goto out;
128
129         *reg_val = param[2];
130         rc = 0;
131
132  out:
133         ps2_end_command(ps2dev);
134         psmouse_activate(psmouse);
135         psmouse_dbg(psmouse,
136                     "READ REG: 0x%02x is 0x%02x (rc = %d)\n",
137                     reg_addr, *reg_val, rc);
138         return rc;
139 }
140
141 static int fsp_reg_write(struct psmouse *psmouse, int reg_addr, int reg_val)
142 {
143         struct ps2dev *ps2dev = &psmouse->ps2dev;
144         unsigned char v;
145         int rc = -1;
146
147         ps2_begin_command(ps2dev);
148
149         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
150                 goto out;
151
152         if ((v = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
153                 /* inversion is required */
154                 ps2_sendbyte(ps2dev, 0x74, FSP_CMD_TIMEOUT2);
155         } else {
156                 if ((v = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
157                         /* swapping is required */
158                         ps2_sendbyte(ps2dev, 0x77, FSP_CMD_TIMEOUT2);
159                 } else {
160                         /* swapping isn't necessary */
161                         ps2_sendbyte(ps2dev, 0x55, FSP_CMD_TIMEOUT2);
162                 }
163         }
164         /* write the register address in correct order */
165         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
166
167         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
168                 goto out;
169
170         if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
171                 /* inversion is required */
172                 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
173         } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
174                 /* swapping is required */
175                 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
176         } else {
177                 /* swapping isn't necessary */
178                 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
179         }
180
181         /* write the register value in correct order */
182         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
183         rc = 0;
184
185  out:
186         ps2_end_command(ps2dev);
187         psmouse_dbg(psmouse,
188                     "WRITE REG: 0x%02x to 0x%02x (rc = %d)\n",
189                     reg_addr, reg_val, rc);
190         return rc;
191 }
192
193 /* Enable register clock gating for writing certain registers */
194 static int fsp_reg_write_enable(struct psmouse *psmouse, bool enable)
195 {
196         int v, nv;
197
198         if (fsp_reg_read(psmouse, FSP_REG_SYSCTL1, &v) == -1)
199                 return -1;
200
201         if (enable)
202                 nv = v | FSP_BIT_EN_REG_CLK;
203         else
204                 nv = v & ~FSP_BIT_EN_REG_CLK;
205
206         /* only write if necessary */
207         if (nv != v)
208                 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL1, nv) == -1)
209                         return -1;
210
211         return 0;
212 }
213
214 static int fsp_page_reg_read(struct psmouse *psmouse, int *reg_val)
215 {
216         struct ps2dev *ps2dev = &psmouse->ps2dev;
217         unsigned char param[3];
218         int rc = -1;
219
220         psmouse_deactivate(psmouse);
221
222         ps2_begin_command(ps2dev);
223
224         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
225                 goto out;
226
227         ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
228         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
229
230         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
231                 goto out;
232
233         ps2_sendbyte(ps2dev, 0x83, FSP_CMD_TIMEOUT2);
234         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
235
236         /* get the returned result */
237         if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
238                 goto out;
239
240         *reg_val = param[2];
241         rc = 0;
242
243  out:
244         ps2_end_command(ps2dev);
245         psmouse_activate(psmouse);
246         psmouse_dbg(psmouse,
247                     "READ PAGE REG: 0x%02x (rc = %d)\n",
248                     *reg_val, rc);
249         return rc;
250 }
251
252 static int fsp_page_reg_write(struct psmouse *psmouse, int reg_val)
253 {
254         struct ps2dev *ps2dev = &psmouse->ps2dev;
255         unsigned char v;
256         int rc = -1;
257
258         ps2_begin_command(ps2dev);
259
260         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
261                 goto out;
262
263         ps2_sendbyte(ps2dev, 0x38, FSP_CMD_TIMEOUT2);
264         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
265
266         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
267                 goto out;
268
269         if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
270                 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
271         } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
272                 /* swapping is required */
273                 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
274         } else {
275                 /* swapping isn't necessary */
276                 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
277         }
278
279         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
280         rc = 0;
281
282  out:
283         ps2_end_command(ps2dev);
284         psmouse_dbg(psmouse,
285                     "WRITE PAGE REG: to 0x%02x (rc = %d)\n",
286                     reg_val, rc);
287         return rc;
288 }
289
290 static int fsp_get_version(struct psmouse *psmouse, int *version)
291 {
292         if (fsp_reg_read(psmouse, FSP_REG_VERSION, version))
293                 return -EIO;
294
295         return 0;
296 }
297
298 static int fsp_get_revision(struct psmouse *psmouse, int *rev)
299 {
300         if (fsp_reg_read(psmouse, FSP_REG_REVISION, rev))
301                 return -EIO;
302
303         return 0;
304 }
305
306 static int fsp_get_buttons(struct psmouse *psmouse, int *btn)
307 {
308         static const int buttons[] = {
309                 0x16, /* Left/Middle/Right/Forward/Backward & Scroll Up/Down */
310                 0x06, /* Left/Middle/Right & Scroll Up/Down/Right/Left */
311                 0x04, /* Left/Middle/Right & Scroll Up/Down */
312                 0x02, /* Left/Middle/Right */
313         };
314         int val;
315
316         if (fsp_reg_read(psmouse, FSP_REG_TMOD_STATUS, &val) == -1)
317                 return -EIO;
318
319         *btn = buttons[(val & 0x30) >> 4];
320         return 0;
321 }
322
323 /* Enable on-pad command tag output */
324 static int fsp_opc_tag_enable(struct psmouse *psmouse, bool enable)
325 {
326         int v, nv;
327         int res = 0;
328
329         if (fsp_reg_read(psmouse, FSP_REG_OPC_QDOWN, &v) == -1) {
330                 psmouse_err(psmouse, "Unable get OPC state.\n");
331                 return -EIO;
332         }
333
334         if (enable)
335                 nv = v | FSP_BIT_EN_OPC_TAG;
336         else
337                 nv = v & ~FSP_BIT_EN_OPC_TAG;
338
339         /* only write if necessary */
340         if (nv != v) {
341                 fsp_reg_write_enable(psmouse, true);
342                 res = fsp_reg_write(psmouse, FSP_REG_OPC_QDOWN, nv);
343                 fsp_reg_write_enable(psmouse, false);
344         }
345
346         if (res != 0) {
347                 psmouse_err(psmouse, "Unable to enable OPC tag.\n");
348                 res = -EIO;
349         }
350
351         return res;
352 }
353
354 static int fsp_onpad_vscr(struct psmouse *psmouse, bool enable)
355 {
356         struct fsp_data *pad = psmouse->private;
357         int val;
358
359         if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
360                 return -EIO;
361
362         pad->vscroll = enable;
363
364         if (enable)
365                 val |= (FSP_BIT_FIX_VSCR | FSP_BIT_ONPAD_ENABLE);
366         else
367                 val &= ~FSP_BIT_FIX_VSCR;
368
369         if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
370                 return -EIO;
371
372         return 0;
373 }
374
375 static int fsp_onpad_hscr(struct psmouse *psmouse, bool enable)
376 {
377         struct fsp_data *pad = psmouse->private;
378         int val, v2;
379
380         if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
381                 return -EIO;
382
383         if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &v2))
384                 return -EIO;
385
386         pad->hscroll = enable;
387
388         if (enable) {
389                 val |= (FSP_BIT_FIX_HSCR | FSP_BIT_ONPAD_ENABLE);
390                 v2 |= FSP_BIT_EN_MSID6;
391         } else {
392                 val &= ~FSP_BIT_FIX_HSCR;
393                 v2 &= ~(FSP_BIT_EN_MSID6 | FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8);
394         }
395
396         if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
397                 return -EIO;
398
399         /* reconfigure horizontal scrolling packet output */
400         if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, v2))
401                 return -EIO;
402
403         return 0;
404 }
405
406 /*
407  * Write device specific initial parameters.
408  *
409  * ex: 0xab 0xcd - write oxcd into register 0xab
410  */
411 static ssize_t fsp_attr_set_setreg(struct psmouse *psmouse, void *data,
412                                    const char *buf, size_t count)
413 {
414         unsigned long reg, val;
415         char *rest;
416         ssize_t retval;
417
418         reg = simple_strtoul(buf, &rest, 16);
419         if (rest == buf || *rest != ' ' || reg > 0xff)
420                 return -EINVAL;
421
422         if (strict_strtoul(rest + 1, 16, &val) || val > 0xff)
423                 return -EINVAL;
424
425         if (fsp_reg_write_enable(psmouse, true))
426                 return -EIO;
427
428         retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
429
430         fsp_reg_write_enable(psmouse, false);
431
432         return count;
433 }
434
435 PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
436
437 static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
438                                         void *data, char *buf)
439 {
440         struct fsp_data *pad = psmouse->private;
441
442         return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
443 }
444
445 /*
446  * Read a register from device.
447  *
448  * ex: 0xab -- read content from register 0xab
449  */
450 static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
451                                         const char *buf, size_t count)
452 {
453         struct fsp_data *pad = psmouse->private;
454         unsigned long reg;
455         int val;
456
457         if (strict_strtoul(buf, 16, &reg) || reg > 0xff)
458                 return -EINVAL;
459
460         if (fsp_reg_read(psmouse, reg, &val))
461                 return -EIO;
462
463         pad->last_reg = reg;
464         pad->last_val = val;
465
466         return count;
467 }
468
469 PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
470                         fsp_attr_show_getreg, fsp_attr_set_getreg);
471
472 static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
473                                         void *data, char *buf)
474 {
475         int val = 0;
476
477         if (fsp_page_reg_read(psmouse, &val))
478                 return -EIO;
479
480         return sprintf(buf, "%02x\n", val);
481 }
482
483 static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
484                                         const char *buf, size_t count)
485 {
486         unsigned long val;
487
488         if (strict_strtoul(buf, 16, &val) || val > 0xff)
489                 return -EINVAL;
490
491         if (fsp_page_reg_write(psmouse, val))
492                 return -EIO;
493
494         return count;
495 }
496
497 PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
498                         fsp_attr_show_pagereg, fsp_attr_set_pagereg);
499
500 static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
501                                         void *data, char *buf)
502 {
503         struct fsp_data *pad = psmouse->private;
504
505         return sprintf(buf, "%d\n", pad->vscroll);
506 }
507
508 static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
509                                         const char *buf, size_t count)
510 {
511         unsigned long val;
512
513         if (strict_strtoul(buf, 10, &val) || val > 1)
514                 return -EINVAL;
515
516         fsp_onpad_vscr(psmouse, val);
517
518         return count;
519 }
520
521 PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
522                         fsp_attr_show_vscroll, fsp_attr_set_vscroll);
523
524 static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
525                                         void *data, char *buf)
526 {
527         struct fsp_data *pad = psmouse->private;
528
529         return sprintf(buf, "%d\n", pad->hscroll);
530 }
531
532 static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
533                                         const char *buf, size_t count)
534 {
535         unsigned long val;
536
537         if (strict_strtoul(buf, 10, &val) || val > 1)
538                 return -EINVAL;
539
540         fsp_onpad_hscr(psmouse, val);
541
542         return count;
543 }
544
545 PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
546                         fsp_attr_show_hscroll, fsp_attr_set_hscroll);
547
548 static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
549                                         void *data, char *buf)
550 {
551         struct fsp_data *pad = psmouse->private;
552
553         return sprintf(buf, "%c\n",
554                         pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
555 }
556
557 static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
558                                         const char *buf, size_t count)
559 {
560         struct fsp_data *pad = psmouse->private;
561         size_t i;
562
563         for (i = 0; i < count; i++) {
564                 switch (buf[i]) {
565                 case 'C':
566                         pad->flags |= FSPDRV_FLAG_EN_OPC;
567                         break;
568                 case 'c':
569                         pad->flags &= ~FSPDRV_FLAG_EN_OPC;
570                         break;
571                 default:
572                         return -EINVAL;
573                 }
574         }
575         return count;
576 }
577
578 PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
579                         fsp_attr_show_flags, fsp_attr_set_flags);
580
581 static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
582                                         void *data, char *buf)
583 {
584         return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
585 }
586
587 PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
588
589 static struct attribute *fsp_attributes[] = {
590         &psmouse_attr_setreg.dattr.attr,
591         &psmouse_attr_getreg.dattr.attr,
592         &psmouse_attr_page.dattr.attr,
593         &psmouse_attr_vscroll.dattr.attr,
594         &psmouse_attr_hscroll.dattr.attr,
595         &psmouse_attr_flags.dattr.attr,
596         &psmouse_attr_ver.dattr.attr,
597         NULL
598 };
599
600 static struct attribute_group fsp_attribute_group = {
601         .attrs = fsp_attributes,
602 };
603
604 #ifdef  FSP_DEBUG
605 static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
606 {
607         static unsigned int ps2_packet_cnt;
608         static unsigned int ps2_last_second;
609         unsigned int jiffies_msec;
610         const char *packet_type = "UNKNOWN";
611         unsigned short abs_x = 0, abs_y = 0;
612
613         /* Interpret & dump the packet data. */
614         switch (packet[0] >> FSP_PKT_TYPE_SHIFT) {
615         case FSP_PKT_TYPE_ABS:
616                 packet_type = "Absolute";
617                 abs_x = GET_ABS_X(packet);
618                 abs_y = GET_ABS_Y(packet);
619                 break;
620         case FSP_PKT_TYPE_NORMAL:
621                 packet_type = "Normal";
622                 break;
623         case FSP_PKT_TYPE_NOTIFY:
624                 packet_type = "Notify";
625                 break;
626         case FSP_PKT_TYPE_NORMAL_OPC:
627                 packet_type = "Normal-OPC";
628                 break;
629         }
630
631         ps2_packet_cnt++;
632         jiffies_msec = jiffies_to_msecs(jiffies);
633         psmouse_dbg(psmouse,
634                     "%08dms %s packets: %02x, %02x, %02x, %02x; "
635                     "abs_x: %d, abs_y: %d\n",
636                     jiffies_msec, packet_type,
637                     packet[0], packet[1], packet[2], packet[3], abs_x, abs_y);
638
639         if (jiffies_msec - ps2_last_second > 1000) {
640                 psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
641                 ps2_packet_cnt = 0;
642                 ps2_last_second = jiffies_msec;
643         }
644 }
645 #else
646 static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
647 {
648 }
649 #endif
650
651 static void fsp_set_slot(struct input_dev *dev, int slot, bool active,
652                          unsigned int x, unsigned int y)
653 {
654         input_mt_slot(dev, slot);
655         input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
656         if (active) {
657                 input_report_abs(dev, ABS_MT_POSITION_X, x);
658                 input_report_abs(dev, ABS_MT_POSITION_Y, y);
659         }
660 }
661
662 static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
663 {
664         struct input_dev *dev = psmouse->dev;
665         struct fsp_data *ad = psmouse->private;
666         unsigned char *packet = psmouse->packet;
667         unsigned char button_status = 0, lscroll = 0, rscroll = 0;
668         unsigned short abs_x, abs_y, fgrs = 0;
669         int rel_x, rel_y;
670
671         if (psmouse->pktcnt < 4)
672                 return PSMOUSE_GOOD_DATA;
673
674         /*
675          * Full packet accumulated, process it
676          */
677
678         fsp_packet_debug(psmouse, packet);
679
680         switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
681         case FSP_PKT_TYPE_ABS:
682                 abs_x = GET_ABS_X(packet);
683                 abs_y = GET_ABS_Y(packet);
684
685                 if (packet[0] & FSP_PB0_MFMC) {
686                         /*
687                          * MFMC packet: assume that there are two fingers on
688                          * pad
689                          */
690                         fgrs = 2;
691
692                         /* MFMC packet */
693                         if (packet[0] & FSP_PB0_MFMC_FGR2) {
694                                 /* 2nd finger */
695                                 if (ad->last_mt_fgr == 2) {
696                                         /*
697                                          * workaround for buggy firmware
698                                          * which doesn't clear MFMC bit if
699                                          * the 1st finger is up
700                                          */
701                                         fgrs = 1;
702                                         fsp_set_slot(dev, 0, false, 0, 0);
703                                 }
704                                 ad->last_mt_fgr = 2;
705
706                                 fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
707                         } else {
708                                 /* 1st finger */
709                                 if (ad->last_mt_fgr == 1) {
710                                         /*
711                                          * workaround for buggy firmware
712                                          * which doesn't clear MFMC bit if
713                                          * the 2nd finger is up
714                                          */
715                                         fgrs = 1;
716                                         fsp_set_slot(dev, 1, false, 0, 0);
717                                 }
718                                 ad->last_mt_fgr = 1;
719                                 fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
720                         }
721                 } else {
722                         /* SFAC packet */
723
724                         /* no multi-finger information */
725                         ad->last_mt_fgr = 0;
726
727                         if (abs_x != 0 && abs_y != 0)
728                                 fgrs = 1;
729
730                         fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
731                         fsp_set_slot(dev, 1, false, 0, 0);
732                 }
733                 if (fgrs > 0) {
734                         input_report_abs(dev, ABS_X, abs_x);
735                         input_report_abs(dev, ABS_Y, abs_y);
736                 }
737                 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
738                 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
739                 input_report_key(dev, BTN_TOUCH, fgrs);
740                 input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
741                 input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
742                 break;
743
744         case FSP_PKT_TYPE_NORMAL_OPC:
745                 /* on-pad click, filter it if necessary */
746                 if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
747                         packet[0] &= ~FSP_PB0_LBTN;
748                 /* fall through */
749
750         case FSP_PKT_TYPE_NORMAL:
751                 /* normal packet */
752                 /* special packet data translation from on-pad packets */
753                 if (packet[3] != 0) {
754                         if (packet[3] & BIT(0))
755                                 button_status |= 0x01;  /* wheel down */
756                         if (packet[3] & BIT(1))
757                                 button_status |= 0x0f;  /* wheel up */
758                         if (packet[3] & BIT(2))
759                                 button_status |= BIT(4);/* horizontal left */
760                         if (packet[3] & BIT(3))
761                                 button_status |= BIT(5);/* horizontal right */
762                         /* push back to packet queue */
763                         if (button_status != 0)
764                                 packet[3] = button_status;
765                         rscroll = (packet[3] >> 4) & 1;
766                         lscroll = (packet[3] >> 5) & 1;
767                 }
768                 /*
769                  * Processing wheel up/down and extra button events
770                  */
771                 input_report_rel(dev, REL_WHEEL,
772                                  (int)(packet[3] & 8) - (int)(packet[3] & 7));
773                 input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
774                 input_report_key(dev, BTN_BACK, lscroll);
775                 input_report_key(dev, BTN_FORWARD, rscroll);
776
777                 /*
778                  * Standard PS/2 Mouse
779                  */
780                 input_report_key(dev, BTN_LEFT, packet[0] & 1);
781                 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
782                 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
783
784                 rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
785                 rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
786
787                 input_report_rel(dev, REL_X, rel_x);
788                 input_report_rel(dev, REL_Y, rel_y);
789                 break;
790         }
791
792         input_sync(dev);
793
794         return PSMOUSE_FULL_PACKET;
795 }
796
797 static int fsp_activate_protocol(struct psmouse *psmouse)
798 {
799         struct fsp_data *pad = psmouse->private;
800         struct ps2dev *ps2dev = &psmouse->ps2dev;
801         unsigned char param[2];
802         int val;
803
804         /*
805          * Standard procedure to enter FSP Intellimouse mode
806          * (scrolling wheel, 4th and 5th buttons)
807          */
808         param[0] = 200;
809         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
810         param[0] = 200;
811         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
812         param[0] =  80;
813         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
814
815         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
816         if (param[0] != 0x04) {
817                 psmouse_err(psmouse,
818                             "Unable to enable 4 bytes packet format.\n");
819                 return -EIO;
820         }
821
822         if (pad->ver < FSP_VER_STL3888_C0) {
823                 /* Preparing relative coordinates output for older hardware */
824                 if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
825                         psmouse_err(psmouse,
826                                     "Unable to read SYSCTL5 register.\n");
827                         return -EIO;
828                 }
829
830                 if (fsp_get_buttons(psmouse, &pad->buttons)) {
831                         psmouse_err(psmouse,
832                                     "Unable to retrieve number of buttons.\n");
833                         return -EIO;
834                 }
835
836                 val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
837                 /* Ensure we are not in absolute mode */
838                 val &= ~FSP_BIT_EN_PKT_G0;
839                 if (pad->buttons == 0x06) {
840                         /* Left/Middle/Right & Scroll Up/Down/Right/Left */
841                         val |= FSP_BIT_EN_MSID6;
842                 }
843
844                 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
845                         psmouse_err(psmouse,
846                                     "Unable to set up required mode bits.\n");
847                         return -EIO;
848                 }
849
850                 /*
851                  * Enable OPC tags such that driver can tell the difference
852                  * between on-pad and real button click
853                  */
854                 if (fsp_opc_tag_enable(psmouse, true))
855                         psmouse_warn(psmouse,
856                                      "Failed to enable OPC tag mode.\n");
857                 /* enable on-pad click by default */
858                 pad->flags |= FSPDRV_FLAG_EN_OPC;
859
860                 /* Enable on-pad vertical and horizontal scrolling */
861                 fsp_onpad_vscr(psmouse, true);
862                 fsp_onpad_hscr(psmouse, true);
863         } else {
864                 /* Enable absolute coordinates output for Cx/Dx hardware */
865                 if (fsp_reg_write(psmouse, FSP_REG_SWC1,
866                                   FSP_BIT_SWC1_EN_ABS_1F |
867                                   FSP_BIT_SWC1_EN_ABS_2F |
868                                   FSP_BIT_SWC1_EN_FUP_OUT |
869                                   FSP_BIT_SWC1_EN_ABS_CON)) {
870                         psmouse_err(psmouse,
871                                     "Unable to enable absolute coordinates output.\n");
872                         return -EIO;
873                 }
874         }
875
876         return 0;
877 }
878
879 static int fsp_set_input_params(struct psmouse *psmouse)
880 {
881         struct input_dev *dev = psmouse->dev;
882         struct fsp_data *pad = psmouse->private;
883
884         if (pad->ver < FSP_VER_STL3888_C0) {
885                 __set_bit(BTN_MIDDLE, dev->keybit);
886                 __set_bit(BTN_BACK, dev->keybit);
887                 __set_bit(BTN_FORWARD, dev->keybit);
888                 __set_bit(REL_WHEEL, dev->relbit);
889                 __set_bit(REL_HWHEEL, dev->relbit);
890         } else {
891                 /*
892                  * Hardware prior to Cx performs much better in relative mode;
893                  * hence, only enable absolute coordinates output as well as
894                  * multi-touch output for the newer hardware.
895                  *
896                  * Maximum coordinates can be computed as:
897                  *
898                  *      number of scanlines * 64 - 57
899                  *
900                  * where number of X/Y scanline lines are 16/12.
901                  */
902                 int abs_x = 967, abs_y = 711;
903
904                 __set_bit(EV_ABS, dev->evbit);
905                 __clear_bit(EV_REL, dev->evbit);
906                 __set_bit(BTN_TOUCH, dev->keybit);
907                 __set_bit(BTN_TOOL_FINGER, dev->keybit);
908                 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
909                 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
910
911                 input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
912                 input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
913                 input_mt_init_slots(dev, 2);
914                 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
915                 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
916         }
917
918         return 0;
919 }
920
921 int fsp_detect(struct psmouse *psmouse, bool set_properties)
922 {
923         int id;
924
925         if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
926                 return -EIO;
927
928         if (id != 0x01)
929                 return -ENODEV;
930
931         if (set_properties) {
932                 psmouse->vendor = "Sentelic";
933                 psmouse->name = "FingerSensingPad";
934         }
935
936         return 0;
937 }
938
939 static void fsp_reset(struct psmouse *psmouse)
940 {
941         fsp_opc_tag_enable(psmouse, false);
942         fsp_onpad_vscr(psmouse, false);
943         fsp_onpad_hscr(psmouse, false);
944 }
945
946 static void fsp_disconnect(struct psmouse *psmouse)
947 {
948         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
949                            &fsp_attribute_group);
950
951         fsp_reset(psmouse);
952         kfree(psmouse->private);
953 }
954
955 static int fsp_reconnect(struct psmouse *psmouse)
956 {
957         int version;
958
959         if (fsp_detect(psmouse, 0))
960                 return -ENODEV;
961
962         if (fsp_get_version(psmouse, &version))
963                 return -ENODEV;
964
965         if (fsp_activate_protocol(psmouse))
966                 return -EIO;
967
968         return 0;
969 }
970
971 int fsp_init(struct psmouse *psmouse)
972 {
973         struct fsp_data *priv;
974         int ver, rev;
975         int error;
976
977         if (fsp_get_version(psmouse, &ver) ||
978             fsp_get_revision(psmouse, &rev)) {
979                 return -ENODEV;
980         }
981
982         psmouse_info(psmouse, "Finger Sensing Pad, hw: %d.%d.%d, sw: %s\n",
983                      ver >> 4, ver & 0x0F, rev, fsp_drv_ver);
984
985         psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
986         if (!priv)
987                 return -ENOMEM;
988
989         priv->ver = ver;
990         priv->rev = rev;
991
992         psmouse->protocol_handler = fsp_process_byte;
993         psmouse->disconnect = fsp_disconnect;
994         psmouse->reconnect = fsp_reconnect;
995         psmouse->cleanup = fsp_reset;
996         psmouse->pktsize = 4;
997
998         error = fsp_activate_protocol(psmouse);
999         if (error)
1000                 goto err_out;
1001
1002         /* Set up various supported input event bits */
1003         error = fsp_set_input_params(psmouse);
1004         if (error)
1005                 goto err_out;
1006
1007         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1008                                    &fsp_attribute_group);
1009         if (error) {
1010                 psmouse_err(psmouse,
1011                             "Failed to create sysfs attributes (%d)", error);
1012                 goto err_out;
1013         }
1014
1015         return 0;
1016
1017  err_out:
1018         kfree(psmouse->private);
1019         psmouse->private = NULL;
1020         return error;
1021 }