synaptics.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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. /* maximum ABS_MT_POSITION displacement (in mm) */
  66. #define DMAX 10
  67. /*****************************************************************************
  68. * Stuff we need even when we do not want native Synaptics support
  69. ****************************************************************************/
  70. /*
  71. * Set the synaptics touchpad mode byte by special commands
  72. */
  73. static int synaptics_mode_cmd(struct psmouse *psmouse, unsigned char mode)
  74. {
  75. unsigned char param[1];
  76. if (psmouse_sliced_command(psmouse, mode))
  77. return -1;
  78. param[0] = SYN_PS_SET_MODE2;
  79. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE))
  80. return -1;
  81. return 0;
  82. }
  83. int synaptics_detect(struct psmouse *psmouse, bool set_properties)
  84. {
  85. struct ps2dev *ps2dev = &psmouse->ps2dev;
  86. unsigned char param[4];
  87. param[0] = 0;
  88. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  89. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  90. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  91. ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
  92. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
  93. if (param[1] != 0x47)
  94. return -ENODEV;
  95. if (set_properties) {
  96. psmouse->vendor = "Synaptics";
  97. psmouse->name = "TouchPad";
  98. }
  99. return 0;
  100. }
  101. void synaptics_reset(struct psmouse *psmouse)
  102. {
  103. /* reset touchpad back to relative mode, gestures enabled */
  104. synaptics_mode_cmd(psmouse, 0);
  105. }
  106. #ifdef CONFIG_MOUSE_PS2_SYNAPTICS
  107. static bool cr48_profile_sensor;
  108. #define ANY_BOARD_ID 0
  109. struct min_max_quirk {
  110. const char * const *pnp_ids;
  111. struct {
  112. unsigned long int min, max;
  113. } board_id;
  114. int x_min, x_max, y_min, y_max;
  115. };
  116. static const struct min_max_quirk min_max_pnpid_table[] = {
  117. {
  118. (const char * const []){"LEN0033", NULL},
  119. {ANY_BOARD_ID, ANY_BOARD_ID},
  120. 1024, 5052, 2258, 4832
  121. },
  122. {
  123. (const char * const []){"LEN0042", NULL},
  124. {ANY_BOARD_ID, ANY_BOARD_ID},
  125. 1232, 5710, 1156, 4696
  126. },
  127. {
  128. (const char * const []){"LEN0034", "LEN0036", "LEN0037",
  129. "LEN0039", "LEN2002", "LEN2004",
  130. NULL},
  131. {ANY_BOARD_ID, 2961},
  132. 1024, 5112, 2024, 4832
  133. },
  134. {
  135. (const char * const []){"LEN2000", NULL},
  136. {ANY_BOARD_ID, ANY_BOARD_ID},
  137. 1024, 5113, 2021, 4832
  138. },
  139. {
  140. (const char * const []){"LEN2001", NULL},
  141. {ANY_BOARD_ID, ANY_BOARD_ID},
  142. 1024, 5022, 2508, 4832
  143. },
  144. {
  145. (const char * const []){"LEN2006", NULL},
  146. {2691, 2691},
  147. 1024, 5045, 2457, 4832
  148. },
  149. {
  150. (const char * const []){"LEN2006", NULL},
  151. {ANY_BOARD_ID, ANY_BOARD_ID},
  152. 1264, 5675, 1171, 4688
  153. },
  154. { }
  155. };
  156. /* This list has been kindly provided by Synaptics. */
  157. static const char * const topbuttonpad_pnp_ids[] = {
  158. "LEN0017",
  159. "LEN0018",
  160. "LEN0019",
  161. "LEN0023",
  162. "LEN002A",
  163. "LEN002B",
  164. "LEN002C",
  165. "LEN002D",
  166. "LEN002E",
  167. "LEN0033", /* Helix */
  168. "LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
  169. "LEN0035", /* X240 */
  170. "LEN0036", /* T440 */
  171. "LEN0037", /* X1 Carbon 2nd */
  172. "LEN0038",
  173. "LEN0039", /* T440s */
  174. "LEN0041",
  175. "LEN0042", /* Yoga */
  176. "LEN0045",
  177. "LEN0047",
  178. "LEN0049",
  179. "LEN2000", /* S540 */
  180. "LEN2001", /* Edge E431 */
  181. "LEN2002", /* Edge E531 */
  182. "LEN2003",
  183. "LEN2004", /* L440 */
  184. "LEN2005",
  185. "LEN2006", /* Edge E440/E540 */
  186. "LEN2007",
  187. "LEN2008",
  188. "LEN2009",
  189. "LEN200A",
  190. "LEN200B",
  191. NULL
  192. };
  193. /* This list has been kindly provided by Synaptics. */
  194. static const char * const forcepad_pnp_ids[] = {
  195. "SYN300D",
  196. "SYN3014",
  197. NULL
  198. };
  199. /*****************************************************************************
  200. * Synaptics communications functions
  201. ****************************************************************************/
  202. /*
  203. * Synaptics touchpads report the y coordinate from bottom to top, which is
  204. * opposite from what userspace expects.
  205. * This function is used to invert y before reporting.
  206. */
  207. static int synaptics_invert_y(int y)
  208. {
  209. return YMAX_NOMINAL + YMIN_NOMINAL - y;
  210. }
  211. /*
  212. * Send a command to the synpatics touchpad by special commands
  213. */
  214. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
  215. {
  216. if (psmouse_sliced_command(psmouse, c))
  217. return -1;
  218. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
  219. return -1;
  220. return 0;
  221. }
  222. /*
  223. * Read the model-id bytes from the touchpad
  224. * see also SYN_MODEL_* macros
  225. */
  226. static int synaptics_model_id(struct psmouse *psmouse)
  227. {
  228. struct synaptics_data *priv = psmouse->private;
  229. unsigned char mi[3];
  230. if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
  231. return -1;
  232. priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
  233. return 0;
  234. }
  235. static int synaptics_more_extended_queries(struct psmouse *psmouse)
  236. {
  237. struct synaptics_data *priv = psmouse->private;
  238. unsigned char buf[3];
  239. if (synaptics_send_cmd(psmouse, SYN_QUE_MEXT_CAPAB_10, buf))
  240. return -1;
  241. priv->ext_cap_10 = (buf[0]<<16) | (buf[1]<<8) | buf[2];
  242. return 0;
  243. }
  244. /*
  245. * Read the board id and the "More Extended Queries" from the touchpad
  246. * The board id is encoded in the "QUERY MODES" response
  247. */
  248. static int synaptics_query_modes(struct psmouse *psmouse)
  249. {
  250. struct synaptics_data *priv = psmouse->private;
  251. unsigned char bid[3];
  252. /* firmwares prior 7.5 have no board_id encoded */
  253. if (SYN_ID_FULL(priv->identity) < 0x705)
  254. return 0;
  255. if (synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid))
  256. return -1;
  257. priv->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
  258. if (SYN_MEXT_CAP_BIT(bid[0]))
  259. return synaptics_more_extended_queries(psmouse);
  260. return 0;
  261. }
  262. /*
  263. * Read the firmware id from the touchpad
  264. */
  265. static int synaptics_firmware_id(struct psmouse *psmouse)
  266. {
  267. struct synaptics_data *priv = psmouse->private;
  268. unsigned char fwid[3];
  269. if (synaptics_send_cmd(psmouse, SYN_QUE_FIRMWARE_ID, fwid))
  270. return -1;
  271. priv->firmware_id = (fwid[0] << 16) | (fwid[1] << 8) | fwid[2];
  272. return 0;
  273. }
  274. /*
  275. * Read the capability-bits from the touchpad
  276. * see also the SYN_CAP_* macros
  277. */
  278. static int synaptics_capability(struct psmouse *psmouse)
  279. {
  280. struct synaptics_data *priv = psmouse->private;
  281. unsigned char cap[3];
  282. if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
  283. return -1;
  284. priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  285. priv->ext_cap = priv->ext_cap_0c = 0;
  286. /*
  287. * Older firmwares had submodel ID fixed to 0x47
  288. */
  289. if (SYN_ID_FULL(priv->identity) < 0x705 &&
  290. SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
  291. return -1;
  292. }
  293. /*
  294. * Unless capExtended is set the rest of the flags should be ignored
  295. */
  296. if (!SYN_CAP_EXTENDED(priv->capabilities))
  297. priv->capabilities = 0;
  298. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
  299. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
  300. psmouse_warn(psmouse,
  301. "device claims to have extended capabilities, but I'm not able to read them.\n");
  302. } else {
  303. priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  304. /*
  305. * if nExtBtn is greater than 8 it should be considered
  306. * invalid and treated as 0
  307. */
  308. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
  309. priv->ext_cap &= 0xff0fff;
  310. }
  311. }
  312. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
  313. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
  314. psmouse_warn(psmouse,
  315. "device claims to have extended capability 0x0c, but I'm not able to read it.\n");
  316. } else {
  317. priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  318. }
  319. }
  320. return 0;
  321. }
  322. /*
  323. * Identify Touchpad
  324. * See also the SYN_ID_* macros
  325. */
  326. static int synaptics_identify(struct psmouse *psmouse)
  327. {
  328. struct synaptics_data *priv = psmouse->private;
  329. unsigned char id[3];
  330. if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
  331. return -1;
  332. priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
  333. if (SYN_ID_IS_SYNAPTICS(priv->identity))
  334. return 0;
  335. return -1;
  336. }
  337. /*
  338. * Read touchpad resolution and maximum reported coordinates
  339. * Resolution is left zero if touchpad does not support the query
  340. */
  341. static int synaptics_resolution(struct psmouse *psmouse)
  342. {
  343. struct synaptics_data *priv = psmouse->private;
  344. unsigned char resp[3];
  345. if (SYN_ID_MAJOR(priv->identity) < 4)
  346. return 0;
  347. if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp) == 0) {
  348. if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
  349. priv->x_res = resp[0]; /* x resolution in units/mm */
  350. priv->y_res = resp[2]; /* y resolution in units/mm */
  351. }
  352. }
  353. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
  354. SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
  355. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MAX_COORDS, resp)) {
  356. psmouse_warn(psmouse,
  357. "device claims to have max coordinates query, but I'm not able to read it.\n");
  358. } else {
  359. priv->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
  360. priv->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
  361. psmouse_info(psmouse,
  362. "queried max coordinates: x [..%d], y [..%d]\n",
  363. priv->x_max, priv->y_max);
  364. }
  365. }
  366. if (SYN_CAP_MIN_DIMENSIONS(priv->ext_cap_0c) &&
  367. (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 7 ||
  368. /*
  369. * Firmware v8.1 does not report proper number of extended
  370. * capabilities, but has been proven to report correct min
  371. * coordinates.
  372. */
  373. SYN_ID_FULL(priv->identity) == 0x801)) {
  374. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_MIN_COORDS, resp)) {
  375. psmouse_warn(psmouse,
  376. "device claims to have min coordinates query, but I'm not able to read it.\n");
  377. } else {
  378. priv->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
  379. priv->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
  380. psmouse_info(psmouse,
  381. "queried min coordinates: x [%d..], y [%d..]\n",
  382. priv->x_min, priv->y_min);
  383. }
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Apply quirk(s) if the hardware matches
  389. */
  390. static void synaptics_apply_quirks(struct psmouse *psmouse)
  391. {
  392. struct synaptics_data *priv = psmouse->private;
  393. int i;
  394. for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
  395. if (!psmouse_matches_pnp_id(psmouse,
  396. min_max_pnpid_table[i].pnp_ids))
  397. continue;
  398. if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
  399. priv->board_id < min_max_pnpid_table[i].board_id.min)
  400. continue;
  401. if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
  402. priv->board_id > min_max_pnpid_table[i].board_id.max)
  403. continue;
  404. priv->x_min = min_max_pnpid_table[i].x_min;
  405. priv->x_max = min_max_pnpid_table[i].x_max;
  406. priv->y_min = min_max_pnpid_table[i].y_min;
  407. priv->y_max = min_max_pnpid_table[i].y_max;
  408. psmouse_info(psmouse,
  409. "quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
  410. priv->x_min, priv->x_max,
  411. priv->y_min, priv->y_max);
  412. break;
  413. }
  414. }
  415. static int synaptics_query_hardware(struct psmouse *psmouse)
  416. {
  417. if (synaptics_identify(psmouse))
  418. return -1;
  419. if (synaptics_model_id(psmouse))
  420. return -1;
  421. if (synaptics_firmware_id(psmouse))
  422. return -1;
  423. if (synaptics_query_modes(psmouse))
  424. return -1;
  425. if (synaptics_capability(psmouse))
  426. return -1;
  427. if (synaptics_resolution(psmouse))
  428. return -1;
  429. synaptics_apply_quirks(psmouse);
  430. return 0;
  431. }
  432. static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
  433. {
  434. static unsigned char param = 0xc8;
  435. struct synaptics_data *priv = psmouse->private;
  436. if (!(SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
  437. SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)))
  438. return 0;
  439. if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
  440. return -1;
  441. if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
  442. return -1;
  443. /* Advanced gesture mode also sends multi finger data */
  444. priv->capabilities |= BIT(1);
  445. return 0;
  446. }
  447. static int synaptics_set_mode(struct psmouse *psmouse)
  448. {
  449. struct synaptics_data *priv = psmouse->private;
  450. priv->mode = 0;
  451. if (priv->absolute_mode)
  452. priv->mode |= SYN_BIT_ABSOLUTE_MODE;
  453. if (priv->disable_gesture)
  454. priv->mode |= SYN_BIT_DISABLE_GESTURE;
  455. if (psmouse->rate >= 80)
  456. priv->mode |= SYN_BIT_HIGH_RATE;
  457. if (SYN_CAP_EXTENDED(priv->capabilities))
  458. priv->mode |= SYN_BIT_W_MODE;
  459. if (synaptics_mode_cmd(psmouse, priv->mode))
  460. return -1;
  461. if (priv->absolute_mode &&
  462. synaptics_set_advanced_gesture_mode(psmouse)) {
  463. psmouse_err(psmouse, "Advanced gesture mode init failed.\n");
  464. return -1;
  465. }
  466. return 0;
  467. }
  468. static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
  469. {
  470. struct synaptics_data *priv = psmouse->private;
  471. if (rate >= 80) {
  472. priv->mode |= SYN_BIT_HIGH_RATE;
  473. psmouse->rate = 80;
  474. } else {
  475. priv->mode &= ~SYN_BIT_HIGH_RATE;
  476. psmouse->rate = 40;
  477. }
  478. synaptics_mode_cmd(psmouse, priv->mode);
  479. }
  480. /*****************************************************************************
  481. * Synaptics pass-through PS/2 port support
  482. ****************************************************************************/
  483. static int synaptics_pt_write(struct serio *serio, unsigned char c)
  484. {
  485. struct psmouse *parent = serio_get_drvdata(serio->parent);
  486. char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
  487. if (psmouse_sliced_command(parent, c))
  488. return -1;
  489. if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
  490. return -1;
  491. return 0;
  492. }
  493. static int synaptics_pt_start(struct serio *serio)
  494. {
  495. struct psmouse *parent = serio_get_drvdata(serio->parent);
  496. struct synaptics_data *priv = parent->private;
  497. serio_pause_rx(parent->ps2dev.serio);
  498. priv->pt_port = serio;
  499. serio_continue_rx(parent->ps2dev.serio);
  500. return 0;
  501. }
  502. static void synaptics_pt_stop(struct serio *serio)
  503. {
  504. struct psmouse *parent = serio_get_drvdata(serio->parent);
  505. struct synaptics_data *priv = parent->private;
  506. serio_pause_rx(parent->ps2dev.serio);
  507. priv->pt_port = NULL;
  508. serio_continue_rx(parent->ps2dev.serio);
  509. }
  510. static int synaptics_is_pt_packet(unsigned char *buf)
  511. {
  512. return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
  513. }
  514. static void synaptics_pass_pt_packet(struct psmouse *psmouse,
  515. struct serio *ptport,
  516. unsigned char *packet)
  517. {
  518. struct synaptics_data *priv = psmouse->private;
  519. struct psmouse *child = serio_get_drvdata(ptport);
  520. if (child && child->state == PSMOUSE_ACTIVATED) {
  521. serio_interrupt(ptport, packet[1] | priv->pt_buttons, 0);
  522. serio_interrupt(ptport, packet[4], 0);
  523. serio_interrupt(ptport, packet[5], 0);
  524. if (child->pktsize == 4)
  525. serio_interrupt(ptport, packet[2], 0);
  526. } else {
  527. serio_interrupt(ptport, packet[1], 0);
  528. }
  529. }
  530. static void synaptics_pt_activate(struct psmouse *psmouse)
  531. {
  532. struct synaptics_data *priv = psmouse->private;
  533. struct psmouse *child = serio_get_drvdata(priv->pt_port);
  534. /* adjust the touchpad to child's choice of protocol */
  535. if (child) {
  536. if (child->pktsize == 4)
  537. priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
  538. else
  539. priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
  540. if (synaptics_mode_cmd(psmouse, priv->mode))
  541. psmouse_warn(psmouse,
  542. "failed to switch guest protocol\n");
  543. }
  544. }
  545. static void synaptics_pt_create(struct psmouse *psmouse)
  546. {
  547. struct serio *serio;
  548. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  549. if (!serio) {
  550. psmouse_err(psmouse,
  551. "not enough memory for pass-through port\n");
  552. return;
  553. }
  554. serio->id.type = SERIO_PS_PSTHRU;
  555. strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
  556. strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
  557. serio->write = synaptics_pt_write;
  558. serio->start = synaptics_pt_start;
  559. serio->stop = synaptics_pt_stop;
  560. serio->parent = psmouse->ps2dev.serio;
  561. psmouse->pt_activate = synaptics_pt_activate;
  562. psmouse_info(psmouse, "serio: %s port at %s\n",
  563. serio->name, psmouse->phys);
  564. serio_register_port(serio);
  565. }
  566. /*****************************************************************************
  567. * Functions to interpret the absolute mode packets
  568. ****************************************************************************/
  569. static void synaptics_parse_agm(const unsigned char buf[],
  570. struct synaptics_data *priv,
  571. struct synaptics_hw_state *hw)
  572. {
  573. struct synaptics_hw_state *agm = &priv->agm;
  574. int agm_packet_type;
  575. agm_packet_type = (buf[5] & 0x30) >> 4;
  576. switch (agm_packet_type) {
  577. case 1:
  578. /* Gesture packet: (x, y, z) half resolution */
  579. agm->w = hw->w;
  580. agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
  581. agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
  582. agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
  583. break;
  584. case 2:
  585. /* AGM-CONTACT packet: we are only interested in the count */
  586. priv->agm_count = buf[1];
  587. break;
  588. default:
  589. break;
  590. }
  591. }
  592. static void synaptics_parse_ext_buttons(const unsigned char buf[],
  593. struct synaptics_data *priv,
  594. struct synaptics_hw_state *hw)
  595. {
  596. unsigned int ext_bits =
  597. (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
  598. unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
  599. hw->ext_buttons = buf[4] & ext_mask;
  600. hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
  601. }
  602. static int synaptics_parse_hw_state(const unsigned char buf[],
  603. struct synaptics_data *priv,
  604. struct synaptics_hw_state *hw)
  605. {
  606. memset(hw, 0, sizeof(struct synaptics_hw_state));
  607. if (SYN_MODEL_NEWABS(priv->model_id)) {
  608. hw->w = (((buf[0] & 0x30) >> 2) |
  609. ((buf[0] & 0x04) >> 1) |
  610. ((buf[3] & 0x04) >> 2));
  611. if ((SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) ||
  612. SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) &&
  613. hw->w == 2) {
  614. synaptics_parse_agm(buf, priv, hw);
  615. return 1;
  616. }
  617. hw->x = (((buf[3] & 0x10) << 8) |
  618. ((buf[1] & 0x0f) << 8) |
  619. buf[4]);
  620. hw->y = (((buf[3] & 0x20) << 7) |
  621. ((buf[1] & 0xf0) << 4) |
  622. buf[5]);
  623. hw->z = buf[2];
  624. hw->left = (buf[0] & 0x01) ? 1 : 0;
  625. hw->right = (buf[0] & 0x02) ? 1 : 0;
  626. if (priv->is_forcepad) {
  627. /*
  628. * ForcePads, like Clickpads, use middle button
  629. * bits to report primary button clicks.
  630. * Unfortunately they report primary button not
  631. * only when user presses on the pad above certain
  632. * threshold, but also when there are more than one
  633. * finger on the touchpad, which interferes with
  634. * out multi-finger gestures.
  635. */
  636. if (hw->z == 0) {
  637. /* No contacts */
  638. priv->press = priv->report_press = false;
  639. } else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
  640. /*
  641. * Single-finger touch with pressure above
  642. * the threshold. If pressure stays long
  643. * enough, we'll start reporting primary
  644. * button. We rely on the device continuing
  645. * sending data even if finger does not
  646. * move.
  647. */
  648. if (!priv->press) {
  649. priv->press_start = jiffies;
  650. priv->press = true;
  651. } else if (time_after(jiffies,
  652. priv->press_start +
  653. msecs_to_jiffies(50))) {
  654. priv->report_press = true;
  655. }
  656. } else {
  657. priv->press = false;
  658. }
  659. hw->left = priv->report_press;
  660. } else if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  661. /*
  662. * Clickpad's button is transmitted as middle button,
  663. * however, since it is primary button, we will report
  664. * it as BTN_LEFT.
  665. */
  666. hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  667. } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  668. hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  669. if (hw->w == 2)
  670. hw->scroll = (signed char)(buf[1]);
  671. }
  672. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  673. hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  674. hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
  675. }
  676. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 0 &&
  677. ((buf[0] ^ buf[3]) & 0x02)) {
  678. synaptics_parse_ext_buttons(buf, priv, hw);
  679. }
  680. } else {
  681. hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
  682. hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
  683. hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
  684. hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
  685. hw->left = (buf[0] & 0x01) ? 1 : 0;
  686. hw->right = (buf[0] & 0x02) ? 1 : 0;
  687. }
  688. /*
  689. * Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
  690. * is used by some firmware to indicate a finger at the edge of
  691. * the touchpad whose precise position cannot be determined, so
  692. * convert these values to the maximum axis value.
  693. */
  694. if (hw->x > X_MAX_POSITIVE)
  695. hw->x -= 1 << ABS_POS_BITS;
  696. else if (hw->x == X_MAX_POSITIVE)
  697. hw->x = XMAX;
  698. if (hw->y > Y_MAX_POSITIVE)
  699. hw->y -= 1 << ABS_POS_BITS;
  700. else if (hw->y == Y_MAX_POSITIVE)
  701. hw->y = YMAX;
  702. return 0;
  703. }
  704. static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
  705. bool active, int x, int y)
  706. {
  707. input_mt_slot(dev, slot);
  708. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  709. if (active) {
  710. input_report_abs(dev, ABS_MT_POSITION_X, x);
  711. input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
  712. }
  713. }
  714. static void synaptics_report_semi_mt_data(struct input_dev *dev,
  715. const struct synaptics_hw_state *a,
  716. const struct synaptics_hw_state *b,
  717. int num_fingers)
  718. {
  719. if (num_fingers >= 2) {
  720. synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
  721. min(a->y, b->y));
  722. synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
  723. max(a->y, b->y));
  724. } else if (num_fingers == 1) {
  725. synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
  726. synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
  727. } else {
  728. synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
  729. synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
  730. }
  731. }
  732. static void synaptics_report_ext_buttons(struct psmouse *psmouse,
  733. const struct synaptics_hw_state *hw)
  734. {
  735. struct input_dev *dev = psmouse->dev;
  736. struct synaptics_data *priv = psmouse->private;
  737. int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) + 1) >> 1;
  738. char buf[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  739. int i;
  740. if (!SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
  741. return;
  742. /* Bug in FW 8.1, buttons are reported only when ExtBit is 1 */
  743. if (SYN_ID_FULL(priv->identity) == 0x801 &&
  744. !((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
  745. return;
  746. if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10)) {
  747. for (i = 0; i < ext_bits; i++) {
  748. input_report_key(dev, BTN_0 + 2 * i,
  749. hw->ext_buttons & (1 << i));
  750. input_report_key(dev, BTN_1 + 2 * i,
  751. hw->ext_buttons & (1 << (i + ext_bits)));
  752. }
  753. return;
  754. }
  755. /*
  756. * This generation of touchpads has the trackstick buttons
  757. * physically wired to the touchpad. Re-route them through
  758. * the pass-through interface.
  759. */
  760. if (!priv->pt_port)
  761. return;
  762. /* The trackstick expects at most 3 buttons */
  763. priv->pt_buttons = SYN_CAP_EXT_BUTTON_STICK_L(hw->ext_buttons) |
  764. SYN_CAP_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
  765. SYN_CAP_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
  766. synaptics_pass_pt_packet(psmouse, priv->pt_port, buf);
  767. }
  768. static void synaptics_report_buttons(struct psmouse *psmouse,
  769. const struct synaptics_hw_state *hw)
  770. {
  771. struct input_dev *dev = psmouse->dev;
  772. struct synaptics_data *priv = psmouse->private;
  773. input_report_key(dev, BTN_LEFT, hw->left);
  774. input_report_key(dev, BTN_RIGHT, hw->right);
  775. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  776. input_report_key(dev, BTN_MIDDLE, hw->middle);
  777. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  778. input_report_key(dev, BTN_FORWARD, hw->up);
  779. input_report_key(dev, BTN_BACK, hw->down);
  780. }
  781. synaptics_report_ext_buttons(psmouse, hw);
  782. }
  783. static void synaptics_report_mt_data(struct psmouse *psmouse,
  784. const struct synaptics_hw_state *sgm,
  785. int num_fingers)
  786. {
  787. struct input_dev *dev = psmouse->dev;
  788. struct synaptics_data *priv = psmouse->private;
  789. const struct synaptics_hw_state *hw[2] = { sgm, &priv->agm };
  790. struct input_mt_pos pos[2];
  791. int slot[2], nsemi, i;
  792. nsemi = clamp_val(num_fingers, 0, 2);
  793. for (i = 0; i < nsemi; i++) {
  794. pos[i].x = hw[i]->x;
  795. pos[i].y = synaptics_invert_y(hw[i]->y);
  796. }
  797. input_mt_assign_slots(dev, slot, pos, nsemi, DMAX * priv->x_res);
  798. for (i = 0; i < nsemi; i++) {
  799. input_mt_slot(dev, slot[i]);
  800. input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
  801. input_report_abs(dev, ABS_MT_POSITION_X, pos[i].x);
  802. input_report_abs(dev, ABS_MT_POSITION_Y, pos[i].y);
  803. input_report_abs(dev, ABS_MT_PRESSURE, hw[i]->z);
  804. }
  805. input_mt_drop_unused(dev);
  806. /* Don't use active slot count to generate BTN_TOOL events. */
  807. input_mt_report_pointer_emulation(dev, false);
  808. /* Send the number of fingers reported by touchpad itself. */
  809. input_mt_report_finger_count(dev, num_fingers);
  810. synaptics_report_buttons(psmouse, sgm);
  811. input_sync(dev);
  812. }
  813. static void synaptics_image_sensor_process(struct psmouse *psmouse,
  814. struct synaptics_hw_state *sgm)
  815. {
  816. struct synaptics_data *priv = psmouse->private;
  817. int num_fingers;
  818. /*
  819. * Update mt_state using the new finger count and current mt_state.
  820. */
  821. if (sgm->z == 0)
  822. num_fingers = 0;
  823. else if (sgm->w >= 4)
  824. num_fingers = 1;
  825. else if (sgm->w == 0)
  826. num_fingers = 2;
  827. else if (sgm->w == 1)
  828. num_fingers = priv->agm_count ? priv->agm_count : 3;
  829. else
  830. num_fingers = 4;
  831. /* Send resulting input events to user space */
  832. synaptics_report_mt_data(psmouse, sgm, num_fingers);
  833. }
  834. /*
  835. * called for each full received packet from the touchpad
  836. */
  837. static void synaptics_process_packet(struct psmouse *psmouse)
  838. {
  839. struct input_dev *dev = psmouse->dev;
  840. struct synaptics_data *priv = psmouse->private;
  841. struct synaptics_hw_state hw;
  842. int num_fingers;
  843. int finger_width;
  844. if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
  845. return;
  846. if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
  847. synaptics_image_sensor_process(psmouse, &hw);
  848. return;
  849. }
  850. if (hw.scroll) {
  851. priv->scroll += hw.scroll;
  852. while (priv->scroll >= 4) {
  853. input_report_key(dev, BTN_BACK, !hw.down);
  854. input_sync(dev);
  855. input_report_key(dev, BTN_BACK, hw.down);
  856. input_sync(dev);
  857. priv->scroll -= 4;
  858. }
  859. while (priv->scroll <= -4) {
  860. input_report_key(dev, BTN_FORWARD, !hw.up);
  861. input_sync(dev);
  862. input_report_key(dev, BTN_FORWARD, hw.up);
  863. input_sync(dev);
  864. priv->scroll += 4;
  865. }
  866. return;
  867. }
  868. if (hw.z > 0 && hw.x > 1) {
  869. num_fingers = 1;
  870. finger_width = 5;
  871. if (SYN_CAP_EXTENDED(priv->capabilities)) {
  872. switch (hw.w) {
  873. case 0 ... 1:
  874. if (SYN_CAP_MULTIFINGER(priv->capabilities))
  875. num_fingers = hw.w + 2;
  876. break;
  877. case 2:
  878. if (SYN_MODEL_PEN(priv->model_id))
  879. ; /* Nothing, treat a pen as a single finger */
  880. break;
  881. case 4 ... 15:
  882. if (SYN_CAP_PALMDETECT(priv->capabilities))
  883. finger_width = hw.w;
  884. break;
  885. }
  886. }
  887. } else {
  888. num_fingers = 0;
  889. finger_width = 0;
  890. }
  891. if (cr48_profile_sensor) {
  892. synaptics_report_mt_data(psmouse, &hw, num_fingers);
  893. return;
  894. }
  895. if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
  896. synaptics_report_semi_mt_data(dev, &hw, &priv->agm,
  897. num_fingers);
  898. /* Post events
  899. * BTN_TOUCH has to be first as mousedev relies on it when doing
  900. * absolute -> relative conversion
  901. */
  902. if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
  903. if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
  904. if (num_fingers > 0) {
  905. input_report_abs(dev, ABS_X, hw.x);
  906. input_report_abs(dev, ABS_Y, synaptics_invert_y(hw.y));
  907. }
  908. input_report_abs(dev, ABS_PRESSURE, hw.z);
  909. if (SYN_CAP_PALMDETECT(priv->capabilities))
  910. input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
  911. input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
  912. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  913. input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
  914. input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
  915. }
  916. synaptics_report_buttons(psmouse, &hw);
  917. input_sync(dev);
  918. }
  919. static int synaptics_validate_byte(struct psmouse *psmouse,
  920. int idx, unsigned char pkt_type)
  921. {
  922. static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
  923. static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
  924. static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
  925. static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
  926. static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
  927. const char *packet = psmouse->packet;
  928. if (idx < 0 || idx > 4)
  929. return 0;
  930. switch (pkt_type) {
  931. case SYN_NEWABS:
  932. case SYN_NEWABS_RELAXED:
  933. return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
  934. case SYN_NEWABS_STRICT:
  935. return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
  936. case SYN_OLDABS:
  937. return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
  938. default:
  939. psmouse_err(psmouse, "unknown packet type %d\n", pkt_type);
  940. return 0;
  941. }
  942. }
  943. static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
  944. {
  945. int i;
  946. for (i = 0; i < 5; i++)
  947. if (!synaptics_validate_byte(psmouse, i, SYN_NEWABS_STRICT)) {
  948. psmouse_info(psmouse, "using relaxed packet validation\n");
  949. return SYN_NEWABS_RELAXED;
  950. }
  951. return SYN_NEWABS_STRICT;
  952. }
  953. static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
  954. {
  955. struct synaptics_data *priv = psmouse->private;
  956. if (psmouse->pktcnt >= 6) { /* Full packet received */
  957. if (unlikely(priv->pkt_type == SYN_NEWABS))
  958. priv->pkt_type = synaptics_detect_pkt_type(psmouse);
  959. if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
  960. synaptics_is_pt_packet(psmouse->packet)) {
  961. if (priv->pt_port)
  962. synaptics_pass_pt_packet(psmouse, priv->pt_port,
  963. psmouse->packet);
  964. } else
  965. synaptics_process_packet(psmouse);
  966. return PSMOUSE_FULL_PACKET;
  967. }
  968. return synaptics_validate_byte(psmouse, psmouse->pktcnt - 1, priv->pkt_type) ?
  969. PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
  970. }
  971. /*****************************************************************************
  972. * Driver initialization/cleanup functions
  973. ****************************************************************************/
  974. static void set_abs_position_params(struct input_dev *dev,
  975. struct synaptics_data *priv, int x_code,
  976. int y_code)
  977. {
  978. int x_min = priv->x_min ?: XMIN_NOMINAL;
  979. int x_max = priv->x_max ?: XMAX_NOMINAL;
  980. int y_min = priv->y_min ?: YMIN_NOMINAL;
  981. int y_max = priv->y_max ?: YMAX_NOMINAL;
  982. int fuzz = SYN_CAP_REDUCED_FILTERING(priv->ext_cap_0c) ?
  983. SYN_REDUCED_FILTER_FUZZ : 0;
  984. input_set_abs_params(dev, x_code, x_min, x_max, fuzz, 0);
  985. input_set_abs_params(dev, y_code, y_min, y_max, fuzz, 0);
  986. input_abs_set_res(dev, x_code, priv->x_res);
  987. input_abs_set_res(dev, y_code, priv->y_res);
  988. }
  989. static void set_input_params(struct psmouse *psmouse,
  990. struct synaptics_data *priv)
  991. {
  992. struct input_dev *dev = psmouse->dev;
  993. int i;
  994. /* Things that apply to both modes */
  995. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  996. __set_bit(EV_KEY, dev->evbit);
  997. __set_bit(BTN_LEFT, dev->keybit);
  998. __set_bit(BTN_RIGHT, dev->keybit);
  999. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  1000. __set_bit(BTN_MIDDLE, dev->keybit);
  1001. if (!priv->absolute_mode) {
  1002. /* Relative mode */
  1003. __set_bit(EV_REL, dev->evbit);
  1004. __set_bit(REL_X, dev->relbit);
  1005. __set_bit(REL_Y, dev->relbit);
  1006. return;
  1007. }
  1008. /* Absolute mode */
  1009. __set_bit(EV_ABS, dev->evbit);
  1010. set_abs_position_params(dev, priv, ABS_X, ABS_Y);
  1011. input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
  1012. if (cr48_profile_sensor)
  1013. input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1014. if (SYN_CAP_IMAGE_SENSOR(priv->ext_cap_0c)) {
  1015. set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
  1016. ABS_MT_POSITION_Y);
  1017. /* Image sensors can report per-contact pressure */
  1018. input_set_abs_params(dev, ABS_MT_PRESSURE, 0, 255, 0, 0);
  1019. input_mt_init_slots(dev, 2, INPUT_MT_POINTER | INPUT_MT_TRACK);
  1020. /* Image sensors can signal 4 and 5 finger clicks */
  1021. __set_bit(BTN_TOOL_QUADTAP, dev->keybit);
  1022. __set_bit(BTN_TOOL_QUINTTAP, dev->keybit);
  1023. } else if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
  1024. set_abs_position_params(dev, priv, ABS_MT_POSITION_X,
  1025. ABS_MT_POSITION_Y);
  1026. /*
  1027. * Profile sensor in CR-48 tracks contacts reasonably well,
  1028. * other non-image sensors with AGM use semi-mt.
  1029. */
  1030. input_mt_init_slots(dev, 2,
  1031. INPUT_MT_POINTER |
  1032. (cr48_profile_sensor ?
  1033. INPUT_MT_TRACK : INPUT_MT_SEMI_MT));
  1034. }
  1035. if (SYN_CAP_PALMDETECT(priv->capabilities))
  1036. input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
  1037. __set_bit(BTN_TOUCH, dev->keybit);
  1038. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  1039. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  1040. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  1041. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  1042. }
  1043. if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
  1044. SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  1045. __set_bit(BTN_FORWARD, dev->keybit);
  1046. __set_bit(BTN_BACK, dev->keybit);
  1047. }
  1048. if (!SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
  1049. for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
  1050. __set_bit(BTN_0 + i, dev->keybit);
  1051. __clear_bit(EV_REL, dev->evbit);
  1052. __clear_bit(REL_X, dev->relbit);
  1053. __clear_bit(REL_Y, dev->relbit);
  1054. if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  1055. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  1056. if (psmouse_matches_pnp_id(psmouse, topbuttonpad_pnp_ids) &&
  1057. !SYN_CAP_EXT_BUTTONS_STICK(priv->ext_cap_10))
  1058. __set_bit(INPUT_PROP_TOPBUTTONPAD, dev->propbit);
  1059. /* Clickpads report only left button */
  1060. __clear_bit(BTN_RIGHT, dev->keybit);
  1061. __clear_bit(BTN_MIDDLE, dev->keybit);
  1062. }
  1063. }
  1064. static ssize_t synaptics_show_disable_gesture(struct psmouse *psmouse,
  1065. void *data, char *buf)
  1066. {
  1067. struct synaptics_data *priv = psmouse->private;
  1068. return sprintf(buf, "%c\n", priv->disable_gesture ? '1' : '0');
  1069. }
  1070. static ssize_t synaptics_set_disable_gesture(struct psmouse *psmouse,
  1071. void *data, const char *buf,
  1072. size_t len)
  1073. {
  1074. struct synaptics_data *priv = psmouse->private;
  1075. unsigned int value;
  1076. int err;
  1077. err = kstrtouint(buf, 10, &value);
  1078. if (err)
  1079. return err;
  1080. if (value > 1)
  1081. return -EINVAL;
  1082. if (value == priv->disable_gesture)
  1083. return len;
  1084. priv->disable_gesture = value;
  1085. if (value)
  1086. priv->mode |= SYN_BIT_DISABLE_GESTURE;
  1087. else
  1088. priv->mode &= ~SYN_BIT_DISABLE_GESTURE;
  1089. if (synaptics_mode_cmd(psmouse, priv->mode))
  1090. return -EIO;
  1091. return len;
  1092. }
  1093. PSMOUSE_DEFINE_ATTR(disable_gesture, S_IWUSR | S_IRUGO, NULL,
  1094. synaptics_show_disable_gesture,
  1095. synaptics_set_disable_gesture);
  1096. static void synaptics_disconnect(struct psmouse *psmouse)
  1097. {
  1098. struct synaptics_data *priv = psmouse->private;
  1099. if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity))
  1100. device_remove_file(&psmouse->ps2dev.serio->dev,
  1101. &psmouse_attr_disable_gesture.dattr);
  1102. synaptics_reset(psmouse);
  1103. kfree(priv);
  1104. psmouse->private = NULL;
  1105. }
  1106. static int synaptics_reconnect(struct psmouse *psmouse)
  1107. {
  1108. struct synaptics_data *priv = psmouse->private;
  1109. struct synaptics_data old_priv = *priv;
  1110. unsigned char param[2];
  1111. int retry = 0;
  1112. int error;
  1113. do {
  1114. psmouse_reset(psmouse);
  1115. if (retry) {
  1116. /*
  1117. * On some boxes, right after resuming, the touchpad
  1118. * needs some time to finish initializing (I assume
  1119. * it needs time to calibrate) and start responding
  1120. * to Synaptics-specific queries, so let's wait a
  1121. * bit.
  1122. */
  1123. ssleep(1);
  1124. }
  1125. ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETID);
  1126. error = synaptics_detect(psmouse, 0);
  1127. } while (error && ++retry < 3);
  1128. if (error)
  1129. return -1;
  1130. if (retry > 1)
  1131. psmouse_dbg(psmouse, "reconnected after %d tries\n", retry);
  1132. if (synaptics_query_hardware(psmouse)) {
  1133. psmouse_err(psmouse, "Unable to query device.\n");
  1134. return -1;
  1135. }
  1136. if (synaptics_set_mode(psmouse)) {
  1137. psmouse_err(psmouse, "Unable to initialize device.\n");
  1138. return -1;
  1139. }
  1140. if (old_priv.identity != priv->identity ||
  1141. old_priv.model_id != priv->model_id ||
  1142. old_priv.capabilities != priv->capabilities ||
  1143. old_priv.ext_cap != priv->ext_cap) {
  1144. psmouse_err(psmouse,
  1145. "hardware appears to be different: id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
  1146. old_priv.identity, priv->identity,
  1147. old_priv.model_id, priv->model_id,
  1148. old_priv.capabilities, priv->capabilities,
  1149. old_priv.ext_cap, priv->ext_cap);
  1150. return -1;
  1151. }
  1152. return 0;
  1153. }
  1154. static bool impaired_toshiba_kbc;
  1155. static const struct dmi_system_id toshiba_dmi_table[] __initconst = {
  1156. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1157. {
  1158. /* Toshiba Satellite */
  1159. .matches = {
  1160. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1161. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  1162. },
  1163. },
  1164. {
  1165. /* Toshiba Dynabook */
  1166. .matches = {
  1167. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1168. DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
  1169. },
  1170. },
  1171. {
  1172. /* Toshiba Portege M300 */
  1173. .matches = {
  1174. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1175. DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
  1176. },
  1177. },
  1178. {
  1179. /* Toshiba Portege M300 */
  1180. .matches = {
  1181. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  1182. DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
  1183. DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
  1184. },
  1185. },
  1186. #endif
  1187. { }
  1188. };
  1189. static bool broken_olpc_ec;
  1190. static const struct dmi_system_id olpc_dmi_table[] __initconst = {
  1191. #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
  1192. {
  1193. /* OLPC XO-1 or XO-1.5 */
  1194. .matches = {
  1195. DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
  1196. DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
  1197. },
  1198. },
  1199. #endif
  1200. { }
  1201. };
  1202. static const struct dmi_system_id __initconst cr48_dmi_table[] = {
  1203. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  1204. {
  1205. /* Cr-48 Chromebook (Codename Mario) */
  1206. .matches = {
  1207. DMI_MATCH(DMI_SYS_VENDOR, "IEC"),
  1208. DMI_MATCH(DMI_PRODUCT_NAME, "Mario"),
  1209. },
  1210. },
  1211. #endif
  1212. { }
  1213. };
  1214. void __init synaptics_module_init(void)
  1215. {
  1216. impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
  1217. broken_olpc_ec = dmi_check_system(olpc_dmi_table);
  1218. cr48_profile_sensor = dmi_check_system(cr48_dmi_table);
  1219. }
  1220. static int __synaptics_init(struct psmouse *psmouse, bool absolute_mode)
  1221. {
  1222. struct synaptics_data *priv;
  1223. int err = -1;
  1224. /*
  1225. * The OLPC XO has issues with Synaptics' absolute mode; the constant
  1226. * packet spew overloads the EC such that key presses on the keyboard
  1227. * are missed. Given that, don't even attempt to use Absolute mode.
  1228. * Relative mode seems to work just fine.
  1229. */
  1230. if (absolute_mode && broken_olpc_ec) {
  1231. psmouse_info(psmouse,
  1232. "OLPC XO detected, not enabling Synaptics protocol.\n");
  1233. return -ENODEV;
  1234. }
  1235. psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
  1236. if (!priv)
  1237. return -ENOMEM;
  1238. psmouse_reset(psmouse);
  1239. if (synaptics_query_hardware(psmouse)) {
  1240. psmouse_err(psmouse, "Unable to query device.\n");
  1241. goto init_fail;
  1242. }
  1243. priv->absolute_mode = absolute_mode;
  1244. if (SYN_ID_DISGEST_SUPPORTED(priv->identity))
  1245. priv->disable_gesture = true;
  1246. /*
  1247. * Unfortunately ForcePad capability is not exported over PS/2,
  1248. * so we have to resort to checking PNP IDs.
  1249. */
  1250. priv->is_forcepad = psmouse_matches_pnp_id(psmouse, forcepad_pnp_ids);
  1251. if (synaptics_set_mode(psmouse)) {
  1252. psmouse_err(psmouse, "Unable to initialize device.\n");
  1253. goto init_fail;
  1254. }
  1255. priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
  1256. psmouse_info(psmouse,
  1257. "Touchpad model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx/%#lx, board id: %lu, fw id: %lu\n",
  1258. SYN_ID_MODEL(priv->identity),
  1259. SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
  1260. priv->model_id,
  1261. priv->capabilities, priv->ext_cap, priv->ext_cap_0c,
  1262. priv->ext_cap_10, priv->board_id, priv->firmware_id);
  1263. set_input_params(psmouse, priv);
  1264. /*
  1265. * Encode touchpad model so that it can be used to set
  1266. * input device->id.version and be visible to userspace.
  1267. * Because version is __u16 we have to drop something.
  1268. * Hardware info bits seem to be good candidates as they
  1269. * are documented to be for Synaptics corp. internal use.
  1270. */
  1271. psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
  1272. (priv->model_id & 0x000000ff);
  1273. if (absolute_mode) {
  1274. psmouse->protocol_handler = synaptics_process_byte;
  1275. psmouse->pktsize = 6;
  1276. } else {
  1277. /* Relative mode follows standard PS/2 mouse protocol */
  1278. psmouse->protocol_handler = psmouse_process_byte;
  1279. psmouse->pktsize = 3;
  1280. }
  1281. psmouse->set_rate = synaptics_set_rate;
  1282. psmouse->disconnect = synaptics_disconnect;
  1283. psmouse->reconnect = synaptics_reconnect;
  1284. psmouse->cleanup = synaptics_reset;
  1285. /* Synaptics can usually stay in sync without extra help */
  1286. psmouse->resync_time = 0;
  1287. if (SYN_CAP_PASS_THROUGH(priv->capabilities))
  1288. synaptics_pt_create(psmouse);
  1289. /*
  1290. * Toshiba's KBC seems to have trouble handling data from
  1291. * Synaptics at full rate. Switch to a lower rate (roughly
  1292. * the same rate as a standard PS/2 mouse).
  1293. */
  1294. if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
  1295. psmouse_info(psmouse,
  1296. "Toshiba %s detected, limiting rate to 40pps.\n",
  1297. dmi_get_system_info(DMI_PRODUCT_NAME));
  1298. psmouse->rate = 40;
  1299. }
  1300. if (!priv->absolute_mode && SYN_ID_DISGEST_SUPPORTED(priv->identity)) {
  1301. err = device_create_file(&psmouse->ps2dev.serio->dev,
  1302. &psmouse_attr_disable_gesture.dattr);
  1303. if (err) {
  1304. psmouse_err(psmouse,
  1305. "Failed to create disable_gesture attribute (%d)",
  1306. err);
  1307. goto init_fail;
  1308. }
  1309. }
  1310. return 0;
  1311. init_fail:
  1312. kfree(priv);
  1313. return err;
  1314. }
  1315. int synaptics_init(struct psmouse *psmouse)
  1316. {
  1317. return __synaptics_init(psmouse, true);
  1318. }
  1319. int synaptics_init_relative(struct psmouse *psmouse)
  1320. {
  1321. return __synaptics_init(psmouse, false);
  1322. }
  1323. #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
  1324. void __init synaptics_module_init(void)
  1325. {
  1326. }
  1327. int synaptics_init(struct psmouse *psmouse)
  1328. {
  1329. return -ENOSYS;
  1330. }
  1331. #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */