synaptics.c 47 KB

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