转自:http://blog.csdn.net/sharecode/article/details/9196591

版权声明:本文为博主原创文章,未经博主允许不得转载。

Linux 中将串口驱动进行了分层,如图:

本节讲解与底层硬件密切相关的层,以S3C2440为例剖析:

实现文件有:/drivers/serial/samsung.c    /drivers/serial/samsung.h      /drivers/serial/s3c2440.c (kernel 2.6.28),Serial Core层在/drivers/serial/serial_core.c主要文件中。

硬件驱动层与Serial-Core沟通数据结构如下:

1. uart_driver包含了串口设备名、串口驱动名、主次设备号、串口控制台(可选)等信息,还封装了tty_driver(底层串口驱动无需关心tty_driver)。

struct uart_driver {
struct module *owner;/* 拥有该uart_driver的模块,一般为THIS_MODULE */
constchar*driver_name;/* 串口驱动名,串口设备文件名以驱动名为基础 */
constchar*dev_name;/* 串口设备名 */
int major;/* 主设备号 */
int minor;/* 次设备号 */
int nr;/* 该uart_driver支持的串口个数(最大) */
struct console *cons;/* 其对应的console.若该uart_driver支持serial console,否则为NULL */

/*
* these are private; the low level driver should not
* touch these; they should be initialised to NULL
*/
struct uart_state *state;
struct tty_driver *tty_driver;     
};

2. uart_port用于描述串口端口的I/O端口或I/O内存地址、FIFO大小、端口类型、串口时钟等信息。实际上,一个uart_port实例对应一个串口设备。

struct uart_port {
spinlock_t lock;/* 串口端口锁 */
unsignedint iobase;/* IO端口基地址 */
unsignedchar __iomem *membase;/* IO内存基地址,经映射(如ioremap)后的IO内存虚拟基地址 */
unsignedint irq;/* 中断号 */
unsignedint uartclk;/* 串口时钟 */
unsignedint fifosize;/* 串口FIFO缓冲大小 */
unsignedchar x_char;/* xon/xoff字符 */
unsignedchar regshift;/* 寄存器位移 */
unsignedchar iotype;/* IO访问方式 */
unsignedchar unused1;

#define UPIO_PORT (0)/* IO端口 */
#define UPIO_HUB6 (1)
#define UPIO_MEM (2)/* IO内存 */
#define UPIO_MEM32 (3)
#define UPIO_AU (4)/* Au1x00 type IO */
#define UPIO_TSI (5)/* Tsi108/109 type IO */
#define UPIO_DWAPB (6)/* DesignWare APB UART */
#define UPIO_RM9000 (7)/* RM9000 type IO */

unsignedint read_status_mask;/* 关心的Rx error status */
unsignedint ignore_status_mask;/* 忽略的Rx error status */
struct uart_info *info;        //重要,见下面
struct uart_icount  icount;   /* 计数器 uart_icount为串口信息计数器,包含了发送字符计数、接收字符计数等。在串口的发送中断处理函数和接收中断处理函数中,我们需要管理这些计数。*/ 

struct console *cons;/* console结构体 */
#ifdefCONFIG_SERIAL_CORE_CONSOLE
unsignedlong sysrq;/* sysrq timeout */
#endif

upf_t flags;

#define UPF_FOURPORT ((__forceupf_t)(1 << 1))
#define UPF_SAK ((__forceupf_t)(1 << 2))
#define UPF_SPD_MASK ((__forceupf_t)(0x1030))
#define UPF_SPD_HI ((__forceupf_t)(0x0010))
#define UPF_SPD_VHI ((__forceupf_t)(0x0020))
#define UPF_SPD_CUST ((__forceupf_t)(0x0030))
#define UPF_SPD_SHI ((__forceupf_t)(0x1000))
#define UPF_SPD_WARP ((__forceupf_t)(0x1010))
#define UPF_SKIP_TEST ((__forceupf_t)(1 << 6))
#define UPF_AUTO_IRQ ((__forceupf_t)(1 << 7))
#define UPF_HARDPPS_CD ((__forceupf_t)(1 << 11))
#define UPF_LOW_LATENCY ((__forceupf_t)(1 << 13))
#define UPF_BUGGY_UART ((__forceupf_t)(1 << 14))
#define UPF_MAGIC_MULTIPLIER((__force upf_t)(1 << 16))
#define UPF_CONS_FLOW ((__forceupf_t)(1 << 23))
#define UPF_SHARE_IRQ ((__forceupf_t)(1 << 24))
#define UPF_BOOT_AUTOCONF ((__forceupf_t)(1 << 28))
#define UPF_FIXED_PORT ((__forceupf_t)(1 << 29))
#define UPF_DEAD ((__forceupf_t)(1 << 30))
#define UPF_IOREMAP ((__forceupf_t)(1 << 31))

#define UPF_CHANGE_MASK ((__forceupf_t)(0x17fff))
#define UPF_USR_MASK ((__forceupf_t)(UPF_SPD_MASK|UPF_LOW_LATENCY))

unsigned int mctrl;/* 当前的moden设置 */
unsigned int timeout;/* character-based timeout */
unsigned int type;/* 端口类型 */
const struct uart_ops *ops;/* 串口端口操作函数集 */
unsigned int custom_divisor;
unsigned int  line;/* 端口索引 */
resource_size_t mapbase;/* IO内存物理基地址,可用于ioremap */
struct device *dev;/* 父设备 */
unsigned char hub6;/* this should be in the 8250 driver */
unsigned char suspended;
unsigned char unused[2];
void*private_data;/* 端口私有数据,一般为platform数据指针 */
};

