linux 串口console注册

printk打印的信息要在console注册之后才能打印出来,但是在start_kernel中,还没有注册console之前
已经有printk的动作.这些信息会保存在printk的buf里边,这个buf大小可以配置:
General setup
(14) Kernel log buffer size (16 => 64KB, 17 => 128KB)
14=2^14=16KB, 16=2^16=64KB......

printk->vprintk_emit->
->log_store 将打印信息保存到printk的buf中
->console_unlock->call_console_drivers->console->write打印信息


console 的注册:
static struct console cdns_uart_console = {
.name = CDNS_UART_TTY_NAME,
.write = cdns_uart_console_write,
.device = uart_console_device,
.setup = cdns_uart_console_setup,
.flags = CON_PRINTBUFFER, // 打印出buf的printk信息
.index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
.data = &cdns_uart_uart_driver,
};

static int __init cdns_uart_console_init(void)
{
register_console(&cdns_uart_console);
return 0;
}
console_initcall(cdns_uart_console_init);

#define console_initcall(fn) \
static initcall_t __initcall_##fn \
__used __section(.con_initcall.init) = fn


console_initcall宏定义把cdns_uart_console_init放到.con_initcall.init段中,该段可从vmlinux.lds中找到.
__con_initcall_start = .; *(.con_initcall.init) __con_initcall_end = .;
该段的起始地址是__con_initcall_start,该段中的所有函数在driver\tty\tty_io.c\console_init函数中调用,
console_init在start_kernel函数中被调用. console的注册和driver是不相关的,它有自己的setup,write函数,
register_console之后,console就可以输出信息.

for (i = 0, c = console_cmdline;
    i < MAX_CMDLINECONSOLES && c->name[0];
    i++, c++) {
if (strcmp(c->name, newcon->name) != 0) /* 驱动中定义的console->name要与命令行中的一致 */
continue;
if (newcon->index >= 0 &&
   newcon->index != c->index)
continue;
if (newcon->index < 0)
newcon->index = c->index;


if (_braille_register_console(newcon, c))
return;


if (newcon->setup &&
   newcon->setup(newcon, console_cmdline[i].options) != 0) /* 调用console->setup函数,options就是 115200n81之类的参数 */
break;
newcon->flags |= CON_ENABLED;
newcon->index = c->index;
if (i == selected_console) {
newcon->flags |= CON_CONSDEV;
preferred_console = selected_console;
}
break;
}

if (newcon->flags & CON_PRINTBUFFER) { // 只有设置了CON_PRINTBUFFER,才会打印被buf的信息
/*
* console_unlock(); will print out the buffered messages
* for us.
*/
raw_spin_lock_irqsave(&logbuf_lock, flags);
console_seq = syslog_seq;
console_idx = syslog_idx;
console_prev = syslog_prev;
raw_spin_unlock_irqrestore(&logbuf_lock, flags);
/*
* We're about to replay the log buffer.  Only do this to the
* just-registered console to avoid excessive message spam to
* the already-registered consoles.
*/
exclusive_console = newcon;
}
console_unlock(); // 该函数打印信息到uart中.

你可能感兴趣的:(---------linux,driver-------)