alps.c 22 KB

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