synaptics.c 42 KB

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