Linux平台总线驱动设备模型
来源:互联网 发布:现在做网络推广好做吗 编辑:程序博客网 时间:2024/05/21 20:26
在Linux 2.6以后的设备驱动模型中,需关心总线、设备和驱动这3个实体,总线将设备和驱动绑定。在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反地,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。
platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver。Linux 2.6的设备驱动模型中,把I2C、RTC、LCD等都归纳为platform_device。
1. platform_device
platform_device结构体的定义如下:
struct platform_device { const char *name; int id; bool id_auto; struct device dev; u32 num_resources; struct resource *resource; const struct platform_device_id *id_entry; char *driver_override; /* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell; /* arch specific additions */ struct pdev_archdata archdata;}
留意platform_device结构体定义的第6、7两行,它们描述了platform_device的资源,资源本身由resource结构体描述,定义如下:
struct resource { resource_size_t start; resource_size_t end; const char *name; unsigned long flags; struct resource *parent, *sibling, *child;};
我们通常关心start、end和flags三个字段,它们分别标识资源的开始值、结束值和类型。flags可为IORESOURCE_IO、IORESOURCE_MEM、IORESOURCE_IRQ和IORESOURCE_DMA等。对resource的定义也通常在BSP的板级文件中进行,在具体的设备驱动中通过platform_get_resource()这样的API来获取,其定义为:
struct resource *platform_get_resource(struct plarform_device* , unsigned int, unsigned int);
对于IRQ而言,还有另一个获取的方法:
int platform_get_irq(struct platform_device* dev, unsigned int num);
设备除了可以在BSP中定义资源以外,还可以附加一些数据信息。platform提供了对platform_data的支持,其形式由每个驱动自定义。在具体的设备驱动中通过dev_get_platdata()这样的API来获取,其定义为:
static inline void *dev_get_platdata(const struct device *dev);
2. platform_driver
platform_driver结构体的定义如下:
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; const struct platform_device_id *id_table; bool prevent_deferred_probe;};
device_driver的定义:
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;};
platform_driver结构体有device_driver成员,该成员的各自字段如上所示,device_driver也有probe、remove、shutdown等函数,在平台驱动注册的时候被初始化。
3. platform_bus_type
Linux2.6系统中定义了一个bus_type的实例platform_bus_type,其定义如下:
struct bus_type platform_bus_type = { .name = "platform", .dev_groups = platform_dev_groups, .match = platform_match, //设备和驱动使用match函数来判断是否匹配 .uevent = platform_uevent, .pm = &platform_dev_pm_ops,};
这里需要重点关注match()成员函数,它决定了platform_device和platform_driver之间是如何进行匹配的,其定义如下:
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); /* When driver_override is set, only bind to the matching driver */ if (pdev->driver_override) return !strcmp(pdev->driver_override, drv->name); /* Attempt an OF style match first */ if (of_driver_match_device(dev, drv)) return 1; /* Then try ACPI style match */ if (acpi_driver_match_device(dev, drv)) return 1; /* Then try to match against the id table */ 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);}
从match()函数的定义可以看出,platform_device和platform_driver匹配有四种可能性:
- 基于devicetree风格的匹配;
- 基于ACPI风格的匹配;
- 匹配ID表(即platform_device设备名是否出现在platform_driver的ID表内);
- 匹配platform_device设备名和驱动的名字
匹配成功则调用platform_driver的probe函数。
4. platform_device的注册和注销
对于Linux 2.6的平台而言,对platform_device的定义通常在BSP的板级文件实现,在板级文件中,将platform_device归纳为一个数组,最终通过platform_add_devices()函数统一注册,将平台设备添加到系统中。其定义如下:
int platform_add_devices(struct platform_device **devs, int num){ int i, ret = 0; for (i = 0; i < num; i++) { ret = platform_device_register(devs[i]); if (ret) { while (--i >= 0) platform_device_unregister(devs[i]); break; } } return ret;}
它内部调用了platform_device_register()函数以注册单个的平台设备。
对于3.x以后的系统,ARM Linux更多使用devicetree的内容自动展开platform_device。
相反地,如果要注销平台设备则使用platform_device_unregister()函数,其定义如下:
void platform_device_unregister(struct platform_device *pdev){ platform_device_del(pdev); platform_device_put(pdev);}
5. platform_driver的注册和注销
platform_driver的注册使用platform_driver_register()函数,其定义如下:
int __platform_driver_register(struct platform_driver *drv, struct module *owner){ drv->driver.owner = owner; 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);}
platform_driver的注销使用platform_driver_unregister()函数,其定义如下:
void platform_driver_unregister(struct platform_driver *drv){ driver_unregister(&drv->driver);}
宏module_platform_driver(struct platform_driver)所定义的模块加载和卸载函数仅仅通过platform_driver_register()和platform_driver_unregister()函数进行platform_driver的注册和注销。
6. platform driver代码
/** a simple char device driver: globalfifo** Licensed under GPLv2 or later.***/#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/cdev.h>#include <linux/slab.h>#include <linux/uaccess.h>#include <linux/sched.h>#include <linux/poll.h>#include <linux/platform_device.h>#include <linux/miscdevice.h>#define GLOBALFIFO_SIZE 0x1000#define FIFO_CLEAR 0x1struct globalfifo_dev{ unsigned int current_len; unsigned char mem[GLOBALFIFO_SIZE]; struct mutex mutex; wait_queue_head_t r_wait; wait_queue_head_t w_wait; struct fasync_struct* async_queue; struct miscdevice miscdev;};static int globalfifo_fasync(int fd, struct file* filp, int mode){ struct globalfifo_dev* dev = container_of(filp->private_data, struct globalfifo_dev, miscdev); //初始化/释放 fasync_struct 结构体 (fasync_struct->fa_file->f_owner->pid) return fasync_helper(fd, filp, mode, &dev->async_queue);}static int globalfifo_open(struct inode* inode, struct file* filp){ return 0;}static int globalfifo_release(struct inode* inode, struct file* filp){ globalfifo_fasync (-1, filp, 0); return 0;}static long globalfifo_ioctl(struct file* filp, unsigned int cmd, unsigned long arg){ struct globalfifo_dev* dev = container_of(filp->private_data, struct globalfifo_dev, miscdev); switch(cmd) { case FIFO_CLEAR: mutex_lock(&dev->mutex); memset(dev->mem, 0, GLOBALFIFO_SIZE); printk(KERN_INFO "globalfifo is set to zero\n"); mutex_unlock(&dev->mutex); break; default: return -EINVAL; } return 0;}static ssize_t globalfifo_read(struct file* filp, char __user* buf, size_t count, loff_t* ppos){ int ret = 0; struct globalfifo_dev* dev = container_of(filp->private_data, struct globalfifo_dev, miscdev); DECLARE_WAITQUEUE(wait, current); mutex_lock(&dev->mutex); add_wait_queue(&dev->r_wait,&wait); while(dev->current_len == 0) { if(filp->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; } __set_current_state(TASK_INTERRUPTIBLE); mutex_unlock(&dev->mutex); schedule(); if( signal_pending(current) ) { ret = -ERESTARTSYS; goto out2; } mutex_lock(&dev->mutex); } if( count > dev->current_len ) count = dev->current_len; if(copy_to_user(buf, dev->mem, count)) { ret = -EFAULT; goto out; } else { memcpy(dev->mem, dev->mem + count, dev->current_len - count ); dev->current_len -= count; printk(KERN_INFO "read %d bytes(s), current_len:%d\n", count, dev->current_len); wake_up_interruptible(&dev->w_wait); ret = count; } out: mutex_unlock(&dev->mutex); out2: remove_wait_queue(&dev->r_wait, &wait); set_current_state(TASK_RUNNING); return ret;}static ssize_t globalfifo_write(struct file* filp, const char __user* buf, size_t count, loff_t* ppos){ int ret; struct globalfifo_dev* dev = container_of(filp->private_data, struct globalfifo_dev, miscdev); DECLARE_WAITQUEUE(wait, current); mutex_lock(&dev->mutex); add_wait_queue(&dev->w_wait,&wait); while( dev->current_len == GLOBALFIFO_SIZE ) { if(filp->f_flags & O_NONBLOCK) { ret = -EAGAIN; goto out; } __set_current_state(TASK_INTERRUPTIBLE); mutex_unlock(&dev->mutex); schedule(); if( signal_pending(current) ) { ret = -ERESTARTSYS; goto out2; } mutex_lock(&dev->mutex); } if( count > GLOBALFIFO_SIZE - dev->current_len ) count = GLOBALFIFO_SIZE - dev->current_len; if( copy_from_user( dev->mem + dev->current_len, buf, count ) ) { ret = -EFAULT; goto out; } else { dev->current_len += count; printk(KERN_INFO "write %d bytes(s),current_len:%d\n", count, dev->current_len); wake_up_interruptible(&dev->r_wait); if ( dev->async_queue) { //发送信号SIGIO信号给fasync_struct 结构体所描述的PID,触发应用程序的SIGIO信号处理函数 kill_fasync(&dev->async_queue, SIGIO, POLL_IN); printk(KERN_DEBUG "%s kill SIGIO\n", __func__); } ret = count; } out: mutex_unlock(&dev->mutex); out2: remove_wait_queue(&dev->w_wait, &wait); set_current_state(TASK_RUNNING); return ret;}static unsigned int globalfifo_poll(struct file* filp, poll_table* wait){ unsigned int mask = 0; struct globalfifo_dev* dev = container_of(filp->private_data, struct globalfifo_dev, miscdev); mutex_lock(&dev->mutex); poll_wait(filp, &dev->r_wait, wait); poll_wait(filp, &dev->w_wait, wait); if(dev->current_len != 0) mask |= POLLIN | POLLRDNORM; if(dev->current_len != GLOBALFIFO_SIZE) mask |= POLLOUT | POLLWRNORM; mutex_unlock(&dev->mutex); return mask;}static const struct file_operations globalfifo_fops ={ .owner = THIS_MODULE, .read = globalfifo_read, .write = globalfifo_write, .unlocked_ioctl = globalfifo_ioctl, .open = globalfifo_open, .release = globalfifo_release, .poll = globalfifo_poll, .fasync = globalfifo_fasync,};static int globalfifo_probe(struct platform_device* pdev){ struct globalfifo_dev* gl; int ret; gl = devm_kzalloc(&pdev->dev, sizeof(*gl), GFP_KERNEL); if(!gl) return -ENOMEM; gl->miscdev.minor = MISC_DYNAMIC_MINOR; gl->miscdev.name = "globalfifo"; gl->miscdev.fops = &globalfifo_fops; mutex_init(&gl->mutex); init_waitqueue_head(&gl->r_wait); init_waitqueue_head(&gl->w_wait); platform_set_drvdata(pdev, gl); ret = misc_register(&gl->miscdev); if(ret < 0) goto err; return 0; err: return ret;}static int globalfifo_remove(struct platform_device* pdev){ struct globalfifo_dev* gl = platform_get_drvdata(pdev); misc_deregister(&gl->miscdev); return 0;}static struct platform_driver globalfifo_driver ={ .driver = { .name = "globalfifo", .owner = THIS_MODULE, }, .probe = globalfifo_probe, .remove = globalfifo_remove,};static int __init globalfifo_init(void){ return platform_driver_register(&globalfifo_driver);}static void __exit globalfifo_exit(void){ platform_driver_unregister(&globalfifo_driver);}module_init(globalfifo_init);module_exit(globalfifo_exit);MODULE_AUTHOR("were0415");MODULE_LICENSE("GPL v2");
7. platform device代码
#include <linux/module.h>#include <linux/init.h>#include <linux/platform_device.h>static struct platform_device* globalfifo_pdev;static int __init globalfifodev_init(void){ int ret; globalfifo_pdev = platform_device_alloc("globalfifo", -1); if(!globalfifo_pdev) return -ENOMEM; ret = platform_device_add(globalfifo_pdev); if(ret) { platform_device_put(globalfifo_pdev); return ret; } return 0;}static void __exit globalfifodev_exit(void){ platform_device_unregister(globalfifo_pdev);}module_init(globalfifodev_init);module_exit(globalfifodev_exit);MODULE_AUTHOR("were0415");MODULE_LICENSE("GPL v2");
8. 编译并测试
编译6、7两个模块代码会生成两个文件:globalfifo.ko和globalfifo-dev.ko,把globalfifo.ko和globalfifo-dev.ko先后insmod,会导致platform_driver和platform_device的匹配,globalfifo_probe()会执行,/dev/globalfifo节点也会自动生成,默认情况下需要root权限来访问/dev/globalfifo。
如果此后我们rmmod globalfifo-dev,则会导致platform_driver的remove函数执行,即globalfifo_remove()函数被执行,/dev/globalfifo节点会自动消失。
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- Linux平台总线驱动设备模型
- linux平台总线驱动设备模型之点亮LED
- linux平台总线驱动设备模型之点亮LED
- linux平台总线驱动设备模型之点亮LED
- Linux设备驱动模型4——平台总线实践
- LINUX设备驱动模型之PLATFORM(平台)总线详解
- C语言实例10——有关ASCII图形的输出
- 跨域错误问题has been blocked by cors policy
- 在linux字体下的显示问题及几种解决办法
- android 6.0权限全面详细分析和解决方案
- 十年工龄的程序员为你揭示最危害程序员职业生涯的三大观念
- Linux平台总线驱动设备模型
- activeMQ 例子(一) 简单的p2p(point to point)模式
- C++ Primer中的陌生概念一:内联函数
- hadoop集群搭建2.7.3
- Spring 学习之错误收集及解决方案
- Linux的I/0模型
- RDD持久化原理与共享变量
- mongo查看当前进程及更改字段类型命令
- 360浏览器的极速模式和兼容模式的区别