sentelic.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. 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 = kstrtoint(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. int reg, val, err;
  391. err = kstrtoint(buf, 16, &reg);
  392. if (err)
  393. return err;
  394. if (reg > 0xff)
  395. return -EINVAL;
  396. if (fsp_reg_read(psmouse, reg, &val))
  397. return -EIO;
  398. pad->last_reg = reg;
  399. pad->last_val = val;
  400. return count;
  401. }
  402. PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
  403. fsp_attr_show_getreg, fsp_attr_set_getreg);
  404. static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
  405. void *data, char *buf)
  406. {
  407. int val = 0;
  408. if (fsp_page_reg_read(psmouse, &val))
  409. return -EIO;
  410. return sprintf(buf, "%02x\n", val);
  411. }
  412. static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
  413. const char *buf, size_t count)
  414. {
  415. int val, err;
  416. err = kstrtoint(buf, 16, &val);
  417. if (err)
  418. return err;
  419. if (val > 0xff)
  420. return -EINVAL;
  421. if (fsp_page_reg_write(psmouse, val))
  422. return -EIO;
  423. return count;
  424. }
  425. PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
  426. fsp_attr_show_pagereg, fsp_attr_set_pagereg);
  427. static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
  428. void *data, char *buf)
  429. {
  430. struct fsp_data *pad = psmouse->private;
  431. return sprintf(buf, "%d\n", pad->vscroll);
  432. }
  433. static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
  434. const char *buf, size_t count)
  435. {
  436. unsigned int val;
  437. int err;
  438. err = kstrtouint(buf, 10, &val);
  439. if (err)
  440. return err;
  441. if (val > 1)
  442. return -EINVAL;
  443. fsp_onpad_vscr(psmouse, val);
  444. return count;
  445. }
  446. PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
  447. fsp_attr_show_vscroll, fsp_attr_set_vscroll);
  448. static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
  449. void *data, char *buf)
  450. {
  451. struct fsp_data *pad = psmouse->private;
  452. return sprintf(buf, "%d\n", pad->hscroll);
  453. }
  454. static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
  455. const char *buf, size_t count)
  456. {
  457. unsigned int val;
  458. int err;
  459. err = kstrtouint(buf, 10, &val);
  460. if (err)
  461. return err;
  462. if (val > 1)
  463. return -EINVAL;
  464. fsp_onpad_hscr(psmouse, val);
  465. return count;
  466. }
  467. PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
  468. fsp_attr_show_hscroll, fsp_attr_set_hscroll);
  469. static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
  470. void *data, char *buf)
  471. {
  472. struct fsp_data *pad = psmouse->private;
  473. return sprintf(buf, "%c\n",
  474. pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
  475. }
  476. static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
  477. const char *buf, size_t count)
  478. {
  479. struct fsp_data *pad = psmouse->private;
  480. size_t i;
  481. for (i = 0; i < count; i++) {
  482. switch (buf[i]) {
  483. case 'C':
  484. pad->flags |= FSPDRV_FLAG_EN_OPC;
  485. break;
  486. case 'c':
  487. pad->flags &= ~FSPDRV_FLAG_EN_OPC;
  488. break;
  489. default:
  490. return -EINVAL;
  491. }
  492. }
  493. return count;
  494. }
  495. PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
  496. fsp_attr_show_flags, fsp_attr_set_flags);
  497. static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
  498. void *data, char *buf)
  499. {
  500. return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
  501. }
  502. PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
  503. static struct attribute *fsp_attributes[] = {
  504. &psmouse_attr_setreg.dattr.attr,
  505. &psmouse_attr_getreg.dattr.attr,
  506. &psmouse_attr_page.dattr.attr,
  507. &psmouse_attr_vscroll.dattr.attr,
  508. &psmouse_attr_hscroll.dattr.attr,
  509. &psmouse_attr_flags.dattr.attr,
  510. &psmouse_attr_ver.dattr.attr,
  511. NULL
  512. };
  513. static struct attribute_group fsp_attribute_group = {
  514. .attrs = fsp_attributes,
  515. };
  516. #ifdef FSP_DEBUG
  517. static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
  518. {
  519. static unsigned int ps2_packet_cnt;
  520. static unsigned int ps2_last_second;
  521. unsigned int jiffies_msec;
  522. const char *packet_type = "UNKNOWN";
  523. unsigned short abs_x = 0, abs_y = 0;
  524. /* Interpret & dump the packet data. */
  525. switch (packet[0] >> FSP_PKT_TYPE_SHIFT) {
  526. case FSP_PKT_TYPE_ABS:
  527. packet_type = "Absolute";
  528. abs_x = GET_ABS_X(packet);
  529. abs_y = GET_ABS_Y(packet);
  530. break;
  531. case FSP_PKT_TYPE_NORMAL:
  532. packet_type = "Normal";
  533. break;
  534. case FSP_PKT_TYPE_NOTIFY:
  535. packet_type = "Notify";
  536. break;
  537. case FSP_PKT_TYPE_NORMAL_OPC:
  538. packet_type = "Normal-OPC";
  539. break;
  540. }
  541. ps2_packet_cnt++;
  542. jiffies_msec = jiffies_to_msecs(jiffies);
  543. psmouse_dbg(psmouse,
  544. "%08dms %s packets: %02x, %02x, %02x, %02x; "
  545. "abs_x: %d, abs_y: %d\n",
  546. jiffies_msec, packet_type,
  547. packet[0], packet[1], packet[2], packet[3], abs_x, abs_y);
  548. if (jiffies_msec - ps2_last_second > 1000) {
  549. psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
  550. ps2_packet_cnt = 0;
  551. ps2_last_second = jiffies_msec;
  552. }
  553. }
  554. #else
  555. static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
  556. {
  557. }
  558. #endif
  559. static void fsp_set_slot(struct input_dev *dev, int slot, bool active,
  560. unsigned int x, unsigned int y)
  561. {
  562. input_mt_slot(dev, slot);
  563. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  564. if (active) {
  565. input_report_abs(dev, ABS_MT_POSITION_X, x);
  566. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  567. }
  568. }
  569. static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
  570. {
  571. struct input_dev *dev = psmouse->dev;
  572. struct fsp_data *ad = psmouse->private;
  573. unsigned char *packet = psmouse->packet;
  574. unsigned char button_status = 0, lscroll = 0, rscroll = 0;
  575. unsigned short abs_x, abs_y, fgrs = 0;
  576. int rel_x, rel_y;
  577. if (psmouse->pktcnt < 4)
  578. return PSMOUSE_GOOD_DATA;
  579. /*
  580. * Full packet accumulated, process it
  581. */
  582. fsp_packet_debug(psmouse, packet);
  583. switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
  584. case FSP_PKT_TYPE_ABS:
  585. if ((packet[0] == 0x48 || packet[0] == 0x49) &&
  586. packet[1] == 0 && packet[2] == 0) {
  587. /*
  588. * Ignore coordinate noise when finger leaving the
  589. * surface, otherwise cursor may jump to upper-left
  590. * corner.
  591. */
  592. packet[3] &= 0xf0;
  593. }
  594. abs_x = GET_ABS_X(packet);
  595. abs_y = GET_ABS_Y(packet);
  596. if (packet[0] & FSP_PB0_MFMC) {
  597. /*
  598. * MFMC packet: assume that there are two fingers on
  599. * pad
  600. */
  601. fgrs = 2;
  602. /* MFMC packet */
  603. if (packet[0] & FSP_PB0_MFMC_FGR2) {
  604. /* 2nd finger */
  605. if (ad->last_mt_fgr == 2) {
  606. /*
  607. * workaround for buggy firmware
  608. * which doesn't clear MFMC bit if
  609. * the 1st finger is up
  610. */
  611. fgrs = 1;
  612. fsp_set_slot(dev, 0, false, 0, 0);
  613. }
  614. ad->last_mt_fgr = 2;
  615. fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
  616. } else {
  617. /* 1st finger */
  618. if (ad->last_mt_fgr == 1) {
  619. /*
  620. * workaround for buggy firmware
  621. * which doesn't clear MFMC bit if
  622. * the 2nd finger is up
  623. */
  624. fgrs = 1;
  625. fsp_set_slot(dev, 1, false, 0, 0);
  626. }
  627. ad->last_mt_fgr = 1;
  628. fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
  629. }
  630. } else {
  631. /* SFAC packet */
  632. if ((packet[0] & (FSP_PB0_LBTN|FSP_PB0_PHY_BTN)) ==
  633. FSP_PB0_LBTN) {
  634. /* On-pad click in SFAC mode should be handled
  635. * by userspace. On-pad clicks in MFMC mode
  636. * are real clickpad clicks, and not ignored.
  637. */
  638. packet[0] &= ~FSP_PB0_LBTN;
  639. }
  640. /* no multi-finger information */
  641. ad->last_mt_fgr = 0;
  642. if (abs_x != 0 && abs_y != 0)
  643. fgrs = 1;
  644. fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
  645. fsp_set_slot(dev, 1, false, 0, 0);
  646. }
  647. if (fgrs == 1 || (fgrs == 2 && !(packet[0] & FSP_PB0_MFMC_FGR2))) {
  648. input_report_abs(dev, ABS_X, abs_x);
  649. input_report_abs(dev, ABS_Y, abs_y);
  650. }
  651. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  652. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  653. input_report_key(dev, BTN_TOUCH, fgrs);
  654. input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
  655. input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
  656. break;
  657. case FSP_PKT_TYPE_NORMAL_OPC:
  658. /* on-pad click, filter it if necessary */
  659. if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
  660. packet[0] &= ~FSP_PB0_LBTN;
  661. /* fall through */
  662. case FSP_PKT_TYPE_NORMAL:
  663. /* normal packet */
  664. /* special packet data translation from on-pad packets */
  665. if (packet[3] != 0) {
  666. if (packet[3] & BIT(0))
  667. button_status |= 0x01; /* wheel down */
  668. if (packet[3] & BIT(1))
  669. button_status |= 0x0f; /* wheel up */
  670. if (packet[3] & BIT(2))
  671. button_status |= BIT(4);/* horizontal left */
  672. if (packet[3] & BIT(3))
  673. button_status |= BIT(5);/* horizontal right */
  674. /* push back to packet queue */
  675. if (button_status != 0)
  676. packet[3] = button_status;
  677. rscroll = (packet[3] >> 4) & 1;
  678. lscroll = (packet[3] >> 5) & 1;
  679. }
  680. /*
  681. * Processing wheel up/down and extra button events
  682. */
  683. input_report_rel(dev, REL_WHEEL,
  684. (int)(packet[3] & 8) - (int)(packet[3] & 7));
  685. input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
  686. input_report_key(dev, BTN_BACK, lscroll);
  687. input_report_key(dev, BTN_FORWARD, rscroll);
  688. /*
  689. * Standard PS/2 Mouse
  690. */
  691. input_report_key(dev, BTN_LEFT, packet[0] & 1);
  692. input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
  693. input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
  694. rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
  695. rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
  696. input_report_rel(dev, REL_X, rel_x);
  697. input_report_rel(dev, REL_Y, rel_y);
  698. break;
  699. }
  700. input_sync(dev);
  701. return PSMOUSE_FULL_PACKET;
  702. }
  703. static int fsp_activate_protocol(struct psmouse *psmouse)
  704. {
  705. struct fsp_data *pad = psmouse->private;
  706. struct ps2dev *ps2dev = &psmouse->ps2dev;
  707. unsigned char param[2];
  708. int val;
  709. /*
  710. * Standard procedure to enter FSP Intellimouse mode
  711. * (scrolling wheel, 4th and 5th buttons)
  712. */
  713. param[0] = 200;
  714. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  715. param[0] = 200;
  716. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  717. param[0] = 80;
  718. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  719. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  720. if (param[0] != 0x04) {
  721. psmouse_err(psmouse,
  722. "Unable to enable 4 bytes packet format.\n");
  723. return -EIO;
  724. }
  725. if (pad->ver < FSP_VER_STL3888_C0) {
  726. /* Preparing relative coordinates output for older hardware */
  727. if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
  728. psmouse_err(psmouse,
  729. "Unable to read SYSCTL5 register.\n");
  730. return -EIO;
  731. }
  732. if (fsp_get_buttons(psmouse, &pad->buttons)) {
  733. psmouse_err(psmouse,
  734. "Unable to retrieve number of buttons.\n");
  735. return -EIO;
  736. }
  737. val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
  738. /* Ensure we are not in absolute mode */
  739. val &= ~FSP_BIT_EN_PKT_G0;
  740. if (pad->buttons == 0x06) {
  741. /* Left/Middle/Right & Scroll Up/Down/Right/Left */
  742. val |= FSP_BIT_EN_MSID6;
  743. }
  744. if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
  745. psmouse_err(psmouse,
  746. "Unable to set up required mode bits.\n");
  747. return -EIO;
  748. }
  749. /*
  750. * Enable OPC tags such that driver can tell the difference
  751. * between on-pad and real button click
  752. */
  753. if (fsp_opc_tag_enable(psmouse, true))
  754. psmouse_warn(psmouse,
  755. "Failed to enable OPC tag mode.\n");
  756. /* enable on-pad click by default */
  757. pad->flags |= FSPDRV_FLAG_EN_OPC;
  758. /* Enable on-pad vertical and horizontal scrolling */
  759. fsp_onpad_vscr(psmouse, true);
  760. fsp_onpad_hscr(psmouse, true);
  761. } else {
  762. /* Enable absolute coordinates output for Cx/Dx hardware */
  763. if (fsp_reg_write(psmouse, FSP_REG_SWC1,
  764. FSP_BIT_SWC1_EN_ABS_1F |
  765. FSP_BIT_SWC1_EN_ABS_2F |
  766. FSP_BIT_SWC1_EN_FUP_OUT |
  767. FSP_BIT_SWC1_EN_ABS_CON)) {
  768. psmouse_err(psmouse,
  769. "Unable to enable absolute coordinates output.\n");
  770. return -EIO;
  771. }
  772. }
  773. return 0;
  774. }
  775. static int fsp_set_input_params(struct psmouse *psmouse)
  776. {
  777. struct input_dev *dev = psmouse->dev;
  778. struct fsp_data *pad = psmouse->private;
  779. if (pad->ver < FSP_VER_STL3888_C0) {
  780. __set_bit(BTN_MIDDLE, dev->keybit);
  781. __set_bit(BTN_BACK, dev->keybit);
  782. __set_bit(BTN_FORWARD, dev->keybit);
  783. __set_bit(REL_WHEEL, dev->relbit);
  784. __set_bit(REL_HWHEEL, dev->relbit);
  785. } else {
  786. /*
  787. * Hardware prior to Cx performs much better in relative mode;
  788. * hence, only enable absolute coordinates output as well as
  789. * multi-touch output for the newer hardware.
  790. *
  791. * Maximum coordinates can be computed as:
  792. *
  793. * number of scanlines * 64 - 57
  794. *
  795. * where number of X/Y scanline lines are 16/12.
  796. */
  797. int abs_x = 967, abs_y = 711;
  798. __set_bit(EV_ABS, dev->evbit);
  799. __clear_bit(EV_REL, dev->evbit);
  800. __set_bit(BTN_TOUCH, dev->keybit);
  801. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  802. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  803. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  804. input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
  805. input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
  806. input_mt_init_slots(dev, 2, 0);
  807. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
  808. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
  809. }
  810. return 0;
  811. }
  812. int fsp_detect(struct psmouse *psmouse, bool set_properties)
  813. {
  814. int id;
  815. if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
  816. return -EIO;
  817. if (id != 0x01)
  818. return -ENODEV;
  819. if (set_properties) {
  820. psmouse->vendor = "Sentelic";
  821. psmouse->name = "FingerSensingPad";
  822. }
  823. return 0;
  824. }
  825. static void fsp_reset(struct psmouse *psmouse)
  826. {
  827. fsp_opc_tag_enable(psmouse, false);
  828. fsp_onpad_vscr(psmouse, false);
  829. fsp_onpad_hscr(psmouse, false);
  830. }
  831. static void fsp_disconnect(struct psmouse *psmouse)
  832. {
  833. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  834. &fsp_attribute_group);
  835. fsp_reset(psmouse);
  836. kfree(psmouse->private);
  837. }
  838. static int fsp_reconnect(struct psmouse *psmouse)
  839. {
  840. int version;
  841. if (fsp_detect(psmouse, 0))
  842. return -ENODEV;
  843. if (fsp_get_version(psmouse, &version))
  844. return -ENODEV;
  845. if (fsp_activate_protocol(psmouse))
  846. return -EIO;
  847. return 0;
  848. }
  849. int fsp_init(struct psmouse *psmouse)
  850. {
  851. struct fsp_data *priv;
  852. int ver, rev, sn = 0;
  853. int error;
  854. if (fsp_get_version(psmouse, &ver) ||
  855. fsp_get_revision(psmouse, &rev)) {
  856. return -ENODEV;
  857. }
  858. if (ver >= FSP_VER_STL3888_C0) {
  859. /* firmware information is only available since C0 */
  860. fsp_get_sn(psmouse, &sn);
  861. }
  862. psmouse_info(psmouse,
  863. "Finger Sensing Pad, hw: %d.%d.%d, sn: %x, sw: %s\n",
  864. ver >> 4, ver & 0x0F, rev, sn, fsp_drv_ver);
  865. psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
  866. if (!priv)
  867. return -ENOMEM;
  868. priv->ver = ver;
  869. priv->rev = rev;
  870. psmouse->protocol_handler = fsp_process_byte;
  871. psmouse->disconnect = fsp_disconnect;
  872. psmouse->reconnect = fsp_reconnect;
  873. psmouse->cleanup = fsp_reset;
  874. psmouse->pktsize = 4;
  875. error = fsp_activate_protocol(psmouse);
  876. if (error)
  877. goto err_out;
  878. /* Set up various supported input event bits */
  879. error = fsp_set_input_params(psmouse);
  880. if (error)
  881. goto err_out;
  882. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  883. &fsp_attribute_group);
  884. if (error) {
  885. psmouse_err(psmouse,
  886. "Failed to create sysfs attributes (%d)", error);
  887. goto err_out;
  888. }
  889. return 0;
  890. err_out:
  891. kfree(psmouse->private);
  892. psmouse->private = NULL;
  893. return error;
  894. }