tty.rst 12 KB

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