synaptics.c 42 KB

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