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