blob: a977bfaa6821faef1c2993c919f77a854b9a03d8 [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
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -070040#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
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -070043/** Driver version. */
44static 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 */
50static 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 */
69static 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
84static 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 */
Paul Foxc35c0e72012-02-24 00:51:37 -080097 psmouse_deactivate(psmouse);
Dmitry Torokhov181d6832009-09-16 01:06:43 -070098
99 ps2_begin_command(ps2dev);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700100
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:
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700133 ps2_end_command(ps2dev);
Paul Foxc35c0e72012-02-24 00:51:37 -0800134 psmouse_activate(psmouse);
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700135 psmouse_dbg(psmouse,
136 "READ REG: 0x%02x is 0x%02x (rc = %d)\n",
137 reg_addr, *reg_val, rc);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700138 return rc;
139}
140
141static 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
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700147 ps2_begin_command(ps2dev);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700148
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)
Tai-hwa Liangd9bae672011-12-23 01:14:31 -0800168 goto out;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700169
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:
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700186 ps2_end_command(ps2dev);
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700187 psmouse_dbg(psmouse,
188 "WRITE REG: 0x%02x to 0x%02x (rc = %d)\n",
189 reg_addr, reg_val, rc);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700190 return rc;
191}
192
193/* Enable register clock gating for writing certain registers */
194static 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
214static 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
Paul Foxc35c0e72012-02-24 00:51:37 -0800220 psmouse_deactivate(psmouse);
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700221
222 ps2_begin_command(ps2dev);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700223
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:
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700244 ps2_end_command(ps2dev);
Paul Foxc35c0e72012-02-24 00:51:37 -0800245 psmouse_activate(psmouse);
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700246 psmouse_dbg(psmouse,
247 "READ PAGE REG: 0x%02x (rc = %d)\n",
248 *reg_val, rc);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700249 return rc;
250}
251
252static 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
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700258 ps2_begin_command(ps2dev);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700259
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)
Tai-hwa Liangd9bae672011-12-23 01:14:31 -0800267 goto out;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700268
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:
Dmitry Torokhov181d6832009-09-16 01:06:43 -0700283 ps2_end_command(ps2dev);
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700284 psmouse_dbg(psmouse,
285 "WRITE PAGE REG: to 0x%02x (rc = %d)\n",
286 reg_val, rc);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700287 return rc;
288}
289
290static 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
298static 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
306static 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
Tai-hwa Liang6ccbcf22011-12-29 09:47:36 -0800316 if (fsp_reg_read(psmouse, FSP_REG_TMOD_STATUS, &val) == -1)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700317 return -EIO;
318
319 *btn = buttons[(val & 0x30) >> 4];
320 return 0;
321}
322
323/* Enable on-pad command tag output */
324static 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) {
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700330 psmouse_err(psmouse, "Unable get OPC state.\n");
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700331 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) {
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700347 psmouse_err(psmouse, "Unable to enable OPC tag.\n");
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700348 res = -EIO;
349 }
350
351 return res;
352}
353
354static 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
375static 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 */
411static ssize_t fsp_attr_set_setreg(struct psmouse *psmouse, void *data,
412 const char *buf, size_t count)
413{
JJ Ding76496e72011-11-09 10:20:14 -0800414 int reg, val;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700415 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
JJ Ding76496e72011-11-09 10:20:14 -0800422 retval = kstrtoint(rest + 1, 16, &val);
423 if (retval)
424 return retval;
425
426 if (val > 0xff)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700427 return -EINVAL;
428
429 if (fsp_reg_write_enable(psmouse, true))
430 return -EIO;
431
432 retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
433
434 fsp_reg_write_enable(psmouse, false);
435
436 return count;
437}
438
439PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
440
441static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
442 void *data, char *buf)
443{
444 struct fsp_data *pad = psmouse->private;
445
446 return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
447}
448
449/*
450 * Read a register from device.
451 *
452 * ex: 0xab -- read content from register 0xab
453 */
454static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
455 const char *buf, size_t count)
456{
457 struct fsp_data *pad = psmouse->private;
JJ Ding76496e72011-11-09 10:20:14 -0800458 int reg, val, err;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700459
JJ Ding76496e72011-11-09 10:20:14 -0800460 err = kstrtoint(buf, 16, &reg);
461 if (err)
462 return err;
463
464 if (reg > 0xff)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700465 return -EINVAL;
466
467 if (fsp_reg_read(psmouse, reg, &val))
468 return -EIO;
469
470 pad->last_reg = reg;
471 pad->last_val = val;
472
473 return count;
474}
475
476PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
477 fsp_attr_show_getreg, fsp_attr_set_getreg);
478
479static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
480 void *data, char *buf)
481{
482 int val = 0;
483
484 if (fsp_page_reg_read(psmouse, &val))
485 return -EIO;
486
487 return sprintf(buf, "%02x\n", val);
488}
489
490static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
491 const char *buf, size_t count)
492{
JJ Ding76496e72011-11-09 10:20:14 -0800493 int val, err;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700494
JJ Ding76496e72011-11-09 10:20:14 -0800495 err = kstrtoint(buf, 16, &val);
496 if (err)
497 return err;
498
499 if (val > 0xff)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700500 return -EINVAL;
501
502 if (fsp_page_reg_write(psmouse, val))
503 return -EIO;
504
505 return count;
506}
507
508PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
509 fsp_attr_show_pagereg, fsp_attr_set_pagereg);
510
511static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
512 void *data, char *buf)
513{
514 struct fsp_data *pad = psmouse->private;
515
516 return sprintf(buf, "%d\n", pad->vscroll);
517}
518
519static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
520 const char *buf, size_t count)
521{
JJ Ding76496e72011-11-09 10:20:14 -0800522 unsigned int val;
523 int err;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700524
JJ Ding76496e72011-11-09 10:20:14 -0800525 err = kstrtouint(buf, 10, &val);
526 if (err)
527 return err;
528
529 if (val > 1)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700530 return -EINVAL;
531
532 fsp_onpad_vscr(psmouse, val);
533
534 return count;
535}
536
537PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
538 fsp_attr_show_vscroll, fsp_attr_set_vscroll);
539
540static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
541 void *data, char *buf)
542{
543 struct fsp_data *pad = psmouse->private;
544
545 return sprintf(buf, "%d\n", pad->hscroll);
546}
547
548static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
549 const char *buf, size_t count)
550{
JJ Ding76496e72011-11-09 10:20:14 -0800551 unsigned int val;
552 int err;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700553
JJ Ding76496e72011-11-09 10:20:14 -0800554 err = kstrtouint(buf, 10, &val);
555 if (err)
556 return err;
557
558 if (val > 1)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700559 return -EINVAL;
560
561 fsp_onpad_hscr(psmouse, val);
562
563 return count;
564}
565
566PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
567 fsp_attr_show_hscroll, fsp_attr_set_hscroll);
568
569static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
570 void *data, char *buf)
571{
572 struct fsp_data *pad = psmouse->private;
573
574 return sprintf(buf, "%c\n",
575 pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
576}
577
578static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
579 const char *buf, size_t count)
580{
581 struct fsp_data *pad = psmouse->private;
582 size_t i;
583
584 for (i = 0; i < count; i++) {
585 switch (buf[i]) {
586 case 'C':
587 pad->flags |= FSPDRV_FLAG_EN_OPC;
588 break;
589 case 'c':
590 pad->flags &= ~FSPDRV_FLAG_EN_OPC;
591 break;
592 default:
593 return -EINVAL;
594 }
595 }
596 return count;
597}
598
599PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
600 fsp_attr_show_flags, fsp_attr_set_flags);
601
602static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
603 void *data, char *buf)
604{
605 return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
606}
607
608PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
609
610static struct attribute *fsp_attributes[] = {
611 &psmouse_attr_setreg.dattr.attr,
612 &psmouse_attr_getreg.dattr.attr,
613 &psmouse_attr_page.dattr.attr,
614 &psmouse_attr_vscroll.dattr.attr,
615 &psmouse_attr_hscroll.dattr.attr,
616 &psmouse_attr_flags.dattr.attr,
617 &psmouse_attr_ver.dattr.attr,
618 NULL
619};
620
621static struct attribute_group fsp_attribute_group = {
622 .attrs = fsp_attributes,
623};
624
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700625#ifdef FSP_DEBUG
626static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700627{
628 static unsigned int ps2_packet_cnt;
629 static unsigned int ps2_last_second;
630 unsigned int jiffies_msec;
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700631 const char *packet_type = "UNKNOWN";
632 unsigned short abs_x = 0, abs_y = 0;
633
634 /* Interpret & dump the packet data. */
635 switch (packet[0] >> FSP_PKT_TYPE_SHIFT) {
636 case FSP_PKT_TYPE_ABS:
637 packet_type = "Absolute";
638 abs_x = GET_ABS_X(packet);
639 abs_y = GET_ABS_Y(packet);
640 break;
641 case FSP_PKT_TYPE_NORMAL:
642 packet_type = "Normal";
643 break;
644 case FSP_PKT_TYPE_NOTIFY:
645 packet_type = "Notify";
646 break;
647 case FSP_PKT_TYPE_NORMAL_OPC:
648 packet_type = "Normal-OPC";
649 break;
650 }
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700651
652 ps2_packet_cnt++;
653 jiffies_msec = jiffies_to_msecs(jiffies);
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700654 psmouse_dbg(psmouse,
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700655 "%08dms %s packets: %02x, %02x, %02x, %02x; "
656 "abs_x: %d, abs_y: %d\n",
657 jiffies_msec, packet_type,
658 packet[0], packet[1], packet[2], packet[3], abs_x, abs_y);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700659
660 if (jiffies_msec - ps2_last_second > 1000) {
Dmitry Torokhovb5d21702011-10-10 18:27:03 -0700661 psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700662 ps2_packet_cnt = 0;
663 ps2_last_second = jiffies_msec;
664 }
665}
666#else
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700667static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700668{
669}
670#endif
671
Tai-hwa Lianga4c85072012-03-25 17:16:36 -0700672static void fsp_set_slot(struct input_dev *dev, int slot, bool active,
673 unsigned int x, unsigned int y)
674{
675 input_mt_slot(dev, slot);
676 input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
677 if (active) {
678 input_report_abs(dev, ABS_MT_POSITION_X, x);
679 input_report_abs(dev, ABS_MT_POSITION_Y, y);
680 }
681}
682
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700683static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
684{
685 struct input_dev *dev = psmouse->dev;
686 struct fsp_data *ad = psmouse->private;
687 unsigned char *packet = psmouse->packet;
688 unsigned char button_status = 0, lscroll = 0, rscroll = 0;
Tai-hwa Lianga4c85072012-03-25 17:16:36 -0700689 unsigned short abs_x, abs_y, fgrs = 0;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700690 int rel_x, rel_y;
691
692 if (psmouse->pktcnt < 4)
693 return PSMOUSE_GOOD_DATA;
694
695 /*
696 * Full packet accumulated, process it
697 */
698
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700699 fsp_packet_debug(psmouse, packet);
700
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700701 switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
702 case FSP_PKT_TYPE_ABS:
Oskari Saarenmaa727f9b42012-03-25 17:17:27 -0700703 abs_x = GET_ABS_X(packet);
704 abs_y = GET_ABS_Y(packet);
Tai-hwa Lianga4c85072012-03-25 17:16:36 -0700705
706 if (packet[0] & FSP_PB0_MFMC) {
707 /*
708 * MFMC packet: assume that there are two fingers on
709 * pad
710 */
711 fgrs = 2;
712
713 /* MFMC packet */
714 if (packet[0] & FSP_PB0_MFMC_FGR2) {
715 /* 2nd finger */
716 if (ad->last_mt_fgr == 2) {
717 /*
718 * workaround for buggy firmware
719 * which doesn't clear MFMC bit if
720 * the 1st finger is up
721 */
722 fgrs = 1;
723 fsp_set_slot(dev, 0, false, 0, 0);
724 }
725 ad->last_mt_fgr = 2;
726
727 fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
728 } else {
729 /* 1st finger */
730 if (ad->last_mt_fgr == 1) {
731 /*
732 * workaround for buggy firmware
733 * which doesn't clear MFMC bit if
734 * the 2nd finger is up
735 */
736 fgrs = 1;
737 fsp_set_slot(dev, 1, false, 0, 0);
738 }
739 ad->last_mt_fgr = 1;
740 fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
741 }
742 } else {
743 /* SFAC packet */
744
745 /* no multi-finger information */
746 ad->last_mt_fgr = 0;
747
748 if (abs_x != 0 && abs_y != 0)
749 fgrs = 1;
750
751 fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
752 fsp_set_slot(dev, 1, false, 0, 0);
753 }
754 if (fgrs > 0) {
755 input_report_abs(dev, ABS_X, abs_x);
756 input_report_abs(dev, ABS_Y, abs_y);
757 }
758 input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
759 input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
760 input_report_key(dev, BTN_TOUCH, fgrs);
761 input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
762 input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700763 break;
764
765 case FSP_PKT_TYPE_NORMAL_OPC:
766 /* on-pad click, filter it if necessary */
767 if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
Tai-hwa Liang7b85f732012-03-25 17:17:00 -0700768 packet[0] &= ~FSP_PB0_LBTN;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700769 /* fall through */
770
771 case FSP_PKT_TYPE_NORMAL:
772 /* normal packet */
773 /* special packet data translation from on-pad packets */
774 if (packet[3] != 0) {
775 if (packet[3] & BIT(0))
776 button_status |= 0x01; /* wheel down */
777 if (packet[3] & BIT(1))
778 button_status |= 0x0f; /* wheel up */
779 if (packet[3] & BIT(2))
Tai-hwa Liangc332e9f2010-01-13 00:25:35 -0800780 button_status |= BIT(4);/* horizontal left */
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700781 if (packet[3] & BIT(3))
Tai-hwa Liangc332e9f2010-01-13 00:25:35 -0800782 button_status |= BIT(5);/* horizontal right */
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700783 /* push back to packet queue */
784 if (button_status != 0)
785 packet[3] = button_status;
786 rscroll = (packet[3] >> 4) & 1;
787 lscroll = (packet[3] >> 5) & 1;
788 }
789 /*
790 * Processing wheel up/down and extra button events
791 */
792 input_report_rel(dev, REL_WHEEL,
793 (int)(packet[3] & 8) - (int)(packet[3] & 7));
794 input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
795 input_report_key(dev, BTN_BACK, lscroll);
796 input_report_key(dev, BTN_FORWARD, rscroll);
797
798 /*
799 * Standard PS/2 Mouse
800 */
801 input_report_key(dev, BTN_LEFT, packet[0] & 1);
802 input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
803 input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
804
805 rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
806 rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
807
808 input_report_rel(dev, REL_X, rel_x);
809 input_report_rel(dev, REL_Y, rel_y);
810 break;
811 }
812
813 input_sync(dev);
814
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700815 return PSMOUSE_FULL_PACKET;
816}
817
818static int fsp_activate_protocol(struct psmouse *psmouse)
819{
820 struct fsp_data *pad = psmouse->private;
821 struct ps2dev *ps2dev = &psmouse->ps2dev;
822 unsigned char param[2];
823 int val;
824
825 /*
826 * Standard procedure to enter FSP Intellimouse mode
827 * (scrolling wheel, 4th and 5th buttons)
828 */
829 param[0] = 200;
830 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
831 param[0] = 200;
832 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
833 param[0] = 80;
834 ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
835
836 ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
837 if (param[0] != 0x04) {
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700838 psmouse_err(psmouse,
839 "Unable to enable 4 bytes packet format.\n");
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700840 return -EIO;
841 }
842
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700843 if (pad->ver < FSP_VER_STL3888_C0) {
844 /* Preparing relative coordinates output for older hardware */
845 if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
846 psmouse_err(psmouse,
847 "Unable to read SYSCTL5 register.\n");
848 return -EIO;
849 }
850
851 if (fsp_get_buttons(psmouse, &pad->buttons)) {
852 psmouse_err(psmouse,
853 "Unable to retrieve number of buttons.\n");
854 return -EIO;
855 }
856
857 val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
858 /* Ensure we are not in absolute mode */
859 val &= ~FSP_BIT_EN_PKT_G0;
860 if (pad->buttons == 0x06) {
861 /* Left/Middle/Right & Scroll Up/Down/Right/Left */
862 val |= FSP_BIT_EN_MSID6;
863 }
864
865 if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
866 psmouse_err(psmouse,
867 "Unable to set up required mode bits.\n");
868 return -EIO;
869 }
870
871 /*
872 * Enable OPC tags such that driver can tell the difference
873 * between on-pad and real button click
874 */
875 if (fsp_opc_tag_enable(psmouse, true))
876 psmouse_warn(psmouse,
877 "Failed to enable OPC tag mode.\n");
878 /* enable on-pad click by default */
879 pad->flags |= FSPDRV_FLAG_EN_OPC;
880
881 /* Enable on-pad vertical and horizontal scrolling */
882 fsp_onpad_vscr(psmouse, true);
883 fsp_onpad_hscr(psmouse, true);
Tai-hwa Lianga4c85072012-03-25 17:16:36 -0700884 } else {
885 /* Enable absolute coordinates output for Cx/Dx hardware */
886 if (fsp_reg_write(psmouse, FSP_REG_SWC1,
887 FSP_BIT_SWC1_EN_ABS_1F |
888 FSP_BIT_SWC1_EN_ABS_2F |
889 FSP_BIT_SWC1_EN_FUP_OUT |
890 FSP_BIT_SWC1_EN_ABS_CON)) {
891 psmouse_err(psmouse,
892 "Unable to enable absolute coordinates output.\n");
893 return -EIO;
894 }
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700895 }
896
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700897 return 0;
898}
899
900static int fsp_set_input_params(struct psmouse *psmouse)
901{
902 struct input_dev *dev = psmouse->dev;
903 struct fsp_data *pad = psmouse->private;
904
905 if (pad->ver < FSP_VER_STL3888_C0) {
906 __set_bit(BTN_MIDDLE, dev->keybit);
907 __set_bit(BTN_BACK, dev->keybit);
908 __set_bit(BTN_FORWARD, dev->keybit);
909 __set_bit(REL_WHEEL, dev->relbit);
910 __set_bit(REL_HWHEEL, dev->relbit);
Tai-hwa Lianga4c85072012-03-25 17:16:36 -0700911 } else {
912 /*
913 * Hardware prior to Cx performs much better in relative mode;
914 * hence, only enable absolute coordinates output as well as
915 * multi-touch output for the newer hardware.
916 *
917 * Maximum coordinates can be computed as:
918 *
919 * number of scanlines * 64 - 57
920 *
921 * where number of X/Y scanline lines are 16/12.
922 */
923 int abs_x = 967, abs_y = 711;
924
925 __set_bit(EV_ABS, dev->evbit);
926 __clear_bit(EV_REL, dev->evbit);
927 __set_bit(BTN_TOUCH, dev->keybit);
928 __set_bit(BTN_TOOL_FINGER, dev->keybit);
929 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
930 __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
931
932 input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
933 input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
934 input_mt_init_slots(dev, 2);
935 input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
936 input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700937 }
938
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700939 return 0;
940}
941
Dmitry Torokhovb7802c52009-09-09 19:13:20 -0700942int fsp_detect(struct psmouse *psmouse, bool set_properties)
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700943{
944 int id;
945
946 if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
947 return -EIO;
948
949 if (id != 0x01)
950 return -ENODEV;
951
952 if (set_properties) {
953 psmouse->vendor = "Sentelic";
954 psmouse->name = "FingerSensingPad";
955 }
956
957 return 0;
958}
959
960static void fsp_reset(struct psmouse *psmouse)
961{
962 fsp_opc_tag_enable(psmouse, false);
963 fsp_onpad_vscr(psmouse, false);
964 fsp_onpad_hscr(psmouse, false);
965}
966
967static void fsp_disconnect(struct psmouse *psmouse)
968{
969 sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
970 &fsp_attribute_group);
971
972 fsp_reset(psmouse);
973 kfree(psmouse->private);
974}
975
976static int fsp_reconnect(struct psmouse *psmouse)
977{
978 int version;
979
980 if (fsp_detect(psmouse, 0))
981 return -ENODEV;
982
983 if (fsp_get_version(psmouse, &version))
984 return -ENODEV;
985
986 if (fsp_activate_protocol(psmouse))
987 return -EIO;
988
989 return 0;
990}
991
992int fsp_init(struct psmouse *psmouse)
993{
994 struct fsp_data *priv;
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700995 int ver, rev;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -0700996 int error;
997
998 if (fsp_get_version(psmouse, &ver) ||
Tai-hwa Liang3ac17802012-03-25 17:15:03 -0700999 fsp_get_revision(psmouse, &rev)) {
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001000 return -ENODEV;
1001 }
1002
Tai-hwa Liang3ac17802012-03-25 17:15:03 -07001003 psmouse_info(psmouse, "Finger Sensing Pad, hw: %d.%d.%d, sw: %s\n",
1004 ver >> 4, ver & 0x0F, rev, fsp_drv_ver);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001005
1006 psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
1007 if (!priv)
1008 return -ENOMEM;
1009
1010 priv->ver = ver;
1011 priv->rev = rev;
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001012
1013 psmouse->protocol_handler = fsp_process_byte;
1014 psmouse->disconnect = fsp_disconnect;
1015 psmouse->reconnect = fsp_reconnect;
1016 psmouse->cleanup = fsp_reset;
1017 psmouse->pktsize = 4;
1018
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001019 error = fsp_activate_protocol(psmouse);
1020 if (error)
1021 goto err_out;
1022
Tai-hwa Liang3ac17802012-03-25 17:15:03 -07001023 /* Set up various supported input event bits */
1024 error = fsp_set_input_params(psmouse);
1025 if (error)
1026 goto err_out;
1027
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001028 error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
1029 &fsp_attribute_group);
1030 if (error) {
Tai-hwa Liang3ac17802012-03-25 17:15:03 -07001031 psmouse_err(psmouse,
1032 "Failed to create sysfs attributes (%d)", error);
Tai-hwa Liangfc69f4a2009-05-10 18:15:39 -07001033 goto err_out;
1034 }
1035
1036 return 0;
1037
1038 err_out:
1039 kfree(psmouse->private);
1040 psmouse->private = NULL;
1041 return error;
1042}