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 #include <linux/fs.h> 8 #include <linux/wait.h> 9 #include <linux/atomic.h> 10 #include <linux/list.h> 11 #include <linux/lockdep.h> 12 #include <linux/seq_file.h> 13 #include <linux/android_kabi.h> 14 15 /* 16 * the semaphore definition 17 */ 18 struct ld_semaphore { 19 atomic_long_t count; 20 raw_spinlock_t wait_lock; 21 unsigned int wait_readers; 22 struct list_head read_wait; 23 struct list_head write_wait; 24 #ifdef CONFIG_DEBUG_LOCK_ALLOC 25 struct lockdep_map dep_map; 26 #endif 27 }; 28 29 void __init_ldsem(struct ld_semaphore *sem, const char *name, 30 struct lock_class_key *key); 31 32 #define init_ldsem(sem) \ 33 do { \ 34 static struct lock_class_key __key; \ 35 \ 36 __init_ldsem((sem), #sem, &__key); \ 37 } while (0) 38 39 40 int ldsem_down_read(struct ld_semaphore *sem, long timeout); 41 int ldsem_down_read_trylock(struct ld_semaphore *sem); 42 int ldsem_down_write(struct ld_semaphore *sem, long timeout); 43 int ldsem_down_write_trylock(struct ld_semaphore *sem); 44 void ldsem_up_read(struct ld_semaphore *sem); 45 void ldsem_up_write(struct ld_semaphore *sem); 46 47 #ifdef CONFIG_DEBUG_LOCK_ALLOC 48 int ldsem_down_read_nested(struct ld_semaphore *sem, int subclass, 49 long timeout); 50 int ldsem_down_write_nested(struct ld_semaphore *sem, int subclass, 51 long timeout); 52 #else 53 # define ldsem_down_read_nested(sem, subclass, timeout) \ 54 ldsem_down_read(sem, timeout) 55 # define ldsem_down_write_nested(sem, subclass, timeout) \ 56 ldsem_down_write(sem, timeout) 57 #endif 58 59 /** 60 * struct tty_ldisc_ops - ldisc operations 61 * 62 * @name: name of this ldisc rendered in /proc/tty/ldiscs 63 * @num: ``N_*`` number (%N_TTY, %N_HDLC, ...) reserved to this ldisc 64 * 65 * @open: [TTY] ``int ()(struct tty_struct *tty)`` 66 * 67 * This function is called when the line discipline is associated with the 68 * @tty. No other call into the line discipline for this tty will occur 69 * until it completes successfully. It should initialize any state needed 70 * by the ldisc, and set @tty->receive_room to the maximum amount of data 71 * the line discipline is willing to accept from the driver with a single 72 * call to @receive_buf(). Returning an error will prevent the ldisc from 73 * being attached. 74 * 75 * Optional. Can sleep. 76 * 77 * @close: [TTY] ``void ()(struct tty_struct *tty)`` 78 * 79 * This function is called when the line discipline is being shutdown, 80 * either because the @tty is being closed or because the @tty is being 81 * changed to use a new line discipline. At the point of execution no 82 * further users will enter the ldisc code for this tty. 83 * 84 * Optional. Can sleep. 85 * 86 * @flush_buffer: [TTY] ``void ()(struct tty_struct *tty)`` 87 * 88 * This function instructs the line discipline to clear its buffers of any 89 * input characters it may have queued to be delivered to the user mode 90 * process. It may be called at any point between open and close. 91 * 92 * Optional. 93 * 94 * @read: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file, u8 *buf, 95 * size_t nr)`` 96 * 97 * This function is called when the user requests to read from the @tty. 98 * The line discipline will return whatever characters it has buffered up 99 * for the user. If this function is not defined, the user will receive 100 * an %EIO error. Multiple read calls may occur in parallel and the ldisc 101 * must deal with serialization issues. 102 * 103 * Optional: %EIO unless provided. Can sleep. 104 * 105 * @write: [TTY] ``ssize_t ()(struct tty_struct *tty, struct file *file, 106 * const u8 *buf, size_t nr)`` 107 * 108 * This function is called when the user requests to write to the @tty. 109 * The line discipline will deliver the characters to the low-level tty 110 * device for transmission, optionally performing some processing on the 111 * characters first. If this function is not defined, the user will 112 * receive an %EIO error. 113 * 114 * Optional: %EIO unless provided. Can sleep. 115 * 116 * @ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd, 117 * unsigned long arg)`` 118 * 119 * This function is called when the user requests an ioctl which is not 120 * handled by the tty layer or the low-level tty driver. It is intended 121 * for ioctls which affect line discpline operation. Note that the search 122 * order for ioctls is (1) tty layer, (2) tty low-level driver, (3) line 123 * discpline. So a low-level driver can "grab" an ioctl request before 124 * the line discpline has a chance to see it. 125 * 126 * Optional. 127 * 128 * @compat_ioctl: [TTY] ``int ()(struct tty_struct *tty, unsigned int cmd, 129 * unsigned long arg)`` 130 * 131 * Process ioctl calls from 32-bit process on 64-bit system. 132 * 133 * Note that only ioctls that are neither "pointer to compatible 134 * structure" nor tty-generic. Something private that takes an integer or 135 * a pointer to wordsize-sensitive structure belongs here, but most of 136 * ldiscs will happily leave it %NULL. 137 * 138 * Optional. 139 * 140 * @set_termios: [TTY] ``void ()(struct tty_struct *tty, const struct ktermios *old)`` 141 * 142 * This function notifies the line discpline that a change has been made 143 * to the termios structure. 144 * 145 * Optional. 146 * 147 * @poll: [TTY] ``int ()(struct tty_struct *tty, struct file *file, 148 * struct poll_table_struct *wait)`` 149 * 150 * This function is called when a user attempts to select/poll on a @tty 151 * device. It is solely the responsibility of the line discipline to 152 * handle poll requests. 153 * 154 * Optional. 155 * 156 * @hangup: [TTY] ``void ()(struct tty_struct *tty)`` 157 * 158 * Called on a hangup. Tells the discipline that it should cease I/O to 159 * the tty driver. The driver should seek to perform this action quickly 160 * but should wait until any pending driver I/O is completed. No further 161 * calls into the ldisc code will occur. 162 * 163 * Optional. Can sleep. 164 * 165 * @receive_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp, 166 * const u8 *fp, size_t count)`` 167 * 168 * This function is called by the low-level tty driver to send characters 169 * received by the hardware to the line discpline for processing. @cp is 170 * a pointer to the buffer of input character received by the device. @fp 171 * is a pointer to an array of flag bytes which indicate whether a 172 * character was received with a parity error, etc. @fp may be %NULL to 173 * indicate all data received is %TTY_NORMAL. 174 * 175 * Optional. 176 * 177 * @write_wakeup: [DRV] ``void ()(struct tty_struct *tty)`` 178 * 179 * This function is called by the low-level tty driver to signal that line 180 * discpline should try to send more characters to the low-level driver 181 * for transmission. If the line discpline does not have any more data to 182 * send, it can just return. If the line discipline does have some data to 183 * send, please arise a tasklet or workqueue to do the real data transfer. 184 * Do not send data in this hook, it may lead to a deadlock. 185 * 186 * Optional. 187 * 188 * @dcd_change: [DRV] ``void ()(struct tty_struct *tty, bool active)`` 189 * 190 * Tells the discipline that the DCD pin has changed its status. Used 191 * exclusively by the %N_PPS (Pulse-Per-Second) line discipline. 192 * 193 * Optional. 194 * 195 * @receive_buf2: [DRV] ``ssize_t ()(struct tty_struct *tty, const u8 *cp, 196 * const u8 *fp, size_t count)`` 197 * 198 * This function is called by the low-level tty driver to send characters 199 * received by the hardware to the line discpline for processing. @cp is a 200 * pointer to the buffer of input character received by the device. @fp 201 * is a pointer to an array of flag bytes which indicate whether a 202 * character was received with a parity error, etc. @fp may be %NULL to 203 * indicate all data received is %TTY_NORMAL. If assigned, prefer this 204 * function for automatic flow control. 205 * 206 * Optional. 207 * 208 * @lookahead_buf: [DRV] ``void ()(struct tty_struct *tty, const u8 *cp, 209 * const u8 *fp, size_t count)`` 210 * 211 * This function is called by the low-level tty driver for characters 212 * not eaten by ->receive_buf() or ->receive_buf2(). It is useful for 213 * processing high-priority characters such as software flow-control 214 * characters that could otherwise get stuck into the intermediate 215 * buffer until tty has room to receive them. Ldisc must be able to 216 * handle later a ->receive_buf() or ->receive_buf2() call for the 217 * same characters (e.g. by skipping the actions for high-priority 218 * characters already handled by ->lookahead_buf()). 219 * 220 * Optional. 221 * 222 * @owner: module containting this ldisc (for reference counting) 223 * 224 * This structure defines the interface between the tty line discipline 225 * implementation and the tty routines. The above routines can be defined. 226 * Unless noted otherwise, they are optional, and can be filled in with a %NULL 227 * pointer. 228 * 229 * Hooks marked [TTY] are invoked from the TTY core, the [DRV] ones from the 230 * tty_driver side. 231 */ 232 struct tty_ldisc_ops { 233 char *name; 234 int num; 235 236 /* 237 * The following routines are called from above. 238 */ 239 int (*open)(struct tty_struct *tty); 240 void (*close)(struct tty_struct *tty); 241 void (*flush_buffer)(struct tty_struct *tty); 242 ssize_t (*read)(struct tty_struct *tty, struct file *file, u8 *buf, 243 size_t nr, void **cookie, unsigned long offset); 244 ssize_t (*write)(struct tty_struct *tty, struct file *file, 245 const u8 *buf, size_t nr); 246 int (*ioctl)(struct tty_struct *tty, unsigned int cmd, 247 unsigned long arg); 248 int (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd, 249 unsigned long arg); 250 void (*set_termios)(struct tty_struct *tty, const struct ktermios *old); 251 __poll_t (*poll)(struct tty_struct *tty, struct file *file, 252 struct poll_table_struct *wait); 253 void (*hangup)(struct tty_struct *tty); 254 255 /* 256 * The following routines are called from below. 257 */ 258 void (*receive_buf)(struct tty_struct *tty, const u8 *cp, 259 const u8 *fp, size_t count); 260 void (*write_wakeup)(struct tty_struct *tty); 261 void (*dcd_change)(struct tty_struct *tty, bool active); 262 size_t (*receive_buf2)(struct tty_struct *tty, const u8 *cp, 263 const u8 *fp, size_t count); 264 void (*lookahead_buf)(struct tty_struct *tty, const u8 *cp, 265 const u8 *fp, size_t count); 266 267 struct module *owner; 268 269 ANDROID_KABI_RESERVE(1); 270 ANDROID_KABI_RESERVE(2); 271 }; 272 273 struct tty_ldisc { 274 struct tty_ldisc_ops *ops; 275 struct tty_struct *tty; 276 }; 277 278 #define MODULE_ALIAS_LDISC(ldisc) \ 279 MODULE_ALIAS("tty-ldisc-" __stringify(ldisc)) 280 281 extern const struct seq_operations tty_ldiscs_seq_ops; 282 283 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *); 284 void tty_ldisc_deref(struct tty_ldisc *); 285 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *); 286 287 void tty_ldisc_flush(struct tty_struct *tty); 288 289 int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc); 290 void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc); 291 int tty_set_ldisc(struct tty_struct *tty, int disc); 292 293 #endif /* _LINUX_TTY_LDISC_H */ 294