alps.c 22 KB

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