dc8b758cb02405c6c9087810789786dfab2dc662
[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-2011 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/ctype.h>
25 #include <linux/libps2.h>
26 #include <linux/serio.h>
27 #include <linux/jiffies.h>
28 #include <linux/slab.h>
29
30 #include "psmouse.h"
31 #include "sentelic.h"
32
33 /*
34  * Timeout for FSP PS/2 command only (in milliseconds).
35  */
36 #define FSP_CMD_TIMEOUT         200
37 #define FSP_CMD_TIMEOUT2        30
38
39 /** Driver version. */
40 static const char fsp_drv_ver[] = "1.0.0-K";
41
42 /*
43  * Make sure that the value being sent to FSP will not conflict with
44  * possible sample rate values.
45  */
46 static unsigned char fsp_test_swap_cmd(unsigned char reg_val)
47 {
48         switch (reg_val) {
49         case 10: case 20: case 40: case 60: case 80: case 100: case 200:
50                 /*
51                  * The requested value being sent to FSP matched to possible
52                  * sample rates, swap the given value such that the hardware
53                  * wouldn't get confused.
54                  */
55                 return (reg_val >> 4) | (reg_val << 4);
56         default:
57                 return reg_val; /* swap isn't necessary */
58         }
59 }
60
61 /*
62  * Make sure that the value being sent to FSP will not conflict with certain
63  * commands.
64  */
65 static unsigned char fsp_test_invert_cmd(unsigned char reg_val)
66 {
67         switch (reg_val) {
68         case 0xe9: case 0xee: case 0xf2: case 0xff:
69                 /*
70                  * The requested value being sent to FSP matched to certain
71                  * commands, inverse the given value such that the hardware
72                  * wouldn't get confused.
73                  */
74                 return ~reg_val;
75         default:
76                 return reg_val; /* inversion isn't necessary */
77         }
78 }
79
80 static int fsp_reg_read(struct psmouse *psmouse, int reg_addr, int *reg_val)
81 {
82         struct ps2dev *ps2dev = &psmouse->ps2dev;
83         unsigned char param[3];
84         unsigned char addr;
85         int rc = -1;
86
87         /*
88          * We need to shut off the device and switch it into command
89          * mode so we don't confuse our protocol handler. We don't need
90          * to do that for writes because sysfs set helper does this for
91          * us.
92          */
93         psmouse_deactivate(psmouse);
94
95         ps2_begin_command(ps2dev);
96
97         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
98                 goto out;
99
100         /* should return 0xfe(request for resending) */
101         ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
102         /* should return 0xfc(failed) */
103         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
104
105         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
106                 goto out;
107
108         if ((addr = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
109                 ps2_sendbyte(ps2dev, 0x68, FSP_CMD_TIMEOUT2);
110         } else if ((addr = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
111                 /* swapping is required */
112                 ps2_sendbyte(ps2dev, 0xcc, FSP_CMD_TIMEOUT2);
113                 /* expect 0xfe */
114         } else {
115                 /* swapping isn't necessary */
116                 ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
117                 /* expect 0xfe */
118         }
119         /* should return 0xfc(failed) */
120         ps2_sendbyte(ps2dev, addr, FSP_CMD_TIMEOUT);
121
122         if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) < 0)
123                 goto out;
124
125         *reg_val = param[2];
126         rc = 0;
127
128  out:
129         ps2_end_command(ps2dev);
130         psmouse_activate(psmouse);
131         dev_dbg(&ps2dev->serio->dev, "READ REG: 0x%02x is 0x%02x (rc = %d)\n",
132                 reg_addr, *reg_val, rc);
133         return rc;
134 }
135
136 static int fsp_reg_write(struct psmouse *psmouse, int reg_addr, int reg_val)
137 {
138         struct ps2dev *ps2dev = &psmouse->ps2dev;
139         unsigned char v;
140         int rc = -1;
141
142         ps2_begin_command(ps2dev);
143
144         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
145                 goto out;
146
147         if ((v = fsp_test_invert_cmd(reg_addr)) != reg_addr) {
148                 /* inversion is required */
149                 ps2_sendbyte(ps2dev, 0x74, FSP_CMD_TIMEOUT2);
150         } else {
151                 if ((v = fsp_test_swap_cmd(reg_addr)) != reg_addr) {
152                         /* swapping is required */
153                         ps2_sendbyte(ps2dev, 0x77, FSP_CMD_TIMEOUT2);
154                 } else {
155                         /* swapping isn't necessary */
156                         ps2_sendbyte(ps2dev, 0x55, FSP_CMD_TIMEOUT2);
157                 }
158         }
159         /* write the register address in correct order */
160         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
161
162         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
163                 goto out;
164
165         if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
166                 /* inversion is required */
167                 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
168         } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
169                 /* swapping is required */
170                 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
171         } else {
172                 /* swapping isn't necessary */
173                 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
174         }
175
176         /* write the register value in correct order */
177         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
178         rc = 0;
179
180  out:
181         ps2_end_command(ps2dev);
182         dev_dbg(&ps2dev->serio->dev, "WRITE REG: 0x%02x to 0x%02x (rc = %d)\n",
183                 reg_addr, reg_val, rc);
184         return rc;
185 }
186
187 /* Enable register clock gating for writing certain registers */
188 static int fsp_reg_write_enable(struct psmouse *psmouse, bool enable)
189 {
190         int v, nv;
191
192         if (fsp_reg_read(psmouse, FSP_REG_SYSCTL1, &v) == -1)
193                 return -1;
194
195         if (enable)
196                 nv = v | FSP_BIT_EN_REG_CLK;
197         else
198                 nv = v & ~FSP_BIT_EN_REG_CLK;
199
200         /* only write if necessary */
201         if (nv != v)
202                 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL1, nv) == -1)
203                         return -1;
204
205         return 0;
206 }
207
208 static int fsp_page_reg_read(struct psmouse *psmouse, int *reg_val)
209 {
210         struct ps2dev *ps2dev = &psmouse->ps2dev;
211         unsigned char param[3];
212         int rc = -1;
213
214         psmouse_deactivate(psmouse);
215
216         ps2_begin_command(ps2dev);
217
218         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
219                 goto out;
220
221         ps2_sendbyte(ps2dev, 0x66, FSP_CMD_TIMEOUT2);
222         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
223
224         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
225                 goto out;
226
227         ps2_sendbyte(ps2dev, 0x83, FSP_CMD_TIMEOUT2);
228         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
229
230         /* get the returned result */
231         if (__ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
232                 goto out;
233
234         *reg_val = param[2];
235         rc = 0;
236
237  out:
238         ps2_end_command(ps2dev);
239         psmouse_activate(psmouse);
240         dev_dbg(&ps2dev->serio->dev, "READ PAGE REG: 0x%02x (rc = %d)\n",
241                 *reg_val, rc);
242         return rc;
243 }
244
245 static int fsp_page_reg_write(struct psmouse *psmouse, int reg_val)
246 {
247         struct ps2dev *ps2dev = &psmouse->ps2dev;
248         unsigned char v;
249         int rc = -1;
250
251         ps2_begin_command(ps2dev);
252
253         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
254                 goto out;
255
256         ps2_sendbyte(ps2dev, 0x38, FSP_CMD_TIMEOUT2);
257         ps2_sendbyte(ps2dev, 0x88, FSP_CMD_TIMEOUT2);
258
259         if (ps2_sendbyte(ps2dev, 0xf3, FSP_CMD_TIMEOUT) < 0)
260                 goto out;
261
262         if ((v = fsp_test_invert_cmd(reg_val)) != reg_val) {
263                 ps2_sendbyte(ps2dev, 0x47, FSP_CMD_TIMEOUT2);
264         } else if ((v = fsp_test_swap_cmd(reg_val)) != reg_val) {
265                 /* swapping is required */
266                 ps2_sendbyte(ps2dev, 0x44, FSP_CMD_TIMEOUT2);
267         } else {
268                 /* swapping isn't necessary */
269                 ps2_sendbyte(ps2dev, 0x33, FSP_CMD_TIMEOUT2);
270         }
271
272         ps2_sendbyte(ps2dev, v, FSP_CMD_TIMEOUT2);
273         rc = 0;
274
275  out:
276         ps2_end_command(ps2dev);
277         dev_dbg(&ps2dev->serio->dev, "WRITE PAGE REG: to 0x%02x (rc = %d)\n",
278                 reg_val, rc);
279         return rc;
280 }
281
282 static int fsp_get_version(struct psmouse *psmouse, int *version)
283 {
284         if (fsp_reg_read(psmouse, FSP_REG_VERSION, version))
285                 return -EIO;
286
287         return 0;
288 }
289
290 static int fsp_get_revision(struct psmouse *psmouse, int *rev)
291 {
292         if (fsp_reg_read(psmouse, FSP_REG_REVISION, rev))
293                 return -EIO;
294
295         return 0;
296 }
297
298 static int fsp_get_buttons(struct psmouse *psmouse, int *btn)
299 {
300         static const int buttons[] = {
301                 0x16, /* Left/Middle/Right/Forward/Backward & Scroll Up/Down */
302                 0x06, /* Left/Middle/Right & Scroll Up/Down/Right/Left */
303                 0x04, /* Left/Middle/Right & Scroll Up/Down */
304                 0x02, /* Left/Middle/Right */
305         };
306         int val;
307
308         if (fsp_reg_read(psmouse, FSP_REG_TMOD_STATUS, &val) == -1)
309                 return -EIO;
310
311         *btn = buttons[(val & 0x30) >> 4];
312         return 0;
313 }
314
315 /* Enable on-pad command tag output */
316 static int fsp_opc_tag_enable(struct psmouse *psmouse, bool enable)
317 {
318         int v, nv;
319         int res = 0;
320
321         if (fsp_reg_read(psmouse, FSP_REG_OPC_QDOWN, &v) == -1) {
322                 dev_err(&psmouse->ps2dev.serio->dev, "Unable get OPC state.\n");
323                 return -EIO;
324         }
325
326         if (enable)
327                 nv = v | FSP_BIT_EN_OPC_TAG;
328         else
329                 nv = v & ~FSP_BIT_EN_OPC_TAG;
330
331         /* only write if necessary */
332         if (nv != v) {
333                 fsp_reg_write_enable(psmouse, true);
334                 res = fsp_reg_write(psmouse, FSP_REG_OPC_QDOWN, nv);
335                 fsp_reg_write_enable(psmouse, false);
336         }
337
338         if (res != 0) {
339                 dev_err(&psmouse->ps2dev.serio->dev,
340                         "Unable to enable OPC tag.\n");
341                 res = -EIO;
342         }
343
344         return res;
345 }
346
347 static int fsp_onpad_vscr(struct psmouse *psmouse, bool enable)
348 {
349         struct fsp_data *pad = psmouse->private;
350         int val;
351
352         if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
353                 return -EIO;
354
355         pad->vscroll = enable;
356
357         if (enable)
358                 val |= (FSP_BIT_FIX_VSCR | FSP_BIT_ONPAD_ENABLE);
359         else
360                 val &= ~FSP_BIT_FIX_VSCR;
361
362         if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
363                 return -EIO;
364
365         return 0;
366 }
367
368 static int fsp_onpad_hscr(struct psmouse *psmouse, bool enable)
369 {
370         struct fsp_data *pad = psmouse->private;
371         int val, v2;
372
373         if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
374                 return -EIO;
375
376         if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &v2))
377                 return -EIO;
378
379         pad->hscroll = enable;
380
381         if (enable) {
382                 val |= (FSP_BIT_FIX_HSCR | FSP_BIT_ONPAD_ENABLE);
383                 v2 |= FSP_BIT_EN_MSID6;
384         } else {
385                 val &= ~FSP_BIT_FIX_HSCR;
386                 v2 &= ~(FSP_BIT_EN_MSID6 | FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8);
387         }
388
389         if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
390                 return -EIO;
391
392         /* reconfigure horizontal scrolling packet output */
393         if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, v2))
394                 return -EIO;
395
396         return 0;
397 }
398
399 /*
400  * Write device specific initial parameters.
401  *
402  * ex: 0xab 0xcd - write oxcd into register 0xab
403  */
404 static ssize_t fsp_attr_set_setreg(struct psmouse *psmouse, void *data,
405                                    const char *buf, size_t count)
406 {
407         unsigned long reg, val;
408         char *rest;
409         ssize_t retval;
410
411         reg = simple_strtoul(buf, &rest, 16);
412         if (rest == buf || *rest != ' ' || reg > 0xff)
413                 return -EINVAL;
414
415         if (strict_strtoul(rest + 1, 16, &val) || val > 0xff)
416                 return -EINVAL;
417
418         if (fsp_reg_write_enable(psmouse, true))
419                 return -EIO;
420
421         retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
422
423         fsp_reg_write_enable(psmouse, false);
424
425         return count;
426 }
427
428 PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
429
430 static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
431                                         void *data, char *buf)
432 {
433         struct fsp_data *pad = psmouse->private;
434
435         return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
436 }
437
438 /*
439  * Read a register from device.
440  *
441  * ex: 0xab -- read content from register 0xab
442  */
443 static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
444                                         const char *buf, size_t count)
445 {
446         struct fsp_data *pad = psmouse->private;
447         unsigned long reg;
448         int val;
449
450         if (strict_strtoul(buf, 16, &reg) || reg > 0xff)
451                 return -EINVAL;
452
453         if (fsp_reg_read(psmouse, reg, &val))
454                 return -EIO;
455
456         pad->last_reg = reg;
457         pad->last_val = val;
458
459         return count;
460 }
461
462 PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
463                         fsp_attr_show_getreg, fsp_attr_set_getreg);
464
465 static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
466                                         void *data, char *buf)
467 {
468         int val = 0;
469
470         if (fsp_page_reg_read(psmouse, &val))
471                 return -EIO;
472
473         return sprintf(buf, "%02x\n", val);
474 }
475
476 static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
477                                         const char *buf, size_t count)
478 {
479         unsigned long val;
480
481         if (strict_strtoul(buf, 16, &val) || val > 0xff)
482                 return -EINVAL;
483
484         if (fsp_page_reg_write(psmouse, val))
485                 return -EIO;
486
487         return count;
488 }
489
490 PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
491                         fsp_attr_show_pagereg, fsp_attr_set_pagereg);
492
493 static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
494                                         void *data, char *buf)
495 {
496         struct fsp_data *pad = psmouse->private;
497
498         return sprintf(buf, "%d\n", pad->vscroll);
499 }
500
501 static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
502                                         const char *buf, size_t count)
503 {
504         unsigned long val;
505
506         if (strict_strtoul(buf, 10, &val) || val > 1)
507                 return -EINVAL;
508
509         fsp_onpad_vscr(psmouse, val);
510
511         return count;
512 }
513
514 PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
515                         fsp_attr_show_vscroll, fsp_attr_set_vscroll);
516
517 static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
518                                         void *data, char *buf)
519 {
520         struct fsp_data *pad = psmouse->private;
521
522         return sprintf(buf, "%d\n", pad->hscroll);
523 }
524
525 static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
526                                         const char *buf, size_t count)
527 {
528         unsigned long val;
529
530         if (strict_strtoul(buf, 10, &val) || val > 1)
531                 return -EINVAL;
532
533         fsp_onpad_hscr(psmouse, val);
534
535         return count;
536 }
537
538 PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
539                         fsp_attr_show_hscroll, fsp_attr_set_hscroll);
540
541 static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
542                                         void *data, char *buf)
543 {
544         struct fsp_data *pad = psmouse->private;
545
546         return sprintf(buf, "%c\n",
547                         pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
548 }
549
550 static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
551                                         const char *buf, size_t count)
552 {
553         struct fsp_data *pad = psmouse->private;
554         size_t i;
555
556         for (i = 0; i < count; i++) {
557                 switch (buf[i]) {
558                 case 'C':
559                         pad->flags |= FSPDRV_FLAG_EN_OPC;
560                         break;
561                 case 'c':
562                         pad->flags &= ~FSPDRV_FLAG_EN_OPC;
563                         break;
564                 default:
565                         return -EINVAL;
566                 }
567         }
568         return count;
569 }
570
571 PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
572                         fsp_attr_show_flags, fsp_attr_set_flags);
573
574 static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
575                                         void *data, char *buf)
576 {
577         return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
578 }
579
580 PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
581
582 static struct attribute *fsp_attributes[] = {
583         &psmouse_attr_setreg.dattr.attr,
584         &psmouse_attr_getreg.dattr.attr,
585         &psmouse_attr_page.dattr.attr,
586         &psmouse_attr_vscroll.dattr.attr,
587         &psmouse_attr_hscroll.dattr.attr,
588         &psmouse_attr_flags.dattr.attr,
589         &psmouse_attr_ver.dattr.attr,
590         NULL
591 };
592
593 static struct attribute_group fsp_attribute_group = {
594         .attrs = fsp_attributes,
595 };
596
597 #ifdef FSP_DEBUG
598 static void fsp_packet_debug(unsigned char packet[])
599 {
600         static unsigned int ps2_packet_cnt;
601         static unsigned int ps2_last_second;
602         unsigned int jiffies_msec;
603
604         ps2_packet_cnt++;
605         jiffies_msec = jiffies_to_msecs(jiffies);
606         psmouse_dbg(psmouse,
607                     "%08dms PS/2 packets: %02x, %02x, %02x, %02x\n",
608                     jiffies_msec, packet[0], packet[1], packet[2], packet[3]);
609
610         if (jiffies_msec - ps2_last_second > 1000) {
611                 psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
612                 ps2_packet_cnt = 0;
613                 ps2_last_second = jiffies_msec;
614         }
615 }
616 #else
617 static void fsp_packet_debug(unsigned char packet[])
618 {
619 }
620 #endif
621
622 static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
623 {
624         struct input_dev *dev = psmouse->dev;
625         struct fsp_data *ad = psmouse->private;
626         unsigned char *packet = psmouse->packet;
627         unsigned char button_status = 0, lscroll = 0, rscroll = 0;
628         int rel_x, rel_y;
629
630         if (psmouse->pktcnt < 4)
631                 return PSMOUSE_GOOD_DATA;
632
633         /*
634          * Full packet accumulated, process it
635          */
636
637         switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
638         case FSP_PKT_TYPE_ABS:
639                 dev_warn(&psmouse->ps2dev.serio->dev,
640                          "Unexpected absolute mode packet, ignored.\n");
641                 break;
642
643         case FSP_PKT_TYPE_NORMAL_OPC:
644                 /* on-pad click, filter it if necessary */
645                 if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
646                         packet[0] &= ~BIT(0);
647                 /* fall through */
648
649         case FSP_PKT_TYPE_NORMAL:
650                 /* normal packet */
651                 /* special packet data translation from on-pad packets */
652                 if (packet[3] != 0) {
653                         if (packet[3] & BIT(0))
654                                 button_status |= 0x01;  /* wheel down */
655                         if (packet[3] & BIT(1))
656                                 button_status |= 0x0f;  /* wheel up */
657                         if (packet[3] & BIT(2))
658                                 button_status |= BIT(4);/* horizontal left */
659                         if (packet[3] & BIT(3))
660                                 button_status |= BIT(5);/* horizontal right */
661                         /* push back to packet queue */
662                         if (button_status != 0)
663                                 packet[3] = button_status;
664                         rscroll = (packet[3] >> 4) & 1;
665                         lscroll = (packet[3] >> 5) & 1;
666                 }
667                 /*
668                  * Processing wheel up/down and extra button events
669                  */
670                 input_report_rel(dev, REL_WHEEL,
671                                  (int)(packet[3] & 8) - (int)(packet[3] & 7));
672                 input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
673                 input_report_key(dev, BTN_BACK, lscroll);
674                 input_report_key(dev, BTN_FORWARD, rscroll);
675
676                 /*
677                  * Standard PS/2 Mouse
678                  */
679                 input_report_key(dev, BTN_LEFT, packet[0] & 1);
680                 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
681                 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
682
683                 rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
684                 rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
685
686                 input_report_rel(dev, REL_X, rel_x);
687                 input_report_rel(dev, REL_Y, rel_y);
688                 break;
689         }
690
691         input_sync(dev);
692
693         fsp_packet_debug(packet);
694
695         return PSMOUSE_FULL_PACKET;
696 }
697
698 static int fsp_activate_protocol(struct psmouse *psmouse)
699 {
700         struct fsp_data *pad = psmouse->private;
701         struct ps2dev *ps2dev = &psmouse->ps2dev;
702         unsigned char param[2];
703         int val;
704
705         /*
706          * Standard procedure to enter FSP Intellimouse mode
707          * (scrolling wheel, 4th and 5th buttons)
708          */
709         param[0] = 200;
710         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
711         param[0] = 200;
712         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
713         param[0] =  80;
714         ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
715
716         ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
717         if (param[0] != 0x04) {
718                 dev_err(&psmouse->ps2dev.serio->dev,
719                         "Unable to enable 4 bytes packet format.\n");
720                 return -EIO;
721         }
722
723         if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
724                 dev_err(&psmouse->ps2dev.serio->dev,
725                         "Unable to read SYSCTL5 register.\n");
726                 return -EIO;
727         }
728
729         val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
730         /* Ensure we are not in absolute mode */
731         val &= ~FSP_BIT_EN_PKT_G0;
732         if (pad->buttons == 0x06) {
733                 /* Left/Middle/Right & Scroll Up/Down/Right/Left */
734                 val |= FSP_BIT_EN_MSID6;
735         }
736
737         if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
738                 dev_err(&psmouse->ps2dev.serio->dev,
739                         "Unable to set up required mode bits.\n");
740                 return -EIO;
741         }
742
743         /*
744          * Enable OPC tags such that driver can tell the difference between
745          * on-pad and real button click
746          */
747         if (fsp_opc_tag_enable(psmouse, true))
748                 dev_warn(&psmouse->ps2dev.serio->dev,
749                          "Failed to enable OPC tag mode.\n");
750
751         /* Enable on-pad vertical and horizontal scrolling */
752         fsp_onpad_vscr(psmouse, true);
753         fsp_onpad_hscr(psmouse, true);
754
755         return 0;
756 }
757
758 int fsp_detect(struct psmouse *psmouse, bool set_properties)
759 {
760         int id;
761
762         if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
763                 return -EIO;
764
765         if (id != 0x01)
766                 return -ENODEV;
767
768         if (set_properties) {
769                 psmouse->vendor = "Sentelic";
770                 psmouse->name = "FingerSensingPad";
771         }
772
773         return 0;
774 }
775
776 static void fsp_reset(struct psmouse *psmouse)
777 {
778         fsp_opc_tag_enable(psmouse, false);
779         fsp_onpad_vscr(psmouse, false);
780         fsp_onpad_hscr(psmouse, false);
781 }
782
783 static void fsp_disconnect(struct psmouse *psmouse)
784 {
785         sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
786                            &fsp_attribute_group);
787
788         fsp_reset(psmouse);
789         kfree(psmouse->private);
790 }
791
792 static int fsp_reconnect(struct psmouse *psmouse)
793 {
794         int version;
795
796         if (fsp_detect(psmouse, 0))
797                 return -ENODEV;
798
799         if (fsp_get_version(psmouse, &version))
800                 return -ENODEV;
801
802         if (fsp_activate_protocol(psmouse))
803                 return -EIO;
804
805         return 0;
806 }
807
808 int fsp_init(struct psmouse *psmouse)
809 {
810         struct fsp_data *priv;
811         int ver, rev, buttons;
812         int error;
813
814         if (fsp_get_version(psmouse, &ver) ||
815             fsp_get_revision(psmouse, &rev) ||
816             fsp_get_buttons(psmouse, &buttons)) {
817                 return -ENODEV;
818         }
819
820         psmouse_info(psmouse,
821                      "Finger Sensing Pad, hw: %d.%d.%d, sw: %s, buttons: %d\n",
822                      ver >> 4, ver & 0x0F, rev, fsp_drv_ver, buttons & 7);
823
824         psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
825         if (!priv)
826                 return -ENOMEM;
827
828         priv->ver = ver;
829         priv->rev = rev;
830         priv->buttons = buttons;
831
832         /* enable on-pad click by default */
833         priv->flags |= FSPDRV_FLAG_EN_OPC;
834
835         /* Set up various supported input event bits */
836         __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
837         __set_bit(BTN_BACK, psmouse->dev->keybit);
838         __set_bit(BTN_FORWARD, psmouse->dev->keybit);
839         __set_bit(REL_WHEEL, psmouse->dev->relbit);
840         __set_bit(REL_HWHEEL, psmouse->dev->relbit);
841
842         psmouse->protocol_handler = fsp_process_byte;
843         psmouse->disconnect = fsp_disconnect;
844         psmouse->reconnect = fsp_reconnect;
845         psmouse->cleanup = fsp_reset;
846         psmouse->pktsize = 4;
847
848         /* set default packet output based on number of buttons we found */
849         error = fsp_activate_protocol(psmouse);
850         if (error)
851                 goto err_out;
852
853         error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
854                                    &fsp_attribute_group);
855         if (error) {
856                 dev_err(&psmouse->ps2dev.serio->dev,
857                         "Failed to create sysfs attributes (%d)", error);
858                 goto err_out;
859         }
860
861         return 0;
862
863  err_out:
864         kfree(psmouse->private);
865         psmouse->private = NULL;
866         return error;
867 }