TTY Struct

structtty_struct is allocated by the TTY layer upon the first open of the TTYdevice and released after the last close. The TTY layer passes this structureto most ofstructtty_operation’s hooks. Members of tty_struct are documentedinTTY Struct Reference at the bottom.

Initialization

voidtty_init_termios(structtty_struct*tty)

helper for termios setup

Parameters

structtty_struct*tty

the tty to set up

Description

Initialise the termios structure for this tty. This runs under thetty_mutex currently so we can be relaxed about ordering.

Name

constchar*tty_name(conststructtty_struct*tty)

return tty naming

Parameters

conststructtty_struct*tty

tty structure

Description

Convert a tty structure into a name. The name reflects the kernel namingpolicy and if udev is in use may not reflect user space

Locking: none

Reference counting

structtty_struct*tty_kref_get(structtty_struct*tty)

get a tty reference

Parameters

structtty_struct*tty

tty device

Return

a new reference to a tty object

Description

Locking: The caller must hold sufficient locks/counts to ensure that theirexisting reference cannot go away.

voidtty_kref_put(structtty_struct*tty)

release a tty kref

Parameters

structtty_struct*tty

tty device

Description

Release a reference to thetty device and if need be let the kref layerdestruct the object for us.

Install

inttty_standard_install(structtty_driver*driver,structtty_struct*tty)

usual tty->ops->install

Parameters

structtty_driver*driver

the driver for the tty

structtty_struct*tty

the tty

Description

If thedriver overridestty->ops->install, it still can call this functionto perform the standard install operations.

Read & Write

inttty_put_char(structtty_struct*tty,u8ch)

write one character to a tty

Parameters

structtty_struct*tty

tty

u8ch

character to write

Description

Write one byte to thetty using the providedtty->ops->put_char() methodif present.

Note

the specific put_char operation in the driver layer may goaway soon. Don’t call it directly, use this method

Return

the number of characters successfully output.

Start & Stop

voidstop_tty(structtty_struct*tty)

propagate flow control

Parameters

structtty_struct*tty

tty to stop

Description

Perform flow control to the driver. May be called on an already stoppeddevice and will not re-call thetty_driver->stop() method.

This functionality is used by both the line disciplines for halting incomingflow and by the driver. It may therefore be called from any context, may beunder the ttyatomic_write_lock but not always.

Locking:

flow.lock

voidstart_tty(structtty_struct*tty)

propagate flow control

Parameters

structtty_struct*tty

tty to start

Description

Start a tty that has been stopped if at all possible. Iftty was previouslystopped and is now being started, thetty_driver->start() method is invokedand the line discipline woken.

Locking:

flow.lock

Wakeup

voidtty_wakeup(structtty_struct*tty)

request more data

Parameters

structtty_struct*tty

terminal

Description

Internal and external helper for wakeups of tty. This function informs theline discipline if present that the driver is ready to receive more outputdata.

Hangup

voidtty_hangup(structtty_struct*tty)

trigger a hangup event

Parameters

structtty_struct*tty

tty to hangup

Description

A carrier loss (virtual or otherwise) has occurred ontty. Schedule ahangup sequence to run after this event.

voidtty_vhangup(structtty_struct*tty)

process vhangup

Parameters

structtty_struct*tty

tty to hangup

Description

The user has asked via system call for the terminal to be hung up. We dothis synchronously so that when the syscall returns the process is complete.That guarantee is necessary for security reasons.

inttty_hung_up_p(structfile*filp)

was tty hung up

Parameters

structfile*filp

file pointer of tty

Return

true if the tty has been subject to a vhangup or a carrier loss

Misc

inttty_do_resize(structtty_struct*tty,structwinsize*ws)

resize event

Parameters

structtty_struct*tty

tty being resized

structwinsize*ws

new dimensions

Description

Update the termios variables and send the necessary signals to peform aterminal resize correctly.

TTY Struct Flags

enumtty_struct_flags

TTY Struct Flags

Constants

TTY_THROTTLED

Driver input is throttled. The ldisc should calltty_driver.unthrottle() in order to resume reception whenit is ready to process more data (at threshold min).

TTY_IO_ERROR

If set, causes all subsequent userspace read/write calls on the tty tofail, returning -EIO. (May be no ldisc too.)

TTY_OTHER_CLOSED

Device is a pty and the other side has closed.

TTY_EXCLUSIVE

Exclusive open mode (a single opener).

TTY_DO_WRITE_WAKEUP

If set, causes the driver to call thetty_ldisc_ops.write_wakeup() method in order to resumetransmission when it can accept more data to transmit.

TTY_LDISC_OPEN

Indicates that a line discipline is open. For debugging purposes only.

TTY_PTY_LOCK

A flag private to pty code to implementTIOCSPTLCK/TIOCGPTLCK logic.

TTY_NO_WRITE_SPLIT

Prevent driver from splitting up writes into smaller chunks (preservewrite boundaries to driver).

TTY_HUPPED

The TTY was hung up. This is set posttty_driver.hangup().

TTY_HUPPING

The TTY is in the process of hanging up to abort potential readers.

TTY_LDISC_CHANGING

