tty.txt 11 KB

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