sentelic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*-
  2. * Finger Sensing Pad PS/2 mouse driver.
  3. *
  4. * Copyright (C) 2005-2007 Asia Vital Components Co., Ltd.
  5. * Copyright (C) 2005-2009 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/version.h>
  23. #include <linux/input.h>
  24. #include <linux/ctype.h>
  25. #include <linux/libps2.h>
  26. #include <linux/serio.h>
  27. #include <linux/jiffies.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. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE);
  86. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  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. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  116. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  117. dev_dbg(&ps2dev->serio->dev, "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. return -1;
  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. dev_dbg(&ps2dev->serio->dev, "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. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE);
  186. psmouse_set_state(psmouse, PSMOUSE_CMD_MODE);
  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. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE);
  204. psmouse_set_state(psmouse, PSMOUSE_ACTIVATED);
  205. dev_dbg(&ps2dev->serio->dev, "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. return -1;
  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. dev_dbg(&ps2dev->serio->dev, "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_STATUS1, &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. dev_err(&psmouse->ps2dev.serio->dev, "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. dev_err(&psmouse->ps2dev.serio->dev,
  285. "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. unsigned long 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. if (strict_strtoul(rest + 1, 16, &val) || val > 0xff)
  343. return -EINVAL;
  344. if (fsp_reg_write_enable(psmouse, true))
  345. return -EIO;
  346. retval = fsp_reg_write(psmouse, reg, val) < 0 ? -EIO : count;
  347. fsp_reg_write_enable(psmouse, false);
  348. return count;
  349. }
  350. PSMOUSE_DEFINE_WO_ATTR(setreg, S_IWUSR, NULL, fsp_attr_set_setreg);
  351. static ssize_t fsp_attr_show_getreg(struct psmouse *psmouse,
  352. void *data, char *buf)
  353. {
  354. struct fsp_data *pad = psmouse->private;
  355. return sprintf(buf, "%02x%02x\n", pad->last_reg, pad->last_val);
  356. }
  357. /*
  358. * Read a register from device.
  359. *
  360. * ex: 0xab -- read content from register 0xab
  361. */
  362. static ssize_t fsp_attr_set_getreg(struct psmouse *psmouse, void *data,
  363. const char *buf, size_t count)
  364. {
  365. struct fsp_data *pad = psmouse->private;
  366. unsigned long reg;
  367. int val;
  368. if (strict_strtoul(buf, 16, &reg) || reg > 0xff)
  369. return -EINVAL;
  370. if (fsp_reg_read(psmouse, reg, &val))
  371. return -EIO;
  372. pad->last_reg = reg;
  373. pad->last_val = val;
  374. return count;
  375. }
  376. PSMOUSE_DEFINE_ATTR(getreg, S_IWUSR | S_IRUGO, NULL,
  377. fsp_attr_show_getreg, fsp_attr_set_getreg);
  378. static ssize_t fsp_attr_show_pagereg(struct psmouse *psmouse,
  379. void *data, char *buf)
  380. {
  381. int val = 0;
  382. if (fsp_page_reg_read(psmouse, &val))
  383. return -EIO;
  384. return sprintf(buf, "%02x\n", val);
  385. }
  386. static ssize_t fsp_attr_set_pagereg(struct psmouse *psmouse, void *data,
  387. const char *buf, size_t count)
  388. {
  389. unsigned long val;
  390. if (strict_strtoul(buf, 16, &val) || val > 0xff)
  391. return -EINVAL;
  392. if (fsp_page_reg_write(psmouse, val))
  393. return -EIO;
  394. return count;
  395. }
  396. PSMOUSE_DEFINE_ATTR(page, S_IWUSR | S_IRUGO, NULL,
  397. fsp_attr_show_pagereg, fsp_attr_set_pagereg);
  398. static ssize_t fsp_attr_show_vscroll(struct psmouse *psmouse,
  399. void *data, char *buf)
  400. {
  401. struct fsp_data *pad = psmouse->private;
  402. return sprintf(buf, "%d\n", pad->vscroll);
  403. }
  404. static ssize_t fsp_attr_set_vscroll(struct psmouse *psmouse, void *data,
  405. const char *buf, size_t count)
  406. {
  407. unsigned long val;
  408. if (strict_strtoul(buf, 10, &val) || val > 1)
  409. return -EINVAL;
  410. fsp_onpad_vscr(psmouse, val);
  411. return count;
  412. }
  413. PSMOUSE_DEFINE_ATTR(vscroll, S_IWUSR | S_IRUGO, NULL,
  414. fsp_attr_show_vscroll, fsp_attr_set_vscroll);
  415. static ssize_t fsp_attr_show_hscroll(struct psmouse *psmouse,
  416. void *data, char *buf)
  417. {
  418. struct fsp_data *pad = psmouse->private;
  419. return sprintf(buf, "%d\n", pad->hscroll);
  420. }
  421. static ssize_t fsp_attr_set_hscroll(struct psmouse *psmouse, void *data,
  422. const char *buf, size_t count)
  423. {
  424. unsigned long val;
  425. if (strict_strtoul(buf, 10, &val) || val > 1)
  426. return -EINVAL;
  427. fsp_onpad_hscr(psmouse, val);
  428. return count;
  429. }
  430. PSMOUSE_DEFINE_ATTR(hscroll, S_IWUSR | S_IRUGO, NULL,
  431. fsp_attr_show_hscroll, fsp_attr_set_hscroll);
  432. static ssize_t fsp_attr_show_flags(struct psmouse *psmouse,
  433. void *data, char *buf)
  434. {
  435. struct fsp_data *pad = psmouse->private;
  436. return sprintf(buf, "%c\n",
  437. pad->flags & FSPDRV_FLAG_EN_OPC ? 'C' : 'c');
  438. }
  439. static ssize_t fsp_attr_set_flags(struct psmouse *psmouse, void *data,
  440. const char *buf, size_t count)
  441. {
  442. struct fsp_data *pad = psmouse->private;
  443. size_t i;
  444. for (i = 0; i < count; i++) {
  445. switch (buf[i]) {
  446. case 'C':
  447. pad->flags |= FSPDRV_FLAG_EN_OPC;
  448. break;
  449. case 'c':
  450. pad->flags &= ~FSPDRV_FLAG_EN_OPC;
  451. break;
  452. default:
  453. return -EINVAL;
  454. }
  455. }
  456. return count;
  457. }
  458. PSMOUSE_DEFINE_ATTR(flags, S_IWUSR | S_IRUGO, NULL,
  459. fsp_attr_show_flags, fsp_attr_set_flags);
  460. static ssize_t fsp_attr_show_ver(struct psmouse *psmouse,
  461. void *data, char *buf)
  462. {
  463. return sprintf(buf, "Sentelic FSP kernel module %s\n", fsp_drv_ver);
  464. }
  465. PSMOUSE_DEFINE_RO_ATTR(ver, S_IRUGO, NULL, fsp_attr_show_ver);
  466. static struct attribute *fsp_attributes[] = {
  467. &psmouse_attr_setreg.dattr.attr,
  468. &psmouse_attr_getreg.dattr.attr,
  469. &psmouse_attr_page.dattr.attr,
  470. &psmouse_attr_vscroll.dattr.attr,
  471. &psmouse_attr_hscroll.dattr.attr,
  472. &psmouse_attr_flags.dattr.attr,
  473. &psmouse_attr_ver.dattr.attr,
  474. NULL
  475. };
  476. static struct attribute_group fsp_attribute_group = {
  477. .attrs = fsp_attributes,
  478. };
  479. #ifdef FSP_DEBUG
  480. static void fsp_packet_debug(unsigned char packet[])
  481. {
  482. static unsigned int ps2_packet_cnt;
  483. static unsigned int ps2_last_second;
  484. unsigned int jiffies_msec;
  485. ps2_packet_cnt++;
  486. jiffies_msec = jiffies_to_msecs(jiffies);
  487. printk(KERN_DEBUG "%08dms PS/2 packets: %02x, %02x, %02x, %02x\n",
  488. jiffies_msec, packet[0], packet[1], packet[2], packet[3]);
  489. if (jiffies_msec - ps2_last_second > 1000) {
  490. printk(KERN_DEBUG "PS/2 packets/sec = %d\n", ps2_packet_cnt);
  491. ps2_packet_cnt = 0;
  492. ps2_last_second = jiffies_msec;
  493. }
  494. }
  495. #else
  496. static void fsp_packet_debug(unsigned char packet[])
  497. {
  498. }
  499. #endif
  500. static psmouse_ret_t fsp_process_byte(struct psmouse *psmouse)
  501. {
  502. struct input_dev *dev = psmouse->dev;
  503. struct fsp_data *ad = psmouse->private;
  504. unsigned char *packet = psmouse->packet;
  505. unsigned char button_status = 0, lscroll = 0, rscroll = 0;
  506. int rel_x, rel_y;
  507. if (psmouse->pktcnt < 4)
  508. return PSMOUSE_GOOD_DATA;
  509. /*
  510. * Full packet accumulated, process it
  511. */
  512. switch (psmouse->packet[0] >> FSP_PKT_TYPE_SHIFT) {
  513. case FSP_PKT_TYPE_ABS:
  514. dev_warn(&psmouse->ps2dev.serio->dev,
  515. "Unexpected absolute mode packet, ignored.\n");
  516. break;
  517. case FSP_PKT_TYPE_NORMAL_OPC:
  518. /* on-pad click, filter it if necessary */
  519. if ((ad->flags & FSPDRV_FLAG_EN_OPC) != FSPDRV_FLAG_EN_OPC)
  520. packet[0] &= ~BIT(0);
  521. /* fall through */
  522. case FSP_PKT_TYPE_NORMAL:
  523. /* normal packet */
  524. /* special packet data translation from on-pad packets */
  525. if (packet[3] != 0) {
  526. if (packet[3] & BIT(0))
  527. button_status |= 0x01; /* wheel down */
  528. if (packet[3] & BIT(1))
  529. button_status |= 0x0f; /* wheel up */
  530. if (packet[3] & BIT(2))
  531. button_status |= BIT(5);/* horizontal left */
  532. if (packet[3] & BIT(3))
  533. button_status |= BIT(4);/* horizontal right */
  534. /* push back to packet queue */
  535. if (button_status != 0)
  536. packet[3] = button_status;
  537. rscroll = (packet[3] >> 4) & 1;
  538. lscroll = (packet[3] >> 5) & 1;
  539. }
  540. /*
  541. * Processing wheel up/down and extra button events
  542. */
  543. input_report_rel(dev, REL_WHEEL,
  544. (int)(packet[3] & 8) - (int)(packet[3] & 7));
  545. input_report_rel(dev, REL_HWHEEL, lscroll - rscroll);
  546. input_report_key(dev, BTN_BACK, lscroll);
  547. input_report_key(dev, BTN_FORWARD, rscroll);
  548. /*
  549. * Standard PS/2 Mouse
  550. */
  551. input_report_key(dev, BTN_LEFT, packet[0] & 1);
  552. input_report_key(dev, BTN_MIDDLE, (packet[0] >> 2) & 1);
  553. input_report_key(dev, BTN_RIGHT, (packet[0] >> 1) & 1);
  554. rel_x = packet[1] ? (int)packet[1] - (int)((packet[0] << 4) & 0x100) : 0;
  555. rel_y = packet[2] ? (int)((packet[0] << 3) & 0x100) - (int)packet[2] : 0;
  556. input_report_rel(dev, REL_X, rel_x);
  557. input_report_rel(dev, REL_Y, rel_y);
  558. break;
  559. }
  560. input_sync(dev);
  561. fsp_packet_debug(packet);
  562. return PSMOUSE_FULL_PACKET;
  563. }
  564. static int fsp_activate_protocol(struct psmouse *psmouse)
  565. {
  566. struct fsp_data *pad = psmouse->private;
  567. struct ps2dev *ps2dev = &psmouse->ps2dev;
  568. unsigned char param[2];
  569. int val;
  570. /*
  571. * Standard procedure to enter FSP Intellimouse mode
  572. * (scrolling wheel, 4th and 5th buttons)
  573. */
  574. param[0] = 200;
  575. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  576. param[0] = 200;
  577. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  578. param[0] = 80;
  579. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRATE);
  580. ps2_command(ps2dev, param, PSMOUSE_CMD_GETID);
  581. if (param[0] != 0x04) {
  582. dev_err(&psmouse->ps2dev.serio->dev,
  583. "Unable to enable 4 bytes packet format.\n");
  584. return -EIO;
  585. }
  586. if (fsp_reg_read(psmouse, FSP_REG_SYSCTL5, &val)) {
  587. dev_err(&psmouse->ps2dev.serio->dev,
  588. "Unable to read SYSCTL5 register.\n");
  589. return -EIO;
  590. }
  591. val &= ~(FSP_BIT_EN_MSID7 | FSP_BIT_EN_MSID8 | FSP_BIT_EN_AUTO_MSID8);
  592. /* Ensure we are not in absolute mode */
  593. val &= ~FSP_BIT_EN_PKT_G0;
  594. if (pad->buttons == 0x06) {
  595. /* Left/Middle/Right & Scroll Up/Down/Right/Left */
  596. val |= FSP_BIT_EN_MSID6;
  597. }
  598. if (fsp_reg_write(psmouse, FSP_REG_SYSCTL5, val)) {
  599. dev_err(&psmouse->ps2dev.serio->dev,
  600. "Unable to set up required mode bits.\n");
  601. return -EIO;
  602. }
  603. /*
  604. * Enable OPC tags such that driver can tell the difference between
  605. * on-pad and real button click
  606. */
  607. if (fsp_opc_tag_enable(psmouse, true))
  608. dev_warn(&psmouse->ps2dev.serio->dev,
  609. "Failed to enable OPC tag mode.\n");
  610. /* Enable on-pad vertical and horizontal scrolling */
  611. fsp_onpad_vscr(psmouse, true);
  612. fsp_onpad_hscr(psmouse, true);
  613. return 0;
  614. }
  615. int fsp_detect(struct psmouse *psmouse, bool set_properties)
  616. {
  617. int id;
  618. if (fsp_reg_read(psmouse, FSP_REG_DEVICE_ID, &id))
  619. return -EIO;
  620. if (id != 0x01)
  621. return -ENODEV;
  622. if (set_properties) {
  623. psmouse->vendor = "Sentelic";
  624. psmouse->name = "FingerSensingPad";
  625. }
  626. return 0;
  627. }
  628. static void fsp_reset(struct psmouse *psmouse)
  629. {
  630. fsp_opc_tag_enable(psmouse, false);
  631. fsp_onpad_vscr(psmouse, false);
  632. fsp_onpad_hscr(psmouse, false);
  633. }
  634. static void fsp_disconnect(struct psmouse *psmouse)
  635. {
  636. sysfs_remove_group(&psmouse->ps2dev.serio->dev.kobj,
  637. &fsp_attribute_group);
  638. fsp_reset(psmouse);
  639. kfree(psmouse->private);
  640. }
  641. static int fsp_reconnect(struct psmouse *psmouse)
  642. {
  643. int version;
  644. if (fsp_detect(psmouse, 0))
  645. return -ENODEV;
  646. if (fsp_get_version(psmouse, &version))
  647. return -ENODEV;
  648. if (fsp_activate_protocol(psmouse))
  649. return -EIO;
  650. return 0;
  651. }
  652. int fsp_init(struct psmouse *psmouse)
  653. {
  654. struct fsp_data *priv;
  655. int ver, rev, buttons;
  656. int error;
  657. if (fsp_get_version(psmouse, &ver) ||
  658. fsp_get_revision(psmouse, &rev) ||
  659. fsp_get_buttons(psmouse, &buttons)) {
  660. return -ENODEV;
  661. }
  662. printk(KERN_INFO
  663. "Finger Sensing Pad, hw: %d.%d.%d, sw: %s, buttons: %d\n",
  664. ver >> 4, ver & 0x0F, rev, fsp_drv_ver, buttons & 7);
  665. psmouse->private = priv = kzalloc(sizeof(struct fsp_data), GFP_KERNEL);
  666. if (!priv)
  667. return -ENOMEM;
  668. priv->ver = ver;
  669. priv->rev = rev;
  670. priv->buttons = buttons;
  671. /* enable on-pad click by default */
  672. priv->flags |= FSPDRV_FLAG_EN_OPC;
  673. /* Set up various supported input event bits */
  674. __set_bit(BTN_MIDDLE, psmouse->dev->keybit);
  675. __set_bit(BTN_BACK, psmouse->dev->keybit);
  676. __set_bit(BTN_FORWARD, psmouse->dev->keybit);
  677. __set_bit(REL_WHEEL, psmouse->dev->relbit);
  678. __set_bit(REL_HWHEEL, psmouse->dev->relbit);
  679. psmouse->protocol_handler = fsp_process_byte;
  680. psmouse->disconnect = fsp_disconnect;
  681. psmouse->reconnect = fsp_reconnect;
  682. psmouse->cleanup = fsp_reset;
  683. psmouse->pktsize = 4;
  684. /* set default packet output based on number of buttons we found */
  685. error = fsp_activate_protocol(psmouse);
  686. if (error)
  687. goto err_out;
  688. error = sysfs_create_group(&psmouse->ps2dev.serio->dev.kobj,
  689. &fsp_attribute_group);
  690. if (error) {
  691. dev_err(&psmouse->ps2dev.serio->dev,
  692. "Failed to create sysfs attributes (%d)", error);
  693. goto err_out;
  694. }
  695. return 0;
  696. err_out:
  697. kfree(psmouse->private);
  698. psmouse->private = NULL;
  699. return error;
  700. }