sentelic.c 21 KB

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