libps2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #include <linux/init.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. /*
  48. * ps2_drain() waits for device to transmit requested number of bytes
  49. * and discards them.
  50. */
  51. void ps2_drain(struct ps2dev *ps2dev, int maxbytes, int timeout)
  52. {
  53. if (maxbytes > sizeof(ps2dev->cmdbuf)) {
  54. WARN_ON(1);
  55. maxbytes = sizeof(ps2dev->cmdbuf);
  56. }
  57. mutex_lock(&ps2dev->cmd_mutex);
  58. serio_pause_rx(ps2dev->serio);
  59. ps2dev->flags = PS2_FLAG_CMD;
  60. ps2dev->cmdcnt = maxbytes;
  61. serio_continue_rx(ps2dev->serio);
  62. wait_event_timeout(ps2dev->wait,
  63. !(ps2dev->flags & PS2_FLAG_CMD),
  64. msecs_to_jiffies(timeout));
  65. mutex_unlock(&ps2dev->cmd_mutex);
  66. }
  67. EXPORT_SYMBOL(ps2_drain);
  68. /*
  69. * ps2_is_keyboard_id() checks received ID byte against the list of
  70. * known keyboard IDs.
  71. */
  72. int ps2_is_keyboard_id(char id_byte)
  73. {
  74. static const char keyboard_ids[] = {
  75. 0xab, /* Regular keyboards */
  76. 0xac, /* NCD Sun keyboard */
  77. 0x2b, /* Trust keyboard, translated */
  78. 0x5d, /* Trust keyboard */
  79. 0x60, /* NMB SGI keyboard, translated */
  80. 0x47, /* NMB SGI keyboard */
  81. };
  82. return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
  83. }
  84. EXPORT_SYMBOL(ps2_is_keyboard_id);
  85. /*
  86. * ps2_adjust_timeout() is called after receiving 1st byte of command
  87. * response and tries to reduce remaining timeout to speed up command
  88. * completion.
  89. */
  90. static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
  91. {
  92. switch (command) {
  93. case PS2_CMD_RESET_BAT:
  94. /*
  95. * Device has sent the first response byte after
  96. * reset command, reset is thus done, so we can
  97. * shorten the timeout.
  98. * The next byte will come soon (keyboard) or not
  99. * at all (mouse).
  100. */
  101. if (timeout > msecs_to_jiffies(100))
  102. timeout = msecs_to_jiffies(100);
  103. break;
  104. case PS2_CMD_GETID:
  105. /*
  106. * Microsoft Natural Elite keyboard responds to
  107. * the GET ID command as it were a mouse, with
  108. * a single byte. Fail the command so atkbd will
  109. * use alternative probe to detect it.
  110. */
  111. if (ps2dev->cmdbuf[1] == 0xaa) {
  112. serio_pause_rx(ps2dev->serio);
  113. ps2dev->flags = 0;
  114. serio_continue_rx(ps2dev->serio);
  115. timeout = 0;
  116. }
  117. /*
  118. * If device behind the port is not a keyboard there
  119. * won't be 2nd byte of ID response.
  120. */
  121. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  122. serio_pause_rx(ps2dev->serio);
  123. ps2dev->flags = ps2dev->cmdcnt = 0;
  124. serio_continue_rx(ps2dev->serio);
  125. timeout = 0;
  126. }
  127. break;
  128. default:
  129. break;
  130. }
  131. return timeout;
  132. }
  133. /*
  134. * ps2_command() sends a command and its parameters to the mouse,
  135. * then waits for the response and puts it in the param array.
  136. *
  137. * ps2_command() can only be called from a process context
  138. */
  139. int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  140. {
  141. int timeout;
  142. int send = (command >> 12) & 0xf;
  143. int receive = (command >> 8) & 0xf;
  144. int rc = -1;
  145. int i;
  146. if (receive > sizeof(ps2dev->cmdbuf)) {
  147. WARN_ON(1);
  148. return -1;
  149. }
  150. if (send && !param) {
  151. WARN_ON(1);
  152. return -1;
  153. }
  154. mutex_lock(&ps2dev->cmd_mutex);
  155. serio_pause_rx(ps2dev->serio);
  156. ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
  157. ps2dev->cmdcnt = receive;
  158. if (receive && param)
  159. for (i = 0; i < receive; i++)
  160. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  161. serio_continue_rx(ps2dev->serio);
  162. /*
  163. * Some devices (Synaptics) peform the reset before
  164. * ACKing the reset command, and so it can take a long
  165. * time before the ACK arrrives.
  166. */
  167. if (ps2_sendbyte(ps2dev, command & 0xff,
  168. command == PS2_CMD_RESET_BAT ? 1000 : 200))
  169. goto out;
  170. for (i = 0; i < send; i++)
  171. if (ps2_sendbyte(ps2dev, param[i], 200))
  172. goto out;
  173. /*
  174. * The reset command takes a long time to execute.
  175. */
  176. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  177. timeout = wait_event_timeout(ps2dev->wait,
  178. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  179. if (ps2dev->cmdcnt && timeout > 0) {
  180. timeout = ps2_adjust_timeout(ps2dev, command, timeout);
  181. wait_event_timeout(ps2dev->wait,
  182. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  183. }
  184. if (param)
  185. for (i = 0; i < receive; i++)
  186. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  187. if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
  188. goto out;
  189. rc = 0;
  190. out:
  191. serio_pause_rx(ps2dev->serio);
  192. ps2dev->flags = 0;
  193. serio_continue_rx(ps2dev->serio);
  194. mutex_unlock(&ps2dev->cmd_mutex);
  195. return rc;
  196. }
  197. EXPORT_SYMBOL(ps2_command);
  198. /*
  199. * ps2_init() initializes ps2dev structure
  200. */
  201. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  202. {
  203. mutex_init(&ps2dev->cmd_mutex);
  204. lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
  205. init_waitqueue_head(&ps2dev->wait);
  206. ps2dev->serio = serio;
  207. }
  208. EXPORT_SYMBOL(ps2_init);
  209. /*
  210. * ps2_handle_ack() is supposed to be used in interrupt handler
  211. * to properly process ACK/NAK of a command from a PS/2 device.
  212. */
  213. int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
  214. {
  215. switch (data) {
  216. case PS2_RET_ACK:
  217. ps2dev->nak = 0;
  218. break;
  219. case PS2_RET_NAK:
  220. ps2dev->nak = 1;
  221. break;
  222. /*
  223. * Workaround for mice which don't ACK the Get ID command.
  224. * These are valid mouse IDs that we recognize.
  225. */
  226. case 0x00:
  227. case 0x03:
  228. case 0x04:
  229. if (ps2dev->flags & PS2_FLAG_WAITID) {
  230. ps2dev->nak = 0;
  231. break;
  232. }
  233. /* Fall through */
  234. default:
  235. return 0;
  236. }
  237. if (!ps2dev->nak && ps2dev->cmdcnt)
  238. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  239. ps2dev->flags &= ~PS2_FLAG_ACK;
  240. wake_up(&ps2dev->wait);
  241. if (data != PS2_RET_ACK)
  242. ps2_handle_response(ps2dev, data);
  243. return 1;
  244. }
  245. EXPORT_SYMBOL(ps2_handle_ack);
  246. /*
  247. * ps2_handle_response() is supposed to be used in interrupt handler
  248. * to properly store device's response to a command and notify process
  249. * waiting for completion of the command.
  250. */
  251. int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
  252. {
  253. if (ps2dev->cmdcnt)
  254. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  255. if (ps2dev->flags & PS2_FLAG_CMD1) {
  256. ps2dev->flags &= ~PS2_FLAG_CMD1;
  257. if (ps2dev->cmdcnt)
  258. wake_up(&ps2dev->wait);
  259. }
  260. if (!ps2dev->cmdcnt) {
  261. ps2dev->flags &= ~PS2_FLAG_CMD;
  262. wake_up(&ps2dev->wait);
  263. }
  264. return 1;
  265. }
  266. EXPORT_SYMBOL(ps2_handle_response);
  267. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  268. {
  269. if (ps2dev->flags & PS2_FLAG_ACK)
  270. ps2dev->nak = 1;
  271. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  272. wake_up(&ps2dev->wait);
  273. ps2dev->flags = 0;
  274. }
  275. EXPORT_SYMBOL(ps2_cmd_aborted);