AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
uart.c File Reference

Serial ports as terminals: /dev/ttyS0 and up (character device 4, 64 and up). More...

#include <core/system.h>
#include <dev/dev.h>
#include <dev/tty.h>
#include <dev/uart.h>
#include <fs/posix.h>
#include <ds/queue.h>
#include <ds/ringbuf.h>
#include <sys/sched.h>

Go to the source code of this file.

Macros

#define UART_BUF   64
#define UART_RING   4096
#define UART_MAX   192

Functions

void uart_receive_handler (struct uart *u, size_t size)
 Takes characters a UART received, and gives them to its terminal.
void uart_transmit_handler (struct uart *u, size_t size)
 Sends characters that the terminal output to its UART.
ssize_t uart_master_write (struct tty *tty, size_t size, void *buf)
 Takes the terminal's output, and sends it to the UART at once: the UART drivers don't use a "transmitter empty" interrupt.
static void uart_input_flush (struct tty *tty)
 Drops the input waiting to be read (tcflush()).
ssize_t uart_slave_write (struct tty *tty, size_t size, void *buf)
 Takes the terminal's input, after its line editing, for the readers of the UART's device.
static struct uart * uart_get (devid_t minor)
 Returns the UART of a minor (they start at 64), or NULL if there's none.
ssize_t uart_read (struct devid *dd, off_t offset, struct iobuf *io)
 Reads a UART's terminal.
ssize_t uart_write (struct devid *dd, off_t offset, struct iobuf *io)
 Writes to a UART's terminal.
static int uart_file_eof (struct file *file)
 Returns whether a read that found no data is at the end of the file (the terminal says, from its mode).
static uint64_t uart_read_timeout (struct file *file)
 Returns how long a read may wait for data, from the terminal's mode (VTIME), or 0 for no limit.
static int uart_can_read (struct file *file, size_t size)
 Returns whether a read wouldn't have to wait.
int uart_ioctl (struct devid *dd, int request, void *argp)
 Handles a UART terminal's ioctl()s, which are the terminal's.
static int uart_setup (struct uart *u, struct vnode *vnode)
 Sets a UART up the first time it is opened: its rings, its terminal, and the UART itself.
int uart_file_open (struct file *file)
 Opens a UART's device, setting the UART up the first time.
int uart_register (int id, struct uart *u)
 Registers a UART, as /dev/ttyS<id>.

Variables

static struct uart * devices [192] = {0}
struct dev uart

Detailed Description

Serial ports as terminals: /dev/ttyS0 and up (character device 4, 64 and up).

A UART driver (8250, PL011) registers its UARTs here, and this layer makes each one a terminal: it connects the UART to the terminal layer (generic.c), which does the line editing, with a ring each way. What the UART receives goes to the terminal; what the terminal outputs goes to the UART. A UART is set up the first time it is opened.

Definition in file uart.c.

Macro Definition Documentation

◆ UART_BUF

#define UART_BUF   64

Definition at line 21 of file uart.c.

Referenced by uart_receive_handler().

◆ UART_MAX

#define UART_MAX   192

Definition at line 115 of file uart.c.

Referenced by uart_get().

◆ UART_RING

#define UART_RING   4096

Definition at line 25 of file uart.c.

Referenced by uart_setup().

Function Documentation

◆ uart_can_read()

int uart_can_read ( struct file * file,
size_t size )
static

Returns whether a read wouldn't have to wait.

Definition at line 195 of file uart.c.

References DEV_MINOR, uart::in, uart::tty, tty_can_read(), uart_can_read(), and uart_get().

Referenced by uart_can_read().

◆ uart_file_eof()

int uart_file_eof ( struct file * file)
static

Returns whether a read that found no data is at the end of the file (the terminal says, from its mode).

Definition at line 180 of file uart.c.

References DEV_MINOR, uart::tty, tty_eof(), uart_file_eof(), and uart_get().

Referenced by uart_file_eof().

◆ uart_file_open()

int uart_file_open ( struct file * file)

Opens a UART's device, setting the UART up the first time.

