U-Boot的设备管理
U-Boot的设备管理
U-Boot通过devices_init函数创建设备链表,然后在devices_init函数中初始化设备并将设备添加到设备链表中。U-Boot使用devices_t结构体来管理设备,设备链表也就是devices_t结构体的链表。通过i2c_init、drv_lcd_init、drv_video_init、drv_keyboard_init、drv_logbuff_init、drv_system_init、serial_devices_init、drv_usbtty_init和drv_nc_init函数初始化设备(这些函数是否执行是通过宏来决定的),并通过device_register函数注册设备。
相关阅读:
图解U-Boot:第一阶段源码分析
图解U-Boot:第二阶段源码分析
图解U-Boot:引导内核分析
一、初始设备链表、初始化设备和注册设备
U-Boot在第二阶段中通过devices_init函数创建设备链表,初始化设备并将其注册到设备链表中。该函数在common/devices.c文件中,其对应的头文件是nclude/devices.h。
1.1 devices_init函数
- int devices_init (void)
- {
- #ifndef CONFIG_ARM /* already relocated for current ARM implementation */
- ulong relocation_offset = gd->reloc_off;
- int i;
- /* relocate device name pointers */
- for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
- stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
- relocation_offset);
- }
- #endif
- /* Initialize the list */
- devlist = ListCreate (sizeof (device_t));//创建设备列表
- if (devlist == NULL) {
- eputs ("Cannot initialize the list of devices!\n");
- return -1;
- }
- #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
- i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);//初始化i2c接口,i2c没有注册到devlist中去
- #endif
- #ifdef CONFIG_LCD
- drv_lcd_init ();
- #endif
- #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
- drv_video_init ();
- #endif
- #ifdef CONFIG_KEYBOARD
- drv_keyboard_init ();
- #endif
- #ifdef CONFIG_LOGBUFFER
- drv_logbuff_init ();
- #endif
- drv_system_init ();//这里其实是定义了一个串口设备,并且注册到devlist中
- #ifdef CONFIG_SERIAL_MULTI
- serial_devices_init ();
- #endif
- #ifdef CONFIG_USB_TTY
- drv_usbtty_init ();
- #endif
- #ifdef CONFIG_NETCONSOLE
- drv_nc_init ();
- #endif
- return (0);
- }
经过devices_init(),创建了devlist,但是只有一个串口设备注册在内。
1.2 devices结构的定义
- /* Device information */
- typedef struct {
- int flags; /* Device flags: input/output/system */
- int ext; /* Supported extensions */
- char name[16]; /* Device name 设备名称 */
- /* GENERAL functions 启动和停止函数 */
- int (*start) (void); /* To start the device */
- int (*stop) (void); /* To stop the device */
- /* OUTPUT functions 输出函数 */
- void (*putc) (const char c); /* To put a char */
- void (*puts) (const char *s); /* To put a string (accelerator) */
- /* INPUT functions 输入函数*/
- int (*tstc) (void); /* To test if a char is ready... */
- int (*getc) (void); /* To get that char */
- /* Other functions */
- void *priv; /* Private extensions */
- } device_t;
1.3 drv_system_init 函数
drv_system_init 函数初始化串口设备,源码如下:
- static void drv_system_init (void)
- {
- device_t dev;//定义一个结构体
- memset (&dev, 0, sizeof (dev));//为刚刚定义的结构体分配内存
- strcpy (dev.name, "serial");//名称
- dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
- #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
- dev.putc = serial_buffered_putc;
- dev.puts = serial_buffered_puts;
- dev.getc = serial_buffered_getc;
- dev.tstc = serial_buffered_tstc;
- #else
- dev.putc = serial_putc;
- dev.puts = serial_puts;
- dev.getc = serial_getc;
- dev.tstc = serial_tstc;
- #endif
- device_register (&dev);//注册函数
- #ifdef CFG_DEVICE_NULLDEV
- memset (&dev, 0, sizeof (dev));
- strcpy (dev.name, "nulldev");
- dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
- dev.putc = nulldev_putc;
- dev.puts = nulldev_puts;
- dev.getc = nulldev_input;
- dev.tstc = nulldev_input;
- device_register (&dev);
- #endif
- }
|
评论暂时关闭