sentelic.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. int rel_x, rel_y;
  579. if (psmouse->pktcnt < 4)
  580. return PSMOUSE_GOOD_DATA;
  581. /*
  582. * Full packet accumulated, process it
  583. */
  584. fsp_packet_debug(psmouse, packet);
  585. switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
  586. case FSP_PKT_TYPE_ABS:
  587. if ((packet[0] == 0x48 || packet[0] == 0x49) &&
  588. packet[1] == 0 && packet[2] == 0) {
  589. /*
  590. * Ignore coordinate noise when finger leaving the
  591. * surface, otherwise cursor may jump to upper-left
  592. * corner.
  593. */
  594. packet[3] &= 0xf0;
  595. }
  596. abs_x = GET_ABS_X(packet);
  597. abs_y = GET_ABS_Y(packet);
  598. if (packet[0] & FSP_PB0_MFMC) {
  599. /*
  600. * MFMC packet: assume that there are two fingers on
  601. * pad
  602. */
  603. fgrs = 2;
  604. /* MFMC packet */
  605. if (packet[0] & FSP_PB0_MFMC_FGR2) {
  606. /* 2nd finger */
  607. if (ad->last_mt_fgr == 2) {
  608. /*
  609. * workaround for buggy firmware
  610. * which doesn't clear MFMC bit if
  611. * the 1st finger is up
  612. */
  613. fgrs = 1;
  614. fsp_set_slot(dev, 0, false, 0, 0);
  615. }
  616. ad->last_mt_fgr = 2;
  617. fsp_set_slot(dev, 1, fgrs == 2, abs_x, abs_y);
  618. } else {
  619. /* 1st finger */
  620. if (ad->last_mt_fgr == 1) {
  621. /*
  622. * workaround for buggy firmware
  623. * which doesn't clear MFMC bit if
  624. * the 2nd finger is up
  625. */
  626. fgrs = 1;
  627. fsp_set_slot(dev, 1, false, 0, 0);
  628. }
  629. ad->last_mt_fgr = 1;
  630. fsp_set_slot(dev, 0, fgrs != 0, abs_x, abs_y);
  631. }
  632. } else {
  633. /* SFAC packet */
  634. if ((packet[0] & (FSP_PB0_LBTN|FSP_PB0_PHY_BTN)) ==
  635. FSP_PB0_LBTN) {
  636. /* On-pad click in SFAC mode should be handled
  637. * by userspace. On-pad clicks in MFMC mode
  638. * are real clickpad clicks, and not ignored.
  639. */
  640. packet[0] &= ~FSP_PB0_LBTN;
  641. }
  642. /* no multi-finger information */
  643. ad->last_mt_fgr = 0;
  644. if (abs_x != 0 && abs_y != 0)
  645. fgrs = 1;
  646. fsp_set_slot(dev, 0, fgrs > 0, abs_x, abs_y);
  647. fsp_set_slot(dev, 1, false, 0, 0);
  648. }
  649. if (fgrs == 1 || (fgrs == 2 && !(packet[0] & FSP_PB0_MFMC_FGR2))) {
  650. input_report_abs(dev, ABS_X, abs_x);
  651. input_report_abs(dev, ABS_Y, abs_y);
  652. }
  653. input_report_key(dev, BTN_LEFT, packet[0] & 0x01);
  654. input_report_key(dev, BTN_RIGHT, packet[0] & 0x02);
  655. input_report_key(dev, BTN_TOUCH, fgrs);
  656. input_report_key(dev, BTN_TOOL_FINGER, fgrs == 1);
  657. input_report_key(dev, BTN_TOOL_DOUBLETAP, fgrs == 2);
  658. break;
  659. case FSP_PKT_TYPE_NORMAL_OPC:
  660. /* on-pad click, filter it if necessary */
  661. if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
  662. packet[0] &= ~FSP_PB0_LBTN;
  663. /* fall through */
  664. case FSP_PKT_TYPE_NORMAL:
  665. /* normal packet */
  666. /* special packet data translation from on-pad packets */
  667. if (packet[3] != 0) {
  668. if (packet[3] & BIT(0))
  669. button_status |= 0x01; /* wheel down */
  670. if (packet[3] & BIT(1))
  671. button_status |= 0x0f; /* wheel up */
  672. if (packet[3] & BIT(2))
  673. button_status |= BIT(4);/* horizontal left */
  674. if (packet[3] & BIT(3))
  675. button_status |= BIT(5);/* horizontal right */
  676. /* push back to packet queue */
  677. if (button_status != 0)
  678. packet[3] = button_status;
  679. rscroll = (packet[3] >> 4) & 1;
  680. lscroll = (packet[3] >> 5) & 1;
  681. }
  682. /*
  683. * Processing wheel up/down and extra button events
  684. */
  685. input_report_rel(dev, REL_WHEEL,
  686. (int)(packet[3] & 8) - (int)(packet[3] & 7));
  687. input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
  688. input_report_key(dev, BTN_BACK, lscroll);
  689. input_report_key(dev, BTN_FORWARD, rscroll);
  690. /*
  691. * Standard PS/2 Mouse
  692. */
  693. input_report_key(dev, BTN_LEFT, packet[0] & 1);
  694. input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
  695. input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
  696. rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
  697. rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
  698. input_report_rel(dev, REL_X, rel_x);
  699. input_report_rel(dev, REL_Y, rel_y);
  700. break;
  701. }
  702. input_sync(dev);
  703. return PSMOUSE_FULL_PACKET;
  704. }
  705. static int fsp_activate_protocol(struct psmouse *psmouse)
  706. {
  707. struct fsp_data *pad = psmouse->private;
  708. struct ps2dev *ps2dev = &psmouse->ps2dev;
  709. unsigned char param[2];
  710. int val;
  711. /*
  712. * Standard procedure to enter FSP Intellimouse mode
  713. * (scrolling wheel, 4th and 5th buttons)
  714. */
  715. param[0] = 200;
  716. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  717. param[0] = 200;
  718. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  719. param[0] = 80;
  720. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  721. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  722. if (param[0] != 0x04) {
  723. psmouse_err(psmouse,
  724. "Unable to enable 4 bytes packet format.\n");
  725. return -EIO;
  726. }
  727. if (pad->ver < FSP_VER_STL3888_C0) {
  728. /* Preparing relative coordinates output for older hardware */
  729. if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
  730. psmouse_err(psmouse,
  731. "Unable to read SYSCTL5 register.\n");
  732. return -EIO;
  733. }
  734. if (fsp_get_buttons(psmouse, &pad->buttons)) {
  735. psmouse_err(psmouse,
  736. "Unable to retrieve number of buttons.\n");
  737. return -EIO;
  738. }
  739. val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
  740. /* Ensure we are not in absolute mode */
  741. val &= ~FSP_BIT_EN_PKT_G0;
  742. if (pad->buttons == 0x06) {
  743. /* Left/Middle/Right & Scroll Up/Down/Right/Left */
  744. val |= FSP_BIT_EN_MSID6;
  745. }
  746. if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
  747. psmouse_err(psmouse,
  748. "Unable to set up required mode bits.\n");
  749. return -EIO;
  750. }
  751. /*
  752. * Enable OPC tags such that driver can tell the difference
  753. * between on-pad and real button click
  754. */
  755. if (fsp_opc_tag_enable(psmouse, true))
  756. psmouse_warn(psmouse,
  757. "Failed to enable OPC tag mode.\n");
  758. /* enable on-pad click by default */
  759. pad->flags |= FSPDRV_FLAG_EN_OPC;
  760. /* Enable on-pad vertical and horizontal scrolling */
  761. fsp_onpad_vscr(psmouse, true);
  762. fsp_onpad_hscr(psmouse, true);
  763. } else {
  764. /* Enable absolute coordinates output for Cx/Dx hardware */
  765. if (fsp_reg_write(psmouse, FSP_REG_SWC1,
  766. FSP_BIT_SWC1_EN_ABS_1F |
  767. FSP_BIT_SWC1_EN_ABS_2F |
  768. FSP_BIT_SWC1_EN_FUP_OUT |
  769. FSP_BIT_SWC1_EN_ABS_CON)) {
  770. psmouse_err(psmouse,
  771. "Unable to enable absolute coordinates output.\n");
  772. return -EIO;
  773. }
  774. }
  775. return 0;
  776. }
  777. static int fsp_set_input_params(struct psmouse *psmouse)
  778. {
  779. struct input_dev *dev = psmouse->dev;
  780. struct fsp_data *pad = psmouse->private;
  781. if (pad->ver < FSP_VER_STL3888_C0) {
  782. __set_bit(BTN_MIDDLE, dev->keybit);
  783. __set_bit(BTN_BACK, dev->keybit);
  784. __set_bit(BTN_FORWARD, dev->keybit);
  785. __set_bit(REL_WHEEL, dev->relbit);
  786. __set_bit(REL_HWHEEL, dev->relbit);
  787. } else {
  788. /*
  789. * Hardware prior to Cx performs much better in relative mode;
  790. * hence, only enable absolute coordinates output as well as
  791. * multi-touch output for the newer hardware.
  792. *
  793. * Maximum coordinates can be computed as:
  794. *
  795. * number of scanlines * 64 - 57
  796. *
  797. * where number of X/Y scanline lines are 16/12.
  798. */
  799. int abs_x = 967, abs_y = 711;
  800. __set_bit(EV_ABS, dev->evbit);
  801. __clear_bit(EV_REL, dev->evbit);
  802. __set_bit(BTN_TOUCH, dev->keybit);
  803. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  804. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  805. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  806. input_set_abs_params(dev, ABS_X, 0, abs_x, 0, 0);
  807. input_set_abs_params(dev, ABS_Y, 0, abs_y, 0, 0);
  808. input_mt_init_slots(dev, 2, 0);
  809. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, abs_x, 0, 0);
  810. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, abs_y, 0, 0);
  811. }
  812. return 0;
  813. }
  814. int fsp_detect(struct psmouse *psmouse, bool set_properties)
  815. {
  816. int id;
  817. if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
  818. return -EIO;
  819. if (id != 0x01)
  820. return -ENODEV;
  821. if (set_properties) {
  822. psmouse->vendor = "Sentelic";
  823. psmouse->name = "FingerSensingPad";
  824. }
  825. return 0;
  826. }
  827. static void fsp_reset(struct psmouse *psmouse)
  828. {
  829. fsp_opc_tag_enable(psmouse, false);
  830. fsp_onpad_vscr(psmouse, false);
  831. fsp_onpad_hscr(psmouse, false);
  832. }
  833. static void fsp_disconnect(struct psmouse *psmouse)
  834. {
  835. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  836. &fsp_attribute_group);
  837. fsp_reset(psmouse);
  838. kfree(psmouse->private);
  839. }
  840. static int fsp_reconnect(struct psmouse *psmouse)
  841. {
  842. int version;
  843. if (fsp_detect(psmouse, 0))
  844. return -ENODEV;
  845. if (fsp_get_version(psmouse, &version))
  846. return -ENODEV;
  847. if (fsp_activate_protocol(psmouse))
  848. return -EIO;
  849. return 0;
  850. }
  851. int fsp_init(struct psmouse *psmouse)
  852. {
  853. struct fsp_data *priv;
  854. int ver, rev, sn = 0;
  855. int error;
  856. if (fsp_get_version(psmouse, &ver) ||
  857. fsp_get_revision(psmouse, &rev)) {
  858. return -ENODEV;
  859. }
  860. if (ver >= FSP_VER_STL3888_C0) {
  861. /* firmware information is only available since C0 */
  862. fsp_get_sn(psmouse, &sn);
  863. }
  864. psmouse_info(psmouse,
  865. "Finger Sensing Pad, hw: %d.%d.%d, sn: %x, sw: %s\n",
  866. ver >> 4, ver & 0x0F, rev, sn, fsp_drv_ver);
  867. psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
  868. if (!priv)
  869. return -ENOMEM;
  870. priv->ver = ver;
  871. priv->rev = rev;
  872. psmouse->protocol_handler = fsp_process_byte;
  873. psmouse->disconnect = fsp_disconnect;
  874. psmouse->reconnect = fsp_reconnect;
  875. psmouse->cleanup = fsp_reset;
  876. psmouse->pktsize = 4;
  877. error = fsp_activate_protocol(psmouse);
  878. if (error)
  879. goto err_out;
  880. /* Set up various supported input event bits */
  881. error = fsp_set_input_params(psmouse);
  882. if (error)
  883. goto err_out;
  884. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  885. &fsp_attribute_group);
  886. if (error) {
  887. psmouse_err(psmouse,
  888. "Failed to create sysfs attributes (%d)", error);
  889. goto err_out;
  890. }
  891. return 0;
  892. err_out:
  893. kfree(psmouse->private);
  894. psmouse->private = NULL;
  895. return error;
  896. }