libps2.c 8.9 KB

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