五: uart_add_one_port()操作本文引用地址:http://www.eepw.com.cn/article/201610/305916.htm

在前面提到。在对uart设备文件过程中。会将操作转换到对应的port上,这个port跟uart_driver是怎么关联起来的呢?这就是uart_add_ont_port()的主要工作了。

顾名思义,这个函数是在uart_driver增加一个port.代码如下:

int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)

{

struct uart_state *state;

int ret = 0;

struct device *tty_dev;

BUG_ON(in_interrupt());

if (port->line >= drv->nr)

return -EINVAL;

state = drv->state + port->line;

mutex_lock(port_mutex);

mutex_lock(state->mutex);

if (state->port) {

ret = -EINVAL;

goto out;

}

state->port = port;

state->pm_state = -1;

port->cons = drv->cons;

port->info = state->info;

/*

* If this port is a console, then the spinlock is already

* initialised.

*/

if (!(uart_console(port) (port->cons->flags CON_ENABLED))) {

spin_lock_init(port->lock);

lockdep_set_class(port->lock, port_lock_key);

}

uart_configure_port(drv, state, port);

/*

* Register the port whether it's detected or not. This allows

* setserial to be used to alter this ports parameters.

*/

tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);

if (likely(!IS_ERR(tty_dev))) {

device_can_wakeup(tty_dev) = 1;

device_set_wakeup_enable(tty_dev, 0);

} else

printk(KERN_ERR Cannot register tty device on line %d\n,

port->line);

/*

* Ensure UPF_DEAD is not set.

*/

port->flags = ~UPF_DEAD;

out:

mutex_unlock(state->mutex);

mutex_unlock(port_mutex);

return ret;

}

首先这个函数不能在中断环境中使用。 Uart_port->line就是对uart设备文件序号。它对应的也就是uart_driver->state数组中的uart_port->line项。

它主要初始化对应uart_driver->state项。接着调用uart_configure_port()进行port的自动配置。然后注册tty_device.如果用户空间运行了udev或者已经配置好了hotplug.就会在/dev下自动生成设备文件了。

操作流程图如下所示:

六:设备节点的open操作

在用户空间执行open操作的时候,就会执行uart_ops->open. Uart_ops的定义如下:

static const struct tty_operations uart_ops = {

.open = uart_open,

.close = uart_close,

.write = uart_write,

.put_char = uart_put_char,

.flush_chars = uart_flush_chars,

.write_room = uart_write_room,

.chars_in_buffer= uart_chars_in_buffer,

.flush_buffer = uart_flush_buffer,

.ioctl = uart_ioctl,

.throttle = uart_throttle,

.unthrottle = uart_unthrottle,

.send_xchar = uart_send_xchar,

.set_termios = uart_set_termios,

.stop = uart_stop,

.start = uart_start,

.hangup = uart_hangup,

.break_ctl = uart_break_ctl,

.wait_until_sent= uart_wait_until_sent,

#ifdef CONFIG_PROC_FS

.read_proc = uart_read_proc,

#endif

.tiocmget = uart_tiocmget,

.tiocmset = uart_tiocmset,

};

对应open的操作接口为uart_open.代码如下:

static int uart_open(struct tty_struct *tty, struct file *filp)

