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