cypress_ps2.c 19 KB

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