Linux下基於tty架構的串口驅動分析(S3C2440)

在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端口設備描述函數,其中包含了前三個結構體。

發佈了35 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章