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