synaptics.c 42 KB

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