synaptics.c 42 KB

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