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 andthen flip the buffer, so that the data are passed toline discipline for further processing.

Flip Buffer Management

size_ttty_prepare_flip_string(structtty_port*port,u8**chars,size_tsize)

make room for characters

Parameters

structtty_port*port

tty port

u8**chars

return pointer for character write area

size_tsize

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 thebuffer. There is no guarantee the buffer is a DMA target!

Return

the length available and buffer pointer (chars) to the space whichis now allocated and accounted for as ready for normal characters.

size_ttty_ldisc_receive_buf(structtty_ldisc*ld,constu8*p,constu8*f,size_tcount)

forward data to line discipline

Parameters

structtty_ldisc*ld

line discipline to process input

constu8*p

char buffer

constu8*f

TTY_NORMAL,TTY_BREAK, etc. flags buffer

size_tcount

number of bytes to process

Description

Callers other thanflush_to_ldisc() need to exclude the kworker fromconcurrent use of the line discipline, seepaste_selection().

Return

the number of bytes processed.

voidtty_flip_buffer_push(structtty_port*port)

push terminal buffers

Parameters

structtty_port*port

tty port to push

Description

Queue a push of the terminal flip buffers to the line discipline. Can becalled from IRQ/atomic context.

In the event of the queue being busy for flipping the work will be held offand retried later.

size_ttty_insert_flip_string_fixed_flag(structtty_port*port,constu8*chars,u8flag,size_tsize)

add characters to the tty buffer

Parameters

structtty_port*port

tty port

constu8*chars

characters

u8flag

flag value for each character

size_tsize

size

Description

Queue a series of bytes to the tty buffering. All the characters passed aremarked with the supplied flag.

Return

the number added.

size_ttty_insert_flip_string_flags(structtty_port*port,constu8*chars,constu8*flags,size_tsize)

add characters to the tty buffer

Parameters

structtty_port*port

tty port

constu8*chars

characters

constu8*flags

flag bytes

size_tsize

size

Description

Queue a series of bytes to the tty buffering. For each character the flagsarray indicates the status of the character.

Return

the number added.

size_ttty_insert_flip_char(structtty_port*port,u8ch,u8flag)

add one character to the tty buffer

Parameters

structtty_port*port

tty port

u8ch

character

u8flag

flag byte

Description

Queue a single bytech to the tty buffering, with an optional flag.


Other Functions

unsignedinttty_buffer_space_avail(structtty_port*port)

return unused buffer space

Parameters

structtty_port*port

tty port owning the flip buffer

Return

the # of bytes which can be written by the driver without reachingthe buffer limit.

Note

this does not guarantee that memory is available to write the returned# of bytes (usetty_prepare_flip_string() to pre-allocate if memoryguarantee is required).

inttty_buffer_set_limit(structtty_port*port,intlimit)

change the tty buffer memory limit

Parameters

structtty_port*port

tty port to change

intlimit

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.

voidtty_buffer_lock_exclusive(structtty_port*port)

gain exclusive access to buffer

Parameters

structtty_port*port

tty port owning the flip buffer

Description

Guarantees safe use of thetty_ldisc_ops.receive_buf() method by excludingthe buffer work and any pending flush from using the flip buffer. Data cancontinue to be added concurrently to the flip buffer from the driver side.

See alsotty_buffer_unlock_exclusive().

voidtty_buffer_unlock_exclusive(structtty_port*port)

release exclusive access

Parameters

structtty_port*port

tty port owning the flip buffer

Description

The buffer work is restarted if there is data in the flip buffer.

See alsotty_buffer_lock_exclusive().


Internal Functions

voidtty_buffer_free_all(structtty_port*port)

free buffers used by a tty

Parameters

structtty_port*port

tty port to free from

Description

Remove all the buffers pending on a tty whether queued with data or in thefree ring. Must be called when the tty is no longer in use.

structtty_buffer*tty_buffer_alloc(structtty_port*port,size_tsize)

allocate a tty buffer

Parameters

structtty_port*port

tty port

size_tsize

desired size (characters)

Description

Allocate a new tty buffer to hold the desired number of characters. Weround our buffers off in 256 character chunks to get better allocationbehaviour.

Return

NULL if out of memory or the allocation would exceed the perdevice queue.

voidtty_buffer_free(structtty_port*port,structtty_buffer*b)

free a tty buffer

Parameters

structtty_port*port

tty port owning the buffer

structtty_buffer*b

the buffer to free

Description

Free a tty buffer, or add it to the free list according to our internalstrategy.

voidtty_buffer_flush(structtty_struct*tty,structtty_ldisc*ld)

flush full tty buffers

Parameters

structtty_struct*tty

tty to flush

structtty_ldisc*ld

optional ldisc ptr (must be referenced)

Description

Flush all the buffers containing receive data. Ifld !=NULL, flush theldisc input buffer.

Locking: takes buffer lock to ensure single-threaded flip buffer ‘consumer’.

int__tty_buffer_request_room(structtty_port*port,size_tsize,boolflags)

grow tty buffer if needed

Parameters

structtty_port*port

tty port

size_tsize

size desired

boolflags

buffer has to store flags along character data

Description

Make at leastsize bytes of linear space available for the tty buffer.

Will change over to a new buffer if the current buffer is encoded asTTY_NORMAL (so has no flags buffer) and the new buffer requires a flagsbuffer.

Return

the size we managed to find.

voidflush_to_ldisc(structwork_struct*work)

flush data from buffer to ldisc

Parameters

structwork_struct*work

tty structure passed from work queue.

Description

This routine is called out of the software interrupt to flush data from thebuffer chain to the line discipline.

Thereceive_buf() method is single threaded for each tty instance.

Locking: takes buffer lock to ensure single-threaded flip buffer ‘consumer’.

inttty_insert_flip_string_and_push_buffer(structtty_port*port,constu8*chars,size_tsize)

add characters to the tty buffer and push

Parameters

structtty_port*port

tty port

constu8*chars

characters

size_tsize

size

Description

The function combinestty_insert_flip_string() andtty_flip_buffer_push()with the exception of properly holding theport->lock.

To be used only internally (by pty currently).

Return

the number added.

voidtty_buffer_init(structtty_port*port)

prepare a tty buffer structure

Parameters

structtty_port*port

tty port to initialise

Description

Set up the initial state of the buffer management for a tty device. Must becalled before the other tty buffer functions are used.