alps.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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
  365. */
  366. param[0] = 0;
  367. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  368. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  369. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  370. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
  371. return NULL;
  372. param[0] = param[1] = param[2] = 0xff;
  373. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  374. return NULL;
  375. dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  376. if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
  377. return NULL;
  378. /*
  379. * Now try "E7 report". Allowed responses are in
  380. * alps_model_data[].signature
  381. */
  382. param[0] = 0;
  383. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  384. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  385. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  386. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
  387. return NULL;
  388. param[0] = param[1] = param[2] = 0xff;
  389. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  390. return NULL;
  391. dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  392. if (version) {
  393. for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++)
  394. /* empty */;
  395. *version = (param[0] << 8) | (param[1] << 4) | i;
  396. }
  397. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
  398. if (!memcmp(param, alps_model_data[i].signature,
  399. sizeof(alps_model_data[i].signature)))
  400. return alps_model_data + i;
  401. return NULL;
  402. }
  403. /*
  404. * For DualPoint devices select the device that should respond to
  405. * subsequent commands. It looks like glidepad is behind stickpointer,
  406. * I'd thought it would be other way around...
  407. */
  408. static int alps_passthrough_mode(struct psmouse *psmouse, bool enable)
  409. {
  410. struct ps2dev *ps2dev = &psmouse->ps2dev;
  411. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  412. if (ps2_command(ps2dev, NULL, cmd) ||
  413. ps2_command(ps2dev, NULL, cmd) ||
  414. ps2_command(ps2dev, NULL, cmd) ||
  415. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  416. return -1;
  417. /* we may get 3 more bytes, just ignore them */
  418. ps2_drain(ps2dev, 3, 100);
  419. return 0;
  420. }
  421. static int alps_absolute_mode(struct psmouse *psmouse)
  422. {
  423. struct ps2dev *ps2dev = &psmouse->ps2dev;
  424. /* Try ALPS magic knock - 4 disable before enable */
  425. if (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_DISABLE) ||
  429. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  430. return -1;
  431. /*
  432. * Switch mouse to poll (remote) mode so motion data will not
  433. * get in our way
  434. */
  435. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  436. }
  437. static int alps_get_status(struct psmouse *psmouse, char *param)
  438. {
  439. struct ps2dev *ps2dev = &psmouse->ps2dev;
  440. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  441. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  442. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  443. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  444. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  445. return -1;
  446. dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  447. return 0;
  448. }
  449. /*
  450. * Turn touchpad tapping on or off. The sequences are:
  451. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  452. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  453. * My guess that 0xE9 (GetInfo) is here as a sync point.
  454. * For models that also have stickpointer (DualPoints) its tapping
  455. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  456. * we don't fiddle with it.
  457. */
  458. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  459. {
  460. struct ps2dev *ps2dev = &psmouse->ps2dev;
  461. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  462. unsigned char tap_arg = enable ? 0x0A : 0x00;
  463. unsigned char param[4];
  464. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  465. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  466. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  467. ps2_command(ps2dev, &tap_arg, cmd))
  468. return -1;
  469. if (alps_get_status(psmouse, param))
  470. return -1;
  471. return 0;
  472. }
  473. /*
  474. * alps_poll() - poll the touchpad for current motion packet.
  475. * Used in resync.
  476. */
  477. static int alps_poll(struct psmouse *psmouse)
  478. {
  479. struct alps_data *priv = psmouse->private;
  480. unsigned char buf[6];
  481. bool poll_failed;
  482. if (priv->i->flags & ALPS_PASS)
  483. alps_passthrough_mode(psmouse, true);
  484. poll_failed = ps2_command(&psmouse->ps2dev, buf,
  485. PSMOUSE_CMD_POLL | (psmouse->pktsize << 8)) < 0;
  486. if (priv->i->flags & ALPS_PASS)
  487. alps_passthrough_mode(psmouse, false);
  488. if (poll_failed || (buf[0] & priv->i->mask0) != priv->i->byte0)
  489. return -1;
  490. if ((psmouse->badbyte & 0xc8) == 0x08) {
  491. /*
  492. * Poll the track stick ...
  493. */
  494. if (ps2_command(&psmouse->ps2dev, buf, PSMOUSE_CMD_POLL | (3 << 8)))
  495. return -1;
  496. }
  497. memcpy(psmouse->packet, buf, sizeof(buf));
  498. return 0;
  499. }
  500. static int alps_hw_init(struct psmouse *psmouse)
  501. {
  502. struct alps_data *priv = psmouse->private;
  503. const struct alps_model_info *model = priv->i;
  504. if ((model->flags & ALPS_PASS) &&
  505. alps_passthrough_mode(psmouse, true)) {
  506. return -1;
  507. }
  508. if (alps_tap_mode(psmouse, true)) {
  509. printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
  510. return -1;
  511. }
  512. if (alps_absolute_mode(psmouse)) {
  513. printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
  514. return -1;
  515. }
  516. if ((model->flags & ALPS_PASS) &&
  517. alps_passthrough_mode(psmouse, false)) {
  518. return -1;
  519. }
  520. /* ALPS needs stream mode, otherwise it won't report any data */
  521. if (ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETSTREAM)) {
  522. printk(KERN_ERR "alps.c: Failed to enable stream mode\n");
  523. return -1;
  524. }
  525. return 0;
  526. }
  527. static int alps_reconnect(struct psmouse *psmouse)
  528. {
  529. const struct alps_model_info *model;
  530. psmouse_reset(psmouse);
  531. model = alps_get_model(psmouse, NULL);
  532. if (!model)
  533. return -1;
  534. return alps_hw_init(psmouse);
  535. }
  536. static void alps_disconnect(struct psmouse *psmouse)
  537. {
  538. struct alps_data *priv = psmouse->private;
  539. psmouse_reset(psmouse);
  540. del_timer_sync(&priv->timer);
  541. input_unregister_device(priv->dev2);
  542. kfree(priv);
  543. }
  544. int alps_init(struct psmouse *psmouse)
  545. {
  546. struct alps_data *priv;
  547. const struct alps_model_info *model;
  548. struct input_dev *dev1 = psmouse->dev, *dev2;
  549. int version;
  550. priv = kzalloc(sizeof(struct alps_data), GFP_KERNEL);
  551. dev2 = input_allocate_device();
  552. if (!priv || !dev2)
  553. goto init_fail;
  554. priv->dev2 = dev2;
  555. setup_timer(&priv->timer, alps_flush_packet, (unsigned long)psmouse);
  556. psmouse->private = priv;
  557. model = alps_get_model(psmouse, &version);
  558. if (!model)
  559. goto init_fail;
  560. priv->i = model;
  561. if (alps_hw_init(psmouse))
  562. goto init_fail;
  563. /*
  564. * Undo part of setup done for us by psmouse core since touchpad
  565. * is not a relative device.
  566. */
  567. __clear_bit(EV_REL, dev1->evbit);
  568. __clear_bit(REL_X, dev1->relbit);
  569. __clear_bit(REL_Y, dev1->relbit);
  570. /*
  571. * Now set up our capabilities.
  572. */
  573. dev1->evbit[BIT_WORD(EV_KEY)] |= BIT_MASK(EV_KEY);
  574. dev1->keybit[BIT_WORD(BTN_TOUCH)] |= BIT_MASK(BTN_TOUCH);
  575. dev1->keybit[BIT_WORD(BTN_TOOL_FINGER)] |= BIT_MASK(BTN_TOOL_FINGER);
  576. dev1->keybit[BIT_WORD(BTN_LEFT)] |=
  577. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT);
  578. dev1->evbit[BIT_WORD(EV_ABS)] |= BIT_MASK(EV_ABS);
  579. input_set_abs_params(dev1, ABS_X, 0, 1023, 0, 0);
  580. input_set_abs_params(dev1, ABS_Y, 0, 767, 0, 0);
  581. input_set_abs_params(dev1, ABS_PRESSURE, 0, 127, 0, 0);
  582. if (model->flags & ALPS_WHEEL) {
  583. dev1->evbit[BIT_WORD(EV_REL)] |= BIT_MASK(EV_REL);
  584. dev1->relbit[BIT_WORD(REL_WHEEL)] |= BIT_MASK(REL_WHEEL);
  585. }
  586. if (model->flags & (ALPS_FW_BK_1 | ALPS_FW_BK_2)) {
  587. dev1->keybit[BIT_WORD(BTN_FORWARD)] |= BIT_MASK(BTN_FORWARD);
  588. dev1->keybit[BIT_WORD(BTN_BACK)] |= BIT_MASK(BTN_BACK);
  589. }
  590. if (model->flags & ALPS_FOUR_BUTTONS) {
  591. dev1->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_0);
  592. dev1->keybit[BIT_WORD(BTN_1)] |= BIT_MASK(BTN_1);
  593. dev1->keybit[BIT_WORD(BTN_2)] |= BIT_MASK(BTN_2);
  594. dev1->keybit[BIT_WORD(BTN_3)] |= BIT_MASK(BTN_3);
  595. } else {
  596. dev1->keybit[BIT_WORD(BTN_MIDDLE)] |= BIT_MASK(BTN_MIDDLE);
  597. }
  598. snprintf(priv->phys, sizeof(priv->phys), "%s/input1", psmouse->ps2dev.serio->phys);
  599. dev2->phys = priv->phys;
  600. dev2->name = (model->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
  601. dev2->id.bustype = BUS_I8042;
  602. dev2->id.vendor = 0x0002;
  603. dev2->id.product = PSMOUSE_ALPS;
  604. dev2->id.version = 0x0000;
  605. dev2->dev.parent = &psmouse->ps2dev.serio->dev;
  606. dev2->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  607. dev2->relbit[BIT_WORD(REL_X)] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  608. dev2->keybit[BIT_WORD(BTN_LEFT)] =
  609. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
  610. if (input_register_device(priv->dev2))
  611. goto init_fail;
  612. psmouse->protocol_handler = alps_process_byte;
  613. psmouse->poll = alps_poll;
  614. psmouse->disconnect = alps_disconnect;
  615. psmouse->reconnect = alps_reconnect;
  616. psmouse->pktsize = 6;
  617. /* We are having trouble resyncing ALPS touchpads so disable it for now */
  618. psmouse->resync_time = 0;
  619. return 0;
  620. init_fail:
  621. psmouse_reset(psmouse);
  622. input_free_device(dev2);
  623. kfree(priv);
  624. psmouse->private = NULL;
  625. return -1;
  626. }
  627. int alps_detect(struct psmouse *psmouse, bool set_properties)
  628. {
  629. int version;
  630. const struct alps_model_info *model;
  631. model = alps_get_model(psmouse, &version);
  632. if (!model)
  633. return -1;
  634. if (set_properties) {
  635. psmouse->vendor = "ALPS";
  636. psmouse->name = model->flags & ALPS_DUALPOINT ?
  637. "DualPoint TouchPad" : "GlidePoint";
  638. psmouse->model = version;
  639. }
  640. return 0;
  641. }