libps2.c 8.4 KB

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