XenevaOS
Loading...
Searching...
No Matches
tty_ldisc.h
Go to the documentation of this file.
1#ifndef _LINUX_TTY_LDISC_H
2#define _LINUX_TTY_LDISC_H
3
4/*
5 * DCL <linux/tty_ldisc.h> -- the line discipline interface, plus the
6 * ld_semaphore it is serialised on.
7 *
8 * Ported from mainline v7.2. The line discipline is the layer that decides
9 * what a byte stream *means*: n_tty (cooked mode) turns 0x7f into a backspace
10 * and a line into an input record, while raw mode hands bytes through
11 * untouched. tty_ldisc.c registers the set and tty_ldisc_ref()/deref() guard
12 * a caller's use of one while it can be swapped underneath (TIOCSETD).
13 *
14 * The struct definitions are exact -- tty_ldisc_ops is the table n_tty.c
15 * fills in, and every member is a member a ported file either assigns or
16 * calls. The optional hooks are all present as function pointers; mainline
17 * explicitly documents them as allowed to be NULL, and tty_buffer.c:397/428
18 * tests receive_buf and lookahead_buf for exactly that reason.
19 *
20 * ld_semaphore lives here because struct tty_struct embeds it by value
21 * (tty.h), so the complete type has to exist before tty.h can. It is
22 * mainline's layout with raw_spinlock_t spelled spinlock_t: DCL's spinlock_t
23 * *is* the raw lock (typedef int spinlock_t, kernel.h:43), so the distinction
24 * mainline draws between raw and non-raw has no referent here.
25 *
26 * init_ldsem() keeps mainline's lock_class_key argument. It used to drop it
27 * -- the reasoning was that there is no lockdep to give a key to -- but the
28 * argument belongs to tty_ldsem.c's *definition*, which is vendored and
29 * cannot be edited to match, so the declaration follows it instead. The key
30 * it passes is a per-call-site static, exactly as mainline's macro mints one,
31 * and lockdep.h's `struct lock_class_key { }` makes it an object with an
32 * address rather than a notion. That is all the argument is ever asked to
33 * be: lockdep_set_class() evaluates it and drops it.
34 */
35
36#include <linux/fs.h>
37/*
38 * spinlock.h, not kernel.h: the wait_lock below is a spinlock_t and
39 * tty_ldisc.c locks and unlocks the ldisc table with the raw_ spelling from
40 * that header (:45, :112). This header is included by <linux/tty.h> at :49,
41 * and tty_ldisc.c includes tty.h at :7 -- so putting it here is what makes
42 * the lock API visible at :45 without editing the vendored source to add an
43 * include mainline happens to get from somewhere else.
44 */
45#include <linux/spinlock.h>
46#include <linux/wait.h>
47#include <linux/atomic.h>
48#include <linux/list.h>
49#include <linux/seq_file.h>
50#include <linux/termios.h>
51#include <linux/rwsem.h>
52
53struct tty_struct;
54struct module;
55
56/* --- the semaphore serialising line discipline changes ----------------- */
64
65/*
66 * lockdep.h for struct lock_class_key, which init_ldsem()'s expansion
67 * declares a static object of -- a pointer in the prototype would have
68 * needed only a forward tag, but `static struct lock_class_key __key;` needs
69 * the complete type. Three arguments because tty_ldsem.c:57 defines it with
70 * three, and the vendored definition is what the declaration answers to.
71 */
72#include <linux/lockdep.h>
73
74void __init_ldsem(struct ld_semaphore* sem, const char* name,
75 struct lock_class_key* key);
76
77#define init_ldsem(sem) \
78 do { \
79 static struct lock_class_key __key; \
80 \
81 __init_ldsem((sem), #sem, &__key); \
82 } while (0)
83
84int ldsem_down_read(struct ld_semaphore* sem, long timeout);
86void ldsem_up_read(struct ld_semaphore* sem);
87int ldsem_down_write(struct ld_semaphore* sem, long timeout);
89void ldsem_up_write(struct ld_semaphore* sem);
92
93/*
94 * The nested pair, exactly as mainline splits them at
95 * include/linux/tty_ldisc.h:45. It matters which arm exists: tty_ldsem.c
96 * puts both *definitions* inside `#ifdef CONFIG_DEBUG_LOCK_ALLOC` (:398-413),
97 * so with lockdep off there is no function to declare. Declaring one
98 * anyway compiled fine and failed at link, on a symbol only tty_ldisc.c:295
99 * names -- the plain declaration never notices that the definition it
100 * promises is conditional.
101 *
102 * The #else arm is mainline's, and it is a macro rather than an inline for
103 * the usual reason: it drops the subclass argument entirely. With lockdep
104 * off the subclass is only ever a note to lockdep about which of several
105 * identical semaphores this is (LDISC_SEM_OTHER at the call site), so the
106 * nested and non-nested take are the same take, and the macro says so by
107 * literally rewriting one into the other.
108 *
109 * CONFIG_DEBUG_LOCK_ALLOC is not in <linux/autoconf.h> at all, which is what
110 * sends this to the #else arm -- `#ifdef` reads absence as false, unlike
111 * IS_ENABLED() in kernel.h, which reads it as a compile error.
112 */
113#ifdef CONFIG_DEBUG_LOCK_ALLOC
114int ldsem_down_read_nested(struct ld_semaphore* sem, int subclass,
115 long timeout);
116int ldsem_down_write_nested(struct ld_semaphore* sem, int subclass,
117 long timeout);
118#else
119# define ldsem_down_read_nested(sem, subclass, timeout) \
120 ldsem_down_read(sem, timeout)
121# define ldsem_down_write_nested(sem, subclass, timeout) \
122 ldsem_down_write(sem, timeout)
123#endif
124
125/* --- the discipline interface ------------------------------------------ */
127 char* name;
128 int num;
129
130 /*
131 * The following routines are called from above.
132 */
133 int (*open)(struct tty_struct* tty);
134 void (*close)(struct tty_struct* tty);
135 void (*flush_buffer)(struct tty_struct* tty);
136 ssize_t (*read)(struct tty_struct* tty, struct file* file, u8* buf,
137 size_t nr, void** cookie, unsigned long offset);
138 ssize_t (*write)(struct tty_struct* tty, struct file* file,
139 const u8* buf, size_t nr);
140 int (*ioctl)(struct tty_struct* tty, unsigned int cmd,
141 unsigned long arg);
142 int (*compat_ioctl)(struct tty_struct* tty, unsigned int cmd,
143 unsigned long arg);
144 void (*set_termios)(struct tty_struct* tty, const struct ktermios* old);
145 __poll_t (*poll)(struct tty_struct* tty, struct file* file,
146 struct poll_table_struct* wait);
147 void (*hangup)(struct tty_struct* tty);
148
149 /*
150 * The following routines are called from below.
151 */
152 void (*receive_buf)(struct tty_struct* tty, const u8* cp,
153 const u8* fp, size_t count);
154 void (*write_wakeup)(struct tty_struct* tty);
155 void (*dcd_change)(struct tty_struct* tty, bool active);
156 size_t (*receive_buf2)(struct tty_struct* tty, const u8* cp,
157 const u8* fp, size_t count);
158 void (*lookahead_buf)(struct tty_struct* tty, const u8* cp,
159 const u8* fp, size_t count);
160
161 struct module* owner;
162};
163
164struct tty_ldisc {
165 const struct tty_ldisc_ops* ops;
167};
168
169#define MODULE_ALIAS_LDISC(ldisc) \
170 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
171
172extern const struct seq_operations tty_ldiscs_seq_ops;
173
177
179
180int tty_register_ldisc(const struct tty_ldisc_ops* new_ldisc);
181void tty_unregister_ldisc(const struct tty_ldisc_ops* ldisc);
182int tty_set_ldisc(struct tty_struct* tty, int disc);
183
184#endif /* _LINUX_TTY_LDISC_H */
int64_t ssize_t
Definition stddef.h:38
uint64_t size_t
Definition stdint.h:104
size_t size_t offset
Definition drm_bridge.h:1299
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
unsigned int __poll_t
Definition fs.h:50
uint8_t u8
Definition kernel.h:14
int spinlock_t
Definition kernel.h:44
Definition atomic.h:211
Definition fs.h:142
Definition termios.h:250
Definition tty_ldisc.h:57
spinlock_t wait_lock
Definition tty_ldisc.h:59
struct list_head write_wait
Definition tty_ldisc.h:62
struct list_head read_wait
Definition tty_ldisc.h:61
atomic_long_t count
Definition tty_ldisc.h:58
unsigned int wait_readers
Definition tty_ldisc.h:60
Definition list.h:11
Definition lockdep.h:160
Definition fs.h:52
Definition seq_file.h:69
Definition tty_ldisc.h:126
ssize_t(* write)(struct tty_struct *tty, struct file *file, const u8 *buf, size_t nr)
Definition tty_ldisc.h:138
void(* dcd_change)(struct tty_struct *tty, bool active)
Definition tty_ldisc.h:155
void(* receive_buf)(struct tty_struct *tty, const u8 *cp, const u8 *fp, size_t count)
Definition tty_ldisc.h:152
int(* compat_ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Definition tty_ldisc.h:142
int num
Definition tty_ldisc.h:128
void(* hangup)(struct tty_struct *tty)
Definition tty_ldisc.h:147
void(* write_wakeup)(struct tty_struct *tty)
Definition tty_ldisc.h:154
void(* set_termios)(struct tty_struct *tty, const struct ktermios *old)
Definition tty_ldisc.h:144
int(* ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Definition tty_ldisc.h:140
int(* open)(struct tty_struct *tty)
Definition tty_ldisc.h:133
void(* close)(struct tty_struct *tty)
Definition tty_ldisc.h:134
__poll_t(* poll)(struct tty_struct *tty, struct file *file, struct poll_table_struct *wait)
Definition tty_ldisc.h:145
size_t(* receive_buf2)(struct tty_struct *tty, const u8 *cp, const u8 *fp, size_t count)
Definition tty_ldisc.h:156
void(* flush_buffer)(struct tty_struct *tty)
Definition tty_ldisc.h:135
void(* lookahead_buf)(struct tty_struct *tty, const u8 *cp, const u8 *fp, size_t count)
Definition tty_ldisc.h:158
ssize_t(* read)(struct tty_struct *tty, struct file *file, u8 *buf, size_t nr, void **cookie, unsigned long offset)
Definition tty_ldisc.h:136
char * name
Definition tty_ldisc.h:127
struct module * owner
Definition tty_ldisc.h:161
Definition tty_ldisc.h:164
const struct tty_ldisc_ops * ops
Definition tty_ldisc.h:165
struct tty_struct * tty
Definition tty_ldisc.h:166
Definition tty.h:203
int ldsem_down_read_trylock(struct ld_semaphore *sem)
void tty_unregister_ldisc(const struct tty_ldisc_ops *ldisc)
void ldsem_up_read(struct ld_semaphore *sem)
int ldsem_down_read(struct ld_semaphore *sem, long timeout)
int ldsem_down_write_trylock(struct ld_semaphore *sem)
#define ldsem_down_write_nested(sem, subclass, timeout)
Definition tty_ldisc.h:121
int tty_register_ldisc(const struct tty_ldisc_ops *new_ldisc)
void tty_ldisc_deref(struct tty_ldisc *)
void tty_ldisc_flush(struct tty_struct *tty)
struct tty_ldisc * tty_ldisc_ref(struct tty_struct *)
void ldsem_up_write(struct ld_semaphore *sem)
int ldsem_down_write_failed(struct ld_semaphore *sem)
struct tty_ldisc * tty_ldisc_ref_wait(struct tty_struct *)
const struct seq_operations tty_ldiscs_seq_ops
int ldsem_down_write(struct ld_semaphore *sem, long timeout)
int ldsem_down_read_failed(struct ld_semaphore *sem)
int tty_set_ldisc(struct tty_struct *tty, int disc)
#define ldsem_down_read_nested(sem, subclass, timeout)
Definition tty_ldisc.h:119
void __init_ldsem(struct ld_semaphore *sem, const char *name, struct lock_class_key *key)