Difference between revisions of "TTY Driver"

From Embedded Xinu
Jump to navigation Jump to search
(Added Update template)
Line 1: Line 1:
 
{{Update}}
 
{{Update}}
[[Image:TtyDriver.png|right|450px]]The TTY driver is built on top of the UART driver (or another appropriate char-oriented device).  The driver is responsible for sending and receiving character input between a user level application, such as the [[Shell|XINU Shell]], and a char-oriented hardware driver.  The TTY driver uses a line disciple that reads and provides input in lines.  When not in raw mode, the TTY driver also cooks both input and output.  
+
[[Image:TtyDriver.png|right|450px]]The TTY driver serves as an intermediary device between hardware device drivers and user applications to provide line buffering of input and cooking of input and output.  The driver is purely software oriented and makes no direct communication with physical hardware.  Instead, the TTY driver relies on an underlying device driver to communicate directly with the hardware.  The [[Shell|XINU Shell]] utilizes a TTY device to line buffer and cook user input read from the [[UART_Driver|UART]].
  
 
== Open & Close ==
 
== Open & Close ==
Open associates a TTY with an underlying char-oriented hardware driver.  The underlying device should already be initialized.  A TTY is allocated from the available TTY devices and its circular buffers are initialized.
+
Open associates a TTY with an underlying char-oriented hardware device.  The underlying device driver must provide both getc and putc functions for the TTY to obtain input and send output character by character.  The device should already be opened and initialized before the TTY is openedWhen a TTY is opened, its device control block, input buffer, and flags are initialized. No input flags are set when a TTY device is opened.  The TTY_ONLCR output flag is set when a TTY device is opened. 
  
Close disasociates a TTY from its underlying device.  It also makes the TTY available to be allocated to other processes.
+
Close disassociates a TTY from its underlying device and resets the TTY's device control block.
  
 
== Read ==
 
== Read ==
The TTY driver reads characters from an underlying device driver, normally a [[UART Driver|UART]].  Input is read from the underlying device in lines and buffered in the TTY's input buffer.  Lines may be terminated with the line feed (LF or '\n') or carriage return (CR or <code>'\r'</code>) characters.  Read requests are not satsified until a full line has been read, or the TTY's buffer is full.
+
The TTY driver reads characters from an underlying device driver using the devices getc function.  Input is first buffered in the TTY driver's circular  buffer before being copied to the user buffer supplied as a parameter in the <code>ttyRead</code> function call.
 
When the TTY is not in raw input mode, lines of input are cooked.  Backspace (BS or <code>'\b'</code>) and delete (DEL or ASCII character 0x7F) remove the last character from the TTY's input buffer, and decrease the secondary buffer count by one.  Escape sequences and the null character (NUL or <code>'\0'</code>) are ignored.
 
  
When the end of file (Control+D)  character is read, the tty stops reading from the underlying deviceSubsquent calls to <code>ttyRead</code> will attempt to staisfy the request from the TTY's input buffer, otherwise <code>EOF</code> will be returned.  The TTY driver sets a flag if EOF (EOT or Control-D) or CBREAK (ETX or Control-C) are seen in the input that satisfies the read request. The flags can be checked using <code>control(''device id'', TTY_IOC_EOF, NULL NULL)</code> or <code>control(''device id'', TTY_IOC_CBREAK, NULL NULL)</code> for EOF and CBREAK, respectively.
+
The <code>ttyRead</code> function begins by checking the <code>ieof</code> flag to determine if the EOF character (Control+D) was read during the last call to <code>ttyRead</code> If the <code>ieof</code> flag is set, the function returns the <code>EOF</code> constant, defined in <code>stddef.h</code>.  <code>EOF</code> is only returned once for each EOF character read by the TTY driverA call made to <code>ttyRead</code> after <code>EOF</code> was returned, will result in an attempt to read more characters from the underlying device driver.
 +
 
 +
If the <code>TTY_IRAW</code> flag is set, the TTY driver performs no line buffering or line editing (input cooking).  The user buffer is first filled from any data remaining in the TTY driver's input buffer from the last <code>ttyRead</code> callThe remaining portion of the user supplied buffer is filled by reading characters from the underlying device driver.
 +
 
 +
The TTY driver performs line buffering and line editing (input cooking) when the <code>TTY_IRAW</code> flag is not set.  Characters are read from the underlying device driver until a line delimiter is read or the TTY driver's input buffer is full.  Lines may be terminated with the newline (LF or <code>'\n'</code>) or end of file (EOF or ASCII character 0x04) characters.  If the <code>TTY_ECHO</code> flag is set, each character is output as it is read.
 +
 
 +
Special handling is required for some characters to perform line editing (input cooking).  If the TTY driver's input buffer contains characters, backspace (BS or <code>'\b'</code>) and delete (DEL or ASCII character 0x7F) remove the last character from the TTY's input buffer.  The newline and carriage return (CR or <code>'\r'</code>) characters are cooked if certain input flags are set.  The end of file character causes the <code>ieof</code> flag to be set.  Any other unprintable characters are ignored.
 +
 
 +
After a line of input is buffered in the TTY's device driver, the user supplied buffer is filled from the TTY's input buffer.  If the end of file character was the only character read, the <code>EOF</code> constant is returned.  Otherwise, the number of characters read into the user buffer is returned.
 +
 
 +
