focaltech.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /*
  60. * Current state of a single finger on the touchpad.
  61. */
  62. struct focaltech_finger_state {
  63. /* The touchpad has generated a touch event for the finger */
  64. bool active;
  65. /*
  66. * The touchpad has sent position data for the finger. The
  67. * flag is 0 when the finger is not active, and there is a
  68. * time between the first touch event for the finger and the
  69. * following absolute position packet for the finger where the
  70. * touchpad has declared the finger to be valid, but we do not
  71. * have any valid position yet.
  72. */
  73. bool valid;
  74. /*
  75. * Absolute position (from the bottom left corner) of the
  76. * finger.
  77. */
  78. unsigned int x;
  79. unsigned int y;
  80. };
  81. /*
  82. * Description of the current state of the touchpad hardware.
  83. */
  84. struct focaltech_hw_state {
  85. /*
  86. * The touchpad tracks the positions of the fingers for us,
  87. * the array indices correspond to the finger indices returned
  88. * in the report packages.
  89. */
  90. struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
  91. /*
  92. * Finger width 0-7 and 15 for a very big contact area.
  93. * 15 value stays until the finger is released.
  94. * Width is reported only in absolute packets.
  95. * Since hardware reports width only for last touching finger,
  96. * there is no need to store width for every specific finger,
  97. * so we keep only last value reported.
  98. */
  99. unsigned int width;
  100. /* True if the clickpad has been pressed. */
  101. bool pressed;
  102. };
  103. struct focaltech_data {
  104. unsigned int x_max, y_max;
  105. struct focaltech_hw_state state;
  106. };
  107. static void focaltech_report_state(struct psmouse *psmouse)
  108. {
  109. struct focaltech_data *priv = psmouse->private;
  110. struct focaltech_hw_state *state = &priv->state;
  111. struct input_dev *dev = psmouse->dev;
  112. int i;
  113. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  114. struct focaltech_finger_state *finger = &state->fingers[i];
  115. bool active = finger->active && finger->valid;
  116. input_mt_slot(dev, i);
  117. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  118. if (active) {
  119. unsigned int clamped_x, clamped_y;
  120. /*
  121. * The touchpad might report invalid data, so we clamp
  122. * the resulting values so that we do not confuse
  123. * userspace.
  124. */
  125. clamped_x = clamp(finger->x, 0U, priv->x_max);
  126. clamped_y = clamp(finger->y, 0U, priv->y_max);
  127. input_report_abs(dev, ABS_MT_POSITION_X, clamped_x);
  128. input_report_abs(dev, ABS_MT_POSITION_Y,
  129. priv->y_max - clamped_y);
  130. input_report_abs(dev, ABS_TOOL_WIDTH, state->width);
  131. }
  132. }
  133. input_mt_report_pointer_emulation(dev, true);
  134. input_report_key(psmouse->dev, BTN_LEFT, state->pressed);
  135. input_sync(psmouse->dev);
  136. }
  137. static void focaltech_process_touch_packet(struct psmouse *psmouse,
  138. unsigned char *packet)
  139. {
  140. struct focaltech_data *priv = psmouse->private;
  141. struct focaltech_hw_state *state = &priv->state;
  142. unsigned char fingers = packet[1];
  143. int i;
  144. state->pressed = (packet[0] >> 4) & 1;
  145. /* the second byte contains a bitmap of all fingers touching the pad */
  146. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  147. state->fingers[i].active = fingers & 0x1;
  148. if (!state->fingers[i].active) {
  149. /*
  150. * Even when the finger becomes active again, we still
  151. * will have to wait for the first valid position.
  152. */
  153. state->fingers[i].valid = false;
  154. }
  155. fingers >>= 1;
  156. }
  157. }
  158. static void focaltech_process_abs_packet(struct psmouse *psmouse,
  159. unsigned char *packet)
  160. {
  161. struct focaltech_data *priv = psmouse->private;
  162. struct focaltech_hw_state *state = &priv->state;
  163. unsigned int finger;
  164. finger = (packet[1] >> 4) - 1;
  165. if (finger >= FOC_MAX_FINGERS) {
  166. psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
  167. finger);
  168. return;
  169. }
  170. state->pressed = (packet[0] >> 4) & 1;
  171. state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
  172. state->fingers[finger].y = (packet[3] << 8) | packet[4];
  173. state->width = packet[5] >> 4;
  174. state->fingers[finger].valid = true;
  175. }
  176. static void focaltech_process_rel_packet(struct psmouse *psmouse,
  177. unsigned char *packet)
  178. {
  179. struct focaltech_data *priv = psmouse->private;
  180. struct focaltech_hw_state *state = &priv->state;
  181. int finger1, finger2;
  182. state->pressed = packet[0] >> 7;
  183. finger1 = ((packet[0] >> 4) & 0x7) - 1;
  184. if (finger1 < FOC_MAX_FINGERS) {
  185. state->fingers[finger1].x += (char)packet[1];
  186. state->fingers[finger1].y += (char)packet[2];
  187. } else {
  188. psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
  189. finger1);
  190. }
  191. /*
  192. * If there is an odd number of fingers, the last relative
  193. * packet only contains one finger. In this case, the second
  194. * finger index in the packet is 0 (we subtract 1 in the lines
  195. * above to create array indices, so the finger will overflow
  196. * and be above FOC_MAX_FINGERS).
  197. */
  198. finger2 = ((packet[3] >> 4) & 0x7) - 1;
  199. if (finger2 < FOC_MAX_FINGERS) {
  200. state->fingers[finger2].x += (char)packet[4];
  201. state->fingers[finger2].y += (char)packet[5];
  202. }
  203. }
  204. static void focaltech_process_packet(struct psmouse *psmouse)
  205. {
  206. unsigned char *packet = psmouse->packet;
  207. switch (packet[0] & 0xf) {
  208. case FOC_TOUCH:
  209. focaltech_process_touch_packet(psmouse, packet);
  210. break;
  211. case FOC_ABS:
  212. focaltech_process_abs_packet(psmouse, packet);
  213. break;
  214. case FOC_REL:
  215. focaltech_process_rel_packet(psmouse, packet);
  216. break;
  217. default:
  218. psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
  219. break;
  220. }
  221. focaltech_report_state(psmouse);
  222. }
  223. static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
  224. {
  225. if (psmouse->pktcnt >= 6) { /* Full packet received */
  226. focaltech_process_packet(psmouse);
  227. return PSMOUSE_FULL_PACKET;
  228. }
  229. /*
  230. * We might want to do some validation of the data here, but
  231. * we do not know the protocol well enough
  232. */
  233. return PSMOUSE_GOOD_DATA;
  234. }
  235. static int focaltech_switch_protocol(struct psmouse *psmouse)
  236. {
  237. struct ps2dev *ps2dev = &psmouse->ps2dev;
  238. unsigned char param[3];
  239. param[0] = 0;
  240. if (ps2_command(ps2dev, param, 0x10f8))
  241. return -EIO;
  242. if (ps2_command(ps2dev, param, 0x10f8))
  243. return -EIO;
  244. if (ps2_command(ps2dev, param, 0x10f8))
  245. return -EIO;
  246. param[0] = 1;
  247. if (ps2_command(ps2dev, param, 0x10f8))
  248. return -EIO;
  249. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
  250. return -EIO;
  251. if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
  252. return -EIO;
  253. return 0;
  254. }
  255. static void focaltech_disconnect(struct psmouse *psmouse)
  256. {
  257. focaltech_reset(psmouse);
  258. kfree(psmouse->private);
  259. psmouse->private = NULL;
  260. }
  261. static int focaltech_reconnect(struct psmouse *psmouse)
  262. {
  263. int error;
  264. focaltech_reset(psmouse);
  265. error = focaltech_switch_protocol(psmouse);
  266. if (error) {
  267. psmouse_err(psmouse, "Unable to initialize the device\n");
  268. return error;
  269. }
  270. return 0;
  271. }
  272. static void focaltech_set_input_params(struct psmouse *psmouse)
  273. {
  274. struct input_dev *dev = psmouse->dev;
  275. struct focaltech_data *priv = psmouse->private;
  276. /*
  277. * Undo part of setup done for us by psmouse core since touchpad
  278. * is not a relative device.
  279. */
  280. __clear_bit(EV_REL, dev->evbit);
  281. __clear_bit(REL_X, dev->relbit);
  282. __clear_bit(REL_Y, dev->relbit);
  283. __clear_bit(BTN_RIGHT, dev->keybit);
  284. __clear_bit(BTN_MIDDLE, dev->keybit);
  285. /*
  286. * Now set up our capabilities.
  287. */
  288. __set_bit(EV_ABS, dev->evbit);
  289. input_set_abs_params(dev, ABS_MT_POSITION_X, 0, priv->x_max, 0, 0);
  290. input_set_abs_params(dev, ABS_MT_POSITION_Y, 0, priv->y_max, 0, 0);
  291. input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
  292. input_mt_init_slots(dev, 5, INPUT_MT_POINTER);
  293. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  294. }
  295. static int focaltech_read_register(struct ps2dev *ps2dev, int reg,
  296. unsigned char *param)
  297. {
  298. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
  299. return -EIO;
  300. param[0] = 0;
  301. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  302. return -EIO;
  303. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  304. return -EIO;
  305. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  306. return -EIO;
  307. param[0] = reg;
  308. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES))
  309. return -EIO;
  310. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  311. return -EIO;
  312. return 0;
  313. }
  314. static int focaltech_read_size(struct psmouse *psmouse)
  315. {
  316. struct ps2dev *ps2dev = &psmouse->ps2dev;
  317. struct focaltech_data *priv = psmouse->private;
  318. char param[3];
  319. if (focaltech_read_register(ps2dev, 2, param))
  320. return -EIO;
  321. /* not sure whether this is 100% correct */
  322. priv->x_max = (unsigned char)param[1] * 128;
  323. priv->y_max = (unsigned char)param[2] * 128;
  324. return 0;
  325. }
  326. void focaltech_set_resolution(struct psmouse *psmouse, unsigned int resolution)
  327. {
  328. /* not supported yet */
  329. }
  330. static void focaltech_set_rate(struct psmouse *psmouse, unsigned int rate)
  331. {
  332. /* not supported yet */
  333. }
  334. static void focaltech_set_scale(struct psmouse *psmouse,
  335. enum psmouse_scale scale)
  336. {
  337. /* not supported yet */
  338. }
  339. int focaltech_init(struct psmouse *psmouse)
  340. {
  341. struct focaltech_data *priv;
  342. int error;
  343. psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
  344. GFP_KERNEL);
  345. if (!priv)
  346. return -ENOMEM;
  347. focaltech_reset(psmouse);
  348. error = focaltech_read_size(psmouse);
  349. if (error) {
  350. psmouse_err(psmouse,
  351. "Unable to read the size of the touchpad\n");
  352. goto fail;
  353. }
  354. error = focaltech_switch_protocol(psmouse);
  355. if (error) {
  356. psmouse_err(psmouse, "Unable to initialize the device\n");
  357. goto fail;
  358. }
  359. focaltech_set_input_params(psmouse);
  360. psmouse->protocol_handler = focaltech_process_byte;
  361. psmouse->pktsize = 6;
  362. psmouse->disconnect = focaltech_disconnect;
  363. psmouse->reconnect = focaltech_reconnect;
  364. psmouse->cleanup = focaltech_reset;
  365. /* resync is not supported yet */
  366. psmouse->resync_time = 0;
  367. /*
  368. * rate/resolution/scale changes are not supported yet, and
  369. * the generic implementations of these functions seem to
  370. * confuse some touchpads
  371. */
  372. psmouse->set_resolution = focaltech_set_resolution;
  373. psmouse->set_rate = focaltech_set_rate;
  374. psmouse->set_scale = focaltech_set_scale;
  375. return 0;
  376. fail:
  377. focaltech_reset(psmouse);
  378. kfree(priv);
  379. return error;
  380. }
  381. #else /* CONFIG_MOUSE_PS2_FOCALTECH */
  382. int focaltech_init(struct psmouse *psmouse)
  383. {
  384. focaltech_reset(psmouse);
  385. return 0;
  386. }
  387. #endif /* CONFIG_MOUSE_PS2_FOCALTECH */