tty.txt 11 KB

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