synaptics.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * Synaptics TouchPad PS/2 mouse driver
  3. *
  4. * 2003 Dmitry Torokhov <dtor@mail.ru>
  5. * Added support for pass-through port. Special thanks to Peter Berg Larsen
  6. * for explaining various Synaptics quirks.
  7. *
  8. * 2003 Peter Osterlund <petero2@telia.com>
  9. * Ported to 2.5 input device infrastructure.
  10. *
  11. * Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
  12. * start merging tpconfig and gpm code to a xfree-input module
  13. * adding some changes and extensions (ex. 3rd and 4th button)
  14. *
  15. * Copyright (c) 1997 C. Scott Ananian <cananian@alumni.priceton.edu>
  16. * Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
  17. * code for the special synaptics commands (from the tpconfig-source)
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License version 2 as published by
  21. * the Free Software Foundation.
  22. *
  23. * Trademarks are the property of their respective owners.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/dmi.h>
  28. #include <linux/input/mt.h>
  29. #include <linux/serio.h>
  30. #include <linux/libps2.h>
  31. #include <linux/slab.h>
  32. #include "psmouse.h"
  33. #include "synaptics.h"
  34. /*
  35. * The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
  36. * section 2.3.2, which says that they should be valid regardless of the
  37. * actual size of the sensor.
  38. * Note that newer firmware allows querying device for maximum useable
  39. * coordinates.
  40. */
  41. #define XMIN 0
  42. #define XMAX 6143
  43. #define YMIN 0
  44. #define YMAX 6143
  45. #define XMIN_NOMINAL 1472
  46. #define XMAX_NOMINAL 5472
  47. #define YMIN_NOMINAL 1408
  48. #define YMAX_NOMINAL 4448
  49. /* Size in bits of absolute position values reported by the hardware */
  50. #define ABS_POS_BITS 13
  51. /*
  52. * These values should represent the absolute maximum value that will
  53. * be reported for a positive position value. Some Synaptics firmware
  54. * uses this value to indicate a finger near the edge of the touchpad
  55. * whose precise position cannot be determined.
  56. *
  57. * At least one touchpad is known to report positions in excess of this
  58. * value which are actually negative values truncated to the 13-bit
  59. * reporting range. These values have never been observed to be lower
  60. * than 8184 (i.e. -8), so we treat all values greater than 8176 as
  61. * negative and any other value as positive.
  62. */
  63. #define X_MAX_POSITIVE 8176
  64. #define Y_MAX_POSITIVE 8176
  65. /*****************************************************************************
  66. * Stuff we need even when we do not want native Synaptics support
  67. ****************************************************************************/
  68. /*
  69. * Set the synaptics touchpad mode byte by special commands
  70. */
  71. static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
  72. {
  73. unsigned char param[1];
  74. if (psmouse_sliced_command(psmouse, mode))
  75. return -1;
  76. param[0] = SYN_PS_SET_MODE2;
  77. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
  78. return -1;
  79. return 0;
  80. }
  81. int synaptics_detect(struct psmouse *psmouse, bool set_properties)
  82. {
  83. struct ps2dev *ps2dev = &psmouse->ps2dev;
  84. unsigned char param[4];
  85. param[0] = 0;
  86. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  87. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  88. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  89. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  90. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
  91. if (param[1] != 0x47)
  92. return -ENODEV;
  93. if (set_properties) {
  94. psmouse->vendor = "Synaptics";
  95. psmouse->name = "TouchPad";
  96. }
  97. return 0;
  98. }
  99. void synaptics_reset(struct psmouse *psmouse)
  100. {
  101. /* reset touchpad back to relative mode, gestures enabled */
  102. synaptics_mode_cmd(psmouse, 0);
  103. }
  104. #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
  105. /*****************************************************************************
  106. * Synaptics communications functions
  107. ****************************************************************************/
  108. /*
  109. * Synaptics touchpads report the y coordinate from bottom to top, which is
  110. * opposite from what userspace expects.
  111. * This function is used to invert y before reporting.
  112. */
  113. static int synaptics_invert_y(int y)
  114. {
  115. return YMAX_NOMINAL + YMIN_NOMINAL - y;
  116. }
  117. /*
  118. * Send a command to the synpatics touchpad by special commands
  119. */
  120. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
  121. {
  122. if (psmouse_sliced_command(psmouse, c))
  123. return -1;
  124. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
  125. return -1;
  126. return 0;
  127. }
  128. /*
  129. * Read the model-id bytes from the touchpad
  130. * see also SYN_MODEL_* macros
  131. */
  132. static int synaptics_model_id(struct psmouse *psmouse)
  133. {
  134. struct synaptics_data *priv = psmouse->private;
  135. unsigned char mi[3];
  136. if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
  137. return -1;
  138. priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
  139. return 0;
  140. }
  141. /*
  142. * Read the board id from the touchpad
  143. * The board id is encoded in the "QUERY MODES" response
  144. */
  145. static int synaptics_board_id(struct psmouse *psmouse)
  146. {
  147. struct synaptics_data *priv = psmouse->private;
  148. unsigned char bid[3];
  149. if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
  150. return -1;
  151. priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
  152. return 0;
  153. }
  154. /*
  155. * Read the firmware id from the touchpad
  156. */
  157. static int synaptics_firmware_id(struct psmouse *psmouse)
  158. {
  159. struct synaptics_data *priv = psmouse->private;
  160. unsigned char fwid[3];
  161. if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
  162. return -1;
  163. priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
  164. return 0;
  165. }
  166. /*
  167. * Read the capability-bits from the touchpad
  168. * see also the SYN_CAP_* macros
  169. */
  170. static int synaptics_capability(struct psmouse *psmouse)
  171. {
  172. struct synaptics_data *priv = psmouse->private;
  173. unsigned char cap[3];
  174. if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
  175. return -1;
  176. priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  177. priv->ext_cap = priv->ext_cap_0c = 0;
  178. /*
  179. * Older firmwares had submodel ID fixed to 0x47
  180. */
  181. if (SYN_ID_FULL(priv->identity) < 0x705 &&
  182. SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
  183. return -1;
  184. }
  185. /*
  186. * Unless capExtended is set the rest of the flags should be ignored
  187. */
  188. if (!SYN_CAP_EXTENDED(priv->capabilities))
  189. priv->capabilities = 0;
  190. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
  191. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
  192. psmouse_warn(psmouse,
  193. "device claims to have extended capabilities, but I'm not able to read them.\n");
  194. } else {
  195. priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  196. /*
  197. * if nExtBtn is greater than 8 it should be considered
  198. * invalid and treated as 0
  199. */
  200. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
  201. priv->ext_cap &= 0xff0fff;
  202. }
  203. }
  204. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
  205. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
  206. psmouse_warn(psmouse,
  207. "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
  208. } else {
  209. priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  210. }
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Identify Touchpad
  216. * See also the SYN_ID_* macros
  217. */
  218. static int synaptics_identify(struct psmouse *psmouse)
  219. {
  220. struct synaptics_data *priv = psmouse->private;
  221. unsigned char id[3];
  222. if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
  223. return -1;
  224. priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
  225. if (SYN_ID_IS_SYNAPTICS(priv->identity))
  226. return 0;
  227. return -1;
  228. }
  229. /*
  230. * Read touchpad resolution and maximum reported coordinates
  231. * Resolution is left zero if touchpad does not support the query
  232. */
  233. static int synaptics_resolution(struct psmouse *psmouse)
  234. {
  235. struct synaptics_data *priv = psmouse->private;
  236. unsigned char resp[3];
  237. if (SYN_ID_MAJOR(priv->identity) < 4)
  238. return 0;
  239. if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
  240. if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
  241. priv->x_res = resp[0]; /* x resolution in units/mm */
  242. priv->y_res = resp[2]; /* y resolution in units/mm */
  243. }
  244. }
  245. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
  246. SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
  247. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
  248. psmouse_warn(psmouse,
  249. "device claims to have max coordinates query, but I'm not able to read it.\n");
  250. } else {
  251. priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
  252. priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
  253. }
  254. }
  255. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 &&
  256. SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c)) {
  257. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
  258. psmouse_warn(psmouse,
  259. "device claims to have min coordinates query, but I'm not able to read it.\n");
  260. } else {
  261. priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
  262. priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
  263. }
  264. }
  265. return 0;
  266. }
  267. static int synaptics_query_hardware(struct psmouse *psmouse)
  268. {
  269. if (synaptics_identify(psmouse))
  270. return -1;
  271. if (synaptics_model_id(psmouse))
  272. return -1;
  273. if (synaptics_firmware_id(psmouse))
  274. return -1;
  275. if (synaptics_board_id(psmouse))
  276. return -1;
  277. if (synaptics_capability(psmouse))
  278. return -1;
  279. if (synaptics_resolution(psmouse))
  280. return -1;
  281. return 0;
  282. }
  283. static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
  284. {
  285. static unsigned char param = 0xc8;
  286. struct synaptics_data *priv = psmouse->private;
  287. if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
  288. SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
  289. return 0;
  290. if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
  291. return -1;
  292. if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
  293. return -1;
  294. /* Advanced gesture mode also sends multi finger data */
  295. priv->capabilities |= BIT(1);
  296. return 0;
  297. }
  298. static int synaptics_set_mode(struct psmouse *psmouse)
  299. {
  300. struct synaptics_data *priv = psmouse->private;
  301. priv->mode = 0;
  302. if (priv->absolute_mode)
  303. priv->mode |= SYN_BIT_ABSOLUTE_MODE;
  304. if (priv->disable_gesture)
  305. priv->mode |= SYN_BIT_DISABLE_GESTURE;
  306. if (psmouse->rate >= 80)
  307. priv->mode |= SYN_BIT_HIGH_RATE;
  308. if (SYN_CAP_EXTENDED(priv->capabilities))
  309. priv->mode |= SYN_BIT_W_MODE;
  310. if (synaptics_mode_cmd(psmouse, priv->mode))
  311. return -1;
  312. if (priv->absolute_mode &&
  313. synaptics_set_advanced_gesture_mode(psmouse)) {
  314. psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
  315. return -1;
  316. }
  317. return 0;
  318. }
  319. static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
  320. {
  321. struct synaptics_data *priv = psmouse->private;
  322. if (rate >= 80) {
  323. priv->mode |= SYN_BIT_HIGH_RATE;
  324. psmouse->rate = 80;
  325. } else {
  326. priv->mode &= ~SYN_BIT_HIGH_RATE;
  327. psmouse->rate = 40;
  328. }
  329. synaptics_mode_cmd(psmouse, priv->mode);
  330. }
  331. /*****************************************************************************
  332. * Synaptics pass-through PS/2 port support
  333. ****************************************************************************/
  334. static int synaptics_pt_write(struct serio *serio, unsigned char c)
  335. {
  336. struct psmouse *parent = serio_get_drvdata(serio->parent);
  337. char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
  338. if (psmouse_sliced_command(parent, c))
  339. return -1;
  340. if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
  341. return -1;
  342. return 0;
  343. }
  344. static int synaptics_pt_start(struct serio *serio)
  345. {
  346. struct psmouse *parent = serio_get_drvdata(serio->parent);
  347. struct synaptics_data *priv = parent->private;
  348. serio_pause_rx(parent->ps2dev.serio);
  349. priv->pt_port = serio;
  350. serio_continue_rx(parent->ps2dev.serio);
  351. return 0;
  352. }
  353. static void synaptics_pt_stop(struct serio *serio)
  354. {
  355. struct psmouse *parent = serio_get_drvdata(serio->parent);
  356. struct synaptics_data *priv = parent->private;
  357. serio_pause_rx(parent->ps2dev.serio);
  358. priv->pt_port = NULL;
  359. serio_continue_rx(parent->ps2dev.serio);
  360. }
  361. static int synaptics_is_pt_packet(unsigned char *buf)
  362. {
  363. return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
  364. }
  365. static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
  366. {
  367. struct psmouse *child = serio_get_drvdata(ptport);
  368. if (child && child->state == PSMOUSE_ACTIVATED) {
  369. serio_interrupt(ptport, packet[1], 0);
  370. serio_interrupt(ptport, packet[4], 0);
  371. serio_interrupt(ptport, packet[5], 0);
  372. if (child->pktsize == 4)
  373. serio_interrupt(ptport, packet[2], 0);
  374. } else
  375. serio_interrupt(ptport, packet[1], 0);
  376. }
  377. static void synaptics_pt_activate(struct psmouse *psmouse)
  378. {
  379. struct synaptics_data *priv = psmouse->private;
  380. struct psmouse *child = serio_get_drvdata(priv->pt_port);
  381. /* adjust the touchpad to child's choice of protocol */
  382. if (child) {
  383. if (child->pktsize == 4)
  384. priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
  385. else
  386. priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
  387. if (synaptics_mode_cmd(psmouse, priv->mode))
  388. psmouse_warn(psmouse,
  389. "failed to switch guest protocol\n");
  390. }
  391. }
  392. static void synaptics_pt_create(struct psmouse *psmouse)
  393. {
  394. struct serio *serio;
  395. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  396. if (!serio) {
  397. psmouse_err(psmouse,
  398. "not enough memory for pass-through port\n");
  399. return;
  400. }
  401. serio->id.type = SERIO_PS_PSTHRU;
  402. strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
  403. strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
  404. serio->write = synaptics_pt_write;
  405. serio->start = synaptics_pt_start;
  406. serio->stop = synaptics_pt_stop;
  407. serio->parent = psmouse->ps2dev.serio;
  408. psmouse->pt_activate = synaptics_pt_activate;
  409. psmouse_info(psmouse, "serio: %s port at %s\n",
  410. serio->name, psmouse->phys);
  411. serio_register_port(serio);
  412. }
  413. /*****************************************************************************
  414. * Functions to interpret the absolute mode packets
  415. ****************************************************************************/
  416. static void synaptics_mt_state_set(struct synaptics_mt_state *state, int count,
  417. int sgm, int agm)
  418. {
  419. state->count = count;
  420. state->sgm = sgm;
  421. state->agm = agm;
  422. }
  423. static void synaptics_parse_agm(const unsigned char buf[],
  424. struct synaptics_data *priv,
  425. struct synaptics_hw_state *hw)
  426. {
  427. struct synaptics_hw_state *agm = &priv->agm;
  428. int agm_packet_type;
  429. agm_packet_type = (buf[5] & 0x30) >> 4;
  430. switch (agm_packet_type) {
  431. case 1:
  432. /* Gesture packet: (x, y, z) half resolution */
  433. agm->w = hw->w;
  434. agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
  435. agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
  436. agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
  437. break;
  438. case 2:
  439. /* AGM-CONTACT packet: (count, sgm, agm) */
  440. synaptics_mt_state_set(&agm->mt_state, buf[1], buf[2], buf[4]);
  441. break;
  442. default:
  443. break;
  444. }
  445. /* Record that at least one AGM has been received since last SGM */
  446. priv->agm_pending = true;
  447. }
  448. static int synaptics_parse_hw_state(const unsigned char buf[],
  449. struct synaptics_data *priv,
  450. struct synaptics_hw_state *hw)
  451. {
  452. memset(hw, 0, sizeof(struct synaptics_hw_state));
  453. if (SYN_MODEL_NEWABS(priv->model_id)) {
  454. hw->w = (((buf[0] & 0x30) >> 2) |
  455. ((buf[0] & 0x04) >> 1) |
  456. ((buf[3] & 0x04) >> 2));
  457. hw->left = (buf[0] & 0x01) ? 1 : 0;
  458. hw->right = (buf[0] & 0x02) ? 1 : 0;
  459. if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  460. /*
  461. * Clickpad's button is transmitted as middle button,
  462. * however, since it is primary button, we will report
  463. * it as BTN_LEFT.
  464. */
  465. hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  466. } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  467. hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  468. if (hw->w == 2)
  469. hw->scroll = (signed char)(buf[1]);
  470. }
  471. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  472. hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  473. hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
  474. }
  475. if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
  476. SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
  477. hw->w == 2) {
  478. synaptics_parse_agm(buf, priv, hw);
  479. return 1;
  480. }
  481. hw->x = (((buf[3] & 0x10) << 8) |
  482. ((buf[1] & 0x0f) << 8) |
  483. buf[4]);
  484. hw->y = (((buf[3] & 0x20) << 7) |
  485. ((buf[1] & 0xf0) << 4) |
  486. buf[5]);
  487. hw->z = buf[2];
  488. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
  489. ((buf[0] ^ buf[3]) & 0x02)) {
  490. switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
  491. default:
  492. /*
  493. * if nExtBtn is greater than 8 it should be
  494. * considered invalid and treated as 0
  495. */
  496. break;
  497. case 8:
  498. hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
  499. hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
  500. case 6:
  501. hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
  502. hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
  503. case 4:
  504. hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
  505. hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
  506. case 2:
  507. hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
  508. hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
  509. }
  510. }
  511. } else {
  512. hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
  513. hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
  514. hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
  515. hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
  516. hw->left = (buf[0] & 0x01) ? 1 : 0;
  517. hw->right = (buf[0] & 0x02) ? 1 : 0;
  518. }
  519. /*
  520. * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
  521. * is used by some firmware to indicate a finger at the edge of
  522. * the touchpad whose precise position cannot be determined, so
  523. * convert these values to the maximum axis value.
  524. */
  525. if (hw->x > X_MAX_POSITIVE)
  526. hw->x -= 1 << ABS_POS_BITS;
  527. else if (hw->x == X_MAX_POSITIVE)
  528. hw->x = XMAX;
  529. if (hw->y > Y_MAX_POSITIVE)
  530. hw->y -= 1 << ABS_POS_BITS;
  531. else if (hw->y == Y_MAX_POSITIVE)
  532. hw->y = YMAX;
  533. return 0;
  534. }
  535. static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
  536. bool active, int x, int y)
  537. {
  538. input_mt_slot(dev, slot);
  539. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  540. if (active) {
  541. input_report_abs(dev, ABS_MT_POSITION_X, x);
  542. input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
  543. }
  544. }
  545. static void synaptics_report_semi_mt_data(struct input_dev *dev,
  546. const struct synaptics_hw_state *a,
  547. const struct synaptics_hw_state *b,
  548. int num_fingers)
  549. {
  550. if (num_fingers >= 2) {
  551. synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
  552. min(a->y, b->y));
  553. synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
  554. max(a->y, b->y));
  555. } else if (num_fingers == 1) {
  556. synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
  557. synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
  558. } else {
  559. synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
  560. synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
  561. }
  562. }
  563. static void synaptics_report_buttons(struct psmouse *psmouse,
  564. const struct synaptics_hw_state *hw)
  565. {
  566. struct input_dev *dev = psmouse->dev;
  567. struct synaptics_data *priv = psmouse->private;
  568. int i;
  569. input_report_key(dev, BTN_LEFT, hw->left);
  570. input_report_key(dev, BTN_RIGHT, hw->right);
  571. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  572. input_report_key(dev, BTN_MIDDLE, hw->middle);
  573. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  574. input_report_key(dev, BTN_FORWARD, hw->up);
  575. input_report_key(dev, BTN_BACK, hw->down);
  576. }
  577. for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
  578. input_report_key(dev, BTN_0 + i, hw->ext_buttons & (1 << i));
  579. }
  580. static void synaptics_report_slot(struct input_dev *dev, int slot,
  581. const struct synaptics_hw_state *hw)
  582. {
  583. input_mt_slot(dev, slot);
  584. input_mt_report_slot_state(dev, MT_TOOL_FINGER, (hw != NULL));
  585. if (!hw)
  586. return;
  587. input_report_abs(dev, ABS_MT_POSITION_X, hw->x);
  588. input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(hw->y));
  589. input_report_abs(dev, ABS_MT_PRESSURE, hw->z);
  590. }
  591. static void synaptics_report_mt_data(struct psmouse *psmouse,
  592. struct synaptics_mt_state *mt_state,
  593. const struct synaptics_hw_state *sgm)
  594. {
  595. struct input_dev *dev = psmouse->dev;
  596. struct synaptics_data *priv = psmouse->private;
  597. struct synaptics_hw_state *agm = &priv->agm;
  598. struct synaptics_mt_state *old = &priv->mt_state;
  599. switch (mt_state->count) {
  600. case 0:
  601. synaptics_report_slot(dev, 0, NULL);
  602. synaptics_report_slot(dev, 1, NULL);
  603. break;
  604. case 1:
  605. if (mt_state->sgm == -1) {
  606. synaptics_report_slot(dev, 0, NULL);
  607. synaptics_report_slot(dev, 1, NULL);
  608. } else if (mt_state->sgm == 0) {
  609. synaptics_report_slot(dev, 0, sgm);
  610. synaptics_report_slot(dev, 1, NULL);
  611. } else {
  612. synaptics_report_slot(dev, 0, NULL);
  613. synaptics_report_slot(dev, 1, sgm);
  614. }
  615. break;
  616. default:
  617. /*
  618. * If the finger slot contained in SGM is valid, and either
  619. * hasn't changed, or is new, or the old SGM has now moved to
  620. * AGM, then report SGM in MTB slot 0.
  621. * Otherwise, empty MTB slot 0.
  622. */
  623. if (mt_state->sgm != -1 &&
  624. (mt_state->sgm == old->sgm ||
  625. old->sgm == -1 || mt_state->agm == old->sgm))
  626. synaptics_report_slot(dev, 0, sgm);
  627. else
  628. synaptics_report_slot(dev, 0, NULL);
  629. /*
  630. * If the finger slot contained in AGM is valid, and either
  631. * hasn't changed, or is new, then report AGM in MTB slot 1.
  632. * Otherwise, empty MTB slot 1.
  633. *
  634. * However, in the case where the AGM is new, make sure that
  635. * that it is either the same as the old SGM, or there was no
  636. * SGM.
  637. *
  638. * Otherwise, if the SGM was just 1, and the new AGM is 2, then
  639. * the new AGM will keep the old SGM's tracking ID, which can
  640. * cause apparent drumroll. This happens if in the following
  641. * valid finger sequence:
  642. *
  643. * Action SGM AGM (MTB slot:Contact)
  644. * 1. Touch contact 0 (0:0)
  645. * 2. Touch contact 1 (0:0, 1:1)
  646. * 3. Lift contact 0 (1:1)
  647. * 4. Touch contacts 2,3 (0:2, 1:3)
  648. *
  649. * In step 4, contact 3, in AGM must not be given the same
  650. * tracking ID as contact 1 had in step 3. To avoid this,
  651. * the first agm with contact 3 is dropped and slot 1 is
  652. * invalidated (tracking ID = -1).
  653. */
  654. if (mt_state->agm != -1 &&
  655. (mt_state->agm == old->agm ||
  656. (old->agm == -1 &&
  657. (old->sgm == -1 || mt_state->agm == old->sgm))))
  658. synaptics_report_slot(dev, 1, agm);
  659. else
  660. synaptics_report_slot(dev, 1, NULL);
  661. break;
  662. }
  663. /* Don't use active slot count to generate BTN_TOOL events. */
  664. input_mt_report_pointer_emulation(dev, false);
  665. /* Send the number of fingers reported by touchpad itself. */
  666. input_mt_report_finger_count(dev, mt_state->count);
  667. synaptics_report_buttons(psmouse, sgm);
  668. input_sync(dev);
  669. }
  670. /* Handle case where mt_state->count = 0 */
  671. static void synaptics_image_sensor_0f(struct synaptics_data *priv,
  672. struct synaptics_mt_state *mt_state)
  673. {
  674. synaptics_mt_state_set(mt_state, 0, -1, -1);
  675. priv->mt_state_lost = false;
  676. }
  677. /* Handle case where mt_state->count = 1 */
  678. static void synaptics_image_sensor_1f(struct synaptics_data *priv,
  679. struct synaptics_mt_state *mt_state)
  680. {
  681. struct synaptics_hw_state *agm = &priv->agm;
  682. struct synaptics_mt_state *old = &priv->mt_state;
  683. /*
  684. * If the last AGM was (0,0,0), and there is only one finger left,
  685. * then we absolutely know that SGM contains slot 0, and all other
  686. * fingers have been removed.
  687. */
  688. if (priv->agm_pending && agm->z == 0) {
  689. synaptics_mt_state_set(mt_state, 1, 0, -1);
  690. priv->mt_state_lost = false;
  691. return;
  692. }
  693. switch (old->count) {
  694. case 0:
  695. synaptics_mt_state_set(mt_state, 1, 0, -1);
  696. break;
  697. case 1:
  698. /*
  699. * If mt_state_lost, then the previous transition was 3->1,
  700. * and SGM now contains either slot 0 or 1, but we don't know
  701. * which. So, we just assume that the SGM now contains slot 1.
  702. *
  703. * If pending AGM and either:
  704. * (a) the previous SGM slot contains slot 0, or
  705. * (b) there was no SGM slot
  706. * then, the SGM now contains slot 1
  707. *
  708. * Case (a) happens with very rapid "drum roll" gestures, where
  709. * slot 0 finger is lifted and a new slot 1 finger touches
  710. * within one reporting interval.
  711. *
  712. * Case (b) happens if initially two or more fingers tap
  713. * briefly, and all but one lift before the end of the first
  714. * reporting interval.
  715. *
  716. * (In both these cases, slot 0 will becomes empty, so SGM
  717. * contains slot 1 with the new finger)
  718. *
  719. * Else, if there was no previous SGM, it now contains slot 0.
  720. *
  721. * Otherwise, SGM still contains the same slot.
  722. */
  723. if (priv->mt_state_lost ||
  724. (priv->agm_pending && old->sgm <= 0))
  725. synaptics_mt_state_set(mt_state, 1, 1, -1);
  726. else if (old->sgm == -1)
  727. synaptics_mt_state_set(mt_state, 1, 0, -1);
  728. break;
  729. case 2:
  730. /*
  731. * If mt_state_lost, we don't know which finger SGM contains.
  732. *
  733. * So, report 1 finger, but with both slots empty.
  734. * We will use slot 1 on subsequent 1->1
  735. */
  736. if (priv->mt_state_lost) {
  737. synaptics_mt_state_set(mt_state, 1, -1, -1);
  738. break;
  739. }
  740. /*
  741. * Since the last AGM was NOT (0,0,0), it was the finger in
  742. * slot 0 that has been removed.
  743. * So, SGM now contains previous AGM's slot, and AGM is now
  744. * empty.
  745. */
  746. synaptics_mt_state_set(mt_state, 1, old->agm, -1);
  747. break;
  748. case 3:
  749. /*
  750. * Since last AGM was not (0,0,0), we don't know which finger
  751. * is left.
  752. *
  753. * So, report 1 finger, but with both slots empty.
  754. * We will use slot 1 on subsequent 1->1
  755. */
  756. synaptics_mt_state_set(mt_state, 1, -1, -1);
  757. priv->mt_state_lost = true;
  758. break;
  759. case 4:
  760. case 5:
  761. /* mt_state was updated by AGM-CONTACT packet */
  762. break;
  763. }
  764. }
  765. /* Handle case where mt_state->count = 2 */
  766. static void synaptics_image_sensor_2f(struct synaptics_data *priv,
  767. struct synaptics_mt_state *mt_state)
  768. {
  769. struct synaptics_mt_state *old = &priv->mt_state;
  770. switch (old->count) {
  771. case 0:
  772. synaptics_mt_state_set(mt_state, 2, 0, 1);
  773. break;
  774. case 1:
  775. /*
  776. * If previous SGM contained slot 1 or higher, SGM now contains
  777. * slot 0 (the newly touching finger) and AGM contains SGM's
  778. * previous slot.
  779. *
  780. * Otherwise, SGM still contains slot 0 and AGM now contains
  781. * slot 1.
  782. */
  783. if (old->sgm >= 1)
  784. synaptics_mt_state_set(mt_state, 2, 0, old->sgm);
  785. else
  786. synaptics_mt_state_set(mt_state, 2, 0, 1);
  787. break;
  788. case 2:
  789. /*
  790. * If mt_state_lost, SGM now contains either finger 1 or 2, but
  791. * we don't know which.
  792. * So, we just assume that the SGM contains slot 0 and AGM 1.
  793. */
  794. if (priv->mt_state_lost)
  795. synaptics_mt_state_set(mt_state, 2, 0, 1);
  796. /*
  797. * Otherwise, use the same mt_state, since it either hasn't
  798. * changed, or was updated by a recently received AGM-CONTACT
  799. * packet.
  800. */
  801. break;
  802. case 3:
  803. /*
  804. * 3->2 transitions have two unsolvable problems:
  805. * 1) no indication is given which finger was removed
  806. * 2) no way to tell if agm packet was for finger 3
  807. * before 3->2, or finger 2 after 3->2.
  808. *
  809. * So, report 2 fingers, but empty all slots.
  810. * We will guess slots [0,1] on subsequent 2->2.
  811. */
  812. synaptics_mt_state_set(mt_state, 2, -1, -1);
  813. priv->mt_state_lost = true;
  814. break;
  815. case 4:
  816. case 5:
  817. /* mt_state was updated by AGM-CONTACT packet */
  818. break;
  819. }
  820. }
  821. /* Handle case where mt_state->count = 3 */
  822. static void synaptics_image_sensor_3f(struct synaptics_data *priv,
  823. struct synaptics_mt_state *mt_state)
  824. {
  825. struct synaptics_mt_state *old = &priv->mt_state;
  826. switch (old->count) {
  827. case 0:
  828. synaptics_mt_state_set(mt_state, 3, 0, 2);
  829. break;
  830. case 1:
  831. /*
  832. * If previous SGM contained slot 2 or higher, SGM now contains
  833. * slot 0 (one of the newly touching fingers) and AGM contains
  834. * SGM's previous slot.
  835. *
  836. * Otherwise, SGM now contains slot 0 and AGM contains slot 2.
  837. */
  838. if (old->sgm >= 2)
  839. synaptics_mt_state_set(mt_state, 3, 0, old->sgm);
  840. else
  841. synaptics_mt_state_set(mt_state, 3, 0, 2);
  842. break;
  843. case 2:
  844. /*
  845. * If the AGM previously contained slot 3 or higher, then the
  846. * newly touching finger is in the lowest available slot.
  847. *
  848. * If SGM was previously 1 or higher, then the new SGM is
  849. * now slot 0 (with a new finger), otherwise, the new finger
  850. * is now in a hidden slot between 0 and AGM's slot.
  851. *
  852. * In all such cases, the SGM now contains slot 0, and the AGM
  853. * continues to contain the same slot as before.
  854. */
  855. if (old->agm >= 3) {
  856. synaptics_mt_state_set(mt_state, 3, 0, old->agm);
  857. break;
  858. }
  859. /*
  860. * After some 3->1 and all 3->2 transitions, we lose track
  861. * of which slot is reported by SGM and AGM.
  862. *
  863. * For 2->3 in this state, report 3 fingers, but empty all
  864. * slots, and we will guess (0,2) on a subsequent 0->3.
  865. *
  866. * To userspace, the resulting transition will look like:
  867. * 2:[0,1] -> 3:[-1,-1] -> 3:[0,2]
  868. */
  869. if (priv->mt_state_lost) {
  870. synaptics_mt_state_set(mt_state, 3, -1, -1);
  871. break;
  872. }
  873. /*
  874. * If the (SGM,AGM) really previously contained slots (0, 1),
  875. * then we cannot know what slot was just reported by the AGM,
  876. * because the 2->3 transition can occur either before or after
  877. * the AGM packet. Thus, this most recent AGM could contain
  878. * either the same old slot 1 or the new slot 2.
  879. * Subsequent AGMs will be reporting slot 2.
  880. *
  881. * To userspace, the resulting transition will look like:
  882. * 2:[0,1] -> 3:[0,-1] -> 3:[0,2]
  883. */
  884. synaptics_mt_state_set(mt_state, 3, 0, -1);
  885. break;
  886. case 3:
  887. /*
  888. * If, for whatever reason, the previous agm was invalid,
  889. * Assume SGM now contains slot 0, AGM now contains slot 2.
  890. */
  891. if (old->agm <= 2)
  892. synaptics_mt_state_set(mt_state, 3, 0, 2);
  893. /*
  894. * mt_state either hasn't changed, or was updated by a recently
  895. * received AGM-CONTACT packet.
  896. */
  897. break;
  898. case 4:
  899. case 5:
  900. /* mt_state was updated by AGM-CONTACT packet */
  901. break;
  902. }
  903. }
  904. /* Handle case where mt_state->count = 4, or = 5 */
  905. static void synaptics_image_sensor_45f(struct synaptics_data *priv,
  906. struct synaptics_mt_state *mt_state)
  907. {
  908. /* mt_state was updated correctly by AGM-CONTACT packet */
  909. priv->mt_state_lost = false;
  910. }
  911. static void synaptics_image_sensor_process(struct psmouse *psmouse,
  912. struct synaptics_hw_state *sgm)
  913. {
  914. struct synaptics_data *priv = psmouse->private;
  915. struct synaptics_hw_state *agm = &priv->agm;
  916. struct synaptics_mt_state mt_state;
  917. /* Initialize using current mt_state (as updated by last agm) */
  918. mt_state = agm->mt_state;
  919. /*
  920. * Update mt_state using the new finger count and current mt_state.
  921. */
  922. if (sgm->z == 0)
  923. synaptics_image_sensor_0f(priv, &mt_state);
  924. else if (sgm->w >= 4)
  925. synaptics_image_sensor_1f(priv, &mt_state);
  926. else if (sgm->w == 0)
  927. synaptics_image_sensor_2f(priv, &mt_state);
  928. else if (sgm->w == 1 && mt_state.count <= 3)
  929. synaptics_image_sensor_3f(priv, &mt_state);
  930. else
  931. synaptics_image_sensor_45f(priv, &mt_state);
  932. /* Send resulting input events to user space */
  933. synaptics_report_mt_data(psmouse, &mt_state, sgm);
  934. /* Store updated mt_state */
  935. priv->mt_state = agm->mt_state = mt_state;
  936. priv->agm_pending = false;
  937. }
  938. /*
  939. * called for each full received packet from the touchpad
  940. */
  941. static void synaptics_process_packet(struct psmouse *psmouse)
  942. {
  943. struct input_dev *dev = psmouse->dev;
  944. struct synaptics_data *priv = psmouse->private;
  945. struct synaptics_hw_state hw;
  946. int num_fingers;
  947. int finger_width;
  948. if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
  949. return;
  950. if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
  951. synaptics_image_sensor_process(psmouse, &hw);
  952. return;
  953. }
  954. if (hw.scroll) {
  955. priv->scroll += hw.scroll;
  956. while (priv->scroll >= 4) {
  957. input_report_key(dev, BTN_BACK, !hw.down);
  958. input_sync(dev);
  959. input_report_key(dev, BTN_BACK, hw.down);
  960. input_sync(dev);
  961. priv->scroll -= 4;
  962. }
  963. while (priv->scroll <= -4) {
  964. input_report_key(dev, BTN_FORWARD, !hw.up);
  965. input_sync(dev);
  966. input_report_key(dev, BTN_FORWARD, hw.up);
  967. input_sync(dev);
  968. priv->scroll += 4;
  969. }
  970. return;
  971. }
  972. if (hw.z > 0 && hw.x > 1) {
  973. num_fingers = 1;
  974. finger_width = 5;
  975. if (SYN_CAP_EXTENDED(priv->capabilities)) {
  976. switch (hw.w) {
  977. case 0 ... 1:
  978. if (SYN_CAP_MULTIFINGER(priv->capabilities))
  979. num_fingers = hw.w + 2;
  980. break;
  981. case 2:
  982. if (SYN_MODEL_PEN(priv->model_id))
  983. ; /* Nothing, treat a pen as a single finger */
  984. break;
  985. case 4 ... 15:
  986. if (SYN_CAP_PALMDETECT(priv->capabilities))
  987. finger_width = hw.w;
  988. break;
  989. }
  990. }
  991. } else {
  992. num_fingers = 0;
  993. finger_width = 0;
  994. }
  995. if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
  996. synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
  997. num_fingers);
  998. /* Post events
  999. * BTN_TOUCH has to be first as mousedev relies on it when doing
  1000. * absolute -> relative conversion
  1001. */
  1002. if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
  1003. if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
  1004. if (num_fingers > 0) {
  1005. input_report_abs(dev, ABS_X, hw.x);
  1006. input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
  1007. }
  1008. input_report_abs(dev, ABS_PRESSURE, hw.z);
  1009. if (SYN_CAP_PALMDETECT(priv->capabilities))
  1010. input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
  1011. input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
  1012. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  1013. input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
  1014. input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
  1015. }
  1016. synaptics_report_buttons(psmouse, &hw);
  1017. input_sync(dev);
  1018. }
  1019. static int synaptics_validate_byte(struct psmouse *psmouse,
  1020. int idx, unsigned char pkt_type)
  1021. {
  1022. static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
  1023. static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
  1024. static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
  1025. static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
  1026. static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
  1027. const char *packet = psmouse->packet;
  1028. if (idx < 0 || idx > 4)
  1029. return 0;
  1030. switch (pkt_type) {
  1031. case SYN_NEWABS:
  1032. case SYN_NEWABS_RELAXED:
  1033. return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
  1034. case SYN_NEWABS_STRICT:
  1035. return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
  1036. case SYN_OLDABS:
  1037. return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
  1038. default:
  1039. psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
  1040. return 0;
  1041. }
  1042. }
  1043. static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
  1044. {
  1045. int i;
  1046. for (i = 0; i < 5; i++)
  1047. if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
  1048. psmouse_info(psmouse, "using relaxed packet validation\n");
  1049. return SYN_NEWABS_RELAXED;
  1050. }
  1051. return SYN_NEWABS_STRICT;
  1052. }
  1053. static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
  1054. {
  1055. struct synaptics_data *priv = psmouse->private;
  1056. if (psmouse->pktcnt >= 6) { /* Full packet received */
  1057. if (unlikely(priv->pkt_type == SYN_NEWABS))
  1058. priv->pkt_type = synaptics_detect_pkt_type(psmouse);
  1059. if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
  1060. synaptics_is_pt_packet(psmouse->packet)) {
  1061. if (priv->pt_port)
  1062. synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
  1063. } else
  1064. synaptics_process_packet(psmouse);
  1065. return PSMOUSE_FULL_PACKET;
  1066. }
  1067. return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
  1068. PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
  1069. }
  1070. /*****************************************************************************
  1071. * Driver initialization/cleanup functions
  1072. ****************************************************************************/
  1073. static void set_abs_position_params(struct input_dev *dev,
  1074. struct synaptics_data *priv, int x_code,
  1075. int y_code)
  1076. {
  1077. int x_min = priv->x_min ?: XMIN_NOMINAL;
  1078. int x_max = priv->x_max ?: XMAX_NOMINAL;
  1079. int y_min = priv->y_min ?: YMIN_NOMINAL;
  1080. int y_max = priv->y_max ?: YMAX_NOMINAL;
  1081. int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
  1082. SYN_REDUCED_FILTER_FUZZ : 0;
  1083. input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
  1084. input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
  1085. input_abs_set_res(dev, x_code, priv->x_res);
  1086. input_abs_set_res(dev, y_code, priv->y_res);
  1087. }
  1088. static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
  1089. {
  1090. int i;
  1091. /* Things that apply to both modes */
  1092. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  1093. __set_bit(EV_KEY, dev->evbit);
  1094. __set_bit(BTN_LEFT, dev->keybit);
  1095. __set_bit(BTN_RIGHT, dev->keybit);
  1096. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  1097. __set_bit(BTN_MIDDLE, dev->keybit);
  1098. if (!priv->absolute_mode) {
  1099. /* Relative mode */
  1100. __set_bit(EV_REL, dev->evbit);
  1101. __set_bit(REL_X, dev->relbit);
  1102. __set_bit(REL_Y, dev->relbit);
  1103. return;
  1104. }
  1105. /* Absolute mode */
  1106. __set_bit(EV_ABS, dev->evbit);
  1107. set_abs_position_params(dev, priv, ABS_X, ABS_Y);
  1108. input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
  1109. if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
  1110. set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
  1111. ABS_MT_POSITION_Y);
  1112. /* Image sensors can report per-contact pressure */
  1113. input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1114. input_mt_init_slots(dev, 2, INPUT_MT_POINTER);
  1115. /* Image sensors can signal 4 and 5 finger clicks */
  1116. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  1117. __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
  1118. } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
  1119. /* Non-image sensors with AGM use semi-mt */
  1120. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  1121. input_mt_init_slots(dev, 2, 0);
  1122. set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
  1123. ABS_MT_POSITION_Y);
  1124. }
  1125. if (SYN_CAP_PALMDETECT(priv->capabilities))
  1126. input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
  1127. __set_bit(BTN_TOUCH, dev->keybit);
  1128. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  1129. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  1130. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  1131. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  1132. }
  1133. if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
  1134. SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  1135. __set_bit(BTN_FORWARD, dev->keybit);
  1136. __set_bit(BTN_BACK, dev->keybit);
  1137. }
  1138. for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
  1139. __set_bit(BTN_0 + i, dev->keybit);
  1140. __clear_bit(EV_REL, dev->evbit);
  1141. __clear_bit(REL_X, dev->relbit);
  1142. __clear_bit(REL_Y, dev->relbit);
  1143. if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  1144. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  1145. /* Clickpads report only left button */
  1146. __clear_bit(BTN_RIGHT, dev->keybit);
  1147. __clear_bit(BTN_MIDDLE, dev->keybit);
  1148. }
  1149. }
  1150. static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
  1151. void *data, char *buf)
  1152. {
  1153. struct synaptics_data *priv = psmouse->private;
  1154. return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
  1155. }
  1156. static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
  1157. void *data, const char *buf,
  1158. size_t len)
  1159. {
  1160. struct synaptics_data *priv = psmouse->private;
  1161. unsigned int value;
  1162. int err;
  1163. err = kstrtouint(buf, 10, &value);
  1164. if (err)
  1165. return err;
  1166. if (value > 1)
  1167. return -EINVAL;
  1168. if (value == priv->disable_gesture)
  1169. return len;
  1170. priv->disable_gesture = value;
  1171. if (value)
  1172. priv->mode |= SYN_BIT_DISABLE_GESTURE;
  1173. else
  1174. priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
  1175. if (synaptics_mode_cmd(psmouse, priv->mode))
  1176. return -EIO;
  1177. return len;
  1178. }
  1179. PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
  1180. synaptics_show_disable_gesture,
  1181. synaptics_set_disable_gesture);
  1182. static void synaptics_disconnect(struct psmouse *psmouse)
  1183. {
  1184. struct synaptics_data *priv = psmouse->private;
  1185. if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
  1186. device_remove_file(&psmouse->ps2dev.serio->dev,
  1187. &psmouse_attr_disable_gesture.dattr);
  1188. synaptics_reset(psmouse);
  1189. kfree(priv);
  1190. psmouse->private = NULL;
  1191. }
  1192. static int synaptics_reconnect(struct psmouse *psmouse)
  1193. {
  1194. struct synaptics_data *priv = psmouse->private;
  1195. struct synaptics_data old_priv = *priv;
  1196. unsigned char param[2];
  1197. int retry = 0;
  1198. int error;
  1199. do {
  1200. psmouse_reset(psmouse);
  1201. if (retry) {
  1202. /*
  1203. * On some boxes, right after resuming, the touchpad
  1204. * needs some time to finish initializing (I assume
  1205. * it needs time to calibrate) and start responding
  1206. * to Synaptics-specific queries, so let's wait a
  1207. * bit.
  1208. */
  1209. ssleep(1);
  1210. }
  1211. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
  1212. error = synaptics_detect(psmouse, 0);
  1213. } while (error && ++retry < 3);
  1214. if (error)
  1215. return -1;
  1216. if (retry > 1)
  1217. psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
  1218. if (synaptics_query_hardware(psmouse)) {
  1219. psmouse_err(psmouse, "Unable to query device.\n");
  1220. return -1;
  1221. }
  1222. if (synaptics_set_mode(psmouse)) {
  1223. psmouse_err(psmouse, "Unable to initialize device.\n");
  1224. return -1;
  1225. }
  1226. if (old_priv.identity != priv->identity ||
  1227. old_priv.model_id != priv->model_id ||
  1228. old_priv.capabilities != priv->capabilities ||
  1229. old_priv.ext_cap != priv->ext_cap) {
  1230. psmouse_err(psmouse,
  1231. "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
  1232. old_priv.identity, priv->identity,
  1233. old_priv.model_id, priv->model_id,
  1234. old_priv.capabilities, priv->capabilities,
  1235. old_priv.ext_cap, priv->ext_cap);
  1236. return -1;
  1237. }
  1238. return 0;
  1239. }
  1240. static bool impaired_toshiba_kbc;
  1241. static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
  1242. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1243. {
  1244. /* Toshiba Satellite */
  1245. .matches = {
  1246. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1247. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  1248. },
  1249. },
  1250. {
  1251. /* Toshiba Dynabook */
  1252. .matches = {
  1253. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1254. DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
  1255. },
  1256. },
  1257. {
  1258. /* Toshiba Portege M300 */
  1259. .matches = {
  1260. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1261. DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
  1262. },
  1263. },
  1264. {
  1265. /* Toshiba Portege M300 */
  1266. .matches = {
  1267. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1268. DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
  1269. DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
  1270. },
  1271. },
  1272. #endif
  1273. { }
  1274. };
  1275. static bool broken_olpc_ec;
  1276. static const struct dmi_system_id olpc_dmi_table[] __initconst = {
  1277. #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
  1278. {
  1279. /* OLPC XO-1 or XO-1.5 */
  1280. .matches = {
  1281. DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
  1282. DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
  1283. },
  1284. },
  1285. #endif
  1286. { }
  1287. };
  1288. void __init synaptics_module_init(void)
  1289. {
  1290. impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
  1291. broken_olpc_ec = dmi_check_system(olpc_dmi_table);
  1292. }
  1293. static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
  1294. {
  1295. struct synaptics_data *priv;
  1296. int err = -1;
  1297. /*
  1298. * The OLPC XO has issues with Synaptics' absolute mode; the constant
  1299. * packet spew overloads the EC such that key presses on the keyboard
  1300. * are missed. Given that, don't even attempt to use Absolute mode.
  1301. * Relative mode seems to work just fine.
  1302. */
  1303. if (absolute_mode && broken_olpc_ec) {
  1304. psmouse_info(psmouse,
  1305. "OLPC XO detected, not enabling Synaptics protocol.\n");
  1306. return -ENODEV;
  1307. }
  1308. psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
  1309. if (!priv)
  1310. return -ENOMEM;
  1311. psmouse_reset(psmouse);
  1312. if (synaptics_query_hardware(psmouse)) {
  1313. psmouse_err(psmouse, "Unable to query device.\n");
  1314. goto init_fail;
  1315. }
  1316. priv->absolute_mode = absolute_mode;
  1317. if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
  1318. priv->disable_gesture = true;
  1319. if (synaptics_set_mode(psmouse)) {
  1320. psmouse_err(psmouse, "Unable to initialize device.\n");
  1321. goto init_fail;
  1322. }
  1323. priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
  1324. psmouse_info(psmouse,
  1325. "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx, board id: %lu, fw id: %lu\n",
  1326. SYN_ID_MODEL(priv->identity),
  1327. SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
  1328. priv->model_id,
  1329. priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
  1330. priv->board_id, priv->firmware_id);
  1331. set_input_params(psmouse->dev, priv);
  1332. /*
  1333. * Encode touchpad model so that it can be used to set
  1334. * input device->id.version and be visible to userspace.
  1335. * Because version is __u16 we have to drop something.
  1336. * Hardware info bits seem to be good candidates as they
  1337. * are documented to be for Synaptics corp. internal use.
  1338. */
  1339. psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
  1340. (priv->model_id & 0x000000ff);
  1341. if (absolute_mode) {
  1342. psmouse->protocol_handler = synaptics_process_byte;
  1343. psmouse->pktsize = 6;
  1344. } else {
  1345. /* Relative mode follows standard PS/2 mouse protocol */
  1346. psmouse->protocol_handler = psmouse_process_byte;
  1347. psmouse->pktsize = 3;
  1348. }
  1349. psmouse->set_rate = synaptics_set_rate;
  1350. psmouse->disconnect = synaptics_disconnect;
  1351. psmouse->reconnect = synaptics_reconnect;
  1352. psmouse->cleanup = synaptics_reset;
  1353. /* Synaptics can usually stay in sync without extra help */
  1354. psmouse->resync_time = 0;
  1355. if (SYN_CAP_PASS_THROUGH(priv->capabilities))
  1356. synaptics_pt_create(psmouse);
  1357. /*
  1358. * Toshiba's KBC seems to have trouble handling data from
  1359. * Synaptics at full rate. Switch to a lower rate (roughly
  1360. * the same rate as a standard PS/2 mouse).
  1361. */
  1362. if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
  1363. psmouse_info(psmouse,
  1364. "Toshiba %s detected, limiting rate to 40pps.\n",
  1365. dmi_get_system_info(DMI_PRODUCT_NAME));
  1366. psmouse->rate = 40;
  1367. }
  1368. if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
  1369. err = device_create_file(&psmouse->ps2dev.serio->dev,
  1370. &psmouse_attr_disable_gesture.dattr);
  1371. if (err) {
  1372. psmouse_err(psmouse,
  1373. "Failed to create disable_gesture attribute (%d)",
  1374. err);
  1375. goto init_fail;
  1376. }
  1377. }
  1378. return 0;
  1379. init_fail:
  1380. kfree(priv);
  1381. return err;
  1382. }
  1383. int synaptics_init(struct psmouse *psmouse)
  1384. {
  1385. return __synaptics_init(psmouse, true);
  1386. }
  1387. int synaptics_init_relative(struct psmouse *psmouse)
  1388. {
  1389. return __synaptics_init(psmouse, false);
  1390. }
  1391. bool synaptics_supported(void)
  1392. {
  1393. return true;
  1394. }
  1395. #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
  1396. void __init synaptics_module_init(void)
  1397. {
  1398. }
  1399. int synaptics_init(struct psmouse *psmouse)
  1400. {
  1401. return -ENOSYS;
  1402. }
  1403. bool synaptics_supported(void)
  1404. {
  1405. return false;
  1406. }
  1407. #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */