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