alps.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #define ALPS_OLDPROTO 0x01 /* old style input */
  24. #define ALPS_DUALPOINT 0x02 /* touchpad has trackstick */
  25. #define ALPS_PASS 0x04 /* device has a pass-through port */
  26. #define ALPS_WHEEL 0x08 /* hardware wheel present */
  27. #define ALPS_FW_BK_1 0x10 /* front & back buttons present */
  28. #define ALPS_FW_BK_2 0x20 /* front & back buttons present */
  29. #define ALPS_FOUR_BUTTONS 0x40 /* 4 direction button present */
  30. #define ALPS_PS2_INTERLEAVED 0x80 /* 3-byte PS/2 packet interleaved with
  31. 6-byte ALPS packet */
  32. static const struct alps_model_info alps_model_data[] = {
  33. { { 0x32, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Toshiba Salellite Pro M10 */
  34. { { 0x33, 0x02, 0x0a }, 0x88, 0xf8, ALPS_OLDPROTO }, /* UMAX-530T */
  35. { { 0x53, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  36. { { 0x53, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  37. { { 0x60, 0x03, 0xc8 }, 0xf8, 0xf8, 0 }, /* HP ze1115 */
  38. { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  39. { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  40. { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Fujitsu Siemens S6010 */
  41. { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
  42. { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
  43. { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
  44. { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D800 */
  45. { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
  46. { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  47. { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
  48. { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
  49. { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
  50. { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
  51. /* Dell Latitude E5500, E6400, E6500, Precision M4400 */
  52. { { 0x62, 0x02, 0x14 }, 0xcf, 0xcf,
  53. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED },
  54. { { 0x73, 0x02, 0x50 }, 0xcf, 0xcf, ALPS_FOUR_BUTTONS }, /* Dell Vostro 1400 */
  55. { { 0x52, 0x01, 0x14 }, 0xff, 0xff,
  56. ALPS_PASS | ALPS_DUALPOINT | ALPS_PS2_INTERLEAVED }, /* Toshiba Tecra A11-11L */
  57. };
  58. /*
  59. * XXX - this entry is suspicious. First byte has zero lower nibble,
  60. * which is what a normal mouse would report. Also, the value 0x0e
  61. * isn't valid per PS/2 spec.
  62. */
  63. /*
  64. * PS/2 packet format
  65. *
  66. * byte 0: 0 0 YSGN XSGN 1 M R L
  67. * byte 1: X7 X6 X5 X4 X3 X2 X1 X0
  68. * byte 2: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  69. *
  70. * Note that the device never signals overflow condition.
  71. *
  72. * ALPS absolute Mode - new format
  73. *
  74. * byte 0: 1 ? ? ? 1 ? ? ?
  75. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  76. * byte 2: 0 x10 x9 x8 x7 ? fin ges
  77. * byte 3: 0 y9 y8 y7 1 M R L
  78. * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
  79. * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
  80. *
  81. * Dualpoint device -- interleaved packet format
  82. *
  83. * byte 0: 1 1 0 0 1 1 1 1
  84. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  85. * byte 2: 0 x10 x9 x8 x7 0 fin ges
  86. * byte 3: 0 0 YSGN XSGN 1 1 1 1
  87. * byte 4: X7 X6 X5 X4 X3 X2 X1 X0
  88. * byte 5: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
  89. * byte 6: 0 y9 y8 y7 1 m r l
  90. * byte 7: 0 y6 y5 y4 y3 y2 y1 y0
  91. * byte 8: 0 z6 z5 z4 z3 z2 z1 z0
  92. *
  93. * CAPITALS = stick, miniscules = touchpad
  94. *
  95. * ?'s can have different meanings on different models,
  96. * such as wheel rotation, extra buttons, stick buttons
  97. * on a dualpoint, etc.
  98. */
  99. static bool alps_is_valid_first_byte(const struct alps_model_info *model,
  100. unsigned char data)
  101. {
  102. return (data & model->mask0) == model->byte0;
  103. }
  104. static void alps_report_buttons(struct psmouse *psmouse,
  105. struct input_dev *dev1, struct input_dev *dev2,
  106. int left, int right, int middle)
  107. {
  108. struct input_dev *dev;
  109. /*
  110. * If shared button has already been reported on the
  111. * other device (dev2) then this event should be also
  112. * sent through that device.
  113. */
  114. dev = test_bit(BTN_LEFT, dev2->key) ? dev2 : dev1;
  115. input_report_key(dev, BTN_LEFT, left);
  116. dev = test_bit(BTN_RIGHT, dev2->key) ? dev2 : dev1;
  117. input_report_key(dev, BTN_RIGHT, right);
  118. dev = test_bit(BTN_MIDDLE, dev2->key) ? dev2 : dev1;
  119. input_report_key(dev, BTN_MIDDLE, middle);
  120. /*
  121. * Sync the _other_ device now, we'll do the first
  122. * device later once we report the rest of the events.
  123. */
  124. input_sync(dev2);
  125. }
  126. static void alps_process_packet(struct psmouse *psmouse)
  127. {
  128. struct alps_data *priv = psmouse->private;
  129. const struct alps_model_info *model = priv->i;
  130. unsigned char *packet = psmouse->packet;
  131. struct input_dev *dev = psmouse->dev;
  132. struct input_dev *dev2 = priv->dev2;
  133. int x, y, z, ges, fin, left, right, middle;
  134. int back = 0, forward = 0;
  135. if (model->flags & ALPS_OLDPROTO) {
  136. left = packet[2] & 0x10;
  137. right = packet[2] & 0x08;
  138. middle = 0;
  139. x = packet[1] | ((packet[0] & 0x07) << 7);
  140. y = packet[4] | ((packet[3] & 0x07) << 7);
  141. z = packet[5];
  142. } else {
  143. left = packet[3] & 1;
  144. right = packet[3] & 2;
  145. middle = packet[3] & 4;
  146. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  147. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  148. z = packet[5];
  149. }
  150. if (model->flags & ALPS_FW_BK_1) {
  151. back = packet[0] & 0x10;
  152. forward = packet[2] & 4;
  153. }
  154. if (model->flags & ALPS_FW_BK_2) {
  155. back = packet[3] & 4;
  156. forward = packet[2] & 4;
  157. if ((middle = forward && back))
  158. forward = back = 0;
  159. }
  160. ges = packet[2] & 1;
  161. fin = packet[2] & 2;
  162. if ((model->flags & ALPS_DUALPOINT) && z == 127) {
  163. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  164. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  165. alps_report_buttons(psmouse, dev2, dev, left, right, middle);
  166. input_sync(dev2);
  167. return;
  168. }
  169. alps_report_buttons(psmouse, dev, dev2, left, right, middle);
  170. /* Convert hardware tap to a reasonable Z value */
  171. if (ges && !fin)
  172. z = 40;
  173. /*
  174. * A "tap and drag" operation is reported by the hardware as a transition
  175. * from (!fin && ges) to (fin && ges). This should be translated to the
  176. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  177. */
  178. if (ges && fin && !priv->prev_fin) {
  179. input_report_abs(dev, ABS_X, x);
  180. input_report_abs(dev, ABS_Y, y);
  181. input_report_abs(dev, ABS_PRESSURE, 0);
  182. input_report_key(dev, BTN_TOOL_FINGER, 0);
  183. input_sync(dev);
  184. }
  185. priv->prev_fin = fin;
  186. if (z > 30)
  187. input_report_key(dev, BTN_TOUCH, 1);
  188. if (z < 25)
  189. input_report_key(dev, BTN_TOUCH, 0);
  190. if (z > 0) {
  191. input_report_abs(dev, ABS_X, x);
  192. input_report_abs(dev, ABS_Y, y);
  193. }
  194. input_report_abs(dev, ABS_PRESSURE, z);
  195. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  196. if (model->flags & ALPS_WHEEL)
  197. input_report_rel(dev, REL_WHEEL, ((packet[2] << 1) & 0x08) - ((packet[0] >> 4) & 0x07));
  198. if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  199. input_report_key(dev, BTN_FORWARD, forward);
  200. input_report_key(dev, BTN_BACK, back);
  201. }
  202. if (model->flags & ALPS_FOUR_BUTTONS) {
  203. input_report_key(dev, BTN_0, packet[2] & 4);
  204. input_report_key(dev, BTN_1, packet[0] & 0x10);
  205. input_report_key(dev, BTN_2, packet[3] & 4);
  206. input_report_key(dev, BTN_3, packet[0] & 0x20);
  207. }
  208. input_sync(dev);
  209. }
  210. static void alps_report_bare_ps2_packet(struct psmouse *psmouse,
  211. unsigned char packet[],
  212. bool report_buttons)
  213. {
  214. struct alps_data *priv = psmouse->private;
  215. struct input_dev *dev2 = priv->dev2;
  216. if (report_buttons)
  217. alps_report_buttons(psmouse, dev2, psmouse->dev,
  218. packet[0] & 1, packet[0] & 2, packet[0] & 4);
  219. input_report_rel(dev2, REL_X,
  220. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  221. input_report_rel(dev2, REL_Y,
  222. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  223. input_sync(dev2);
  224. }
  225. static psmouse_ret_t alps_handle_interleaved_ps2(struct psmouse *psmouse)
  226. {
  227. struct alps_data *priv = psmouse->private;
  228. if (psmouse->pktcnt < 6)
  229. return PSMOUSE_GOOD_DATA;
  230. if (psmouse->pktcnt == 6) {
  231. /*
  232. * Start a timer to flush the packet if it ends up last
  233. * 6-byte packet in the stream. Timer needs to fire
  234. * psmouse core times out itself. 20 ms should be enough
  235. * to decide if we are getting more data or not.
  236. */
  237. mod_timer(&priv->timer, jiffies + msecs_to_jiffies(20));
  238. return PSMOUSE_GOOD_DATA;
  239. }
  240. del_timer(&priv->timer);
  241. if (psmouse->packet[6] & 0x80) {
  242. /*
  243. * Highest bit is set - that means we either had
  244. * complete ALPS packet and this is start of the
  245. * next packet or we got garbage.
  246. */
  247. if (((psmouse->packet[3] |
  248. psmouse->packet[4] |
  249. psmouse->packet[5]) & 0x80) ||
  250. (!alps_is_valid_first_byte(priv->i, psmouse->packet[6]))) {
  251. psmouse_dbg(psmouse,
  252. "refusing packet %x %x %x %x (suspected interleaved ps/2)\n",
  253. psmouse->packet[3], psmouse->packet[4],
  254. psmouse->packet[5], psmouse->packet[6]);
  255. return PSMOUSE_BAD_DATA;
  256. }
  257. alps_process_packet(psmouse);
  258. /* Continue with the next packet */
  259. psmouse->packet[0] = psmouse->packet[6];
  260. psmouse->pktcnt = 1;
  261. } else {
  262. /*
  263. * High bit is 0 - that means that we indeed got a PS/2
  264. * packet in the middle of ALPS packet.
  265. *
  266. * There is also possibility that we got 6-byte ALPS
  267. * packet followed by 3-byte packet from trackpoint. We
  268. * can not distinguish between these 2 scenarios but
  269. * because the latter is unlikely to happen in course of
  270. * normal operation (user would need to press all
  271. * buttons on the pad and start moving trackpoint
  272. * without touching the pad surface) we assume former.
  273. * Even if we are wrong the wost thing that would happen
  274. * the cursor would jump but we should not get protocol
  275. * de-synchronization.
  276. */
  277. alps_report_bare_ps2_packet(psmouse, &psmouse->packet[3],
  278. false);
  279. /*
  280. * Continue with the standard ALPS protocol handling,
  281. * but make sure we won't process it as an interleaved
  282. * packet again, which may happen if all buttons are
  283. * pressed. To avoid this let's reset the 4th bit which
  284. * is normally 1.
  285. */
  286. psmouse->packet[3] = psmouse->packet[6] & 0xf7;
  287. psmouse->pktcnt = 4;
  288. }
  289. return PSMOUSE_GOOD_DATA;
  290. }
  291. static void alps_flush_packet(unsigned long data)
  292. {
  293. struct psmouse *psmouse = (struct psmouse *)data;
  294. serio_pause_rx(psmouse->ps2dev.serio);
  295. if (psmouse->pktcnt == 6) {
  296. /*
  297. * We did not any more data in reasonable amount of time.
  298. * Validate the last 3 bytes and process as a standard
  299. * ALPS packet.
  300. */
  301. if ((psmouse->packet[3] |
  302. psmouse->packet[4] |
  303. psmouse->packet[5]) & 0x80) {
  304. psmouse_dbg(psmouse,
  305. "refusing packet %x %x %x (suspected interleaved ps/2)\n",
  306. psmouse->packet[3], psmouse->packet[4],
  307. psmouse->packet[5]);
  308. } else {
  309. alps_process_packet(psmouse);
  310. }
  311. psmouse->pktcnt = 0;
  312. }
  313. serio_continue_rx(psmouse->ps2dev.serio);
  314. }
  315. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse)
  316. {
  317. struct alps_data *priv = psmouse->private;
  318. const struct alps_model_info *model = priv->i;
  319. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  320. if (psmouse->pktcnt == 3) {
  321. alps_report_bare_ps2_packet(psmouse, psmouse->packet,
  322. true);
  323. return PSMOUSE_FULL_PACKET;
  324. }
  325. return PSMOUSE_GOOD_DATA;
  326. }
  327. /* Check for PS/2 packet stuffed in the middle of ALPS packet. */
  328. if ((model->flags & ALPS_PS2_INTERLEAVED) &&
  329. psmouse->pktcnt >= 4 && (psmouse->packet[3] & 0x0f) == 0x0f) {
  330. return alps_handle_interleaved_ps2(psmouse);
  331. }
  332. if (!alps_is_valid_first_byte(model, psmouse->packet[0])) {
  333. psmouse_dbg(psmouse,
  334. "refusing packet[0] = %x (mask0 = %x, byte0 = %x)\n",
  335. psmouse->packet[0], model->mask0, model->byte0);
  336. return PSMOUSE_BAD_DATA;
  337. }
  338. /* Bytes 2 - 6 should have 0 in the highest bit */
  339. if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
  340. (psmouse->packet[psmouse->pktcnt - 1] & 0x80)) {
  341. psmouse_dbg(psmouse, "refusing packet[%i] = %x\n",
  342. psmouse->pktcnt - 1,
  343. psmouse->packet[psmouse->pktcnt - 1]);
  344. return PSMOUSE_BAD_DATA;
  345. }
  346. if (psmouse->pktcnt == 6) {
  347. alps_process_packet(psmouse);
  348. return PSMOUSE_FULL_PACKET;
  349. }
  350. return PSMOUSE_GOOD_DATA;
  351. }
  352. static const struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
  353. {
  354. struct ps2dev *ps2dev = &psmouse->ps2dev;
  355. static const unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
  356. unsigned char param[4];
  357. int i;
  358. /*
  359. * First try "E6 report".
  360. * ALPS should return 0,0,10 or 0,0,100
  361. */
  362. param[0] = 0;
  363. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  364. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  365. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  366. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
  367. return NULL;
  368. param[0] = param[1] = param[2] = 0xff;
  369. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  370. return NULL;
  371. psmouse_dbg(psmouse, "E6 report: %2.2x %2.2x %2.2x",
  372. param[0], param[1], param[2]);
  373. if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
  374. return NULL;
  375. /*
  376. * Now try "E7 report". Allowed responses are in
  377. * alps_model_data[].signature
  378. */
  379. param[0] = 0;
  380. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  381. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  382. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  383. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
  384. return NULL;
  385. param[0] = param[1] = param[2] = 0xff;
  386. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  387. return NULL;
  388. psmouse_dbg(psmouse, "E7 report: %2.2x %2.2x %2.2x",
  389. param[0], param[1], param[2]);
  390. if (version) {
  391. for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
  392. /* empty */;
  393. *version = (param[0] << 8) | (param[1] << 4) | i;
  394. }
  395. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
  396. if (!memcmp(param, alps_model_data[i].signature,
  397. sizeof(alps_model_data[i].signature)))
  398. return alps_model_data + i;
  399. return NULL;
  400. }
  401. /*
  402. * For DualPoint devices select the device that should respond to
  403. * subsequent commands. It looks like glidepad is behind stickpointer,
  404. * I'd thought it would be other way around...
  405. */
  406. static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
  407. {
  408. struct ps2dev *ps2dev = &psmouse->ps2dev;
  409. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  410. if (ps2_command(ps2dev, NULL, cmd) ||
  411. ps2_command(ps2dev, NULL, cmd) ||
  412. ps2_command(ps2dev, NULL, cmd) ||
  413. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  414. return -1;
  415. /* we may get 3 more bytes, just ignore them */
  416. ps2_drain(ps2dev, 3, 100);
  417. return 0;
  418. }
  419. static int alps_absolute_mode(struct psmouse *psmouse)
  420. {
  421. struct ps2dev *ps2dev = &psmouse->ps2dev;
  422. /* Try ALPS magic knock - 4 disable before enable */
  423. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  424. 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_ENABLE))
  428. return -1;
  429. /*
  430. * Switch mouse to poll (remote) mode so motion data will not
  431. * get in our way
  432. */
  433. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  434. }
  435. static int alps_get_status(struct psmouse *psmouse, char *param)
  436. {
  437. struct ps2dev *ps2dev = &psmouse->ps2dev;
  438. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  439. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  440. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  441. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  442. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  443. return -1;
  444. psmouse_dbg(psmouse, "Status: %2.2x %2.2x %2.2x",
  445. 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. psmouse_warn(psmouse, "Failed to enable hardware tapping\n");
  509. return -1;
  510. }
  511. if (alps_absolute_mode(psmouse)) {
  512. psmouse_err(psmouse, "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. psmouse_err(psmouse, "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. }