tty.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. The
  84. character buffer passed to this method is already
  85. in kernel space.
  86. put_char() Queues a character for writing to the tty device.
  87. If there is no room in the queue, the character is
  88. ignored.
  89. flush_chars() (Optional) If defined, must be called after
  90. queueing characters with put_char() in order to
  91. start transmission.
  92. write_room() Returns the numbers of characters the tty driver
  93. will accept for queueing to be written.
  94. ioctl() Invoke device specific ioctl.
  95. Expects data pointers to refer to userspace.
  96. Returns ENOIOCTLCMD for unrecognized ioctl numbers.
  97. set_termios() Notify the tty driver that the device's termios
  98. settings have changed. New settings are in
  99. tty->termios. Previous settings should be passed in
  100. the "old" argument.
  101. The API is defined such that the driver should return
  102. the actual modes selected. This means that the
  103. driver function is responsible for modifying any
  104. bits in the request it cannot fulfill to indicate
  105. the actual modes being used. A device with no
  106. hardware capability for change (eg a USB dongle or
  107. virtual port) can provide NULL for this method.
  108. throttle() Notify the tty driver that input buffers for the
  109. line discipline are close to full, and it should
  110. somehow signal that no more characters should be
  111. sent to the tty.
  112. unthrottle() Notify the tty driver that characters can now be
  113. sent to the tty without fear of overrunning the
  114. input buffers of the line disciplines.
  115. stop() Ask the tty driver to stop outputting characters
  116. to the tty device.
  117. start() Ask the tty driver to resume sending characters
  118. to the tty device.
  119. hangup() Ask the tty driver to hang up the tty device.
  120. break_ctl() (Optional) Ask the tty driver to turn on or off
  121. BREAK status on the RS-232 port. If state is -1,
  122. then the BREAK status should be turned on; if
  123. state is 0, then BREAK should be turned off.
  124. If this routine is not implemented, use ioctls
  125. TIOCSBRK / TIOCCBRK instead.
  126. wait_until_sent() Waits until the device has written out all of the
  127. characters in its transmitter FIFO.
  128. send_xchar() Send a high-priority XON/XOFF character to the device.
  129. Flags
  130. Line discipline methods have access to tty->flags field containing the
  131. following interesting flags:
  132. TTY_THROTTLED Driver input is throttled. The ldisc should call
  133. tty->driver->unthrottle() in order to resume
  134. reception when it is ready to process more data.
  135. TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc's
  136. write_wakeup() method in order to resume
  137. transmission when it can accept more data
  138. to transmit.
  139. TTY_IO_ERROR If set, causes all subsequent userspace read/write
  140. calls on the tty to fail, returning -EIO.
  141. TTY_OTHER_CLOSED Device is a pty and the other side has closed.
  142. TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
  143. smaller chunks.
  144. Locking
  145. Callers to the line discipline functions from the tty layer are required to
  146. take line discipline locks. The same is true of calls from the driver side
  147. but not yet enforced.
  148. Three calls are now provided
  149. ldisc = tty_ldisc_ref(tty);
  150. takes a handle to the line discipline in the tty and returns it. If no ldisc
  151. is currently attached or the ldisc is being closed and re-opened at this
  152. point then NULL is returned. While this handle is held the ldisc will not
  153. change or go away.
  154. tty_ldisc_deref(ldisc)
  155. Returns the ldisc reference and allows the ldisc to be closed. Returning the
  156. reference takes away your right to call the ldisc functions until you take
  157. a new reference.
  158. ldisc = tty_ldisc_ref_wait(tty);
  159. Performs the same function as tty_ldisc_ref except that it will wait for an
  160. ldisc change to complete and then return a reference to the new ldisc.
  161. While these functions are slightly slower than the old code they should have
  162. minimal impact as most receive logic uses the flip buffers and they only
  163. need to take a reference when they push bits up through the driver.
  164. A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
  165. functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
  166. fail in this situation if used within these functions. Ldisc and driver
  167. code calling its own functions must be careful in this case.
  168. Driver Interface
  169. ----------------
  170. open() - Called when a device is opened. May sleep
  171. close() - Called when a device is closed. At the point of
  172. return from this call the driver must make no
  173. further ldisc calls of any kind. May sleep
  174. write() - Called to write bytes to the device. May not
  175. sleep. May occur in parallel in special cases.
  176. Because this includes panic paths drivers generally
  177. shouldn't try and do clever locking here.
  178. put_char() - Stuff a single character onto the queue. The
  179. driver is guaranteed following up calls to
  180. flush_chars.
  181. flush_chars() - Ask the kernel to write put_char queue
  182. write_room() - Return the number of characters tht can be stuffed
  183. into the port buffers without overflow (or less).
  184. The ldisc is responsible for being intelligent
  185. about multi-threading of write_room/write calls
  186. ioctl() - Called when an ioctl may be for the driver
  187. set_termios() - Called on termios change, serialized against
  188. itself by a semaphore. May sleep.
  189. set_ldisc() - Notifier for discipline change. At the point this
  190. is done the discipline is not yet usable. Can now
  191. sleep (I think)
  192. throttle() - Called by the ldisc to ask the driver to do flow
  193. control. Serialization including with unthrottle
  194. is the job of the ldisc layer.
  195. unthrottle() - Called by the ldisc to ask the driver to stop flow
  196. control.
  197. stop() - Ldisc notifier to the driver to stop output. As with
  198. throttle the serializations with start() are down
  199. to the ldisc layer.
  200. start() - Ldisc notifier to the driver to start output.
  201. hangup() - Ask the tty driver to cause a hangup initiated
  202. from the host side. [Can sleep ??]
  203. break_ctl() - Send RS232 break. Can sleep. Can get called in
  204. parallel, driver must serialize (for now), and
  205. with write calls.
  206. wait_until_sent() - Wait for characters to exit the hardware queue
  207. of the driver. Can sleep
  208. send_xchar() - Send XON/XOFF and if possible jump the queue with
  209. it in order to get fast flow control responses.
  210. Cannot sleep ??