sentelic.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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.0.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_buttons(struct psmouse *psmouse, int *btn)
  254. {
  255. static const int buttons[] = {
  256. 0x16, /* Left/Middle/Right/Forward/Backward & Scroll Up/Down */
  257. 0x06, /* Left/Middle/Right & Scroll Up/Down/Right/Left */
  258. 0x04, /* Left/Middle/Right & Scroll Up/Down */
  259. 0x02, /* Left/Middle/Right */
  260. };
  261. int val;
  262. if (fsp_reg_read(psmouse, FSP_REG_TMOD_STATUS, &val) == -1)
  263. return -EIO;
  264. *btn = buttons[(val & 0x30) >> 4];
  265. return 0;
  266. }
  267. /* Enable on-pad command tag output */
  268. static int fsp_opc_tag_enable(struct psmouse *psmouse, bool enable)
  269. {
  270. int v, nv;
  271. int res = 0;
  272. if (fsp_reg_read(psmouse, FSP_REG_OPC_QDOWN, &v) == -1) {
  273. psmouse_err(psmouse, "Unable get OPC state.\n");
  274. return -EIO;
  275. }
  276. if (enable)
  277. nv = v | FSP_BIT_EN_OPC_TAG;
  278. else
  279. nv = v & ~FSP_BIT_EN_OPC_TAG;
  280. /* only write if necessary */
  281. if (nv != v) {
  282. fsp_reg_write_enable(psmouse, true);
  283. res = fsp_reg_write(psmouse, FSP_REG_OPC_QDOWN, nv);
  284. fsp_reg_write_enable(psmouse, false);
  285. }
  286. if (res != 0) {
  287. psmouse_err(psmouse, "Unable to enable OPC tag.\n");
  288. res = -EIO;
  289. }
  290. return res;
  291. }
  292. static int fsp_onpad_vscr(struct psmouse *psmouse, bool enable)
  293. {
  294. struct fsp_data *pad = psmouse->private;
  295. int val;
  296. if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
  297. return -EIO;
  298. pad->vscroll = enable;
  299. if (enable)
  300. val |= (FSP_BIT_FIX_VSCR | FSP_BIT_ONPAD_ENABLE);
  301. else
  302. val &= ~FSP_BIT_FIX_VSCR;
  303. if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
  304. return -EIO;
  305. return 0;
  306. }
  307. static int fsp_onpad_hscr(struct psmouse *psmouse, bool enable)
  308. {
  309. struct fsp_data *pad = psmouse->private;
  310. int val, v2;
  311. if (fsp_reg_read(psmouse, FSP_REG_ONPAD_CTL, &val))
  312. return -EIO;
  313. if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &v2))
  314. return -EIO;
  315. pad->hscroll = enable;
  316. if (enable) {
  317. val |= (FSP_BIT_FIX_HSCR | FSP_BIT_ONPAD_ENABLE);
  318. v2 |= FSP_BIT_EN_MSID6;
  319. } else {
  320. val &= ~FSP_BIT_FIX_HSCR;
  321. v2 &= ~(FSP_BIT_EN_MSID6 | FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8);
  322. }
  323. if (fsp_reg_write(psmouse, FSP_REG_ONPAD_CTL, val))
  324. return -EIO;
  325. /* reconfigure horizontal scrolling packet output */
  326. if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, v2))
  327. return -EIO;
  328. return 0;
  329. }
  330. /*
  331. * Write device specific initial parameters.
  332. *
  333. * ex: 0xab 0xcd - write oxcd into register 0xab
  334. */
  335. static ssize_t fsp_attr_set_setreg(struct psmouse *psmouse, void *data,
  336. const char *buf, size_t count)
  337. {
  338. int reg, val;
  339. char *rest;
  340. ssize_t retval;
  341. reg = simple_strtoul(buf, &rest, 16);
  342. if (rest == buf || *rest != ' ' || reg > 0xff)
  343. return -EINVAL;
  344. retval = kstrtoint(rest + 1, 16, &val);
  345. if (retval)
  346. return retval;
  347. if (val > 0xff)
  348. return -EINVAL;
  349. if (fsp_reg_write_enable(psmouse, true))
  350. return -EIO;
  351. retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
  352. fsp_reg_write_enable(psmouse, false);
  353. return count;
  354. }
  355. PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
  356. static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
  357. void *data, char *buf)
  358. {
  359. struct fsp_data *pad = psmouse->private;
  360. return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
  361. }
  362. /*
  363. * Read a register from device.
  364. *
  365. * ex: 0xab -- read content from register 0xab
  366. */
  367. static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
  368. const char *buf, size_t count)
  369. {
  370. struct fsp_data *pad = psmouse->private;
  371. int reg, val, err;
  372. err = kstrtoint(buf, 16, &reg);
  373. if (err)
  374. return err;
  375. if (reg > 0xff)
  376. return -EINVAL;
  377. if (fsp_reg_read(psmouse, reg, &val))
  378. return -EIO;
  379. pad->last_reg = reg;
  380. pad->last_val = val;
  381. return count;
  382. }
  383. PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
  384. fsp_attr_show_getreg, fsp_attr_set_getreg);
  385. static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
  386. void *data, char *buf)
  387. {
  388. int val = 0;
  389. if (fsp_page_reg_read(psmouse, &val))
  390. return -EIO;
  391. return sprintf(buf, "%02x\n", val);
  392. }
  393. static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
  394. const char *buf, size_t count)
  395. {
  396. int val, err;
  397. err = kstrtoint(buf, 16, &val);
  398. if (err)
  399. return err;
  400. if (val > 0xff)
  401. return -EINVAL;
  402. if (fsp_page_reg_write(psmouse, val))
  403. return -EIO;
  404. return count;
  405. }
  406. PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
  407. fsp_attr_show_pagereg, fsp_attr_set_pagereg);
  408. static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
  409. void *data, char *buf)
  410. {
  411. struct fsp_data *pad = psmouse->private;
  412. return sprintf(buf, "%d\n", pad->vscroll);
  413. }
  414. static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
  415. const char *buf, size_t count)
  416. {
  417. unsigned int val;
  418. int err;
  419. err = kstrtouint(buf, 10, &val);
  420. if (err)
  421. return err;
  422. if (val > 1)
  423. return -EINVAL;
  424. fsp_onpad_vscr(psmouse, val);
  425. return count;
  426. }
  427. PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
  428. fsp_attr_show_vscroll, fsp_attr_set_vscroll);
  429. static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
  430. void *data, char *buf)
  431. {
  432. struct fsp_data *pad = psmouse->private;
  433. return sprintf(buf, "%d\n", pad->hscroll);
  434. }
  435. static ssize_t fsp_attr_set_hscroll(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_hscr(psmouse, val);
  446. return count;
  447. }
  448. PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
  449. fsp_attr_show_hscroll, fsp_attr_set_hscroll);
  450. static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
  451. void *data, char *buf)
  452. {
  453. struct fsp_data *pad = psmouse->private;
  454. return sprintf(buf, "%c\n",
  455. pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
  456. }
  457. static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
  458. const char *buf, size_t count)
  459. {
  460. struct fsp_data *pad = psmouse->private;
  461. size_t i;
  462. for (i = 0; i < count; i++) {
  463. switch (buf[i]) {
  464. case 'C':
  465. pad->flags |= FSPDRV_FLAG_EN_OPC;
  466. break;
  467. case 'c':
  468. pad->flags &= ~FSPDRV_FLAG_EN_OPC;
  469. break;
  470. default:
  471. return -EINVAL;
  472. }
  473. }
  474. return count;
  475. }
  476. PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
  477. fsp_attr_show_flags, fsp_attr_set_flags);
  478. static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
  479. void *data, char *buf)
  480. {
  481. return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
  482. }
  483. PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
  484. static struct attribute *fsp_attributes[] = {
  485. &psmouse_attr_setreg.dattr.attr,
  486. &psmouse_attr_getreg.dattr.attr,
  487. &psmouse_attr_page.dattr.attr,
  488. &psmouse_attr_vscroll.dattr.attr,
  489. &psmouse_attr_hscroll.dattr.attr,
  490. &psmouse_attr_flags.dattr.attr,
  491. &psmouse_attr_ver.dattr.attr,
  492. NULL
  493. };
  494. static struct attribute_group fsp_attribute_group = {
  495. .attrs = fsp_attributes,
  496. };
  497. #ifdef FSP_DEBUG
  498. static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
  499. {
  500. static unsigned int ps2_packet_cnt;
  501. static unsigned int ps2_last_second;
  502. unsigned int jiffies_msec;
  503. const char *packet_type = "UNKNOWN";
  504. unsigned short abs_x = 0, abs_y = 0;
  505. /* Interpret & dump the packet data. */
  506. switch (packet[0] >> FSP_PKT_TYPE_SHIFT) {
  507. case FSP_PKT_TYPE_ABS:
  508. packet_type = "Absolute";
  509. abs_x = GET_ABS_X(packet);
  510. abs_y = GET_ABS_Y(packet);
  511. break;
  512. case FSP_PKT_TYPE_NORMAL:
  513. packet_type = "Normal";
  514. break;
  515. case FSP_PKT_TYPE_NOTIFY:
  516. packet_type = "Notify";
  517. break;
  518. case FSP_PKT_TYPE_NORMAL_OPC:
  519. packet_type = "Normal-OPC";
  520. break;
  521. }
  522. ps2_packet_cnt++;
  523. jiffies_msec = jiffies_to_msecs(jiffies);
  524. psmouse_dbg(psmouse,
  525. "%08dms %s packets: %02x, %02x, %02x, %02x; "
  526. "abs_x: %d, abs_y: %d\n",
  527. jiffies_msec, packet_type,
  528. packet[0], packet[1], packet[2], packet[3], abs_x, abs_y);
  529. if (jiffies_msec - ps2_last_second > 1000) {
  530. psmouse_dbg(psmouse, "PS/2 packets/sec = %d\n", ps2_packet_cnt);
  531. ps2_packet_cnt = 0;
  532. ps2_last_second = jiffies_msec;
  533. }
  534. }
  535. #else
  536. static void fsp_packet_debug(struct psmouse *psmouse, unsigned char packet[])
  537. {
  538. }
  539. #endif
  540. static void fsp_set_slot(struct input_dev *dev, int slot, bool active,
  541. unsigned int x, unsigned int y)
  542. {
  543. input_mt_slot(dev, slot);
  544. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  545. if (active) {
  546. input_report_abs(dev, ABS_MT_POSITION_X, x);
  547. input_report_abs(dev, ABS_MT_POSITION_Y, y);
  548. }
  549. }
  550. static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
  551. {
  552. struct input_dev *dev = psmouse->dev;
  553. struct fsp_data *ad = psmouse->private;
  554. unsigned char *packet = psmouse->packet;
  555. unsigned char button_status = 0, lscroll = 0, rscroll = 0;
  556. unsigned short abs_x, abs_y, fgrs = 0;
  557. int rel_x, rel_y;
  558. if (psmouse->pktcnt < 4)
  559. return PSMOUSE_GOOD_DATA;
  560. /*
  561. * Full packet accumulated, process it
  562. */
  563. fsp_packet_debug(psmouse, packet);
  564. switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
  565. case FSP_PKT_TYPE_ABS:
  566. abs_x = GET_ABS_X(packet);
  567. abs_y = GET_ABS_Y(packet);
  568. if (packet[0] & FSP_PB0_MFMC) {
  569. /*
  570. * MFMC packet: assume that there are two fingers on
  571. * pad
  572. */
  573. fgrs = 2;
  574. /* MFMC packet */
  575. if (packet[0] & FSP_PB0_MFMC_FGR2) {
  576. /* 2nd finger */
  577. if (ad->last_mt_fgr == 2) {
  578. /*
  579. * workaround for buggy firmware
  580. * which doesn't clear MFMC bit if
  581. * the 1st finger is up
  582. */
  583. fgrs = 1;
  584. fsp_set_slot(dev, 0, false, 0, 0);
  585. }
  586. ad->last_mt_fgr = 2;
  587. fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
  588. } else {
  589. /* 1st finger */
  590. if (ad->last_mt_fgr == 1) {
  591. /*
  592. * workaround for buggy firmware
  593. * which doesn't clear MFMC bit if
  594. * the 2nd finger is up
  595. */
  596. fgrs = 1;
  597. fsp_set_slot(dev, 1, false, 0, 0);
  598. }
  599. ad->last_mt_fgr = 1;
  600. fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
  601. }
  602. } else {
  603. /* SFAC packet */
  604. if ((packet[0] & (FSP_PB0_LBTN|FSP_PB0_PHY_BTN)) ==
  605. FSP_PB0_LBTN) {
  606. /* On-pad click in SFAC mode should be handled
  607. * by userspace. On-pad clicks in MFMC mode
  608. * are real clickpad clicks, and not ignored.
  609. */
  610. packet[0] &= ~FSP_PB0_LBTN;
  611. }
  612. /* no multi-finger information */
  613. ad->last_mt_fgr = 0;
  614. if (abs_x != 0 && abs_y != 0)
  615. fgrs = 1;
  616. fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
  617. fsp_set_slot(dev, 1, false, 0, 0);
  618. }
  619. if (fgrs == 1 || (fgrs == 2 && !(packet[0] & FSP_PB0_MFMC_FGR2))) {
  620. input_report_abs(dev, ABS_X, abs_x);
  621. input_report_abs(dev, ABS_Y, abs_y);
  622. }
  623. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  624. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  625. input_report_key(dev, BTN_TOUCH, fgrs);
  626. input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
  627. input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
  628. break;
  629. case FSP_PKT_TYPE_NORMAL_OPC:
  630. /* on-pad click, filter it if necessary */
  631. if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
  632. packet[0] &= ~FSP_PB0_LBTN;
  633. /* fall through */
  634. case FSP_PKT_TYPE_NORMAL:
  635. /* normal packet */
  636. /* special packet data translation from on-pad packets */
  637. if (packet[3] != 0) {
  638. if (packet[3] & BIT(0))
  639. button_status |= 0x01; /* wheel down */
  640. if (packet[3] & BIT(1))
  641. button_status |= 0x0f; /* wheel up */
  642. if (packet[3] & BIT(2))
  643. button_status |= BIT(4);/* horizontal left */
  644. if (packet[3] & BIT(3))
  645. button_status |= BIT(5);/* horizontal right */
  646. /* push back to packet queue */
  647. if (button_status != 0)
  648. packet[3] = button_status;
  649. rscroll = (packet[3] >> 4) & 1;
  650. lscroll = (packet[3] >> 5) & 1;
  651. }
  652. /*
  653. * Processing wheel up/down and extra button events
  654. */
  655. input_report_rel(dev, REL_WHEEL,
  656. (int)(packet[3] & 8) - (int)(packet[3] & 7));
  657. input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
  658. input_report_key(dev, BTN_BACK, lscroll);
  659. input_report_key(dev, BTN_FORWARD, rscroll);
  660. /*
  661. * Standard PS/2 Mouse
  662. */
  663. input_report_key(dev, BTN_LEFT, packet[0] & 1);
  664. input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
  665. input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
  666. rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
  667. rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
  668. input_report_rel(dev, REL_X, rel_x);
  669. input_report_rel(dev, REL_Y, rel_y);
  670. break;
  671. }
  672. input_sync(dev);
  673. return PSMOUSE_FULL_PACKET;
  674. }
  675. static int fsp_activate_protocol(struct psmouse *psmouse)
  676. {
  677. struct fsp_data *pad = psmouse->private;
  678. struct ps2dev *ps2dev = &psmouse->ps2dev;
  679. unsigned char param[2];
  680. int val;
  681. /*
  682. * Standard procedure to enter FSP Intellimouse mode
  683. * (scrolling wheel, 4th and 5th buttons)
  684. */
  685. param[0] = 200;
  686. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  687. param[0] = 200;
  688. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  689. param[0] = 80;
  690. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  691. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  692. if (param[0] != 0x04) {
  693. psmouse_err(psmouse,
  694. "Unable to enable 4 bytes packet format.\n");
  695. return -EIO;
  696. }
  697. if (pad->ver < FSP_VER_STL3888_C0) {
  698. /* Preparing relative coordinates output for older hardware */
  699. if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
  700. psmouse_err(psmouse,
  701. "Unable to read SYSCTL5 register.\n");
  702. return -EIO;
  703. }
  704. if (fsp_get_buttons(psmouse, &pad->buttons)) {
  705. psmouse_err(psmouse,
  706. "Unable to retrieve number of buttons.\n");
  707. return -EIO;
  708. }
  709. val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
  710. /* Ensure we are not in absolute mode */
  711. val &= ~FSP_BIT_EN_PKT_G0;
  712. if (pad->buttons == 0x06) {
  713. /* Left/Middle/Right & Scroll Up/Down/Right/Left */
  714. val |= FSP_BIT_EN_MSID6;
  715. }
  716. if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
  717. psmouse_err(psmouse,
  718. "Unable to set up required mode bits.\n");
  719. return -EIO;
  720. }
  721. /*
  722. * Enable OPC tags such that driver can tell the difference
  723. * between on-pad and real button click
  724. */
  725. if (fsp_opc_tag_enable(psmouse, true))
  726. psmouse_warn(psmouse,
  727. "Failed to enable OPC tag mode.\n");
  728. /* enable on-pad click by default */
  729. pad->flags |= FSPDRV_FLAG_EN_OPC;
  730. /* Enable on-pad vertical and horizontal scrolling */
  731. fsp_onpad_vscr(psmouse, true);
  732. fsp_onpad_hscr(psmouse, true);
  733. } else {
  734. /* Enable absolute coordinates output for Cx/Dx hardware */
  735. if (fsp_reg_write(psmouse, FSP_REG_SWC1,
  736. FSP_BIT_SWC1_EN_ABS_1F |
  737. FSP_BIT_SWC1_EN_ABS_2F |
  738. FSP_BIT_SWC1_EN_FUP_OUT |
  739. FSP_BIT_SWC1_EN_ABS_CON)) {
  740. psmouse_err(psmouse,
  741. "Unable to enable absolute coordinates output.\n");
  742. return -EIO;
  743. }
  744. }
  745. return 0;
  746. }
  747. static int fsp_set_input_params(struct psmouse *psmouse)
  748. {
  749. struct input_dev *dev = psmouse->dev;
  750. struct fsp_data *pad = psmouse->private;
  751. if (pad->ver < FSP_VER_STL3888_C0) {
  752. __set_bit(BTN_MIDDLE, dev->keybit);
  753. __set_bit(BTN_BACK, dev->keybit);
  754. __set_bit(BTN_FORWARD, dev->keybit);
  755. __set_bit(REL_WHEEL, dev->relbit);
  756. __set_bit(REL_HWHEEL, dev->relbit);
  757. } else {
  758. /*
  759. * Hardware prior to Cx performs much better in relative mode;
  760. * hence, only enable absolute coordinates output as well as
  761. * multi-touch output for the newer hardware.
  762. *
  763. * Maximum coordinates can be computed as:
  764. *
  765. * number of scanlines * 64 - 57
  766. *
  767. * where number of X/Y scanline lines are 16/12.
  768. */
  769. int abs_x = 967, abs_y = 711;
  770. __set_bit(EV_ABS, dev->evbit);
  771. __clear_bit(EV_REL, dev->evbit);
  772. __set_bit(BTN_TOUCH, dev->keybit);
  773. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  774. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  775. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  776. input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
  777. input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
  778. input_mt_init_slots(dev, 2);
  779. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
  780. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
  781. }
  782. return 0;
  783. }
  784. int fsp_detect(struct psmouse *psmouse, bool set_properties)
  785. {
  786. int id;
  787. if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
  788. return -EIO;
  789. if (id != 0x01)
  790. return -ENODEV;
  791. if (set_properties) {
  792. psmouse->vendor = "Sentelic";
  793. psmouse->name = "FingerSensingPad";
  794. }
  795. return 0;
  796. }
  797. static void fsp_reset(struct psmouse *psmouse)
  798. {
  799. fsp_opc_tag_enable(psmouse, false);
  800. fsp_onpad_vscr(psmouse, false);
  801. fsp_onpad_hscr(psmouse, false);
  802. }
  803. static void fsp_disconnect(struct psmouse *psmouse)
  804. {
  805. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  806. &fsp_attribute_group);
  807. fsp_reset(psmouse);
  808. kfree(psmouse->private);
  809. }
  810. static int fsp_reconnect(struct psmouse *psmouse)
  811. {
  812. int version;
  813. if (fsp_detect(psmouse, 0))
  814. return -ENODEV;
  815. if (fsp_get_version(psmouse, &version))
  816. return -ENODEV;
  817. if (fsp_activate_protocol(psmouse))
  818. return -EIO;
  819. return 0;
  820. }
  821. int fsp_init(struct psmouse *psmouse)
  822. {
  823. struct fsp_data *priv;
  824. int ver, rev;
  825. int error;
  826. if (fsp_get_version(psmouse, &ver) ||
  827. fsp_get_revision(psmouse, &rev)) {
  828. return -ENODEV;
  829. }
  830. psmouse_info(psmouse, "Finger Sensing Pad, hw: %d.%d.%d, sw: %s\n",
  831. ver >> 4, ver & 0x0F, rev, fsp_drv_ver);
  832. psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
  833. if (!priv)
  834. return -ENOMEM;
  835. priv->ver = ver;
  836. priv->rev = rev;
  837. psmouse->protocol_handler = fsp_process_byte;
  838. psmouse->disconnect = fsp_disconnect;
  839. psmouse->reconnect = fsp_reconnect;
  840. psmouse->cleanup = fsp_reset;
  841. psmouse->pktsize = 4;
  842. error = fsp_activate_protocol(psmouse);
  843. if (error)
  844. goto err_out;
  845. /* Set up various supported input event bits */
  846. error = fsp_set_input_params(psmouse);
  847. if (error)
  848. goto err_out;
  849. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  850. &fsp_attribute_group);
  851. if (error) {
  852. psmouse_err(psmouse,
  853. "Failed to create sysfs attributes (%d)", error);
  854. goto err_out;
  855. }
  856. return 0;
  857. err_out:
  858. kfree(psmouse->private);
  859. psmouse->private = NULL;
  860. return error;
  861. }