synaptics.c 42 KB

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