synaptics.c 43 KB

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