URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [tty.txt] - Rev 78
Go to most recent revision | Compare with Previous | Blame | View Log
The LockronomiconYour guide to the ancient and twisted locking policies of the tty layer andthe warped logic behind them. Beware all ye who read on.FIXME: still need to work out the full set of BKL assumptions and documentthem so they can eventually be killed off.Line Discipline---------------Line disciplines are registered with tty_register_ldisc() passing thediscipline number and the ldisc structure. At the point of registration thediscipline must be ready to use and it is possible it will get used beforethe call returns success. If the call returns an error then it won't getcalled. Do not re-use ldisc numbers as they are part of the userspace ABIand writing over an existing ldisc will cause demons to eat your computer.After the return the ldisc data has been copied so you may free your owncopy of the structure. You must not re-register over the top of the linediscipline even with the same data or your computer again will be eaten bydemons.In order to remove a line discipline call tty_unregister_ldisc().In ancient times this always worked. In modern times the function willreturn -EBUSY if the ldisc is currently in use. Since the ldisc referencingcode manages the module counts this should not usually be a concern.Heed this warning: the reference count field of the registered copies of thetty_ldisc structure in the ldisc table counts the number of lines using thisdiscipline. The reference count of the tty_ldisc structure within a ttycounts the number of active users of the ldisc at this instant. In effect itcounts the number of threads of execution within an ldisc method (plus thoseabout to enter and exit although this detail matters not).Line Discipline Methods-----------------------TTY side interfaces:open() - Called when the line discipline is attached tothe terminal. No other call into the linediscipline for this tty will occur until itcompletes successfully. Can sleep.close() - This is called on a terminal when the linediscipline is being unplugged. At the point ofexecution no further users will enter theldisc code for this tty. Can sleep.hangup() - Called when the tty line is hung up.The line discipline should cease I/O to the tty.No further calls into the ldisc code will occur.Can sleep.write() - A process is writing data through the linediscipline. Multiple write calls are serializedby the tty layer for the ldisc. May sleep.flush_buffer() - (optional) May be called at any point betweenopen and close, and instructs the line disciplineto empty its input buffer.chars_in_buffer() - (optional) Report the number of bytes in the inputbuffer.set_termios() - (optional) Called on termios structure changes.The caller passes the old termios data and thecurrent data is in the tty. Called under thetermios semaphore so allowed to sleep. Serializedagainst itself only.read() - Move data from the line discipline to the user.Multiple read calls may occur in parallel and theldisc must deal with serialization issues. Maysleep.poll() - Check the status for the poll/select calls. Multiplepoll calls may occur in parallel. May sleep.ioctl() - Called when an ioctl is handed to the tty layerthat might be for the ldisc. Multiple ioctl callsmay occur in parallel. May sleep.Driver Side Interfaces:receive_buf() - Hand buffers of bytes from the driver to the ldiscfor processing. Semantics currently rathermysterious 8(write_wakeup() - May be called at any point between open and close.The TTY_DO_WRITE_WAKEUP flag indicates if a callis needed but always races versus calls. Thus theldisc must be careful about setting order and tohandle unexpected calls. Must not sleep.The driver is forbidden from calling this directlyfrom the ->write call from the ldisc as the ldiscis permitted to call the driver write method fromthis function. In such a situation defer it.Driver AccessLine discipline methods can call the following methods of the underlyinghardware driver through the function pointers within the tty->driverstructure:write() Write a block of characters to the tty device.Returns the number of characters accepted. Thecharacter buffer passed to this method is alreadyin kernel space.put_char() Queues a character for writing to the tty device.If there is no room in the queue, the character isignored.flush_chars() (Optional) If defined, must be called afterqueueing characters with put_char() in order tostart transmission.write_room() Returns the numbers of characters the tty driverwill accept for queueing to be written.ioctl() Invoke device specific ioctl.Expects data pointers to refer to userspace.Returns ENOIOCTLCMD for unrecognized ioctl numbers.set_termios() Notify the tty driver that the device's termiossettings have changed. New settings are intty->termios. Previous settings should be passed inthe "old" argument.The API is defined such that the driver should returnthe actual modes selected. This means that thedriver function is responsible for modifying anybits in the request it cannot fulfill to indicatethe actual modes being used. A device with nohardware capability for change (eg a USB dongle orvirtual port) can provide NULL for this method.throttle() Notify the tty driver that input buffers for theline discipline are close to full, and it shouldsomehow signal that no more characters should besent to the tty.unthrottle() Notify the tty driver that characters can now besent to the tty without fear of overrunning theinput buffers of the line disciplines.stop() Ask the tty driver to stop outputting charactersto the tty device.start() Ask the tty driver to resume sending charactersto the tty device.hangup() Ask the tty driver to hang up the tty device.break_ctl() (Optional) Ask the tty driver to turn on or offBREAK status on the RS-232 port. If state is -1,then the BREAK status should be turned on; ifstate is 0, then BREAK should be turned off.If this routine is not implemented, use ioctlsTIOCSBRK / TIOCCBRK instead.wait_until_sent() Waits until the device has written out all of thecharacters in its transmitter FIFO.send_xchar() Send a high-priority XON/XOFF character to the device.FlagsLine discipline methods have access to tty->flags field containing thefollowing interesting flags:TTY_THROTTLED Driver input is throttled. The ldisc should calltty->driver->unthrottle() in order to resumereception when it is ready to process more data.TTY_DO_WRITE_WAKEUP If set, causes the driver to call the ldisc'swrite_wakeup() method in order to resumetransmission when it can accept more datato transmit.TTY_IO_ERROR If set, causes all subsequent userspace read/writecalls on the tty to fail, returning -EIO.TTY_OTHER_CLOSED Device is a pty and the other side has closed.TTY_NO_WRITE_SPLIT Prevent driver from splitting up writes intosmaller chunks.LockingCallers to the line discipline functions from the tty layer are required totake line discipline locks. The same is true of calls from the driver sidebut not yet enforced.Three calls are now providedldisc = tty_ldisc_ref(tty);takes a handle to the line discipline in the tty and returns it. If no ldiscis currently attached or the ldisc is being closed and re-opened at thispoint then NULL is returned. While this handle is held the ldisc will notchange or go away.tty_ldisc_deref(ldisc)Returns the ldisc reference and allows the ldisc to be closed. Returning thereference takes away your right to call the ldisc functions until you takea new reference.ldisc = tty_ldisc_ref_wait(tty);Performs the same function as tty_ldisc_ref except that it will wait for anldisc change to complete and then return a reference to the new ldisc.While these functions are slightly slower than the old code they should haveminimal impact as most receive logic uses the flip buffers and they onlyneed to take a reference when they push bits up through the driver.A caution: The ldisc->open(), ldisc->close() and driver->set_ldiscfunctions are called with the ldisc unavailable. Thus tty_ldisc_ref willfail in this situation if used within these functions. Ldisc and drivercode calling its own functions must be careful in this case.Driver Interface----------------open() - Called when a device is opened. May sleepclose() - Called when a device is closed. At the point ofreturn from this call the driver must make nofurther ldisc calls of any kind. May sleepwrite() - Called to write bytes to the device. May notsleep. May occur in parallel in special cases.Because this includes panic paths drivers generallyshouldn't try and do clever locking here.put_char() - Stuff a single character onto the queue. Thedriver is guaranteed following up calls toflush_chars.flush_chars() - Ask the kernel to write put_char queuewrite_room() - Return the number of characters tht can be stuffedinto the port buffers without overflow (or less).The ldisc is responsible for being intelligentabout multi-threading of write_room/write callsioctl() - Called when an ioctl may be for the driverset_termios() - Called on termios change, serialized againstitself by a semaphore. May sleep.set_ldisc() - Notifier for discipline change. At the point thisis done the discipline is not yet usable. Can nowsleep (I think)throttle() - Called by the ldisc to ask the driver to do flowcontrol. Serialization including with unthrottleis the job of the ldisc layer.unthrottle() - Called by the ldisc to ask the driver to stop flowcontrol.stop() - Ldisc notifier to the driver to stop output. As withthrottle the serializations with start() are downto the ldisc layer.start() - Ldisc notifier to the driver to start output.hangup() - Ask the tty driver to cause a hangup initiatedfrom the host side. [Can sleep ??]break_ctl() - Send RS232 break. Can sleep. Can get called inparallel, driver must serialize (for now), andwith write calls.wait_until_sent() - Wait for characters to exit the hardware queueof the driver. Can sleepsend_xchar() - Send XON/XOFF and if possible jump the queue withit in order to get fast flow control responses.Cannot sleep ??
Go to most recent revision | Compare with Previous | Blame | View Log
