在TTY驱动架构中有四个重要的结构体:termios、tty_driver、tty_operations和tty_struct。下面分别介绍:
(1)
termios结构体
struct termios
{
tcflag_t
c_iflag;
tcflag_t
c_oflag;
tcflag_t
c_cflag;
tcflag_t
c_lflag;
tcflag_t
c_cc[NCCS];
};
这个结构体是用来对串口进行设置的。在include/linux/tty.h中,有对结构体成员变量复制的宏,可根据实际需要进行设置。同时它也是tty_driver结构的成员。
(2)
tty_driver结构体
struct tty_driver {
struct cdev cdev;
const char
*driver_name;
const char
*name;
… …
int
major;
int
minor_start;
int
minor_num;
int
num;
struct ktermios init_termios;
struct tty_struct **ttys;
void *driver_state;
const struct tty_operations *ops;
};
tty_driver 结构用来向tty的核心来注册一个tty驱动程序。它是serial_core中uart_driver结构的一个成员。为了实现按芯片分类,在samsung
又用结构s3c24xx_uart_drv对其做了一次封装。
(3)
tty_operations结构体
struct tty_operations {
int
(*open)(struct tty_struct * tty, struct file * filp);
void (*close)(struct tty_struct * tty, struct file *
filp);
int
(*write)(struct tty_struct * tty,
const
unsigned char *buf, int count);
int
(*put_char)(struct tty_struct *tty, unsigned char ch);
void (*flush_chars)(struct tty_struct *tty);
int
(*write_room)(struct tty_struct *tty);
int
(*chars_in_buffer)(struct tty_struct *tty);
int
(*ioctl)(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg);
void (*throttle)(struct tty_struct * tty);
void (*unthrottle)(struct tty_struct * tty);
void (*stop)(struct tty_struct *tty);
void (*start)(struct tty_struct *tty);
void (*hangup)(struct tty_struct *tty);
int (*break_ctl)(struct tty_struct *tty, int state);
void (*set_ldisc)(struct tty_struct *tty);
void (*wait_until_sent)(struct tty_struct *tty, int
timeout);
void (*send_xchar)(struct tty_struct *tty, char ch);
int (*tiocmget)(struct tty_struct *tty, struct file
*file);
int (*tiocmset)(struct tty_struct *tty, struct file
*file,
unsigned int set, unsigned int clear);
int (*set_termiox)(struct tty_struct *tty, struct termiox
*tnew);
#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;
};
这个结构体中包含了所有的回调函数,他们被tty驱动程序设置,并被tty
核心调用。在serial_core中,有两个uart_ops,其中一个是tty_operations结构的封装,另一个是结构体的定义。在samsung中s3c24xx_serial_ops是对结构uart_ops的封装。变量uart_ops会被结构uart_ops的成员函数的实现所调用。
(4)
tty_struct结构体
struct tty_struct {
struct tty_driver *driver;
const struct tty_operations *ops;
struct tty_ldisc *ldisc;
wait_queue_head_t write_wait;
void *driver_data;
unsigned char closing:1;
……
};
tty端口设备描述函数,其中包含了前三个结构体。