linux内核中的设备
对于挂接在具体总线上外部设备都有一个struct device与之对应,比如平台设备有platform_device与之对应,其中platform_device结构体如下: struct platform_device {const char * name; //设备名称,要与driver的名字进行匹配,如果成功则进入驱动的探测函数
int id;
struct device dev;
u32 num_resources;
struct resource* resource; //设备的私有资源
const struct platform_device_id*id_entry;
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdataarchdata;
};
对于usb总线上的usb设备用struct usb_device 来代表具体的usb设备或则是记录具体usb的相关信息,其中usb_device的结构体如下: struct usb_device {
char path[SYSFS_PATH_MAX];
char busid[SYSFS_BUS_ID_SIZE];
uint32_t busnum;
uint32_t devnum;
uint32_t speed;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t bDeviceClass;
uint8_t bDeviceSubClass;
uint8_t bDeviceProtocol;
uint8_t bConfigurationValue;
uint8_t bNumConfigurations;
uint8_t bNumInterfaces;
} __attribute__((packed));
linux系统中所代表的外部设备的结构体变量时在系统的启动阶段或则动态注册到相应的总线上去。并且注册到设备到相应的总线是通过调用device_add函数来实现的。所谓的注册设备总线是将设备加载总线代表设备的链表中区。 struct device { struct klist_node knode_bus; struct klist_node knode_driver; struct bus_type *bus; struct device_driver *driver; };
struct klist_node knode_bus; linux 内核通过该成员device加载他所在的链表设备的链表成,即是把device 加载到klist_devices链表上去。
struct klist_node knode_driver; 即是把driver加载到klist_drivers中去
struct bus_type *bus; 表示设备所挂载 的总线 struct device_driver *driver; 表示设备所对应的设备驱动
添加平台设备到平台总线的相关函数以及周边函数 extern int platform_device_register(struct platform_device *);
extern void platform_device_unregister(struct platform_device *);
extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
extern int platform_get_irq(struct platform_device *, unsigned int);
extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, const char *);
extern int platform_get_irq_byname(struct platform_device *, const char *);
extern int platform_add_devices(struct platform_device **, int);