首页 技术 正文
技术 2022年11月14日
0 收藏 718 点赞 3,971 浏览 5988 个字

以下是在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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848