focaltech.c 11 KB

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