alps.c 22 KB

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