歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> U-Boot的設備管理

U-Boot的設備管理

日期:2017/3/1 10:27:12   编辑:Linux編程

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:第一階段源碼分析 http://www.linuxidc.com/Linux/2012-03/55963.htm

圖解U-Boot:第二階段源碼分析 http://www.linuxidc.com/Linux/2012-03/55964.htm

圖解U-Boot:引導內核分析 http://www.linuxidc.com/Linux/2012-03/55965.htm

一、初始設備鏈表、初始化設備和注冊設備

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. /* relocate device name pointers */
  7. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  8. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  9. relocation_offset);
  10. }
  11. #endif
  12. /* Initialize the list */
  13. devlist = ListCreate (sizeof (device_t));//創建設備列表
  14. if (devlist == NULL) {
  15. eputs ("Cannot initialize the list of devices!\n");
  16. return -1;
  17. }
  18. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  19. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);//初始化i2c接口,i2c沒有注冊到devlist中去
  20. #endif
  21. #ifdef CONFIG_LCD
  22. drv_lcd_init ();
  23. #endif
  24. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  25. drv_video_init ();
  26. #endif
  27. #ifdef CONFIG_KEYBOARD
  28. drv_keyboard_init ();
  29. #endif
  30. #ifdef CONFIG_LOGBUFFER
  31. drv_logbuff_init ();
  32. #endif
  33. drv_system_init ();//這裡其實是定義了一個串口設備,並且注冊到devlist中
  34. #ifdef CONFIG_SERIAL_MULTI
  35. serial_devices_init ();
  36. #endif
  37. #ifdef CONFIG_USB_TTY
  38. drv_usbtty_init ();
  39. #endif
  40. #ifdef CONFIG_NETCONSOLE
  41. drv_nc_init ();
  42. #endif
  43. return (0);
  44. }

經過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. /* GENERAL functions 啟動和停止函數 */
  7. int (*start) (void); /* To start the device */
  8. int (*stop) (void); /* To stop the device */
  9. /* OUTPUT functions 輸出函數 */
  10. void (*putc) (const char c); /* To put a char */
  11. void (*puts) (const char *s); /* To put a string (accelerator) */
  12. /* INPUT functions 輸入函數*/
  13. int (*tstc) (void); /* To test if a char is ready... */
  14. int (*getc) (void); /* To get that char */
  15. /* Other functions */
  16. void *priv; /* Private extensions */
  17. } device_t;

1.3 drv_system_init 函數

drv_system_init 函數初始化串口設備,源碼如下:

  1. static void drv_system_init (void)
  2. {
  3. device_t dev;//定義一個結構體
  4. memset (&dev, 0, sizeof (dev));//為剛剛定義的結構體分配內存
  5. strcpy (dev.name, "serial");//名稱
  6. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  7. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  8. dev.putc = serial_buffered_putc;
  9. dev.puts = serial_buffered_puts;
  10. dev.getc = serial_buffered_getc;
  11. dev.tstc = serial_buffered_tstc;
  12. #else
  13. dev.putc = serial_putc;
  14. dev.puts = serial_puts;
  15. dev.getc = serial_getc;
  16. dev.tstc = serial_tstc;
  17. #endif
  18. device_register (&dev);//注冊函數
  19. #ifdef CFG_DEVICE_NULLDEV
  20. memset (&dev, 0, sizeof (dev));
  21. strcpy (dev.name, "nulldev");
  22. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  23. dev.putc = nulldev_putc;
  24. dev.puts = nulldev_puts;
  25. dev.getc = nulldev_input;
  26. dev.tstc = nulldev_input;
  27. device_register (&dev);
  28. #endif
  29. }
Copyright © Linux教程網 All Rights Reserved