blob: 5e5349a4fcd281b2450415d7d668456aedd34b4b [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
Tilman Schmidt7e11a0f2009-11-04 16:04:52 -080045 completes successfully. Returning an error will
46 prevent the ldisc from being attached. Can sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tilman Schmidt1f59c142006-12-29 16:48:03 -080048close() - This is called on a terminal when the line
49 discipline is being unplugged. At the point of
50 execution no further users will enter the
51 ldisc code for this tty. Can sleep.
52
53hangup() - Called when the tty line is hung up.
54 The line discipline should cease I/O to the tty.
55 No further calls into the ldisc code will occur.
Tilman Schmidt7e11a0f2009-11-04 16:04:52 -080056 The return value is ignored. Can sleep.
Tilman Schmidt1f59c142006-12-29 16:48:03 -080057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058write() - A process is writing data through the line
59 discipline. Multiple write calls are serialized
60 by the tty layer for the ldisc. May sleep.
61
Tilman Schmidt1f59c142006-12-29 16:48:03 -080062flush_buffer() - (optional) May be called at any point between
63 open and close, and instructs the line discipline
64 to empty its input buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tilman Schmidt1f59c142006-12-29 16:48:03 -080066chars_in_buffer() - (optional) Report the number of bytes in the input
67 buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Tilman Schmidt1f59c142006-12-29 16:48:03 -080069set_termios() - (optional) Called on termios structure changes.
70 The caller passes the old termios data and the
71 current data is in the tty. Called under the
72 termios semaphore so allowed to sleep. Serialized
73 against itself only.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75read() - Move data from the line discipline to the user.
76 Multiple read calls may occur in parallel and the
77 ldisc must deal with serialization issues. May
78 sleep.
79
80poll() - Check the status for the poll/select calls. Multiple
81 poll calls may occur in parallel. May sleep.
82
83ioctl() - Called when an ioctl is handed to the tty layer
84 that might be for the ldisc. Multiple ioctl calls
85 may occur in parallel. May sleep.
86
Tilman Schmidt7e11a0f2009-11-04 16:04:52 -080087compat_ioctl() - Called when a 32 bit ioctl is handed to the tty layer
88 that might be for the ldisc. Multiple ioctl calls
89 may occur in parallel. May sleep.
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091Driver Side Interfaces:
92
93receive_buf() - Hand buffers of bytes from the driver to the ldisc
94 for processing. Semantics currently rather
95 mysterious 8(
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097write_wakeup() - May be called at any point between open and close.
98 The TTY_DO_WRITE_WAKEUP flag indicates if a call
99 is needed but always races versus calls. Thus the
100 ldisc must be careful about setting order and to
101 handle unexpected calls. Must not sleep.
102
103 The driver is forbidden from calling this directly
104 from the ->write call from the ldisc as the ldisc
105 is permitted to call the driver write method from
106 this function. In such a situation defer it.
107
108
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800109Driver Access
110
111Line discipline methods can call the following methods of the underlying
112hardware driver through the function pointers within the tty->driver
113structure:
114
115write() Write a block of characters to the tty device.
Alan6309ed72007-05-08 00:24:21 -0700116 Returns the number of characters accepted. The
117 character buffer passed to this method is already
118 in kernel space.
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800119
120put_char() Queues a character for writing to the tty device.
121 If there is no room in the queue, the character is
122 ignored.
123
124flush_chars() (Optional) If defined, must be called after
125 queueing characters with put_char() in order to
126 start transmission.
127
128write_room() Returns the numbers of characters the tty driver
129 will accept for queueing to be written.
130
131ioctl() Invoke device specific ioctl.
132 Expects data pointers to refer to userspace.
133 Returns ENOIOCTLCMD for unrecognized ioctl numbers.
134
135set_termios() Notify the tty driver that the device's termios
136 settings have changed. New settings are in
137 tty->termios. Previous settings should be passed in
138 the "old" argument.
139
Alan Cox3ac40b92007-11-28 16:21:28 -0800140 The API is defined such that the driver should return
141 the actual modes selected. This means that the
142 driver function is responsible for modifying any
143 bits in the request it cannot fulfill to indicate
144 the actual modes being used. A device with no
145 hardware capability for change (eg a USB dongle or
146 virtual port) can provide NULL for this method.
147
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800148throttle() Notify the tty driver that input buffers for the
149 line discipline are close to full, and it should
150 somehow signal that no more characters should be
151 sent to the tty.
152
153unthrottle() Notify the tty driver that characters can now be
154 sent to the tty without fear of overrunning the
155 input buffers of the line disciplines.
156
157stop() Ask the tty driver to stop outputting characters
158 to the tty device.
159
160start() Ask the tty driver to resume sending characters
161 to the tty device.
162
163hangup() Ask the tty driver to hang up the tty device.
164
165break_ctl() (Optional) Ask the tty driver to turn on or off
166 BREAK status on the RS-232 port. If state is -1,
167 then the BREAK status should be turned on; if
168 state is 0, then BREAK should be turned off.
169 If this routine is not implemented, use ioctls
170 TIOCSBRK / TIOCCBRK instead.
171
172wait_until_sent() Waits until the device has written out all of the
173 characters in its transmitter FIFO.
174
175send_xchar() Send a high-priority XON/XOFF character to the device.
176
177
178Flags
179
180Line discipline methods have access to tty->flags field containing the
181following interesting flags:
182
183TTY_THROTTLED Driver input is throttled. The ldisc should call
184 tty->driver->unthrottle() in order to resume
185 reception when it is ready to process more data.
186
187TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc's
188 write_wakeup() method in order to resume
189 transmission when it can accept more data
190 to transmit.
191
192TTY_IO_ERROR If set, causes all subsequent userspace read/write
193 calls on the tty to fail, returning -EIO.
194
195TTY_OTHER_CLOSED Device is a pty and the other side has closed.
196
197TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
198 smaller chunks.
199
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201Locking
202
203Callers to the line discipline functions from the tty layer are required to
204take line discipline locks. The same is true of calls from the driver side
205but not yet enforced.
206
207Three calls are now provided
208
209 ldisc = tty_ldisc_ref(tty);
210
211takes a handle to the line discipline in the tty and returns it. If no ldisc
212is currently attached or the ldisc is being closed and re-opened at this
213point then NULL is returned. While this handle is held the ldisc will not
214change or go away.
215
216 tty_ldisc_deref(ldisc)
217
218Returns the ldisc reference and allows the ldisc to be closed. Returning the
219reference takes away your right to call the ldisc functions until you take
220a new reference.
221
222 ldisc = tty_ldisc_ref_wait(tty);
223
224Performs the same function as tty_ldisc_ref except that it will wait for an
225ldisc change to complete and then return a reference to the new ldisc.
226
227While these functions are slightly slower than the old code they should have
228minimal impact as most receive logic uses the flip buffers and they only
229need to take a reference when they push bits up through the driver.
230
231A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
232functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
233fail in this situation if used within these functions. Ldisc and driver
234code calling its own functions must be careful in this case.
235
236
237Driver Interface
238----------------
239
240open() - Called when a device is opened. May sleep
241
242close() - Called when a device is closed. At the point of
243 return from this call the driver must make no
244 further ldisc calls of any kind. May sleep
245
246write() - Called to write bytes to the device. May not
247 sleep. May occur in parallel in special cases.
248 Because this includes panic paths drivers generally
249 shouldn't try and do clever locking here.
250
251put_char() - Stuff a single character onto the queue. The
252 driver is guaranteed following up calls to
253 flush_chars.
254
255flush_chars() - Ask the kernel to write put_char queue
256
257write_room() - Return the number of characters tht can be stuffed
258 into the port buffers without overflow (or less).
259 The ldisc is responsible for being intelligent
260 about multi-threading of write_room/write calls
261
262ioctl() - Called when an ioctl may be for the driver
263
264set_termios() - Called on termios change, serialized against
265 itself by a semaphore. May sleep.
266
267set_ldisc() - Notifier for discipline change. At the point this
268 is done the discipline is not yet usable. Can now
269 sleep (I think)
270
271throttle() - Called by the ldisc to ask the driver to do flow
272 control. Serialization including with unthrottle
273 is the job of the ldisc layer.
274
275unthrottle() - Called by the ldisc to ask the driver to stop flow
276 control.
277
278stop() - Ldisc notifier to the driver to stop output. As with
279 throttle the serializations with start() are down
280 to the ldisc layer.
281
282start() - Ldisc notifier to the driver to start output.
283
284hangup() - Ask the tty driver to cause a hangup initiated
285 from the host side. [Can sleep ??]
286
287break_ctl() - Send RS232 break. Can sleep. Can get called in
288 parallel, driver must serialize (for now), and
289 with write calls.
290
291wait_until_sent() - Wait for characters to exit the hardware queue
292 of the driver. Can sleep
293
294send_xchar() - Send XON/XOFF and if possible jump the queue with
295 it in order to get fast flow control responses.
296 Cannot sleep ??
297