Parameters
filethe file being opened
Returns
0, -ENXIO if no UART has that minor, or a negative errno

Definition at line 251 of file uart.c.

References DEV_MINOR, uart::read_queue, uart::tty, tty_open(), uart_file_open(), uart_get(), uart_setup(), and uart::write_queue.

Referenced by uart_file_open().

◆ uart_get()

struct uart * uart_get ( devid_t minor)
static

Returns the UART of a minor (they start at 64), or NULL if there's none.

Without virtual terminals, minor 1 (/dev/console) is the first UART too.

Definition at line 120 of file uart.c.

References devices, NULL, uart_get(), and UART_MAX.

Referenced by uart_can_read(), uart_file_eof(), uart_file_open(), uart_get(), uart_ioctl(), uart_read(), uart_read_timeout(), and uart_write().

◆ uart_input_flush()

void uart_input_flush ( struct tty * tty)
static

Drops the input waiting to be read (tcflush()).

Definition at line 91 of file uart.c.

References uart::in, tty::p, and uart_input_flush().

Referenced by uart_input_flush(), and uart_setup().

◆ uart_ioctl()

int uart_ioctl ( struct devid * dd,
int request,
void * argp )

Handles a UART terminal's ioctl()s, which are the terminal's.

Parameters
ddthe device's number
requestthe request
argpits argument
Returns
what the request returns, or a negative errno

Definition at line 209 of file uart.c.

References devid::minor, uart::tty, tty_ioctl(), uart_get(), and uart_ioctl().

Referenced by uart_ioctl().

◆ uart_master_write()

ssize_t uart_master_write ( struct tty * tty,
size_t size,
void * buf )

Takes the terminal's output, and sends it to the UART at once: the UART drivers don't use a "transmitter empty" interrupt.

Parameters
ttythe terminal
sizethe output's size, in bytes
bufthe output
Returns
how much of it was taken

Definition at line 82 of file uart.c.

References uart::out, tty::p, uart_master_write(), and uart_transmit_handler().

Referenced by uart_master_write(), and uart_setup().

◆ uart_read()

ssize_t uart_read ( struct devid * dd,
off_t offset,
struct iobuf * io )

Reads a UART's terminal.

A process of a background group gets SIGTTIN instead.

Parameters
ddthe device's number
offsetnot used: a terminal has no offset
iowhere to put what is read
Returns
how much was read, or a negative errno

Definition at line 143 of file uart.c.

References uart::in, devid::minor, iobuf::resid, uart::tty, tty_background(), uart_get(), and uart_read().

Referenced by uart_read().

◆ uart_read_timeout()

uint64_t uart_read_timeout ( struct file * file)
static

Returns how long a read may wait for data, from the terminal's mode (VTIME), or 0 for no limit.

Definition at line 188 of file uart.c.

References DEV_MINOR, uart::tty, tty_read_timeout(), uart_get(), and uart_read_timeout().

Referenced by uart_read_timeout().

◆ uart_receive_handler()

void uart_receive_handler ( struct uart * u,
size_t size )

Takes characters a UART received, and gives them to its terminal.

The UART's driver calls it from its interrupt handler.

Parameters
uthe UART
sizehow many characters to take from it

Definition at line 35 of file uart.c.

References MIN, uart::read_queue, uart::receive, uart::tty, tty_master_write(), UART_BUF, uart_receive_handler(), and waitq_wakeup().

Referenced by pl011_irq(), uart_8250_irq(), and uart_receive_handler().

◆ uart_register()

int uart_register ( int id,
struct uart * u )

Registers a UART, as /dev/ttyS<id>.

Parameters
idits number (below 192), or -1 for the first free one
uthe UART
Returns
its number, -EINVAL if id is too big, or -ENOSPC if every number is taken

Definition at line 278 of file uart.c.

References devices, uart::name, printk(), and uart_register().

Referenced by pl011_init(), uart_8250_attach(), and uart_register().

◆ uart_setup()

int uart_setup ( struct uart * u,
struct vnode * vnode )
static

