美国亚利桑那州旅游:Linux终端设备驱动(理顺概念)(5)

来源:百度文库 编辑:偶看新闻 时间:2024/04/27 18:17:03
 当 tty驱动挂起 tty设备时,hangup()函数被调用,在此函数中进行相关的硬件操作。当tty 驱动要在 RS-232 端口上打开或关闭线路的 BREAK 状态时,break_ctl()线路中断控制函数被调用。如果state状态设为-1,BREAK 状态打开,如果状态设为 0,BREAK 状态关闭。如果这个函数由 tty 驱动实现,而tty核心将处理TCSBRK、TCSBRKP、TIOCSBRK和 TIOCCBRK这些ioctl命令。flush_buffer()函数用于刷新缓冲区并丢弃任何剩下的数据。set_ldisc()函数用于设置线路规程,当 tty 核心改变tty驱动的线路规程时这个函数被调用,这个函数通常不需要被驱动定义。send_xchar()为X-类型字符发送函数,这个函数用来发送一个高优先级 XON 或者 XOFF 字符给 tty设备,要被发送的字符在第2个参数ch中指定。read_proc()和write_proc()为/proc 读和写函数。tiocmget()函数用于获得tty 设备的线路设置,对应的tiocmset()用于设置tty设备的线路设置,参数set和clear包含了要设置或者清除的线路设置。
    Linux内核提供了一组函数用于操作tty_driver结构体及tty设备,包括:
?  分配tty驱动
struct tty_driver *alloc_tty_driver(int lines);
这个函数返回tty_driver指针,其参数为要分配的设备数量,line会被赋值给tty_driver的num成员,例如:
xxx_tty_driver = alloc_tty_driver(XXX_TTY_MINORS);
if (!xxx_tty_driver) //分配失败
 return -ENOMEM;
?  注册tty驱动
int tty_register_driver(struct tty_driver *driver);
注册tty驱动成功时返回0,参数为由alloc_tty_driver ()分配的tty_driver结构体指针,例如:
retval = tty_register_driver(xxx_tty_driver);
if (retval) //注册失败
{
  printk(KERN_ERR "failed to register tiny tty driver");
  put_tty_driver(xxx_tty_driver);
  return retval;
}
?  注销tty驱动
int tty_unregister_driver(struct tty_driver *driver);
这个函数与tty_register_driver ()对应,tty驱动最终会调用上述函数注销tty_driver。
?  注册tty设备
void tty_register_device(struct tty_driver *driver, unsigned index,
    struct device *device);
仅有tty_driver是不够的,驱动必须依附于设备,tty_register_device()函数用于注册关联于tty_driver的设备,index为设备的索引(范围是0~driver->num),如:
for (i = 0; i < XXX_TTY_MINORS; ++i)
tty_register_device(xxx_tty_driver, i, NULL);
?  注销tty设备
void tty_unregister_device(struct tty_driver *driver, unsigned index);
上述函数与tty_register_device()对应,用于注销tty设备,其使用方法如:
for (i = 0; i < XXX_TTY_MINORS; ++i)
tty_unregister_device(xxx_tty_driver, i);
?  设置tty驱动操作
void tty_set_operations(struct tty_driver *driver, struct tty_operations *op);
上述函数会将tty_operations结构体中的函数指针拷贝给tty_driver对应的函数指针,在具体的tty驱动中,通常会定义1个设备特定的 tty_operations,tty_operations的定义如代码清单14.3。tty_operations中的成员函数与 tty_driver中的同名成员函数意义完全一致,因此,这里不再赘述。
代码清单14.3 tty_operations结构体
1  struct tty_operations
2  {
3     int  (*open)(struct tty_struct * tty, struct file * filp);
4     void (*close)(struct tty_struct * tty, struct file * filp);
5     int  (*write)(struct tty_struct * tty,
6                   const unsigned char *buf, int count);
7     void (*put_char)(struct tty_struct *tty, unsigned char ch);
8     void (*flush_chars)(struct tty_struct *tty);
9     int  (*write_room)(struct tty_struct *tty);
10    int  (*chars_in_buffer)(struct tty_struct *tty);
11    int  (*ioctl)(struct tty_struct *tty, struct file * file,
12                unsigned int cmd, unsigned long arg);
13    void (*set_termios)(struct tty_struct *tty, struct termios * old);
14    void (*throttle)(struct tty_struct * tty);
15    void (*unthrottle)(struct tty_struct * tty);
16    void (*stop)(struct tty_struct *tty);
17    void (*start)(struct tty_struct *tty);
18    void (*hangup)(struct tty_struct *tty);
19    void (*break_ctl)(struct tty_struct *tty, int state);
20    void (*flush_buffer)(struct tty_struct *tty);
21    void (*set_ldisc)(struct tty_struct *tty);
22    void (*wait_until_sent)(struct tty_struct *tty, int timeout);
23    void (*send_xchar)(struct tty_struct *tty, char ch);
24    int (*read_proc)(char *page, char **start, off_t off,
25                      int count, int *eof, void *data);