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