技術(shù)文章:基于Linux的tty架構(gòu)及UART驅(qū)動詳解
2.2.3. struct uart_state
每一個uart端口對應著一個uart_state,該結(jié)構(gòu)體將uart_port與對應的circ_buf聯(lián)系起來。uart_state有兩個成員在底層串口驅(qū)動會用到:xmit和port。
用戶空間程序通過串口發(fā)送數(shù)據(jù)時,上層驅(qū)動將用戶數(shù)據(jù)保存在xmit;而串口發(fā)送中斷處理函數(shù)就是通過xmit獲取到用戶數(shù)據(jù)并將它們發(fā)送出去。串口接收中斷處理函數(shù)需要通過port將接收到的數(shù)據(jù)傳遞給線路規(guī)程層。
struct uart_state {
struct tty_port port;
enum uart_pm_state pm_state;
struct circ_buf xmit;
struct uart_port *uart_port; 對應于一個串口設(shè)備
};
2.2.4. struct uart_port
uart_port用于描述串口端口的I/O端口或I/O內(nèi)存地址、FIFO大小、端口類型、串口時鐘等信息。實際上,一個uart_port實現(xiàn)對應一個串口設(shè)備。
struct uart_port {
spinlock_t lock; port lock
unsigned long iobase; in/out[bwl]
unsigned char __iomem *membase; read/write[bwl]
unsigned int (*serial_in)(struct uart_port *, int);
void (*serial_out)(struct uart_port *, int, int);
void (*set_termios)(struct uart_port *,
struct ktermios *new,
struct ktermios *old);
int (*handle_irq)(struct uart_port *);
void (*pm)(struct uart_port *, unsigned int state,
unsigned int old);
void (*handle_break)(struct uart_port *);
unsigned int irq; irq number
unsigned long irqflags; irq flags
unsigned int uartclk; base uart clock
unsigned int fifosize; tx fifo size
unsigned char x_char; xon/xoff char
unsigned char regshift; reg offset shift
unsigned char iotype; io access style
unsigned char unused1;
#define UPIO_PORT (0)
#define UPIO_HUB6 (1)
#define UPIO_MEM (2)
#define UPIO_MEM32 (3)
#define UPIO_AU (4) Au1x00 and RT288x type IO
#define UPIO_TSI (5) Tsi108/109 type IO
unsigned int read_status_mask; driver specific
unsigned int ignore_status_mask; driver specific
struct uart_state *state; pointer to parent state
struct uart_icount icount; statistics
struct console *cons; struct console, if any
#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)
unsigned long sysrq; sysrq timeout
#endif
upf_t flags;
#define UPF_FOURPORT ((__force upf_t) (1 << 1))
#define UPF_SAK ((__force upf_t) (1 << 2))
#define UPF_SPD_MASK ((__force upf_t) (0x1030))
#define UPF_SPD_HI ((__force upf_t) (0x0010))
#define UPF_SPD_VHI ((__force upf_t) (0x0020))
#define UPF_SPD_CUST ((__force upf_t) (0x0030))
#define UPF_SPD_SHI ((__force upf_t) (0x1000))
#define UPF_SPD_WARP ((__force upf_t) (0x1010))
#define UPF_SKIP_TEST ((__force upf_t) (1 << 6))
#define UPF_AUTO_IRQ ((__force upf_t) (1 << 7))
#define UPF_HARDPPS_CD ((__force upf_t) (1 << 11))
#define UPF_LOW_LATENCY ((__force upf_t) (1 << 13))
#define UPF_BUGGY_UART ((__force upf_t) (1 << 14))
#define UPF_NO_TXEN_TEST ((__force upf_t) (1 << 15))
#define UPF_MAGIC_MULTIPLIER ((__force upf_t) (1 << 16))
Port has hardware-assisted h/w flow control (iow, auto-RTS *not* auto-CTS)
#define UPF_HARD_FLOW ((__force upf_t) (1 << 21))
Port has hardware-assisted s/w flow control
#define UPF_SOFT_FLOW ((__force upf_t) (1 << 22))
#define UPF_CONS_FLOW ((__force upf_t) (1 << 23))
#define UPF_SHARE_IRQ ((__force upf_t) (1 << 24))
#define UPF_EXAR_EFR ((__force upf_t) (1 << 25))
#define UPF_BUG_THRE ((__force upf_t) (1 << 26))
The exact UART type is known and should not be probed.
#define UPF_FIXED_TYPE ((__force upf_t) (1 << 27))
#define UPF_BOOT_AUTOCONF ((__force upf_t) (1 << 28))
#define UPF_FIXED_PORT ((__force upf_t) (1 << 29))
#define UPF_DEAD ((__force upf_t) (1 << 30))
#define UPF_IOREMAP ((__force upf_t) (1 << 31))
#define UPF_CHANGE_MASK ((__force upf_t) (0x17fff))
#define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
unsigned int mctrl; current modem ctrl settings
unsigned int timeout; character-based timeout
unsigned int type; port type
const struct uart_ops *ops;
unsigned int custom_divisor;
unsigned int line; port index
resource_size_t mapbase; for ioremap
struct device *dev; parent device
unsigned char hub6; this should be in the 8250 driver
unsigned char suspended;
unsigned char irq_wake;
unsigned char unused[2];
void *private_data; generic platform data pointer
};
2.2.5. struct uart_ops
struct uart_ops涵蓋了驅(qū)動可對串口的所有操作
struct uart_ops {
unsigned int (*tx_empty)(struct uart_port *);
void (*set_mctrl)(struct uart_port *, unsigned int mctrl);
unsigned int (*get_mctrl)(struct uart_port *);
void (*stop_tx)(struct uart_port *);
void (*start_tx)(struct uart_port *);
void (*throttle)(struct uart_port *);
void (*unthrottle)(struct uart_port *);
void (*send_xchar)(struct uart_port *, char ch);
void (*stop_rx)(struct uart_port *);
void (*enable_ms)(struct uart_port *);
void (*break_ctl)(struct uart_port *, int ctl);
int (*startup)(struct uart_port *);
void (*shutdown)(struct uart_port *);
void (*flush_buffer)(struct uart_port *);
void (*set_termios)(struct uart_port *, struct ktermios *new,
struct ktermios *old);
void (*set_ldisc)(struct uart_port *, int new);
void (*pm)(struct uart_port *, unsigned int state,
unsigned int oldstate);
int (*set_wake)(struct uart_port *, unsigned int state);
* Return a string describing the type of the port
const char *(*type)(struct uart_port *);
* Release IO and memory resources used by the port.
* This includes iounmap if necessary.
void (*release_port)(struct uart_port *);
* Request IO and memory resources used by the port.
* This includes iomapping the port if necessary.
int (*request_port)(struct uart_port *);
void (*config_port)(struct uart_port *, int);
int (*verify_port)(struct uart_port *, struct serial_struct *);
int (*ioctl)(struct uart_port *, unsigned int, unsigned long);
#ifdef CONFIG_CONSOLE_POLL
int (*poll_init)(struct uart_port *);
void (*poll_put_char)(struct uart_port *, unsigned char);
int (*poll_get_char)(struct uart_port *);
#endif
};
2.3. 關(guān)鍵流程2.3.1. 注冊流程2.3.1.1. 注冊uart_driver
此接口在uart driver中調(diào)用,用來注冊uart_driver到kernel中,調(diào)用階段在uart driver的初始階段,例如:module_init(), uart_driver的注冊流程圖
圖3.3uart driver注冊流程
注冊過程主要做了以下操作:
1、根據(jù)driver支持的最大設(shè)備數(shù),申請n個uart_state空間,每一個uart_state都有一個uart_port。2、分配一個tty_driver,并將uart_driver->tty_driver指向它。3、對tty_driver進行設(shè)置,其中包括默認波特率、檢驗方式等,還有一個重要的ops,結(jié)構(gòu)體tty_operation的注冊,它是tty核心與串口驅(qū)動通信的接口。4、初始化每一個uart_state的tty_port;5、注冊tty_driver。注冊uart_driver實際上是注冊tty_driver,與用戶空間打交道的工作完全交給tty_driver,這一部分是內(nèi)核實現(xiàn)好的不需要修改2.3.1.2. 添加uart_port
此接口用于注冊一個uart port 到uart driver上,通過注冊,uart driver就可以訪問對應的uart port,進行數(shù)據(jù)收發(fā)。該接口在uart driver中的probe函數(shù)調(diào)用,必須保證晚于uart_register_drver的注冊過程。
請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
-
10月31日立即下載>> 【限時免費下載】TE暖通空調(diào)系統(tǒng)高效可靠的組件解決方案
-
即日-11.13立即報名>>> 【在線會議】多物理場仿真助跑新能源汽車
-
11月28日立即報名>>> 2024工程師系列—工業(yè)電子技術(shù)在線會議
-
12月19日立即報名>> 【線下會議】OFweek 2024(第九屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會
-
即日-12.26火熱報名中>> OFweek2024中國智造CIO在線峰會
-
即日-2025.8.1立即下載>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍皮書》
推薦專題
- 高級軟件工程師 廣東省/深圳市
- 自動化高級工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級銷售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專家 廣東省/江門市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市