tty.txt 11 KB

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