cypress_ps2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * Cypress Trackpad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2012 Cypress Semiconductor Corporation.
  5. *
  6. * Author:
  7. * Dudley Du <dudl@cypress.com>
  8. *
  9. * Additional contributors include:
  10. * Kamal Mostafa <kamal@canonical.com>
  11. * Kyle Fazzari <git@status.e4ward.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2 as published by
  15. * the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/serio.h>
  22. #include <linux/libps2.h>
  23. #include <linux/input.h>
  24. #include <linux/input/mt.h>
  25. #include <linux/sched.h>
  26. #include <linux/wait.h>
  27. #include "cypress_ps2.h"
  28. #undef CYTP_DEBUG_VERBOSE /* define this and DEBUG for more verbose dump */
  29. static void cypress_set_packet_size(struct psmouse *psmouse, unsigned int n)
  30. {
  31. struct cytp_data *cytp = psmouse->private;
  32. cytp->pkt_size = n;
  33. }
  34. static const unsigned char cytp_rate[] = {10, 20, 40, 60, 100, 200};
  35. static const unsigned char cytp_resolution[] = {0x00, 0x01, 0x02, 0x03};
  36. static int cypress_ps2_sendbyte(struct psmouse *psmouse, int value)
  37. {
  38. struct ps2dev *ps2dev = &psmouse->ps2dev;
  39. if (ps2_sendbyte(ps2dev, value & 0xff, CYTP_CMD_TIMEOUT) < 0) {
  40. psmouse_dbg(psmouse,
  41. "sending command 0x%02x failed, resp 0x%02x\n",
  42. value & 0xff, ps2dev->nak);
  43. if (ps2dev->nak == CYTP_PS2_RETRY)
  44. return CYTP_PS2_RETRY;
  45. else
  46. return CYTP_PS2_ERROR;
  47. }
  48. #ifdef CYTP_DEBUG_VERBOSE
  49. psmouse_dbg(psmouse, "sending command 0x%02x succeeded, resp 0xfa\n",
  50. value & 0xff);
  51. #endif
  52. return 0;
  53. }
  54. static int cypress_ps2_ext_cmd(struct psmouse *psmouse, unsigned short cmd,
  55. unsigned char data)
  56. {
  57. struct ps2dev *ps2dev = &psmouse->ps2dev;
  58. int tries = CYTP_PS2_CMD_TRIES;
  59. int rc;
  60. ps2_begin_command(ps2dev);
  61. do {
  62. /*
  63. * Send extension command byte (0xE8 or 0xF3).
  64. * If sending the command fails, send recovery command
  65. * to make the device return to the ready state.
  66. */
  67. rc = cypress_ps2_sendbyte(psmouse, cmd & 0xff);
  68. if (rc == CYTP_PS2_RETRY) {
  69. rc = cypress_ps2_sendbyte(psmouse, 0x00);
  70. if (rc == CYTP_PS2_RETRY)
  71. rc = cypress_ps2_sendbyte(psmouse, 0x0a);
  72. }
  73. if (rc == CYTP_PS2_ERROR)
  74. continue;
  75. rc = cypress_ps2_sendbyte(psmouse, data);
  76. if (rc == CYTP_PS2_RETRY)
  77. rc = cypress_ps2_sendbyte(psmouse, data);
  78. if (rc == CYTP_PS2_ERROR)
  79. continue;
  80. else
  81. break;
  82. } while (--tries > 0);
  83. ps2_end_command(ps2dev);
  84. return rc;
  85. }
  86. static int cypress_ps2_read_cmd_status(struct psmouse *psmouse,
  87. unsigned char cmd,
  88. unsigned char *param)
  89. {
  90. int rc;
  91. struct ps2dev *ps2dev = &psmouse->ps2dev;
  92. enum psmouse_state old_state;
  93. int pktsize;
  94. ps2_begin_command(&psmouse->ps2dev);
  95. old_state = psmouse->state;
  96. psmouse->state = PSMOUSE_CMD_MODE;
  97. psmouse->pktcnt = 0;
  98. pktsize = (cmd == CYTP_CMD_READ_TP_METRICS) ? 8 : 3;
  99. memset(param, 0, pktsize);
  100. rc = cypress_ps2_sendbyte(psmouse, 0xe9);
  101. if (rc < 0)
  102. goto out;
  103. wait_event_timeout(ps2dev->wait,
  104. (psmouse->pktcnt >= pktsize),
  105. msecs_to_jiffies(CYTP_CMD_TIMEOUT));
  106. memcpy(param, psmouse->packet, pktsize);
  107. psmouse_dbg(psmouse, "Command 0x%02x response data (0x): %*ph\n",
  108. cmd, pktsize, param);
  109. out:
  110. psmouse->state = old_state;
  111. psmouse->pktcnt = 0;
  112. ps2_end_command(&psmouse->ps2dev);
  113. return rc;
  114. }
  115. static bool cypress_verify_cmd_state(struct psmouse *psmouse,
  116. unsigned char cmd, unsigned char *param)
  117. {
  118. bool rate_match = false;
  119. bool resolution_match = false;
  120. int i;
  121. /* callers will do further checking. */
  122. if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
  123. cmd == CYTP_CMD_STANDARD_MODE ||
  124. cmd == CYTP_CMD_READ_TP_METRICS)
  125. return true;
  126. if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
  127. (param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE) {
  128. for (i = 0; i < sizeof(cytp_resolution); i++)
  129. if (cytp_resolution[i] == param[1])
  130. resolution_match = true;
  131. for (i = 0; i < sizeof(cytp_rate); i++)
  132. if (cytp_rate[i] == param[2])
  133. rate_match = true;
  134. if (resolution_match && rate_match)
  135. return true;
  136. }
  137. psmouse_dbg(psmouse, "verify cmd state failed.\n");
  138. return false;
  139. }
  140. static int cypress_send_ext_cmd(struct psmouse *psmouse, unsigned char cmd,
  141. unsigned char *param)
  142. {
  143. int tries = CYTP_PS2_CMD_TRIES;
  144. int rc;
  145. psmouse_dbg(psmouse, "send extension cmd 0x%02x, [%d %d %d %d]\n",
  146. cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd),
  147. DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
  148. do {
  149. cypress_ps2_ext_cmd(psmouse,
  150. PSMOUSE_CMD_SETRES, DECODE_CMD_DD(cmd));
  151. cypress_ps2_ext_cmd(psmouse,
  152. PSMOUSE_CMD_SETRES, DECODE_CMD_CC(cmd));
  153. cypress_ps2_ext_cmd(psmouse,
  154. PSMOUSE_CMD_SETRES, DECODE_CMD_BB(cmd));
  155. cypress_ps2_ext_cmd(psmouse,
  156. PSMOUSE_CMD_SETRES, DECODE_CMD_AA(cmd));
  157. rc = cypress_ps2_read_cmd_status(psmouse, cmd, param);
  158. if (rc)
  159. continue;
  160. if (cypress_verify_cmd_state(psmouse, cmd, param))
  161. return 0;
  162. } while (--tries > 0);
  163. return -EIO;
  164. }
  165. int cypress_detect(struct psmouse *psmouse, bool set_properties)
  166. {
  167. unsigned char param[3];
  168. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
  169. return -ENODEV;
  170. /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
  171. if (param[0] != 0x33 || param[1] != 0xCC)
  172. return -ENODEV;
  173. if (set_properties) {
  174. psmouse->vendor = "Cypress";
  175. psmouse->name = "Trackpad";
  176. }
  177. return 0;
  178. }
  179. static int cypress_read_fw_version(struct psmouse *psmouse)
  180. {
  181. struct cytp_data *cytp = psmouse->private;
  182. unsigned char param[3];
  183. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
  184. return -ENODEV;
  185. /* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
  186. if (param[0] != 0x33 || param[1] != 0xCC)
  187. return -ENODEV;
  188. cytp->fw_version = param[2] & FW_VERSION_MASX;
  189. cytp->tp_metrics_supported = (param[2] & TP_METRICS_MASK) ? 1 : 0;
  190. /*
  191. * Trackpad fw_version 11 (in Dell XPS12) yields a bogus response to
  192. * CYTP_CMD_READ_TP_METRICS so do not try to use it. LP: #1103594.
  193. */
  194. if (cytp->fw_version >= 11)
  195. cytp->tp_metrics_supported = 0;
  196. psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
  197. psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
  198. cytp->tp_metrics_supported);
  199. return 0;
  200. }
  201. static int cypress_read_tp_metrics(struct psmouse *psmouse)
  202. {
  203. struct cytp_data *cytp = psmouse->private;
  204. unsigned char param[8];
  205. /* set default values for tp metrics. */
  206. cytp->tp_width = CYTP_DEFAULT_WIDTH;
  207. cytp->tp_high = CYTP_DEFAULT_HIGH;
  208. cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
  209. cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
  210. cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
  211. cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
  212. cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
  213. cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
  214. if (!cytp->tp_metrics_supported)
  215. return 0;
  216. memset(param, 0, sizeof(param));
  217. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
  218. /* Update trackpad parameters. */
  219. cytp->tp_max_abs_x = (param[1] << 8) | param[0];
  220. cytp->tp_max_abs_y = (param[3] << 8) | param[2];
  221. cytp->tp_min_pressure = param[4];
  222. cytp->tp_max_pressure = param[5];
  223. }
  224. if (!cytp->tp_max_pressure ||
  225. cytp->tp_max_pressure < cytp->tp_min_pressure ||
  226. !cytp->tp_width || !cytp->tp_high ||
  227. !cytp->tp_max_abs_x ||
  228. cytp->tp_max_abs_x < cytp->tp_width ||
  229. !cytp->tp_max_abs_y ||
  230. cytp->tp_max_abs_y < cytp->tp_high)
  231. return -EINVAL;
  232. cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
  233. cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
  234. #ifdef CYTP_DEBUG_VERBOSE
  235. psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
  236. psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
  237. psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
  238. psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
  239. psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
  240. psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n", cytp->tp_min_pressure);
  241. psmouse_dbg(psmouse, "cytp->tp_max_pressure = %d\n", cytp->tp_max_pressure);
  242. psmouse_dbg(psmouse, "cytp->tp_res_x = %d\n", cytp->tp_res_x);
  243. psmouse_dbg(psmouse, "cytp->tp_res_y = %d\n", cytp->tp_res_y);
  244. psmouse_dbg(psmouse, "tp_type_APA = %d\n",
  245. (param[6] & TP_METRICS_BIT_APA) ? 1 : 0);
  246. psmouse_dbg(psmouse, "tp_type_MTG = %d\n",
  247. (param[6] & TP_METRICS_BIT_MTG) ? 1 : 0);
  248. psmouse_dbg(psmouse, "tp_palm = %d\n",
  249. (param[6] & TP_METRICS_BIT_PALM) ? 1 : 0);
  250. psmouse_dbg(psmouse, "tp_stubborn = %d\n",
  251. (param[6] & TP_METRICS_BIT_STUBBORN) ? 1 : 0);
  252. psmouse_dbg(psmouse, "tp_1f_jitter = %d\n",
  253. (param[6] & TP_METRICS_BIT_1F_JITTER) >> 2);
  254. psmouse_dbg(psmouse, "tp_2f_jitter = %d\n",
  255. (param[6] & TP_METRICS_BIT_2F_JITTER) >> 4);
  256. psmouse_dbg(psmouse, "tp_1f_spike = %d\n",
  257. param[7] & TP_METRICS_BIT_1F_SPIKE);
  258. psmouse_dbg(psmouse, "tp_2f_spike = %d\n",
  259. (param[7] & TP_METRICS_BIT_2F_SPIKE) >> 2);
  260. psmouse_dbg(psmouse, "tp_abs_packet_format_set = %d\n",
  261. (param[7] & TP_METRICS_BIT_ABS_PKT_FORMAT_SET) >> 4);
  262. #endif
  263. return 0;
  264. }
  265. static int cypress_query_hardware(struct psmouse *psmouse)
  266. {
  267. int ret;
  268. ret = cypress_read_fw_version(psmouse);
  269. if (ret)
  270. return ret;
  271. ret = cypress_read_tp_metrics(psmouse);
  272. if (ret)
  273. return ret;
  274. return 0;
  275. }
  276. static int cypress_set_absolute_mode(struct psmouse *psmouse)
  277. {
  278. struct cytp_data *cytp = psmouse->private;
  279. unsigned char param[3];
  280. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_ABS_WITH_PRESSURE_MODE, param) < 0)
  281. return -1;
  282. cytp->mode = (cytp->mode & ~CYTP_BIT_ABS_REL_MASK)
  283. | CYTP_BIT_ABS_PRESSURE;
  284. cypress_set_packet_size(psmouse, 5);
  285. return 0;
  286. }
  287. /*
  288. * Reset trackpad device.
  289. * This is also the default mode when trackpad powered on.
  290. */
  291. static void cypress_reset(struct psmouse *psmouse)
  292. {
  293. struct cytp_data *cytp = psmouse->private;
  294. cytp->mode = 0;
  295. psmouse_reset(psmouse);
  296. }
  297. static int cypress_set_input_params(struct input_dev *input,
  298. struct cytp_data *cytp)
  299. {
  300. int ret;
  301. if (!cytp->tp_res_x || !cytp->tp_res_y)
  302. return -EINVAL;
  303. __set_bit(EV_ABS, input->evbit);
  304. input_set_abs_params(input, ABS_X, 0, cytp->tp_max_abs_x, 0, 0);
  305. input_set_abs_params(input, ABS_Y, 0, cytp->tp_max_abs_y, 0, 0);
  306. input_set_abs_params(input, ABS_PRESSURE,
  307. cytp->tp_min_pressure, cytp->tp_max_pressure, 0, 0);
  308. input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 255, 0, 0);
  309. /* finger position */
  310. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cytp->tp_max_abs_x, 0, 0);
  311. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cytp->tp_max_abs_y, 0, 0);
  312. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  313. ret = input_mt_init_slots(input, CYTP_MAX_MT_SLOTS,
  314. INPUT_MT_DROP_UNUSED|INPUT_MT_TRACK);
  315. if (ret < 0)
  316. return ret;
  317. __set_bit(INPUT_PROP_SEMI_MT, input->propbit);
  318. input_abs_set_res(input, ABS_X, cytp->tp_res_x);
  319. input_abs_set_res(input, ABS_Y, cytp->tp_res_y);
  320. input_abs_set_res(input, ABS_MT_POSITION_X, cytp->tp_res_x);
  321. input_abs_set_res(input, ABS_MT_POSITION_Y, cytp->tp_res_y);
  322. __set_bit(BTN_TOUCH, input->keybit);
  323. __set_bit(BTN_TOOL_FINGER, input->keybit);
  324. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  325. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  326. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  327. __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
  328. __clear_bit(EV_REL, input->evbit);
  329. __clear_bit(REL_X, input->relbit);
  330. __clear_bit(REL_Y, input->relbit);
  331. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  332. __set_bit(EV_KEY, input->evbit);
  333. __set_bit(BTN_LEFT, input->keybit);
  334. __set_bit(BTN_RIGHT, input->keybit);
  335. __set_bit(BTN_MIDDLE, input->keybit);
  336. input_set_drvdata(input, cytp);
  337. return 0;
  338. }
  339. static int cypress_get_finger_count(unsigned char header_byte)
  340. {
  341. unsigned char bits6_7;
  342. int finger_count;
  343. bits6_7 = header_byte >> 6;
  344. finger_count = bits6_7 & 0x03;
  345. if (finger_count == 1)
  346. return 1;
  347. if (header_byte & ABS_HSCROLL_BIT) {
  348. /* HSCROLL gets added on to 0 finger count. */
  349. switch (finger_count) {
  350. case 0: return 4;
  351. case 2: return 5;
  352. default:
  353. /* Invalid contact (e.g. palm). Ignore it. */
  354. return -1;
  355. }
  356. }
  357. return finger_count;
  358. }
  359. static int cypress_parse_packet(struct psmouse *psmouse,
  360. struct cytp_data *cytp, struct cytp_report_data *report_data)
  361. {
  362. unsigned char *packet = psmouse->packet;
  363. unsigned char header_byte = packet[0];
  364. int contact_cnt;
  365. memset(report_data, 0, sizeof(struct cytp_report_data));
  366. contact_cnt = cypress_get_finger_count(header_byte);
  367. if (contact_cnt < 0) /* e.g. palm detect */
  368. return -EINVAL;
  369. report_data->contact_cnt = contact_cnt;
  370. report_data->tap = (header_byte & ABS_MULTIFINGER_TAP) ? 1 : 0;
  371. if (report_data->contact_cnt == 1) {
  372. report_data->contacts[0].x =
  373. ((packet[1] & 0x70) << 4) | packet[2];
  374. report_data->contacts[0].y =
  375. ((packet[1] & 0x07) << 8) | packet[3];
  376. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  377. report_data->contacts[0].z = packet[4];
  378. } else if (report_data->contact_cnt >= 2) {
  379. report_data->contacts[0].x =
  380. ((packet[1] & 0x70) << 4) | packet[2];
  381. report_data->contacts[0].y =
  382. ((packet[1] & 0x07) << 8) | packet[3];
  383. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  384. report_data->contacts[0].z = packet[4];
  385. report_data->contacts[1].x =
  386. ((packet[5] & 0xf0) << 4) | packet[6];
  387. report_data->contacts[1].y =
  388. ((packet[5] & 0x0f) << 8) | packet[7];
  389. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  390. report_data->contacts[1].z = report_data->contacts[0].z;
  391. }
  392. report_data->left = (header_byte & BTN_LEFT_BIT) ? 1 : 0;
  393. report_data->right = (header_byte & BTN_RIGHT_BIT) ? 1 : 0;
  394. /*
  395. * This is only true if one of the mouse buttons were tapped. Make
  396. * sure it doesn't turn into a click. The regular tap-to-click
  397. * functionality will handle that on its own. If we don't do this,
  398. * disabling tap-to-click won't affect the mouse button zones.
  399. */
  400. if (report_data->tap)
  401. report_data->left = 0;
  402. #ifdef CYTP_DEBUG_VERBOSE
  403. {
  404. int i;
  405. int n = report_data->contact_cnt;
  406. psmouse_dbg(psmouse, "Dump parsed report data as below:\n");
  407. psmouse_dbg(psmouse, "contact_cnt = %d\n",
  408. report_data->contact_cnt);
  409. if (n > CYTP_MAX_MT_SLOTS)
  410. n = CYTP_MAX_MT_SLOTS;
  411. for (i = 0; i < n; i++)
  412. psmouse_dbg(psmouse, "contacts[%d] = {%d, %d, %d}\n", i,
  413. report_data->contacts[i].x,
  414. report_data->contacts[i].y,
  415. report_data->contacts[i].z);
  416. psmouse_dbg(psmouse, "left = %d\n", report_data->left);
  417. psmouse_dbg(psmouse, "right = %d\n", report_data->right);
  418. psmouse_dbg(psmouse, "middle = %d\n", report_data->middle);
  419. }
  420. #endif
  421. return 0;
  422. }
  423. static void cypress_process_packet(struct psmouse *psmouse, bool zero_pkt)
  424. {
  425. int i;
  426. struct input_dev *input = psmouse->dev;
  427. struct cytp_data *cytp = psmouse->private;
  428. struct cytp_report_data report_data;
  429. struct cytp_contact *contact;
  430. struct input_mt_pos pos[CYTP_MAX_MT_SLOTS];
  431. int slots[CYTP_MAX_MT_SLOTS];
  432. int n;
  433. if (cypress_parse_packet(psmouse, cytp, &report_data))
  434. return;
  435. n = report_data.contact_cnt;
  436. if (n > CYTP_MAX_MT_SLOTS)
  437. n = CYTP_MAX_MT_SLOTS;
  438. for (i = 0; i < n; i++) {
  439. contact = &report_data.contacts[i];
  440. pos[i].x = contact->x;
  441. pos[i].y = contact->y;
  442. }
  443. input_mt_assign_slots(input, slots, pos, n);
  444. for (i = 0; i < n; i++) {
  445. contact = &report_data.contacts[i];
  446. input_mt_slot(input, slots[i]);
  447. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  448. input_report_abs(input, ABS_MT_POSITION_X, contact->x);
  449. input_report_abs(input, ABS_MT_POSITION_Y, contact->y);
  450. input_report_abs(input, ABS_MT_PRESSURE, contact->z);
  451. }
  452. input_mt_sync_frame(input);
  453. input_mt_report_finger_count(input, report_data.contact_cnt);
  454. input_report_key(input, BTN_LEFT, report_data.left);
  455. input_report_key(input, BTN_RIGHT, report_data.right);
  456. input_report_key(input, BTN_MIDDLE, report_data.middle);
  457. input_sync(input);
  458. }
  459. static psmouse_ret_t cypress_validate_byte(struct psmouse *psmouse)
  460. {
  461. int contact_cnt;
  462. int index = psmouse->pktcnt - 1;
  463. unsigned char *packet = psmouse->packet;
  464. struct cytp_data *cytp = psmouse->private;
  465. if (index < 0 || index > cytp->pkt_size)
  466. return PSMOUSE_BAD_DATA;
  467. if (index == 0 && (packet[0] & 0xfc) == 0) {
  468. /* call packet process for reporting finger leave. */
  469. cypress_process_packet(psmouse, 1);
  470. return PSMOUSE_FULL_PACKET;
  471. }
  472. /*
  473. * Perform validation (and adjust packet size) based only on the
  474. * first byte; allow all further bytes through.
  475. */
  476. if (index != 0)
  477. return PSMOUSE_GOOD_DATA;
  478. /*
  479. * If absolute/relative mode bit has not been set yet, just pass
  480. * the byte through.
  481. */
  482. if ((cytp->mode & CYTP_BIT_ABS_REL_MASK) == 0)
  483. return PSMOUSE_GOOD_DATA;
  484. if ((packet[0] & 0x08) == 0x08)
  485. return PSMOUSE_BAD_DATA;
  486. contact_cnt = cypress_get_finger_count(packet[0]);
  487. if (contact_cnt < 0)
  488. return PSMOUSE_BAD_DATA;
  489. if (cytp->mode & CYTP_BIT_ABS_NO_PRESSURE)
  490. cypress_set_packet_size(psmouse, contact_cnt == 2 ? 7 : 4);
  491. else
  492. cypress_set_packet_size(psmouse, contact_cnt == 2 ? 8 : 5);
  493. return PSMOUSE_GOOD_DATA;
  494. }
  495. static psmouse_ret_t cypress_protocol_handler(struct psmouse *psmouse)
  496. {
  497. struct cytp_data *cytp = psmouse->private;
  498. if (psmouse->pktcnt >= cytp->pkt_size) {
  499. cypress_process_packet(psmouse, 0);
  500. return PSMOUSE_FULL_PACKET;
  501. }
  502. return cypress_validate_byte(psmouse);
  503. }
  504. static void cypress_set_rate(struct psmouse *psmouse, unsigned int rate)
  505. {
  506. struct cytp_data *cytp = psmouse->private;
  507. if (rate >= 80) {
  508. psmouse->rate = 80;
  509. cytp->mode |= CYTP_BIT_HIGH_RATE;
  510. } else {
  511. psmouse->rate = 40;
  512. cytp->mode &= ~CYTP_BIT_HIGH_RATE;
  513. }
  514. ps2_command(&psmouse->ps2dev, (unsigned char *)&psmouse->rate,
  515. PSMOUSE_CMD_SETRATE);
  516. }
  517. static void cypress_disconnect(struct psmouse *psmouse)
  518. {
  519. cypress_reset(psmouse);
  520. kfree(psmouse->private);
  521. psmouse->private = NULL;
  522. }
  523. static int cypress_reconnect(struct psmouse *psmouse)
  524. {
  525. int tries = CYTP_PS2_CMD_TRIES;
  526. int rc;
  527. do {
  528. cypress_reset(psmouse);
  529. rc = cypress_detect(psmouse, false);
  530. } while (rc && (--tries > 0));
  531. if (rc) {
  532. psmouse_err(psmouse, "Reconnect: unable to detect trackpad.\n");
  533. return -1;
  534. }
  535. if (cypress_set_absolute_mode(psmouse)) {
  536. psmouse_err(psmouse, "Reconnect: Unable to initialize Cypress absolute mode.\n");
  537. return -1;
  538. }
  539. return 0;
  540. }
  541. int cypress_init(struct psmouse *psmouse)
  542. {
  543. struct cytp_data *cytp;
  544. cytp = (struct cytp_data *)kzalloc(sizeof(struct cytp_data), GFP_KERNEL);
  545. psmouse->private = (void *)cytp;
  546. if (cytp == NULL)
  547. return -ENOMEM;
  548. cypress_reset(psmouse);
  549. psmouse->pktsize = 8;
  550. if (cypress_query_hardware(psmouse)) {
  551. psmouse_err(psmouse, "Unable to query Trackpad hardware.\n");
  552. goto err_exit;
  553. }
  554. if (cypress_set_absolute_mode(psmouse)) {
  555. psmouse_err(psmouse, "init: Unable to initialize Cypress absolute mode.\n");
  556. goto err_exit;
  557. }
  558. if (cypress_set_input_params(psmouse->dev, cytp) < 0) {
  559. psmouse_err(psmouse, "init: Unable to set input params.\n");
  560. goto err_exit;
  561. }
  562. psmouse->model = 1;
  563. psmouse->protocol_handler = cypress_protocol_handler;
  564. psmouse->set_rate = cypress_set_rate;
  565. psmouse->disconnect = cypress_disconnect;
  566. psmouse->reconnect = cypress_reconnect;
  567. psmouse->cleanup = cypress_reset;
  568. psmouse->resync_time = 0;
  569. return 0;
  570. err_exit:
  571. /*
  572. * Reset Cypress Trackpad as a standard mouse. Then
  573. * let psmouse driver commmunicating with it as default PS2 mouse.
  574. */
  575. cypress_reset(psmouse);
  576. psmouse->private = NULL;
  577. kfree(cytp);
  578. return -1;
  579. }
  580. bool cypress_supported(void)
  581. {
  582. return true;
  583. }