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