以下是在include/uapi/linux/tty.h中定义了现有的线规号,如果需要定义新的,则需要在后面添加新的

 /* line disciplines */
#define N_TTY 0
#define N_SLIP 1
... ...
#define N_TRACESINK 23 /* Trace data routing for MIPI P1149.7 */
#define N_TRACEROUTER 24 /* Trace data routing for MIPI P1149.7 */

1.应用层发送数据

-->write()

  -->file_operation.tty_write                  /* file_operation函数集在何时被赋值? */

    -->do_tty_write(ld->ops->write, tty, file, buf, count)   /* tty_ldisc->tty_ldisc_ops->write */

      -->tty_ldisc_ops.ldisc_write /* tty_ldisc_ops函数集在何时被赋值? */ ///该write函数是在线路规程模块中定义

        -->tty->driver->ops->write (tty, tbuf->buf, tbuf->count) /* tty_struct->tty_driver->tty_operations->write */

          -->tty_operations.uart_write     /* tty_operation函数集在何时被赋值? */

            -->uart_start(tty);

              -->__uart_start(tty);

                -->port->ops->start_tx(port);  /* uart_port->uart_ops->start_tx */

                  -->uart_ops.imx_start_tx  /* uart_ops函数集在何时被赋值? */

至此消息也发送出去了,从消息流程可以看出来消息是经过ldisc线路规程层,然后tty层,然后到硬件驱动层。

上面的调用关系有个问题,为什么顺序是调到ldisc层,又调回tty层,再直接到hardware层?

  • file_operation.do_tty_write(ld->ops->write, tty, file, buf, count)::: tty_ldisc->ops->(*write)(struct tty_struct *tty, struct file *file, const unsigned char *buf, size_t nr);
  • tty_ldisc_ops.n_tty_write(struct tty_struct *tty, struct file *file,const unsigned char *buf, size_t nr):::tty->ops->write(tty, b, nr);  tty_operations->ops->(*write)(struct tty_struct * tty,const unsigned char *buf, int count)
  • tty_operations.uart_write(struct tty_struct * tty, const unsigned char *buf, int count )::: memcpy(circ->buf + circ->head, buf, c);
  • uart_ops.imx_start_tx(struct uart_port *port)::: writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);

2.uart和tty逐层调用关系

2.1.关于file_operation函数集的赋值

  • uart_register_driver函数中完成了file_operations和tty_operations函数集的初始化

-->uart_register_driver(uart_driver*)  //uart driver驱动文件中

  -->tty_set_operations(normal, &uart_ops);//完成tty_operation赋值,serail_core.c中初始化结构体

  -->tty_register_driver(normal)

    -->tty_cdev_add(driver, dev, 0, driver->num);

      -->cdev_init(&driver->cdevs[index], &tty_fops);//而tty_fops为file_operations 类型,tty_io.c中初始化结构体

 /* drivers/tty/tty_io.c */
static const struct file_operations tty_fops = {
  .llseek = no_llseek,
  .read = tty_read,
  .write = tty_write,
  .poll = tty_poll,
  .unlocked_ioctl = tty_ioctl,
  .compat_ioctl = tty_compat_ioctl,
  .open = tty_open,
  .release = tty_release,
  .fasync = tty_fasync,
};

2.2.关于tty_operation函数集的赋值(同上)

-->uart_register_driver(uart_driver*)  //uart driver驱动文件中

  -->tty_set_operations(normal, &uart_ops);  //完成tty_operation赋值,serial_core.c

  -->tty_register_driver(normal)

 /* drivers/tty/serial/serial_core.c */
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,
  .set_ldisc = uart_set_ldisc,
  .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
  .proc_fops = &uart_proc_fops,
#endif
  .tiocmget = uart_tiocmget,
  .tiocmset = uart_tiocmset,
  .get_icount = uart_get_icount,
#ifdef CONFIG_CONSOLE_POLL
  .poll_init = uart_poll_init,
  .poll_get_char = uart_poll_get_char,
  .poll_put_char = uart_poll_put_char,
#endif
};

2.3.关于tty_ldisc_ops函数集的赋值

