现在的位置: 首页 > 综合 > 正文

console,uart,tty的关联关系

2013年07月16日 ⁄ 综合 ⁄ 共 5902字 ⁄ 字号 评论关闭

console可以是串口,也可以是vga,console确实是只能输出,write,内核打印。

在UNIX系统中,计算机显示器通常被称为控制台终端(Console)。它仿真了类型为
Linux的一种终端(TERM=Linux),并且有一些设备特殊文件与之相关联:tty0、tty1、tty2等。当你在控制台上登录时,使用的是
tty1。使用Alt+[F1—F6]组合键时,我们就可以切换到tty2、tty3等上面去。tty1
–tty6等称为虚拟终端,而tty0则是当前所使用虚拟终端的一个别名,系统所产生的信息会发送到该终端上。因此不管当前正在使用哪个虚拟终端,系统信
息都会发送到控制台终端上。你可以登录到不同的虚拟终端上去,因而可以让系统同时有几个不同的会话期存在。只有系统或超级用户root可以向/dev
/tty0进行写操作,

serial_em86xx.c实现的是串口uart驱动,也注册了console,在
console_init中调用console初始化,但是如果serial不做console的话,应该就不用注册console,但是串口uart驱
动是必须的,就是uart_register_driver,其中涉及最关键的tty_driver,


tty是一类char设备的通称,它们有相同的特性,比如对^C的处理,驱动使用tty_register_driver注册一个tty。

/dev/console是一个虚拟的tty,它映射到真正的tty上,如何映射等会再说。

console
有多种含义,这里特指printk输出的设备,驱动使用register_console注册一个console。
console和tty有很大区别:console是个只输出的设备,功能很简单,只能在内核中访问;tty是char设备,可以被用户程序访问。

实际的驱动比如串口对一个物理设备会注册两次,一个是tty,一个是console,并通过在console的结构中记录tty的主次设备号建立了联系。


内核中,tty和console都可以注册多个。当内核命令行上指定console=ttyS0之类的参数时,首先确定了printk实际使用那个
console作为输出,其次由于console和tty之间的对应关系,打开/dev/console时,就会映射到相应的tty上。用一句话说:
/dev/console将映射到默认console对应的tty上。

顺便说一句,console=ttyS0和/dev/ttyS0包含相同的设备名字完全是巧合,不同也没事。


所以如果是一个单纯的串口通信,可以不用实现console,只要实现uart驱动,包括tty_driver,就可以在应用层调用串口设备实现的
接口,比如open(/dev/ttyS0),然后selest,用于等待串口上的数据,而在内和层,驱动中,就是用wait_queue等来实现
select的。/dev/tty,/dev/console,/dev/tty是正在使用的虚拟终端,因此在这里tty_open就是ttyS0,因为
前台进程的控制终端现在就是ttyS0,/dev/tty可以在用户空间访问,就是用户打印可以在该空间。

serial.c不是必须的,而serial_em86xx.c中提供了与其相似的功能,比如shutdown,startup等方法,以及
console_write等,而console是在内核打印,而在erial_em86xx.c中,已经有串口uart驱动,而且与
tty_driver关联起来,所以tty_open打开的就是ttyS0了。如果当前进程有控制终端(Controlling
Terminal)的话,那么/dev/tty就是当前进程的控制终端的设备特殊文件,用户打印。tty_io.c中除了有
tty_init,tty_open之外还有tty_poll,用于实现poll,另外还有用于异步通知的tty_fasync,主要是通过SIGIO信
号来通知用户进程,因为串口速度不快,用异步通知也可以。而如果速度要求快的话,就需要在中断中唤醒进程,用户程序用poll,select来等待了。

ldd3中有tty_driver的介绍了。

tty_ldisc是和console有关系的,n_tty,多串口支持,SIGIO。

tty_ldisc下面才是tty_driver。

串口如果是普通功能,就可以不跟tty关联起来,而实现char设备即可。

console是内核打印,在tty_io.c中console_init中有初始化
em86xx_uart_console_init,在serial_em86xx.c中,有register_console,而
register_console的实现是在printk.c,从而也说明了console内核打印和用户打印tty底层实现不一样。而用户程序打印以及
输入是tty,代码在行规n_tty.c中,而tty_ldisc的注册也是在console_init开始部分的,