{

struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;

struct uart_state *state;

int retval, line = tty->index;

BUG_ON(!kernel_locked());

pr_debug(uart_open(%d) called\n, line);

/*

* tty->driver->num won't change, so we won't fail here with

* tty->driver_data set to something non-NULL (and therefore

* we won't get caught by uart_close())。

*/

retval = -ENODEV;

if (line >= tty->driver->num)

goto fail;

/*

* We take the semaphore inside uart_get to guarantee that we won't

* be re-entered while allocating the info structure, or while we

* request any IRQs that the driver may need. This also has the nice

* side-effect that it delays the action of uart_hangup, so we can

* guarantee that info->tty will always contain something reasonable.

*/

state = uart_get(drv, line);

if (IS_ERR(state)) {

retval = PTR_ERR(state);

goto fail;

}

/*

* Once we set tty->driver_data here, we are guaranteed that

* uart_close() will decrement the driver module use count.

* Any failures from here onwards should not touch the count.

*/

tty->driver_data = state;

tty->low_latency = (state->port->flags UPF_LOW_LATENCY) ? 1 : 0;

linux设备模型之tty驱动架构分析,linux设备模型之uart驱动架构分析相关推荐

  1. linux设备驱动之串口移植,Linux设备驱动之UART驱动结构

    一.对于串口驱动Linux系统中UART驱动属于终端设备驱动,应该说是实现串口驱动和终端驱动来实现串口终端设备的驱动.要了解串口终端的驱动在Linux系统的结构就先要了解终端设备驱动在Linux系统中 ...

  2. 基于Linux的UART驱动框架源码分析笔记

    文章目录 前言 一.I.MX6ULL串口接收和发送方式 1.非DMA方式 1.1.接收方式 1.2 发送方式 2.DMA方式 2.1.接收方式 2.2 发送方式 二.UART驱动注册 1.uart_r ...

  3. linux UART驱动

    目录 一.UART驱动关键结构体 二.UART驱动分析 三.UART驱动编写 一.UART驱动关键结构体 1.uart_driver 结构体 (1) uart_driver 结构体表示 UART 驱动 ...

  4. CC2640R2F BLE5.0 CC2640R2F UART驱动

    UART驱动 这一节我们讲一下UART驱动的分层实现,UART APIs以及如何调用UART APIs来实现基本的串口打印. 概述 UART用于芯片和串行端口之间的数据传输,UART驱动程序经过多层的 ...

  5. linux内核部件分析之——设备驱动模型之class

    前面看过了设备驱动模型中的bus.device.driver,这三种都是有迹可循的.其中bus代表实际的总线,device代表实际的设备和接口,而driver则对应存在的驱动.但本节要介绍的class ...

  6. linux内核部件分析(十)——设备驱动模型之class,linux内核部件分析(十)——设备驱动模型之class...

    前面看过了设备驱动模型中的bus.device.driver,这三种都是有迹可循的.其中bus代表实际的总线,device代表实际的设备和接口,而driver则对应存在的驱动.但本节要介绍的class ...

  7. linux内核组件分析之--设备驱动模型之bus

    前面我们分析了设备驱动模型中的device和driver,device和driver本来是不相关的东西,只因为bus的存在,才被联系到了一起.本节就来看看设备驱动模型中起枢纽作用的bus.本节的头文件 ...

  8. linux i2c adapter 增加设备_LINUX设备驱动模型分析之四 设备模块相关(DEVICE)接口分析...

    本系列前几篇文章链接如下: <LINUX设备驱动模型分析之一 总体概念说明> <LINUX设备驱动模型分析之二 总线(BUS)接口分析> <LINUX设备驱动模型分析之三 ...

  9. linux下camera驱动分析_LINUX设备驱动模型分析之三 驱动模块相关(DRIVER)接口分析...

    本系列前几篇文章链接如下: <LINUX设备驱动模型分析之一 总体概念说明> <LINUX设备驱动模型分析之二 总线(BUS)接口分析> 上一章我们分析了bus-driver- ...

  10. 嵌入式Linux驱动笔记(十六)------设备驱动模型(kobject、kset、ktype)

    ###你好!这里是风筝的博客, ###欢迎和我一起交流. 前几天去面试,被问到Linux设备驱动模型这个问题,没答好,回来后恶补知识,找了些资料,希望下次能答出个满意答案. Linux早期时候,一个驱 ...

最新文章

  1. 使用 Android Studio 跑新浪微博SDK Demo遇到的问题及解决
  2. java事务过大影响系统性能吗_Java编程性能优化-影响性能的因素你都知道吗?
  3. Python编码规范:IF中的多行条件
  4. php 变量 类名,关于php:使用变量类名和静态方法时出错
  5. redhat 添加ssh端口_Linux修改SSH远程登录端口 --服务器安全篇
  6. linux释放内存后设备起不来,Linux-Memory小记
  7. 算法导论 思考题6-2
  8. svn拒绝访问是什么原因_为什么Windows无法访问U盘|电脑U盘被拒绝访问
  9. mysql字符集导出_关于mysql字符集及导入导出
  10. 你为什么用微信,不用 QQ ?
  11. 单例设计模式全局缓存accessToken
  12. 内存管理-内存slub分配器(二)
  13. 前端日志输出分享(没有技术,纯娱乐)
  14. python卸载pip重新安装_pip的卸载、重装、升级(from pip19.3 to pip20.1)
  15. Chrome浏览器保存整个网页为图片的方法
  16. 多个excel工作簿合并_无需VBA代码,1分钟合并多个工作簿至一个工作簿!
  17. Windows系统内置测试工具(winsat)
  18. kettle复杂表头的Excel数据源处理
  19. Win7怎么设置工作组?Win7电脑设置工作组的方法
  20. Web站点的欢迎页面 web.xml - welcome-file-list

热门文章

  1. 看门狗计算机丢失xinput13.dll,windows10系统打开程序提示丢失xinput13dll怎么办
  2. matlab imf1,NGPM_v1.4y GA多目标优化的程序 带约束 里面有教程 matlab 272万源代码下载- www.pudn.com...
  3. html打印日志_Graylog(四)使用Log4j2发送日志到Graylog
  4. kubernetes安装_在 Kubernetes 上安装 Gitlab CI Runner
  5. mysql5.7改了配置文件怎么生效_如何找到并修改MySQL57的配置文件m
  6. python time sleep 阻塞 异步_Python Tornado异步请求被阻塞
  7. linux 二进制安装mysql
  8. 虚拟机 django 端口无法连接
  9. Java并发和多线程3:线程调度和有条件取消调度
  10. ssh连接Linux很慢,且ssh传输文件很慢的解决方案