focaltech.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 = "Touchpad";
  42. }
  43. return 0;
  44. }
  45. #ifdef CONFIG_MOUSE_PS2_FOCALTECH
  46. /*
  47. * Packet types - the numbers are not consecutive, so we might be missing
  48. * something here.
  49. */
  50. #define FOC_TOUCH 0x3 /* bitmap of active fingers */
  51. #define FOC_ABS 0x6 /* absolute position of one finger */
  52. #define FOC_REL 0x9 /* relative position of 1-2 fingers */
  53. #define FOC_MAX_FINGERS 5
  54. /*
  55. * Current state of a single finger on the touchpad.
  56. */
  57. struct focaltech_finger_state {
  58. /* The touchpad has generated a touch event for the finger */
  59. bool active;
  60. /*
  61. * The touchpad has sent position data for the finger. The
  62. * flag is 0 when the finger is not active, and there is a
  63. * time between the first touch event for the finger and the
  64. * following absolute position packet for the finger where the
  65. * touchpad has declared the finger to be valid, but we do not
  66. * have any valid position yet.
  67. */
  68. bool valid;
  69. /*
  70. * Absolute position (from the bottom left corner) of the
  71. * finger.
  72. */
  73. unsigned int x;
  74. unsigned int y;
  75. };
  76. /*
  77. * Description of the current state of the touchpad hardware.
  78. */
  79. struct focaltech_hw_state {
  80. /*
  81. * The touchpad tracks the positions of the fingers for us,
  82. * the array indices correspond to the finger indices returned
  83. * in the report packages.
  84. */
  85. struct focaltech_finger_state fingers[FOC_MAX_FINGERS];
  86. /*
  87. * Finger width 0-7 and 15 for a very big contact area.
  88. * 15 value stays until the finger is released.
  89. * Width is reported only in absolute packets.
  90. * Since hardware reports width only for last touching finger,
  91. * there is no need to store width for every specific finger,
  92. * so we keep only last value reported.
  93. */
  94. unsigned int width;
  95. /* True if the clickpad has been pressed. */
  96. bool pressed;
  97. };
  98. struct focaltech_data {
  99. unsigned int x_max, y_max;
  100. struct focaltech_hw_state state;
  101. };
  102. static void focaltech_report_state(struct psmouse *psmouse)
  103. {
  104. struct focaltech_data *priv = psmouse->private;
  105. struct focaltech_hw_state *state = &priv->state;
  106. struct input_dev *dev = psmouse->dev;
  107. int i;
  108. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  109. struct focaltech_finger_state *finger = &state->fingers[i];
  110. bool active = finger->active && finger->valid;
  111. input_mt_slot(dev, i);
  112. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  113. if (active) {
  114. unsigned int clamped_x, clamped_y;
  115. /*
  116. * The touchpad might report invalid data, so we clamp
  117. * the resulting values so that we do not confuse
  118. * userspace.
  119. */
  120. clamped_x = clamp(finger->x, 0U, priv->x_max);
  121. clamped_y = clamp(finger->y, 0U, priv->y_max);
  122. input_report_abs(dev, ABS_MT_POSITION_X, clamped_x);
  123. input_report_abs(dev, ABS_MT_POSITION_Y,
  124. priv->y_max - clamped_y);
  125. input_report_abs(dev, ABS_TOOL_WIDTH, state->width);
  126. }
  127. }
  128. input_mt_report_pointer_emulation(dev, true);
  129. input_report_key(dev, BTN_LEFT, state->pressed);
  130. input_sync(dev);
  131. }
  132. static void focaltech_process_touch_packet(struct psmouse *psmouse,
  133. unsigned char *packet)
  134. {
  135. struct focaltech_data *priv = psmouse->private;
  136. struct focaltech_hw_state *state = &priv->state;
  137. unsigned char fingers = packet[1];
  138. int i;
  139. state->pressed = (packet[0] >> 4) & 1;
  140. /* the second byte contains a bitmap of all fingers touching the pad */
  141. for (i = 0; i < FOC_MAX_FINGERS; i++) {
  142. state->fingers[i].active = fingers & 0x1;
  143. if (!state->fingers[i].active) {
  144. /*
  145. * Even when the finger becomes active again, we still
  146. * will have to wait for the first valid position.
  147. */
  148. state->fingers[i].valid = false;
  149. }
  150. fingers >>= 1;
  151. }
  152. }
  153. static void focaltech_process_abs_packet(struct psmouse *psmouse,
  154. unsigned char *packet)
  155. {
  156. struct focaltech_data *priv = psmouse->private;
  157. struct focaltech_hw_state *state = &priv->state;
  158. unsigned int finger;
  159. finger = (packet[1] >> 4) - 1;
  160. if (finger >= FOC_MAX_FINGERS) {
  161. psmouse_err(psmouse, "Invalid finger in abs packet: %d\n",
  162. finger);
  163. return;
  164. }
  165. state->pressed = (packet[0] >> 4) & 1;
  166. state->fingers[finger].x = ((packet[1] & 0xf) << 8) | packet[2];
  167. state->fingers[finger].y = (packet[3] << 8) | packet[4];
  168. state->width = packet[5] >> 4;
  169. state->fingers[finger].valid = true;
  170. }
  171. static void focaltech_process_rel_packet(struct psmouse *psmouse,
  172. unsigned char *packet)
  173. {
  174. struct focaltech_data *priv = psmouse->private;
  175. struct focaltech_hw_state *state = &priv->state;
  176. int finger1, finger2;
  177. state->pressed = packet[0] >> 7;
  178. finger1 = ((packet[0] >> 4) & 0x7) - 1;
  179. if (finger1 < FOC_MAX_FINGERS) {
  180. state->fingers[finger1].x += (char)packet[1];
  181. state->fingers[finger1].y += (char)packet[2];
  182. } else {
  183. psmouse_err(psmouse, "First finger in rel packet invalid: %d\n",
  184. finger1);
  185. }
  186. /*
  187. * If there is an odd number of fingers, the last relative
  188. * packet only contains one finger. In this case, the second
  189. * finger index in the packet is 0 (we subtract 1 in the lines
  190. * above to create array indices, so the finger will overflow
  191. * and be above FOC_MAX_FINGERS).
  192. */
  193. finger2 = ((packet[3] >> 4) & 0x7) - 1;
  194. if (finger2 < FOC_MAX_FINGERS) {
  195. state->fingers[finger2].x += (char)packet[4];
  196. state->fingers[finger2].y += (char)packet[5];
  197. }
  198. }
  199. static void focaltech_process_packet(struct psmouse *psmouse)
  200. {
  201. unsigned char *packet = psmouse->packet;
  202. switch (packet[0] & 0xf) {
  203. case FOC_TOUCH:
  204. focaltech_process_touch_packet(psmouse, packet);
  205. break;
  206. case FOC_ABS:
  207. focaltech_process_abs_packet(psmouse, packet);
  208. break;
  209. case FOC_REL:
  210. focaltech_process_rel_packet(psmouse, packet);
  211. break;
  212. default:
  213. psmouse_err(psmouse, "Unknown packet type: %02x\n", packet[0]);
  214. break;
  215. }
  216. focaltech_report_state(psmouse);
  217. }
  218. static psmouse_ret_t focaltech_process_byte(struct psmouse *psmouse)
  219. {
  220. if (psmouse->pktcnt >= 6) { /* Full packet received */
  221. focaltech_process_packet(psmouse);
  222. return PSMOUSE_FULL_PACKET;
  223. }
  224. /*
  225. * We might want to do some validation of the data here, but
  226. * we do not know the protocol well enough
  227. */
  228. return PSMOUSE_GOOD_DATA;
  229. }
  230. static int focaltech_switch_protocol(struct psmouse *psmouse)
  231. {
  232. struct ps2dev *ps2dev = &psmouse->ps2dev;
  233. unsigned char param[3];
  234. param[0] = 0;
  235. if (ps2_command(ps2dev, param, 0x10f8))
  236. return -EIO;
  237. if (ps2_command(ps2dev, param, 0x10f8))
  238. return -EIO;
  239. if (ps2_command(ps2dev, param, 0x10f8))
  240. return -EIO;
  241. param[0] = 1;
  242. if (ps2_command(ps2dev, param, 0x10f8))
  243. return -EIO;
  244. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETSCALE11))
  245. return -EIO;
  246. if (ps2_command(ps2dev, param, PSMOUSE_CMD_ENABLE))
  247. return -EIO;
  248. return 0;
  249. }
  250. static void focaltech_reset(struct psmouse *psmouse)
  251. {
  252. ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
  253. psmouse_reset(psmouse);
  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. static void focaltech_set_resolution(struct psmouse *psmouse,
  327. unsigned int resolution)
  328. {
  329. /* not supported yet */
  330. }
  331. static void focaltech_set_rate(struct psmouse *psmouse, unsigned int rate)
  332. {
  333. /* not supported yet */
  334. }
  335. static void focaltech_set_scale(struct psmouse *psmouse,
  336. enum psmouse_scale scale)
  337. {
  338. /* not supported yet */
  339. }
  340. int focaltech_init(struct psmouse *psmouse)
  341. {
  342. struct focaltech_data *priv;
  343. int error;
  344. psmouse->private = priv = kzalloc(sizeof(struct focaltech_data),
  345. GFP_KERNEL);
  346. if (!priv)
  347. return -ENOMEM;
  348. focaltech_reset(psmouse);
  349. error = focaltech_read_size(psmouse);
  350. if (error) {
  351. psmouse_err(psmouse,
  352. "Unable to read the size of the touchpad\n");
  353. goto fail;
  354. }
  355. error = focaltech_switch_protocol(psmouse);
  356. if (error) {
  357. psmouse_err(psmouse, "Unable to initialize the device\n");
  358. goto fail;
  359. }
  360. focaltech_set_input_params(psmouse);
  361. psmouse->protocol_handler = focaltech_process_byte;
  362. psmouse->pktsize = 6;
  363. psmouse->disconnect = focaltech_disconnect;
  364. psmouse->reconnect = focaltech_reconnect;
  365. psmouse->cleanup = focaltech_reset;
  366. /* resync is not supported yet */
  367. psmouse->resync_time = 0;
  368. /*
  369. * rate/resolution/scale changes are not supported yet, and
  370. * the generic implementations of these functions seem to
  371. * confuse some touchpads
  372. */
  373. psmouse->set_resolution = focaltech_set_resolution;
  374. psmouse->set_rate = focaltech_set_rate;
  375. psmouse->set_scale = focaltech_set_scale;
  376. return 0;
  377. fail:
  378. focaltech_reset(psmouse);
  379. kfree(priv);
  380. return error;
  381. }
  382. #endif /* CONFIG_MOUSE_PS2_FOCALTECH */