sentelic.c 26 KB

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