驱动04.平台总线驱动模型——点亮LED灯
1 平台总线的简介
平台总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver。总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。
我们可以把一个驱动程序抽出来分为两部分,一部分是硬件相关的dev,另一部分则是稳定的纯软件部分driver。而总线只是一种机制,把dev和driver这两部分建立“联系”的机制。
eg:
①dev部分
a.把device放入bus的dev链表
b.从bus的drv链表取出每一个drv,用bus的match函数判断drv是否支持dev
c.如果支持,将调用drv的probe函数
②drv部分
a.把driver放入bus的drv链表
b.从bus的dev链表取出每一个dev,用bus的match函数判断dev是否支持drv
c.如果支持,将调用drv的probe函数
2 linux平台总线的代码分析
struct bus_type platform_bus_type = {
    .name        = "platform",
    .dev_attrs    = platform_dev_attrs,
    .match        = platform_match,  //dev和drv匹配时调用的函数
    .uevent        = platform_uevent,
    .suspend    = platform_suspend,
    .suspend_late    = platform_suspend_late,
    .resume_early    = platform_resume_early,
    .resume        = platform_resume,
};
static int platform_match(struct device * dev, struct device_driver * drv)
{
struct platform_device *pdev = container_of(dev, struct platform_device, dev); return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == );//当dev的设备名与drv的名字相同时,则匹配成功
}
platform_device结构体的定义
struct platform_device {
    const char    * name;
    u32        id;
    struct device    dev;
    u32        num_resources;//资源数目
    struct resource    * resource;//设备信息,使用platform_get_resource函数来获取资源信息
};
注册平台设备platform_device_register,实际上是加入到bus的dev链表中
int platform_device_register(struct platform_device * pdev)
{
device_initialize(&pdev->dev);//初始化platform_device的device成员
return platform_device_add(pdev);//实际上是调用device_add函数
}
同样的,也有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 (*suspend_late)(struct platform_device *, pm_message_t state);
     int (*resume_early)(struct platform_device *);
     int (*resume)(struct platform_device *);
     struct device_driver driver;
 }
//device_driver的定义:
 struct device_driver {
     const char        * name;
     struct bus_type        * bus;
     struct kobject        kobj;
     struct klist        klist_devices;
     struct klist_node    knode_bus;
     struct module        * owner;
     const char         * mod_name;    /* used for built-in modules */
     struct module_kobject    * mkobj;
     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);
 };
注册device_driver结构体:
int driver_register(struct device_driver * drv)
{
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown)) {
printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
}
klist_init(&drv->klist_devices, NULL, NULL);
return bus_add_driver(drv);
}
3 写代码
3.1框架
(1)分配、设置、注册一个platform_device/platform_driver结构体
(2)按照led.c的框架来构建
3.2 源代码
#include <linux/module.h>
#include <linux/version.h> #include <linux/init.h> #include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h> static struct resource myled_dev_resource[] = {
[] = {
.start = 0x56000050,
.end = 0x56000050 + - ,
.flags = IORESOURCE_MEM,
},
[] = {
.start = ,
.end = ,
.flags = IORESOURCE_IRQ,
}
}; static void myled_dev_release(struct device * dev)
{
} static struct platform_device myled_dev = {
.name = "myled",
.id = -,
.num_resources = ARRAY_SIZE(myled_dev_resource),
.resource = myled_dev_resource,
.dev = {
.release = myled_dev_release,
}
}; static int myled_dev_init(void)
{
platform_device_register(&myled_dev);
return ;
} static void myled_dev_exit(void)
{
platform_device_unregister(&myled_dev);
} module_init(myled_dev_init);
module_exit(myled_dev_exit); MODULE_LICENSE("GPL");
myled_dev.c
#include <linux/module.h>
#include <linux/version.h> #include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h> static struct class *g_ptMyledCls;
static struct class_device *g_ptMyledClsDev;
static int g_iPin; volatile unsigned long *gpiocon = NULL;
volatile unsigned long *gpiodat = NULL; static int myled_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置GPF4,5,6为输出 */
*gpiocon &= ~(0x3<<(g_iPin*)) ;
*gpiodat |= (0x1<<(g_iPin*)) ;
return ;
} static ssize_t myled_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val; //printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user(); if (val == )
{
// 点灯
*gpiodat &= ~(<<g_iPin);
}
else
{
// 灭灯
*gpiodat |= (<<g_iPin);
} return ;
} static struct file_operations myled_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = myled_open,
.write = myled_write,
}; static int g_iMajor; static int myled_probe(struct platform_device *dev)
{
struct resource *tRes;
tRes = platform_get_resource(dev, IORESOURCE_MEM, );
gpiocon = ioremap(tRes->start, tRes->end - tRes->start +);
gpiodat = gpiocon + ; tRes = platform_get_resource(dev, IORESOURCE_IRQ, );
g_iPin = tRes->start; printk("myled_probe, found led\n"); g_iMajor = register_chrdev(, "myled", &myled_fops); // 注册, 告诉内核
g_ptMyledCls = class_create(THIS_MODULE, "myled"); g_ptMyledClsDev = class_device_create(g_ptMyledCls, NULL, MKDEV(g_iMajor, ), NULL, "myled"); /* /dev/xyz */
return ;
} static int myled_remove(struct platform_device *dev)
{
printk("led_remove, remove led\n"); class_device_destroy(g_ptMyledCls, MKDEV(g_iMajor, ));
class_destroy(g_ptMyledCls);
unregister_chrdev(g_iMajor, "myled");
iounmap(gpiocon); return ;
} static struct platform_driver myled_driver = {
.probe = myled_probe,
.remove = myled_remove,
.driver = {
.name = "myled",
},
}; static int myled_drv_init(void)
{
platform_driver_register(&myled_driver);
return ;
} static void myled_drv_exit(void)
{
platform_driver_unregister(&myled_driver);
} module_init(myled_drv_init);
module_exit(myled_drv_exit); MODULE_LICENSE("GPL");
myled_drv.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h> /* firstdrvtest on
* firstdrvtest off
*/
int main(int argc, char **argv)
{
int fd;
int val = ;
fd = open("/dev/myled", O_RDWR);
if (fd < )
{
printf("can't open!\n");
}
if (argc != )
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[]);
return ;
} if (strcmp(argv[], "on") == )
{
val = ;
}
else
{
val = ;
} write(fd, &val, );
return ;
}
测试程序
编辑于2017-01-09 16:36:01
驱动04.平台总线驱动模型——点亮LED灯的更多相关文章
- linux平台总线驱动设备模型之点亮LED
		
