設備總線驅動模型:http://blog.csdn.NET/lizuobin2/article/details/51570196
本文主要參考:http://www.wowotech.net/device_model/platform_device.html
platform平臺設備驅動是基於設備總線驅動模型的,它只不過是將 device 進一步封裝成爲 platform_device,將 device_driver 進一步封裝成爲 platform_device_driver,前面已經分析過設備總線驅動模型,關於device 與 device_driver 的註冊過程以及它們在sysfs文件系統中的層次關係就不在分析,本文重點分析platform平臺設備驅動與設備總線驅動模型相比較新增添的那些東西。
在Linux設備模型的抽象中,存在着一類稱作“Platform Device”的設備,內核是這樣描述它們的(Documentation/driver-model/platform.txt):
Platform devices are devices that typically appear as autonomous entities in the system. This includes legacy port-based devices and host bridges to peripheral buses, and most controllers integrated into system-on-chip platforms. What they usually have
in common is direct addressing from a CPU bus. Rarely, a platform_device will be connected through a segment of some other kind of bus; but its registers will still be directly addressable.
概括來說,Platform設備包括:基於端口的設備(已不推薦使用,保留下來只爲兼容舊設備,legacy);連接物理總線的橋設備;集成在SOC平臺上面的控制器;連接在其它bus上的設備(很少見)。等等。
這些設備有一個基本的特徵:可以通過CPU bus直接尋址(例如在嵌入式系統常見的“寄存器”)。因此,由於這個共性,內核在設備模型的基礎上(device和device_driver),對這些設備進行了更進一步的封裝,抽象出paltform
bus、platform device和platform driver,以便驅動開發人員可以方便的開發這類設備的驅動。
可以說,paltform設備對linux驅動工程師是非常重要的,因爲我們編寫的大多數設備驅動,都是爲了驅動plaftom設備。
platform_bus_type
我們知道,在設備總線驅動模型的中,BUS像一個月老一樣,通過它的match函數,將註冊到bus中的device與driver進行配對,那麼每一個不同的bus 都有自己的match函數,我們來看看platform_bus_type.
-
struct bus_type platform_bus_type = {
-
.name = "platform",
-
.dev_attrs = platform_dev_attrs,
-
.match = platform_match,
-
.uevent = platform_uevent,
-
.pm = &platform_dev_pm_ops,
-
};
-
static int platform_match(struct device *dev, struct device_driver *drv)
-
{
-
struct platform_device *pdev = to_platform_device(dev);
-
struct platform_driver *pdrv = to_platform_driver(drv);
-
-
-
if (pdrv->id_table)
-
return platform_match_id(pdrv->id_table, pdev) != NULL;
-
-
-
return (strcmp(pdev->name, drv->name) == 0);
-
}
如果platform_device_driver中定義了id_table,則調用 platform_match_id 進行匹配
舉個例子:
-
static struct platform_device_id s3c24xx_driver_ids[] = {
-
{
-
.name = "s3c2410-i2c",
-
.driver_data = TYPE_S3C2410,
-
}, {
-
.name = "s3c2440-i2c",
-
.driver_data = TYPE_S3C2440,
-
}, { },
-
};
-
struct platform_device s3c_device_i2c0 = {
-
.name = "s3c2410-i2c",
-
#ifdef CONFIG_S3C_DEV_I2C1
-
.id = 0,
-
#else
-
.id = -1,
-
#endif
-
.num_resources = ARRAY_SIZE(s3c_i2c_resource),
-
.resource = s3c_i2c_resource,
-
};
-
static const struct platform_device_id *platform_match_id(struct platform_device_id *id, struct platform_device *pdev)
-
{
-
while (id->name[0]) {
-
if (strcmp(pdev->name, id->name) == 0) {
-
pdev->id_entry = id;
-
return id;
-
}
-
id++;
-
}
-
return NULL;
-
}
顯然,platform_match_id 的作用就是遍歷整個 Id_table 數組,尋找是否有與 platform_device->name 同名的,如果有,則返回這個 Platform_device_id ,使用Id_table 打破了原本設備總線驅動模型,一個 device 只能用與一個 device_driver 配對的侷限性。現在一個platform_device_driver 可以與多個platform_device配對。
如果沒有,則只是根據 platform_device_driver->name 與 platform_device->name 進行比較,這也就是老師爲啥在寫平臺設備驅動程序的時候經常說,“將驅動註冊到內核中去,如果有同名設備,則調用driver->probe函數....”。
pletform_device 中的 id 的作用:
if (pdev->id != -1) /* 如果不是-1 對name編號 */
dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
else /* -1時直接是名字 */
dev_set_name(&pdev->dev, pdev->name);
從device封裝而來的platform_device
-
struct platform_device {
-
const char * name;
-
int id;
-
struct device dev;
-
u32 num_resources;
-
struct resource * resource;
-
-
struct platform_device_id *id_entry;
-
-
-
struct pdev_archdata archdata;
-
};
name,設備的名稱,該名稱在設備註冊時,會拷貝到dev.init_name中。
dev,真正的設備,通過 container_of ,就能找到整個platform_device ,訪問其它成員,如後面要提到的 resource
num_resources、resource,該設備的資源描述,由struct resource(include/linux/ioport.h)結構抽象。
在Linux中,
系統資源包括I/O、Memory、Register、IRQ、DMA、Bus等多種類型。這些資源大多具有獨佔性,不允許多個設備同時使用,因此Linux內核提供了一些API,用於分配、管理這些資源。
當某個設備需要使用某些資源時,只需利用struct resource組織這些資源(如名稱、類型、起始、結束地址等),並保存在該設備的resource指針中即可。然後在設備probe時,設備需求會調用資源管理接口,分配、使用這些資源。而內核的資源管理邏輯,可以判斷這些資源是否已被使用、是否可被使用等等。
-
struct resource {
-
resource_size_t start;
-
resource_size_t end;
-
const char *name;
-
unsigned long flags;
-
struct resource *parent, *sibling, *child;
-
};
-
static struct resource led_resource[] = {
-
[0] = {
-
.start = 0x56000010,
-
.end = 0x56000010 + 8 - 1,
-
.flags = IORESOURCE_MEM,
-
},
-
[1] = {
-
.start = 5,
-
.end = 5,
-
.flags = IORESOURCE_IRQ,
-
},
-
};
-
static struct platform_device led_dev = {
-
.name = "myled",
-
.id = -1,
-
.num_resources = ARRAY_SIZE(led_resource),
-
.resource = led_resource,
-
-
.dev = {
-
.release = led_release,
-
-
},
-
};
從 device_driver 封裝而來的platform_device_dirver
-
struct platform_driver {
-
int (*probe)(struct platform_device *);
-
int (*remove)(struct platform_device *);
-
void (*shutdown)(struct platform_device *);
-
int (*suspend)(struct platform_device *, pm_message_t state);
-
int (*resume)(struct platform_device *);
-
struct device_driver driver;
-
struct platform_device_id *id_table;
-
};
-
int platform_driver_register(struct platform_driver *drv)
-
{
-
drv->driver.bus = &platform_bus_type;
-
if (drv->probe)
-
drv->driver.probe = platform_drv_probe;
-
if (drv->remove)
-
drv->driver.remove = platform_drv_remove;
-
if (drv->shutdown)
-
drv->driver.shutdown = platform_drv_shutdown;
-
-
return driver_register(&drv->driver);
-
}
struct platform_driver結構和struct device_driver非常類似,上邊的platform_drv_probe、platform_drv_remove、platform_drv_shutdown,只不過稍作轉換調用platform_driver中的probe、remove、shutdown函數,舉個例子稍微看一下
-
static int platform_drv_probe(struct device *_dev)
-
{
-
struct platform_driver *drv = to_platform_driver(_dev->driver);
-
struct platform_device *dev = to_platform_device(_dev);
-
-
return drv->probe(dev);
-
}
Platform Device提供的API
-
-
extern int platform_device_register(struct platform_device *);
-
extern void platform_device_unregister(struct platform_device *);
-
-
extern void arch_setup_pdev_archdata(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);
-
-
extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);
-
-
static inline struct platform_device *platform_device_register_resndata(
-
struct device *parent, const char *name, int id,
-
const struct resource *res, unsigned int num,
-
const void *data, size_t size)
-
-
static inline struct platform_device *platform_device_register_simple(
-
const char *name, int id,
-
const struct resource *res, unsigned int num)
-
-
static inline struct platform_device *platform_device_register_data(
-
struct device *parent, const char *name, int id,
-
const void *data, size_t size)
-
-
extern struct platform_device *platform_device_alloc(const char *name, int id);
-
extern int platform_device_add_resources(struct platform_device *pdev,
-
const struct resource *res,
-
unsigned int num);
-
extern int platform_device_add_data(struct platform_device *pdev,
-
const void *data, size_t size);
-
extern int platform_device_add(struct platform_device *pdev);
-
extern void platform_device_del(struct platform_device *pdev);
-
extern void platform_device_put(struct platform_device *pdev);
platform_device_register、platform_device_unregister,Platform設備的註冊/註銷接口,和底層的device_register等接口類似。
arch_setup_pdev_archdata,設置platform_device變量中的archdata指針。
platform_get_resource、platform_get_irq、platform_get_resource_byname、platform_get_irq_byname,通過這些接口,可以獲取platform_device變量中的resource信息,以及直接獲取IRQ的number等等。
platform_device_register_full、platform_device_register_resndata、platform_device_register_simple、platform_device_register_data,其它形式的設備註冊。調用者只需要提供一些必要的信息,如name、ID、resource等,Platform模塊就會自動分配一個struct platform_device變量,填充內容後,註冊到內核中。
platform_device_alloc,以name和id爲參數,動態分配一個struct platform_device變量。
platform_device_add_resources,向platform device中增加資源描述。
platform_device_add_data,向platform device中添加自定義的數據(保存在pdev->dev.platform_data指針中)。
platform_device_add、platform_device_del、platform_device_put,其它操作接口。
Platform Driver提供的API
platform_driver_registe、platform_driver_unregister,platform driver的註冊、註銷接口。
platform_driver_probe,主動執行probe動作。
platform_set_drvdata、platform_get_drvdata,設置或者獲取driver保存在device變量中的私有數據。
懶人API
-
extern struct platform_device *platform_create_bundle(
-
struct platform_driver *driver, int (*probe)(struct platform_device *),
-
struct resource *res, unsigned int n_res,
-
const void *data, size_t size);
只要提供一個platform_driver(要把driver的probe接口顯式的傳入),並告知該設備佔用的資源信息,platform模塊就會幫忙分配資源,並執行probe操作。對於那些不需要熱拔插的設備來說,這種方式是最省事的了。
簡單一例:
開發板:Mini2440
內核版本:2.6.32.2
-
#include <linux/module.h>
-
#include <linux/kernel.h>
-
#include <linux/fs.h>
-
#include <linux/init.h>
-
#include <linux/device.h>
-
#include <linux/interrupt.h>
-
#include <linux/sched.h>
-
#include <linux/irq.h>
-
#include <asm/uaccess.h>
-
#include <linux/input.h>
-
#include <linux/platform_device.h>
-
-
static struct resource led_resource[] = {
-
[0] = {
-
.start = 0x56000010,
-
.end = 0x56000010 + 8 - 1,
-
.flags = IORESOURCE_MEM,
-
},
-
[1] = {
-
.start = 5,
-
.end = 5,
-
.flags = IORESOURCE_IRQ,
-
},
-
};
-
-
static void led_release(struct device *dev){
-
-
}
-
-
-
static struct platform_device led_dev = {
-
.name = "myled",
-
.id = -1,
-
.num_resources = ARRAY_SIZE(led_resource),
-
.resource = led_resource,
-
-
.dev = {
-
.release = led_release,
-
-
},
-
};
-
-
static int led_dev_init(void){
-
-
-
platform_device_register(&led_dev);
-
return 0;
-
}
-
-
static void led_dev_exit(void){
-
platform_device_unregister(&led_dev);
-
}
-
-
module_init(led_dev_init);
-
module_exit(led_dev_exit);
-
MODULE_LICENSE("GPL");
-
-
#include <linux/module.h>
-
#include <linux/kernel.h>
-
#include <linux/fs.h>
-
#include <linux/init.h>
-
#include <linux/device.h>
-
#include <linux/interrupt.h>
-
#include <linux/sched.h>
-
#include <linux/irq.h>
-
#include <asm/uaccess.h>
-
-
#include <linux/platform_device.h>
-
#include <linux/io.h>
-
-
static int major;
-
-
static struct class *cls;
-
static struct device *dev;
-
-
static volatile unsigned long *gpio_con;
-
static volatile unsigned long *gpio_dat;
-
static int pin;
-
-
static int led_open(struct inode *inode, struct file *file){
-
-
*gpio_con &= ~(0x03 << (pin*2));
-
*gpio_con |= (0x01 << (pin*2));
-
return 0;
-
}
-
-
static ssize_t led_write(struct file *file, const char __user *buf,
-
size_t count, loff_t *ppos){
-
-
int val;
-
copy_from_user(&val, buf, count);
-
-
if(val == 1){
-
-
*gpio_dat &= ~(1<<pin);
-
}else{
-
-
*gpio_dat &= (1<<pin);
-
}
-
-
return 0;
-
}
-
-
static struct file_operations led_fops = {
-
-
.owner = THIS_MODULE,
-
.open = led_open,
-
.write = led_write,
-
};
-
-
static int led_probe(struct platform_device *pdev){
-
-
struct resource *res;
-
-
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-
gpio_con = ioremap(res->start, res->end - res->start + 1);
-
gpio_dat = gpio_con + 1;
-
-
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-
pin = res->start;
-
-
printk("led_probe, found led\n");
-
-
-
major = register_chrdev(0, "myled", &led_fops);
-
-
cls = class_create(THIS_MODULE, "myled");
-
-
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");
-
-
return 0;
-
}
-
-
static int led_remove(struct platform_device *pdev){
-
-
printk("led_remove, remove led\n");
-
-
device_unregister(dev);
-
-
class_destroy(cls);
-
-
unregister_chrdev(major, "myled");
-
-
iounmap(gpio_con);
-
-
return 0;
-
}
-
-
struct platform_driver led_drv = {
-
-
.probe = led_probe,
-
.remove = led_remove,
-
.driver = {
-
.name = "myled",
-
},
-
};
-
-
static int led_drv_init(void){
-
-
platform_driver_register(&led_drv);
-
return 0;
-
}
-
-
static void led_drv_exit(void){
-
-
platform_driver_unregister(&led_drv);
-
}
-
-
module_init(led_drv_init);
-
module_exit(led_drv_exit);
-
MODULE_LICENSE("GPL");