synaptics.c 47 KB

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