sentelic.c 21 KB

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