标签:
设备总线驱动模型: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); /* match against the id table first */ if (pdrv->id_table) return platform_match_id(pdrv->id_table, pdev) != NULL; /* fall-back to driver name match */ return (strcmp(pdev->name, drv->name) == 0); }如果platform_device_driver中定义了id_table,则调用 platform_match_id 进行匹配
struct platform_device { const char * name; int id; struct device dev; u32 num_resources; struct resource * resource; struct platform_device_id *id_entry; /* arch specific additions */ struct pdev_archdata archdata; };name,设备的名称,该名称在设备注册时,会拷贝到dev.init_name中。
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[] = { //jz2440的参数,驱动未测试 [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, //.devt = MKDEV(252, 1), }, };
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); }
/* include/linux/platform_device.h */ 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等接口类似。
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[] = { //jz2440的参数,驱动未测试 [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, //.devt = MKDEV(252, 1), }, }; static int led_dev_init(void){ //向bus注册led_dev match drv链表进行配对 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; // 最后一个参数 0 表示第1个该类型的资源 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, //匹配到dev之后调用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");
标签:
原文地址:http://blog.csdn.net/lizuobin2/article/details/51607813