structleak: disable STRUCTLEAK_BYREF in combination with KASAN_STACK
[sfrench/cifs-2.6.git] / Documentation / driver-api / serial / tty.rst
1 =================
2 The Lockronomicon
3 =================
4
5 Your guide to the ancient and twisted locking policies of the tty layer and
6 the warped logic behind them. Beware all ye who read on.
7
8
9 Line Discipline
10 ---------------
11
12 Line disciplines are registered with tty_register_ldisc() passing the
13 discipline number and the ldisc structure. At the point of registration the
14 discipline must be ready to use and it is possible it will get used before
15 the call returns success. If the call returns an error then it won't get
16 called. Do not re-use ldisc numbers as they are part of the userspace ABI
17 and writing over an existing ldisc will cause demons to eat your computer.
18 After the return the ldisc data has been copied so you may free your own
19 copy of the structure. You must not re-register over the top of the line
20 discipline even with the same data or your computer again will be eaten by
21 demons.
22
23 In order to remove a line discipline call tty_unregister_ldisc().
24 In ancient times this always worked. In modern times the function will
25 return -EBUSY if the ldisc is currently in use. Since the ldisc referencing
26 code manages the module counts this should not usually be a concern.
27
28 Heed this warning: the reference count field of the registered copies of the
29 tty_ldisc structure in the ldisc table counts the number of lines using this
30 discipline. The reference count of the tty_ldisc structure within a tty
31 counts the number of active users of the ldisc at this instant. In effect it
32 counts the number of threads of execution within an ldisc method (plus those
33 about to enter and exit although this detail matters not).
34
35 Line Discipline Methods
36 -----------------------
37
38 TTY side interfaces
39 ^^^^^^^^^^^^^^^^^^^
40
41 ======================= =======================================================
42 open()                  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. Should initialize any
46                         state needed by the ldisc, and set receive_room
47                         in the tty_struct to the maximum amount of data
48                         the line discipline is willing to accept from the
49                         driver with a single call to receive_buf().
50                         Returning an error will prevent the ldisc from
51                         being attached. Can sleep.
52
53 close()                 This is called on a terminal when the line
54                         discipline is being unplugged. At the point of
55                         execution no further users will enter the
56                         ldisc code for this tty. Can sleep.
57
58 hangup()                Called when the tty line is hung up.
59                         The line discipline should cease I/O to the tty.
60                         No further calls into the ldisc code will occur.
61                         The return value is ignored. Can sleep.
62
63 read()                  (optional) A process requests reading data from
64                         the line. Multiple read calls may occur in parallel
65                         and the ldisc must deal with serialization issues.
66                         If not defined, the process will receive an EIO
67                         error. May sleep.
68
69 write()                 (optional) A process requests writing data to the
70                         line. Multiple write calls are serialized by the
71                         tty layer for the ldisc. If not defined, the
72                         process will receive an EIO error. May sleep.
73
74 flush_buffer()          (optional) May be called at any point between
75                         open and close, and instructs the line discipline
76                         to empty its input buffer.
77
78 set_termios()           (optional) Called on termios structure changes.
79                         The caller passes the old termios data and the
80                         current data is in the tty. Called under the
81                         termios semaphore so allowed to sleep. Serialized
82                         against itself only.
83
84 poll()                  (optional) Check the status for the poll/select
85                         calls. Multiple poll calls may occur in parallel.
86                         May sleep.
87
88 ioctl()                 (optional) Called when an ioctl is handed to the
89                         tty layer that might be for the ldisc. Multiple
90                         ioctl calls may occur in parallel. May sleep.
91
92 compat_ioctl()          (optional) Called when a 32 bit ioctl is handed
93                         to the tty layer that might be for the ldisc.
94                         Multiple ioctl calls may occur in parallel.
95                         May sleep.
96 ======================= =======================================================
97
98 Driver Side Interfaces
99 ^^^^^^^^^^^^^^^^^^^^^^
100
101 ======================= =======================================================
102 receive_buf()           (optional) Called by the low-level driver to hand
103                         a buffer of received bytes to the ldisc for
104                         processing. The number of bytes is guaranteed not
105                         to exceed the current value of tty->receive_room.
106                         All bytes must be processed.
107
108 receive_buf2()          (optional) Called by the low-level driver to hand
109                         a buffer of received bytes to the ldisc for
110                         processing. Returns the number of bytes processed.
111
112                         If both receive_buf() and receive_buf2() are
113                         defined, receive_buf2() should be preferred.
114
115 write_wakeup()          May be called at any point between open and close.
116                         The TTY_DO_WRITE_WAKEUP flag indicates if a call
117                         is needed but always races versus calls. Thus the
118                         ldisc must be careful about setting order and to
119                         handle unexpected calls. Must not sleep.
120
121                         The driver is forbidden from calling this directly
122                         from the ->write call from the ldisc as the ldisc
123                         is permitted to call the driver write method from
124                         this function. In such a situation defer it.
125
126 dcd_change()            Report to the tty line the current DCD pin status
127                         changes and the relative timestamp. The timestamp
128                         cannot be NULL.
129 ======================= =======================================================
130
131
132 Driver Access
133 ^^^^^^^^^^^^^
134
135 Line discipline methods can call the following methods of the underlying
136 hardware driver through the function pointers within the tty->driver
137 structure:
138
139 ======================= =======================================================
140 write()                 Write a block of characters to the tty device.
141                         Returns the number of characters accepted. The
142                         character buffer passed to this method is already
143                         in kernel space.
144
145 put_char()              Queues a character for writing to the tty device.
146                         If there is no room in the queue, the character is
147                         ignored.
148
149 flush_chars()           (Optional) If defined, must be called after
150                         queueing characters with put_char() in order to
151                         start transmission.
152
153 write_room()            Returns the numbers of characters the tty driver
154                         will accept for queueing to be written.
155
156 ioctl()                 Invoke device specific ioctl.
157                         Expects data pointers to refer to userspace.
158                         Returns ENOIOCTLCMD for unrecognized ioctl numbers.
159
160 set_termios()           Notify the tty driver that the device's termios
161                         settings have changed. New settings are in
162                         tty->termios. Previous settings should be passed in
163                         the "old" argument.
164
165                         The API is defined such that the driver should return
166                         the actual modes selected. This means that the
167                         driver function is responsible for modifying any
168                         bits in the request it cannot fulfill to indicate
169                         the actual modes being used. A device with no
170                         hardware capability for change (e.g. a USB dongle or
171                         virtual port) can provide NULL for this method.
172
173 throttle()              Notify the tty driver that input buffers for the
174                         line discipline are close to full, and it should
175                         somehow signal that no more characters should be
176                         sent to the tty.
177
178 unthrottle()            Notify the tty driver that characters can now be
179                         sent to the tty without fear of overrunning the
180                         input buffers of the line disciplines.
181
182 stop()                  Ask the tty driver to stop outputting characters
183                         to the tty device.
184
185 start()                 Ask the tty driver to resume sending characters
186                         to the tty device.
187
188 hangup()                Ask the tty driver to hang up the tty device.
189
190 break_ctl()             (Optional) Ask the tty driver to turn on or off
191                         BREAK status on the RS-232 port.  If state is -1,
192                         then the BREAK status should be turned on; if
193                         state is 0, then BREAK should be turned off.
194                         If this routine is not implemented, use ioctls
195                         TIOCSBRK / TIOCCBRK instead.
196
197 wait_until_sent()       Waits until the device has written out all of the
198                         characters in its transmitter FIFO.
199
200 send_xchar()            Send a high-priority XON/XOFF character to the device.
201 ======================= =======================================================
202
203
204 Flags
205 ^^^^^
206
207 Line discipline methods have access to tty->flags field containing the
208 following interesting flags:
209
210 ======================= =======================================================
211 TTY_THROTTLED           Driver input is throttled. The ldisc should call
212                         tty->driver->unthrottle() in order to resume
213                         reception when it is ready to process more data.
214
215 TTY_DO_WRITE_WAKEUP     If set, causes the driver to call the ldisc's
216                         write_wakeup() method in order to resume
217                         transmission when it can accept more data
218                         to transmit.
219
220 TTY_IO_ERROR            If set, causes all subsequent userspace read/write
221                         calls on the tty to fail, returning -EIO.
222
223 TTY_OTHER_CLOSED        Device is a pty and the other side has closed.
224
225 TTY_NO_WRITE_SPLIT      Prevent driver from splitting up writes into
226                         smaller chunks.
227 ======================= =======================================================
228
229
230 Locking
231 ^^^^^^^
232
233 Callers to the line discipline functions from the tty layer are required to
234 take line discipline locks. The same is true of calls from the driver side
235 but not yet enforced.
236
237 Three calls are now provided::
238
239         ldisc = tty_ldisc_ref(tty);
240
241 takes a handle to the line discipline in the tty and returns it. If no ldisc
242 is currently attached or the ldisc is being closed and re-opened at this
243 point then NULL is returned. While this handle is held the ldisc will not
244 change or go away::
245
246         tty_ldisc_deref(ldisc)
247
248 Returns the ldisc reference and allows the ldisc to be closed. Returning the
249 reference takes away your right to call the ldisc functions until you take
250 a new reference::
251
252         ldisc = tty_ldisc_ref_wait(tty);
253
254 Performs the same function as tty_ldisc_ref except that it will wait for an
255 ldisc change to complete and then return a reference to the new ldisc.
256
257 While these functions are slightly slower than the old code they should have
258 minimal impact as most receive logic uses the flip buffers and they only
259 need to take a reference when they push bits up through the driver.
260
261 A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
262 functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
263 fail in this situation if used within these functions. Ldisc and driver
264 code calling its own functions must be careful in this case.
265
266
267 Driver Interface
268 ----------------
269
270 ======================= =======================================================
271 open()                  Called when a device is opened. May sleep
272
273 close()                 Called when a device is closed. At the point of
274                         return from this call the driver must make no
275                         further ldisc calls of any kind. May sleep
276
277 write()                 Called to write bytes to the device. May not
278                         sleep. May occur in parallel in special cases.
279                         Because this includes panic paths drivers generally
280                         shouldn't try and do clever locking here.
281
282 put_char()              Stuff a single character onto the queue. The
283                         driver is guaranteed following up calls to
284                         flush_chars.
285
286 flush_chars()           Ask the kernel to write put_char queue
287
288 write_room()            Return the number of characters that can be stuffed
289                         into the port buffers without overflow (or less).
290                         The ldisc is responsible for being intelligent
291                         about multi-threading of write_room/write calls
292
293 ioctl()                 Called when an ioctl may be for the driver
294
295 set_termios()           Called on termios change, serialized against
296                         itself by a semaphore. May sleep.
297
298 set_ldisc()             Notifier for discipline change. At the point this
299                         is done the discipline is not yet usable. Can now
300                         sleep (I think)
301
302 throttle()              Called by the ldisc to ask the driver to do flow
303                         control.  Serialization including with unthrottle
304                         is the job of the ldisc layer.
305
306 unthrottle()            Called by the ldisc to ask the driver to stop flow
307                         control.
308
309 stop()                  Ldisc notifier to the driver to stop output. As with
310                         throttle the serializations with start() are down
311                         to the ldisc layer.
312
313 start()                 Ldisc notifier to the driver to start output.
314
315 hangup()                Ask the tty driver to cause a hangup initiated
316                         from the host side. [Can sleep ??]
317
318 break_ctl()             Send RS232 break. Can sleep. Can get called in
319                         parallel, driver must serialize (for now), and
320                         with write calls.
321
322 wait_until_sent()       Wait for characters to exit the hardware queue
323                         of the driver. Can sleep
324
325 send_xchar()            Send XON/XOFF and if possible jump the queue with
326                         it in order to get fast flow control responses.
327                         Cannot sleep ??
328 ======================= =======================================================