TTY Subsystem¶
The TTY Subsystem is made up of
the tty drivers, providing tty devices – currently only the virtual terminal driver (see there)
the implementation of the subsystem itself, providing means for managing ttys
a standard line discipline, processing input and output
the virtual terminal subsystem, providing virtual terminals
Additionally, the tty subsystem provides a tty frontend to be used by tty drivers.
The TTY Subsystem is implemented in
os/drivers/tty/tty.c.
This subsystem allocates new ttys and provides them with a set of default file operations. It also provides a means to registering tty devices.
Initialization¶
This subsystem is initialized by calling
-
void tty_init(void)¶
Initialize the tty subsystem and the virtual terminal subsystem.
Allocating a TTY¶
A new tty is allocated by calling
-
tty_t *tty_alloc(void)¶
Allocate a new tty.
Store the new tty’s minor device number in the tty struct. Store the default file operations in the tty struct. Store the new tty in the list of available ttys.
Note
This function is intended to be called by the respective tty driver.
- Returns:
: An initialized tty.
Registering a TTY Device¶
A new TTY device is registered by calling
Inserting a character from lower-level input¶
The TTY subsystem receives input characters via
File Operations¶
The tty subsystem implements a set of file operations, which are assigned to the tty upon allocation. This set of file operations can be regarded as the tty frontend.
Opening and Closing a tty¶
The tty subsystem provides a file operation for opening a tty device:
-
static int __tty_fops_open(inode_t *inode, file_t *file)¶
Open the tty represented by
inode, store tty pointer infile.Opening a tty will also open, i.e., allocate and attach, the line discipline and the respective data.
- Parameters:
inode – the inode representing the tty device
file – the file struct to refer to the opened tty
- Returns:
0 on success
- Returns:
(an ldisc error code) if ldisc open failed
- Returns:
-EINVAL if no tty could be found for the minor device id
It furthermore provides a file operation for closing a tty device:
-
static int __tty_fops_close(inode_t *inode, file_t *file)¶
Close the tty represented by
inode, opened infile.- Parameters:
inode – the inode representing the tty device
file – the file struct to refer to the opened tty
Reading from and Writing to a tty¶
The tty subsystem provides a file operation for reading from an open tty device:
-
static ssize_t __tty_fops_read(file_t *file, char *buffer, size_t buffer_size, size_t *offset)¶
Read input from the open tty referred to by
file.- Parameters:
file – the file struct referring to the open tty
buffer – the user-space buffer to read into
buffer_size – the size of that buffer
offset – the offset into the
filefor reading
It furthermore provides a file operation for writing to an open tty device:
-
static ssize_t __tty_fops_write(file_t *file, const char *buffer, size_t buffer_size, size_t *offset)¶
Write to the open tty referred to by
file.- Parameters:
file – the file struct referring to the open tty
buffer – the user-space buffer containing the output
buffer_size – the size of that buffer
offset – the offset into the
filefor writing
Data Types Provided by the Subsystem¶
The tty subsystem provides the following data types.
TTY Data Structure¶
The data structure representing a tty device, keeping track of tty-related data.
This data structure is implemented in os/drivers/tty/tty.h.
-
struct tty_t¶
Information required for operating a tty.
Public Members
-
const tty_operations_ttty_operations_t *tty_ops¶
Interface for tty <-> driver, implementing the tty.
-
uint8_t minor¶
Minor device number.
-
const file_operations_t *file_ops¶
File operations of this tty.
-
const tty_ldisc_operations_ttty_ldisc_operations_t *ldisc_ops¶
Interface of the registered line discipline.
-
void *ldisc_data¶
Private data of the registered line disipline.
-
void *driver_data¶
Private data of the driver implementing the tty.
-
const tty_operations_ttty_operations_t *tty_ops¶
TTY Operations¶
The interface between the tty frontend and the tty backend.
This data structure is implemented in os/drivers/tty/tty_operations.h.
-
struct tty_operations_t¶
Interface between the tty frontend and the tty backend.
Defines the operations that a tty driver must implement when using the default tty frontend. This frontend provides file operations, which in turn invoke tty operations to, for example, write output to a lower-level console.
TTY Line Discipline Operations¶
The interface between the tty (subsystem) and the line discipline.
This data structure is implemented in os/drivers/tty/tty_ldisc.h.
-
struct tty_ldisc_operations_t¶
The interface between the tty (subsystem) and the line discipline.
Public Members
-
void (*receive_char)(tty_t *tty, char c)¶
Receive a char from lower-level input.
To be called by
ttywhen input is passed, e.g., from the keyboard input handler.
-
int (*open)(tty_t *tty)¶
Open, i.e., initialize, a line discipline for a
tty.To be called by the
ttywhen it gets opened.
-
int (*close)(tty_t *tty)¶
Close, i.e., release, a line discipline for a
tty.To be called by the
ttywhen it gets closed.
-
ssize_t (*read)(tty_t *tty, file_t *file, char *buffer, size_t buffer_size, size_t *offset)¶
Read input from the line discipline.
When a user process is attempting to read from a
tty, that tty will pass the request on to the line discipline.- Param tty:
the tty to read from
- Param file:
the file to read from
- Param buffer:
the buffer to read the data into
- Param buffer_size:
the size of that buffer
- Param offset:
the offset in the file
-
ssize_t (*write)(tty_t *tty, file_t *file, const char *buffer, size_t buffer_size, size_t *offset)¶
Write output th the line discipline.
When a user process is attempting to write to a
tty, that tty will pass the request on to the line discipline.- Param tty:
the tty to write to
- Param file:
the file to write to
- Param buffer:
the buffer to write the data from
- Param buffer_size:
the size of that buffer
- Param offset:
the offset in the file
-
void (*receive_char)(tty_t *tty, char c)¶