TTY Buffer
Here, we document functions for taking care of tty buffer and their flipping. Drivers are supposed to fill the buffer by one of those functions below and then flip the buffer, so that the data are passed to line discipline for further processing.
Flip Buffer Management #
tty_prepare_flip_string() #
size_t make room for characters
Parameters
struct tty_port *port: tty portu8 **chars: return pointer for character write areasize_t size: desired size
Description
Prepare a block of space in the buffer for data.
This is used for drivers that need their own block copy routines into the buffer. There is no guarantee the buffer is a DMA target!
Return
the length available and buffer pointer (chars) to the space which is now allocated and accounted for as ready for normal characters.
tty_ldisc_receive_buf() #
size_t forward data to line discipline
Parameters
struct tty_ldisc *ld: line discipline to process inputconst u8 *p: char bufferconst u8 *f:TTY_NORMAL,TTY_BREAK, etc. flags buffersize_t count: number of bytes to process
Description
Callers other than flush_to_ldisc() need to exclude the kworker from
concurrent use of the line discipline, see paste_selection().
Return
the number of bytes processed.
tty_flip_buffer_push() #
void push terminal buffers
Parameters
struct tty_port *port: tty port to push
Description
Queue a push of the terminal flip buffers to the line discipline. Can be called from IRQ/atomic context.
In the event of the queue being busy for flipping the work will be held off and retried later.
tty_insert_flip_string_fixed_flag() #
size_t add characters to the tty buffer
Parameters
struct tty_port *port: tty portconst u8 *chars: charactersu8 flag: flag value for each charactersize_t size: size
Description
Queue a series of bytes to the tty buffering. All the characters passed are marked with the supplied flag.
Return
the number added.
tty_insert_flip_string_flags() #
size_t add characters to the tty buffer
Parameters
struct tty_port *port: tty portconst u8 *chars: charactersconst u8 *flags: flag bytessize_t size: size
Description
Queue a series of bytes to the tty buffering. For each character the flags array indicates the status of the character.
Return
the number added.
tty_insert_flip_char() #
size_t add one character to the tty buffer
Parameters
struct tty_port *port: tty portu8 ch: characteru8 flag: flag byte
Description
Queue a single byte ch to the tty buffering, with an optional flag.
Other Functions #
tty_buffer_space_avail() #
unsigned int return unused buffer space
Parameters
struct tty_port *port: tty port owning the flip buffer
Return
the # of bytes which can be written by the driver without reaching the buffer limit.
Note
this does not guarantee that memory is available to write the returned
# of bytes (use tty_prepare_flip_string() to pre-allocate if memory
guarantee is required).
tty_buffer_set_limit() #
int change the tty buffer memory limit
Parameters
struct tty_port *port: tty port to changeint limit: memory limit to set
Description
Change the tty buffer memory limit.
Must be called before the other tty buffer functions are used.
Buffer Locking #
These are used only in special circumstances. Avoid them.
tty_buffer_lock_exclusive() #
void gain exclusive access to buffer
Parameters
struct tty_port *port: tty port owning the flip buffer
Description
Guarantees safe use of the tty_ldisc_ops.receive_buf() method by excluding
the buffer work and any pending flush from using the flip buffer. Data can
continue to be added concurrently to the flip buffer from the driver side.
See also tty_buffer_unlock_exclusive().
tty_buffer_unlock_exclusive() #
void release exclusive access
Parameters
struct tty_port *port: tty port owning the flip buffer
Description
The buffer work is restarted if there is data in the flip buffer.
See also tty_buffer_lock_exclusive().
Internal Functions #
tty_buffer_free_all() #
void free buffers used by a tty
Parameters
struct tty_port *port: tty port to free from
Description
Remove all the buffers pending on a tty whether queued with data or in the free ring. Must be called when the tty is no longer in use.
tty_buffer_alloc() #
struct tty_buffer * allocate a tty buffer
Parameters
struct tty_port *port: tty portsize_t size: desired size (characters)
Description
Allocate a new tty buffer to hold the desired number of characters. We round our buffers off in 256 character chunks to get better allocation behaviour.
Return
NULL if out of memory or the allocation would exceed the per
device queue.
tty_buffer_free() #
void free a tty buffer
Parameters
struct tty_port *port: tty port owning the bufferstruct tty_buffer *b: the buffer to free
Description
Free a tty buffer, or add it to the free list according to our internal strategy.
tty_buffer_flush() #
void flush full tty buffers
Parameters
struct tty_struct *tty: tty to flushstruct tty_ldisc *ld: optional ldisc ptr (must be referenced)
Description
Flush all the buffers containing receive data. If ld != NULL, flush the
ldisc input buffer.
Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
__tty_buffer_request_room() #
int grow tty buffer if needed
Parameters
struct tty_port *port: tty portsize_t size: size desiredbool flags: buffer has to store flags along character data
Description
Make at least size bytes of linear space available for the tty buffer.
Will change over to a new buffer if the current buffer is encoded as
TTY_NORMAL (so has no flags buffer) and the new buffer requires a flags
buffer.
Return
the size we managed to find.
flush_to_ldisc() #
void flush data from buffer to ldisc
Parameters
struct work_struct *work: tty structure passed from work queue.
Description
This routine is called out of the software interrupt to flush data from the buffer chain to the line discipline.
The receive_buf() method is single threaded for each tty instance.
Locking: takes buffer lock to ensure single-threaded flip buffer 'consumer'.
tty_insert_flip_string_and_push_buffer() #
int add characters to the tty buffer and push
Parameters
struct tty_port *port: tty portconst u8 *chars: characterssize_t size: size
Description
The function combines tty_insert_flip_string() and tty_flip_buffer_push()
with the exception of properly holding the port->lock.
To be used only internally (by pty currently).
Return
the number added.
tty_buffer_init() #
void prepare a tty buffer structure
Parameters
struct tty_port *port: tty port to initialise
Description
Set up the initial state of the buffer management for a tty device. Must be called before the other tty buffer functions are used.