sentelic.c 24 KB

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