TTY Subsystem

The TTY Subsystem is made up of

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.

^ Back to top.

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.

^ Back to top.

Registering a TTY Device

A new TTY device is registered by calling

void tty_register(tty_t *tty, char *name)

Register a tty device to the system, exposing its file operations.

Parameters:
  • tty – A tty that has been allocated by tty_alloc before

  • name – The name of the tty, for use, e.g., in the dev filesystem

^ Back to top.

Inserting a character from lower-level input

The TTY subsystem receives input characters via

void tty_insert_char(tty_t *tty, char character)

Insert a character from lower-level input and pass to the line discipline of tty.

^ Back to top.

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 in file.

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

^ Back to top.

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 in file.

Parameters:
  • inode – the inode representing the tty device

  • file – the file struct to refer to the opened tty

^ Back to top.

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 file for reading

^ Back to top.

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 file for writing

^ Back to top.

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.

^ Back to top.

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.

Public Members

ssize_t (*write)(tty_t *tty, const char *buf, size_t count)

Pass output to the lower level, e.g., a console.

Param tty:

the respective tty

Param buf:

the buffer containing the output to be written

Param count:

the number of characters to be written

^ Back to top.

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 tty when 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 tty when it gets opened.

int (*close)(tty_t *tty)

Close, i.e., release, a line discipline for a tty.

To be called by the tty when 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

^ Back to top.