tty.txt 11 KB

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