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