cypress_ps2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
  191. psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
  192. cytp->tp_metrics_supported);
  193. return 0;
  194. }
  195. static int cypress_read_tp_metrics(struct psmouse *psmouse)
  196. {
  197. struct cytp_data *cytp = psmouse->private;
  198. unsigned char param[8];
  199. /* set default values for tp metrics. */
  200. cytp->tp_width = CYTP_DEFAULT_WIDTH;
  201. cytp->tp_high = CYTP_DEFAULT_HIGH;
  202. cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
  203. cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
  204. cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
  205. cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
  206. cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
  207. cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
  208. memset(param, 0, sizeof(param));
  209. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
  210. /* Update trackpad parameters. */
  211. cytp->tp_max_abs_x = (param[1] << 8) | param[0];
  212. cytp->tp_max_abs_y = (param[3] << 8) | param[2];
  213. cytp->tp_min_pressure = param[4];
  214. cytp->tp_max_pressure = param[5];
  215. }
  216. if (!cytp->tp_max_pressure ||
  217. cytp->tp_max_pressure < cytp->tp_min_pressure ||
  218. !cytp->tp_width || !cytp->tp_high ||
  219. !cytp->tp_max_abs_x ||
  220. cytp->tp_max_abs_x < cytp->tp_width ||
  221. !cytp->tp_max_abs_y ||
  222. cytp->tp_max_abs_y < cytp->tp_high)
  223. return -EINVAL;
  224. cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
  225. cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
  226. #ifdef CYTP_DEBUG_VERBOSE
  227. psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
  228. psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
  229. psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
  230. psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
  231. psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
  232. psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n", cytp->tp_min_pressure);
  233. psmouse_dbg(psmouse, "cytp->tp_max_pressure = %d\n", cytp->tp_max_pressure);
  234. psmouse_dbg(psmouse, "cytp->tp_res_x = %d\n", cytp->tp_res_x);
  235. psmouse_dbg(psmouse, "cytp->tp_res_y = %d\n", cytp->tp_res_y);
  236. psmouse_dbg(psmouse, "tp_type_APA = %d\n",
  237. (param[6] & TP_METRICS_BIT_APA) ? 1 : 0);
  238. psmouse_dbg(psmouse, "tp_type_MTG = %d\n",
  239. (param[6] & TP_METRICS_BIT_MTG) ? 1 : 0);
  240. psmouse_dbg(psmouse, "tp_palm = %d\n",
  241. (param[6] & TP_METRICS_BIT_PALM) ? 1 : 0);
  242. psmouse_dbg(psmouse, "tp_stubborn = %d\n",
  243. (param[6] & TP_METRICS_BIT_STUBBORN) ? 1 : 0);
  244. psmouse_dbg(psmouse, "tp_1f_jitter = %d\n",
  245. (param[6] & TP_METRICS_BIT_1F_JITTER) >> 2);
  246. psmouse_dbg(psmouse, "tp_2f_jitter = %d\n",
  247. (param[6] & TP_METRICS_BIT_2F_JITTER) >> 4);
  248. psmouse_dbg(psmouse, "tp_1f_spike = %d\n",
  249. param[7] & TP_METRICS_BIT_1F_SPIKE);
  250. psmouse_dbg(psmouse, "tp_2f_spike = %d\n",
  251. (param[7] & TP_METRICS_BIT_2F_SPIKE) >> 2);
  252. psmouse_dbg(psmouse, "tp_abs_packet_format_set = %d\n",
  253. (param[7] & TP_METRICS_BIT_ABS_PKT_FORMAT_SET) >> 4);
  254. #endif
  255. return 0;
  256. }
  257. static int cypress_query_hardware(struct psmouse *psmouse)
  258. {
  259. struct cytp_data *cytp = psmouse->private;
  260. int ret;
  261. ret = cypress_read_fw_version(psmouse);
  262. if (ret)
  263. return ret;
  264. if (cytp->tp_metrics_supported) {
  265. ret = cypress_read_tp_metrics(psmouse);
  266. if (ret)
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. static int cypress_set_absolute_mode(struct psmouse *psmouse)
  272. {
  273. struct cytp_data *cytp = psmouse->private;
  274. unsigned char param[3];
  275. if (cypress_send_ext_cmd(psmouse, CYTP_CMD_ABS_WITH_PRESSURE_MODE, param) < 0)
  276. return -1;
  277. cytp->mode = (cytp->mode & ~CYTP_BIT_ABS_REL_MASK)
  278. | CYTP_BIT_ABS_PRESSURE;
  279. cypress_set_packet_size(psmouse, 5);
  280. return 0;
  281. }
  282. /*
  283. * Reset trackpad device.
  284. * This is also the default mode when trackpad powered on.
  285. */
  286. static void cypress_reset(struct psmouse *psmouse)
  287. {
  288. struct cytp_data *cytp = psmouse->private;
  289. cytp->mode = 0;
  290. psmouse_reset(psmouse);
  291. }
  292. static int cypress_set_input_params(struct input_dev *input,
  293. struct cytp_data *cytp)
  294. {
  295. int ret;
  296. if (!cytp->tp_res_x || !cytp->tp_res_y)
  297. return -EINVAL;
  298. __set_bit(EV_ABS, input->evbit);
  299. input_set_abs_params(input, ABS_X, 0, cytp->tp_max_abs_x, 0, 0);
  300. input_set_abs_params(input, ABS_Y, 0, cytp->tp_max_abs_y, 0, 0);
  301. input_set_abs_params(input, ABS_PRESSURE,
  302. cytp->tp_min_pressure, cytp->tp_max_pressure, 0, 0);
  303. input_set_abs_params(input, ABS_TOOL_WIDTH, 0, 255, 0, 0);
  304. /* finger position */
  305. input_set_abs_params(input, ABS_MT_POSITION_X, 0, cytp->tp_max_abs_x, 0, 0);
  306. input_set_abs_params(input, ABS_MT_POSITION_Y, 0, cytp->tp_max_abs_y, 0, 0);
  307. input_set_abs_params(input, ABS_MT_PRESSURE, 0, 255, 0, 0);
  308. ret = input_mt_init_slots(input, CYTP_MAX_MT_SLOTS,
  309. INPUT_MT_DROP_UNUSED|INPUT_MT_TRACK);
  310. if (ret < 0)
  311. return ret;
  312. __set_bit(INPUT_PROP_SEMI_MT, input->propbit);
  313. input_abs_set_res(input, ABS_X, cytp->tp_res_x);
  314. input_abs_set_res(input, ABS_Y, cytp->tp_res_y);
  315. input_abs_set_res(input, ABS_MT_POSITION_X, cytp->tp_res_x);
  316. input_abs_set_res(input, ABS_MT_POSITION_Y, cytp->tp_res_y);
  317. __set_bit(BTN_TOUCH, input->keybit);
  318. __set_bit(BTN_TOOL_FINGER, input->keybit);
  319. __set_bit(BTN_TOOL_DOUBLETAP, input->keybit);
  320. __set_bit(BTN_TOOL_TRIPLETAP, input->keybit);
  321. __set_bit(BTN_TOOL_QUADTAP, input->keybit);
  322. __set_bit(BTN_TOOL_QUINTTAP, input->keybit);
  323. __clear_bit(EV_REL, input->evbit);
  324. __clear_bit(REL_X, input->relbit);
  325. __clear_bit(REL_Y, input->relbit);
  326. __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
  327. __set_bit(EV_KEY, input->evbit);
  328. __set_bit(BTN_LEFT, input->keybit);
  329. __set_bit(BTN_RIGHT, input->keybit);
  330. __set_bit(BTN_MIDDLE, input->keybit);
  331. input_set_drvdata(input, cytp);
  332. return 0;
  333. }
  334. static int cypress_get_finger_count(unsigned char header_byte)
  335. {
  336. unsigned char bits6_7;
  337. int finger_count;
  338. bits6_7 = header_byte >> 6;
  339. finger_count = bits6_7 & 0x03;
  340. if (finger_count == 1)
  341. return 1;
  342. if (header_byte & ABS_HSCROLL_BIT) {
  343. /* HSCROLL gets added on to 0 finger count. */
  344. switch (finger_count) {
  345. case 0: return 4;
  346. case 2: return 5;
  347. default:
  348. /* Invalid contact (e.g. palm). Ignore it. */
  349. return -1;
  350. }
  351. }
  352. return finger_count;
  353. }
  354. static int cypress_parse_packet(struct psmouse *psmouse,
  355. struct cytp_data *cytp, struct cytp_report_data *report_data)
  356. {
  357. unsigned char *packet = psmouse->packet;
  358. unsigned char header_byte = packet[0];
  359. int contact_cnt;
  360. memset(report_data, 0, sizeof(struct cytp_report_data));
  361. contact_cnt = cypress_get_finger_count(header_byte);
  362. if (contact_cnt < 0) /* e.g. palm detect */
  363. return -EINVAL;
  364. report_data->contact_cnt = contact_cnt;
  365. report_data->tap = (header_byte & ABS_MULTIFINGER_TAP) ? 1 : 0;
  366. if (report_data->contact_cnt == 1) {
  367. report_data->contacts[0].x =
  368. ((packet[1] & 0x70) << 4) | packet[2];
  369. report_data->contacts[0].y =
  370. ((packet[1] & 0x07) << 8) | packet[3];
  371. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  372. report_data->contacts[0].z = packet[4];
  373. } else if (report_data->contact_cnt >= 2) {
  374. report_data->contacts[0].x =
  375. ((packet[1] & 0x70) << 4) | packet[2];
  376. report_data->contacts[0].y =
  377. ((packet[1] & 0x07) << 8) | packet[3];
  378. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  379. report_data->contacts[0].z = packet[4];
  380. report_data->contacts[1].x =
  381. ((packet[5] & 0xf0) << 4) | packet[6];
  382. report_data->contacts[1].y =
  383. ((packet[5] & 0x0f) << 8) | packet[7];
  384. if (cytp->mode & CYTP_BIT_ABS_PRESSURE)
  385. report_data->contacts[1].z = report_data->contacts[0].z;
  386. }
  387. report_data->left = (header_byte & BTN_LEFT_BIT) ? 1 : 0;
  388. report_data->right = (header_byte & BTN_RIGHT_BIT) ? 1 : 0;
  389. /*
  390. * This is only true if one of the mouse buttons were tapped. Make
  391. * sure it doesn't turn into a click. The regular tap-to-click
  392. * functionality will handle that on its own. If we don't do this,
  393. * disabling tap-to-click won't affect the mouse button zones.
  394. */
  395. if (report_data->tap)
  396. report_data->left = 0;
  397. #ifdef CYTP_DEBUG_VERBOSE
  398. {
  399. int i;
  400. int n = report_data->contact_cnt;
  401. psmouse_dbg(psmouse, "Dump parsed report data as below:\n");
  402. psmouse_dbg(psmouse, "contact_cnt = %d\n",
  403. report_data->contact_cnt);
  404. if (n > CYTP_MAX_MT_SLOTS)
  405. n = CYTP_MAX_MT_SLOTS;
  406. for (i = 0; i < n; i++)
  407. psmouse_dbg(psmouse, "contacts[%d] = {%d, %d, %d}\n", i,
  408. report_data->contacts[i].x,
  409. report_data->contacts[i].y,
  410. report_data->contacts[i].z);
  411. psmouse_dbg(psmouse, "left = %d\n", report_data->left);
  412. psmouse_dbg(psmouse, "right = %d\n", report_data->right);
  413. psmouse_dbg(psmouse, "middle = %d\n", report_data->middle);
  414. }
  415. #endif
  416. return 0;
  417. }
  418. static void cypress_process_packet(struct psmouse *psmouse, bool zero_pkt)
  419. {
  420. int i;
  421. struct input_dev *input = psmouse->dev;
  422. struct cytp_data *cytp = psmouse->private;
  423. struct cytp_report_data report_data;
  424. struct cytp_contact *contact;
  425. struct input_mt_pos pos[CYTP_MAX_MT_SLOTS];
  426. int slots[CYTP_MAX_MT_SLOTS];
  427. int n;
  428. if (cypress_parse_packet(psmouse, cytp, &report_data))
  429. return;
  430. n = report_data.contact_cnt;
  431. if (n > CYTP_MAX_MT_SLOTS)
  432. n = CYTP_MAX_MT_SLOTS;
  433. for (i = 0; i < n; i++) {
  434. contact = &report_data.contacts[i];
  435. pos[i].x = contact->x;
  436. pos[i].y = contact->y;
  437. }
  438. input_mt_assign_slots(input, slots, pos, n);
  439. for (i = 0; i < n; i++) {
  440. contact = &report_data.contacts[i];
  441. input_mt_slot(input, slots[i]);
  442. input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
  443. input_report_abs(input, ABS_MT_POSITION_X, contact->x);
  444. input_report_abs(input, ABS_MT_POSITION_Y, contact->y);
  445. input_report_abs(input, ABS_MT_PRESSURE, contact->z);
  446. }
  447. input_mt_sync_frame(input);
  448. input_mt_report_finger_count(input, report_data.contact_cnt);
  449. input_report_key(input, BTN_LEFT, report_data.left);
  450. input_report_key(input, BTN_RIGHT, report_data.right);
  451. input_report_key(input, BTN_MIDDLE, report_data.middle);
  452. input_sync(input);
  453. }
  454. static psmouse_ret_t cypress_validate_byte(struct psmouse *psmouse)
  455. {
  456. int contact_cnt;
  457. int index = psmouse->pktcnt - 1;
  458. unsigned char *packet = psmouse->packet;
  459. struct cytp_data *cytp = psmouse->private;
  460. if (index < 0 || index > cytp->pkt_size)
  461. return PSMOUSE_BAD_DATA;
  462. if (index == 0 && (packet[0] & 0xfc) == 0) {
  463. /* call packet process for reporting finger leave. */
  464. cypress_process_packet(psmouse, 1);
  465. return PSMOUSE_FULL_PACKET;
  466. }
  467. /*
  468. * Perform validation (and adjust packet size) based only on the
  469. * first byte; allow all further bytes through.
  470. */
  471. if (index != 0)
  472. return PSMOUSE_GOOD_DATA;
  473. /*
  474. * If absolute/relative mode bit has not been set yet, just pass
  475. * the byte through.
  476. */
  477. if ((cytp->mode & CYTP_BIT_ABS_REL_MASK) == 0)
  478. return PSMOUSE_GOOD_DATA;
  479. if ((packet[0] & 0x08) == 0x08)
  480. return PSMOUSE_BAD_DATA;
  481. contact_cnt = cypress_get_finger_count(packet[0]);
  482. if (contact_cnt < 0)
  483. return PSMOUSE_BAD_DATA;
  484. if (cytp->mode & CYTP_BIT_ABS_NO_PRESSURE)
  485. cypress_set_packet_size(psmouse, contact_cnt == 2 ? 7 : 4);
  486. else
  487. cypress_set_packet_size(psmouse, contact_cnt == 2 ? 8 : 5);
  488. return PSMOUSE_GOOD_DATA;
  489. }
  490. static psmouse_ret_t cypress_protocol_handler(struct psmouse *psmouse)
  491. {
  492. struct cytp_data *cytp = psmouse->private;
  493. if (psmouse->pktcnt >= cytp->pkt_size) {
  494. cypress_process_packet(psmouse, 0);
  495. return PSMOUSE_FULL_PACKET;
  496. }
  497. return cypress_validate_byte(psmouse);
  498. }
  499. static void cypress_set_rate(struct psmouse *psmouse, unsigned int rate)
  500. {
  501. struct cytp_data *cytp = psmouse->private;
  502. if (rate >= 80) {
  503. psmouse->rate = 80;
  504. cytp->mode |= CYTP_BIT_HIGH_RATE;
  505. } else {
  506. psmouse->rate = 40;
  507. cytp->mode &= ~CYTP_BIT_HIGH_RATE;
  508. }
  509. ps2_command(&psmouse->ps2dev, (unsigned char *)&psmouse->rate,
  510. PSMOUSE_CMD_SETRATE);
  511. }
  512. static void cypress_disconnect(struct psmouse *psmouse)
  513. {
  514. cypress_reset(psmouse);
  515. kfree(psmouse->private);
  516. psmouse->private = NULL;
  517. }
  518. static int cypress_reconnect(struct psmouse *psmouse)
  519. {
  520. int tries = CYTP_PS2_CMD_TRIES;
  521. int rc;
  522. do {
  523. cypress_reset(psmouse);
  524. rc = cypress_detect(psmouse, false);
  525. } while (rc && (--tries > 0));
  526. if (rc) {
  527. psmouse_err(psmouse, "Reconnect: unable to detect trackpad.\n");
  528. return -1;
  529. }
  530. if (cypress_set_absolute_mode(psmouse)) {
  531. psmouse_err(psmouse, "Reconnect: Unable to initialize Cypress absolute mode.\n");
  532. return -1;
  533. }
  534. return 0;
  535. }
  536. int cypress_init(struct psmouse *psmouse)
  537. {
  538. struct cytp_data *cytp;
  539. cytp = (struct cytp_data *)kzalloc(sizeof(struct cytp_data), GFP_KERNEL);
  540. psmouse->private = (void *)cytp;
  541. if (cytp == NULL)
  542. return -ENOMEM;
  543. cypress_reset(psmouse);
  544. psmouse->pktsize = 8;
  545. if (cypress_query_hardware(psmouse)) {
  546. psmouse_err(psmouse, "Unable to query Trackpad hardware.\n");
  547. goto err_exit;
  548. }
  549. if (cypress_set_absolute_mode(psmouse)) {
  550. psmouse_err(psmouse, "init: Unable to initialize Cypress absolute mode.\n");
  551. goto err_exit;
  552. }
  553. if (cypress_set_input_params(psmouse->dev, cytp) < 0) {
  554. psmouse_err(psmouse, "init: Unable to set input params.\n");
  555. goto err_exit;
  556. }
  557. psmouse->model = 1;
  558. psmouse->protocol_handler = cypress_protocol_handler;
  559. psmouse->set_rate = cypress_set_rate;
  560. psmouse->disconnect = cypress_disconnect;
  561. psmouse->reconnect = cypress_reconnect;
  562. psmouse->cleanup = cypress_reset;
  563. psmouse->resync_time = 0;
  564. return 0;
  565. err_exit:
  566. /*
  567. * Reset Cypress Trackpad as a standard mouse. Then
  568. * let psmouse driver commmunicating with it as default PS2 mouse.
  569. */
  570. cypress_reset(psmouse);
  571. psmouse->private = NULL;
  572. kfree(cytp);
  573. return -1;
  574. }
  575. bool cypress_supported(void)
  576. {
  577. return true;
  578. }