在linux2.6設(shè)備模型中癣诱,關(guān)心總線,設(shè)備袜香,驅(qū)動這三個實體撕予,總線將設(shè)備和驅(qū)動綁定,在系統(tǒng)每注冊一個設(shè)備的時候蜈首,會尋找與之匹配的驅(qū)動实抡。相反,在系統(tǒng)每注冊一個驅(qū)動的時候欢策,尋找與之匹配的設(shè)備吆寨,匹配是由總線來完成的。
硬件資源用專門的模塊維護踩寇,驅(qū)動用專門的模塊維護啄清,使用platform總線連接,設(shè)備用 platform_device 表示;驅(qū)動用platform_driver 進行注冊。
對于依附在USB俺孙、PCI盒延、I2C、SPI等物理總線來 這些都不是問題鼠冕。但是在嵌入式系統(tǒng)里面,在Soc系統(tǒng)中集成的獨立外設(shè)控制器胯盯,掛接在Soc內(nèi)存空間的外設(shè)等卻不依附在此類總線懈费。基于這一背景博脑,Linux發(fā)明了一種虛擬總線憎乙,稱為platform。
platform總線相關(guān)代碼:driver\base\platform.c
相關(guān)結(jié)構(gòu)體定義:include\linux\platform_device.h
一叉趣、platform_driver
struct platform_driver {
int (*probe)(struct platform_device *); //探測函數(shù)泞边,在注冊平臺設(shè)備時被調(diào)用
int (*remove)(struct platform_device *); //刪除函數(shù),在注銷平臺設(shè)備時被調(diào)用
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver; //設(shè)備驅(qū)動結(jié)構(gòu)
const struct platform_device_id *id_table;//該設(shè)備驅(qū)動支持的設(shè)備的列表
bool prevent_deferred_probe;
};
驅(qū)動程序的描述結(jié)構(gòu)體:
struct device_driver {
const char *name;
struct bus_type *bus;
struct module *owner;
const char *mod_name; /* used for built-in modules */
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
const struct of_device_id *of_match_table;
const struct acpi_device_id *acpi_match_table;
int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};
struct platform_driver pdrv={
.probe = hello_probe,
.remove = hello_remove,
.driver.name = "chuanpu", //設(shè)備名稱必須匹配
};
接口函數(shù)(driver\base\platform.c)
int platform_driver_register(struct platform_driver *); // 用來注冊我們的設(shè)備驅(qū)動
void platform_driver_unregister(struct platform_driver *); // 用來卸載我們的設(shè)備驅(qū)動
二疗杉、platform_device
struct platform_device {
const char *name; //設(shè)備的名字
int id; //ID 是用來區(qū)分如果設(shè)備名字相同的時候
bool id_auto;
struct device dev; //設(shè)備結(jié)構(gòu)
u32 num_resources; //resource結(jié)構(gòu)個數(shù)
struct resource *resource; //設(shè)備資源
const struct platform_device_id *id_entry;
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
resource結(jié)構(gòu)體存入了最為重要的設(shè)備資源信息
struct resource {
resource_size_t start; //資源起始地址
resource_size_t end; //資源結(jié)束地址
const char *name;
unsigned long flags; //資源類型
struct resource *parent, *sibling, *child;
};
我們通常關(guān)心start阵谚、end 和flags 這3 個字段蚕礼,分別標(biāo)明資源的開始值、結(jié)束值和類型梢什,flags 可以為IORESOURCE_IO奠蹬、IORESOURCE_MEM、IORESOURCE_IRQ嗡午、IORESOURCE_DMA 等
#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
#define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */
#define IORESOURCE_MEM 0x00000200
#define IORESOURCE_REG 0x00000300 /* Register offsets */
#define IORESOURCE_IRQ 0x00000400
#define IORESOURCE_DMA 0x00000800
#define IORESOURCE_BUS 0x00001000
例如 fs4412上的beep驅(qū)動
struct resource beep_res[] ={
[0]={
.start = 0x114000a0,
.end = 0x114000a0+0x3,
.flags = IORESOURCE_MEM,
},
[1]={
.start = 0x139d0000,
.end = 0x139d0000+0x13,
.flags = IORESOURCE_MEM,
},
};
struct platform_device pdev={
.name = "chuanpu", //設(shè)備名稱
.id = -1,
.dev.release = hello_release, //必須實現(xiàn)
.num_resources = ARRAY_SIZE(beep_res),
.resource = beep_res,
};
接口函數(shù)(driver\base\platform.c)
int platform_device_register(struct platform_device *); // 用來注冊我們的設(shè)備
void platform_device_unregister(struct platform_device *); // 用來卸載我們的設(shè)備
三囤躁、platform下的驅(qū)動架構(gòu)
硬件資源從probe函數(shù)從platform_device傳遞,不應(yīng)該用宏定義死硬件地址
int hello_probe(struct platform_device *pdev)
{
//當(dāng)platform_device 和platform_driver 匹配成功之后荔睹,才會調(diào)用probe狸演,
1. 申請設(shè)備號
2. cdev
3. class
4. ioremap
5. beep初始化
}
int hello_remove(struct platform_device *pdev)
{
釋放資源
}
struct platform_driver pdrv={
.probe = hello_probe,
.remove = hello_remove,
.driver.name = "chuanpu",
};
static int hello_init(void)
{
return platform_driver_register(&pdrv);
}
static void hello_exit(void)
{
platform_driver_unregister(&pdrv);
}