(1) Uart_struct uart_icount {
__u32 cts;
__u32 dsr;
__u32 rng;
__u32 dcd;
__u32 rx;/* 发送字符计数 */
__u32 tx;/* 接受字符计数 */
__u32 frame;/* 帧错误计数 */
__u32 overrun;/* Rx FIFO溢出计数 */
__u32 parity; /* 帧校验错误计数 */
__u32 brk; /* break计数 */
__u32 buf_overrun;
};

 uart_info有两个成员在底层串口驱动会用到:xmit和tty。用户空间程序通过串口发送数据时,上层驱动将用户数据保存在xmit;而串口发送中断处理函数就是通过xmit获取到用户数据并将它们发送出去。串口接收中断处理函数需要通过tty将接收到的数据传递给行规则层

struct uart_info {
struct tty_struct *tty;   //接受
struct circ_buf xmit;    //发送
uif_t flags;

/*
* Definitions for info->flags. These are _private_ to serial_core,and
* are specific to this structure. They may be queried by low leveldrivers.
*/
#define UIF_CHECK_CD ((__force uif_t)(1 << 25))
#define UIF_CTS_FLOW ((__force uif_t)(1 << 26))
#define UIF_NORMAL_ACTIVE ((__force uif_t)(1 << 29))
#define UIF_INITIALIZED ((__force uif_t)(1 << 31))
#define UIF_SUSPENDED ((__force uif_t)(1 << 30))

int blocked_open;

struct tasklet_struct tlet;    //上层驱动任务等待队列的

wait_queue_head_t open_wait;
wait_queue_head_t delta_msr_wait;
};

3. Uart_port中有一个重要的uart_ops,底层硬件需要实现:

struct uart_ops {
unsignedint(*tx_empty)(struct uart_port *); /* 串口的Tx FIFO缓存是否为空 */
void(*set_mctrl)(struct uart_port *,unsignedint mctrl);/* 设置串口modem控制 */
unsignedint(*get_mctrl)(struct uart_port *);/* 获取串口modem控制 */
void(*stop_tx)(struct uart_port *);/* 禁止串口发送数据 */
void(*start_tx)(struct uart_port *);/* 使能串口发送数据 */
void(*send_xchar)(struct uart_port *,char ch);/* 发送xChar */
void(*stop_rx)(struct uart_port *);/* 禁止串口接收数据 */
void(*enable_ms)(struct uart_port *);/* 使能modem的状态信号 */
void(*break_ctl)(struct uart_port *,int ctl);/* 设置break信号 */
int(*startup)(struct uart_port *);/* 启动串口,应用程序打开串口设备文件时,该函数会被调用 */
void(*shutdown)(struct uart_port *);/* 关闭串口,应用程序关闭串口设备文件时,该函数会被调用 */
void(*set_termios)(struct uart_port *,struct ktermios *new,struct ktermios *old);/* 设置串口参数 */
void(*pm)(struct uart_port *,unsignedint state,
unsignedint oldstate);/* 串口电源管理 */
int(*set_wake)(struct uart_port *,unsignedint state);/* */
constchar*(*type)(struct uart_port *);/* 返回一描述串口类型的字符串 */
void(*release_port)(struct uart_port *);/* 释放串口已申请的IO端口/IO内存资源,必要时还需iounmap */
int(*request_port)(struct uart_port *);/* 申请必要的IO端口/IO内存资源,必要时还可以重新映射串口端口 */
void(*config_port)(struct uart_port *,int);/* 执行串口所需的自动配置 */
int(*verify_port)(struct uart_port *,struct serial_struct *);/* 核实新串口的信息 */
int(*ioctl)(struct uart_port *,unsignedint,unsignedlong);/* IO控制 */
};

4. uart_driver通过Serial Core层的

int uart_register_driver(struct uart_driver *drv)

向Core注册,通过int uart_add_one_port(struct uart_driver *drv,struct uart_port *port)向该驱动添加uart_port。

Serial Core层还导出如下函数共上层或下层调用:

EXPORT_SYMBOL(uart_match_port);   //判断两个uart_port是否相等

EXPORT_SYMBOL(uart_write_wakeup);    //该函数常在中断处理函数中调用用来唤醒上层因向串口端口写数据而阻塞的进程

EXPORT_SYMBOL(uart_register_driver);  //已解释
EXPORT_SYMBOL(uart_unregister_driver);
EXPORT_SYMBOL(uart_suspend_port);   //用于挂起特定的串口端口
EXPORT_SYMBOL(uart_resume_port);
EXPORT_SYMBOL(uart_add_one_port);   //已解释
EXPORT_SYMBOL(uart_remove_one_port);

