libps2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PS/2 driver library
  4. *
  5. * Copyright (c) 1999-2002 Vojtech Pavlik
  6. * Copyright (c) 2004 Dmitry Torokhov
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/input.h>
  13. #include <linux/kmsan-checks.h>
  14. #include <linux/serio.h>
  15. #include <linux/i8042.h>
  16. #include <linux/libps2.h>
  17. #define DRIVER_DESC "PS/2 driver library"
  18. #define PS2_CMD_SETSCALE11 0x00e6
  19. #define PS2_CMD_SETRES 0x10e8
  20. #define PS2_CMD_EX_SETLEDS 0x20eb
  21. #define PS2_CMD_SETLEDS 0x10ed
  22. #define PS2_CMD_GETID 0x02f2
  23. #define PS2_CMD_SETREP 0x10f3 /* Set repeat rate/set report rate */
  24. #define PS2_CMD_RESET_BAT 0x02ff
  25. #define PS2_RET_BAT 0xaa
  26. #define PS2_RET_ID 0x00
  27. #define PS2_RET_ACK 0xfa
  28. #define PS2_RET_NAK 0xfe
  29. #define PS2_RET_ERR 0xfc
  30. #define PS2_FLAG_ACK BIT(0) /* Waiting for ACK/NAK */
  31. #define PS2_FLAG_CMD BIT(1) /* Waiting for a command to finish */
  32. #define PS2_FLAG_CMD1 BIT(2) /* Waiting for the first byte of command response */
  33. #define PS2_FLAG_WAITID BIT(3) /* Command executing is GET ID */
  34. #define PS2_FLAG_NAK BIT(4) /* Last transmission was NAKed */
  35. #define PS2_FLAG_PASS_NOACK BIT(5) /* Pass non-ACK byte to receive handler */
  36. static int ps2_do_sendbyte(struct ps2dev *ps2dev, u8 byte,
  37. unsigned int timeout, unsigned int max_attempts)
  38. __releases(&ps2dev->serio->lock) __acquires(&ps2dev->serio->lock)
  39. {
  40. int attempt = 0;
  41. int error;
  42. lockdep_assert_held(&ps2dev->serio->lock);
  43. do {
  44. ps2dev->nak = 1;
  45. ps2dev->flags |= PS2_FLAG_ACK;
  46. serio_continue_rx(ps2dev->serio);
  47. error = serio_write(ps2dev->serio, byte);
  48. if (error)
  49. dev_dbg(&ps2dev->serio->dev,
  50. "failed to write %#02x: %d\n", byte, error);
  51. else
  52. wait_event_timeout(ps2dev->wait,
  53. !(ps2dev->flags & PS2_FLAG_ACK),
  54. msecs_to_jiffies(timeout));
  55. serio_pause_rx(ps2dev->serio);
  56. } while (ps2dev->nak == PS2_RET_NAK && ++attempt < max_attempts);
  57. ps2dev->flags &= ~PS2_FLAG_ACK;
  58. if (!error) {
  59. switch (ps2dev->nak) {
  60. case 0:
  61. break;
  62. case PS2_RET_NAK:
  63. error = -EAGAIN;
  64. break;
  65. case PS2_RET_ERR:
  66. error = -EPROTO;
  67. break;
  68. default:
  69. error = -EIO;
  70. break;
  71. }
  72. }
  73. if (error || attempt > 1)
  74. dev_dbg(&ps2dev->serio->dev,
  75. "%02x - %d (%x), attempt %d\n",
  76. byte, error, ps2dev->nak, attempt);
  77. return error;
  78. }
  79. /**
  80. * ps2_sendbyte - sends a byte to the device and wait for acknowledgement
  81. * @ps2dev: a PS/2 device to send the data to
  82. * @byte: data to be sent to the device
  83. * @timeout: timeout for sending the data and receiving an acknowledge
  84. *
  85. * The function doesn't handle retransmission, the caller is expected to handle
  86. * it when needed.
  87. *
  88. * ps2_sendbyte() can only be called from a process context.
  89. */
  90. int ps2_sendbyte(struct ps2dev *ps2dev, u8 byte, unsigned int timeout)
  91. {
  92. int retval;
  93. serio_pause_rx(ps2dev->serio);
  94. retval = ps2_do_sendbyte(ps2dev, byte, timeout, 1);
  95. dev_dbg(&ps2dev->serio->dev, "%02x - %x\n", byte, ps2dev->nak);
  96. serio_continue_rx(ps2dev->serio);
  97. return retval;
  98. }
  99. EXPORT_SYMBOL(ps2_sendbyte);
  100. /**
  101. * ps2_begin_command - mark beginning of execution of a complex command
  102. * @ps2dev: a PS/2 device executing the command
  103. *
  104. * Serializes a complex/compound command. Once command is finished
  105. * ps2_end_command() should be called.
  106. */
  107. void ps2_begin_command(struct ps2dev *ps2dev)
  108. {
  109. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  110. mutex_lock(m);
  111. }
  112. EXPORT_SYMBOL(ps2_begin_command);
  113. /**
  114. * ps2_end_command - mark end of execution of a complex command
  115. * @ps2dev: a PS/2 device executing the command
  116. */
  117. void ps2_end_command(struct ps2dev *ps2dev)
  118. {
  119. struct mutex *m = ps2dev->serio->ps2_cmd_mutex ?: &ps2dev->cmd_mutex;
  120. mutex_unlock(m);
  121. }
  122. EXPORT_SYMBOL(ps2_end_command);
  123. /**
  124. * ps2_drain - waits for device to transmit requested number of bytes
  125. * and discards them
  126. * @ps2dev: the PS/2 device that should be drained
  127. * @maxbytes: maximum number of bytes to be drained
  128. * @timeout: time to drain the device
  129. */
  130. void ps2_drain(struct ps2dev *ps2dev, size_t maxbytes, unsigned int timeout)
  131. {
  132. if (maxbytes > sizeof(ps2dev->cmdbuf)) {
  133. WARN_ON(1);
  134. maxbytes = sizeof(ps2dev->cmdbuf);
  135. }
  136. ps2_begin_command(ps2dev);
  137. serio_pause_rx(ps2dev->serio);
  138. ps2dev->flags = PS2_FLAG_CMD;
  139. ps2dev->cmdcnt = maxbytes;
  140. serio_continue_rx(ps2dev->serio);
  141. wait_event_timeout(ps2dev->wait,
  142. !(ps2dev->flags & PS2_FLAG_CMD),
  143. msecs_to_jiffies(timeout));
  144. ps2_end_command(ps2dev);
  145. }
  146. EXPORT_SYMBOL(ps2_drain);
  147. /**
  148. * ps2_is_keyboard_id - checks received ID byte against the list of
  149. * known keyboard IDs
  150. * @id_byte: data byte that should be checked
  151. */
  152. bool ps2_is_keyboard_id(u8 id_byte)
  153. {
  154. static const u8 keyboard_ids[] = {
  155. 0xab, /* Regular keyboards */
  156. 0xac, /* NCD Sun keyboard */
  157. 0x2b, /* Trust keyboard, translated */
  158. 0x5d, /* Trust keyboard */
  159. 0x60, /* NMB SGI keyboard, translated */
  160. 0x47, /* NMB SGI keyboard */
  161. };
  162. return memchr(keyboard_ids, id_byte, sizeof(keyboard_ids)) != NULL;
  163. }
  164. EXPORT_SYMBOL(ps2_is_keyboard_id);
  165. /*
  166. * ps2_adjust_timeout() is called after receiving 1st byte of command
  167. * response and tries to reduce remaining timeout to speed up command
  168. * completion.
  169. */
  170. static int ps2_adjust_timeout(struct ps2dev *ps2dev,
  171. unsigned int command, unsigned int timeout)
  172. {
  173. switch (command) {
  174. case PS2_CMD_RESET_BAT:
  175. /*
  176. * Device has sent the first response byte after
  177. * reset command, reset is thus done, so we can
  178. * shorten the timeout.
  179. * The next byte will come soon (keyboard) or not
  180. * at all (mouse).
  181. */
  182. if (timeout > msecs_to_jiffies(100))
  183. timeout = msecs_to_jiffies(100);
  184. break;
  185. case PS2_CMD_GETID:
  186. /*
  187. * Microsoft Natural Elite keyboard responds to
  188. * the GET ID command as it were a mouse, with
  189. * a single byte. Fail the command so atkbd will
  190. * use alternative probe to detect it.
  191. */
  192. if (ps2dev->cmdbuf[1] == 0xaa) {
  193. serio_pause_rx(ps2dev->serio);
  194. ps2dev->flags = 0;
  195. serio_continue_rx(ps2dev->serio);
  196. timeout = 0;
  197. }
  198. /*
  199. * If device behind the port is not a keyboard there
  200. * won't be 2nd byte of ID response.
  201. */
  202. if (!ps2_is_keyboard_id(ps2dev->cmdbuf[1])) {
  203. serio_pause_rx(ps2dev->serio);
  204. ps2dev->flags = ps2dev->cmdcnt = 0;
  205. serio_continue_rx(ps2dev->serio);
  206. timeout = 0;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. return timeout;
  213. }
  214. /**
  215. * __ps2_command - send a command to PS/2 device
  216. * @ps2dev: the PS/2 device that should execute the command
  217. * @param: a buffer containing parameters to be sent along with the command,
  218. * or place where the results of the command execution will be deposited,
  219. * or both
  220. * @command: command word that encodes the command itself, as well as number of
  221. * additional parameter bytes that should be sent to the device and expected
  222. * length of the command response
  223. *
  224. * Not serialized. Callers should use ps2_begin_command() and ps2_end_command()
  225. * to ensure proper serialization for complex commands.
  226. */
  227. int __ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
  228. {
  229. unsigned int timeout;
  230. unsigned int send = (command >> 12) & 0xf;
  231. unsigned int receive = (command >> 8) & 0xf;
  232. int rc;
  233. int i;
  234. u8 send_param[16];
  235. if (receive > sizeof(ps2dev->cmdbuf)) {
  236. WARN_ON(1);
  237. return -EINVAL;
  238. }
  239. if (send && !param) {
  240. WARN_ON(1);
  241. return -EINVAL;
  242. }
  243. memcpy(send_param, param, send);
  244. serio_pause_rx(ps2dev->serio);
  245. ps2dev->cmdcnt = receive;
  246. switch (command) {
  247. case PS2_CMD_GETID:
  248. /*
  249. * Some mice do not ACK the "get ID" command, prepare to
  250. * handle this.
  251. */
  252. ps2dev->flags = PS2_FLAG_WAITID;
  253. break;
  254. case PS2_CMD_SETLEDS:
  255. case PS2_CMD_EX_SETLEDS:
  256. case PS2_CMD_SETREP:
  257. ps2dev->flags = PS2_FLAG_PASS_NOACK;
  258. break;
  259. default:
  260. ps2dev->flags = 0;
  261. break;
  262. }
  263. if (receive) {
  264. /* Indicate that we expect response to the command. */
  265. ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;
  266. if (param)
  267. for (i = 0; i < receive; i++)
  268. ps2dev->cmdbuf[(receive - 1) - i] = param[i];
  269. }
  270. /*
  271. * Some devices (Synaptics) perform the reset before
  272. * ACKing the reset command, and so it can take a long
  273. * time before the ACK arrives.
  274. */
  275. timeout = command == PS2_CMD_RESET_BAT ? 1000 : 200;
  276. rc = ps2_do_sendbyte(ps2dev, command & 0xff, timeout, 2);
  277. if (rc)
  278. goto out_reset_flags;
  279. /* Send command parameters, if any. */
  280. for (i = 0; i < send; i++) {
  281. rc = ps2_do_sendbyte(ps2dev, param[i], 200, 2);
  282. if (rc)
  283. goto out_reset_flags;
  284. }
  285. serio_continue_rx(ps2dev->serio);
  286. /*
  287. * The reset command takes a long time to execute.
  288. */
  289. timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);
  290. timeout = wait_event_timeout(ps2dev->wait,
  291. !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
  292. if (ps2dev->cmdcnt && !(ps2dev->flags & PS2_FLAG_CMD1)) {
  293. timeout = ps2_adjust_timeout(ps2dev, command, timeout);
  294. wait_event_timeout(ps2dev->wait,
  295. !(ps2dev->flags & PS2_FLAG_CMD), timeout);
  296. }
  297. serio_pause_rx(ps2dev->serio);
  298. if (param) {
  299. for (i = 0; i < receive; i++)
  300. param[i] = ps2dev->cmdbuf[(receive - 1) - i];
  301. kmsan_unpoison_memory(param, receive);
  302. }
  303. if (ps2dev->cmdcnt &&
  304. (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1)) {
  305. rc = -EPROTO;
  306. goto out_reset_flags;
  307. }
  308. rc = 0;
  309. out_reset_flags:
  310. ps2dev->flags = 0;
  311. serio_continue_rx(ps2dev->serio);
  312. dev_dbg(&ps2dev->serio->dev,
  313. "%02x [%*ph] - %x/%08lx [%*ph]\n",
  314. command & 0xff, send, send_param,
  315. ps2dev->nak, ps2dev->flags,
  316. receive, param ?: send_param);
  317. /*
  318. * ps_command() handles resends itself, so do not leak -EAGAIN
  319. * to the callers.
  320. */
  321. return rc != -EAGAIN ? rc : -EPROTO;
  322. }
  323. EXPORT_SYMBOL(__ps2_command);
  324. /**
  325. * ps2_command - send a command to PS/2 device
  326. * @ps2dev: the PS/2 device that should execute the command
  327. * @param: a buffer containing parameters to be sent along with the command,
  328. * or place where the results of the command execution will be deposited,
  329. * or both
  330. * @command: command word that encodes the command itself, as well as number of
  331. * additional parameter bytes that should be sent to the device and expected
  332. * length of the command response
  333. *
  334. * Note: ps2_command() serializes the command execution so that only one
  335. * command can be executed at a time for either individual port or the entire
  336. * 8042 controller.
  337. */
  338. int ps2_command(struct ps2dev *ps2dev, u8 *param, unsigned int command)
  339. {
  340. int rc;
  341. ps2_begin_command(ps2dev);
  342. rc = __ps2_command(ps2dev, param, command);
  343. ps2_end_command(ps2dev);
  344. return rc;
  345. }
  346. EXPORT_SYMBOL(ps2_command);
  347. /**
  348. * ps2_sliced_command - sends an extended PS/2 command to a mouse
  349. * @ps2dev: the PS/2 device that should execute the command
  350. * @command: command byte
  351. *
  352. * The command is sent using "sliced" syntax understood by advanced devices,
  353. * such as Logitech or Synaptics touchpads. The command is encoded as:
  354. * 0xE6 0xE8 rr 0xE8 ss 0xE8 tt 0xE8 uu where (rr*64)+(ss*16)+(tt*4)+uu
  355. * is the command.
  356. */
  357. int ps2_sliced_command(struct ps2dev *ps2dev, u8 command)
  358. {
  359. int i;
  360. int retval;
  361. ps2_begin_command(ps2dev);
  362. retval = __ps2_command(ps2dev, NULL, PS2_CMD_SETSCALE11);
  363. if (retval)
  364. goto out;
  365. for (i = 6; i >= 0; i -= 2) {
  366. u8 d = (command >> i) & 3;
  367. retval = __ps2_command(ps2dev, &d, PS2_CMD_SETRES);
  368. if (retval)
  369. break;
  370. }
  371. out:
  372. dev_dbg(&ps2dev->serio->dev, "%02x - %d\n", command, retval);
  373. ps2_end_command(ps2dev);
  374. return retval;
  375. }
  376. EXPORT_SYMBOL(ps2_sliced_command);
  377. /**
  378. * ps2_init - initializes ps2dev structure
  379. * @ps2dev: structure to be initialized
  380. * @serio: serio port associated with the PS/2 device
  381. * @pre_receive_handler: validation handler to check basic communication state
  382. * @receive_handler: main protocol handler
  383. *
  384. * Prepares ps2dev structure for use in drivers for PS/2 devices.
  385. */
  386. void ps2_init(struct ps2dev *ps2dev, struct serio *serio,
  387. ps2_pre_receive_handler_t pre_receive_handler,
  388. ps2_receive_handler_t receive_handler)
  389. {
  390. ps2dev->pre_receive_handler = pre_receive_handler;
  391. ps2dev->receive_handler = receive_handler;
  392. mutex_init(&ps2dev->cmd_mutex);
  393. lockdep_set_subclass(&ps2dev->cmd_mutex, serio->depth);
  394. init_waitqueue_head(&ps2dev->wait);
  395. ps2dev->serio = serio;
  396. serio_set_drvdata(serio, ps2dev);
  397. }
  398. EXPORT_SYMBOL(ps2_init);
  399. /*
  400. * ps2_handle_response() stores device's response to a command and notifies
  401. * the process waiting for completion of the command. Note that there is a
  402. * distinction between waiting for the first byte of the response, and
  403. * waiting for subsequent bytes. It is done so that callers could shorten
  404. * timeouts once first byte of response is received.
  405. */
  406. static void ps2_handle_response(struct ps2dev *ps2dev, u8 data)
  407. {
  408. if (ps2dev->cmdcnt)
  409. ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;
  410. if (ps2dev->flags & PS2_FLAG_CMD1) {
  411. ps2dev->flags &= ~PS2_FLAG_CMD1;
  412. if (ps2dev->cmdcnt)
  413. wake_up(&ps2dev->wait);
  414. }
  415. if (!ps2dev->cmdcnt) {
  416. ps2dev->flags &= ~PS2_FLAG_CMD;
  417. wake_up(&ps2dev->wait);
  418. }
  419. }
  420. /*
  421. * ps2_handle_ack() processes ACK/NAK of a command from a PS/2 device,
  422. * possibly applying workarounds for mice not acknowledging the "get ID"
  423. * command.
  424. */
  425. static void ps2_handle_ack(struct ps2dev *ps2dev, u8 data)
  426. {
  427. switch (data) {
  428. case PS2_RET_ACK:
  429. ps2dev->nak = 0;
  430. break;
  431. case PS2_RET_NAK:
  432. ps2dev->flags |= PS2_FLAG_NAK;
  433. ps2dev->nak = PS2_RET_NAK;
  434. break;
  435. case PS2_RET_ERR:
  436. if (ps2dev->flags & PS2_FLAG_NAK) {
  437. ps2dev->flags &= ~PS2_FLAG_NAK;
  438. ps2dev->nak = PS2_RET_ERR;
  439. break;
  440. }
  441. fallthrough;
  442. /*
  443. * Workaround for mice which don't ACK the Get ID command.
  444. * These are valid mouse IDs that we recognize.
  445. */
  446. case 0x00:
  447. case 0x03:
  448. case 0x04:
  449. if (ps2dev->flags & PS2_FLAG_WAITID) {
  450. ps2dev->nak = 0;
  451. break;
  452. }
  453. fallthrough;
  454. default:
  455. /*
  456. * Do not signal errors if we get unexpected reply while
  457. * waiting for an ACK to the initial (first) command byte:
  458. * the device might not be quiesced yet and continue
  459. * delivering data. For certain commands (such as set leds and
  460. * set repeat rate) that can be used during normal device
  461. * operation, we even pass this data byte to the normal receive
  462. * handler.
  463. * Note that we reset PS2_FLAG_WAITID flag, so the workaround
  464. * for mice not acknowledging the Get ID command only triggers
  465. * on the 1st byte; if device spews data we really want to see
  466. * a real ACK from it.
  467. */
  468. dev_dbg(&ps2dev->serio->dev, "unexpected %#02x\n", data);
  469. if (ps2dev->flags & PS2_FLAG_PASS_NOACK)
  470. ps2dev->receive_handler(ps2dev, data);
  471. ps2dev->flags &= ~(PS2_FLAG_WAITID | PS2_FLAG_PASS_NOACK);
  472. return;
  473. }
  474. if (!ps2dev->nak)
  475. ps2dev->flags &= ~PS2_FLAG_NAK;
  476. ps2dev->flags &= ~PS2_FLAG_ACK;
  477. if (!ps2dev->nak && data != PS2_RET_ACK)
  478. ps2_handle_response(ps2dev, data);
  479. else
  480. wake_up(&ps2dev->wait);
  481. }
  482. /*
  483. * Clears state of PS/2 device after communication error by resetting majority
  484. * of flags and waking up waiters, if any.
  485. */
  486. static void ps2_cleanup(struct ps2dev *ps2dev)
  487. {
  488. unsigned long old_flags = ps2dev->flags;
  489. /* reset all flags except last nak */
  490. ps2dev->flags &= PS2_FLAG_NAK;
  491. if (old_flags & PS2_FLAG_ACK)
  492. ps2dev->nak = 1;
  493. if (old_flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
  494. wake_up(&ps2dev->wait);
  495. }
  496. /**
  497. * ps2_interrupt - common interrupt handler for PS/2 devices
  498. * @serio: serio port for the device
  499. * @data: a data byte received from the device
  500. * @flags: flags such as %SERIO_PARITY or %SERIO_TIMEOUT indicating state of
  501. * the data transfer
  502. *
  503. * ps2_interrupt() invokes pre-receive handler, optionally handles command
  504. * acknowledgement and response from the device, and finally passes the data
  505. * to the main protocol handler for future processing.
  506. */
  507. irqreturn_t ps2_interrupt(struct serio *serio, u8 data, unsigned int flags) {
  508. struct ps2dev *ps2dev = serio_get_drvdata(serio);
  509. enum ps2_disposition rc;
  510. rc = ps2dev->pre_receive_handler(ps2dev, data, flags);
  511. switch (rc) {
  512. case PS2_ERROR:
  513. ps2_cleanup(ps2dev);
  514. break;
  515. case PS2_IGNORE:
  516. break;
  517. case PS2_PROCESS:
  518. if (ps2dev->flags & PS2_FLAG_ACK)
  519. ps2_handle_ack(ps2dev, data);
  520. else if (ps2dev->flags & PS2_FLAG_CMD)
  521. ps2_handle_response(ps2dev, data);
  522. else
  523. ps2dev->receive_handler(ps2dev, data);
  524. break;
  525. }
  526. return IRQ_HANDLED;
  527. }
  528. EXPORT_SYMBOL(ps2_interrupt);
  529. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  530. MODULE_DESCRIPTION("PS/2 driver library");
  531. MODULE_LICENSE("GPL");