sentelic.c 21 KB

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