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