synaptics.c 39 KB

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