Sets a UART up the first time it is opened: its rings, its terminal, and the UART itself.

Returns 0 or a negative errno.

Definition at line 218 of file uart.c.

References curproc, uart::in, uart::init, tty::input_flush, NULL, uart::out, vnode::ref, uart::tty, tty_new(), uart_input_flush(), uart_master_write(), UART_RING, uart_setup(), uart_slave_write(), tty::vnode, and uart::vnode.

Referenced by uart_file_open(), and uart_setup().

◆ uart_slave_write()

ssize_t uart_slave_write ( struct tty * tty,
size_t size,
void * buf )

Takes the terminal's input, after its line editing, for the readers of the UART's device.

Parameters
ttythe terminal
sizethe input's size, in bytes
bufthe input
Returns
how much of it was taken

Definition at line 109 of file uart.c.

References uart::in, tty::p, and uart_slave_write().

Referenced by uart_setup(), and uart_slave_write().

◆ uart_transmit_handler()

void uart_transmit_handler ( struct uart * u,
size_t size )

Sends characters that the terminal output to its UART.

Parameters
uthe UART
sizehow many characters to send, at most

Definition at line 59 of file uart.c.

References MIN, uart::out, uart::transmit, uart_transmit_handler(), waitq_wakeup(), and uart::write_queue.

Referenced by uart_8250_irq(), uart_master_write(), and uart_transmit_handler().

◆ uart_write()

ssize_t uart_write ( struct devid * dd,
off_t offset,
struct iobuf * io )

Writes to a UART's terminal.

With TOSTOP set, a process of a background group gets SIGTTOU instead.

Parameters
ddthe device's number
offsetnot used: a terminal has no offset
iowhat to write
Returns
how much was written, or a negative errno

Definition at line 163 of file uart.c.

References devid::minor, tty::tios, uart::tty, tty_background(), tty_slave_write(), tty_write_iobuf(), uart_get(), and uart_write().

Referenced by uart_write().

Variable Documentation

◆ devices

struct uart* devices[192] = {0}
static

Definition at line 26 of file uart.c.

Referenced by uart_get(), and uart_register().

◆ uart

struct dev uart
Initial value:
= {
.name = "uart",
.read = uart_read,
.write = uart_write,
.ioctl = uart_ioctl,
.fops = {
.open = uart_file_open,
.read = posix_file_read,
.write = posix_file_write,
.ioctl = posix_file_ioctl,
.can_read = uart_can_read,
.can_write = __vfs_can_always,
.eof = uart_file_eof,
.read_timeout = uart_read_timeout,
},
}
static int uart_file_eof(struct file *file)
Returns whether a read that found no data is at the end of the file (the terminal says,...
Definition uart.c:180
int uart_file_open(struct file *file)
Opens a UART's device, setting the UART up the first time.
Definition uart.c:251
ssize_t uart_read(struct devid *dd, off_t offset, struct iobuf *io)
Reads a UART's terminal.
Definition uart.c:143
static uint64_t uart_read_timeout(struct file *file)
Returns how long a read may wait for data, from the terminal's mode (VTIME), or 0 for no limit.
Definition uart.c:188
static int uart_can_read(struct file *file, size_t size)
Returns whether a read wouldn't have to wait.
Definition uart.c:195
int uart_ioctl(struct devid *dd, int request, void *argp)
Handles a UART terminal's ioctl()s, which are the terminal's.
Definition uart.c:209
ssize_t uart_write(struct devid *dd, off_t offset, struct iobuf *io)
Writes to a UART's terminal.
Definition uart.c:163
int posix_file_ioctl(struct file *file, int request, void *argp)
Passes an ioctl() request on to the file's vnode (a device's, or its filesystem's).
Definition ioctl.c:19
ssize_t posix_file_read(struct file *file, struct iobuf *io)
Reads from an open file at its offset, and moves the offset past what was read.
Definition read.c:29
ssize_t posix_file_write(struct file *file, struct iobuf *io)
Writes to an open file at its offset, and moves the offset past what was written.
Definition write.c:25

Definition at line 302 of file uart.c.