synaptics.c 42 KB

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