libps2.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * PS/2 driver library
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. * Copyright (c) 2004 Dmitry Torokhov
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #include <linux/i8042.h>
  19. #include <linux/libps2.h>
  20. #define DRIVER_DESC "PS/2 driver library"
  21. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  22. MODULE_DESCRIPTION("PS/2 driver library");
  23. MODULE_LICENSE("GPL");
  24. /*
  25. * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
  26. * It doesn't handle retransmission, though it could - because if there
  27. * is a need for retransmissions device has to be replaced anyway.
  28. *
  29. * ps2_sendbyte() can only be called from a process context.
  30. */
  31. int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
  32. {
  33. serio_pause_rx(ps2dev->serio);
  34. ps2dev->nak = 1;
  35. ps2dev->flags |= PS2_FLAG_ACK;
  36. serio_continue_rx(ps2dev->serio);
  37. if (serio_write(ps2dev->serio, byte) == 0)
  38. wait_event_timeout(ps2dev->wait,
  39. !(ps2dev->flags & PS2_FLAG_ACK),
  40. msecs_to_jiffies(timeout));
  41. serio_pause_rx(ps2dev->serio);
  42. ps2dev->flags &= ~PS2_FLAG_ACK;
  43. serio_continue_rx(ps2dev->serio);
  44. return -ps2dev->nak;
  45. }
  46. EXPORT_SYMBOL(ps2_sendbyte);
  47. void ps2_begin_command(struct ps2dev *ps2dev)
  48. {
  49. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  50. mutex_lock(m);
  51. }
  52. EXPORT_SYMBOL(ps2_begin_command);
  53. void ps2_end_command(struct ps2dev *ps2dev)
  54. {
  55. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  56. mutex_unlock(m);
  57. }
  58. EXPORT_SYMBOL(ps2_end_command);
  59. /*
  60. * ps2_drain() waits for device to transmit requested number of bytes
  61. * and discards them.
  62. */
  63. void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
  64. {
  65. if (maxbytes > sizeof(ps2dev->cmdbuf)) {
  66. WARN_ON(1);
  67. maxbytes = sizeof(ps2dev->cmdbuf);
  68. }
  69. ps2_begin_command(ps2dev);
  70. serio_pause_rx(ps2dev->serio);
  71. ps2dev->flags = PS2_FLAG_CMD;
  72. ps2dev->cmdcnt = maxbytes;
  73. serio_continue_rx(ps2dev->serio);
  74. wait_event_timeout(ps2dev->wait,
  75. !(ps2dev->flags & PS2_FLAG_CMD),
  76. msecs_to_jiffies(timeout));
  77. ps2_end_command(ps2dev);
  78. }
  79. EXPORT_SYMBOL(ps2_drain);
  80. /*
  81. * ps2_is_keyboard_id() checks received ID byte against the list of
  82. * known keyboard IDs.
  83. */
  84. int ps2_is_keyboard_id(char id_byte)
  85. {
  86. static const char keyboard_ids[] = {
  87. 0xab, /* Regular keyboards */
  88. 0xac, /* NCD Sun keyboard */
  89. 0x2b, /* Trust keyboard, translated */
  90. 0x5d, /* Trust keyboard */
  91. 0x60, /* NMB SGI keyboard, translated */
  92. 0x47, /* NMB SGI keyboard */
  93. };
  94. return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
  95. }
  96. EXPORT_SYMBOL(ps2_is_keyboard_id);
  97. /*
  98. * ps2_adjust_timeout() is called after receiving 1st byte of command
  99. * response and tries to reduce remaining timeout to speed up command
  100. * completion.
  101. */
  102. static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
  103. {
  104. switch (command) {
  105. case PS2_CMD_RESET_BAT:
  106. /*
  107. * Device has sent the first response byte after
  108. * reset command, reset is thus done, so we can
  109. * shorten the timeout.
  110. * The next byte will come soon (keyboard) or not
  111. * at all (mouse).
  112. */
  113. if (timeout > msecs_to_jiffies(100))
  114. timeout = msecs_to_jiffies(100);
  115. break;
  116. case PS2_CMD_GETID:
  117. /*
  118. * Microsoft Natural Elite keyboard responds to
  119. * the GET ID command as it were a mouse, with
  120. * a single byte. Fail the command so atkbd will
  121. * use alternative probe to detect it.
  122. */
  123. if (ps2dev->cmdbuf[1] == 0xaa) {
  124. serio_pause_rx(ps2dev->serio);
  125. ps2dev->flags = 0;
  126. serio_continue_rx(ps2dev->serio);
  127. timeout = 0;
  128. }
  129. /*
  130. * If device behind the port is not a keyboard there
  131. * won't be 2nd byte of ID response.
  132. */
  133. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  134. serio_pause_rx(ps2dev->serio);
  135. ps2dev->flags = ps2dev->cmdcnt = 0;
  136. serio_continue_rx(ps2dev->serio);
  137. timeout = 0;
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. return timeout;
  144. }
  145. /*
  146. * ps2_command() sends a command and its parameters to the mouse,
  147. * then waits for the response and puts it in the param array.
  148. *
  149. * ps2_command() can only be called from a process context
  150. */
  151. int __ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  152. {
  153. int timeout;
  154. int send = (command >> 12) & 0xf;
  155. int receive = (command >> 8) & 0xf;
  156. int rc = -1;
  157. int i;
  158. if (receive > sizeof(ps2dev->cmdbuf)) {
  159. WARN_ON(1);
  160. return -1;
  161. }
  162. if (send && !param) {
  163. WARN_ON(1);
  164. return -1;
  165. }
  166. serio_pause_rx(ps2dev->serio);
  167. ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
  168. ps2dev->cmdcnt = receive;
  169. if (receive && param)
  170. for (i = 0; i < receive; i++)
  171. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  172. serio_continue_rx(ps2dev->serio);
  173. /*
  174. * Some devices (Synaptics) peform the reset before
  175. * ACKing the reset command, and so it can take a long
  176. * time before the ACK arrives.
  177. */
  178. if (ps2_sendbyte(ps2dev, command & 0xff,
  179. command == PS2_CMD_RESET_BAT ? 1000 : 200)) {
  180. serio_pause_rx(ps2dev->serio);
  181. goto out_reset_flags;
  182. }
  183. for (i = 0; i < send; i++) {
  184. if (ps2_sendbyte(ps2dev, param[i], 200)) {
  185. serio_pause_rx(ps2dev->serio);
  186. goto out_reset_flags;
  187. }
  188. }
  189. /*
  190. * The reset command takes a long time to execute.
  191. */
  192. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  193. timeout = wait_event_timeout(ps2dev->wait,
  194. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  195. if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
  196. timeout = ps2_adjust_timeout(ps2dev, command, timeout);
  197. wait_event_timeout(ps2dev->wait,
  198. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  199. }
  200. serio_pause_rx(ps2dev->serio);
  201. if (param)
  202. for (i = 0; i < receive; i++)
  203. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  204. if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
  205. goto out_reset_flags;
  206. rc = 0;
  207. out_reset_flags:
  208. ps2dev->flags = 0;
  209. serio_continue_rx(ps2dev->serio);
  210. return rc;
  211. }
  212. EXPORT_SYMBOL(__ps2_command);
  213. int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  214. {
  215. int rc;
  216. ps2_begin_command(ps2dev);
  217. rc = __ps2_command(ps2dev, param, command);
  218. ps2_end_command(ps2dev);
  219. return rc;
  220. }
  221. EXPORT_SYMBOL(ps2_command);
  222. /*
  223. * ps2_init() initializes ps2dev structure
  224. */
  225. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  226. {
  227. mutex_init(&ps2dev->cmd_mutex);
  228. lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
  229. init_waitqueue_head(&ps2dev->wait);
  230. ps2dev->serio = serio;
  231. }
  232. EXPORT_SYMBOL(ps2_init);
  233. /*
  234. * ps2_handle_ack() is supposed to be used in interrupt handler
  235. * to properly process ACK/NAK of a command from a PS/2 device.
  236. */
  237. int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
  238. {
  239. switch (data) {
  240. case PS2_RET_ACK:
  241. ps2dev->nak = 0;
  242. break;
  243. case PS2_RET_NAK:
  244. ps2dev->flags |= PS2_FLAG_NAK;
  245. ps2dev->nak = PS2_RET_NAK;
  246. break;
  247. case PS2_RET_ERR:
  248. if (ps2dev->flags & PS2_FLAG_NAK) {
  249. ps2dev->flags &= ~PS2_FLAG_NAK;
  250. ps2dev->nak = PS2_RET_ERR;
  251. break;
  252. }
  253. /*
  254. * Workaround for mice which don't ACK the Get ID command.
  255. * These are valid mouse IDs that we recognize.
  256. */
  257. case 0x00:
  258. case 0x03:
  259. case 0x04:
  260. if (ps2dev->flags & PS2_FLAG_WAITID) {
  261. ps2dev->nak = 0;
  262. break;
  263. }
  264. /* Fall through */
  265. default:
  266. return 0;
  267. }
  268. if (!ps2dev->nak) {
  269. ps2dev->flags &= ~PS2_FLAG_NAK;
  270. if (ps2dev->cmdcnt)
  271. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  272. }
  273. ps2dev->flags &= ~PS2_FLAG_ACK;
  274. wake_up(&ps2dev->wait);
  275. if (data != PS2_RET_ACK)
  276. ps2_handle_response(ps2dev, data);
  277. return 1;
  278. }
  279. EXPORT_SYMBOL(ps2_handle_ack);
  280. /*
  281. * ps2_handle_response() is supposed to be used in interrupt handler
  282. * to properly store device's response to a command and notify process
  283. * waiting for completion of the command.
  284. */
  285. int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
  286. {
  287. if (ps2dev->cmdcnt)
  288. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  289. if (ps2dev->flags & PS2_FLAG_CMD1) {
  290. ps2dev->flags &= ~PS2_FLAG_CMD1;
  291. if (ps2dev->cmdcnt)
  292. wake_up(&ps2dev->wait);
  293. }
  294. if (!ps2dev->cmdcnt) {
  295. ps2dev->flags &= ~PS2_FLAG_CMD;
  296. wake_up(&ps2dev->wait);
  297. }
  298. return 1;
  299. }
  300. EXPORT_SYMBOL(ps2_handle_response);
  301. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  302. {
  303. if (ps2dev->flags & PS2_FLAG_ACK)
  304. ps2dev->nak = 1;
  305. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  306. wake_up(&ps2dev->wait);
  307. /* reset all flags except last nack */
  308. ps2dev->flags &= PS2_FLAG_NAK;
  309. }
  310. EXPORT_SYMBOL(ps2_cmd_aborted);