blob: 5f799e612e039f19f530090cd01161fdfc12c1ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2 The Lockronomicon
3
4Your guide to the ancient and twisted locking policies of the tty layer and
5the warped logic behind them. Beware all ye who read on.
6
7FIXME: still need to work out the full set of BKL assumptions and document
8them so they can eventually be killed off.
9
10
11Line Discipline
12---------------
13
14Line disciplines are registered with tty_register_ldisc() passing the
15discipline number and the ldisc structure. At the point of registration the
16discipline must be ready to use and it is possible it will get used before
17the call returns success. If the call returns an error then it won't get
18called. Do not re-use ldisc numbers as they are part of the userspace ABI
19and writing over an existing ldisc will cause demons to eat your computer.
20After the return the ldisc data has been copied so you may free your own
21copy of the structure. You must not re-register over the top of the line
22discipline even with the same data or your computer again will be eaten by
23demons.
24
Alexey Dobriyanbfb07592005-06-23 00:10:32 -070025In order to remove a line discipline call tty_unregister_ldisc().
Linus Torvalds1da177e2005-04-16 15:20:36 -070026In ancient times this always worked. In modern times the function will
27return -EBUSY if the ldisc is currently in use. Since the ldisc referencing
28code manages the module counts this should not usually be a concern.
29
30Heed this warning: the reference count field of the registered copies of the
31tty_ldisc structure in the ldisc table counts the number of lines using this
32discipline. The reference count of the tty_ldisc structure within a tty
33counts the number of active users of the ldisc at this instant. In effect it
34counts the number of threads of execution within an ldisc method (plus those
35about to enter and exit although this detail matters not).
36
37Line Discipline Methods
38-----------------------
39
40TTY side interfaces:
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042open() - Called when the line discipline is attached to
43 the terminal. No other call into the line
44 discipline for this tty will occur until it
45 completes successfully. Can sleep.
46
Tilman Schmidt1f59c142006-12-29 16:48:03 -080047close() - This is called on a terminal when the line
48 discipline is being unplugged. At the point of
49 execution no further users will enter the
50 ldisc code for this tty. Can sleep.
51
52hangup() - Called when the tty line is hung up.
53 The line discipline should cease I/O to the tty.
54 No further calls into the ldisc code will occur.
55 Can sleep.
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057write() - A process is writing data through the line
58 discipline. Multiple write calls are serialized
59 by the tty layer for the ldisc. May sleep.
60
Tilman Schmidt1f59c142006-12-29 16:48:03 -080061flush_buffer() - (optional) May be called at any point between
62 open and close, and instructs the line discipline
63 to empty its input buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Tilman Schmidt1f59c142006-12-29 16:48:03 -080065chars_in_buffer() - (optional) Report the number of bytes in the input
66 buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tilman Schmidt1f59c142006-12-29 16:48:03 -080068set_termios() - (optional) Called on termios structure changes.
69 The caller passes the old termios data and the
70 current data is in the tty. Called under the
71 termios semaphore so allowed to sleep. Serialized
72 against itself only.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74read() - Move data from the line discipline to the user.
75 Multiple read calls may occur in parallel and the
76 ldisc must deal with serialization issues. May
77 sleep.
78
79poll() - Check the status for the poll/select calls. Multiple
80 poll calls may occur in parallel. May sleep.
81
82ioctl() - Called when an ioctl is handed to the tty layer
83 that might be for the ldisc. Multiple ioctl calls
84 may occur in parallel. May sleep.
85
86Driver Side Interfaces:
87
88receive_buf() - Hand buffers of bytes from the driver to the ldisc
89 for processing. Semantics currently rather
90 mysterious 8(
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092write_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
98 The driver is forbidden from calling this directly
99 from the ->write call from the ldisc as the ldisc
100 is permitted to call the driver write method from
101 this function. In such a situation defer it.
102
103
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800104Driver Access
105
106Line discipline methods can call the following methods of the underlying
107hardware driver through the function pointers within the tty->driver
108structure:
109
110write() Write a block of characters to the tty device.
111 Returns the number of characters accepted.
112
113put_char() Queues a character for writing to the tty device.
114 If there is no room in the queue, the character is
115 ignored.
116
117flush_chars() (Optional) If defined, must be called after
118 queueing characters with put_char() in order to
119 start transmission.
120
121write_room() Returns the numbers of characters the tty driver
122 will accept for queueing to be written.
123
124ioctl() Invoke device specific ioctl.
125 Expects data pointers to refer to userspace.
126 Returns ENOIOCTLCMD for unrecognized ioctl numbers.
127
128set_termios() Notify the tty driver that the device's termios
129 settings have changed. New settings are in
130 tty->termios. Previous settings should be passed in
131 the "old" argument.
132
133throttle() Notify the tty driver that input buffers for the
134 line discipline are close to full, and it should
135 somehow signal that no more characters should be
136 sent to the tty.
137
138unthrottle() Notify the tty driver that characters can now be
139 sent to the tty without fear of overrunning the
140 input buffers of the line disciplines.
141
142stop() Ask the tty driver to stop outputting characters
143 to the tty device.
144
145start() Ask the tty driver to resume sending characters
146 to the tty device.
147
148hangup() Ask the tty driver to hang up the tty device.
149
150break_ctl() (Optional) Ask the tty driver to turn on or off
151 BREAK status on the RS-232 port. If state is -1,
152 then the BREAK status should be turned on; if
153 state is 0, then BREAK should be turned off.
154 If this routine is not implemented, use ioctls
155 TIOCSBRK / TIOCCBRK instead.
156
157wait_until_sent() Waits until the device has written out all of the
158 characters in its transmitter FIFO.
159
160send_xchar() Send a high-priority XON/XOFF character to the device.
161
162
163Flags
164
165Line discipline methods have access to tty->flags field containing the
166following interesting flags:
167
168TTY_THROTTLED Driver input is throttled. The ldisc should call
169 tty->driver->unthrottle() in order to resume
170 reception when it is ready to process more data.
171
172TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc's
173 write_wakeup() method in order to resume
174 transmission when it can accept more data
175 to transmit.
176
177TTY_IO_ERROR If set, causes all subsequent userspace read/write
178 calls on the tty to fail, returning -EIO.
179
180TTY_OTHER_CLOSED Device is a pty and the other side has closed.
181
182TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
183 smaller chunks.
184
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186Locking
187
188Callers to the line discipline functions from the tty layer are required to
189take line discipline locks. The same is true of calls from the driver side
190but not yet enforced.
191
192Three calls are now provided
193
194 ldisc = tty_ldisc_ref(tty);
195
196takes a handle to the line discipline in the tty and returns it. If no ldisc
197is currently attached or the ldisc is being closed and re-opened at this
198point then NULL is returned. While this handle is held the ldisc will not
199change or go away.
200
201 tty_ldisc_deref(ldisc)
202
203Returns the ldisc reference and allows the ldisc to be closed. Returning the
204reference takes away your right to call the ldisc functions until you take
205a new reference.
206
207 ldisc = tty_ldisc_ref_wait(tty);
208
209Performs the same function as tty_ldisc_ref except that it will wait for an
210ldisc change to complete and then return a reference to the new ldisc.
211
212While these functions are slightly slower than the old code they should have
213minimal impact as most receive logic uses the flip buffers and they only
214need to take a reference when they push bits up through the driver.
215
216A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
217functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
218fail in this situation if used within these functions. Ldisc and driver
219code calling its own functions must be careful in this case.
220
221
222Driver Interface
223----------------
224
225open() - Called when a device is opened. May sleep
226
227close() - Called when a device is closed. At the point of
228 return from this call the driver must make no
229 further ldisc calls of any kind. May sleep
230
231write() - Called to write bytes to the device. May not
232 sleep. May occur in parallel in special cases.
233 Because this includes panic paths drivers generally
234 shouldn't try and do clever locking here.
235
236put_char() - Stuff a single character onto the queue. The
237 driver is guaranteed following up calls to
238 flush_chars.
239
240flush_chars() - Ask the kernel to write put_char queue
241
242write_room() - Return the number of characters tht can be stuffed
243 into the port buffers without overflow (or less).
244 The ldisc is responsible for being intelligent
245 about multi-threading of write_room/write calls
246
247ioctl() - Called when an ioctl may be for the driver
248
249set_termios() - Called on termios change, serialized against
250 itself by a semaphore. May sleep.
251
252set_ldisc() - Notifier for discipline change. At the point this
253 is done the discipline is not yet usable. Can now
254 sleep (I think)
255
256throttle() - Called by the ldisc to ask the driver to do flow
257 control. Serialization including with unthrottle
258 is the job of the ldisc layer.
259
260unthrottle() - Called by the ldisc to ask the driver to stop flow
261 control.
262
263stop() - Ldisc notifier to the driver to stop output. As with
264 throttle the serializations with start() are down
265 to the ldisc layer.
266
267start() - Ldisc notifier to the driver to start output.
268
269hangup() - Ask the tty driver to cause a hangup initiated
270 from the host side. [Can sleep ??]
271
272break_ctl() - Send RS232 break. Can sleep. Can get called in
273 parallel, driver must serialize (for now), and
274 with write calls.
275
276wait_until_sent() - Wait for characters to exit the hardware queue
277 of the driver. Can sleep
278
279send_xchar() - Send XON/XOFF and if possible jump the queue with
280 it in order to get fast flow control responses.
281 Cannot sleep ??
282