libps2.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/moduleparam.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/input.h>
  18. #include <linux/serio.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. EXPORT_SYMBOL(ps2_init);
  26. EXPORT_SYMBOL(ps2_sendbyte);
  27. EXPORT_SYMBOL(ps2_drain);
  28. EXPORT_SYMBOL(ps2_command);
  29. EXPORT_SYMBOL(ps2_schedule_command);
  30. EXPORT_SYMBOL(ps2_handle_ack);
  31. EXPORT_SYMBOL(ps2_handle_response);
  32. EXPORT_SYMBOL(ps2_cmd_aborted);
  33. /* Work structure to schedule execution of a command */
  34. struct ps2work {
  35. struct work_struct work;
  36. struct ps2dev *ps2dev;
  37. int command;
  38. unsigned char param[0];
  39. };
  40. /*
  41. * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
  42. * It doesn't handle retransmission, though it could - because if there
  43. * is a need for retransmissions device has to be replaced anyway.
  44. *
  45. * ps2_sendbyte() can only be called from a process context.
  46. */
  47. int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
  48. {
  49. serio_pause_rx(ps2dev->serio);
  50. ps2dev->nak = 1;
  51. ps2dev->flags |= PS2_FLAG_ACK;
  52. serio_continue_rx(ps2dev->serio);
  53. if (serio_write(ps2dev->serio, byte) == 0)
  54. wait_event_timeout(ps2dev->wait,
  55. !(ps2dev->flags & PS2_FLAG_ACK),
  56. msecs_to_jiffies(timeout));
  57. serio_pause_rx(ps2dev->serio);
  58. ps2dev->flags &= ~PS2_FLAG_ACK;
  59. serio_continue_rx(ps2dev->serio);
  60. return -ps2dev->nak;
  61. }
  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. mutex_lock(&ps2dev->cmd_mutex);
  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. mutex_unlock(&ps2dev->cmd_mutex);
  81. }
  82. /*
  83. * ps2_is_keyboard_id() checks received ID byte against the list of
  84. * known keyboard IDs.
  85. */
  86. static inline int ps2_is_keyboard_id(char id_byte)
  87. {
  88. static char keyboard_ids[] = {
  89. 0xab, /* Regular keyboards */
  90. 0xac, /* NCD Sun keyboard */
  91. 0x2b, /* Trust keyboard, translated */
  92. 0x5d, /* Trust keyboard */
  93. 0x60, /* NMB SGI keyboard, translated */
  94. 0x47, /* NMB SGI keyboard */
  95. };
  96. return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
  97. }
  98. /*
  99. * ps2_adjust_timeout() is called after receiving 1st byte of command
  100. * response and tries to reduce remaining timeout to speed up command
  101. * completion.
  102. */
  103. static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
  104. {
  105. switch (command) {
  106. case PS2_CMD_RESET_BAT:
  107. /*
  108. * Device has sent the first response byte after
  109. * reset command, reset is thus done, so we can
  110. * shorten the timeout.
  111. * The next byte will come soon (keyboard) or not
  112. * at all (mouse).
  113. */
  114. if (timeout > msecs_to_jiffies(100))
  115. timeout = msecs_to_jiffies(100);
  116. break;
  117. case PS2_CMD_GETID:
  118. /*
  119. * If device behind the port is not a keyboard there
  120. * won't be 2nd byte of ID response.
  121. */
  122. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  123. serio_pause_rx(ps2dev->serio);
  124. ps2dev->flags = ps2dev->cmdcnt = 0;
  125. serio_continue_rx(ps2dev->serio);
  126. timeout = 0;
  127. }
  128. break;
  129. default:
  130. break;
  131. }
  132. return timeout;
  133. }
  134. /*
  135. * ps2_command() sends a command and its parameters to the mouse,
  136. * then waits for the response and puts it in the param array.
  137. *
  138. * ps2_command() can only be called from a process context
  139. */
  140. int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  141. {
  142. int timeout;
  143. int send = (command >> 12) & 0xf;
  144. int receive = (command >> 8) & 0xf;
  145. int rc = -1;
  146. int i;
  147. if (receive > sizeof(ps2dev->cmdbuf)) {
  148. WARN_ON(1);
  149. return -1;
  150. }
  151. if (send && !param) {
  152. WARN_ON(1);
  153. return -1;
  154. }
  155. mutex_lock_nested(&ps2dev->cmd_mutex, SINGLE_DEPTH_NESTING);
  156. serio_pause_rx(ps2dev->serio);
  157. ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
  158. ps2dev->cmdcnt = receive;
  159. if (receive && param)
  160. for (i = 0; i < receive; i++)
  161. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  162. serio_continue_rx(ps2dev->serio);
  163. /*
  164. * Some devices (Synaptics) peform the reset before
  165. * ACKing the reset command, and so it can take a long
  166. * time before the ACK arrrives.
  167. */
  168. if (ps2_sendbyte(ps2dev, command & 0xff,
  169. command == PS2_CMD_RESET_BAT ? 1000 : 200))
  170. goto out;
  171. for (i = 0; i < send; i++)
  172. if (ps2_sendbyte(ps2dev, param[i], 200))
  173. goto out;
  174. /*
  175. * The reset command takes a long time to execute.
  176. */
  177. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  178. timeout = wait_event_timeout(ps2dev->wait,
  179. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  180. if (ps2dev->cmdcnt && timeout > 0) {
  181. timeout = ps2_adjust_timeout(ps2dev, command, timeout);
  182. wait_event_timeout(ps2dev->wait,
  183. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  184. }
  185. if (param)
  186. for (i = 0; i < receive; i++)
  187. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  188. if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
  189. goto out;
  190. rc = 0;
  191. out:
  192. serio_pause_rx(ps2dev->serio);
  193. ps2dev->flags = 0;
  194. serio_continue_rx(ps2dev->serio);
  195. mutex_unlock(&ps2dev->cmd_mutex);
  196. return rc;
  197. }
  198. /*
  199. * ps2_execute_scheduled_command() sends a command, previously scheduled by
  200. * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
  201. */
  202. static void ps2_execute_scheduled_command(void *data)
  203. {
  204. struct ps2work *ps2work = data;
  205. ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
  206. kfree(ps2work);
  207. }
  208. /*
  209. * ps2_schedule_command() allows to schedule delayed execution of a PS/2
  210. * command and can be used to issue a command from an interrupt or softirq
  211. * context.
  212. */
  213. int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  214. {
  215. struct ps2work *ps2work;
  216. int send = (command >> 12) & 0xf;
  217. int receive = (command >> 8) & 0xf;
  218. if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
  219. return -1;
  220. memset(ps2work, 0, sizeof(struct ps2work));
  221. ps2work->ps2dev = ps2dev;
  222. ps2work->command = command;
  223. memcpy(ps2work->param, param, send);
  224. INIT_WORK(&ps2work->work, ps2_execute_scheduled_command, ps2work);
  225. if (!schedule_work(&ps2work->work)) {
  226. kfree(ps2work);
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. /*
  232. * ps2_init() initializes ps2dev structure
  233. */
  234. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  235. {
  236. mutex_init(&ps2dev->cmd_mutex);
  237. init_waitqueue_head(&ps2dev->wait);
  238. ps2dev->serio = serio;
  239. }
  240. /*
  241. * ps2_handle_ack() is supposed to be used in interrupt handler
  242. * to properly process ACK/NAK of a command from a PS/2 device.
  243. */
  244. int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
  245. {
  246. switch (data) {
  247. case PS2_RET_ACK:
  248. ps2dev->nak = 0;
  249. break;
  250. case PS2_RET_NAK:
  251. ps2dev->nak = 1;
  252. break;
  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 && ps2dev->cmdcnt)
  269. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  270. ps2dev->flags &= ~PS2_FLAG_ACK;
  271. wake_up(&ps2dev->wait);
  272. if (data != PS2_RET_ACK)
  273. ps2_handle_response(ps2dev, data);
  274. return 1;
  275. }
  276. /*
  277. * ps2_handle_response() is supposed to be used in interrupt handler
  278. * to properly store device's response to a command and notify process
  279. * waiting for completion of the command.
  280. */
  281. int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
  282. {
  283. if (ps2dev->cmdcnt)
  284. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  285. if (ps2dev->flags & PS2_FLAG_CMD1) {
  286. ps2dev->flags &= ~PS2_FLAG_CMD1;
  287. if (ps2dev->cmdcnt)
  288. wake_up(&ps2dev->wait);
  289. }
  290. if (!ps2dev->cmdcnt) {
  291. ps2dev->flags &= ~PS2_FLAG_CMD;
  292. wake_up(&ps2dev->wait);
  293. }
  294. return 1;
  295. }
  296. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  297. {
  298. if (ps2dev->flags & PS2_FLAG_ACK)
  299. ps2dev->nak = 1;
  300. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  301. wake_up(&ps2dev->wait);
  302. ps2dev->flags = 0;
  303. }