这一节里,我们来使用平台驱动设备这一套架构来实现我们之前使用简单的字符设备驱动点亮LED,这里并无实际意义,只是告诉大家如果编写平台总线驱动设备. 问:如何编写平台总线驱动设备这一套架构的设备驱动? ...
 - 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联
		
转载自:http://www.kancloud.cn/yueqian_scut/emlinux/106829 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动.设备驱动模型和sy ...
 - [kernel]字符设备驱动、平台设备驱动、设备驱动模型、sysfs几者之间的比较和关联
		
转自:http://www.2cto.com/kf/201510/444943.html Linux驱动开发经验总结,绝对干货! 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动 ...
 - 第7章     使用寄存器点亮LED灯
		
第7章 使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
 - 第7章 	使用寄存器点亮LED灯—零死角玩转STM32-F429系列
		
第7章 使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
 - 字符型设备驱动程序-first-printf以及点亮LED灯(三)
		
根据 字符型设备驱动程序-first-printf以及点亮LED灯(二) 学习 修改函数 中的printf 为 printk. #include <linux/module.h> /* ...
 - Raspberry PI 系列 —— 裸机点亮LED灯
		
Raspberry PI 系列 -- 裸机点亮LED灯 背景 近期刚买了Raspberry PI B+,配置执行了官方提供的Raspbian系统,折腾了一周Linux系统,感觉没啥意思,于是就试着想了 ...
 - 第二章之S5PV210在BL1中点亮LED灯
		
1,u-boot中第一个入口在./arch/arm/cpu/armv7/start.S 翻到153行:如下图 前面都是进行一些基本设置,不用管. cpu_init_cp15设置协处理器, cpu_in ...
 - C语言版——点亮LED灯,深入到栈
		
在上一篇进行了汇编语言的编写之后,我们采用C语言来编写程序,毕竟C语言才是我们使用最多的语言. 仅仅是点亮LED灯显然太过于简单,我们需要分析最后的反汇编,了解函数调用栈,深入C语言骨髓去分析代码,并 ...
 
随机推荐
- LinQ—扩展方法
			
概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...
 - leetcode[88] Gray Code
			
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
 - windows下mysql备份、还原,使用mysqldump
			
直接备份 mysqldump -u用户名 -p密码 -h 192.168.1.15 -c --default-character-set=utf8 数据库名>xxx.sql 使用gz ...
 - Android高效开发环境(Genymotion,Gradle,Andriod Studio)
			
临近十一,项目接近上线,终于有些碎片时间可以查看一些博客. 这篇博客是Android开发大牛Cyril Mottier在去年写的博客,我把它翻译一下共享给国内志同道合的朋友,同时也是对自己一个很好的锻 ...
 - 使用Json实体类构建菜单数据
			
基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开发框 ...
 - iOS基础 - 静态库
			
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系 ...
 - Day2:T3DP(基于排列组合思想)
			
T3:DP(基于排列组合思想的状态转移) 其实之前写排列组合的题目有一种很茫然的感觉.... 应该是因为之前没有刷过所以没有什么体会 上次刷的vj1060有用到,但是写状态转移还是第一次学习吧 ccy ...
 - net破解一(反编译,反混淆-剥壳,工具推荐)
			
net破解一(反编译,反混淆-剥壳,工具推荐) 大家好,前段时间做数据分析,需要解析对方数据,而数据文件是对方公司内部的生成方式,完全不知道它是怎么生成的. 不过还好能拿到客户端(正好是C#开发)所以 ...
 - 活动图activity diagram
			
活动图activity diagram 系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) [UML]UML系列——类 ...
 - cookie和session详解[转]
			
文章链接: http://aijezdm915.iteye.com/blog/1272530 cookie.session 都是用来保存用户状态信息的一种方法或手段 二者主要区别是: ...