linux 串口驱动(二)初始化 【转】
轉(zhuǎn)自:http://blog.chinaunix.net/uid-27717694-id-3493611.html
8250串口的初始化:
(1)定義uart_driver、uart_ops、uart_port等結(jié)構(gòu)體的實例并在適當(dāng)?shù)牡胤礁呔唧w的硬件驅(qū)動情況初始化他們,當(dāng)然具體設(shè)備xxx的驅(qū)動可以將這些結(jié)構(gòu)體套在新定義的xxx_uart_driver、xxx_uart_ops、xxx_uart_port之內(nèi)。
(2)在模塊初始化調(diào)用uart_register()和uart_add_one_port()以注冊UART驅(qū)動并添加端口,在模塊卸載時調(diào)用uart_unregister_driver()和uart_remove_one_port()以注銷UART驅(qū)動以移除端口。
(3)根據(jù)具體硬件的uart_ops中的成員函數(shù),這些函數(shù)的實現(xiàn)成為UART驅(qū)動的主體工作。
1.串口結(jié)構(gòu)體,他們之間的關(guān)系如圖所示:
//(1)串口驅(qū)動結(jié)構(gòu)體
struct uart_driver {??
??? struct module?? *owner; //模塊所有者???
??? const char? *driver_name;?? //驅(qū)動名???
??? const char? *dev_name;? //設(shè)備名???
??? int? major; //主設(shè)備號???
??? int? minor; //次設(shè)備號???
??? int? nr;??? //支持串口個數(shù)???
??? struct console? *cons;//控制臺設(shè)備???
??? struct uart_state?? *state; //串口狀態(tài)???
??? struct tty_driver?? *tty_driver; //tty設(shè)備???
};?
//(2)串口端口結(jié)構(gòu)體
struct uart_port {??
??? spinlock_t? lock;??
??? unsigned long?? iobase; //io端口基地址???
??? unsigned char __iomem?? *membase; //內(nèi)存端口基地址???
??? unsigned int??? (*serial_in)(struct uart_port *, int); //串口讀函數(shù)?
??? void??? (*serial_out)(struct uart_port *, int, int); //串口寫方法
??? void??? (*set_termios)(struct uart_port *,struct ktermios *new,struct ktermios *old); //串口配置方法函數(shù)
??? void??? (*pm)(struct uart_port *, unsigned int state,unsigned int old);??
??? unsigned int??? irq;??? //中斷號???
??? unsigned long?? irqflags;?? //中斷標(biāo)志???
??? unsigned int??? uartclk;?? //串口時鐘
??? unsigned int??? fifosize;?? //fifo大小???????
??? unsigned char?? x_char;??
??? unsigned char?? regshift;?? //寄存器偏移值???????
??? unsigned char?? iotype; //io訪問類型???
??? unsigned char?? unused1;??
??? unsigned int??? read_status_mask;??
??? unsigned int??? ignore_status_mask;??
??? struct uart_state?? *state; //uart_state結(jié)構(gòu)體????
??? struct uart_icount? icount; //串口使用計數(shù)???
??? struct console? *cons;? //console控制臺???
#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)???
??? unsigned long?? sysrq;??
#endif???
??? upf_t?? flags;??
??? unsigned int??? mctrl;??
??? unsigned int??? timeout;??
??? unsigned int??? type; //串口類型?
??? const struct uart_ops?? *ops;?? //串口操作函數(shù)集???
??? unsigned int??? custom_divisor;??
??? unsigned int??? line;?? //端口號???
??? resource_size_t mapbase; //串口寄存器基地址(物理地址)
??? struct device?? *dev;?? //設(shè)備文件???
??? unsigned char?? hub6;??
??? unsigned char?? suspended;??
??? unsigned char?? irq_wake;??
??? unsigned char?? unused[2];??
??? void??? *private_data;??
};?
//(3)操作函數(shù)集
struct uart_ops {??
??? unsigned int??? (*tx_empty)(struct uart_port *);??? //發(fā)送緩沖區(qū)為空???
??? void??? (*set_mctrl)(struct uart_port *, unsigned int mctrl);?? //設(shè)置串口modem控制模式???
??? unsigned int??? (*get_mctrl)(struct uart_port *);?? //獲取串口modem控制模式???
??? void??? (*stop_tx)(struct uart_port *); //停止發(fā)送???
??? void??? (*start_tx)(struct uart_port *);??? //開始發(fā)送???
??? void??? (*send_xchar)(struct uart_port *, char ch);??
??? void??? (*stop_rx)(struct uart_port *); //停止接收???
??? void??? (*enable_ms)(struct uart_port *);?? //使能modem狀態(tài)信息???
??? void??? (*break_ctl)(struct uart_port *, int ctl);??
??? int (*startup)(struct uart_port *); //打開串口???
??? void??? (*shutdown)(struct uart_port *);??? //關(guān)閉串口???
??? void??? (*flush_buffer)(struct uart_port *);??
??? void??? (*set_termios)(struct uart_port *, struct ktermios *new,struct ktermios *old);? //設(shè)置串口參數(shù)???
??? void??? (*set_ldisc)(struct uart_port *, int new);??
??? void??? (*pm)(struct uart_port *, unsigned int state,unsigned int oldstate);??
??? int (*set_wake)(struct uart_port *, unsigned int state);??
??? const char *(*type)(struct uart_port *);??
??? void??? (*release_port)(struct uart_port *);??? //釋放端口???
??? int (*request_port)(struct uart_port *);??? //請求端口???
??? void??? (*config_port)(struct uart_port *, int);??? //配置端口???
??? int (*verify_port)(struct uart_port *, struct serial_struct *); //校驗端口???
??? int (*ioctl)(struct uart_port *, unsigned int, unsigned long);? //控制???
#ifdef CONFIG_CONSOLE_POLL???
??? void??? (*poll_put_char)(struct uart_port *, unsigned char);??
??? int (*poll_get_char)(struct uart_port *);??
#endif???
};?
//(4)uart_state
struct uart_state {??
??? struct tty_port port;??
??? int???? pm_state;??
??? struct circ_buf xmit;??
??? struct tasklet_struct?? tlet;??
??? struct uart_port??? *uart_port;//指向?qū)?yīng)的串口結(jié)構(gòu)??
};?
2.串口初始化
static int __init serial8250_init(void)
{
?int ret;
?if (nr_uarts > UART_NR)//UART_NR =3
??nr_uarts = UART_NR;//串口數(shù)量最多設(shè)為3個
?printk(KERN_INFO "Serial: 8250/16550 driver, ""%d ports, IRQ sharing %sabled\n", nr_uarts,share_irqs ? "en" : "dis");
?serial8250_reg.nr = UART_NR;//串口數(shù)量
?/*
??static struct uart_driver serial8250_reg = {
??.owner???= THIS_MODULE,
??.driver_name??= "serial",
??.dev_name??= "ttyS",
??.major???= TTY_MAJOR,//主設(shè)備號是4
??.minor???= 64,
??.cons???= SERIAL8250_CONSOLE,//終端
?};
?
?#define SERIAL8250_CONSOLE?&serial8250_console
?
?static struct console serial8250_console = {
??.name??= "ttyS",
??.write??= serial8250_console_write,
??.device??= uart_console_device,
??.setup??= serial8250_console_setup,//設(shè)置串口波特率,也就是設(shè)置串口。很重要,里面涉及到平臺特性,波特率相關(guān)。
??.early_setup?= serial8250_console_early_setup,
??.flags??= CON_PRINTBUFFER | CON_ANYTIME,
??.index??= -1,
??.data??= &serial8250_reg,
?};
?*/
?//函數(shù)定義在serial_core.c中
?//注冊uart串口驅(qū)動,完善uart_driver結(jié)構(gòu)serial8250_reg的uart_state成員及tty_driver成員,并注冊tty驅(qū)動
?ret = uart_register_driver(&serial8250_reg);
#endif
?if (ret)
??goto out;
?
?//創(chuàng)建一個platform_device結(jié)構(gòu):serial8250_isa_devs
?serial8250_isa_devs = platform_device_alloc("serial8250",PLAT8250_DEV_LEGACY);
?if (!serial8250_isa_devs) {
??ret = -ENOMEM;
??goto unreg_uart_drv;
?}
?
? //將該結(jié)構(gòu)serial8250_isa_devs注冊到總線上
?ret = platform_device_add(serial8250_isa_devs);
?if (ret)
??goto put_dev;
??
?//對uart_8250_port結(jié)構(gòu)serial8250_reg[]初始化,即對3個串口的uart_port結(jié)構(gòu)初始化,并添加端口
?serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
?
?/*
?static struct platform_driver serial8250_isa_driver = {
??.probe??= serial8250_probe,
??.remove??= __devexit_p(serial8250_remove),
??.suspend?= serial8250_suspend,
??.resume??= serial8250_resume,
??.driver??= {
???.name?= "serial8250",
???.owner?= THIS_MODULE,
??},
?};
?*/
?//注冊設(shè)備,會調(diào)用serial8250_probe()。怎樣調(diào)用的serial8250_probe???????
?ret = platform_driver_register(&serial8250_isa_driver);
?if (ret == 0)
??goto out;
?platform_device_del(serial8250_isa_devs);
put_dev:
?platform_device_put(serial8250_isa_devs);
unreg_uart_drv:
#ifdef CONFIG_SPARC
?sunserial_unregister_minors(&serial8250_reg, UART_NR);
#else
?uart_unregister_driver(&serial8250_reg);
#endif
out:
?return ret;
}
//注冊串口驅(qū)動
int uart_register_driver(struct uart_driver *drv)
{
?struct tty_driver *normal;
?int i, retval;
?BUG_ON(drv->state);
?//為串口的uart_driver結(jié)構(gòu)分配要指向的uart_state結(jié)構(gòu)的空間*串口數(shù)量
?drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
?if (!drv->state)
??goto out;
?
?//為uart_driver結(jié)構(gòu)分配要指向的tty_driver結(jié)構(gòu)的空間
?/*struct tty_driver *alloc_tty_driver(int lines)
?{
??struct tty_driver *driver;
?
??driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);//分配空間
??if (driver) {
???kref_init(&driver->kref);
???driver->magic = TTY_DRIVER_MAGIC;//tty_driver的魔數(shù)
???driver->num = lines;//串口數(shù)量3個
??}
??return driver;
?}*/
?normal = alloc_tty_driver(drv->nr);
?if (!normal)
??goto out_kfree;
?drv->tty_driver = normal;//賦值給uart_driver結(jié)構(gòu)的tty_driver成員
?//對tty_driver成員指向的結(jié)構(gòu)初始化
?normal->owner??= drv->owner;//THIS_MODULE
?normal->driver_name?= drv->driver_name;//"serial"
?normal->name??= drv->dev_name;//"ttyS"
?normal->major??= drv->major;//主設(shè)備號:TTY_MAJOR=4
?normal->minor_start?= drv->minor;//次設(shè)備號:64
?normal->type??= TTY_DRIVER_TYPE_SERIAL;//#define TTY_DRIVER_TYPE_SERIAL 0x0003
?normal->subtype??= SERIAL_TYPE_NORMAL;//#define SERIAL_TYPE_NORMAL? 1(定義在include/linux/tty_driver.h)
?normal->init_termios?= tty_std_termios;//終端的標(biāo)準(zhǔn)配置(drivers/tty/tty_io.c)
?normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;//配置c_cflag
?normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;//波特率設(shè)為9600
?normal->flags??= TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
?normal->driver_state??? = drv;//指向相應(yīng)的uart_driver
?tty_set_operations(normal, &uart_ops);//將tty_driver結(jié)構(gòu)的normal操作指向uart_ops
?/*void tty_set_operations(struct tty_driver *driver,const struct tty_operations *op)
?{
??driver->ops = op;
?};
?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
?};
?*/
?for (i = 0; i < drv->nr; i++) {//根據(jù)串口數(shù)量依次掃描drv->nr=3
??struct uart_state *state = drv->state + i;//找到每個串口的uart_state地址
??struct tty_port *port = &state->port;//每個串口有一個tty_port結(jié)構(gòu)
??
??//初始化每個串口的tty_port結(jié)構(gòu)
??tty_port_init(port);
??port->ops = &uart_port_ops;
??port->close_delay???? = 500;?/* .5 seconds */
??port->closing_wait??? = 30000;?/* 30 seconds */
??tasklet_init(&state->tlet, uart_tasklet_action,(unsigned long)state);
?}
?
?//注冊tty驅(qū)動
?retval = tty_register_driver(normal);
?if (retval >= 0)
??return retval;
?put_tty_driver(normal);
out_kfree:
?kfree(drv->state);
out:
?return -ENOMEM;
}
//初始化串口的uart_port結(jié)構(gòu)
static void __init serial8250_register_ports(struct uart_driver *drv, struct device *dev)
{
?int i;
?
?//對uart_8250_port結(jié)構(gòu)的serial8250_ports進(jìn)行初始化
?for (i = 0; i < nr_uarts; i++) {//共3個串口
??struct uart_8250_port *up = &serial8250_ports[i];
??up->cur_iotype = 0xFF;//先初始化為0xFF,在set_io_from_upio()會賦值成uart_port->iotype(即UPIO_MEM=2)
?}
?
?//對uart_8250_port結(jié)構(gòu)的serial8250_ports[]接著進(jìn)行初始化,主要是設(shè)置uart_port字段ops的操作
?//由于此函數(shù)在初始化console時被調(diào)用過,所以現(xiàn)在調(diào)用的話會直接返回。即串口的uart_port的ops字段已經(jīng)賦過值了
?serial8250_isa_init_ports();
?for (i = 0; i < nr_uarts; i++) {//共3個串口
??struct uart_8250_port *up = &serial8250_ports[i];
??up->port.dev = dev;//指向相應(yīng)的struct device結(jié)構(gòu)
??if (up->port.flags & UPF_FIXED_TYPE)//因為未進(jìn)行serial8250_probe(),所以此標(biāo)志未設(shè)置,不進(jìn)入下邊函數(shù)
???serial8250_init_fixed_type_port(up, up->port.type);//設(shè)置串口type=PORT_AR7,即18
??
??//向設(shè)備添加端口,是在uart_driver增加一個port,在未進(jìn)行serial8250_probe()之前,這個函數(shù)的串口配置操作會失敗
??//進(jìn)行過serial8250_probe()后,還會再調(diào)用此函數(shù),那時就可以配置好串口。
??uart_add_one_port(drv, &up->port);
?}
}
static void __init serial8250_isa_init_ports(void)
{
?struct uart_8250_port *up;
?static int first = 1;
?int i, irqflag = 0;
?if (!first)//靜態(tài)變量,serial8250_console_init()第一次進(jìn)入這個函數(shù),之后serial8250_init()再進(jìn)入這個函數(shù)就會直接返回
??return;
?first = 0;
?
?//對三個串口的uart_8250_port結(jié)構(gòu)serial8250_ports結(jié)構(gòu)體進(jìn)行初始化
?for (i = 0; i < nr_uarts; i++) {
??struct uart_8250_port *up = &serial8250_ports[i];
??up->port.line = i;//0代表串口0,1代表串口1
??spin_lock_init(&up->port.lock);
??init_timer(&up->timer);//初始化定時器
??up->timer.function = serial8250_timeout;//初始化定時器的超時函數(shù)
??//ALPHA_KLUDGE_MCR needs to be killed.
??up->mcr_mask = ~ALPHA_KLUDGE_MCR;
??up->mcr_force = ALPHA_KLUDGE_MCR;
??
??//初始化uart_8250_port指向的uart_port字段port的操作
??up->port.ops = &serial8250_pops;
??/*
??static struct uart_ops serial8250_pops = {
???.tx_empty?= serial8250_tx_empty,
???.set_mctrl?= serial8250_set_mctrl,
???.get_mctrl?= serial8250_get_mctrl,
???.stop_tx?= serial8250_stop_tx,
???.start_tx?= serial8250_start_tx,
???.stop_rx?= serial8250_stop_rx,
???.enable_ms?= serial8250_enable_ms,
???.break_ctl?= serial8250_break_ctl,
???.startup?= serial8250_startup,
???.shutdown?= serial8250_shutdown,
???.set_termios?= serial8250_set_termios,
???.set_ldisc?= serial8250_set_ldisc,
???.pm??= serial8250_pm,
???.type??= serial8250_type,
???.release_port?= serial8250_release_port,
???.request_port?= serial8250_request_port,
???.config_port?= serial8250_config_port,
???.verify_port?= serial8250_verify_port,
??#ifdef CONFIG_CONSOLE_POLL
???.poll_get_char = serial8250_get_poll_char,
???.poll_put_char = serial8250_put_poll_char,
??#endif
??};
??*/
?}
?if (share_irqs)//中斷是否共享(這里設(shè)置成不共享)
??irqflag = IRQF_SHARED;
?
?//條件不滿足,不會進(jìn)來初始化
?for (i = 0, up = serial8250_ports;i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;i++, up++) {
/*?up->port.iobase?? = old_serial_port[i].port;
??up->port.irq????? = irq_canonicalize(old_serial_port[i].irq);
??up->port.irqflags = old_serial_port[i].irqflags;
??up->port.uartclk? = old_serial_port[i].baud_base * 16;
??up->port.flags??? = old_serial_port[i].flags;
??up->port.hub6???? = old_serial_port[i].hub6;
??up->port.membase? = old_serial_port[i].iomem_base;
??up->port.iotype?? = old_serial_port[i].io_type;
??up->port.regshift = old_serial_port[i].iomem_reg_shift;
??set_io_from_upio(&up->port);
??up->port.irqflags |= irqflag;
??if (serial8250_isa_config != NULL)
???serial8250_isa_config(i, &up->port, &up->capabilities);
*/
?}
}
int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
{
?struct uart_state *state;
?struct tty_port *port;
?int ret = 0;
?struct device *tty_dev;
?BUG_ON(in_interrupt());//不能在中斷中調(diào)用
?
?//Uart_port->line就是對uart設(shè)備文件序號.它對應(yīng)的也就是uart_driver->state數(shù)組中的uart_port->line項.
?if (uport->line >= drv->nr)//在serial8250_isa_init_ports()已經(jīng)初始化過,代表端口號,0代表串口0。
??return -EINVAL;
?
?//根據(jù)串口號找到每個串口對應(yīng)的uart_state結(jié)構(gòu)
?state = drv->state + uport->line;
?port = &state->port;//通過uart_state結(jié)構(gòu)找到每個串口的tty_port結(jié)構(gòu)
?mutex_lock(&port_mutex);
?mutex_lock(&port->mutex);
?if (state->uart_port) {
??ret = -EINVAL;
??goto out;
?}
?
?//將uart_state和uart_port結(jié)構(gòu)相關(guān)聯(lián)起來
?state->uart_port = uport;?
?state->pm_state = -1;
?uport->cons = drv->cons;//將uart_driver的serial8250_console成員賦值給uart_port成員
?uport->state = state;//uart_port的state成員指向相應(yīng)的uart_state
? //If this port is a console, then the spinlock is already initialised.
? //檢查這個串口是否就是終端,并且此終端是否已經(jīng)注冊完畢
?if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
??spin_lock_init(&uport->lock);
??lockdep_set_class(&uport->lock, &port_lock_key);
?}
?
?//進(jìn)行port的自動配置,在未進(jìn)行serial8250_probe(),串口的port->iobase、port->mapbase、port->membase都為空,所以函數(shù)進(jìn)去會立即返回,即未配置成功。
?//當(dāng)進(jìn)行完serial8250_probe()函數(shù)時,還會調(diào)用uart_add_one_port(),再到這個函數(shù)配置時就會配置成功
?uart_configure_port(drv, state, uport);
? //然后注冊tty_device.如果用戶空間運(yùn)行了udev或者已經(jīng)配置好了hotplug.就會在/dev下自動生成設(shè)備文件了.
?tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
?if (likely(!IS_ERR(tty_dev))) {//設(shè)置設(shè)備的喚醒狀態(tài)
??device_init_wakeup(tty_dev, 1);
??device_set_wakeup_enable(tty_dev, 0);
?} else
??printk(KERN_ERR "Cannot register tty device on line %d\n",uport->line);
? //Ensure UPF_DEAD is not set.
?uport->flags &= ~UPF_DEAD;
?out:
?mutex_unlock(&port->mutex);
?mutex_unlock(&port_mutex);
?return ret;
}
static void uart_configure_port(struct uart_driver *drv, struct uart_state *state,struct uart_port *port)
{
?unsigned int flags;
?if (!port->iobase && !port->mapbase && !port->membase)//未調(diào)用serial8250_probe()之前,會從這里直接返回。
??return;
?
?//調(diào)用serial8250_probe()之后,會接著往下進(jìn)行
?flags = 0;
?if (port->flags & UPF_AUTO_IRQ)//未設(shè)置此標(biāo)志
??flags |= UART_CONFIG_IRQ;
??
?if (port->flags & UPF_BOOT_AUTOCONF) {//經(jīng)過probe()函數(shù),此標(biāo)志已配置
??if (!(port->flags & UPF_FIXED_TYPE)) {//已設(shè)置該標(biāo)志,下邊的不會進(jìn)入
???port->type = PORT_UNKNOWN;//不會進(jìn)入
???flags |= UART_CONFIG_TYPE;
??}
??port->ops->config_port(port, flags);//調(diào)用設(shè)備的自動配置函數(shù),即serial8250_config_port()
?}
?if (port->type != PORT_UNKNOWN) {
??unsigned long flags;
??
??//打印串口的信息
??uart_report_port(drv, port);
??/* Power up port for set_mctrl() */
??uart_change_pm(state, 0);改變端口的電源狀態(tài),上電
??spin_lock_irqsave(&port->lock, flags);
??port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);設(shè)置串口modem控制,調(diào)用serial8250_set_mctrl()
??spin_unlock_irqrestore(&port->lock, flags);
?? //注冊終端,配置終端的信息,若此端口有cons字段,并且console的注冊不成功。注冊成功CON_ENABLED標(biāo)志為1
??if (port->cons && !(port->cons->flags & CON_ENABLED))
??{
???/*serial8250_console_init()函數(shù)會比serial8250_probe()先調(diào)用,所以調(diào)用register_console的時候,port還沒有初始化,所以當(dāng)
????register_console調(diào)用serial8250_console_setup()設(shè)置buad,parity bits的時候,
????serial8250_console_setup()會檢測port->iobase和port->membase是否是有效值,如果不是就返回,
????放棄初始化console,所以實際上,console不是在serial8250_console_init()里邊初始化。
????
????當(dāng)serial8250_probe()調(diào)用uart_add_one_port->uart_configure_port:又會調(diào)用register_console(),
????在這里會將真正的console注冊掉。
????該函數(shù)會檢查console有沒有初始化,如果沒有初始化,則調(diào)用register_console來初始化.
????所以console放在這里初始化也是比較好一些.
???*/
???register_console(port->cons);//將該console注冊到console_drivers鏈表上,最后調(diào)用release_console_sem,將printk緩沖的數(shù)據(jù)打印到ttyS2上
??}
??//檢查此串口是否是終端,除了我們使用作為console的串口,其余的進(jìn)行斷電
??if (!uart_console(port))//#define uart_console(port)?((port)->cons && (port)->cons->index == (port)->line)
???uart_change_pm(state, 3);//除了我們使用的console,其余的進(jìn)行斷電
?}
}
//注冊設(shè)備serial8250_isa_driver時,會調(diào)用此函數(shù)
static int __devinit serial8250_probe(struct platform_device *dev)
{
?//傳入的參數(shù)就是da8xx_serial_device
?/*struct platform_device da8xx_serial_device = {//platform_device就描述了設(shè)備對象。
??.name?= "serial8250",
??.id?= PLAT8250_DEV_PLATFORM,
??.dev?= {
???.platform_data?= da8xx_serial_pdata,//這個platform_device對象的私有數(shù)據(jù)指成員向一個plat_serial8250_port類型的數(shù)組。在這里該數(shù)組描述了三個串口接口的基本信息。
?????????????????????//當(dāng)8250驅(qū)動檢測到這個platform_device對象后,就分析該對象的私有數(shù)據(jù)成員指向的那個plat_serial8250_port類型的數(shù)組。
?????????????????????//然后根據(jù)該數(shù)組的每個成員描述的信息生成一個串口對象設(shè)備。
??},
?};
?
?static struct plat_serial8250_port da8xx_serial_pdata[] = {
??{
???.mapbase?= DA8XX_UART0_BASE,//串口接口寄存器物理地址的基地址,#define DAVINCI_UART0_BASE?(IO_PHYS + 0x20000)(見/arch/arm/mach-davinci/include/mach/serial.h)
???.irq??= IRQ_DA8XX_UARTINT0,//該串口接口使用的中斷號, #define IRQ_DA8XX_UARTINT0? 25,(在/arch/arm/mach-davinci/include/mach/irqs.h)
???.flags??= UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_IOREMAP | UPF_FIXED_TYPE,
???.type??= PORT_AR7,//串口0類型
???.iotype??= UPIO_MEM,//成員表示該串口接口寄存器的地址類型,8位的內(nèi)存地址?
???.regshift?= 2,//在訪問該串口接口的某個寄存器時,需把該寄存器的號左移多少位然后加基地址(不管是物理或虛擬地址)才能得能到這個寄存器的址址
??},
??{
???.mapbase?= DA8XX_UART1_BASE, //DAVINCI_UART1_BASE?(IO_PHYS + 0x20400)
???.irq??= IRQ_DA8XX_UARTINT1, //中斷號是53
???.flags??= UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |UPF_IOREMAP | UPF_FIXED_TYPE,
???.type??= PORT_AR7,
???.iotype??= UPIO_MEM,
???.regshift?= 2,
??},
??{
???.mapbase?= DA8XX_UART2_BASE, //#define DAVINCI_UART2_BASE?(IO_PHYS + 0x20800)
???.irq??= IRQ_DA8XX_UARTINT2, //中斷號是61
???.flags??= UPF_BOOT_AUTOCONF | UPF_SKIP_TEST |UPF_IOREMAP | UPF_FIXED_TYPE,
???.type??= PORT_AR7,
???.iotype??= UPIO_MEM,
???.regshift?= 2,
??},
??{
???.flags?= 0,
??},
?};*/
?struct plat_serial8250_port *p = dev->dev.platform_data;
?struct uart_port port;
?int ret, i, irqflag = 0;
?memset(&port, 0, sizeof(struct uart_port));
?if (share_irqs)
??irqflag = IRQF_SHARED;
??
?// 會 將 dev->dev.platform_data 所代表的 port 添加到 uart_driver 中
?for (i = 0; p && p->flags != 0; p++, i++) {//遍歷三個串口
??port.iobase??= p->iobase;
??port.membase??= p->membase;
??port.irq??= p->irq;//中斷號
??port.irqflags??= p->irqflags;
??port.uartclk??= p->uartclk;//時鐘
??port.regshift??= p->regshift;//寄存器偏移量 2
??port.iotype??= p->iotype;//IO類型
??port.flags??= p->flags;//標(biāo)志
??port.mapbase??= p->mapbase;//串口接口寄存器物理地址的基地址
??port.hub6??= p->hub6;
??port.private_data?= p->private_data;
??port.type??= p->type;//PORT_AR7,串口類型
??port.serial_in??= p->serial_in;
??port.serial_out??= p->serial_out;
??port.set_termios?= p->set_termios;
??port.pm???= p->pm;
??port.dev??= &dev->dev;
??port.irqflags??|= irqflag;
??if (p->clk)
???serial8250_ports[i].clk = p->clk;
??
??//再重新注冊串口
??ret = serial8250_register_port(&port);
??if (ret < 0) {
???dev_err(&dev->dev, "unable to register port at index %d ""(IO%lx MEM%llx IRQ%d): %d\n", i,p->iobase, (unsigned long long)p->mapbase,p->irq, ret);
??}
?}
?return 0;//到這里串口的初始化就結(jié)束!!!!
}
int serial8250_register_port(struct uart_port *port)
{
?struct uart_8250_port *uart;
?int ret = -ENOSPC;
?if (port->uartclk == 0)
??return -EINVAL;
?mutex_lock(&serial_mutex);
?
?//查找在serial8250_ports[]數(shù)組中是否已有記錄
?uart = serial8250_find_match_or_unused(port);
?if (uart) {//都會查到有記錄
??uart_remove_one_port(&serial8250_reg, &uart->port);//把原來的串口移除掉,再重新添加
??
??//串口的uart_port再賦值
??uart->port.iobase?????? = port->iobase;
??uart->port.membase????? = port->membase;
??uart->port.irq????????? = port->irq;
??uart->port.irqflags???? = port->irqflags;
??uart->port.uartclk????? = port->uartclk;
??uart->port.fifosize???? = port->fifosize;
??uart->port.regshift???? = port->regshift;
??uart->port.iotype?????? = port->iotype;
??uart->port.flags??????? = port->flags | UPF_BOOT_AUTOCONF;
??uart->port.mapbase????? = port->mapbase;
??uart->port.private_data = port->private_data;
??if (port->dev)
???uart->port.dev = port->dev;
??if (port->flags & UPF_FIXED_TYPE)//probe()進(jìn)行時,此標(biāo)志已設(shè)置
???serial8250_init_fixed_type_port(uart, port->type);//設(shè)置串口type=PORT_AR7,即18
??set_io_from_upio(&uart->port);//設(shè)置串口的讀寫函數(shù)
??
??//如果傳進(jìn)來的參數(shù)此成員有值,用原來的。實際時原來此成員變量為空,da8xx_serial_pdata變量中未賦值
??if (port->serial_in)
???uart->port.serial_in = port->serial_in;
??if (port->serial_out)
???uart->port.serial_out = port->serial_out;
??if (port->set_termios)
???uart->port.set_termios = port->set_termios;
??if (port->pm)
???uart->port.pm = port->pm;
??if (serial8250_isa_config != NULL)
???serial8250_isa_config(0, &uart->port,&uart->capabilities);
??
??//再重新添加串口,這時配置串口就能成功
??ret = uart_add_one_port(&serial8250_reg, &uart->port);
??if (ret == 0)
???ret = uart->port.line;
??ret = serial8250_cpufreq_register(uart);
??if (ret < 0)
???printk(KERN_ERR "Failed to add cpufreq notifier\n");
?}
?mutex_unlock(&serial_mutex);
?return ret;
}
static void serial8250_config_port(struct uart_port *port, int flags)
{
?struct uart_8250_port *up = (struct uart_8250_port *)port;
?int probeflags = PROBE_ANY;
?int ret;
?if (cpu_is_davinci_da850())//是否為達(dá)芬奇平臺
??up->bugs |= UART_BUG_NOMSR;
?ret = serial8250_request_std_resource(up);//分配內(nèi)存資源,IO資源
?if (ret < 0)
??return;
?ret = serial8250_request_rsa_resource(up);//會返回失敗
?if (ret < 0)
??probeflags &= ~PROBE_RSA;//清除PROBE_RSA標(biāo)志
?
?//up->cur_iotype在serial8250_register_ports()設(shè)置成0xFF,
?//而up->port.iotype在serial8250_isa_init_ports()被設(shè)置為UPIO_MEM,即2
?if (up->port.iotype != up->cur_iotype)
??set_io_from_upio(port);//設(shè)置uart_port結(jié)構(gòu)的serial_in函數(shù)和serial_out函數(shù)
?if (flags & UART_CONFIG_TYPE)//此標(biāo)志沒有被設(shè)置
??autoconfig(up, probeflags);//不會調(diào)用
?if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
??up->bugs |= UART_BUG_NOMSR;
?if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)//此標(biāo)志UART_CONFIG_IRQ未設(shè)置
??autoconfig_irq(up);//不會調(diào)用
?
?//以下不會調(diào)用
?if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
??serial8250_release_rsa_resource(up);
?if (up->port.type == PORT_UNKNOWN)
??serial8250_release_std_resource(up);
}
static int serial8250_request_std_resource(struct uart_8250_port *up)
{
?unsigned int size = serial8250_port_size(up);
?int ret = 0;
?switch (up->port.iotype) {//up->port.iotype == UPIO_MEM
?case UPIO_AU:
?case UPIO_TSI:
?case UPIO_MEM32:
?case UPIO_MEM:
?case UPIO_DWAPB:
??if (!up->port.mapbase)
???break;
??if (!request_mem_region(up->port.mapbase, size, "serial")) {
???ret = -EBUSY;
???break;
??}
??if (up->port.flags & UPF_IOREMAP) {//如果前邊映射過了,UPF_IOREMAP被清零了,這里就不需再映射了
???up->port.membase = ioremap_nocache(up->port.mapbase,size);
???if (!up->port.membase) {
????release_mem_region(up->port.mapbase, size);
????ret = -ENOMEM;
???}
??}
??break;
?case UPIO_HUB6:
?case UPIO_PORT:
??if (!request_region(up->port.iobase, size, "serial"))
???ret = -EBUSY;
??break;
?}
?return ret;
}
static void set_io_from_upio(struct uart_port *p)
{
?struct uart_8250_port *up = (struct uart_8250_port *)p;
?switch (p->iotype) {
?case UPIO_HUB6:
??p->serial_in = hub6_serial_in;
??p->serial_out = hub6_serial_out;
??break;
?case UPIO_MEM://p->iotype=2,采用這里的讀寫函數(shù)
??p->serial_in = mem_serial_in;
??p->serial_out = mem_serial_out;
??break;
?case UPIO_RM9000:
?case UPIO_MEM32:
??p->serial_in = mem32_serial_in;
??p->serial_out = mem32_serial_out;
??break;
?case UPIO_AU:
??p->serial_in = au_serial_in;
??p->serial_out = au_serial_out;
??break;
?case UPIO_TSI:
??p->serial_in = tsi_serial_in;
??p->serial_out = tsi_serial_out;
??break;
?case UPIO_DWAPB:
??p->serial_in = mem_serial_in;
??p->serial_out = dwapb_serial_out;
??break;
?default:
??p->serial_in = io_serial_in;
??p->serial_out = io_serial_out;
??break;
?}
?/* Remember loaded iotype */
?up->cur_iotype = p->iotype;//注意要賦值
}
static inline void uart_report_port(struct uart_driver *drv, struct uart_port *port)
{
?char address[64];
?switch (port->iotype) {//port->iotype == UPIO_MEM
?case UPIO_PORT:
??snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
??break;
?case UPIO_HUB6:
??snprintf(address, sizeof(address),"I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
??break;
?case UPIO_MEM:
?case UPIO_MEM32:
?case UPIO_AU:
?case UPIO_TSI:
?case UPIO_DWAPB:
??snprintf(address, sizeof(address), "MMIO 0x%llx", (unsigned long long)port->mapbase);
??break;
?default:
??strlcpy(address, "*unknown*", sizeof(address));
??break;
?}
?/*打印出的信息如下:
?serial8250.0: ttyS0 at MMIO 0x1c42000 (irq = 25) is a AR7
?serial8250.0: ttyS1 at MMIO 0x1d0c000 (irq = 53) is a AR7
?serial8250.0: ttyS2 at MMIO 0x1d0d000 (irq = 61) is a AR7
?*/
?printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
??????? port->dev ? dev_name(port->dev) : "",
??????? port->dev ? ": " : "",
??????? drv->dev_name,
??????? drv->tty_driver->name_base + port->line,
??????? address, port->irq, uart_type(port));
}
//下邊再次調(diào)用register_console()注冊serial8250_console真正的console終端
void register_console(struct console *newcon)
{
?int i;
?unsigned long flags;
?struct console *bcon = NULL;
?/*
?現(xiàn)在是注冊一個serial8250_console,即
?static struct console serial8250_console = {
??.name??= "ttyS",
??.write??= serial8250_console_write,//寫方法
??.device??= uart_console_device,//tty驅(qū)動
??.setup??= serial8250_console_setup,//設(shè)置串口波特率,也就是設(shè)置串口。很重要,里面涉及到平臺特性,波特率相關(guān)。
??.early_setup?= serial8250_console_early_setup,
??.flags??= CON_PRINTBUFFER | CON_ANYTIME,
??.index??= -1,
??.data??= &serial8250_reg,
?};
?*/
?if (console_drivers && newcon->flags & CON_BOOT) {//注冊的是serial8250_console,CON_BOOT沒有置位,不是引導(dǎo)控制臺。下邊不會進(jìn)去遍歷
??for_each_console(bcon) {遍歷全局console_drivers數(shù)組???
???if (!(bcon->flags & CON_BOOT)) {//判斷是否已經(jīng)有引導(dǎo)控制臺了,有了的話就直接退出
????printk(KERN_INFO "Too late to register bootconsole %s%d\n",newcon->name, newcon->index);
????return;
???}
??}
?}
?
?if (console_drivers && console_drivers->flags & CON_BOOT)//如果注冊的是引導(dǎo)控制臺,serial8250_console不是引導(dǎo)控制臺
??bcon = console_drivers;//這里不執(zhí)行
?if (preferred_console < 0 || bcon || !console_drivers)
??preferred_console = selected_console;//設(shè)置preferred_console為uboot命令選擇的selected_console(即在Uboot傳入的參數(shù)“console=ttyS2,115200n8”在console_cmdline[]數(shù)組中的索引)???
???????????????????? //這里preferred_console =0
?if (newcon->early_setup)//serial8250_console初始化early_setup字段
??newcon->early_setup();//調(diào)用serial8250_console_early_setup()
?if (preferred_console < 0) {//由于preferred_console =0,不會進(jìn)入下邊
??if (newcon->index < 0)
???newcon->index = 0;
??if (newcon->setup == NULL ||newcon->setup(newcon, NULL) == 0) {
???newcon->flags |= CON_ENABLED;
???if (newcon->device) {
????newcon->flags |= CON_CONSDEV;
????preferred_console = 0;
???}
??}
?}
? //傳給內(nèi)核參數(shù):
? //Kernel command line: console=ttyS2,115200n8 rw root=/dev/ram0 initrd=0xc2000000,20M mem=128M ip=192.168.1.220::192.168.1.1:255.255.255.0::eth0:off
? //所以這里將根據(jù)傳參console=ttyS2,115200來配置作為console的ttyS2串口
?for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];i++) {//遍歷全局console_cmdline找到匹配的,i=0就是匹配的“ttyS2”
??if (strcmp(console_cmdline[i].name, newcon->name) != 0)//比較終端名稱“ttyS”
???continue;
??if (newcon->index >= 0 &&newcon->index != console_cmdline[i].index)//console_cmdline[i].index=2。//比較次設(shè)備號??
???continue;
??if (newcon->index < 0)
???newcon->index = console_cmdline[i].index;//將終端號賦值給serial8250_console->index,這里是2
???
??//console_cmdline[i].options = "115200n8",對于serial8250_console而言setup字段已初始化
??if (newcon->setup && newcon->setup(newcon, console_cmdline[i].options) != 0)//調(diào)用serial8250_console_setup()對終端進(jìn)行配置,未probe()前調(diào)用不成功,probe()后調(diào)用成功。
???break;
??//在這里注冊serial8250_console時,調(diào)用serial8250_console_setup()由于port->iobase和port->membase不是有效值,
??//故返回錯誤,這樣下邊的操作不會執(zhí)行,直接break跳出,從flag1出跳出函數(shù)。即在這里serial8250_console沒有注冊成功
??//由于內(nèi)核在下邊的操作隊串口進(jìn)行初始化時,還會調(diào)用register_console()來注冊serial8250_console,在那時注冊就會成功
??
??newcon->flags |= CON_ENABLED; //設(shè)置標(biāo)志為CON_ENABLE,表示console使能(這個在printk調(diào)用中使用到)?
??newcon->index = console_cmdline[i].index;//設(shè)置索引號???
??if (i == selected_console) { //索引號和uboot指定的console的一樣?
???newcon->flags |= CON_CONSDEV;//設(shè)置標(biāo)志CON_CONSDEV(全局console_drivers鏈表中靠前)?
???preferred_console = selected_console;
??}
??break;
?}//for循環(huán)作用大致是查看注冊的console是否是uboot知道的引導(dǎo)console,是則設(shè)置相關(guān)標(biāo)志和preferred_console
? //flag1:
?if (!(newcon->flags & CON_ENABLED))//若前邊沒有設(shè)置CON_ENABLED標(biāo)志,就退出。若進(jìn)行過probe(),CON_ENABLED置位,這里就往下接著注冊console
??return;
?if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))//防止重復(fù)打印???
??newcon->flags &= ~CON_PRINTBUFFER;
?acquire_console_sem();
?if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {//如果是preferred控制臺,此標(biāo)志CON_CONSDEV在前邊已設(shè)置過
??newcon->next = console_drivers;
??console_drivers = newcon;//添加進(jìn)全局console_drivers鏈表前面位置(printk中會遍歷該表調(diào)用合適的console的write方法打印信息)
??if (newcon->next)
???newcon->next->flags &= ~CON_CONSDEV;
?} else {//如果不是preferred控制臺?
??newcon->next = console_drivers->next;
??console_drivers->next = newcon; //添加進(jìn)全局console_drivers鏈表后面位置
?}
?
?//主冊console主要是刷選preferred_console放置在全局console_drivers鏈表前面,剩下的console放置鏈表靠后的位置,并設(shè)置相應(yīng)的flags,
?//console_drivers最終會在printk函數(shù)的層層調(diào)用中遍歷到,并調(diào)用console的write方法將信息打印出來
?if (newcon->flags & CON_PRINTBUFFER) {
??spin_lock_irqsave(&logbuf_lock, flags);
??con_start = log_start;
??spin_unlock_irqrestore(&logbuf_lock, flags);
?}
?release_console_sem();
?if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
??printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",newcon->name, newcon->index);
??for_each_console(bcon)
???if (bcon->flags & CON_BOOT)
????unregister_console(bcon);
?} else {//調(diào)用這里
??printk(KERN_INFO "%sconsole [%s%d] enabled\n",(newcon->flags & CON_BOOT) ? "boot" : "" ,newcon->name, newcon->index);
?}
}
//serial8250_console_early_setup()-->serial8250_find_port_for_earlycon()
int serial8250_find_port_for_earlycon(void)
{
?struct early_serial8250_device *device = &early_device;//early console初始化時對early_device結(jié)構(gòu)的初始化
?struct uart_port *port = &device->port;
?int line;
?int ret;
?if (!device->port.membase && !device->port.iobase)//early_device結(jié)構(gòu)初始化時已經(jīng)配置好
??return -ENODEV;
?//early console注冊時不會調(diào)用此函數(shù)。
?//當(dāng)真正的console初始化時,會調(diào)用此函數(shù)。
?//真正的console初始化時,會查找early console注冊時用的是哪一個串口號,從serial8250_ports[]中根據(jù)uart_port->mapbase地址來比對
?line = serial8250_find_port(port);//根據(jù)uart_port結(jié)構(gòu)找到串口號,比對沒有找到串口號,line返回負(fù)值
?if (line < 0)
??return -ENODEV;//從這里返回,下邊的不再執(zhí)行
?
?//若找到early console用的串口號,更新當(dāng)初傳入內(nèi)核參數(shù)使用的console_cmdline[i],名稱改成ttyS。。。。
?ret = update_console_cmdline("uart", 8250, "ttyS", line, device->options);
?if (ret < 0)
??ret = update_console_cmdline("uart", 0,"ttyS", line, device->options);
?return ret;
}
static int __init serial8250_console_setup(struct console *co, char *options)
{
?struct uart_port *port;
?int baud = 9600;
?int bits = 8;
?int parity = 'n';
?int flow = 'n';
?if (co->index >= nr_uarts)//console的索引,這里是2,即ttyS2
??co->index = 0;
?port = &serial8250_ports[co->index].port;//找到對應(yīng)的ttyS2的uart_port結(jié)構(gòu)
?
?//由于console_init在注冊serial8250_console時調(diào)用的register_console()函數(shù)調(diào)用serial8250_console_setup()
?//進(jìn)入這個函數(shù)時,由于ttyS2的uart_port結(jié)構(gòu)沒有初始化,port->iobase 和port->membase值都未設(shè)置,所以直接從下邊返回
?//當(dāng)進(jìn)行串口初始化時,還會回來注冊serial8250_console,再調(diào)用到這里,由于設(shè)置了ttyS2的uart_port結(jié)構(gòu),所以下邊的配置就會成功
?if (!port->iobase && !port->membase)//第一次注冊時,由于未設(shè)置,從這里直接返回
??return -ENODEV;
?if (options)//如果options不為空,就將options里的數(shù)值寫給baud, &parity, &bits, &flow
??uart_parse_options(options, &baud, &parity, &bits, &flow);
?//沒有配置options,則使用缺省值,否則使用傳下來的的參數(shù)options里的串口配置
?return uart_set_options(port, co, baud, parity, bits, flow);
}
void uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
{
?char *s = options;
?*baud = simple_strtoul(s, NULL, 10);
?while (*s >= '0' && *s <= '9')
??s++;
?if (*s)
??*parity = *s++;
?if (*s)
??*bits = *s++ - '0';
?if (*s)
??*flow = *s;
}
int uart_set_options(struct uart_port *port, struct console *co,int baud, int parity, int bits, int flow)
{
?struct ktermios termios;
?static struct ktermios dummy;
?int i;
?//Ensure that the serial console lock is initialised early.
?spin_lock_init(&port->lock);
?lockdep_set_class(&port->lock, &port_lock_key);
?memset(&termios, 0, sizeof(struct ktermios));
?termios.c_cflag = CREAD | HUPCL | CLOCAL;
?//Construct a cflag setting.
?for (i = 0; baud_rates[i].rate; i++)
??if (baud_rates[i].rate <= baud)
???break;
?termios.c_cflag |= baud_rates[i].cflag;
?if (bits == 7)
??termios.c_cflag |= CS7;
?else
??termios.c_cflag |= CS8;
?switch (parity) {
?case 'o': case 'O':
??termios.c_cflag |= PARODD;
??/*fall through*/
?case 'e': case 'E':
??termios.c_cflag |= PARENB;
??break;
?}
?if (flow == 'r')
??termios.c_cflag |= CRTSCTS;
?/*
? * some uarts on other side don't support no flow control.
? * So we set * DTR in host uart to make them happy
? */
?port->mctrl |= TIOCM_DTR;
?port->ops->set_termios(port, &termios, &dummy);//調(diào)用serial8250_set_termios()對串口進(jìn)行配置
?//Allow the setting of the UART parameters with a NULL console too:
?if (co)
??co->cflag = termios.c_cflag;
?return 0;
}
struct device *tty_register_device(struct tty_driver *driver, unsigned index,struct device *device)
{
?char name[64];
?dev_t dev = MKDEV(driver->major, driver->minor_start) + index;//得到設(shè)備號
?if (index >= driver->num) {
??printk(KERN_ERR "Attempt to register invalid tty line number "" (%d).\n", index);
??return ERR_PTR(-EINVAL);
?}
?if (driver->type == TTY_DRIVER_TYPE_PTY)
??pty_line_name(driver, index, name);
?else
??tty_line_name(driver, index, name);//得到串口設(shè)備名稱ttyS0,ttyS1,ttyS2
?
?return device_create(tty_class, device, dev, NULL, name);//在/dev下創(chuàng)建設(shè)備文件
}
?
總結(jié)
以上是生活随笔為你收集整理的linux 串口驱动(二)初始化 【转】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET 5 and .NET C
- 下一篇: openstack 调试