tty.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. The Lockronomicon
  2. Your guide to the ancient and twisted locking policies of the tty layer and
  3. the warped logic behind them. Beware all ye who read on.
  4. FIXME: still need to work out the full set of BKL assumptions and document
  5. them so they can eventually be killed off.
  6. Line Discipline
  7. ---------------
  8. Line disciplines are registered with tty_register_ldisc() passing the
  9. discipline number and the ldisc structure. At the point of registration the
  10. discipline must be ready to use and it is possible it will get used before
  11. the call returns success. If the call returns an error then it won't get
  12. called. Do not re-use ldisc numbers as they are part of the userspace ABI
  13. and writing over an existing ldisc will cause demons to eat your computer.
  14. After the return the ldisc data has been copied so you may free your own
  15. copy of the structure. You must not re-register over the top of the line
  16. discipline even with the same data or your computer again will be eaten by
  17. demons.
  18. In order to remove a line discipline call tty_unregister_ldisc().
  19. In ancient times this always worked. In modern times the function will
  20. return -EBUSY if the ldisc is currently in use. Since the ldisc referencing
  21. code manages the module counts this should not usually be a concern.
  22. Heed this warning: the reference count field of the registered copies of the
  23. tty_ldisc structure in the ldisc table counts the number of lines using this
  24. discipline. The reference count of the tty_ldisc structure within a tty
  25. counts the number of active users of the ldisc at this instant. In effect it
  26. counts the number of threads of execution within an ldisc method (plus those
  27. about to enter and exit although this detail matters not).
  28. Line Discipline Methods
  29. -----------------------
  30. TTY side interfaces:
  31. close() - This is called on a terminal when the line
  32. discipline is being unplugged. At the point of
  33. execution no further users will enter the
  34. ldisc code for this tty. Can sleep.
  35. open() - Called when the line discipline is attached to
  36. the terminal. No other call into the line
  37. discipline for this tty will occur until it
  38. completes successfully. Can sleep.
  39. write() - A process is writing data through the line
  40. discipline. Multiple write calls are serialized
  41. by the tty layer for the ldisc. May sleep.
  42. flush_buffer() - May be called at any point between open and close.
  43. chars_in_buffer() - Report the number of bytes in the buffer.
  44. set_termios() - Called on termios structure changes. The caller
  45. passes the old termios data and the current data
  46. is in the tty. Called under the termios semaphore so
  47. allowed to sleep. Serialized against itself only.
  48. read() - Move data from the line discipline to the user.
  49. Multiple read calls may occur in parallel and the
  50. ldisc must deal with serialization issues. May
  51. sleep.
  52. poll() - Check the status for the poll/select calls. Multiple
  53. poll calls may occur in parallel. May sleep.
  54. ioctl() - Called when an ioctl is handed to the tty layer
  55. that might be for the ldisc. Multiple ioctl calls
  56. may occur in parallel. May sleep.
  57. Driver Side Interfaces:
  58. receive_buf() - Hand buffers of bytes from the driver to the ldisc
  59. for processing. Semantics currently rather
  60. mysterious 8(
  61. write_wakeup() - May be called at any point between open and close.
  62. The TTY_DO_WRITE_WAKEUP flag indicates if a call
  63. is needed but always races versus calls. Thus the
  64. ldisc must be careful about setting order and to
  65. handle unexpected calls. Must not sleep.
  66. The driver is forbidden from calling this directly
  67. from the ->write call from the ldisc as the ldisc
  68. is permitted to call the driver write method from
  69. this function. In such a situation defer it.
  70. Locking
  71. Callers to the line discipline functions from the tty layer are required to
  72. take line discipline locks. The same is true of calls from the driver side
  73. but not yet enforced.
  74. Three calls are now provided
  75. ldisc = tty_ldisc_ref(tty);
  76. takes a handle to the line discipline in the tty and returns it. If no ldisc
  77. is currently attached or the ldisc is being closed and re-opened at this
  78. point then NULL is returned. While this handle is held the ldisc will not
  79. change or go away.
  80. tty_ldisc_deref(ldisc)
  81. Returns the ldisc reference and allows the ldisc to be closed. Returning the
  82. reference takes away your right to call the ldisc functions until you take
  83. a new reference.
  84. ldisc = tty_ldisc_ref_wait(tty);
  85. Performs the same function as tty_ldisc_ref except that it will wait for an
  86. ldisc change to complete and then return a reference to the new ldisc.
  87. While these functions are slightly slower than the old code they should have
  88. minimal impact as most receive logic uses the flip buffers and they only
  89. need to take a reference when they push bits up through the driver.
  90. A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
  91. functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
  92. fail in this situation if used within these functions. Ldisc and driver
  93. code calling its own functions must be careful in this case.
  94. Driver Interface
  95. ----------------
  96. open() - Called when a device is opened. May sleep
  97. close() - Called when a device is closed. At the point of
  98. return from this call the driver must make no
  99. further ldisc calls of any kind. May sleep
  100. write() - Called to write bytes to the device. May not
  101. sleep. May occur in parallel in special cases.
  102. Because this includes panic paths drivers generally
  103. shouldn't try and do clever locking here.
  104. put_char() - Stuff a single character onto the queue. The
  105. driver is guaranteed following up calls to
  106. flush_chars.
  107. flush_chars() - Ask the kernel to write put_char queue
  108. write_room() - Return the number of characters tht can be stuffed
  109. into the port buffers without overflow (or less).
  110. The ldisc is responsible for being intelligent
  111. about multi-threading of write_room/write calls
  112. ioctl() - Called when an ioctl may be for the driver
  113. set_termios() - Called on termios change, serialized against
  114. itself by a semaphore. May sleep.
  115. set_ldisc() - Notifier for discipline change. At the point this
  116. is done the discipline is not yet usable. Can now
  117. sleep (I think)
  118. throttle() - Called by the ldisc to ask the driver to do flow
  119. control. Serialization including with unthrottle
  120. is the job of the ldisc layer.
  121. unthrottle() - Called by the ldisc to ask the driver to stop flow
  122. control.
  123. stop() - Ldisc notifier to the driver to stop output. As with
  124. throttle the serializations with start() are down
  125. to the ldisc layer.
  126. start() - Ldisc notifier to the driver to start output.
  127. hangup() - Ask the tty driver to cause a hangup initiated
  128. from the host side. [Can sleep ??]
  129. break_ctl() - Send RS232 break. Can sleep. Can get called in
  130. parallel, driver must serialize (for now), and
  131. with write calls.
  132. wait_until_sent() - Wait for characters to exit the hardware queue
  133. of the driver. Can sleep
  134. send_xchar() - Send XON/XOFF and if possible jump the queue with
  135. it in order to get fast flow control responses.
  136. Cannot sleep ??