synaptics.c 42 KB

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