tty.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. open() - Called when the line discipline is attached to
  32. the terminal. No other call into the line
  33. discipline for this tty will occur until it
  34. completes successfully. Can sleep.
  35. close() - This is called on a terminal when the line
  36. discipline is being unplugged. At the point of
  37. execution no further users will enter the
  38. ldisc code for this tty. Can sleep.
  39. hangup() - Called when the tty line is hung up.
  40. The line discipline should cease I/O to the tty.
  41. No further calls into the ldisc code will occur.
  42. Can sleep.
  43. write() - A process is writing data through the line
  44. discipline. Multiple write calls are serialized
  45. by the tty layer for the ldisc. May sleep.
  46. flush_buffer() - (optional) May be called at any point between
  47. open and close, and instructs the line discipline
  48. to empty its input buffer.
  49. chars_in_buffer() - (optional) Report the number of bytes in the input
  50. buffer.
  51. set_termios() - (optional) Called on termios structure changes.
  52. The caller passes the old termios data and the
  53. current data is in the tty. Called under the
  54. termios semaphore so allowed to sleep. Serialized
  55. against itself only.
  56. read() - Move data from the line discipline to the user.
  57. Multiple read calls may occur in parallel and the
  58. ldisc must deal with serialization issues. May
  59. sleep.
  60. poll() - Check the status for the poll/select calls. Multiple
  61. poll calls may occur in parallel. May sleep.
  62. ioctl() - Called when an ioctl is handed to the tty layer
  63. that might be for the ldisc. Multiple ioctl calls
  64. may occur in parallel. May sleep.
  65. Driver Side Interfaces:
  66. receive_buf() - Hand buffers of bytes from the driver to the ldisc
  67. for processing. Semantics currently rather
  68. mysterious 8(
  69. write_wakeup() - May be called at any point between open and close.
  70. The TTY_DO_WRITE_WAKEUP flag indicates if a call
  71. is needed but always races versus calls. Thus the
  72. ldisc must be careful about setting order and to
  73. handle unexpected calls. Must not sleep.
  74. The driver is forbidden from calling this directly
  75. from the ->write call from the ldisc as the ldisc
  76. is permitted to call the driver write method from
  77. this function. In such a situation defer it.
  78. Driver Access
  79. Line discipline methods can call the following methods of the underlying
  80. hardware driver through the function pointers within the tty->driver
  81. structure:
  82. write() Write a block of characters to the tty device.
  83. Returns the number of characters accepted.
  84. put_char() Queues a character for writing to the tty device.
  85. If there is no room in the queue, the character is
  86. ignored.
  87. flush_chars() (Optional) If defined, must be called after
  88. queueing characters with put_char() in order to
  89. start transmission.
  90. write_room() Returns the numbers of characters the tty driver
  91. will accept for queueing to be written.
  92. ioctl() Invoke device specific ioctl.
  93. Expects data pointers to refer to userspace.
  94. Returns ENOIOCTLCMD for unrecognized ioctl numbers.
  95. set_termios() Notify the tty driver that the device's termios
  96. settings have changed. New settings are in
  97. tty->termios. Previous settings should be passed in
  98. the "old" argument.
  99. throttle() Notify the tty driver that input buffers for the
  100. line discipline are close to full, and it should
  101. somehow signal that no more characters should be
  102. sent to the tty.
  103. unthrottle() Notify the tty driver that characters can now be
  104. sent to the tty without fear of overrunning the
  105. input buffers of the line disciplines.
  106. stop() Ask the tty driver to stop outputting characters
  107. to the tty device.
  108. start() Ask the tty driver to resume sending characters
  109. to the tty device.
  110. hangup() Ask the tty driver to hang up the tty device.
  111. break_ctl() (Optional) Ask the tty driver to turn on or off
  112. BREAK status on the RS-232 port. If state is -1,
  113. then the BREAK status should be turned on; if
  114. state is 0, then BREAK should be turned off.
  115. If this routine is not implemented, use ioctls
  116. TIOCSBRK / TIOCCBRK instead.
  117. wait_until_sent() Waits until the device has written out all of the
  118. characters in its transmitter FIFO.
  119. send_xchar() Send a high-priority XON/XOFF character to the device.
  120. Flags
  121. Line discipline methods have access to tty->flags field containing the
  122. following interesting flags:
  123. TTY_THROTTLED Driver input is throttled. The ldisc should call
  124. tty->driver->unthrottle() in order to resume
  125. reception when it is ready to process more data.
  126. TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc's
  127. write_wakeup() method in order to resume
  128. transmission when it can accept more data
  129. to transmit.
  130. TTY_IO_ERROR If set, causes all subsequent userspace read/write
  131. calls on the tty to fail, returning -EIO.
  132. TTY_OTHER_CLOSED Device is a pty and the other side has closed.
  133. TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
  134. smaller chunks.
  135. Locking
  136. Callers to the line discipline functions from the tty layer are required to
  137. take line discipline locks. The same is true of calls from the driver side
  138. but not yet enforced.
  139. Three calls are now provided
  140. ldisc = tty_ldisc_ref(tty);
  141. takes a handle to the line discipline in the tty and returns it. If no ldisc
  142. is currently attached or the ldisc is being closed and re-opened at this
  143. point then NULL is returned. While this handle is held the ldisc will not
  144. change or go away.
  145. tty_ldisc_deref(ldisc)
  146. Returns the ldisc reference and allows the ldisc to be closed. Returning the
  147. reference takes away your right to call the ldisc functions until you take
  148. a new reference.
  149. ldisc = tty_ldisc_ref_wait(tty);
  150. Performs the same function as tty_ldisc_ref except that it will wait for an
  151. ldisc change to complete and then return a reference to the new ldisc.
  152. While these functions are slightly slower than the old code they should have
  153. minimal impact as most receive logic uses the flip buffers and they only
  154. need to take a reference when they push bits up through the driver.
  155. A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
  156. functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
  157. fail in this situation if used within these functions. Ldisc and driver
  158. code calling its own functions must be careful in this case.
  159. Driver Interface
  160. ----------------
  161. open() - Called when a device is opened. May sleep
  162. close() - Called when a device is closed. At the point of
  163. return from this call the driver must make no
  164. further ldisc calls of any kind. May sleep
  165. write() - Called to write bytes to the device. May not
  166. sleep. May occur in parallel in special cases.
  167. Because this includes panic paths drivers generally
  168. shouldn't try and do clever locking here.
  169. put_char() - Stuff a single character onto the queue. The
  170. driver is guaranteed following up calls to
  171. flush_chars.
  172. flush_chars() - Ask the kernel to write put_char queue
  173. write_room() - Return the number of characters tht can be stuffed
  174. into the port buffers without overflow (or less).
  175. The ldisc is responsible for being intelligent
  176. about multi-threading of write_room/write calls
  177. ioctl() - Called when an ioctl may be for the driver
  178. set_termios() - Called on termios change, serialized against
  179. itself by a semaphore. May sleep.
  180. set_ldisc() - Notifier for discipline change. At the point this
  181. is done the discipline is not yet usable. Can now
  182. sleep (I think)
  183. throttle() - Called by the ldisc to ask the driver to do flow
  184. control. Serialization including with unthrottle
  185. is the job of the ldisc layer.
  186. unthrottle() - Called by the ldisc to ask the driver to stop flow
  187. control.
  188. stop() - Ldisc notifier to the driver to stop output. As with
  189. throttle the serializations with start() are down
  190. to the ldisc layer.
  191. start() - Ldisc notifier to the driver to start output.
  192. hangup() - Ask the tty driver to cause a hangup initiated
  193. from the host side. [Can sleep ??]
  194. break_ctl() - Send RS232 break. Can sleep. Can get called in
  195. parallel, driver must serialize (for now), and
  196. with write calls.
  197. wait_until_sent() - Wait for characters to exit the hardware queue
  198. of the driver. Can sleep
  199. send_xchar() - Send XON/XOFF and if possible jump the queue with
  200. it in order to get fast flow control responses.
  201. Cannot sleep ??