libps2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/input.h>
  17. #include <linux/serio.h>
  18. #include <linux/i8042.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. static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte,
  25. unsigned int timeout, unsigned int max_attempts)
  26. __releases(&ps2dev->serio->lock) __acquires(&ps2dev->serio->lock)
  27. {
  28. int attempt = 0;
  29. int error;
  30. lockdep_assert_held(&ps2dev->serio->lock);
  31. do {
  32. ps2dev->nak = 1;
  33. ps2dev->flags |= PS2_FLAG_ACK;
  34. serio_continue_rx(ps2dev->serio);
  35. error = serio_write(ps2dev->serio, byte);
  36. if (error)
  37. dev_dbg(&ps2dev->serio->dev,
  38. "failed to write %#02x: %d\n", byte, error);
  39. else
  40. wait_event_timeout(ps2dev->wait,
  41. !(ps2dev->flags & PS2_FLAG_ACK),
  42. msecs_to_jiffies(timeout));
  43. serio_pause_rx(ps2dev->serio);
  44. } while (ps2dev->nak == PS2_RET_NAK && ++attempt < max_attempts);
  45. ps2dev->flags &= ~PS2_FLAG_ACK;
  46. if (!error) {
  47. switch (ps2dev->nak) {
  48. case 0:
  49. break;
  50. case PS2_RET_NAK:
  51. error = -EAGAIN;
  52. break;
  53. case PS2_RET_ERR:
  54. error = -EPROTO;
  55. break;
  56. default:
  57. error = -EIO;
  58. break;
  59. }
  60. }
  61. if (error || attempt > 1)
  62. dev_dbg(&ps2dev->serio->dev,
  63. "%02x - %d (%x), attempt %d\n",
  64. byte, error, ps2dev->nak, attempt);
  65. return error;
  66. }
  67. /*
  68. * ps2_sendbyte() sends a byte to the device and waits for acknowledge.
  69. * It doesn't handle retransmission, the caller is expected to handle
  70. * it when needed.
  71. *
  72. * ps2_sendbyte() can only be called from a process context.
  73. */
  74. int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout)
  75. {
  76. int retval;
  77. serio_pause_rx(ps2dev->serio);
  78. retval = ps2_do_sendbyte(ps2dev, byte, timeout, 1);
  79. dev_dbg(&ps2dev->serio->dev, "%02x - %x\n", byte, ps2dev->nak);
  80. serio_continue_rx(ps2dev->serio);
  81. return retval;
  82. }
  83. EXPORT_SYMBOL(ps2_sendbyte);
  84. void ps2_begin_command(struct ps2dev *ps2dev)
  85. {
  86. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  87. mutex_lock(m);
  88. }
  89. EXPORT_SYMBOL(ps2_begin_command);
  90. void ps2_end_command(struct ps2dev *ps2dev)
  91. {
  92. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  93. mutex_unlock(m);
  94. }
  95. EXPORT_SYMBOL(ps2_end_command);
  96. /*
  97. * ps2_drain() waits for device to transmit requested number of bytes
  98. * and discards them.
  99. */
  100. void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout)
  101. {
  102. if (maxbytes > sizeof(ps2dev->cmdbuf)) {
  103. WARN_ON(1);
  104. maxbytes = sizeof(ps2dev->cmdbuf);
  105. }
  106. ps2_begin_command(ps2dev);
  107. serio_pause_rx(ps2dev->serio);
  108. ps2dev->flags = PS2_FLAG_CMD;
  109. ps2dev->cmdcnt = maxbytes;
  110. serio_continue_rx(ps2dev->serio);
  111. wait_event_timeout(ps2dev->wait,
  112. !(ps2dev->flags & PS2_FLAG_CMD),
  113. msecs_to_jiffies(timeout));
  114. ps2_end_command(ps2dev);
  115. }
  116. EXPORT_SYMBOL(ps2_drain);
  117. /*
  118. * ps2_is_keyboard_id() checks received ID byte against the list of
  119. * known keyboard IDs.
  120. */
  121. bool ps2_is_keyboard_id(u8 id_byte)
  122. {
  123. static const u8 keyboard_ids[] = {
  124. 0xab, /* Regular keyboards */
  125. 0xac, /* NCD Sun keyboard */
  126. 0x2b, /* Trust keyboard, translated */
  127. 0x5d, /* Trust keyboard */
  128. 0x60, /* NMB SGI keyboard, translated */
  129. 0x47, /* NMB SGI keyboard */
  130. };
  131. return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
  132. }
  133. EXPORT_SYMBOL(ps2_is_keyboard_id);
  134. /*
  135. * ps2_adjust_timeout() is called after receiving 1st byte of command
  136. * response and tries to reduce remaining timeout to speed up command
  137. * completion.
  138. */
  139. static int ps2_adjust_timeout(struct ps2dev *ps2dev,
  140. unsigned int command, unsigned int timeout)
  141. {
  142. switch (command) {
  143. case PS2_CMD_RESET_BAT:
  144. /*
  145. * Device has sent the first response byte after
  146. * reset command, reset is thus done, so we can
  147. * shorten the timeout.
  148. * The next byte will come soon (keyboard) or not
  149. * at all (mouse).
  150. */
  151. if (timeout > msecs_to_jiffies(100))
  152. timeout = msecs_to_jiffies(100);
  153. break;
  154. case PS2_CMD_GETID:
  155. /*
  156. * Microsoft Natural Elite keyboard responds to
  157. * the GET ID command as it were a mouse, with
  158. * a single byte. Fail the command so atkbd will
  159. * use alternative probe to detect it.
  160. */
  161. if (ps2dev->cmdbuf[1] == 0xaa) {
  162. serio_pause_rx(ps2dev->serio);
  163. ps2dev->flags = 0;
  164. serio_continue_rx(ps2dev->serio);
  165. timeout = 0;
  166. }
  167. /*
  168. * If device behind the port is not a keyboard there
  169. * won't be 2nd byte of ID response.
  170. */
  171. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  172. serio_pause_rx(ps2dev->serio);
  173. ps2dev->flags = ps2dev->cmdcnt = 0;
  174. serio_continue_rx(ps2dev->serio);
  175. timeout = 0;
  176. }
  177. break;
  178. default:
  179. break;
  180. }
  181. return timeout;
  182. }
  183. /*
  184. * ps2_command() sends a command and its parameters to the mouse,
  185. * then waits for the response and puts it in the param array.
  186. *
  187. * ps2_command() can only be called from a process context
  188. */
  189. int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
  190. {
  191. unsigned int timeout;
  192. unsigned int send = (command >> 12) & 0xf;
  193. unsigned int receive = (command >> 8) & 0xf;
  194. int rc;
  195. int i;
  196. u8 send_param[16];
  197. if (receive > sizeof(ps2dev->cmdbuf)) {
  198. WARN_ON(1);
  199. return -EINVAL;
  200. }
  201. if (send && !param) {
  202. WARN_ON(1);
  203. return -EINVAL;
  204. }
  205. memcpy(send_param, param, send);
  206. serio_pause_rx(ps2dev->serio);
  207. ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
  208. ps2dev->cmdcnt = receive;
  209. if (receive && param)
  210. for (i = 0; i < receive; i++)
  211. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  212. /* Signal that we are sending the command byte */
  213. ps2dev->flags |= PS2_FLAG_ACK_CMD;
  214. /*
  215. * Some devices (Synaptics) peform the reset before
  216. * ACKing the reset command, and so it can take a long
  217. * time before the ACK arrives.
  218. */
  219. timeout = command == PS2_CMD_RESET_BAT ? 1000 : 200;
  220. rc = ps2_do_sendbyte(ps2dev, command & 0xff, timeout, 2);
  221. if (rc)
  222. goto out_reset_flags;
  223. /* Now we are sending command parameters, if any */
  224. ps2dev->flags &= ~PS2_FLAG_ACK_CMD;
  225. for (i = 0; i < send; i++) {
  226. rc = ps2_do_sendbyte(ps2dev, param[i], 200, 2);
  227. if (rc)
  228. goto out_reset_flags;
  229. }
  230. serio_continue_rx(ps2dev->serio);
  231. /*
  232. * The reset command takes a long time to execute.
  233. */
  234. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  235. timeout = wait_event_timeout(ps2dev->wait,
  236. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  237. if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
  238. timeout = ps2_adjust_timeout(ps2dev, command, timeout);
  239. wait_event_timeout(ps2dev->wait,
  240. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  241. }
  242. serio_pause_rx(ps2dev->serio);
  243. if (param)
  244. for (i = 0; i < receive; i++)
  245. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  246. if (ps2dev->cmdcnt &&
  247. (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) {
  248. rc = -EPROTO;
  249. goto out_reset_flags;
  250. }
  251. rc = 0;
  252. out_reset_flags:
  253. ps2dev->flags = 0;
  254. serio_continue_rx(ps2dev->serio);
  255. dev_dbg(&ps2dev->serio->dev,
  256. "%02x [%*ph] - %x/%08lx [%*ph]\n",
  257. command & 0xff, send, send_param,
  258. ps2dev->nak, ps2dev->flags,
  259. receive, param ?: send_param);
  260. /*
  261. * ps_command() handles resends itself, so do not leak -EAGAIN
  262. * to the callers.
  263. */
  264. return rc != -EAGAIN ? rc : -EPROTO;
  265. }
  266. EXPORT_SYMBOL(__ps2_command);
  267. int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
  268. {
  269. int rc;
  270. ps2_begin_command(ps2dev);
  271. rc = __ps2_command(ps2dev, param, command);
  272. ps2_end_command(ps2dev);
  273. return rc;
  274. }
  275. EXPORT_SYMBOL(ps2_command);
  276. /*
  277. * ps2_sliced_command() sends an extended PS/2 command to the mouse
  278. * using sliced syntax, understood by advanced devices, such as Logitech
  279. * or Synaptics touchpads. The command is encoded as:
  280. * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
  281. * is the command.
  282. */
  283. int ps2_sliced_command(struct ps2dev *ps2dev, u8 command)
  284. {
  285. int i;
  286. int retval;
  287. ps2_begin_command(ps2dev);
  288. retval = __ps2_command(ps2dev, NULL, PS2_CMD_SETSCALE11);
  289. if (retval)
  290. goto out;
  291. for (i = 6; i >= 0; i -= 2) {
  292. u8 d = (command >> i) & 3;
  293. retval = __ps2_command(ps2dev, &d, PS2_CMD_SETRES);
  294. if (retval)
  295. break;
  296. }
  297. out:
  298. dev_dbg(&ps2dev->serio->dev, "%02x - %d\n", command, retval);
  299. ps2_end_command(ps2dev);
  300. return retval;
  301. }
  302. EXPORT_SYMBOL(ps2_sliced_command);
  303. /*
  304. * ps2_init() initializes ps2dev structure
  305. */
  306. void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
  307. {
  308. mutex_init(&ps2dev->cmd_mutex);
  309. lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
  310. init_waitqueue_head(&ps2dev->wait);
  311. ps2dev->serio = serio;
  312. }
  313. EXPORT_SYMBOL(ps2_init);
  314. /*
  315. * ps2_handle_ack() is supposed to be used in interrupt handler
  316. * to properly process ACK/NAK of a command from a PS/2 device.
  317. */
  318. bool ps2_handle_ack(struct ps2dev *ps2dev, u8 data)
  319. {
  320. switch (data) {
  321. case PS2_RET_ACK:
  322. ps2dev->nak = 0;
  323. break;
  324. case PS2_RET_NAK:
  325. ps2dev->flags |= PS2_FLAG_NAK;
  326. ps2dev->nak = PS2_RET_NAK;
  327. break;
  328. case PS2_RET_ERR:
  329. if (ps2dev->flags & PS2_FLAG_NAK) {
  330. ps2dev->flags &= ~PS2_FLAG_NAK;
  331. ps2dev->nak = PS2_RET_ERR;
  332. break;
  333. }
  334. /*
  335. * Workaround for mice which don't ACK the Get ID command.
  336. * These are valid mouse IDs that we recognize.
  337. */
  338. case 0x00:
  339. case 0x03:
  340. case 0x04:
  341. if (ps2dev->flags & PS2_FLAG_WAITID) {
  342. ps2dev->nak = 0;
  343. break;
  344. }
  345. /* Fall through */
  346. default:
  347. /*
  348. * Do not signal errors if we get unexpected reply while
  349. * waiting for an ACK to the initial (first) command byte:
  350. * the device might not be quiesced yet and continue
  351. * delivering data.
  352. * Note that we reset PS2_FLAG_WAITID flag, so the workaround
  353. * for mice not acknowledging the Get ID command only triggers
  354. * on the 1st byte; if device spews data we really want to see
  355. * a real ACK from it.
  356. */
  357. dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data);
  358. ps2dev->flags &= ~PS2_FLAG_WAITID;
  359. return ps2dev->flags & PS2_FLAG_ACK_CMD;
  360. }
  361. if (!ps2dev->nak) {
  362. ps2dev->flags &= ~PS2_FLAG_NAK;
  363. if (ps2dev->cmdcnt)
  364. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  365. }
  366. ps2dev->flags &= ~PS2_FLAG_ACK;
  367. wake_up(&ps2dev->wait);
  368. if (data != PS2_RET_ACK)
  369. ps2_handle_response(ps2dev, data);
  370. return true;
  371. }
  372. EXPORT_SYMBOL(ps2_handle_ack);
  373. /*
  374. * ps2_handle_response() is supposed to be used in interrupt handler
  375. * to properly store device's response to a command and notify process
  376. * waiting for completion of the command.
  377. */
  378. bool ps2_handle_response(struct ps2dev *ps2dev, u8 data)
  379. {
  380. if (ps2dev->cmdcnt)
  381. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  382. if (ps2dev->flags & PS2_FLAG_CMD1) {
  383. ps2dev->flags &= ~PS2_FLAG_CMD1;
  384. if (ps2dev->cmdcnt)
  385. wake_up(&ps2dev->wait);
  386. }
  387. if (!ps2dev->cmdcnt) {
  388. ps2dev->flags &= ~PS2_FLAG_CMD;
  389. wake_up(&ps2dev->wait);
  390. }
  391. return true;
  392. }
  393. EXPORT_SYMBOL(ps2_handle_response);
  394. void ps2_cmd_aborted(struct ps2dev *ps2dev)
  395. {
  396. if (ps2dev->flags & PS2_FLAG_ACK)
  397. ps2dev->nak = 1;
  398. if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  399. wake_up(&ps2dev->wait);
  400. /* reset all flags except last nack */
  401. ps2dev->flags &= PS2_FLAG_NAK;
  402. }
  403. EXPORT_SYMBOL(ps2_cmd_aborted);