tty驱动之tty_operations结构体详解

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_37600027/article/details/84037746
struct tty_operations {
        struct tty_struct * (*lookup)(struct tty_driver *driver,
                        struct inode *inode, int idx);
//返回对应的tty设备, 若为NULL则返回ERR_PTR, 在tty_mutex函数中调用

        int  (*install)(struct tty_driver *driver, struct tty_struct *tty);
//install一个tty设备到tty驱动的内部表,与lookup和remove法相关联

        void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
//从tty驱动的内部表,remove一个关闭了的tty设备,与lookup和remove法相关联

        int  (*open)(struct tty_struct * tty, struct file * filp);
//当一个特别的tty设备open的时候,将调用该例程;该例程是强制性的,若该例程没有加入operations,open tty的时候将返回ENODEV
//必须使用的方法
        void (*close)(struct tty_struct * tty, struct file * filp);
//当tty设备关闭时调用
//必须使用的方法
        void (*shutdown)(struct tty_struct *tty);
//在tty在关闭时,若仍持有一个带锁的特殊设备,需要调用该例程,想想我们经常使用的sudo shutdown -r/h/... now就明白了
//它执行在tty设备释放资源之前,所以可能执行在另一个tty设备持有kref的时候
        void (*cleanup)(struct tty_struct *tty);
//当一个tty设备在最后一次被关闭和释放资源时,异步地调用该例程,这个例程可以看成shutdown的可休眠的第二部分
        int  (*write)(struct tty_struct * tty,
                      const unsigned char *buf, int count);
//写函数,不多说
//可选使用:在需要写的设备中
        int  (*put_char)(struct tty_struct *tty, unsigned char ch);
//当内核想写单个字符到tty设备时,调用该例程(其实也可以调用write例程,调用时置count为1即可)
//可选使用:若设备在调用时未提供将使用write法
//注意:不要直接调用该例程,而应调用tty_put_char
        void (*flush_chars)(struct tty_struct *tty);
//这个例程调用在tty使用put_char输出很多个字符后
//可选
//注意:不要直接调用,调用tty_driver_flush_chars
        int  (*write_room)(struct tty_struct *tty);
//这个例程返回tty设备将被写入的字符队列长度
//这个数字受输出缓冲区和输出流的变化影响
//当有write函数时需要
//注意:不要直接调用,调用tty_write_room
        int  (*chars_in_buffer)(struct tty_struct *tty);
//在buffer区中的字符
        int  (*ioctl)(struct tty_struct *tty,
                    unsigned int cmd, unsigned long arg);
//该例程允许tty设备实施设备特殊的ioctl,若ioctl的数值在cmd中未被设备检测到,将返回ENOIOCTLCMD
//可选
        long (*compat_ioctl)(struct tty_struct *tty,
                             unsigned int cmd, unsigned long arg);
//在64位系统中执行32位的ioctl的例程
//可选
        void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
//该例程在设备的termios设置改变时通知驱动
//可选:在持termios lock的时候调用
        void (*throttle)(struct tty_struct * tty);
//当线程规划快满的时候提醒驱动程序,提示不要再输入字符到tty中
//可选:通常在tty_throttle()函数中获得termios lock时调用
void (*unthrottle)(struct tty_struct * tty);
//提醒驱动程序,可以输入字符到tty中,因为线程规划空间够用
//可选:通常在tty_unthrottle()函数中,获得termios lock时调用
        void (*stop)(struct tty_struct *tty);
//提醒驱动,停止输出字符
//可选,注意:不是调用stop_tty
        void (*start)(struct tty_struct *tty);
//提醒驱动,接着输入字符
//可选,助于:不是调用start_tty
        void (*hangup)(struct tty_struct *tty);
//挂起tty,可选
        int (*break_ctl)(struct tty_struct *tty, int state);
//打开或关闭RS-232口,state为-1则打开,state为0则关闭
//若该函数执行,则高一级的tty将处理四种ioctls:TCSBRK, TCSBRKP, TIOCSBRK, TIOCCBRK
//如果驱动程序设置了TTY_DRIVER_HARDWARE_BREAK,接着接口将被称作实际时间,而硬件延迟工作
//可选:需要收到TCSBRK/BRKP/etc
        void (*flush_buffer)(struct tty_struct *tty);
//在使用put_char()写入一串字符后,该函数被内核调用
//可选
//注意:调用是应为tty_driver_flush_buffer
        void (*set_ldisc)(struct tty_struct *tty);
//该例程在设备的termios设置改变时通知驱动
//可选:有BKL (currently)时调用
        void (*wait_until_sent)(struct tty_struct *tty, int timeout);
//该例程等待设备将其所有字符写入传输FIFO中
//可选:若设备有FIFO则需要
//注意:通常使用为tty_wait_until_sent
        void (*send_xchar)(struct tty_struct *tty, char ch);
//该例程发送一个大端的XON/XOFF给驱动程序
//可选:如果不提供,之后写函数将被原子锁调用来保持它的序列性
        int (*tiocmget)(struct tty_struct *tty);
//获得tty的线路设置
        int (*tiocmset)(struct tty_struct *tty,
                        unsigned int set, unsigned int clear);
//设置tty的线路设置
        int (*resize)(struct tty_struct *tty, struct winsize *ws);
//当一个termios的请求将改变 请求终端几何(requested terminal geometry)时调用
//可选:默认行为是 无错误地更新termios structure......
        int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
//当收到一个termiox基于ioctl的信号时调用,将用户空间的请求向下传递
//这个函数只有当tty包含tty->termiox指针的时候才会执行
//可选:持有termios lock的时候调用
        int (*get_icount)(struct tty_struct *tty,
                                struct serial_icounter_struct *icount);
//当设备收到一个TIOCGICOUNT的信号时调用,将一个内核结构传入使其完成。
//这个函数只有在提供的时候调用,否则将返回EINVAL
#ifdef CONFIG_CONSOLE_POLL
        int (*poll_init)(struct tty_driver *driver, int line, char *options);
        int (*poll_get_char)(struct tty_driver *driver, int line);
        void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
#endif
        const struct file_operations *proc_fops;
};

猜你喜欢

转载自blog.csdn.net/qq_37600027/article/details/84037746
tty