(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);

只不过这里的console_init部分是与定义的
CONFIG_SERIAL_EM86XX_CONSOLE没有关系的,而是与tty有关
的,n_tty->tty_io->serial_em86xx,serial_core.也就是说即使没有
CONFIG_SERIAL_EM86XX_CONSOLE,假设console内核打印是在framebuffer,键盘的tty0,但用户程序的打印
输入是在/dev/tty,最终是可以是在/dev/ttyS0,从而用户程序就可以通过/dev/ttyS0普通串口通信与串口通信设备通信了,而此时
串口驱动可以不实现serial_console功能.

console.c

#ifdef CONFIG_VT
     con_init();
     #endif

tty_init

#ifdef CONFIG_VT
dev_console_driver = dev_tty_driver;
dev_console_driver.driver_name = "/dev/vc/0";
dev_console_driver.name = dev_console_driver.driver_name + 5;
dev_console_driver.major = TTY_MAJOR;
dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;

if (tty_register_driver(&dev_console_driver))
   panic("Couldn't register /dev/tty0 driver/n");

kbd_init();
#endif

console.c以及CONFIG_VT是内核控制台,内核打印方面的。

config VT
bool "Virtual terminal" if EMBEDDED
select INPUT
default y if !VIOCONS
---help---
    If you say Y here, you will get support for terminal devices with
    display and keyboard devices. These are called "virtual" because you
    can run several virtual terminals (also called virtual consoles) on
    one physical terminal. This is rather useful, for example one
    virtual terminal can collect system messages and warnings, another
    one can be used for a text-mode user session, and a third could run
    an X session, all in parallel. Switching between virtual terminals
    is done with certain key combinations, usually Alt-<function key>.

    The setterm command ("man setterm") can be used to change the
    properties (such as colors or beeping) of a virtual terminal. The
    man page console_codes(4) ("man console_codes") contains the special
    character sequences that can be used to change those properties
    directly. The fonts used on virtual terminals can be changed with
    the setfont ("man setfont") command and the key bindings are defined
    with the loadkeys ("man loadkeys") command.

    You need at least one virtual terminal device in order to make use
    of your keyboard and monitor. Therefore, only people configuring an
    embedded system would want to say N here in order to save some
    memory; the only way to log into such a system is then via a serial
    or network connection.

    If unsure, say Y, or else you won't be able to do much with your new
    shiny Linux system :-)

config VT_CONSOLE
bool "Support for console on virtual terminal" if EMBEDDED
depends on VT
default y
---help---
    The system console is the device which receives all kernel messages
    and warnings and which allows logins in single user mode. If you
    answer Y here, a virtual terminal (the device used to interact with
    a physical terminal) can be used as system console. This is the most
    common mode of operations, so you should say Y here unless you want
    the kernel messages be output only to a serial port (in which case
    you should say Y to "Console on serial port", below).

    If you do say Y here, by default the currently visible virtual
    terminal (/dev/tty0) will be used as system console. You can change
    that with a kernel command line option such as "console=tty3" which
    would use the third virtual terminal as system console. (Try "man
    bootparam" or see the documentation of your boot loader (lilo or
    loadlin) about how to pass options to the kernel at boot time.)

    If unsure, say Y.


config SERIAL_8250_CONSOLE
bool "Console on 8250/16550 and compatible serial port"
depends on SERIAL_8250=y
select SERIAL_CORE_CONSOLE
---help---
    If you say Y here, it will be possible to use a serial port as the
    system console (the system console is the device which receives all
    kernel messages and warnings and which allows logins in single user
    mode). This could be useful if some terminal or printer is connected
    to that serial port.

    Even if you say Y here, the currently visible virtual console
    (/dev/tty0) will still be used as the system console by default, but
    you can alter that using a kernel command line option such as
    "console=ttyS1". (Try "man bootparam" or see the documentation of
    your boot loader (grub or lilo or loadlin) about how to pass options
    to the kernel at boot time.)

    If you don't have a VGA card installed and you say Y here, the
    kernel will automatically use the first serial line, /dev/ttyS0, as
    system console.

    If unsure, say N.

 

 

原文地址:http://blogold.chinaunix.net/u2/83842/showart_1384754.html

抱歉!评论已关闭.