sentelic.c 26 KB

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