libps2.c 16 KB

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