平台总线(Platform bus)是Linux2.6内核加入的一种虚拟总线,其优势在于采用了总线的模型对设备与驱动进行了管理,这样提高了程序可移植性。
用户不用自己创建虚拟总线,可直接调用此总线使用。
通过平台总线机制开发设备驱动流程图:
平台设备匹配函数:
/**
* platform_match - bind platform device to platform driver.
* @dev: device.
* @drv: driver.
*
* Platform device IDs are assumed to be encoded like this:
* "<name><instance>", where <name> is a short description of the type of
* device, like "pci" or "floppy", and <instance> is the enumerated
* instance of the device, like '0' or '42'. Driver IDs are simply
* "<name>". So, extract the <name> from the platform_device structure,
* and compare it against the name of the driver. Return whether they match
* or not.
*/
static int platform_match(struct device *dev, struct device_driver *drv)
{
struct platform_device *pdev;
pdev = container_of(dev, struct platform_device, dev);
return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
}
平台设备使用struct platform_device来描述:
struct platform_device {
const char * name;<span style="white-space:pre"> </span>设备名
int id;<span style="white-space:pre"> </span>设备编号,配合设备名使用
struct device dev;
u32 num_resources;
struct resource * resource;<span style="white-space:pre"> </span>设备资源(基地址、中断号、DMA...)
};
struct resource *resource结构体:
struct resource {
resource_size_t start;<span style="white-space:pre"> </span>起始地址(寄存器、中断号)
resource_size_t end;
const char *name;
unsigned long flags;<span style="white-space:pre"> </span>资源的类型(中断号、内存...)
struct resource *parent, *sibling, *child;
};
int platform_device_register(struct platform_device *pdev)
void platform_device_unregister(struct platform_device *pdev)
struct platform_driver {
int (*probe)(struct platform_device *);<span style="white-space:pre"> </span>//平台设备匹配上后调用此函数,实现此函数时必须加上返回值,不然会报错</span>
int (*remove)(struct platform_device *);<span style="white-space:pre"> </span>//平台设备从总线移出后调用此函数
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*suspend_late)(struct platform_device *, pm_message_t state);
int (*resume_early)(struct platform_device *);
int (*resume)(struct platform_device *);
struct pm_ext_ops *pm;
struct device_driver driver;
};
平台驱动注册函数:
int platform_driver_register(struct platform_driver *drv)
平台设备注销函数:
void platform_driver_unregister(struct platform_driver *drv)
平台设备获取资源函数:
struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num)
*dev:设备结构体中的设备名字
type:需要获取的设备资源类型
num:第几个资源(start->end之间的资源)