U-Boot在第二阶段中通过devices_init函数创建设备链表,初始化设备并将其注册到设备链表中。该函数在common/devices.c文件中,其对应的头文件是nclude/devices.h。
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,但是只有一个串口设备注册在内。
/* 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;
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 }
有连个设备注册函数,一个的参数时devces结构体,另一个的参数是设备的名称
设备注册,将结构体添加到devlist的后面,device_register函数的源码如下:
int device_register (device_t * dev) { ListInsertItem (devlist, dev, LIST_END); return 0; }
/*******************************/ /* * returns 1 if the item is inserted, returns 0 if out of memory or * bad arguments were passed. */ int ListInsertItem (list_t list, void *ptrToItem, int itemPosition) { return ListInsertItems (list, ptrToItem, itemPosition, 1); } /*******************************/ int ListInsertItems (list_t list, void *ptrToItems, int firstItemPosition, int numItemsToInsert) { int numItems = (*list)->numItems; if (firstItemPosition == numItems + 1) firstItemPosition = LIST_END; else if (firstItemPosition > numItems) return 0; if ((*list)->numItems >= (*list)->listSize) { if (!ExpandListSpace (list, -numItemsToInsert)) return 0; } if (firstItemPosition == LIST_START) { if (numItems == 0) { /* special case for empty list */ firstItemPosition = LIST_END; } else { firstItemPosition = 1; } } if (firstItemPosition == LIST_END) { /* add at the end of the list */ if (ptrToItems) memcpy (ITEMPTR (list, numItems), ptrToItems, (*list)->itemSize * numItemsToInsert); else memset (ITEMPTR (list, numItems), 0, (*list)->itemSize * numItemsToInsert); (*list)->numItems += numItemsToInsert; } else { /* move part of list up to make room for new item */ memmove (ITEMPTR (list, firstItemPosition - 1 + numItemsToInsert), ITEMPTR (list, firstItemPosition - 1), (numItems + 1 - firstItemPosition) * (*list)->itemSize); if (ptrToItems) memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems, (*list)->itemSize * numItemsToInsert); else memset (ITEMPTR (list, firstItemPosition - 1), 0, (*list)->itemSize * numItemsToInsert); (*list)->numItems += numItemsToInsert; } return 1; }
int device_deregister(char *devname) { int i,l,dev_index; device_t *dev = NULL; char temp_names[3][8]; dev_index=-1; for (i=1; i<=ListNumItems(devlist); i++) { dev = ListGetPtrToItem (devlist, i); if(strcmp(dev->name,devname)==0) { dev_index=i; break; } } if(dev_index<0) /* device not found */ return 0; /* get stdio devices (ListRemoveItem changes the dev list) */ for (l=0 ; l< MAX_FILES; l++) { if (stdio_devices[l] == dev) { /* Device is assigned -> report error */ return -1; } memcpy (&temp_names[l][0], stdio_devices[l]->name, sizeof(stdio_devices[l]->name)); } ListRemoveItem(devlist,NULL,dev_index); /* reassign Device list */ for (i=1; i<=ListNumItems(devlist); i++) { dev = ListGetPtrToItem (devlist, i); for (l=0 ; l< MAX_FILES; l++) { if(strcmp(dev->name,temp_names[l])==0) { stdio_devices[l] = dev; } } } return 0; }
/* search a device */ device_t *search_device (int flags, char *name) { int i, items; device_t *dev = NULL; items = ListNumItems (devlist); if (name == NULL) return dev; for (i = 1; i <= items; i++) { dev = ListGetPtrToItem (devlist, i); if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) { break; } } return dev; }