alps.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * ALPS touchpad PS/2 mouse driver
  3. *
  4. * Copyright (c) 2003 Neil Brown <neilb@cse.unsw.edu.au>
  5. * Copyright (c) 2003 Peter Osterlund <petero2@telia.com>
  6. * Copyright (c) 2004 Dmitry Torokhov <dtor@mail.ru>
  7. * Copyright (c) 2005 Vojtech Pavlik <vojtech@suse.cz>
  8. *
  9. * ALPS detection, tap switching and status querying info is taken from
  10. * tpconfig utility (by C. Scott Ananian and Bruce Kall).
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. */
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #include <linux/libps2.h>
  19. #include "psmouse.h"
  20. #include "alps.h"
  21. #undef DEBUG
  22. #ifdef DEBUG
  23. #define dbg(format, arg...) printk(KERN_INFO "alps.c: " format "\n", ## arg)
  24. #else
  25. #define dbg(format, arg...) do {} while (0)
  26. #endif
  27. #define ALPS_DUALPOINT 0x01
  28. #define ALPS_WHEEL 0x02
  29. #define ALPS_FW_BK 0x04
  30. #define ALPS_4BTN 0x08
  31. #define ALPS_OLDPROTO 0x10
  32. #define ALPS_PASS 0x20
  33. static struct alps_model_info alps_model_data[] = {
  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. { { 0x63, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  38. { { 0x63, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  39. { { 0x63, 0x02, 0x28 }, 0xf8, 0xf8, 0 },
  40. { { 0x63, 0x02, 0x3c }, 0x8f, 0x8f, ALPS_WHEEL }, /* Toshiba Satellite S2400-103 */
  41. { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK }, /* NEC Versa L320 */
  42. { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
  43. { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS }, /* Dell Latitude D800 */
  44. { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
  45. { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, 0 },
  46. { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
  47. { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
  48. { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
  49. };
  50. /*
  51. * XXX - this entry is suspicious. First byte has zero lower nibble,
  52. * which is what a normal mouse would report. Also, the value 0x0e
  53. * isn't valid per PS/2 spec.
  54. */
  55. /*
  56. * ALPS abolute Mode - new format
  57. *
  58. * byte 0: 1 ? ? ? 1 ? ? ?
  59. * byte 1: 0 x6 x5 x4 x3 x2 x1 x0
  60. * byte 2: 0 x10 x9 x8 x7 ? fin ges
  61. * byte 3: 0 y9 y8 y7 1 M R L
  62. * byte 4: 0 y6 y5 y4 y3 y2 y1 y0
  63. * byte 5: 0 z6 z5 z4 z3 z2 z1 z0
  64. *
  65. * ?'s can have different meanings on different models,
  66. * such as wheel rotation, extra buttons, stick buttons
  67. * on a dualpoint, etc.
  68. */
  69. static void alps_process_packet(struct psmouse *psmouse, struct pt_regs *regs)
  70. {
  71. struct alps_data *priv = psmouse->private;
  72. unsigned char *packet = psmouse->packet;
  73. struct input_dev *dev = &psmouse->dev;
  74. struct input_dev *dev2 = &priv->dev2;
  75. int x, y, z, ges, fin, left, right, middle;
  76. input_regs(dev, regs);
  77. if ((packet[0] & 0xc8) == 0x08) { /* 3-byte PS/2 packet */
  78. input_report_key(dev2, BTN_LEFT, packet[0] & 1);
  79. input_report_key(dev2, BTN_RIGHT, packet[0] & 2);
  80. input_report_key(dev2, BTN_MIDDLE, packet[0] & 4);
  81. input_report_rel(dev2, REL_X,
  82. packet[1] ? packet[1] - ((packet[0] << 4) & 0x100) : 0);
  83. input_report_rel(dev2, REL_Y,
  84. packet[2] ? ((packet[0] << 3) & 0x100) - packet[2] : 0);
  85. input_sync(dev2);
  86. return;
  87. }
  88. if (priv->i->flags & ALPS_OLDPROTO) {
  89. left = packet[2] & 0x08;
  90. right = packet[2] & 0x10;
  91. middle = 0;
  92. x = packet[1] | ((packet[0] & 0x07) << 7);
  93. y = packet[4] | ((packet[3] & 0x07) << 7);
  94. z = packet[5];
  95. } else {
  96. left = packet[3] & 1;
  97. right = packet[3] & 2;
  98. middle = packet[3] & 4;
  99. x = packet[1] | ((packet[2] & 0x78) << (7 - 3));
  100. y = packet[4] | ((packet[3] & 0x70) << (7 - 4));
  101. z = packet[5];
  102. }
  103. ges = packet[2] & 1;
  104. fin = packet[2] & 2;
  105. input_report_key(dev, BTN_LEFT, left);
  106. input_report_key(dev, BTN_RIGHT, right);
  107. input_report_key(dev, BTN_MIDDLE, middle);
  108. if ((priv->i->flags & ALPS_DUALPOINT) && z == 127) {
  109. input_report_rel(dev2, REL_X, (x > 383 ? (x - 768) : x));
  110. input_report_rel(dev2, REL_Y, -(y > 255 ? (y - 512) : y));
  111. input_sync(dev);
  112. input_sync(dev2);
  113. return;
  114. }
  115. /* Convert hardware tap to a reasonable Z value */
  116. if (ges && !fin) z = 40;
  117. /*
  118. * A "tap and drag" operation is reported by the hardware as a transition
  119. * from (!fin && ges) to (fin && ges). This should be translated to the
  120. * sequence Z>0, Z==0, Z>0, so the Z==0 event has to be generated manually.
  121. */
  122. if (ges && fin && !priv->prev_fin) {
  123. input_report_abs(dev, ABS_X, x);
  124. input_report_abs(dev, ABS_Y, y);
  125. input_report_abs(dev, ABS_PRESSURE, 0);
  126. input_report_key(dev, BTN_TOOL_FINGER, 0);
  127. input_sync(dev);
  128. }
  129. priv->prev_fin = fin;
  130. if (z > 30) input_report_key(dev, BTN_TOUCH, 1);
  131. if (z < 25) input_report_key(dev, BTN_TOUCH, 0);
  132. if (z > 0) {
  133. input_report_abs(dev, ABS_X, x);
  134. input_report_abs(dev, ABS_Y, y);
  135. }
  136. input_report_abs(dev, ABS_PRESSURE, z);
  137. input_report_key(dev, BTN_TOOL_FINGER, z > 0);
  138. if (priv->i->flags & ALPS_WHEEL)
  139. input_report_rel(dev, REL_WHEEL, ((packet[0] >> 4) & 0x07) | ((packet[2] >> 2) & 0x08));
  140. if (priv->i->flags & ALPS_FW_BK) {
  141. input_report_key(dev, BTN_FORWARD, packet[0] & 0x10);
  142. input_report_key(dev, BTN_BACK, packet[2] & 0x04);
  143. }
  144. input_sync(dev);
  145. }
  146. static psmouse_ret_t alps_process_byte(struct psmouse *psmouse, struct pt_regs *regs)
  147. {
  148. struct alps_data *priv = psmouse->private;
  149. if ((psmouse->packet[0] & 0xc8) == 0x08) { /* PS/2 packet */
  150. if (psmouse->pktcnt == 3) {
  151. alps_process_packet(psmouse, regs);
  152. return PSMOUSE_FULL_PACKET;
  153. }
  154. return PSMOUSE_GOOD_DATA;
  155. }
  156. if ((psmouse->packet[0] & priv->i->mask0) != priv->i->byte0)
  157. return PSMOUSE_BAD_DATA;
  158. /* Bytes 2 - 6 should have 0 in the highest bit */
  159. if (psmouse->pktcnt >= 2 && psmouse->pktcnt <= 6 &&
  160. (psmouse->packet[psmouse->pktcnt - 1] & 0x80))
  161. return PSMOUSE_BAD_DATA;
  162. if (psmouse->pktcnt == 6) {
  163. alps_process_packet(psmouse, regs);
  164. return PSMOUSE_FULL_PACKET;
  165. }
  166. return PSMOUSE_GOOD_DATA;
  167. }
  168. static struct alps_model_info *alps_get_model(struct psmouse *psmouse, int *version)
  169. {
  170. struct ps2dev *ps2dev = &psmouse->ps2dev;
  171. unsigned char rates[] = { 0, 10, 20, 40, 60, 80, 100, 200 };
  172. unsigned char param[4];
  173. int i;
  174. /*
  175. * First try "E6 report".
  176. * ALPS should return 0,0,10 or 0,0,100
  177. */
  178. param[0] = 0;
  179. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  180. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  181. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
  182. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE11))
  183. return NULL;
  184. param[0] = param[1] = param[2] = 0xff;
  185. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  186. return NULL;
  187. dbg("E6 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  188. if (param[0] != 0 || param[1] != 0 || (param[2] != 10 && param[2] != 100))
  189. return NULL;
  190. /*
  191. * Now try "E7 report". Allowed responses are in
  192. * alps_model_data[].signature
  193. */
  194. param[0] = 0;
  195. if (ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES) ||
  196. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  197. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
  198. ps2_command(ps2dev, NULL, PSMOUSE_CMD_SETSCALE21))
  199. return NULL;
  200. param[0] = param[1] = param[2] = 0xff;
  201. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  202. return NULL;
  203. dbg("E7 report: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  204. for (i = 0; i < ARRAY_SIZE(rates) && param[2] != rates[i]; i++);
  205. *version = (param[0] << 8) | (param[1] << 4) | i;
  206. for (i = 0; i < ARRAY_SIZE(alps_model_data); i++)
  207. if (!memcmp(param, alps_model_data[i].signature, sizeof(alps_model_data[i].signature)))
  208. return alps_model_data + i;
  209. return NULL;
  210. }
  211. /*
  212. * For DualPoint devices select the device that should respond to
  213. * subsequent commands. It looks like glidepad is behind stickpointer,
  214. * I'd thought it would be other way around...
  215. */
  216. static int alps_passthrough_mode(struct psmouse *psmouse, int enable)
  217. {
  218. struct ps2dev *ps2dev = &psmouse->ps2dev;
  219. unsigned char param[3];
  220. int cmd = enable ? PSMOUSE_CMD_SETSCALE21 : PSMOUSE_CMD_SETSCALE11;
  221. if (ps2_command(ps2dev, NULL, cmd) ||
  222. ps2_command(ps2dev, NULL, cmd) ||
  223. ps2_command(ps2dev, NULL, cmd) ||
  224. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE))
  225. return -1;
  226. /* we may get 3 more bytes, just ignore them */
  227. ps2_command(ps2dev, param, 0x0300);
  228. return 0;
  229. }
  230. static int alps_absolute_mode(struct psmouse *psmouse)
  231. {
  232. struct ps2dev *ps2dev = &psmouse->ps2dev;
  233. /* Try ALPS magic knock - 4 disable before enable */
  234. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  235. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  236. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  237. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  238. ps2_command(ps2dev, NULL, PSMOUSE_CMD_ENABLE))
  239. return -1;
  240. /*
  241. * Switch mouse to poll (remote) mode so motion data will not
  242. * get in our way
  243. */
  244. return ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_SETPOLL);
  245. }
  246. static int alps_get_status(struct psmouse *psmouse, char *param)
  247. {
  248. struct ps2dev *ps2dev = &psmouse->ps2dev;
  249. /* Get status: 0xF5 0xF5 0xF5 0xE9 */
  250. if (ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  251. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  252. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  253. ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO))
  254. return -1;
  255. dbg("Status: %2.2x %2.2x %2.2x", param[0], param[1], param[2]);
  256. return 0;
  257. }
  258. /*
  259. * Turn touchpad tapping on or off. The sequences are:
  260. * 0xE9 0xF5 0xF5 0xF3 0x0A to enable,
  261. * 0xE9 0xF5 0xF5 0xE8 0x00 to disable.
  262. * My guess that 0xE9 (GetInfo) is here as a sync point.
  263. * For models that also have stickpointer (DualPoints) its tapping
  264. * is controlled separately (0xE6 0xE6 0xE6 0xF3 0x14|0x0A) but
  265. * we don't fiddle with it.
  266. */
  267. static int alps_tap_mode(struct psmouse *psmouse, int enable)
  268. {
  269. struct ps2dev *ps2dev = &psmouse->ps2dev;
  270. int cmd = enable ? PSMOUSE_CMD_SETRATE : PSMOUSE_CMD_SETRES;
  271. unsigned char tap_arg = enable ? 0x0A : 0x00;
  272. unsigned char param[4];
  273. if (ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO) ||
  274. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  275. ps2_command(ps2dev, NULL, PSMOUSE_CMD_DISABLE) ||
  276. ps2_command(ps2dev, &tap_arg, cmd))
  277. return -1;
  278. if (alps_get_status(psmouse, param))
  279. return -1;
  280. return 0;
  281. }
  282. static int alps_reconnect(struct psmouse *psmouse)
  283. {
  284. struct alps_data *priv = psmouse->private;
  285. unsigned char param[4];
  286. int version;
  287. psmouse_reset(psmouse);
  288. if (!(priv->i = alps_get_model(psmouse, &version)))
  289. return -1;
  290. if (priv->i->flags & ALPS_PASS && alps_passthrough_mode(psmouse, 1))
  291. return -1;
  292. if (alps_get_status(psmouse, param))
  293. return -1;
  294. if (!(param[0] & 0x04))
  295. alps_tap_mode(psmouse, 1);
  296. if (alps_absolute_mode(psmouse)) {
  297. printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
  298. return -1;
  299. }
  300. if (priv->i->flags == ALPS_PASS && alps_passthrough_mode(psmouse, 0))
  301. return -1;
  302. return 0;
  303. }
  304. static void alps_disconnect(struct psmouse *psmouse)
  305. {
  306. struct alps_data *priv = psmouse->private;
  307. psmouse_reset(psmouse);
  308. input_unregister_device(&priv->dev2);
  309. kfree(priv);
  310. }
  311. int alps_init(struct psmouse *psmouse)
  312. {
  313. struct alps_data *priv;
  314. unsigned char param[4];
  315. int version;
  316. psmouse->private = priv = kmalloc(sizeof(struct alps_data), GFP_KERNEL);
  317. if (!priv)
  318. goto init_fail;
  319. memset(priv, 0, sizeof(struct alps_data));
  320. if (!(priv->i = alps_get_model(psmouse, &version)))
  321. goto init_fail;
  322. if ((priv->i->flags & ALPS_PASS) && alps_passthrough_mode(psmouse, 1))
  323. goto init_fail;
  324. if (alps_get_status(psmouse, param)) {
  325. printk(KERN_ERR "alps.c: touchpad status report request failed\n");
  326. goto init_fail;
  327. }
  328. if (param[0] & 0x04) {
  329. printk(KERN_INFO "alps.c: Enabling hardware tapping\n");
  330. if (alps_tap_mode(psmouse, 1))
  331. printk(KERN_WARNING "alps.c: Failed to enable hardware tapping\n");
  332. }
  333. if (alps_absolute_mode(psmouse)) {
  334. printk(KERN_ERR "alps.c: Failed to enable absolute mode\n");
  335. goto init_fail;
  336. }
  337. if ((priv->i->flags & ALPS_PASS) && alps_passthrough_mode(psmouse, 0))
  338. goto init_fail;
  339. psmouse->dev.evbit[LONG(EV_KEY)] |= BIT(EV_KEY);
  340. psmouse->dev.keybit[LONG(BTN_TOUCH)] |= BIT(BTN_TOUCH);
  341. psmouse->dev.keybit[LONG(BTN_TOOL_FINGER)] |= BIT(BTN_TOOL_FINGER);
  342. psmouse->dev.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
  343. psmouse->dev.evbit[LONG(EV_ABS)] |= BIT(EV_ABS);
  344. input_set_abs_params(&psmouse->dev, ABS_X, 0, 1023, 0, 0);
  345. input_set_abs_params(&psmouse->dev, ABS_Y, 0, 767, 0, 0);
  346. input_set_abs_params(&psmouse->dev, ABS_PRESSURE, 0, 127, 0, 0);
  347. if (priv->i->flags & ALPS_WHEEL) {
  348. psmouse->dev.evbit[LONG(EV_REL)] |= BIT(EV_REL);
  349. psmouse->dev.relbit[LONG(REL_WHEEL)] |= BIT(REL_WHEEL);
  350. }
  351. if (priv->i->flags & ALPS_FW_BK) {
  352. psmouse->dev.keybit[LONG(BTN_FORWARD)] |= BIT(BTN_FORWARD);
  353. psmouse->dev.keybit[LONG(BTN_BACK)] |= BIT(BTN_BACK);
  354. }
  355. sprintf(priv->phys, "%s/input1", psmouse->ps2dev.serio->phys);
  356. priv->dev2.phys = priv->phys;
  357. priv->dev2.name = (priv->i->flags & ALPS_DUALPOINT) ? "DualPoint Stick" : "PS/2 Mouse";
  358. priv->dev2.id.bustype = BUS_I8042;
  359. priv->dev2.id.vendor = 0x0002;
  360. priv->dev2.id.product = PSMOUSE_ALPS;
  361. priv->dev2.id.version = 0x0000;
  362. priv->dev2.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
  363. priv->dev2.relbit[LONG(REL_X)] |= BIT(REL_X) | BIT(REL_Y);
  364. priv->dev2.keybit[LONG(BTN_LEFT)] |= BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
  365. input_register_device(&priv->dev2);
  366. printk(KERN_INFO "input: %s on %s\n", priv->dev2.name, psmouse->ps2dev.serio->phys);
  367. psmouse->protocol_handler = alps_process_byte;
  368. psmouse->disconnect = alps_disconnect;
  369. psmouse->reconnect = alps_reconnect;
  370. psmouse->pktsize = 6;
  371. return 0;
  372. init_fail:
  373. kfree(priv);
  374. return -1;
  375. }
  376. int alps_detect(struct psmouse *psmouse, int set_properties)
  377. {
  378. int version;
  379. struct alps_model_info *model;
  380. if (!(model = alps_get_model(psmouse, &version)))
  381. return -1;
  382. if (set_properties) {
  383. psmouse->vendor = "ALPS";
  384. if (model->flags & ALPS_DUALPOINT)
  385. psmouse->name = "DualPoint TouchPad";
  386. else
  387. psmouse->name = "GlidePoint";
  388. psmouse->model = version;
  389. }
  390. return 0;
  391. }