具体实现请参照/drivers/serial/samsung.c  /drivers/serial/s3c2440.c

下一节重点介绍Serial Core层

Linux TTY驱动--Uart_driver底层【转】的更多相关文章

  1. Linux TTY驱动--Serial Core层【转】

    转自:http://blog.csdn.net/sharecode/article/details/9197567 版权声明:本文为博主原创文章,未经博主允许不得转载. 接上一节: Linux TTY ...

  2. Linux设备驱动模型底层架构及组织方式

    1.什么是设备驱动模型? 设备驱动模型,说实话这个概念真的不好解释,他是一个比较抽象的概念,我在网上也是没有找到关于设备驱动模型的一个定义,那么今天就我所学.所了解 到的,我对设备驱动模型的一个理解: ...

  3. Linux tty驱动架构

    Linux tty子系统包含:tty核心,tty线路规程和tty驱动.tty核心是对整个tty设备的抽象,对用户提供统一的接口,tty线路规程是对传输数据的格式化,tty驱动则是面向tty设备的硬件驱 ...

  4. Smart210学习记录------linux串口驱动

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=27025492&id=327609 一.核心数据结构 串口驱动有 ...

  5. linux串口驱动分析——打开设备

    串口驱动是由tty_driver架构实现的.一个应用程序中的函数要操作硬件,首先会经过tty,级级调用之后才会到达驱动之中.本文先介绍应用程序中打开设备的open函数的整个历程. 首先在串口初始化中会 ...

  6. linux串口驱动分析

    linux串口驱动分析 硬件资源及描写叙述 s3c2440A 通用异步接收器和发送器(UART)提供了三个独立的异步串行 I/O(SIO)port,每一个port都能够在中断模式或 DMA 模式下操作 ...

  7. linux串口驱动分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/11946591 硬件资源及描述 s3c2440A 通用异步接收器和发送器(UART)提供了 ...

  8. Linux设备驱动中的软件架构思想

    目录 更新记录 一.Linux驱动的软件架构 1.1 出发点 1.2 分离思想 1.3 分层思想 二.platform设备驱动 2.1 platform设备 2.2 platform驱动 2.3 pl ...

  9. Linux 设备驱动 Edition 3

    原文网址:http://oss.org.cn/kernel-book/ldd3/index.html Linux 设备驱动 Edition 3 By Jonathan Corbet, Alessand ...

随机推荐

  1. 对MP4一些概念的理解

    首先,对视频一些基本概念的理解: I帧:i帧又称为内编码帧,是一种自带全部信息的独立帧,可独立解码,可理解为一张静态图片,视频序列中的第一个帧始终是i帧,因为它是关键帧. P帧:P帧又称为帧间预测编码 ...

  2. 80C51存储器与C51内存优化

    80C51在物理结构上有四个存储空间:片内程序存储器.片外程序存储器.片内数据存储器和片外数据存储器.但在逻辑上,即从用户使用的角度上,80C51有三个存储空间:片内外统一编址的64KB的程序存储器地 ...

  3. CentOS下安装Python3

    目录 CentOS下安装Python3 下载 解压 配置 gcc sudo权限 vim 编译 安装 添加软链接 pip安装出错,找不到SSL 安装virtualenv和virtualenvwrappe ...

  4. DevOps简介

    DevOps 是一个完整的面向IT运维的工作流,以 IT 自动化以及持续集成(CI).持续部署(CD)为基础,来优化程式开发.测试.系统运维等所有环节. DevOps的概念 DevOps一词的来自于D ...

  5. Play with Floor and Ceil UVA - 10673(拓展欧几里得)

    因为我现在还不会用这个...emm...蒟蒻...只看了 从来没用过....所以切一道水题...练一下... 人家讲的很好  https://blog.csdn.net/u012860428/arti ...

  6. How do you add? UVA - 10943(组合数的隔板法!!)

    题意: 把K个不超过N的非负整数加起来,使它们的和为N,有多少种方法? 隔板法...不会的可以买一本高中数学知识清单...给高中班主任打个广告.... 隔板法分两种...一种是不存在空集 = C(n- ...

  7. 使用System.getProperty方法,如何配置JVM系统属性

    原创文章,欢迎转载,转载请注明出处! 很多时候我们需要在项目中读取外部属性文件,用到了System.getProperty("")方法.这个方法需要配置JVM系统属性,那么如何配置 ...

  8. c++11 静态断言

    c++11 静态断言 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #includ ...

  9. 动态规划DP的优化

    写一写要讲什么免得忘记了.DP的优化. 大概围绕着"是什么","有什么用","怎么用"三个方面讲. 主要是<算法竞赛入门经典>里 ...

  10. 前端学习 -- Css -- display和Visibility

    display 将一个内联元素变成块元素,通过display样式可以修改元素的类型.可选值: 1 inline:可以将一个元素作为内联元素显示. 2 block: 可以将一个元素设置块元素显示. 3 ...