1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_TTY_LDISC_H
3#define _LINUX_TTY_LDISC_H
4
5/*
6 * This structure defines the interface between the tty line discipline
7 * implementation and the tty routines. The following routines can be
8 * defined; unless noted otherwise, they are optional, and can be
9 * filled in with a null pointer.
10 *
11 * int (*open)(struct tty_struct *);
12 *
13 * This function is called when the line discipline is associated
14 * with the tty. The line discipline can use this as an
15 * opportunity to initialize any state needed by the ldisc routines.
16 *
17 * void (*close)(struct tty_struct *);
18 *
19 * This function is called when the line discipline is being
20 * shutdown, either because the tty is being closed or because
21 * the tty is being changed to use a new line discipline
22 *
23 * void (*flush_buffer)(struct tty_struct *tty);
24 *
25 * This function instructs the line discipline to clear its
26 * buffers of any input characters it may have queued to be
27 * delivered to the user mode process.
28 *
29 * ssize_t (*read)(struct tty_struct * tty, struct file * file,
30 * unsigned char * buf, size_t nr);
31 *
32 * This function is called when the user requests to read from
33 * the tty. The line discipline will return whatever characters
34 * it has buffered up for the user. If this function is not
35 * defined, the user will receive an EIO error.
36 *
37 * ssize_t (*write)(struct tty_struct * tty, struct file * file,
38 * const unsigned char * buf, size_t nr);
39 *
40 * This function is called when the user requests to write to the
41 * tty. The line discipline will deliver the characters to the
42 * low-level tty device for transmission, optionally performing
43 * some processing on the characters first. If this function is
44 * not defined, the user will receive an EIO error.
45 *
46 * int (*ioctl)(struct tty_struct * tty, struct file * file,
47 * unsigned int cmd, unsigned long arg);
48 *
49 * This function is called when the user requests an ioctl which
50 * is not handled by the tty layer or the low-level tty driver.
51 * It is intended for ioctls which affect line discpline
52 * operation. Note that the search order for ioctls is (1) tty
53 * layer, (2) tty low-level driver, (3) line discpline. So a
54 * low-level driver can "grab" an ioctl request before the line
55 * discpline has a chance to see it.
56 *
57 * int (*compat_ioctl)(struct tty_struct * tty, struct file * file,
58 * unsigned int cmd, unsigned long arg);
59 *
60 * Process ioctl calls from 32-bit process on 64-bit system
61 *
62 * NOTE: only ioctls that are neither "pointer to compatible
63 * structure" nor tty-generic. Something private that takes
64 * an integer or a pointer to wordsize-sensitive structure
65 * belongs here, but most of ldiscs will happily leave
66 * it NULL.
67 *
68 * void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
69 *
70 * This function notifies the line discpline that a change has
71 * been made to the termios structure.
72 *
73 * int (*poll)(struct tty_struct * tty, struct file * file,
74 * poll_table *wait);
75 *
76 * This function is called when a user attempts to select/poll on a
77 * tty device. It is solely the responsibility of the line
78 * discipline to handle poll requests.
79 *
80 * void (*receive_buf)(struct tty_struct *, const unsigned char *cp,
81 * char *fp, int count);
82 *
83 * This function is called by the low-level tty driver to send
84 * characters received by the hardware to the line discpline for
85 * processing. <cp> is a pointer to the buffer of input
86 * character received by the device. <fp> is a pointer to a
87 * pointer of flag bytes which indicate whether a character was
88 * received with a parity error, etc. <fp> may be NULL to indicate
89 * all data received is TTY_NORMAL.
90 *
91 * void (*write_wakeup)(struct tty_struct *);
92 *
93 * This function is called by the low-level tty driver to signal
94 * that line discpline should try to send more characters to the
95 * low-level driver for transmission. If the line discpline does
96 * not have any more data to send, it can just return. If the line
97 * discipline does have some data to send, please arise a tasklet
98 * or workqueue to do the real data transfer. Do not send data in
99 * this hook, it may leads to a deadlock.
100 *
101 * int (*hangup)(struct tty_struct *)
102 *
103 * Called on a hangup. Tells the discipline that it should
104 * cease I/O to the tty driver. Can sleep. The driver should
105 * seek to perform this action quickly but should wait until
106 * any pending driver I/O is completed.
107 *
108 * void (*dcd_change)(struct tty_struct *tty, unsigned int status)
109 *
110 * Tells the discipline that the DCD pin has changed its status.
111 * Used exclusively by the N_PPS (Pulse-Per-Second) line discipline.
112 *
113 * int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
114 * char *fp, int count);
115 *
116 * This function is called by the low-level tty driver to send
117 * characters received by the hardware to the line discpline for
118 * processing. <cp> is a pointer to the buffer of input
119 * character received by the device. <fp> is a pointer to a
120 * pointer of flag bytes which indicate whether a character was
121 * received with a parity error, etc. <fp> may be NULL to indicate
122 * all data received is TTY_NORMAL.
123 * If assigned, prefer this function for automatic flow control.
124 */
125
126#include <linux/fs.h>
127#include <linux/wait.h>
128#include <linux/atomic.h>
129
130/*
131 * the semaphore definition
132 */
133struct ld_semaphore {
134 atomic_long_t count;
135 raw_spinlock_t wait_lock;
136 unsigned int wait_readers;
137 struct list_head read_wait;
138 struct list_head write_wait;
139#ifdef CONFIG_DEBUG_LOCK_ALLOC
140 struct lockdep_map dep_map;
141#endif
142};
143
144extern void __init_ldsem(struct ld_semaphore *sem, const char *name,
145 struct lock_class_key *key);
146
147#define init_ldsem(sem) \
148do { \
149 static struct lock_class_key __key; \
150 \
151 __init_ldsem((sem), #sem, &__key); \
152} while (0)
153
154
155extern int ldsem_down_read(struct ld_semaphore *sem, long timeout);
156extern int ldsem_down_read_trylock(struct ld_semaphore *sem);
157extern int ldsem_down_write(struct ld_semaphore *sem, long timeout);
158extern int ldsem_down_write_trylock(struct ld_semaphore *sem);
159extern void ldsem_up_read(struct ld_semaphore *sem);
160extern void ldsem_up_write(struct ld_semaphore *sem);
161
162#ifdef CONFIG_DEBUG_LOCK_ALLOC
163extern int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass,
164 long timeout);
165extern int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass,
166 long timeout);
167#else
168# define ldsem_down_read_nested(sem, subclass, timeout) \
169 ldsem_down_read(sem, timeout)
170# define ldsem_down_write_nested(sem, subclass, timeout) \
171 ldsem_down_write(sem, timeout)
172#endif
173
174
175struct tty_ldisc_ops {
176 int magic;
177 char *name;
178 int num;
179 int flags;
180
181 /*
182 * The following routines are called from above.
183 */
184 int (*open)(struct tty_struct *);
185 void (*close)(struct tty_struct *);
186 void (*flush_buffer)(struct tty_struct *tty);
187 ssize_t (*read)(struct tty_struct *tty, struct file *file,
188 unsigned char __user *buf, size_t nr);
189 ssize_t (*write)(struct tty_struct *tty, struct file *file,
190 const unsigned char *buf, size_t nr);
191 int (*ioctl)(struct tty_struct *tty, struct file *file,
192 unsigned int cmd, unsigned long arg);
193 int (*compat_ioctl)(struct tty_struct *tty, struct file *file,
194 unsigned int cmd, unsigned long arg);
195 void (*set_termios)(struct tty_struct *tty, struct ktermios *old);
196 __poll_t (*poll)(struct tty_struct *, struct file *,
197 struct poll_table_struct *);
198 int (*hangup)(struct tty_struct *tty);
199
200 /*
201 * The following routines are called from below.
202 */
203 void (*receive_buf)(struct tty_struct *, const unsigned char *cp,
204 char *fp, int count);
205 void (*write_wakeup)(struct tty_struct *);
206 void (*dcd_change)(struct tty_struct *, unsigned int);
207 int (*receive_buf2)(struct tty_struct *, const unsigned char *cp,
208 char *fp, int count);
209
210 struct module *owner;
211
212 int refcount;
213};
214
215struct tty_ldisc {
216 struct tty_ldisc_ops *ops;
217 struct tty_struct *tty;
218};
219
220#define TTY_LDISC_MAGIC 0x5403
221
222#define LDISC_FLAG_DEFINED 0x00000001
223
224#define MODULE_ALIAS_LDISC(ldisc) \
225 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc))
226
227#endif /* _LINUX_TTY_LDISC_H */
228