1=================
2The Lockronomicon
3=================
4
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
8
9Line Discipline
10---------------
11
12Line disciplines are registered with tty_register_ldisc() passing the
13discipline number and the ldisc structure. At the point of registration the
14discipline 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.
18After the return the ldisc data has been copied so you may free your own
19copy 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
23In order to remove a line discipline call tty_unregister_ldisc().
24In 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
30discipline. The reference count of the tty_ldisc structure within a tty
31counts 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
38TTY side interfaces
39^^^^^^^^^^^^^^^^^^^
40
41======================= =======================================================
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. 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
53close()			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
58hangup()		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			Can sleep.
62
63read()			(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
69write()			(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
74flush_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
78set_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
84poll()			(optional) Check the status for the poll/select
85			calls. Multiple poll calls may occur in parallel.
86			May sleep.
87
88ioctl()			(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
92compat_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
98Driver Side Interfaces
99^^^^^^^^^^^^^^^^^^^^^^
100
101======================= =======================================================
102receive_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
108receive_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
115write_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
126dcd_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
132Driver Access
133^^^^^^^^^^^^^
134
135Line discipline methods can call the following methods of the underlying
136hardware driver through the function pointers within the tty->driver
137structure:
138
139======================= =======================================================
140write()			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
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
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
173throttle()		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.
201======================= =======================================================
202
203
204Flags
205^^^^^
206
207Line discipline methods have access to tty->flags field containing the
208following interesting flags:
209
210======================= =======================================================
211TTY_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.
227======================= =======================================================
228
229
230Locking
231^^^^^^^
232
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
237Three calls are now provided::
238
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
244change or go away::
245
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
250a new reference::
251
252	ldisc = tty_ldisc_ref_wait(tty);
253
254Performs the same function as tty_ldisc_ref except that it will wait for an
255ldisc change to complete and then return a reference to the new ldisc.
256
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
261A caution: The ldisc->open(), ldisc->close() and driver->set_ldisc
262functions are called with the ldisc unavailable. Thus tty_ldisc_ref will
263fail in this situation if used within these functions. Ldisc and driver
264code calling its own functions must be careful in this case.
265
266
267Driver Interface
268----------------
269
270======================= =======================================================
271open()			Called when a device is opened. May sleep
272
273close()			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
277write()			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
282put_char()		Stuff a single character onto the queue. The
283			driver is guaranteed following up calls to
284			flush_chars.
285
286flush_chars()		Ask the kernel to write put_char queue
287
288write_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
293ioctl()			Called when an ioctl may be for the driver
294
295set_termios()		Called on termios change, serialized against
296			itself by a semaphore. May sleep.
297
298set_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
302throttle()		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
306unthrottle()		Called by the ldisc to ask the driver to stop flow
307			control.
308
309stop()			Ldisc notifier to the driver to stop output. As with
310			throttle the serializations with start() are down
311			to the ldisc layer.
312
313start()			Ldisc notifier to the driver to start output.
314
315hangup()		Ask the tty driver to cause a hangup initiated
316			from the host side. [Can sleep ??]
317
318break_ctl()		Send RS232 break. Can sleep. Can get called in
319			parallel, driver must serialize (for now), and
320			with write calls.
321
322wait_until_sent()	Wait for characters to exit the hardware queue
323			of the driver. Can sleep
324
325send_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======================= =======================================================
329