-->tty_register_ldisc(ldisc, tty_ldisc_ops);  /* 此函数可以是在线路规程模块初始化时调用 */

  -->tty_ldiscs[disc] = tty_ldisc_ops;    /* 可见是tty_ldiscs中包含每个线路规程号对应的ops函数集, 假如是默认tty, 则在n_tty.c中,全局变量tty_ldisc_N_TTY */

下面只是默认N_tty.c中默认线路规程号N_tty的例子,用户可以编写自己的线路规程模块,进行数据封装,有自己的方法集。

 /* drivers/tty/N_tty.c */
struct tty_ldisc_ops tty_ldisc_N_TTY = {
  .magic = TTY_LDISC_MAGIC,
  .name = "n_tty",
  .open = n_tty_open,
  .close = n_tty_close,
  .flush_buffer = n_tty_flush_buffer,
  .chars_in_buffer = n_tty_chars_in_buffer,
  .read = n_tty_read,
  .write = n_tty_write,
  .ioctl = n_tty_ioctl,
  .set_termios = n_tty_set_termios,
  .poll = n_tty_poll,
  .receive_buf = n_tty_receive_buf,
  .write_wakeup = n_tty_write_wakeup,
  .fasync = n_tty_fasync,
  .receive_buf2 = n_tty_receive_buf2,
};

2.4.关于uart_ops函数集赋值

-->platform_driver_register(platform_driver*)

  -->serial_imx_probe()  //设备驱动匹配

    -->sport->port.ops = &imx_pops; //调用也是通过port口来调ops

这里举的例子是freescale的串口方法集

 /* drivers/tty/serial/imx.c */
static struct uart_ops imx_pops = {
  .tx_empty = imx_tx_empty,
  .set_mctrl = imx_set_mctrl,
  .get_mctrl = imx_get_mctrl,
  .stop_tx = imx_stop_tx,
  .start_tx = imx_start_tx,
  .stop_rx = imx_stop_rx,
  .enable_ms = imx_enable_ms,
  .break_ctl = imx_break_ctl,
  .startup = imx_startup,
  .shutdown = imx_shutdown,
  .flush_buffer = imx_flush_buffer,
  .set_termios = imx_set_termios,
  .type = imx_type,
  .release_port = imx_release_port,
  .request_port = imx_request_port,
  .config_port = imx_config_port,
  .verify_port = imx_verify_port,
#if defined(CONFIG_CONSOLE_POLL)
  .poll_get_char = imx_poll_get_char,
  .poll_put_char = imx_poll_put_char,
#endif
#ifdef CONFIG_IB2_SUPPORT
  .ioctl = imx_ioctl,
#endif
};

3.线路规程号配置

 3.1.Console的线路规程是怎么回事

-->__init console_init(void)

   -->tty_ldisc_begin();  /* Setup the default TTY line discipline. */

      -->(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);  /* tty_ldisc_N_TTY中对应都是n_tty_**函数集 */

3.2.那么其他串口的的线路规程默认是怎样的?也是N_TTY吗

答案:是的,这要从应用层open设备文件开始说起

亦可参考该链接:http://blog.csdn.net/rockrockwu/article/details/7897283

-->open("/dev/ttys0",O_RDWR|O_NOCTTY);  /* app layer */

  -->tty_operations.tty_open

    -->tty_init_dev

      -->initialize_tty_struct

        -->tty_ldisc_init

          -->struct tty_ldisc *ld = tty_ldisc_get(N_TTY)

          -->struct tty_struct *tty->ldisc = ld;  /* 至此tty_struct和N_TTY绑定,该串口默认线路规程 */

3.3.应用程序修改线路规程

如果不适用默认的线路规程,需要在串口实现一些协议,那该如何做呢?--新建线路规程号,实现线路规程代码

ioctl(fd, TIOCSETD, &ldisc);  //application:ldisc=25

-->tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)  /* tty_io.c*/

  -->tiocsetd(tty, p)

    -->tty_set_ldisc(tty, ldisc)

      -->tty->ldisc = new_ldisc;  /* Now set up the new line discipline. */

至此完成了新的ldisc设置。

4.推荐几个Linux uart的博客

http://www.uml.org.cn/embeded/201209071.asp

http://www.wowotech.net/linux_kenrel/183.html

http://blog.csdn.net/goodluckwhh/article/details/13368279

