blob: 4b709f392713d42c68c663d582a524d65de04a81 [file] [log] [blame]
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -03001=================
2The Lockronomicon
3=================
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5Your guide to the ancient and twisted locking policies of the tty layer and
6the warped logic behind them. Beware all ye who read on.
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9Line Discipline
10---------------
11
12Line disciplines are registered with tty_register_ldisc() passing the
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030013discipline number and the ldisc structure. At the point of registration the
Linus Torvalds1da177e2005-04-16 15:20:36 -070014discipline must be ready to use and it is possible it will get used before
15the call returns success. If the call returns an error then it won't get
16called. Do not re-use ldisc numbers as they are part of the userspace ABI
17and writing over an existing ldisc will cause demons to eat your computer.
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030018After the return the ldisc data has been copied so you may free your own
Linus Torvalds1da177e2005-04-16 15:20:36 -070019copy of the structure. You must not re-register over the top of the line
20discipline even with the same data or your computer again will be eaten by
21demons.
22
Alexey Dobriyanbfb07592005-06-23 00:10:32 -070023In order to remove a line discipline call tty_unregister_ldisc().
Linus Torvalds1da177e2005-04-16 15:20:36 -070024In ancient times this always worked. In modern times the function will
25return -EBUSY if the ldisc is currently in use. Since the ldisc referencing
26code manages the module counts this should not usually be a concern.
27
28Heed this warning: the reference count field of the registered copies of the
29tty_ldisc structure in the ldisc table counts the number of lines using this
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030030discipline. The reference count of the tty_ldisc structure within a tty
Linus Torvalds1da177e2005-04-16 15:20:36 -070031counts the number of active users of the ldisc at this instant. In effect it
32counts the number of threads of execution within an ldisc method (plus those
33about to enter and exit although this detail matters not).
34
35Line Discipline Methods
36-----------------------
37
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030038TTY side interfaces
39^^^^^^^^^^^^^^^^^^^
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030041======================= =======================================================
42open() Called when the line discipline is attached to
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 the terminal. No other call into the line
44 discipline for this tty will occur until it
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020045 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030053close() This is called on a terminal when the line
Tilman Schmidt1f59c142006-12-29 16:48:03 -080054 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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030058hangup() Called when the tty line is hung up.
Tilman Schmidt1f59c142006-12-29 16:48:03 -080059 The line discipline should cease I/O to the tty.
60 No further calls into the ldisc code will occur.
Jiri Slaby28f194d2021-09-14 11:11:22 +020061 Can sleep.
Tilman Schmidt1f59c142006-12-29 16:48:03 -080062
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030063read() (optional) A process requests reading data from
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020064 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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030069write() (optional) A process requests writing data to the
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020070 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030074flush_buffer() (optional) May be called at any point between
Tilman Schmidt1f59c142006-12-29 16:48:03 -080075 open and close, and instructs the line discipline
76 to empty its input buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030078set_termios() (optional) Called on termios structure changes.
Tilman Schmidt1f59c142006-12-29 16:48:03 -080079 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030084poll() (optional) Check the status for the poll/select
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020085 calls. Multiple poll calls may occur in parallel.
86 May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030088ioctl() (optional) Called when an ioctl is handed to the
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020089 tty layer that might be for the ldisc. Multiple
90 ioctl calls may occur in parallel. May sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030092compat_ioctl() (optional) Called when a 32 bit ioctl is handed
Tilman Schmidt1a76eb52015-09-30 01:45:11 +020093 to the tty layer that might be for the ldisc.
94 Multiple ioctl calls may occur in parallel.
95 May sleep.
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030096======================= =======================================================
Tilman Schmidt7e11a0f2009-11-04 16:04:52 -080097
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -030098Driver Side Interfaces
99^^^^^^^^^^^^^^^^^^^^^^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300101======================= =======================================================
102receive_buf() (optional) Called by the low-level driver to hand
Tilman Schmidt1a76eb52015-09-30 01:45:11 +0200103 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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300108receive_buf2() (optional) Called by the low-level driver to hand
Tilman Schmidt1a76eb52015-09-30 01:45:11 +0200109 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300115write_wakeup() May be called at any point between open and close.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 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
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300126dcd_change() Report to the tty line the current DCD pin status
Rodolfo Giomettib3e63af2010-03-10 15:23:45 -0800127 changes and the relative timestamp. The timestamp
Alexander Gordeev12f9b1f2011-01-12 17:00:55 -0800128 cannot be NULL.
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300129======================= =======================================================
Rodolfo Giomettib3e63af2010-03-10 15:23:45 -0800130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800132Driver Access
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300133^^^^^^^^^^^^^
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800134
135Line discipline methods can call the following methods of the underlying
136hardware driver through the function pointers within the tty->driver
137structure:
138
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300139======================= =======================================================
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800140write() Write a block of characters to the tty device.
Alan6309ed72007-05-08 00:24:21 -0700141 Returns the number of characters accepted. The
142 character buffer passed to this method is already
143 in kernel space.
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800144
145put_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
149flush_chars() (Optional) If defined, must be called after
150 queueing characters with put_char() in order to
151 start transmission.
152
153write_room() Returns the numbers of characters the tty driver
154 will accept for queueing to be written.
155
156ioctl() Invoke device specific ioctl.
157 Expects data pointers to refer to userspace.
158 Returns ENOIOCTLCMD for unrecognized ioctl numbers.
159
160set_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
Alan Cox3ac40b92007-11-28 16:21:28 -0800165 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
Richard Genoud83ee73c2014-09-03 17:53:48 +0200170 hardware capability for change (e.g. a USB dongle or
Alan Cox3ac40b92007-11-28 16:21:28 -0800171 virtual port) can provide NULL for this method.
172
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800173throttle() 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
178unthrottle() 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
182stop() Ask the tty driver to stop outputting characters
183 to the tty device.
184
185start() Ask the tty driver to resume sending characters
186 to the tty device.
187
188hangup() Ask the tty driver to hang up the tty device.
189
190break_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
197wait_until_sent() Waits until the device has written out all of the
198 characters in its transmitter FIFO.
199
200send_xchar() Send a high-priority XON/XOFF character to the device.
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300201======================= =======================================================
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800202
203
204Flags
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300205^^^^^
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800206
207Line discipline methods have access to tty->flags field containing the
208following interesting flags:
209
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300210======================= =======================================================
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800211TTY_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
215TTY_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
220TTY_IO_ERROR If set, causes all subsequent userspace read/write
221 calls on the tty to fail, returning -EIO.
222
223TTY_OTHER_CLOSED Device is a pty and the other side has closed.
224
225TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes into
226 smaller chunks.
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300227======================= =======================================================
Tilman Schmidt1f59c142006-12-29 16:48:03 -0800228
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230Locking
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300231^^^^^^^
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233Callers to the line discipline functions from the tty layer are required to
234take line discipline locks. The same is true of calls from the driver side
235but not yet enforced.
236
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300237Three calls are now provided::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 ldisc = tty_ldisc_ref(tty);
240
241takes a handle to the line discipline in the tty and returns it. If no ldisc
242is currently attached or the ldisc is being closed and re-opened at this
243point then NULL is returned. While this handle is held the ldisc will not
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300244change or go away::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 tty_ldisc_deref(ldisc)
247
248Returns the ldisc reference and allows the ldisc to be closed. Returning the
249reference takes away your right to call the ldisc functions until you take
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300250a new reference::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 ldisc = tty_ldisc_ref_wait(tty);
253
254Performs the same function as tty_ldisc_ref except that it will wait for an
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300255ldisc change to complete and then return a reference to the new ldisc.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257While these functions are slightly slower than the old code they should have
258minimal impact as most receive logic uses the flip buffers and they only
259need to take a reference when they push bits up through the driver.
260
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300261A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
263fail in this situation if used within these functions. Ldisc and driver
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300264code calling its own functions must be careful in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266
267Driver Interface
268----------------
269
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300270======================= =======================================================
271open() Called when a device is opened. May sleep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300273close() Called when a device is closed. At the point of
274 return from this call the driver must make no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 further ldisc calls of any kind. May sleep
276
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300277write() Called to write bytes to the device. May not
278 sleep. May occur in parallel in special cases.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 Because this includes panic paths drivers generally
280 shouldn't try and do clever locking here.
281
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300282put_char() Stuff a single character onto the queue. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 driver is guaranteed following up calls to
284 flush_chars.
285
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300286flush_chars() Ask the kernel to write put_char queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300288write_room() Return the number of characters that can be stuffed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 into the port buffers without overflow (or less).
290 The ldisc is responsible for being intelligent
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300291 about multi-threading of write_room/write calls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300293ioctl() Called when an ioctl may be for the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300295set_termios() Called on termios change, serialized against
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 itself by a semaphore. May sleep.
297
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300298set_ldisc() Notifier for discipline change. At the point this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 is done the discipline is not yet usable. Can now
300 sleep (I think)
301
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300302throttle() Called by the ldisc to ask the driver to do flow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 control. Serialization including with unthrottle
304 is the job of the ldisc layer.
305
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300306unthrottle() Called by the ldisc to ask the driver to stop flow
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 control.
308
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300309stop() Ldisc notifier to the driver to stop output. As with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 throttle the serializations with start() are down
311 to the ldisc layer.
312
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300313start() Ldisc notifier to the driver to start output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300315hangup() Ask the tty driver to cause a hangup initiated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 from the host side. [Can sleep ??]
317
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300318break_ctl() Send RS232 break. Can sleep. Can get called in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 parallel, driver must serialize (for now), and
320 with write calls.
321
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300322wait_until_sent() Wait for characters to exit the hardware queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 of the driver. Can sleep
324
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300325send_xchar() Send XON/XOFF and if possible jump the queue with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 it in order to get fast flow control responses.
327 Cannot sleep ??
Mauro Carvalho Chehabf1374012019-04-22 10:27:22 -0300328======================= =======================================================