alps.c 21 KB

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