[uart]2.tty和uart的函数调用流程的更多相关文章

  1. [tty与uart]2.tty和uart的函数调用流程

    以下是在include/uapi/linux/tty.h中定义了现有的线规号,如果需要定义新的,则需要在后面添加新的 /* line disciplines */ #define N_TTY 0 #d ...

  2. [tty与uart]3.tty驱动分析

    转自:http://www.wowotech.net/linux_kenrel/183.html 目录: 1 首先分析设备驱动的注册 1.1 uart_register_driver分析 1.2 tt ...

  3. [uart]3.tty驱动分析

    转自:http://www.wowotech.net/linux_kenrel/183.html 目录: 1 首先分析设备驱动的注册 1.1 uart_register_driver分析 1.2 tt ...

  4. [tty与uart]1.Linux中tty框架与uart框架之间的调用关系剖析

    转自:http://developer.51cto.com/art/201209/357501_all.htm 目录 1.tty框架 2.uart框架 3.自底向上 4.自顶向下 5.关系图 在这期间 ...

  5. tty初探 — uart驱动框架分析

    写在前面: 我们没有讲UART驱动,不过我们认为,只要系统学习了第2期,应该具备分析UART驱动的能力,小编做答疑几年以来,陆陆续续有不少人问到UART驱动怎么写,所以今天就分享一篇深度长文(1700 ...

  6. [tty与uart]UART中的硬件流控RTS与CTS

    转自:http://blog.csdn.net/zeroboundary/article/details/8966586 在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES ) ...

  7. [tty与uart]理解线路规程的作用

    转自:http://biancheng.dnbcw.info/linux/336240.html Linux OS的设备驱动有相当经典的抽象思想以及分层思想.与通信世界里面的思想相一致. 一.在Lin ...

  8. [tty与uart]stty命令使用

    中文解释链接:http://linux.51yip.com/search/stty 英文解释链接:http://pubs.opengroup.org/onlinepubs/9699919799/uti ...

  9. s3c2440裸机-UART编程(二、UART编程实现)

    UART编程 1.初始化 我们的2440支持3个UART串口,以uart0为例讲解. 那么我们需要实现以下这几个函数完成串口的最基本功能: (1)uart0_init()用于初始化串口 (2)putc ...

随机推荐

  1. shell脚本死循环判断nginx日志reqest_time时间大于3秒是否增加,若增加发送相关日志信息到开发人员

    #!/bin/bash while [ 1 ] do pre_request_time_count=`cat /var/log/nginx/access.log |awk '{print $NF}'| ...

  2. HDUOJ----4509湫湫系列故事——减肥记II

    湫湫系列故事——减肥记II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. 两个有序数组求中位数log(m+n)复杂度

    leetcode 第4题 中位数技巧: 对于长度为L的有序数组,它的中位数是(a[ceil((L+1)/2)]+a[floor((L+1)/2)])/2 算法原理: 类似三分法求极值 两个人都前进,谁 ...

  4. pip安装注意事项

    pip源 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...

  5. 深入理解SpringBoot配置

    一.application.properties的位置 1.当前目录的 "/config"的子目录下 2.当前目录下 3.classpath根目录的"/config&qu ...

  6. 计算机组成原理实验之CPU组成与指令周期实验

    (实验五  CPU组成与指令周期实验) 课程 计算机组成原理实验 实验日期 2015 年 12 月  8 日 一.实验目的 1.将微程序控制器同执行部件(整个数据通路)联机,组成一台模型计算机. 2. ...

  7. struts2 xml配置文件配置传参数

    传参方式 重定向 第一方式: <action name="search" method="search"            class="c ...

  8. 关于android.view.WindowLeaked(窗体泄露)的解决方案

    虽然是小问题一个,但也困扰了我一段时间,现在记下来,给自己做个备忘,也可以给其他人一个参考 view plaincopy to clipboardprint? 01-08 01:49:27.874:  ...

  9. Externalizable的使用方法

    package com.itbuluoge.object; import java.io.Externalizable; import java.io.FileInputStream; import ...

  10. struts2每次访问都会创建一个新的session

    1.第一次 项目在测试过程中,突然发现登陆之后再去访问其他菜单时都会提示未登录: 查看日志之后发现是因为很多次请求时都会自动创建一个新的session,这就费解了, 因为之前也没改动什么session ...