synaptics.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. * Send a command to the synpatics touchpad by special commands
  90. */
  91. static int synaptics_send_cmd(struct psmouse *psmouse, unsigned char c, unsigned char *param)
  92. {
  93. if (psmouse_sliced_command(psmouse, c))
  94. return -1;
  95. if (ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO))
  96. return -1;
  97. return 0;
  98. }
  99. /*
  100. * Read the model-id bytes from the touchpad
  101. * see also SYN_MODEL_* macros
  102. */
  103. static int synaptics_model_id(struct psmouse *psmouse)
  104. {
  105. struct synaptics_data *priv = psmouse->private;
  106. unsigned char mi[3];
  107. if (synaptics_send_cmd(psmouse, SYN_QUE_MODEL, mi))
  108. return -1;
  109. priv->model_id = (mi[0]<<16) | (mi[1]<<8) | mi[2];
  110. return 0;
  111. }
  112. /*
  113. * Read the capability-bits from the touchpad
  114. * see also the SYN_CAP_* macros
  115. */
  116. static int synaptics_capability(struct psmouse *psmouse)
  117. {
  118. struct synaptics_data *priv = psmouse->private;
  119. unsigned char cap[3];
  120. if (synaptics_send_cmd(psmouse, SYN_QUE_CAPABILITIES, cap))
  121. return -1;
  122. priv->capabilities = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  123. priv->ext_cap = priv->ext_cap_0c = 0;
  124. /*
  125. * Older firmwares had submodel ID fixed to 0x47
  126. */
  127. if (SYN_ID_FULL(priv->identity) < 0x705 &&
  128. SYN_CAP_SUBMODEL_ID(priv->capabilities) != 0x47) {
  129. return -1;
  130. }
  131. /*
  132. * Unless capExtended is set the rest of the flags should be ignored
  133. */
  134. if (!SYN_CAP_EXTENDED(priv->capabilities))
  135. priv->capabilities = 0;
  136. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 1) {
  137. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB, cap)) {
  138. printk(KERN_ERR "Synaptics claims to have extended capabilities,"
  139. " but I'm not able to read them.\n");
  140. } else {
  141. priv->ext_cap = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  142. /*
  143. * if nExtBtn is greater than 8 it should be considered
  144. * invalid and treated as 0
  145. */
  146. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 8)
  147. priv->ext_cap &= 0xff0fff;
  148. }
  149. }
  150. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 4) {
  151. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_CAPAB_0C, cap)) {
  152. printk(KERN_ERR "Synaptics claims to have extended capability 0x0c,"
  153. " but I'm not able to read it.\n");
  154. } else {
  155. priv->ext_cap_0c = (cap[0] << 16) | (cap[1] << 8) | cap[2];
  156. }
  157. }
  158. return 0;
  159. }
  160. /*
  161. * Identify Touchpad
  162. * See also the SYN_ID_* macros
  163. */
  164. static int synaptics_identify(struct psmouse *psmouse)
  165. {
  166. struct synaptics_data *priv = psmouse->private;
  167. unsigned char id[3];
  168. if (synaptics_send_cmd(psmouse, SYN_QUE_IDENTIFY, id))
  169. return -1;
  170. priv->identity = (id[0]<<16) | (id[1]<<8) | id[2];
  171. if (SYN_ID_IS_SYNAPTICS(priv->identity))
  172. return 0;
  173. return -1;
  174. }
  175. /*
  176. * Read touchpad resolution and maximum reported coordinates
  177. * Resolution is left zero if touchpad does not support the query
  178. */
  179. static int synaptics_resolution(struct psmouse *psmouse)
  180. {
  181. struct synaptics_data *priv = psmouse->private;
  182. unsigned char res[3];
  183. unsigned char max[3];
  184. if (SYN_ID_MAJOR(priv->identity) < 4)
  185. return 0;
  186. if (synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, res) == 0) {
  187. if (res[0] != 0 && (res[1] & 0x80) && res[2] != 0) {
  188. priv->x_res = res[0]; /* x resolution in units/mm */
  189. priv->y_res = res[2]; /* y resolution in units/mm */
  190. }
  191. }
  192. if (SYN_EXT_CAP_REQUESTS(priv->capabilities) >= 5 &&
  193. SYN_CAP_MAX_DIMENSIONS(priv->ext_cap_0c)) {
  194. if (synaptics_send_cmd(psmouse, SYN_QUE_EXT_DIMENSIONS, max)) {
  195. printk(KERN_ERR "Synaptics claims to have dimensions query,"
  196. " but I'm not able to read it.\n");
  197. } else {
  198. priv->x_max = (max[0] << 5) | ((max[1] & 0x0f) << 1);
  199. priv->y_max = (max[2] << 5) | ((max[1] & 0xf0) >> 3);
  200. }
  201. }
  202. return 0;
  203. }
  204. static int synaptics_query_hardware(struct psmouse *psmouse)
  205. {
  206. if (synaptics_identify(psmouse))
  207. return -1;
  208. if (synaptics_model_id(psmouse))
  209. return -1;
  210. if (synaptics_capability(psmouse))
  211. return -1;
  212. if (synaptics_resolution(psmouse))
  213. return -1;
  214. return 0;
  215. }
  216. static int synaptics_set_absolute_mode(struct psmouse *psmouse)
  217. {
  218. struct synaptics_data *priv = psmouse->private;
  219. priv->mode = SYN_BIT_ABSOLUTE_MODE;
  220. if (SYN_ID_MAJOR(priv->identity) >= 4)
  221. priv->mode |= SYN_BIT_DISABLE_GESTURE;
  222. if (SYN_CAP_EXTENDED(priv->capabilities))
  223. priv->mode |= SYN_BIT_W_MODE;
  224. if (synaptics_mode_cmd(psmouse, priv->mode))
  225. return -1;
  226. return 0;
  227. }
  228. static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
  229. {
  230. struct synaptics_data *priv = psmouse->private;
  231. if (rate >= 80) {
  232. priv->mode |= SYN_BIT_HIGH_RATE;
  233. psmouse->rate = 80;
  234. } else {
  235. priv->mode &= ~SYN_BIT_HIGH_RATE;
  236. psmouse->rate = 40;
  237. }
  238. synaptics_mode_cmd(psmouse, priv->mode);
  239. }
  240. static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
  241. {
  242. static unsigned char param = 0xc8;
  243. struct synaptics_data *priv = psmouse->private;
  244. if (!SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
  245. return 0;
  246. if (psmouse_sliced_command(psmouse, SYN_QUE_MODEL))
  247. return -1;
  248. if (ps2_command(&psmouse->ps2dev, &param, PSMOUSE_CMD_SETRATE))
  249. return -1;
  250. /* Advanced gesture mode also sends multi finger data */
  251. priv->capabilities |= BIT(1);
  252. return 0;
  253. }
  254. /*****************************************************************************
  255. * Synaptics pass-through PS/2 port support
  256. ****************************************************************************/
  257. static int synaptics_pt_write(struct serio *serio, unsigned char c)
  258. {
  259. struct psmouse *parent = serio_get_drvdata(serio->parent);
  260. char rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
  261. if (psmouse_sliced_command(parent, c))
  262. return -1;
  263. if (ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE))
  264. return -1;
  265. return 0;
  266. }
  267. static int synaptics_pt_start(struct serio *serio)
  268. {
  269. struct psmouse *parent = serio_get_drvdata(serio->parent);
  270. struct synaptics_data *priv = parent->private;
  271. serio_pause_rx(parent->ps2dev.serio);
  272. priv->pt_port = serio;
  273. serio_continue_rx(parent->ps2dev.serio);
  274. return 0;
  275. }
  276. static void synaptics_pt_stop(struct serio *serio)
  277. {
  278. struct psmouse *parent = serio_get_drvdata(serio->parent);
  279. struct synaptics_data *priv = parent->private;
  280. serio_pause_rx(parent->ps2dev.serio);
  281. priv->pt_port = NULL;
  282. serio_continue_rx(parent->ps2dev.serio);
  283. }
  284. static int synaptics_is_pt_packet(unsigned char *buf)
  285. {
  286. return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
  287. }
  288. static void synaptics_pass_pt_packet(struct serio *ptport, unsigned char *packet)
  289. {
  290. struct psmouse *child = serio_get_drvdata(ptport);
  291. if (child && child->state == PSMOUSE_ACTIVATED) {
  292. serio_interrupt(ptport, packet[1], 0);
  293. serio_interrupt(ptport, packet[4], 0);
  294. serio_interrupt(ptport, packet[5], 0);
  295. if (child->pktsize == 4)
  296. serio_interrupt(ptport, packet[2], 0);
  297. } else
  298. serio_interrupt(ptport, packet[1], 0);
  299. }
  300. static void synaptics_pt_activate(struct psmouse *psmouse)
  301. {
  302. struct synaptics_data *priv = psmouse->private;
  303. struct psmouse *child = serio_get_drvdata(priv->pt_port);
  304. /* adjust the touchpad to child's choice of protocol */
  305. if (child) {
  306. if (child->pktsize == 4)
  307. priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
  308. else
  309. priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
  310. if (synaptics_mode_cmd(psmouse, priv->mode))
  311. printk(KERN_INFO "synaptics: failed to switch guest protocol\n");
  312. }
  313. }
  314. static void synaptics_pt_create(struct psmouse *psmouse)
  315. {
  316. struct serio *serio;
  317. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  318. if (!serio) {
  319. printk(KERN_ERR "synaptics: not enough memory to allocate pass-through port\n");
  320. return;
  321. }
  322. serio->id.type = SERIO_PS_PSTHRU;
  323. strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
  324. strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
  325. serio->write = synaptics_pt_write;
  326. serio->start = synaptics_pt_start;
  327. serio->stop = synaptics_pt_stop;
  328. serio->parent = psmouse->ps2dev.serio;
  329. psmouse->pt_activate = synaptics_pt_activate;
  330. printk(KERN_INFO "serio: %s port at %s\n", serio->name, psmouse->phys);
  331. serio_register_port(serio);
  332. }
  333. /*****************************************************************************
  334. * Functions to interpret the absolute mode packets
  335. ****************************************************************************/
  336. static int synaptics_parse_hw_state(const unsigned char buf[],
  337. struct synaptics_data *priv,
  338. struct synaptics_hw_state *hw)
  339. {
  340. memset(hw, 0, sizeof(struct synaptics_hw_state));
  341. if (SYN_MODEL_NEWABS(priv->model_id)) {
  342. hw->x = (((buf[3] & 0x10) << 8) |
  343. ((buf[1] & 0x0f) << 8) |
  344. buf[4]);
  345. hw->y = (((buf[3] & 0x20) << 7) |
  346. ((buf[1] & 0xf0) << 4) |
  347. buf[5]);
  348. hw->z = buf[2];
  349. hw->w = (((buf[0] & 0x30) >> 2) |
  350. ((buf[0] & 0x04) >> 1) |
  351. ((buf[3] & 0x04) >> 2));
  352. if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c) && hw->w == 2) {
  353. /* Gesture packet: (x, y, z) at half resolution */
  354. priv->mt.x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
  355. priv->mt.y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
  356. priv->mt.z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
  357. return 1;
  358. }
  359. hw->left = (buf[0] & 0x01) ? 1 : 0;
  360. hw->right = (buf[0] & 0x02) ? 1 : 0;
  361. if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  362. /*
  363. * Clickpad's button is transmitted as middle button,
  364. * however, since it is primary button, we will report
  365. * it as BTN_LEFT.
  366. */
  367. hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  368. } else if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  369. hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  370. if (hw->w == 2)
  371. hw->scroll = (signed char)(buf[1]);
  372. }
  373. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  374. hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
  375. hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
  376. }
  377. if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) &&
  378. ((buf[0] ^ buf[3]) & 0x02)) {
  379. switch (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) & ~0x01) {
  380. default:
  381. /*
  382. * if nExtBtn is greater than 8 it should be
  383. * considered invalid and treated as 0
  384. */
  385. break;
  386. case 8:
  387. hw->ext_buttons |= ((buf[5] & 0x08)) ? 0x80 : 0;
  388. hw->ext_buttons |= ((buf[4] & 0x08)) ? 0x40 : 0;
  389. case 6:
  390. hw->ext_buttons |= ((buf[5] & 0x04)) ? 0x20 : 0;
  391. hw->ext_buttons |= ((buf[4] & 0x04)) ? 0x10 : 0;
  392. case 4:
  393. hw->ext_buttons |= ((buf[5] & 0x02)) ? 0x08 : 0;
  394. hw->ext_buttons |= ((buf[4] & 0x02)) ? 0x04 : 0;
  395. case 2:
  396. hw->ext_buttons |= ((buf[5] & 0x01)) ? 0x02 : 0;
  397. hw->ext_buttons |= ((buf[4] & 0x01)) ? 0x01 : 0;
  398. }
  399. }
  400. } else {
  401. hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
  402. hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
  403. hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
  404. hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
  405. hw->left = (buf[0] & 0x01) ? 1 : 0;
  406. hw->right = (buf[0] & 0x02) ? 1 : 0;
  407. }
  408. return 0;
  409. }
  410. static void set_slot(struct input_dev *dev, int slot, bool active, int x, int y)
  411. {
  412. input_mt_slot(dev, slot);
  413. input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
  414. if (active) {
  415. input_report_abs(dev, ABS_MT_POSITION_X, x);
  416. input_report_abs(dev, ABS_MT_POSITION_Y,
  417. YMAX_NOMINAL + YMIN_NOMINAL - y);
  418. }
  419. }
  420. static void synaptics_report_semi_mt_data(struct input_dev *dev,
  421. const struct synaptics_hw_state *a,
  422. const struct synaptics_hw_state *b,
  423. int num_fingers)
  424. {
  425. if (num_fingers >= 2) {
  426. set_slot(dev, 0, true, min(a->x, b->x), min(a->y, b->y));
  427. set_slot(dev, 1, true, max(a->x, b->x), max(a->y, b->y));
  428. } else if (num_fingers == 1) {
  429. set_slot(dev, 0, true, a->x, a->y);
  430. set_slot(dev, 1, false, 0, 0);
  431. } else {
  432. set_slot(dev, 0, false, 0, 0);
  433. set_slot(dev, 1, false, 0, 0);
  434. }
  435. }
  436. /*
  437. * called for each full received packet from the touchpad
  438. */
  439. static void synaptics_process_packet(struct psmouse *psmouse)
  440. {
  441. struct input_dev *dev = psmouse->dev;
  442. struct synaptics_data *priv = psmouse->private;
  443. struct synaptics_hw_state hw;
  444. int num_fingers;
  445. int finger_width;
  446. int i;
  447. if (synaptics_parse_hw_state(psmouse->packet, priv, &hw))
  448. return;
  449. if (hw.scroll) {
  450. priv->scroll += hw.scroll;
  451. while (priv->scroll >= 4) {
  452. input_report_key(dev, BTN_BACK, !hw.down);
  453. input_sync(dev);
  454. input_report_key(dev, BTN_BACK, hw.down);
  455. input_sync(dev);
  456. priv->scroll -= 4;
  457. }
  458. while (priv->scroll <= -4) {
  459. input_report_key(dev, BTN_FORWARD, !hw.up);
  460. input_sync(dev);
  461. input_report_key(dev, BTN_FORWARD, hw.up);
  462. input_sync(dev);
  463. priv->scroll += 4;
  464. }
  465. return;
  466. }
  467. if (hw.z > 0 && hw.x > 1) {
  468. num_fingers = 1;
  469. finger_width = 5;
  470. if (SYN_CAP_EXTENDED(priv->capabilities)) {
  471. switch (hw.w) {
  472. case 0 ... 1:
  473. if (SYN_CAP_MULTIFINGER(priv->capabilities))
  474. num_fingers = hw.w + 2;
  475. break;
  476. case 2:
  477. if (SYN_MODEL_PEN(priv->model_id))
  478. ; /* Nothing, treat a pen as a single finger */
  479. break;
  480. case 4 ... 15:
  481. if (SYN_CAP_PALMDETECT(priv->capabilities))
  482. finger_width = hw.w;
  483. break;
  484. }
  485. }
  486. } else {
  487. num_fingers = 0;
  488. finger_width = 0;
  489. }
  490. if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c))
  491. synaptics_report_semi_mt_data(dev, &hw, &priv->mt, num_fingers);
  492. /* Post events
  493. * BTN_TOUCH has to be first as mousedev relies on it when doing
  494. * absolute -> relative conversion
  495. */
  496. if (hw.z > 30) input_report_key(dev, BTN_TOUCH, 1);
  497. if (hw.z < 25) input_report_key(dev, BTN_TOUCH, 0);
  498. if (num_fingers > 0) {
  499. input_report_abs(dev, ABS_X, hw.x);
  500. input_report_abs(dev, ABS_Y, YMAX_NOMINAL + YMIN_NOMINAL - hw.y);
  501. }
  502. input_report_abs(dev, ABS_PRESSURE, hw.z);
  503. if (SYN_CAP_PALMDETECT(priv->capabilities))
  504. input_report_abs(dev, ABS_TOOL_WIDTH, finger_width);
  505. input_report_key(dev, BTN_TOOL_FINGER, num_fingers == 1);
  506. input_report_key(dev, BTN_LEFT, hw.left);
  507. input_report_key(dev, BTN_RIGHT, hw.right);
  508. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  509. input_report_key(dev, BTN_TOOL_DOUBLETAP, num_fingers == 2);
  510. input_report_key(dev, BTN_TOOL_TRIPLETAP, num_fingers == 3);
  511. }
  512. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  513. input_report_key(dev, BTN_MIDDLE, hw.middle);
  514. if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
  515. input_report_key(dev, BTN_FORWARD, hw.up);
  516. input_report_key(dev, BTN_BACK, hw.down);
  517. }
  518. for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
  519. input_report_key(dev, BTN_0 + i, hw.ext_buttons & (1 << i));
  520. input_sync(dev);
  521. }
  522. static int synaptics_validate_byte(unsigned char packet[], int idx, unsigned char pkt_type)
  523. {
  524. static const unsigned char newabs_mask[] = { 0xC8, 0x00, 0x00, 0xC8, 0x00 };
  525. static const unsigned char newabs_rel_mask[] = { 0xC0, 0x00, 0x00, 0xC0, 0x00 };
  526. static const unsigned char newabs_rslt[] = { 0x80, 0x00, 0x00, 0xC0, 0x00 };
  527. static const unsigned char oldabs_mask[] = { 0xC0, 0x60, 0x00, 0xC0, 0x60 };
  528. static const unsigned char oldabs_rslt[] = { 0xC0, 0x00, 0x00, 0x80, 0x00 };
  529. if (idx < 0 || idx > 4)
  530. return 0;
  531. switch (pkt_type) {
  532. case SYN_NEWABS:
  533. case SYN_NEWABS_RELAXED:
  534. return (packet[idx] & newabs_rel_mask[idx]) == newabs_rslt[idx];
  535. case SYN_NEWABS_STRICT:
  536. return (packet[idx] & newabs_mask[idx]) == newabs_rslt[idx];
  537. case SYN_OLDABS:
  538. return (packet[idx] & oldabs_mask[idx]) == oldabs_rslt[idx];
  539. default:
  540. printk(KERN_ERR "synaptics: unknown packet type %d\n", pkt_type);
  541. return 0;
  542. }
  543. }
  544. static unsigned char synaptics_detect_pkt_type(struct psmouse *psmouse)
  545. {
  546. int i;
  547. for (i = 0; i < 5; i++)
  548. if (!synaptics_validate_byte(psmouse->packet, i, SYN_NEWABS_STRICT)) {
  549. printk(KERN_INFO "synaptics: using relaxed packet validation\n");
  550. return SYN_NEWABS_RELAXED;
  551. }
  552. return SYN_NEWABS_STRICT;
  553. }
  554. static psmouse_ret_t synaptics_process_byte(struct psmouse *psmouse)
  555. {
  556. struct synaptics_data *priv = psmouse->private;
  557. if (psmouse->pktcnt >= 6) { /* Full packet received */
  558. if (unlikely(priv->pkt_type == SYN_NEWABS))
  559. priv->pkt_type = synaptics_detect_pkt_type(psmouse);
  560. if (SYN_CAP_PASS_THROUGH(priv->capabilities) &&
  561. synaptics_is_pt_packet(psmouse->packet)) {
  562. if (priv->pt_port)
  563. synaptics_pass_pt_packet(priv->pt_port, psmouse->packet);
  564. } else
  565. synaptics_process_packet(psmouse);
  566. return PSMOUSE_FULL_PACKET;
  567. }
  568. return synaptics_validate_byte(psmouse->packet, psmouse->pktcnt - 1, priv->pkt_type) ?
  569. PSMOUSE_GOOD_DATA : PSMOUSE_BAD_DATA;
  570. }
  571. /*****************************************************************************
  572. * Driver initialization/cleanup functions
  573. ****************************************************************************/
  574. static void set_input_params(struct input_dev *dev, struct synaptics_data *priv)
  575. {
  576. int i;
  577. __set_bit(INPUT_PROP_POINTER, dev->propbit);
  578. __set_bit(EV_ABS, dev->evbit);
  579. input_set_abs_params(dev, ABS_X,
  580. XMIN_NOMINAL, priv->x_max ?: XMAX_NOMINAL, 0, 0);
  581. input_set_abs_params(dev, ABS_Y,
  582. YMIN_NOMINAL, priv->y_max ?: YMAX_NOMINAL, 0, 0);
  583. input_set_abs_params(dev, ABS_PRESSURE, 0, 255, 0, 0);
  584. if (SYN_CAP_ADV_GESTURE(priv->ext_cap_0c)) {
  585. __set_bit(INPUT_PROP_SEMI_MT, dev->propbit);
  586. input_mt_init_slots(dev, 2);
  587. input_set_abs_params(dev, ABS_MT_POSITION_X, XMIN_NOMINAL,
  588. priv->x_max ?: XMAX_NOMINAL, 0, 0);
  589. input_set_abs_params(dev, ABS_MT_POSITION_Y, YMIN_NOMINAL,
  590. priv->y_max ?: YMAX_NOMINAL, 0, 0);
  591. }
  592. if (SYN_CAP_PALMDETECT(priv->capabilities))
  593. input_set_abs_params(dev, ABS_TOOL_WIDTH, 0, 15, 0, 0);
  594. __set_bit(EV_KEY, dev->evbit);
  595. __set_bit(BTN_TOUCH, dev->keybit);
  596. __set_bit(BTN_TOOL_FINGER, dev->keybit);
  597. __set_bit(BTN_LEFT, dev->keybit);
  598. __set_bit(BTN_RIGHT, dev->keybit);
  599. if (SYN_CAP_MULTIFINGER(priv->capabilities)) {
  600. __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
  601. __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit);
  602. }
  603. if (SYN_CAP_MIDDLE_BUTTON(priv->capabilities))
  604. __set_bit(BTN_MIDDLE, dev->keybit);
  605. if (SYN_CAP_FOUR_BUTTON(priv->capabilities) ||
  606. SYN_CAP_MIDDLE_BUTTON(priv->capabilities)) {
  607. __set_bit(BTN_FORWARD, dev->keybit);
  608. __set_bit(BTN_BACK, dev->keybit);
  609. }
  610. for (i = 0; i < SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap); i++)
  611. __set_bit(BTN_0 + i, dev->keybit);
  612. __clear_bit(EV_REL, dev->evbit);
  613. __clear_bit(REL_X, dev->relbit);
  614. __clear_bit(REL_Y, dev->relbit);
  615. input_abs_set_res(dev, ABS_X, priv->x_res);
  616. input_abs_set_res(dev, ABS_Y, priv->y_res);
  617. if (SYN_CAP_CLICKPAD(priv->ext_cap_0c)) {
  618. __set_bit(INPUT_PROP_BUTTONPAD, dev->propbit);
  619. /* Clickpads report only left button */
  620. __clear_bit(BTN_RIGHT, dev->keybit);
  621. __clear_bit(BTN_MIDDLE, dev->keybit);
  622. }
  623. }
  624. static void synaptics_disconnect(struct psmouse *psmouse)
  625. {
  626. synaptics_reset(psmouse);
  627. kfree(psmouse->private);
  628. psmouse->private = NULL;
  629. }
  630. static int synaptics_reconnect(struct psmouse *psmouse)
  631. {
  632. struct synaptics_data *priv = psmouse->private;
  633. struct synaptics_data old_priv = *priv;
  634. int retry = 0;
  635. int error;
  636. do {
  637. psmouse_reset(psmouse);
  638. if (retry) {
  639. /*
  640. * On some boxes, right after resuming, the touchpad
  641. * needs some time to finish initializing (I assume
  642. * it needs time to calibrate) and start responding
  643. * to Synaptics-specific queries, so let's wait a
  644. * bit.
  645. */
  646. ssleep(1);
  647. }
  648. error = synaptics_detect(psmouse, 0);
  649. } while (error && ++retry < 3);
  650. if (error)
  651. return -1;
  652. if (retry > 1)
  653. printk(KERN_DEBUG "Synaptics reconnected after %d tries\n",
  654. retry);
  655. if (synaptics_query_hardware(psmouse)) {
  656. printk(KERN_ERR "Unable to query Synaptics hardware.\n");
  657. return -1;
  658. }
  659. if (synaptics_set_absolute_mode(psmouse)) {
  660. printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
  661. return -1;
  662. }
  663. if (synaptics_set_advanced_gesture_mode(psmouse)) {
  664. printk(KERN_ERR "Advanced gesture mode reconnect failed.\n");
  665. return -1;
  666. }
  667. if (old_priv.identity != priv->identity ||
  668. old_priv.model_id != priv->model_id ||
  669. old_priv.capabilities != priv->capabilities ||
  670. old_priv.ext_cap != priv->ext_cap) {
  671. printk(KERN_ERR "Synaptics hardware appears to be different: "
  672. "id(%ld-%ld), model(%ld-%ld), caps(%lx-%lx), ext(%lx-%lx).\n",
  673. old_priv.identity, priv->identity,
  674. old_priv.model_id, priv->model_id,
  675. old_priv.capabilities, priv->capabilities,
  676. old_priv.ext_cap, priv->ext_cap);
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. static bool impaired_toshiba_kbc;
  682. static const struct dmi_system_id __initconst toshiba_dmi_table[] = {
  683. #if defined(CONFIG_DMI) && defined(CONFIG_X86)
  684. {
  685. /* Toshiba Satellite */
  686. .matches = {
  687. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  688. DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
  689. },
  690. },
  691. {
  692. /* Toshiba Dynabook */
  693. .matches = {
  694. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  695. DMI_MATCH(DMI_PRODUCT_NAME, "dynabook"),
  696. },
  697. },
  698. {
  699. /* Toshiba Portege M300 */
  700. .matches = {
  701. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  702. DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE M300"),
  703. },
  704. },
  705. {
  706. /* Toshiba Portege M300 */
  707. .matches = {
  708. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  709. DMI_MATCH(DMI_PRODUCT_NAME, "Portable PC"),
  710. DMI_MATCH(DMI_PRODUCT_VERSION, "Version 1.0"),
  711. },
  712. },
  713. #endif
  714. { }
  715. };
  716. static bool broken_olpc_ec;
  717. static const struct dmi_system_id __initconst olpc_dmi_table[] = {
  718. #if defined(CONFIG_DMI) && defined(CONFIG_OLPC)
  719. {
  720. /* OLPC XO-1 or XO-1.5 */
  721. .matches = {
  722. DMI_MATCH(DMI_SYS_VENDOR, "OLPC"),
  723. DMI_MATCH(DMI_PRODUCT_NAME, "XO"),
  724. },
  725. },
  726. #endif
  727. { }
  728. };
  729. void __init synaptics_module_init(void)
  730. {
  731. impaired_toshiba_kbc = dmi_check_system(toshiba_dmi_table);
  732. broken_olpc_ec = dmi_check_system(olpc_dmi_table);
  733. }
  734. int synaptics_init(struct psmouse *psmouse)
  735. {
  736. struct synaptics_data *priv;
  737. /*
  738. * The OLPC XO has issues with Synaptics' absolute mode; similarly to
  739. * the HGPK, it quickly degrades and the hardware becomes jumpy and
  740. * overly sensitive. Not only that, but the constant packet spew
  741. * (even at a lowered 40pps rate) overloads the EC such that key
  742. * presses on the keyboard are missed. Given all of that, don't
  743. * even attempt to use Synaptics mode. Relative mode seems to work
  744. * just fine.
  745. */
  746. if (broken_olpc_ec) {
  747. printk(KERN_INFO "synaptics: OLPC XO detected, not enabling Synaptics protocol.\n");
  748. return -ENODEV;
  749. }
  750. psmouse->private = priv = kzalloc(sizeof(struct synaptics_data), GFP_KERNEL);
  751. if (!priv)
  752. return -ENOMEM;
  753. psmouse_reset(psmouse);
  754. if (synaptics_query_hardware(psmouse)) {
  755. printk(KERN_ERR "Unable to query Synaptics hardware.\n");
  756. goto init_fail;
  757. }
  758. if (synaptics_set_absolute_mode(psmouse)) {
  759. printk(KERN_ERR "Unable to initialize Synaptics hardware.\n");
  760. goto init_fail;
  761. }
  762. if (synaptics_set_advanced_gesture_mode(psmouse)) {
  763. printk(KERN_ERR "Advanced gesture mode init failed.\n");
  764. goto init_fail;
  765. }
  766. priv->pkt_type = SYN_MODEL_NEWABS(priv->model_id) ? SYN_NEWABS : SYN_OLDABS;
  767. printk(KERN_INFO "Synaptics Touchpad, model: %ld, fw: %ld.%ld, id: %#lx, caps: %#lx/%#lx/%#lx\n",
  768. SYN_ID_MODEL(priv->identity),
  769. SYN_ID_MAJOR(priv->identity), SYN_ID_MINOR(priv->identity),
  770. priv->model_id, priv->capabilities, priv->ext_cap, priv->ext_cap_0c);
  771. set_input_params(psmouse->dev, priv);
  772. /*
  773. * Encode touchpad model so that it can be used to set
  774. * input device->id.version and be visible to userspace.
  775. * Because version is __u16 we have to drop something.
  776. * Hardware info bits seem to be good candidates as they
  777. * are documented to be for Synaptics corp. internal use.
  778. */
  779. psmouse->model = ((priv->model_id & 0x00ff0000) >> 8) |
  780. (priv->model_id & 0x000000ff);
  781. psmouse->protocol_handler = synaptics_process_byte;
  782. psmouse->set_rate = synaptics_set_rate;
  783. psmouse->disconnect = synaptics_disconnect;
  784. psmouse->reconnect = synaptics_reconnect;
  785. psmouse->cleanup = synaptics_reset;
  786. psmouse->pktsize = 6;
  787. /* Synaptics can usually stay in sync without extra help */
  788. psmouse->resync_time = 0;
  789. if (SYN_CAP_PASS_THROUGH(priv->capabilities))
  790. synaptics_pt_create(psmouse);
  791. /*
  792. * Toshiba's KBC seems to have trouble handling data from
  793. * Synaptics at full rate. Switch to a lower rate (roughly
  794. * the same rate as a standard PS/2 mouse).
  795. */
  796. if (psmouse->rate >= 80 && impaired_toshiba_kbc) {
  797. printk(KERN_INFO "synaptics: Toshiba %s detected, limiting rate to 40pps.\n",
  798. dmi_get_system_info(DMI_PRODUCT_NAME));
  799. psmouse->rate = 40;
  800. }
  801. return 0;
  802. init_fail:
  803. kfree(priv);
  804. return -1;
  805. }
  806. bool synaptics_supported(void)
  807. {
  808. return true;
  809. }
  810. #else /* CONFIG_MOUSE_PS2_SYNAPTICS */
  811. void __init synaptics_module_init(void)
  812. {
  813. }
  814. int synaptics_init(struct psmouse *psmouse)
  815. {
  816. return -ENOSYS;
  817. }
  818. bool synaptics_supported(void)
  819. {
  820. return false;
  821. }
  822. #endif /* CONFIG_MOUSE_PS2_SYNAPTICS */