libps2.c 8.9 KB

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