focaltech.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Focaltech TouchPad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2014 Red Hat Inc.
  5. * Copyright (c) 2014 Mathias Gottschlag <mgottschlag@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Red Hat authors:
  13. *
  14. * Hans de Goede <hdegoede@redhat.com>
  15. */
  16. #include <linux/device.h>
  17. #include <linux/libps2.h>
  18. #include <linux/input/mt.h>
  19. #include <linux/serio.h>
  20. #include <linux/slab.h>
  21. #include "psmouse.h"
  22. #include "focaltech.h"
  23. static const char * const focaltech_pnp_ids[] = {
  24. "FLT0101",
  25. "FLT0102",
  26. "FLT0103",
  27. NULL
  28. };
  29. /*
  30. * Even if the kernel is built without support for Focaltech PS/2 touchpads (or
  31. * when the real driver fails to recognize the device), we still have to detect
  32. * them in order to avoid further detection attempts confusing the touchpad.
  33. * This way it at least works in PS/2 mouse compatibility mode.
  34. */
  35. int focaltech_detect(struct psmouse *psmouse, bool set_properties)
  36. {
  37. if (!psmouse_matches_pnp_id(psmouse, focaltech_pnp_ids))
  38. return -ENODEV;
  39. if (set_properties) {
  40. psmouse->vendor = "FocalTech";
  41. psmouse->name = "FocalTech Touchpad";
  42. }
  43. return 0;
  44. }
  45. static void focaltech_reset(struct psmouse *psmouse)
  46. {
  47. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  48. psmouse_reset(psmouse);
  49. }
  50. #ifdef CONFIG_MOUSE_PS2_FOCALTECH
  51. /*
  52. * Packet types - the numbers are not consecutive, so we might be missing
  53. * something here.
  54. */
  55. #define FOC_TOUCH 0x3 /* bitmap of active fingers */
  56. #define FOC_ABS 0x6 /* absolute position of one finger */
  57. #define FOC_REL 0x9 /* relative position of 1-2 fingers */
  58. #define FOC_MAX_FINGERS 5
  59. #define FOC_MAX_X 2431
  60. #define FOC_MAX_Y 1663
  61. /*
  62. * Current state of a single finger on the touchpad.
  63. */
  64. struct focaltech_finger_state {
  65. /* The touchpad has generated a touch event for the finger */
  66. bool active;
  67. /*
  68. * The touchpad has sent position data for the finger. The
  69. * flag is 0 when the finger is not active, and there is a
  70. * time between the first touch event for the finger and the
  71. * following absolute position packet for the finger where the
  72. * touchpad has declared the finger to be valid, but we do not
  73. * have any valid position yet.
  74. */
  75. bool valid;
  76. /*
  77. * Absolute position (from the bottom left corner) of the
  78. * finger.
  79. */
  80. unsigned int x;
  81. unsigned int y;
  82. };
  83. /*
  84. * Description of the current state of the touchpad hardware.
  85. */
  86. struct focaltech_hw_state {
  87. /*
  88. * The touchpad tracks the positions of the fingers for us,
  89. * the array indices correspond to the finger indices returned
  90. * in the report packages.
  91. */
  92. struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
  93. /* True if the clickpad has been pressed. */
  94. bool pressed;
  95. };
  96. struct focaltech_data {
  97. unsigned int x_max, y_max;
  98. struct focaltech_hw_state state;
  99. };
  100. static void focaltech_report_state(struct psmouse *psmouse)
  101. {
  102. struct focaltech_data *priv = psmouse->private;
  103. struct focaltech_hw_state *state = &priv->state;
  104. struct input_dev *dev = psmouse->dev;
  105. int i;
  106. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  107. struct focaltech_finger_state *finger = &state->fingers[i];
  108. bool active = finger->active && finger->valid;
  109. input_mt_slot(dev, i);
  110. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  111. if (active) {
  112. input_report_abs(dev, ABS_MT_POSITION_X, finger->x);
  113. input_report_abs(dev, ABS_MT_POSITION_Y,
  114. FOC_MAX_Y - finger->y);
  115. }
  116. }
  117. input_mt_report_pointer_emulation(dev, true);
  118. input_report_key(psmouse->dev, BTN_LEFT, state->pressed);
  119. input_sync(psmouse->dev);
  120. }
  121. static void focaltech_process_touch_packet(struct psmouse *psmouse,
  122. unsigned char *packet)
  123. {
  124. struct focaltech_data *priv = psmouse->private;
  125. struct focaltech_hw_state *state = &priv->state;
  126. unsigned char fingers = packet[1];
  127. int i;
  128. state->pressed = (packet[0] >> 4) & 1;
  129. /* the second byte contains a bitmap of all fingers touching the pad */
  130. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  131. state->fingers[i].active = fingers & 0x1;
  132. if (!state->fingers[i].active) {
  133. /*
  134. * Even when the finger becomes active again, we still
  135. * will have to wait for the first valid position.
  136. */
  137. state->fingers[i].valid = false;
  138. }
  139. fingers >>= 1;
  140. }
  141. }
  142. static void focaltech_process_abs_packet(struct psmouse *psmouse,
  143. unsigned char *packet)
  144. {
  145. struct focaltech_data *priv = psmouse->private;
  146. struct focaltech_hw_state *state = &priv->state;
  147. unsigned int finger;
  148. finger = (packet[1] >> 4) - 1;
  149. if (finger >= FOC_MAX_FINGERS) {
  150. psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
  151. finger);
  152. return;
  153. }
  154. state->pressed = (packet[0] >> 4) & 1;
  155. /*
  156. * packet[5] contains some kind of tool size in the most
  157. * significant nibble. 0xff is a special value (latching) that
  158. * signals a large contact area.
  159. */
  160. if (packet[5] == 0xff) {
  161. state->fingers[finger].valid = false;
  162. return;
  163. }
  164. state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
  165. state->fingers[finger].y = (packet[3] << 8) | packet[4];
  166. state->fingers[finger].valid = true;
  167. }
  168. static void focaltech_process_rel_packet(struct psmouse *psmouse,
  169. unsigned char *packet)
  170. {
  171. struct focaltech_data *priv = psmouse->private;
  172. struct focaltech_hw_state *state = &priv->state;
  173. int finger1, finger2;
  174. state->pressed = packet[0] >> 7;
  175. finger1 = ((packet[0] >> 4) & 0x7) - 1;
  176. if (finger1 < FOC_MAX_FINGERS) {
  177. state->fingers[finger1].x += (char)packet[1];
  178. state->fingers[finger1].y += (char)packet[2];
  179. } else {
  180. psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
  181. finger1);
  182. }
  183. /*
  184. * If there is an odd number of fingers, the last relative
  185. * packet only contains one finger. In this case, the second
  186. * finger index in the packet is 0 (we subtract 1 in the lines
  187. * above to create array indices, so the finger will overflow
  188. * and be above FOC_MAX_FINGERS).
  189. */
  190. finger2 = ((packet[3] >> 4) & 0x7) - 1;
  191. if (finger2 < FOC_MAX_FINGERS) {
  192. state->fingers[finger2].x += (char)packet[4];
  193. state->fingers[finger2].y += (char)packet[5];
  194. }
  195. }
  196. static void focaltech_process_packet(struct psmouse *psmouse)
  197. {
  198. unsigned char *packet = psmouse->packet;
  199. switch (packet[0] & 0xf) {
  200. case FOC_TOUCH:
  201. focaltech_process_touch_packet(psmouse, packet);
  202. break;
  203. case FOC_ABS:
  204. focaltech_process_abs_packet(psmouse, packet);
  205. break;
  206. case FOC_REL:
  207. focaltech_process_rel_packet(psmouse, packet);
  208. break;
  209. default:
  210. psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
  211. break;
  212. }
  213. focaltech_report_state(psmouse);
  214. }
  215. static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
  216. {
  217. if (psmouse->pktcnt >= 6) { /* Full packet received */
  218. focaltech_process_packet(psmouse);
  219. return PSMOUSE_FULL_PACKET;
  220. }
  221. /*
  222. * We might want to do some validation of the data here, but
  223. * we do not know the protocol well enough
  224. */
  225. return PSMOUSE_GOOD_DATA;
  226. }
  227. static int focaltech_switch_protocol(struct psmouse *psmouse)
  228. {
  229. struct ps2dev *ps2dev = &psmouse->ps2dev;
  230. unsigned char param[3];
  231. param[0] = 0;
  232. if (ps2_command(ps2dev, param, 0x10f8))
  233. return -EIO;
  234. if (ps2_command(ps2dev, param, 0x10f8))
  235. return -EIO;
  236. if (ps2_command(ps2dev, param, 0x10f8))
  237. return -EIO;
  238. param[0] = 1;
  239. if (ps2_command(ps2dev, param, 0x10f8))
  240. return -EIO;
  241. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
  242. return -EIO;
  243. if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
  244. return -EIO;
  245. return 0;
  246. }
  247. static void focaltech_disconnect(struct psmouse *psmouse)
  248. {
  249. focaltech_reset(psmouse);
  250. kfree(psmouse->private);
  251. psmouse->private = NULL;
  252. }
  253. static int focaltech_reconnect(struct psmouse *psmouse)
  254. {
  255. int error;
  256. focaltech_reset(psmouse);
  257. error = focaltech_switch_protocol(psmouse);
  258. if (error) {
  259. psmouse_err(psmouse, "Unable to initialize the device\n");
  260. return error;
  261. }
  262. return 0;
  263. }
  264. static void focaltech_set_input_params(struct psmouse *psmouse)
  265. {
  266. struct input_dev *dev = psmouse->dev;
  267. struct focaltech_data *priv = psmouse->private;
  268. /*
  269. * Undo part of setup done for us by psmouse core since touchpad
  270. * is not a relative device.
  271. */
  272. __clear_bit(EV_REL, dev->evbit);
  273. __clear_bit(REL_X, dev->relbit);
  274. __clear_bit(REL_Y, dev->relbit);
  275. __clear_bit(BTN_RIGHT, dev->keybit);
  276. __clear_bit(BTN_MIDDLE, dev->keybit);
  277. /*
  278. * Now set up our capabilities.
  279. */
  280. __set_bit(EV_ABS, dev->evbit);
  281. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  282. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  283. input_mt_init_slots(dev, 5, INPUT_MT_POINTER);
  284. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  285. }
  286. static int focaltech_read_register(struct ps2dev *ps2dev, int reg,
  287. unsigned char *param)
  288. {
  289. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
  290. return -EIO;
  291. param[0] = 0;
  292. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  293. return -EIO;
  294. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  295. return -EIO;
  296. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  297. return -EIO;
  298. param[0] = reg;
  299. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  300. return -EIO;
  301. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  302. return -EIO;
  303. return 0;
  304. }
  305. static int focaltech_read_size(struct psmouse *psmouse)
  306. {
  307. struct ps2dev *ps2dev = &psmouse->ps2dev;
  308. struct focaltech_data *priv = psmouse->private;
  309. char param[3];
  310. if (focaltech_read_register(ps2dev, 2, param))
  311. return -EIO;
  312. /* not sure whether this is 100% correct */
  313. priv->x_max = (unsigned char)param[1] * 128;
  314. priv->y_max = (unsigned char)param[2] * 128;
  315. return 0;
  316. }
  317. int focaltech_init(struct psmouse *psmouse)
  318. {
  319. struct focaltech_data *priv;
  320. int error;
  321. psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
  322. GFP_KERNEL);
  323. if (!priv)
  324. return -ENOMEM;
  325. focaltech_reset(psmouse);
  326. error = focaltech_read_size(psmouse);
  327. if (error) {
  328. psmouse_err(psmouse,
  329. "Unable to read the size of the touchpad\n");
  330. goto fail;
  331. }
  332. error = focaltech_switch_protocol(psmouse);
  333. if (error) {
  334. psmouse_err(psmouse, "Unable to initialize the device\n");
  335. goto fail;
  336. }
  337. focaltech_set_input_params(psmouse);
  338. psmouse->protocol_handler = focaltech_process_byte;
  339. psmouse->pktsize = 6;
  340. psmouse->disconnect = focaltech_disconnect;
  341. psmouse->reconnect = focaltech_reconnect;
  342. psmouse->cleanup = focaltech_reset;
  343. /* resync is not supported yet */
  344. psmouse->resync_time = 0;
  345. return 0;
  346. fail:
  347. focaltech_reset(psmouse);
  348. kfree(priv);
  349. return error;
  350. }
  351. #else /* CONFIG_MOUSE_PS2_FOCALTECH */
  352. int focaltech_init(struct psmouse *psmouse)
  353. {
  354. focaltech_reset(psmouse);
  355. return 0;
  356. }
  357. #endif /* CONFIG_MOUSE_PS2_FOCALTECH */