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函数

  1. int devices_init (void)  
  2. {  
  3. #ifndef CONFIG_ARM     /* already relocated for current ARM implementation */  
  4.     ulong relocation_offset = gd->reloc_off;  
  5.     int i;  
  6.   
  7.     /* relocate device name pointers */  
  8.     for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {  
  9.         stdio_names[i] = (char *) (((ulong) stdio_names[i]) +  
  10.                         relocation_offset);  
  11.     }  
  12. #endif  
  13.   
  14.     /* Initialize the list */  
  15.     devlist = ListCreate (sizeof (device_t));//创建设备列表  
  16.   
  17.     if (devlist == NULL) {  
  18.         eputs ("Cannot initialize the list of devices!\n");  
  19.         return -1;  
  20.     }  
  21. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)  
  22.     i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);//初始化i2c接口,i2c没有注册到devlist中去  
  23. #endif  
  24. #ifdef CONFIG_LCD  
  25.     drv_lcd_init ();  
  26. #endif  
  27. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)  
  28.     drv_video_init ();  
  29. #endif  
  30. #ifdef CONFIG_KEYBOARD  
  31.     drv_keyboard_init ();  
  32. #endif  
  33. #ifdef CONFIG_LOGBUFFER  
  34.     drv_logbuff_init ();  
  35. #endif  
  36.     drv_system_init ();//这里其实是定义了一个串口设备,并且注册到devlist中  
  37. #ifdef CONFIG_SERIAL_MULTI  
  38.     serial_devices_init ();  
  39. #endif  
  40. #ifdef CONFIG_USB_TTY  
  41.     drv_usbtty_init ();  
  42. #endif  
  43. #ifdef CONFIG_NETCONSOLE  
  44.     drv_nc_init ();  
  45. #endif  
  46.   
  47.     return (0);  
  48. }  

经过devices_init(),创建了devlist,但是只有一个串口设备注册在内。

1.2 devices结构的定义

  1. /* Device information */  
  2. typedef struct {  
  3.     int flags;          /* Device flags: input/output/system    */  
  4.     int ext;            /* Supported extensions         */  
  5.     char    name[16];       /* Device name  设备名称            */  
  6.   
  7. /* GENERAL functions 启动和停止函数 */  
  8.   
  9.     int (*start) (void);        /* To start the device          */  
  10.     int (*stop) (void);     /* To stop the device           */  
  11.   
  12. /* OUTPUT functions 输出函数 */  
  13.   
  14.     void (*putc) (const char c);    /* To put a char            */  
  15.     void (*puts) (const char *s);   /* To put a string (accelerator)    */  
  16.   
  17. /* INPUT functions  输入函数*/  
  18.   
  19.     int (*tstc) (void);     /* To test if a char is ready...    */  
  20.     int (*getc) (void);     /* To get that char         */  
  21.   
  22. /* Other functions */  
  23.   
  24.     void *priv;         /* Private extensions           */  
  25. } device_t;  

1.3 drv_system_init 函数

drv_system_init 函数初始化串口设备,源码如下:

  1. static void drv_system_init (void)  
  2. {  
  3.     device_t dev;//定义一个结构体  
  4.   
  5.     memset (&dev, 0, sizeof (dev));//为刚刚定义的结构体分配内存  
  6.   
  7.     strcpy (dev.name, "serial");//名称  
  8.     dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;   
  9.   
  10. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO  
  11.     dev.putc = serial_buffered_putc;  
  12.     dev.puts = serial_buffered_puts;  
  13.     dev.getc = serial_buffered_getc;  
  14.     dev.tstc = serial_buffered_tstc;  
  15. #else  
  16.     dev.putc = serial_putc;  
  17.     dev.puts = serial_puts;  
  18.     dev.getc = serial_getc;  
  19.     dev.tstc = serial_tstc;  
  20. #endif  
  21.   
  22.     device_register (&dev);//注册函数  
  23.   
  24. #ifdef CFG_DEVICE_NULLDEV  
  25.     memset (&dev, 0, sizeof (dev));  
  26.   
  27.     strcpy (dev.name, "nulldev");  
  28.     dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;  
  29.     dev.putc = nulldev_putc;  
  30.     dev.puts = nulldev_puts;  
  31.     dev.getc = nulldev_input;  
  32.     dev.tstc = nulldev_input;  
  33.   
  34.     device_register (&dev);  
  35. #endif  
  36. }  
  • 1
  • 2
  • 下一页

相关内容