The TTY driver has the following input flags:
 +
* <code>TTY_IRAW</code> - reads input unbuffered and uncooked
 +
* <code>TTY_INLCR</code> - converts '\n' to '\r'
 +
* <code>TTY_IGNCR</code> - ignores '\r'
 +
* <code>TTY_ICRNL</code> - converts '\r' to '\n'
 +
* <code>TTY_ECHO</code> - echoes input
  
 
== Write ==
 
== Write ==
The TTY driver does not buffer output; it writes characters to an underlying device driver.  When not in RAW output mode, the TTY driver cooks line feeds (LF or <code>'\n'</code>) and carriage returns (CR or <code>'\r'</code>). The <code>ttyWrite</code> function always ensures CRLF is written to the output buffer when any of the following escape sequences are to be written: <code>'\r'</code>, <code>'\n'</code>, or <code>'\r\n'</code>.  Backspace and delete are cooked to <code>'\b\0\b'</code> to remove the last character output.  Null characters are ignored.  When in RAW mode, set using <code>control(device id, TTC_ORAW, TRUE, NULL)</code>, no output is cooked
+
The TTY driver does not buffer output; it writes characters directly to an underlying device driver.  The TTY driver cooks newlines (LF or <code>'\n'</code>) and carriage returns (CR or <code>'\r'</code>) if certain output flags are set.
 +
 
 +
The TTY driver has the following output flags:
 +
* <code>TTY_ONLCR</code> - converts '\n' to '\r\n'
 +
* <code>TTY_OCRNL</code> - converts '\r' to '\n'
  
 
== See also ==
 
== See also ==
 
* [[UART Driver]]
 
* [[UART Driver]]

Revision as of 19:56, 23 May 2008

Clockimportant.png This article or section needs to be updated.
Parts of this article or section have been identified as no longer being up to date.
Please update the article to reflect recent changes, and remove this template when finished.

TtyDriver.png

The TTY driver serves as an intermediary device between hardware device drivers and user applications to provide line buffering of input and cooking of input and output. The driver is purely software oriented and makes no direct communication with physical hardware. Instead, the TTY driver relies on an underlying device driver to communicate directly with the hardware. The XINU Shell utilizes a TTY device to line buffer and cook user input read from the UART.

Open & Close

Open associates a TTY with an underlying char-oriented hardware device. The underlying device driver must provide both getc and putc functions for the TTY to obtain input and send output character by character. The device should already be opened and initialized before the TTY is opened. When a TTY is opened, its device control block, input buffer, and flags are initialized. No input flags are set when a TTY device is opened. The TTY_ONLCR output flag is set when a TTY device is opened.

Close disassociates a TTY from its underlying device and resets the TTY's device control block.

Read

The TTY driver reads characters from an underlying device driver using the devices getc function. Input is first buffered in the TTY driver's circular buffer before being copied to the user buffer supplied as a parameter in the ttyRead function call.

The ttyRead function begins by checking the ieof flag to determine if the EOF character (Control+D) was read during the last call to ttyRead If the ieof flag is set, the function returns the EOF constant, defined in stddef.h. EOF is only returned once for each EOF character read by the TTY driver. A call made to ttyRead after EOF was returned, will result in an attempt to read more characters from the underlying device driver.

If the TTY_IRAW flag is set, the TTY driver performs no line buffering or line editing (input cooking). The user buffer is first filled from any data remaining in the TTY driver's input buffer from the last ttyRead call. The remaining portion of the user supplied buffer is filled by reading characters from the underlying device driver.

The TTY driver performs line buffering and line editing (input cooking) when the TTY_IRAW flag is not set. Characters are read from the underlying device driver until a line delimiter is read or the TTY driver's input buffer is full. Lines may be terminated with the newline (LF or '\n') or end of file (EOF or ASCII character 0x04) characters. If the TTY_ECHO flag is set, each character is output as it is read.

Special handling is required for some characters to perform line editing (input cooking). If the TTY driver's input buffer contains characters, backspace (BS or '\b') and delete (DEL or ASCII character 0x7F) remove the last character from the TTY's input buffer. The newline and carriage return (CR or '\r') characters are cooked if certain input flags are set. The end of file character causes the ieof flag to be set. Any other unprintable characters are ignored.

After a line of input is buffered in the TTY's device driver, the user supplied buffer is filled from the TTY's input buffer. If the end of file character was the only character read, the EOF constant is returned. Otherwise, the number of characters read into the user buffer is returned.

The TTY driver has the following input flags:

  • TTY_IRAW - reads input unbuffered and uncooked
  • TTY_INLCR - converts '\n' to '\r'
  • TTY_IGNCR - ignores '\r'
  • TTY_ICRNL - converts '\r' to '\n'
  • TTY_ECHO - echoes input

Write

The TTY driver does not buffer output; it writes characters directly to an underlying device driver. The TTY driver cooks newlines (LF or '\n') and carriage returns (CR or '\r') if certain output flags are set.

The TTY driver has the following output flags:

  • TTY_ONLCR - converts '\n' to '\r\n'
  • TTY_OCRNL - converts '\r' to '\n'

See also