Line discipline for this TTY is being changed. I/O should not blockwhen this is set. Usetty_io_nonblock() to check.

TTY_LDISC_HALTED

Line discipline for this TTY was stopped. No work should be queued tothis ldisc.

Description

These bits are used in thetty_struct.flags field.

So that interrupts won’t be able to mess up the queues,copy_to_cooked must be atomic with respect to itself, as musttty->write. Thus, you must use the inline functionsset_bit() andclear_bit() to make things atomic.

TTY Struct Reference

structtty_struct

state associated with a tty while open

Definition:

struct tty_struct {    struct kref kref;    int index;    struct device *dev;    struct tty_driver *driver;    struct tty_port *port;    const struct tty_operations *ops;    struct tty_ldisc *ldisc;    struct ld_semaphore ldisc_sem;    struct mutex atomic_write_lock;    struct mutex legacy_mutex;    struct mutex throttle_mutex;    struct rw_semaphore termios_rwsem;    struct mutex winsize_mutex;    struct ktermios termios, termios_locked;    char name[64];    unsigned long flags;    int count;    unsigned int receive_room;    struct winsize winsize;    struct {        spinlock_t lock;        bool stopped;        bool tco_stopped;    } flow;    struct {        struct pid *pgrp;        struct pid *session;        spinlock_t lock;        unsigned char pktstatus;        bool packet;    } ctrl;    bool hw_stopped;    bool closing;    int flow_change;    struct tty_struct *link;    struct fasync_struct *fasync;    wait_queue_head_t write_wait;    wait_queue_head_t read_wait;    struct work_struct hangup_work;    void *disc_data;    void *driver_data;    spinlock_t files_lock;    int write_cnt;    u8 *write_buf;    struct list_head tty_files;    struct work_struct SAK_work;};

Members

kref

reference counting bytty_kref_get() andtty_kref_put(), reaching zerofrees the structure

index

index of this tty (e.g. to constructname like tty12)

dev

class device orNULL (e.g. ptys, serdev)

driver

structtty_driver operating this tty

port

persistent storage for this device (i.e.structtty_port)

ops

structtty_operations ofdriver for this tty (open, close, etc.)

ldisc

the current line discipline for this tty (n_tty by default)

ldisc_sem

protects line discipline changes (ldisc) -- lock tty not pty

atomic_write_lock

protects against concurrent writers, i.e. lockswrite_cnt,write_buf and similar

legacy_mutex

leftover from history (BKL -> BTM ->legacy_mutex),protecting several operations on this tty

throttle_mutex

protects against concurrenttty_throttle_safe() andtty_unthrottle_safe() (but nottty_unthrottle())

termios_rwsem

protectstermios andtermios_locked

winsize_mutex

protectswinsize

termios

termios for the current tty, copied from/todriver.termios

termios_locked

locked termios (byTIOCGLCKTRMIOS andTIOCSLCKTRMIOSioctls)

name

name of the tty constructed bytty_line_name() (e.g. ttyS3)

flags

bitwise OR ofTTY_THROTTLED,TTY_IO_ERROR, ...

count

count of open processes, reaching zero cancels all the work forthis tty and drops akref too (but does not free this tty)

receive_room

bytes permitted to feed toldisc without any being lost

winsize

size of the terminal “window” (cf.winsize_mutex)

flow

flow settings grouped together

flow.lock

lock forflow members

flow.stopped

tty stopped/started bystop_tty()/start_tty()

flow.tco_stopped

tty stopped/started byTCOOFF/TCOON ioctls (it hasprecedence overflow.stopped)

ctrl

control settings grouped together

ctrl.pgrp

process group of this tty (setpgrp(2))

ctrl.session

session of this tty (setsid(2)). Writes are protected by bothctrl.lock andlegacy_mutex, readers must use at least one ofthem.

ctrl.lock

lock forctrl members

ctrl.pktstatus

packet mode status (bitwise OR ofTIOCPKT_ constants)

ctrl.packet

packet mode enabled

hw_stopped

not controlled by the tty layer, underdriver’s control for CTShandling

closing

when set during close, n_tty processes only START & STOP chars

flow_change

controls behavior of throttling, seetty_throttle_safe() andtty_unthrottle_safe()

link

link to another pty (master -> slave and vice versa)

fasync

state forO_ASYNC (forSIGIO); managed byfasync_helper()

write_wait

concurrent writers are waiting in this queue until they areallowed to write

read_wait

readers wait for data in this queue

hangup_work

normally a work to perform a hangup (do_tty_hangup()); whilefreeing the tty, (re)used torelease_one_tty()

disc_data

pointer toldisc’s private data (e.g. tostructn_tty_data)

driver_data

pointer todriver’s private data (e.g.structuart_state)

files_lock

protectstty_files list

write_cnt

count of bytes written intty_write() towrite_buf

write_buf

temporary buffer used duringtty_write() to copy user data to

tty_files

list of (re)openers of this tty (i.e. linkedstructtty_file_private)

SAK_work

if the tty has a pending do_SAK, it is queued here

Description

All of the state associated with a tty while the tty is open. Persistentstorage for tty devices is referenced here asport and is documented instructtty_port.