libps2.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_command);
  28. EXPORT_SYMBOL(ps2_schedule_command);
  29. EXPORT_SYMBOL(ps2_handle_ack);
  30. EXPORT_SYMBOL(ps2_handle_response);
  31. EXPORT_SYMBOL(ps2_cmd_aborted);
  32. /* Work structure to schedule execution of a command */
  33. struct ps2work {
  34. struct work_struct work;
  35. struct ps2dev *ps2dev;
  36. int command;
  37. unsigned char param[0];
  38. };
  39. /*
  40. * ps2_sendbyte() sends a byte to the mouse, and waits for acknowledge.
  41. * It doesn't handle retransmission, though it could - because when there would
  42. * be need for retransmissions, the mouse has to be replaced anyway.
  43. *
  44. * ps2_sendbyte() can only be called from a process context
  45. */
  46. int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
  47. {
  48. serio_pause_rx(ps2dev->serio);
  49. ps2dev->nak = 1;
  50. ps2dev->flags |= PS2_FLAG_ACK;
  51. serio_continue_rx(ps2dev->serio);
  52. if (serio_write(ps2dev->serio, byte) == 0)
  53. wait_event_timeout(ps2dev->wait,
  54. !(ps2dev->flags & PS2_FLAG_ACK),
  55. msecs_to_jiffies(timeout));
  56. serio_pause_rx(ps2dev->serio);
  57. ps2dev->flags &= ~PS2_FLAG_ACK;
  58. serio_continue_rx(ps2dev->serio);
  59. return -ps2dev->nak;
  60. }
  61. /*
  62. * ps2_command() sends a command and its parameters to the mouse,
  63. * then waits for the response and puts it in the param array.
  64. *
  65. * ps2_command() can only be called from a process context
  66. */
  67. int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  68. {
  69. int timeout;
  70. int send = (command >> 12) & 0xf;
  71. int receive = (command >> 8) & 0xf;
  72. int rc = -1;
  73. int i;
  74. down(&ps2dev->cmd_sem);
  75. serio_pause_rx(ps2dev->serio);
  76. ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
  77. ps2dev->cmdcnt = receive;
  78. if (receive && param)
  79. for (i = 0; i < receive; i++)
  80. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  81. serio_continue_rx(ps2dev->serio);
  82. /*
  83. * Some devices (Synaptics) peform the reset before
  84. * ACKing the reset command, and so it can take a long
  85. * time before the ACK arrrives.
  86. */
  87. if (command & 0xff)
  88. if (ps2_sendbyte(ps2dev, command & 0xff,
  89. command == PS2_CMD_RESET_BAT ? 1000 : 200))
  90. goto out;
  91. for (i = 0; i < send; i++)
  92. if (ps2_sendbyte(ps2dev, param[i], 200))
  93. goto out;
  94. /*
  95. * The reset command takes a long time to execute.
  96. */
  97. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  98. timeout = wait_event_timeout(ps2dev->wait,
  99. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  100. if (ps2dev->cmdcnt && timeout > 0) {
  101. if (command == PS2_CMD_RESET_BAT && timeout > msecs_to_jiffies(100)) {
  102. /*
  103. * Device has sent the first response byte
  104. * after a reset command, reset is thus done,
  105. * shorten the timeout. The next byte will come
  106. * soon (keyboard) or not at all (mouse).
  107. */
  108. timeout = msecs_to_jiffies(100);
  109. }
  110. if (command == PS2_CMD_GETID &&
  111. ps2dev->cmdbuf[receive - 1] != 0xab && /* Regular keyboards */
  112. ps2dev->cmdbuf[receive - 1] != 0xac && /* NCD Sun keyboard */
  113. ps2dev->cmdbuf[receive - 1] != 0x2b && /* Trust keyboard, translated */
  114. ps2dev->cmdbuf[receive - 1] != 0x5d && /* Trust keyboard */
  115. ps2dev->cmdbuf[receive - 1] != 0x60 && /* NMB SGI keyboard, translated */
  116. ps2dev->cmdbuf[receive - 1] != 0x47) { /* NMB SGI keyboard */
  117. /*
  118. * Device behind the port is not a keyboard
  119. * so we don't need to wait for the 2nd byte
  120. * of ID response.
  121. */
  122. serio_pause_rx(ps2dev->serio);
  123. ps2dev->flags = ps2dev->cmdcnt = 0;
  124. serio_continue_rx(ps2dev->serio);
  125. }
  126. wait_event_timeout(ps2dev->wait,
  127. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  128. }
  129. if (param)
  130. for (i = 0; i < receive; i++)
  131. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  132. if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
  133. goto out;
  134. rc = 0;
  135. out:
  136. serio_pause_rx(ps2dev->serio);
  137. ps2dev->flags = 0;
  138. serio_continue_rx(ps2dev->serio);
  139. up(&ps2dev->cmd_sem);
  140. return rc;
  141. }
  142. /*
  143. * ps2_execute_scheduled_command() sends a command, previously scheduled by
  144. * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
  145. */
  146. static void ps2_execute_scheduled_command(void *data)
  147. {
  148. struct ps2work *ps2work = data;
  149. ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
  150. kfree(ps2work);
  151. }
  152. /*
  153. * ps2_schedule_command() allows to schedule delayed execution of a PS/2
  154. * command and can be used to issue a command from an interrupt or softirq
  155. * context.
  156. */
  157. int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  158. {
  159. struct ps2work *ps2work;
  160. int send = (command >> 12) & 0xf;
  161. int receive = (command >> 8) & 0xf;
  162. if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
  163. return -1;
  164. memset(ps2work, 0, sizeof(struct ps2work));
  165. ps2work->ps2dev = ps2dev;
  166. ps2work->command = command;
  167. memcpy(ps2work->param, param, send);
  168. INIT_WORK(&ps2work->work, ps2_execute_scheduled_command, ps2work);
  169. if (!schedule_work(&ps2work->work)) {
  170. kfree(ps2work);
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. /*
  176. * ps2_init() initializes ps2dev structure
  177. */
  178. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  179. {
  180. init_MUTEX(&ps2dev->cmd_sem);
  181. init_waitqueue_head(&ps2dev->wait);
  182. ps2dev->serio = serio;
  183. }
  184. /*
  185. * ps2_handle_ack() is supposed to be used in interrupt handler
  186. * to properly process ACK/NAK of a command from a PS/2 device.
  187. */
  188. int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
  189. {
  190. switch (data) {
  191. case PS2_RET_ACK:
  192. ps2dev->nak = 0;
  193. break;
  194. case PS2_RET_NAK:
  195. ps2dev->nak = 1;
  196. break;
  197. /*
  198. * Workaround for mice which don't ACK the Get ID command.
  199. * These are valid mouse IDs that we recognize.
  200. */
  201. case 0x00:
  202. case 0x03:
  203. case 0x04:
  204. if (ps2dev->flags & PS2_FLAG_WAITID) {
  205. ps2dev->nak = 0;
  206. break;
  207. }
  208. /* Fall through */
  209. default:
  210. return 0;
  211. }
  212. if (!ps2dev->nak && ps2dev->cmdcnt)
  213. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  214. ps2dev->flags &= ~PS2_FLAG_ACK;
  215. wake_up(&ps2dev->wait);
  216. if (data != PS2_RET_ACK)
  217. ps2_handle_response(ps2dev, data);
  218. return 1;
  219. }
  220. /*
  221. * ps2_handle_response() is supposed to be used in interrupt handler
  222. * to properly store device's response to a command and notify process
  223. * waiting for completion of the command.
  224. */
  225. int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
  226. {
  227. if (ps2dev->cmdcnt)
  228. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  229. if (ps2dev->flags & PS2_FLAG_CMD1) {
  230. ps2dev->flags &= ~PS2_FLAG_CMD1;
  231. if (ps2dev->cmdcnt)
  232. wake_up(&ps2dev->wait);
  233. }
  234. if (!ps2dev->cmdcnt) {
  235. ps2dev->flags &= ~PS2_FLAG_CMD;
  236. wake_up(&ps2dev->wait);
  237. }
  238. return 1;
  239. }
  240. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  241. {
  242. if (ps2dev->flags & PS2_FLAG_ACK)
  243. ps2dev->nak = 1;
  244. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  245. wake_up(&ps2dev->wait);
  246. ps2dev->flags = 0;
  247. }