libps2.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. EXPORT_SYMBOL(ps2_is_keyboard_id);
  34. /* Work structure to schedule execution of a command */
  35. struct ps2work {
  36. struct work_struct work;
  37. struct ps2dev *ps2dev;
  38. int command;
  39. unsigned char param[0];
  40. };
  41. /*
  42. * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
  43. * It doesn't handle retransmission, though it could - because if there
  44. * is a need for retransmissions device has to be replaced anyway.
  45. *
  46. * ps2_sendbyte() can only be called from a process context.
  47. */
  48. int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
  49. {
  50. serio_pause_rx(ps2dev->serio);
  51. ps2dev->nak = 1;
  52. ps2dev->flags |= PS2_FLAG_ACK;
  53. serio_continue_rx(ps2dev->serio);
  54. if (serio_write(ps2dev->serio, byte) == 0)
  55. wait_event_timeout(ps2dev->wait,
  56. !(ps2dev->flags & PS2_FLAG_ACK),
  57. msecs_to_jiffies(timeout));
  58. serio_pause_rx(ps2dev->serio);
  59. ps2dev->flags &= ~PS2_FLAG_ACK;
  60. serio_continue_rx(ps2dev->serio);
  61. return -ps2dev->nak;
  62. }
  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. mutex_lock(&ps2dev->cmd_mutex);
  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. mutex_unlock(&ps2dev->cmd_mutex);
  82. }
  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. const static 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. /*
  100. * ps2_adjust_timeout() is called after receiving 1st byte of command
  101. * response and tries to reduce remaining timeout to speed up command
  102. * completion.
  103. */
  104. static int ps2_adjust_timeout(struct ps2dev *ps2dev, int command, int timeout)
  105. {
  106. switch (command) {
  107. case PS2_CMD_RESET_BAT:
  108. /*
  109. * Device has sent the first response byte after
  110. * reset command, reset is thus done, so we can
  111. * shorten the timeout.
  112. * The next byte will come soon (keyboard) or not
  113. * at all (mouse).
  114. */
  115. if (timeout > msecs_to_jiffies(100))
  116. timeout = msecs_to_jiffies(100);
  117. break;
  118. case PS2_CMD_GETID:
  119. /*
  120. * Microsoft Natural Elite keyboard responds to
  121. * the GET ID command as it were a mouse, with
  122. * a single byte. Fail the command so atkbd will
  123. * use alternative probe to detect it.
  124. */
  125. if (ps2dev->cmdbuf[1] == 0xaa) {
  126. serio_pause_rx(ps2dev->serio);
  127. ps2dev->flags = 0;
  128. serio_continue_rx(ps2dev->serio);
  129. timeout = 0;
  130. }
  131. /*
  132. * If device behind the port is not a keyboard there
  133. * won't be 2nd byte of ID response.
  134. */
  135. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  136. serio_pause_rx(ps2dev->serio);
  137. ps2dev->flags = ps2dev->cmdcnt = 0;
  138. serio_continue_rx(ps2dev->serio);
  139. timeout = 0;
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. return timeout;
  146. }
  147. /*
  148. * ps2_command() sends a command and its parameters to the mouse,
  149. * then waits for the response and puts it in the param array.
  150. *
  151. * ps2_command() can only be called from a process context
  152. */
  153. int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  154. {
  155. int timeout;
  156. int send = (command >> 12) & 0xf;
  157. int receive = (command >> 8) & 0xf;
  158. int rc = -1;
  159. int i;
  160. if (receive > sizeof(ps2dev->cmdbuf)) {
  161. WARN_ON(1);
  162. return -1;
  163. }
  164. if (send && !param) {
  165. WARN_ON(1);
  166. return -1;
  167. }
  168. mutex_lock_nested(&ps2dev->cmd_mutex, SINGLE_DEPTH_NESTING);
  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 arrrives.
  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 && timeout > 0) {
  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. mutex_unlock(&ps2dev->cmd_mutex);
  209. return rc;
  210. }
  211. /*
  212. * ps2_execute_scheduled_command() sends a command, previously scheduled by
  213. * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
  214. */
  215. static void ps2_execute_scheduled_command(void *data)
  216. {
  217. struct ps2work *ps2work = data;
  218. ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
  219. kfree(ps2work);
  220. }
  221. /*
  222. * ps2_schedule_command() allows to schedule delayed execution of a PS/2
  223. * command and can be used to issue a command from an interrupt or softirq
  224. * context.
  225. */
  226. int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
  227. {
  228. struct ps2work *ps2work;
  229. int send = (command >> 12) & 0xf;
  230. int receive = (command >> 8) & 0xf;
  231. if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
  232. return -1;
  233. memset(ps2work, 0, sizeof(struct ps2work));
  234. ps2work->ps2dev = ps2dev;
  235. ps2work->command = command;
  236. memcpy(ps2work->param, param, send);
  237. INIT_WORK(&ps2work->work, ps2_execute_scheduled_command, ps2work);
  238. if (!schedule_work(&ps2work->work)) {
  239. kfree(ps2work);
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. /*
  245. * ps2_init() initializes ps2dev structure
  246. */
  247. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  248. {
  249. mutex_init(&ps2dev->cmd_mutex);
  250. init_waitqueue_head(&ps2dev->wait);
  251. ps2dev->serio = serio;
  252. }
  253. /*
  254. * ps2_handle_ack() is supposed to be used in interrupt handler
  255. * to properly process ACK/NAK of a command from a PS/2 device.
  256. */
  257. int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
  258. {
  259. switch (data) {
  260. case PS2_RET_ACK:
  261. ps2dev->nak = 0;
  262. break;
  263. case PS2_RET_NAK:
  264. ps2dev->nak = 1;
  265. break;
  266. /*
  267. * Workaround for mice which don't ACK the Get ID command.
  268. * These are valid mouse IDs that we recognize.
  269. */
  270. case 0x00:
  271. case 0x03:
  272. case 0x04:
  273. if (ps2dev->flags & PS2_FLAG_WAITID) {
  274. ps2dev->nak = 0;
  275. break;
  276. }
  277. /* Fall through */
  278. default:
  279. return 0;
  280. }
  281. if (!ps2dev->nak && ps2dev->cmdcnt)
  282. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  283. ps2dev->flags &= ~PS2_FLAG_ACK;
  284. wake_up(&ps2dev->wait);
  285. if (data != PS2_RET_ACK)
  286. ps2_handle_response(ps2dev, data);
  287. return 1;
  288. }
  289. /*
  290. * ps2_handle_response() is supposed to be used in interrupt handler
  291. * to properly store device's response to a command and notify process
  292. * waiting for completion of the command.
  293. */
  294. int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
  295. {
  296. if (ps2dev->cmdcnt)
  297. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  298. if (ps2dev->flags & PS2_FLAG_CMD1) {
  299. ps2dev->flags &= ~PS2_FLAG_CMD1;
  300. if (ps2dev->cmdcnt)
  301. wake_up(&ps2dev->wait);
  302. }
  303. if (!ps2dev->cmdcnt) {
  304. ps2dev->flags &= ~PS2_FLAG_CMD;
  305. wake_up(&ps2dev->wait);
  306. }
  307. return 1;
  308. }
  309. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  310. {
  311. if (ps2dev->flags & PS2_FLAG_ACK)
  312. ps2dev->nak = 1;
  313. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  314. wake_up(&ps2dev->wait);
  315. ps2dev->flags = 0;
  316. }