alps.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * ALPS touchpad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  5. * Copyright (c) 2003-2005 Peter Osterlund <petero2@telia.com>
  6. * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  7. * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2009 Sebastian Kapfer <sebastian_kapfer@gmx.net>
  9. *
  10. * ALPS detection, tap switching and status querying info is taken from
  11. * tpconfig utility (by C. Scott Ananian and Bruce Kall).
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2 as published by
  15. * the Free Software Foundation.
  16. */
  17. #include <linux/input.h>
  18. #include <linux/serio.h>
  19. #include <linux/libps2.h>
  20. #include "psmouse.h"
  21. #include "alps.h"
  22. #undef DEBUG
  23. #ifdef DEBUG
  24. #define dbg(format, arg...) printk(KERN_INFO "alps.c: " format "\n", ## arg)
  25. #else
  26. #define dbg(format, arg...) do {} while (0)
  27. #endif
  28. #define ALPS_OLDPROTO 0x01 /* old style input */
  29. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  30. #define ALPS_PASS 0x04 /* device has a pass-through port */
  31. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  32. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  33. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  34. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  35. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  36. 6-byte ALPS packet */
  37. static const struct alps_model_info alps_model_data[] = {
  38. { { 0x32, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
  39. { { 0x33, 0x02, 0x0a }, 0x88, 0xf8, ALPS_OLDPROTO }, /* UMAX-530T */
  40. { { 0x53, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  41. { { 0x53, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  42. { { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 }, /* HP ze1115 */
  43. { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  44. { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  45. { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
  46. { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
  47. { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
  48. { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
  49. { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
  50. { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
  51. { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  52. { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
  53. { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
  54. { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
  55. { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
  56. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  57. { { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
  58. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
  59. { { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
  60. };
  61. /*
  62. * XXX - this entry is suspicious. First byte has zero lower nibble,
  63. * which is what a normal mouse would report. Also, the value 0x0e
  64. * isn't valid per PS/2 spec.
  65. */
  66. /*
  67. * PS/2 packet format
  68. *
  69. * byte 0: 0 0 YSGN XSGN 1 M R L
  70. * byte 1: X7 X6 X5 X4 X3 X2 X1 X0
  71. * byte 2: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  72. *
  73. * Note that the device never signals overflow condition.
  74. *
  75. * ALPS absolute Mode - new format
  76. *
  77. * byte 0: 1 ? ? ? 1 ? ? ?
  78. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  79. * byte 2: 0 x10 x9 x8 x7 ? fin ges
  80. * byte 3: 0 y9 y8 y7 1 M R L
  81. * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
  82. * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
  83. *
  84. * Dualpoint device -- interleaved packet format
  85. *
  86. * byte 0: 1 1 0 0 1 1 1 1
  87. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  88. * byte 2: 0 x10 x9 x8 x7 0 fin ges
  89. * byte 3: 0 0 YSGN XSGN 1 1 1 1
  90. * byte 4: X7 X6 X5 X4 X3 X2 X1 X0
  91. * byte 5: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  92. * byte 6: 0 y9 y8 y7 1 m r l
  93. * byte 7: 0 y6 y5 y4 y3 y2 y1 y0
  94. * byte 8: 0 z6 z5 z4 z3 z2 z1 z0
  95. *
  96. * CAPITALS = stick, miniscules = touchpad
  97. *
  98. * ?'s can have different meanings on different models,
  99. * such as wheel rotation, extra buttons, stick buttons
  100. * on a dualpoint, etc.
  101. */
  102. static bool alps_is_valid_first_byte(const struct alps_model_info *model,
  103. unsigned char data)
  104. {
  105. return (data & model->mask0) == model->byte0;
  106. }
  107. static void alps_report_buttons(struct psmouse *psmouse,
  108. struct input_dev *dev1, struct input_dev *dev2,
  109. int left, int right, int middle)
  110. {
  111. struct alps_data *priv = psmouse->private;
  112. const struct alps_model_info *model = priv->i;
  113. if (model->flags & ALPS_PS2_INTERLEAVED) {
  114. struct input_dev *dev;
  115. /*
  116. * If shared button has already been reported on the
  117. * other device (dev2) then this event should be also
  118. * sent through that device.
  119. */
  120. dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
  121. input_report_key(dev, BTN_LEFT, left);
  122. dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
  123. input_report_key(dev, BTN_RIGHT, right);
  124. dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
  125. input_report_key(dev, BTN_MIDDLE, middle);
  126. /*
  127. * Sync the _other_ device now, we'll do the first
  128. * device later once we report the rest of the events.
  129. */
  130. input_sync(dev2);
  131. } else {
  132. /*
  133. * For devices with non-interleaved packets we know what
  134. * device buttons belong to so we can simply report them.
  135. */
  136. input_report_key(dev1, BTN_LEFT, left);
  137. input_report_key(dev1, BTN_RIGHT, right);
  138. input_report_key(dev1, BTN_MIDDLE, middle);
  139. }
  140. }
  141. static void alps_process_packet(struct psmouse *psmouse)
  142. {
  143. struct alps_data *priv = psmouse->private;
  144. const struct alps_model_info *model = priv->i;
  145. unsigned char *packet = psmouse->packet;
  146. struct input_dev *dev = psmouse->dev;
  147. struct input_dev *dev2 = priv->dev2;
  148. int x, y, z, ges, fin, left, right, middle;
  149. int back = 0, forward = 0;
  150. if (model->flags & ALPS_OLDPROTO) {
  151. left = packet[2] & 0x10;
  152. right = packet[2] & 0x08;
  153. middle = 0;
  154. x = packet[1] | ((packet[0] & 0x07) << 7);
  155. y = packet[4] | ((packet[3] & 0x07) << 7);
  156. z = packet[5];
  157. } else {
  158. left = packet[3] & 1;
  159. right = packet[3] & 2;
  160. middle = packet[3] & 4;
  161. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  162. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  163. z = packet[5];
  164. }
  165. if (model->flags & ALPS_FW_BK_1) {
  166. back = packet[0] & 0x10;
  167. forward = packet[2] & 4;
  168. }
  169. if (model->flags & ALPS_FW_BK_2) {
  170. back = packet[3] & 4;
  171. forward = packet[2] & 4;
  172. if ((middle = forward && back))
  173. forward = back = 0;
  174. }
  175. ges = packet[2] & 1;
  176. fin = packet[2] & 2;
  177. if ((model->flags & ALPS_DUALPOINT) && z == 127) {
  178. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  179. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  180. alps_report_buttons(psmouse, dev2, dev, left, right, middle);
  181. input_sync(dev2);
  182. return;
  183. }
  184. alps_report_buttons(psmouse, dev, dev2, left, right, middle);
  185. /* Convert hardware tap to a reasonable Z value */
  186. if (ges && !fin)
  187. z = 40;
  188. /*
  189. * A "tap and drag" operation is reported by the hardware as a transition
  190. * from (!fin && ges) to (fin && ges). This should be translated to the
  191. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  192. */
  193. if (ges && fin && !priv->prev_fin) {
  194. input_report_abs(dev, ABS_X, x);
  195. input_report_abs(dev, ABS_Y, y);
  196. input_report_abs(dev, ABS_PRESSURE, 0);
  197. input_report_key(dev, BTN_TOOL_FINGER, 0);
  198. input_sync(dev);
  199. }
  200. priv->prev_fin = fin;
  201. if (z > 30)
  202. input_report_key(dev, BTN_TOUCH, 1);
  203. if (z < 25)
  204. input_report_key(dev, BTN_TOUCH, 0);
  205. if (z > 0) {
  206. input_report_abs(dev, ABS_X, x);
  207. input_report_abs(dev, ABS_Y, y);
  208. }
  209. input_report_abs(dev, ABS_PRESSURE, z);
  210. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  211. if (model->flags & ALPS_WHEEL)
  212. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  213. if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  214. input_report_key(dev, BTN_FORWARD, forward);
  215. input_report_key(dev, BTN_BACK, back);
  216. }
  217. if (model->flags & ALPS_FOUR_BUTTONS) {
  218. input_report_key(dev, BTN_0, packet[2] & 4);
  219. input_report_key(dev, BTN_1, packet[0] & 0x10);
  220. input_report_key(dev, BTN_2, packet[3] & 4);
  221. input_report_key(dev, BTN_3, packet[0] & 0x20);
  222. }
  223. input_sync(dev);
  224. }
  225. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  226. unsigned char packet[],
  227. bool report_buttons)
  228. {
  229. struct alps_data *priv = psmouse->private;
  230. struct input_dev *dev2 = priv->dev2;
  231. if (report_buttons)
  232. alps_report_buttons(psmouse, dev2, psmouse->dev,
  233. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  234. input_report_rel(dev2, REL_X,
  235. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  236. input_report_rel(dev2, REL_Y,
  237. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  238. input_sync(dev2);
  239. }
  240. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  241. {
  242. struct alps_data *priv = psmouse->private;
  243. if (psmouse->pktcnt < 6)
  244. return PSMOUSE_GOOD_DATA;
  245. if (psmouse->pktcnt == 6) {
  246. /*
  247. * Start a timer to flush the packet if it ends up last
  248. * 6-byte packet in the stream. Timer needs to fire
  249. * psmouse core times out itself. 20 ms should be enough
  250. * to decide if we are getting more data or not.
  251. */
  252. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  253. return PSMOUSE_GOOD_DATA;
  254. }
  255. del_timer(&priv->timer);
  256. if (psmouse->packet[6] & 0x80) {
  257. /*
  258. * Highest bit is set - that means we either had
  259. * complete ALPS packet and this is start of the
  260. * next packet or we got garbage.
  261. */
  262. if (((psmouse->packet[3] |
  263. psmouse->packet[4] |
  264. psmouse->packet[5]) & 0x80) ||
  265. (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
  266. dbg("refusing packet %x %x %x %x "
  267. "(suspected interleaved ps/2)\n",
  268. psmouse->packet[3], psmouse->packet[4],
  269. psmouse->packet[5], psmouse->packet[6]);
  270. return PSMOUSE_BAD_DATA;
  271. }
  272. alps_process_packet(psmouse);
  273. /* Continue with the next packet */
  274. psmouse->packet[0] = psmouse->packet[6];
  275. psmouse->pktcnt = 1;
  276. } else {
  277. /*
  278. * High bit is 0 - that means that we indeed got a PS/2
  279. * packet in the middle of ALPS packet.
  280. *
  281. * There is also possibility that we got 6-byte ALPS
  282. * packet followed by 3-byte packet from trackpoint. We
  283. * can not distinguish between these 2 scenarios but
  284. * becase the latter is unlikely to happen in course of
  285. * normal operation (user would need to press all
  286. * buttons on the pad and start moving trackpoint
  287. * without touching the pad surface) we assume former.
  288. * Even if we are wrong the wost thing that would happen
  289. * the cursor would jump but we should not get protocol
  290. * desynchronization.
  291. */
  292. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  293. false);
  294. /*
  295. * Continue with the standard ALPS protocol handling,
  296. * but make sure we won't process it as an interleaved
  297. * packet again, which may happen if all buttons are
  298. * pressed. To avoid this let's reset the 4th bit which
  299. * is normally 1.
  300. */
  301. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  302. psmouse->pktcnt = 4;
  303. }
  304. return PSMOUSE_GOOD_DATA;
  305. }
  306. static void alps_flush_packet(unsigned long data)
  307. {
  308. struct psmouse *psmouse = (struct psmouse *)data;
  309. serio_pause_rx(psmouse->ps2dev.serio);
  310. if (psmouse->pktcnt == 6) {
  311. /*
  312. * We did not any more data in reasonable amount of time.
  313. * Validate the last 3 bytes and process as a standard
  314. * ALPS packet.
  315. */
  316. if ((psmouse->packet[3] |
  317. psmouse->packet[4] |
  318. psmouse->packet[5]) & 0x80) {
  319. dbg("refusing packet %x %x %x "
  320. "(suspected interleaved ps/2)\n",
  321. psmouse->packet[3], psmouse->packet[4],
  322. psmouse->packet[5]);
  323. } else {
  324. alps_process_packet(psmouse);
  325. }
  326. psmouse->pktcnt = 0;
  327. }
  328. serio_continue_rx(psmouse->ps2dev.serio);
  329. }
  330. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  331. {
  332. struct alps_data *priv = psmouse->private;
  333. const struct alps_model_info *model = priv->i;
  334. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  335. if (psmouse->pktcnt == 3) {
  336. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  337. true);
  338. return PSMOUSE_FULL_PACKET;
  339. }
  340. return PSMOUSE_GOOD_DATA;
  341. }
  342. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  343. if ((model->flags & ALPS_PS2_INTERLEAVED) &&
  344. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  345. return alps_handle_interleaved_ps2(psmouse);
  346. }
  347. if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
  348. dbg("refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  349. psmouse->packet[0], model->mask0, model->byte0);
  350. return PSMOUSE_BAD_DATA;
  351. }
  352. /* Bytes 2 - 6 should have 0 in the highest bit */
  353. if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
  354. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  355. dbg("refusing packet[%i] = %x\n",
  356. psmouse->pktcnt - 1, psmouse->packet[psmouse->pktcnt - 1]);
  357. return PSMOUSE_BAD_DATA;
  358. }
  359. if (psmouse->pktcnt == 6) {
  360. alps_process_packet(psmouse);
  361. return PSMOUSE_FULL_PACKET;
  362. }
  363. return PSMOUSE_GOOD_DATA;
  364. }
  365. static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
  366. {
  367. struct ps2dev *ps2dev = &psmouse->ps2dev;
  368. static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
  369. unsigned char param[4];
  370. int i;
  371. /*
  372. * First try "E6 report".
  373. * ALPS should return 0,0,10 or 0,0,100
  374. */
  375. param[0] = 0;
  376. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  377. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  378. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  379. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
  380. return NULL;
  381. param[0] = param[1] = param[2] = 0xff;
  382. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  383. return NULL;
  384. dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  385. if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
  386. return NULL;
  387. /*
  388. * Now try "E7 report". Allowed responses are in
  389. * alps_model_data[].signature
  390. */
  391. param[0] = 0;
  392. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  393. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  394. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  395. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
  396. return NULL;
  397. param[0] = param[1] = param[2] = 0xff;
  398. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  399. return NULL;
  400. dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  401. if (version) {
  402. for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
  403. /* empty */;
  404. *version = (param[0] << 8) | (param[1] << 4) | i;
  405. }
  406. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
  407. if (!memcmp(param, alps_model_data[i].signature,
  408. sizeof(alps_model_data[i].signature)))
  409. return alps_model_data + i;
  410. return NULL;
  411. }
  412. /*
  413. * For DualPoint devices select the device that should respond to
  414. * subsequent commands. It looks like glidepad is behind stickpointer,
  415. * I'd thought it would be other way around...
  416. */
  417. static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
  418. {
  419. struct ps2dev *ps2dev = &psmouse->ps2dev;
  420. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  421. if (ps2_command(ps2dev, NULL, cmd) ||
  422. ps2_command(ps2dev, NULL, cmd) ||
  423. ps2_command(ps2dev, NULL, cmd) ||
  424. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  425. return -1;
  426. /* we may get 3 more bytes, just ignore them */
  427. ps2_drain(ps2dev, 3, 100);
  428. return 0;
  429. }
  430. static int alps_absolute_mode(struct psmouse *psmouse)
  431. {
  432. struct ps2dev *ps2dev = &psmouse->ps2dev;
  433. /* Try ALPS magic knock - 4 disable before enable */
  434. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  435. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  436. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  437. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  438. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  439. return -1;
  440. /*
  441. * Switch mouse to poll (remote) mode so motion data will not
  442. * get in our way
  443. */
  444. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  445. }
  446. static int alps_get_status(struct psmouse *psmouse, char *param)
  447. {
  448. struct ps2dev *ps2dev = &psmouse->ps2dev;
  449. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  450. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  451. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  452. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  453. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  454. return -1;
  455. dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  456. return 0;
  457. }
  458. /*
  459. * Turn touchpad tapping on or off. The sequences are:
  460. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  461. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  462. * My guess that 0xE9 (GetInfo) is here as a sync point.
  463. * For models that also have stickpointer (DualPoints) its tapping
  464. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  465. * we don't fiddle with it.
  466. */
  467. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  468. {
  469. struct ps2dev *ps2dev = &psmouse->ps2dev;
  470. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  471. unsigned char tap_arg = enable ? 0x0A : 0x00;
  472. unsigned char param[4];
  473. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  474. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  475. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  476. ps2_command(ps2dev, &tap_arg, cmd))
  477. return -1;
  478. if (alps_get_status(psmouse, param))
  479. return -1;
  480. return 0;
  481. }
  482. /*
  483. * alps_poll() - poll the touchpad for current motion packet.
  484. * Used in resync.
  485. */
  486. static int alps_poll(struct psmouse *psmouse)
  487. {
  488. struct alps_data *priv = psmouse->private;
  489. unsigned char buf[6];
  490. bool poll_failed;
  491. if (priv->i->flags & ALPS_PASS)
  492. alps_passthrough_mode(psmouse, true);
  493. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  494. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  495. if (priv->i->flags & ALPS_PASS)
  496. alps_passthrough_mode(psmouse, false);
  497. if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
  498. return -1;
  499. if ((psmouse->badbyte & 0xc8) == 0x08) {
  500. /*
  501. * Poll the track stick ...
  502. */
  503. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  504. return -1;
  505. }
  506. memcpy(psmouse->packet, buf, sizeof(buf));
  507. return 0;
  508. }
  509. static int alps_hw_init(struct psmouse *psmouse)
  510. {
  511. struct alps_data *priv = psmouse->private;
  512. const struct alps_model_info *model = priv->i;
  513. if ((model->flags & ALPS_PASS) &&
  514. alps_passthrough_mode(psmouse, true)) {
  515. return -1;
  516. }
  517. if (alps_tap_mode(psmouse, true)) {
  518. printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
  519. return -1;
  520. }
  521. if (alps_absolute_mode(psmouse)) {
  522. printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
  523. return -1;
  524. }
  525. if ((model->flags & ALPS_PASS) &&
  526. alps_passthrough_mode(psmouse, false)) {
  527. return -1;
  528. }
  529. /* ALPS needs stream mode, otherwise it won't report any data */
  530. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  531. printk(KERN_ERR "alps.c: Failed to enable stream mode\n");
  532. return -1;
  533. }
  534. return 0;
  535. }
  536. static int alps_reconnect(struct psmouse *psmouse)
  537. {
  538. const struct alps_model_info *model;
  539. psmouse_reset(psmouse);
  540. model = alps_get_model(psmouse, NULL);
  541. if (!model)
  542. return -1;
  543. return alps_hw_init(psmouse);
  544. }
  545. static void alps_disconnect(struct psmouse *psmouse)
  546. {
  547. struct alps_data *priv = psmouse->private;
  548. psmouse_reset(psmouse);
  549. del_timer_sync(&priv->timer);
  550. input_unregister_device(priv->dev2);
  551. kfree(priv);
  552. }
  553. int alps_init(struct psmouse *psmouse)
  554. {
  555. struct alps_data *priv;
  556. const struct alps_model_info *model;
  557. struct input_dev *dev1 = psmouse->dev, *dev2;
  558. int version;
  559. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  560. dev2 = input_allocate_device();
  561. if (!priv || !dev2)
  562. goto init_fail;
  563. priv->dev2 = dev2;
  564. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  565. psmouse->private = priv;
  566. model = alps_get_model(psmouse, &version);
  567. if (!model)
  568. goto init_fail;
  569. priv->i = model;
  570. if (alps_hw_init(psmouse))
  571. goto init_fail;
  572. /*
  573. * Undo part of setup done for us by psmouse core since touchpad
  574. * is not a relative device.
  575. */
  576. __clear_bit(EV_REL, dev1->evbit);
  577. __clear_bit(REL_X, dev1->relbit);
  578. __clear_bit(REL_Y, dev1->relbit);
  579. /*
  580. * Now set up our capabilities.
  581. */
  582. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  583. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  584. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  585. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  586. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  587. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  588. input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
  589. input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
  590. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  591. if (model->flags & ALPS_WHEEL) {
  592. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  593. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  594. }
  595. if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  596. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  597. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  598. }
  599. if (model->flags & ALPS_FOUR_BUTTONS) {
  600. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  601. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  602. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  603. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  604. } else {
  605. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  606. }
  607. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  608. dev2->phys = priv->phys;
  609. dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
  610. dev2->id.bustype = BUS_I8042;
  611. dev2->id.vendor = 0x0002;
  612. dev2->id.product = PSMOUSE_ALPS;
  613. dev2->id.version = 0x0000;
  614. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  615. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  616. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  617. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  618. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  619. if (input_register_device(priv->dev2))
  620. goto init_fail;
  621. psmouse->protocol_handler = alps_process_byte;
  622. psmouse->poll = alps_poll;
  623. psmouse->disconnect = alps_disconnect;
  624. psmouse->reconnect = alps_reconnect;
  625. psmouse->pktsize = 6;
  626. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  627. psmouse->resync_time = 0;
  628. return 0;
  629. init_fail:
  630. psmouse_reset(psmouse);
  631. input_free_device(dev2);
  632. kfree(priv);
  633. psmouse->private = NULL;
  634. return -1;
  635. }
  636. int alps_detect(struct psmouse *psmouse, bool set_properties)
  637. {
  638. int version;
  639. const struct alps_model_info *model;
  640. model = alps_get_model(psmouse, &version);
  641. if (!model)
  642. return -1;
  643. if (set_properties) {
  644. psmouse->vendor = "ALPS";
  645. psmouse->name = model->flags & ALPS_DUALPOINT ?
  646. "DualPoint TouchPad" : "GlidePoint";
  647. psmouse->model = version;
  648. }
  649. return 0;
  650. }