]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - Documentation/serial/tty.txt
Merge branch 'topic/pcsp-fix' into topic/misc
[mirror_ubuntu-bionic-kernel.git] / Documentation / serial / tty.txt
CommitLineData
1da177e4
LT
1
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
bfb07599 25In order to remove a line discipline call tty_unregister_ldisc().
1da177e4
LT
26In 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
1f59c140
TS
42open() - 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
1da177e4
LT
47close() - 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
1f59c140
TS
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.
1da177e4
LT
56
57write() - 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
1f59c140
TS
61flush_buffer() - (optional) May be called at any point between
62 open and close, and instructs the line discipline
63 to empty its input buffer.
1da177e4 64
1f59c140
TS
65chars_in_buffer() - (optional) Report the number of bytes in the input
66 buffer.
1da177e4 67
1f59c140
TS
68set_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.
1da177e4
LT
73
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
1da177e4
LT
92write_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
1f59c140
TS
104Driver 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.
6309ed7c
AC
111 Returns the number of characters accepted. The
112 character buffer passed to this method is already
113 in kernel space.
1f59c140
TS
114
115put_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
119flush_chars() (Optional) If defined, must be called after
120 queueing characters with put_char() in order to
121 start transmission.
122
123write_room() Returns the numbers of characters the tty driver
124 will accept for queueing to be written.
125
126ioctl() Invoke device specific ioctl.
127 Expects data pointers to refer to userspace.
128 Returns ENOIOCTLCMD for unrecognized ioctl numbers.
129
130set_termios() Notify the tty driver that the device's termios
131 settings have changed. New settings are in
132 tty->termios. Previous settings should be passed in
133 the "old" argument.
134
3ac40b9b
AC
135 The API is defined such that the driver should return
136 the actual modes selected. This means that the
137 driver function is responsible for modifying any
138 bits in the request it cannot fulfill to indicate
139 the actual modes being used. A device with no
140 hardware capability for change (eg a USB dongle or
141 virtual port) can provide NULL for this method.
142
1f59c140
TS
143throttle() Notify the tty driver that input buffers for the
144 line discipline are close to full, and it should
145 somehow signal that no more characters should be
146 sent to the tty.
147
148unthrottle() Notify the tty driver that characters can now be
149 sent to the tty without fear of overrunning the
150 input buffers of the line disciplines.
151
152stop() Ask the tty driver to stop outputting characters
153 to the tty device.
154
155start() Ask the tty driver to resume sending characters
156 to the tty device.
157
158hangup() Ask the tty driver to hang up the tty device.
159
160break_ctl() (Optional) Ask the tty driver to turn on or off
161 BREAK status on the RS-232 port. If state is -1,
162 then the BREAK status should be turned on; if
163 state is 0, then BREAK should be turned off.
164 If this routine is not implemented, use ioctls
165 TIOCSBRK / TIOCCBRK instead.
166
167wait_until_sent() Waits until the device has written out all of the
168 characters in its transmitter FIFO.
169
170send_xchar() Send a high-priority XON/XOFF character to the device.
171
172
173Flags
174
175Line discipline methods have access to tty->flags field containing the
176following interesting flags:
177
178TTY_THROTTLED Driver input is throttled. The ldisc should call
179 tty->driver->unthrottle() in order to resume
180 reception when it is ready to process more data.
181
182TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc's
183 write_wakeup() method in order to resume
184 transmission when it can accept more data
185 to transmit.
186
187TTY_IO_ERROR If set, causes all subsequent userspace read/write
188 calls on the tty to fail, returning -EIO.
189
190TTY_OTHER_CLOSED Device is a pty and the other side has closed.
191
192TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
193 smaller chunks.
194
195
1da177e4
LT
196Locking
197
198Callers to the line discipline functions from the tty layer are required to
199take line discipline locks. The same is true of calls from the driver side
200but not yet enforced.
201
202Three calls are now provided
203
204 ldisc = tty_ldisc_ref(tty);
205
206takes a handle to the line discipline in the tty and returns it. If no ldisc
207is currently attached or the ldisc is being closed and re-opened at this
208point then NULL is returned. While this handle is held the ldisc will not
209change or go away.
210
211 tty_ldisc_deref(ldisc)
212
213Returns the ldisc reference and allows the ldisc to be closed. Returning the
214reference takes away your right to call the ldisc functions until you take
215a new reference.
216
217 ldisc = tty_ldisc_ref_wait(tty);
218
219Performs the same function as tty_ldisc_ref except that it will wait for an
220ldisc change to complete and then return a reference to the new ldisc.
221
222While these functions are slightly slower than the old code they should have
223minimal impact as most receive logic uses the flip buffers and they only
224need to take a reference when they push bits up through the driver.
225
226A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
227functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
228fail in this situation if used within these functions. Ldisc and driver
229code calling its own functions must be careful in this case.
230
231
232Driver Interface
233----------------
234
235open() - Called when a device is opened. May sleep
236
237close() - Called when a device is closed. At the point of
238 return from this call the driver must make no
239 further ldisc calls of any kind. May sleep
240
241write() - Called to write bytes to the device. May not
242 sleep. May occur in parallel in special cases.
243 Because this includes panic paths drivers generally
244 shouldn't try and do clever locking here.
245
246put_char() - Stuff a single character onto the queue. The
247 driver is guaranteed following up calls to
248 flush_chars.
249
250flush_chars() - Ask the kernel to write put_char queue
251
252write_room() - Return the number of characters tht can be stuffed
253 into the port buffers without overflow (or less).
254 The ldisc is responsible for being intelligent
255 about multi-threading of write_room/write calls
256
257ioctl() - Called when an ioctl may be for the driver
258
259set_termios() - Called on termios change, serialized against
260 itself by a semaphore. May sleep.
261
262set_ldisc() - Notifier for discipline change. At the point this
263 is done the discipline is not yet usable. Can now
264 sleep (I think)
265
266throttle() - Called by the ldisc to ask the driver to do flow
267 control. Serialization including with unthrottle
268 is the job of the ldisc layer.
269
270unthrottle() - Called by the ldisc to ask the driver to stop flow
271 control.
272
273stop() - Ldisc notifier to the driver to stop output. As with
274 throttle the serializations with start() are down
275 to the ldisc layer.
276
277start() - Ldisc notifier to the driver to start output.
278
279hangup() - Ask the tty driver to cause a hangup initiated
280 from the host side. [Can sleep ??]
281
282break_ctl() - Send RS232 break. Can sleep. Can get called in
283 parallel, driver must serialize (for now), and
284 with write calls.
285
286wait_until_sent() - Wait for characters to exit the hardware queue
287 of the driver. Can sleep
288
289send_xchar() - Send XON/XOFF and if possible jump the queue with
290 it in order to get fast flow control responses.
291 Cannot sleep ??
292