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*ttythe 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*ttytty 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*ttytty 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*ttytty 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*driverthe driver for the tty
structtty_struct*ttythe 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*ttytty
u8chcharacter 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*ttytty 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*ttytty 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*ttyterminal
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*ttytty 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*ttytty 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.
Parameters
structfile*filpfile 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*ttytty being resized
structwinsize*wsnew 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_THROTTLEDDriver input is throttled. The ldisc should call
tty_driver.unthrottle()in order to resume reception whenit is ready to process more data (at threshold min).TTY_IO_ERRORIf set, causes all subsequent userspace read/write calls on the tty tofail, returning -
EIO. (May be no ldisc too.)TTY_OTHER_CLOSEDDevice is a pty and the other side has closed.
TTY_EXCLUSIVEExclusive open mode (a single opener).
TTY_DO_WRITE_WAKEUPIf set, causes the driver to call the
tty_ldisc_ops.write_wakeup()method in order to resumetransmission when it can accept more data to transmit.TTY_LDISC_OPENIndicates that a line discipline is open. For debugging purposes only.
TTY_PTY_LOCKA flag private to pty code to implement
TIOCSPTLCK/TIOCGPTLCKlogic.TTY_NO_WRITE_SPLITPrevent driver from splitting up writes into smaller chunks (preservewrite boundaries to driver).
TTY_HUPPEDThe TTY was hung up. This is set post
tty_driver.hangup().TTY_HUPPINGThe TTY is in the process of hanging up to abort potential readers.
TTY_LDISC_CHANGINGLine discipline for this TTY is being changed. I/O should not blockwhen this is set. Use
tty_io_nonblock()to check.TTY_LDISC_HALTEDLine 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
krefreference counting by
tty_kref_get()andtty_kref_put(), reaching zerofrees the structureindexindex of this tty (e.g. to constructname like tty12)
devclass device or
NULL(e.g. ptys, serdev)driverstructtty_driveroperating this ttyportpersistent storage for this device (i.e.
structtty_port)opsstructtty_operationsofdriver for this tty (open, close, etc.)ldiscthe current line discipline for this tty (n_tty by default)
ldisc_semprotects line discipline changes (ldisc) -- lock tty not pty
atomic_write_lockprotects against concurrent writers, i.e. lockswrite_cnt,write_buf and similar
legacy_mutexleftover from history (BKL -> BTM ->legacy_mutex),protecting several operations on this tty
throttle_mutexprotects against concurrent
tty_throttle_safe()andtty_unthrottle_safe()(but nottty_unthrottle())termios_rwsemprotectstermios andtermios_locked
winsize_mutexprotectswinsize
termiostermios for the current tty, copied from/todriver.termios
termios_lockedlocked termios (by
TIOCGLCKTRMIOSandTIOCSLCKTRMIOSioctls)namename of the tty constructed by
tty_line_name()(e.g. ttyS3)flagsbitwise OR of
TTY_THROTTLED,TTY_IO_ERROR, ...countcount of open processes, reaching zero cancels all the work forthis tty and drops akref too (but does not free this tty)
receive_roombytes permitted to feed toldisc without any being lost
winsizesize of the terminal “window” (cf.winsize_mutex)
flowflow settings grouped together
flow.locklock forflow members
flow.stoppedtty stopped/started by
stop_tty()/start_tty()flow.tco_stoppedtty stopped/started by
TCOOFF/TCOONioctls (it hasprecedence overflow.stopped)ctrlcontrol settings grouped together
ctrl.pgrpprocess group of this tty (setpgrp(2))
ctrl.sessionsession of this tty (setsid(2)). Writes are protected by bothctrl.lock andlegacy_mutex, readers must use at least one ofthem.
ctrl.locklock forctrl members
ctrl.pktstatuspacket mode status (bitwise OR of
TIOCPKT_constants)ctrl.packetpacket mode enabled
hw_stoppednot controlled by the tty layer, underdriver’s control for CTShandling
closingwhen set during close, n_tty processes only START & STOP chars
flow_changecontrols behavior of throttling, see
tty_throttle_safe()andtty_unthrottle_safe()linklink to another pty (master -> slave and vice versa)
fasyncstate for
O_ASYNC(forSIGIO); managed byfasync_helper()write_waitconcurrent writers are waiting in this queue until they areallowed to write
read_waitreaders wait for data in this queue
hangup_worknormally a work to perform a hangup (
do_tty_hangup()); whilefreeing the tty, (re)used torelease_one_tty()disc_datapointer toldisc’s private data (e.g. to
structn_tty_data)driver_datapointer todriver’s private data (e.g.
structuart_state)files_lockprotectstty_files list
write_cntcount of bytes written in
tty_write()towrite_bufwrite_buftemporary buffer used during
tty_write()to copy user data totty_fileslist of (re)openers of this tty (i.e. linked
structtty_file_private)SAK_workif 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.