drivers/tty/n_tty.c
Source file repositories/reference/linux-study-clean/drivers/tty/n_tty.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/tty/n_tty.c- Extension
.c- Size
- 64766 bytes
- Lines
- 2536
- Domain
- Driver Families
- Bucket
- drivers/tty
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitmap.hlinux/bitops.hlinux/ctype.hlinux/errno.hlinux/export.hlinux/fcntl.hlinux/file.hlinux/jiffies.hlinux/math.hlinux/poll.hlinux/ratelimit.hlinux/sched.hlinux/signal.hlinux/slab.hlinux/string.hlinux/tty.hlinux/types.hlinux/uaccess.hlinux/vmalloc.htty.h
Detected Declarations
struct n_tty_datafunction read_cntfunction read_buffunction echo_buffunction zero_bufferfunction tty_copyfunction n_tty_readfunction chars_in_bufferfunction n_tty_write_wakeupfunction n_tty_check_throttlefunction n_tty_check_unthrottlefunction n_tty_receive_buffunction n_tty_openfunction n_tty_packet_mode_flushfunction queuefunction is_utf8_continuationfunction is_continuationfunction characterfunction n_tty_writefunction n_tty_writefunction n_tty_process_echo_opsfunction echofunction commit_echoesfunction scoped_guardfunction process_echoesfunction scoped_guardfunction flush_echoesfunction add_echo_bytefunction echo_move_back_colfunction echo_set_canon_colfunction echo_erase_tabfunction L_ECHOfunction L_ECHOfunction finish_erasingfunction eraserfunction __isigfunction isigfunction scoped_guardfunction n_tty_receive_breakfunction n_tty_receive_overrunfunction n_tty_receive_parity_errorfunction n_tty_receive_signal_charfunction n_tty_is_char_flow_ctrlfunction n_tty_receive_char_flow_ctrlfunction n_tty_receive_handle_newlinefunction n_tty_receive_char_canonfunction n_tty_receive_char_specialfunction n_tty_receive_char
Annotated Snippet
struct n_tty_data {
/* producer-published */
size_t read_head;
size_t commit_head;
size_t canon_head;
size_t echo_head;
size_t echo_commit;
size_t echo_mark;
DECLARE_BITMAP(char_map, 256);
/* private to n_tty_receive_overrun (single-threaded) */
unsigned long overrun_time;
unsigned int num_overrun;
/* non-atomic */
bool no_room;
/* must hold exclusive termios_rwsem to reset these */
unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
unsigned char push:1;
/* shared by producer and consumer */
u8 read_buf[N_TTY_BUF_SIZE];
DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
u8 echo_buf[N_TTY_BUF_SIZE];
/* consumer-published */
size_t read_tail;
size_t line_start;
/* # of chars looked ahead (to find software flow control chars) */
size_t lookahead_count;
/* protected by output lock */
unsigned int column;
unsigned int canon_column;
size_t echo_tail;
struct mutex atomic_read_lock;
struct mutex output_lock;
};
#define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
static inline size_t read_cnt(struct n_tty_data *ldata)
{
return ldata->read_head - ldata->read_tail;
}
static inline u8 read_buf(struct n_tty_data *ldata, size_t i)
{
return ldata->read_buf[MASK(i)];
}
static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i)
{
return &ldata->read_buf[MASK(i)];
}
static inline u8 echo_buf(struct n_tty_data *ldata, size_t i)
{
smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
return ldata->echo_buf[MASK(i)];
}
static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i)
{
return &ldata->echo_buf[MASK(i)];
}
/* If we are not echoing the data, perhaps this is a secret so erase it */
static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size)
{
if (L_ICANON(tty) && !L_ECHO(tty))
memset(buffer, 0, size);
}
static void tty_copy(const struct tty_struct *tty, void *to, size_t tail,
size_t n)
{
struct n_tty_data *ldata = tty->disc_data;
size_t size = N_TTY_BUF_SIZE - tail;
void *from = read_buf_addr(ldata, tail);
if (n > size) {
tty_audit_add_data(tty, from, size);
memcpy(to, from, size);
zero_buffer(tty, from, size);
to += size;
n -= size;
Annotation
- Immediate include surface: `linux/bitmap.h`, `linux/bitops.h`, `linux/ctype.h`, `linux/errno.h`, `linux/export.h`, `linux/fcntl.h`, `linux/file.h`, `linux/jiffies.h`.
- Detected declarations: `struct n_tty_data`, `function read_cnt`, `function read_buf`, `function echo_buf`, `function zero_buffer`, `function tty_copy`, `function n_tty_read`, `function chars_in_buffer`, `function n_tty_write_wakeup`, `function n_tty_check_throttle`.
- Atlas domain: Driver Families / drivers/tty.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.