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灯的更多相关文章

  1. linux平台总线驱动设备模型之点亮LED

    这一节里,我们来使用平台驱动设备这一套架构来实现我们之前使用简单的字符设备驱动点亮LED,这里并无实际意义,只是告诉大家如果编写平台总线驱动设备. 问:如何编写平台总线驱动设备这一套架构的设备驱动? ...

  2. 字符设备驱动、平台设备驱动、设备驱动模型、sysfs的比较和关联

    转载自:http://www.kancloud.cn/yueqian_scut/emlinux/106829 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动.设备驱动模型和sy ...

  3. [kernel]字符设备驱动、平台设备驱动、设备驱动模型、sysfs几者之间的比较和关联

    转自:http://www.2cto.com/kf/201510/444943.html Linux驱动开发经验总结,绝对干货! 学习Linux设备驱动开发的过程中自然会遇到字符设备驱动.平台设备驱动 ...

  4. 第7章 使用寄存器点亮LED灯

    第7章     使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  5. 第7章 使用寄存器点亮LED灯—零死角玩转STM32-F429系列

    第7章     使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  6. 字符型设备驱动程序-first-printf以及点亮LED灯(三)

    根据  字符型设备驱动程序-first-printf以及点亮LED灯(二) 学习 修改函数 中的printf 为 printk. #include <linux/module.h> /* ...

  7. Raspberry PI 系列 —— 裸机点亮LED灯

    Raspberry PI 系列 -- 裸机点亮LED灯 背景 近期刚买了Raspberry PI B+,配置执行了官方提供的Raspbian系统,折腾了一周Linux系统,感觉没啥意思,于是就试着想了 ...

  8. 第二章之S5PV210在BL1中点亮LED灯

    1,u-boot中第一个入口在./arch/arm/cpu/armv7/start.S 翻到153行:如下图 前面都是进行一些基本设置,不用管. cpu_init_cp15设置协处理器, cpu_in ...

  9. C语言版——点亮LED灯,深入到栈

    在上一篇进行了汇编语言的编写之后,我们采用C语言来编写程序,毕竟C语言才是我们使用最多的语言. 仅仅是点亮LED灯显然太过于简单,我们需要分析最后的反汇编,了解函数调用栈,深入C语言骨髓去分析代码,并 ...

随机推荐

  1. OCP-1Z0-051-题目解析-第3题

    3. You need to extract details of those products in the SALES table where the PROD_ID columncontains ...

  2. leetcode第36题--Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  3. Sql Server 主键由字母数字组成并按照数字自动增长

    在SQL SERVER 中如果我们想要使主键按照一定规则自动增长我们可以这样做: 这里我们新建一张研究表,里面有研究ID,研究人员姓名和研究医院. 我们使SicentificId 设为主键 并且从1开 ...

  4. 处理动态SQL语句的参数

    原文:处理动态SQL语句的参数 经常对SQL进行开发,写动态的SQL语句,是少之不了的,但是在使用动态语句中,常是因为有动态的参数的出现.参考下面代码示例: 正因为有了标记1的动态条件代码,而让SQL ...

  5. Dev环境中的集成测试用例执行时上下文环境检查(实战)

    Dev环境中的集成测试用例执行时上下文环境检查(实战) Microsoft.NET 解决方案,项目开发必知必会. 从这篇文章开始我将分享一系列我认为在实际工作中很有必要的一些.NET项目开发的核心技术 ...

  6. delphi字符串函数大全

    转帖:delphi字符串函数大全 2009-11-17 16:43:55 分类: delphi字符串函数大全 ━━━━━━━━━━━━━━━━━━━━━首部 function StringToGUID ...

  7. Java 8新特性前瞻

    快端午小长假了,要上线的项目差不多完结了,终于有时间可以坐下来写篇博客了. 这是篇对我看到的java 8新特性的一些总结,也是自己学习过程的总结. 几乎可以说java 8是目前为止,自2004年jav ...

  8. c#中关于sealed修饰类的性能提升的测试

    在clr var c#一书中,作者描述当用sealed修饰类时,可以提高系统性能而且建议大家也养成用sealed来修饰类的习惯.由于对性能二字比较敏感,所以本文先测试一下用sealed分别修饰和不修饰 ...

  9. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  10. [转]在 Mac OS X上编译 libimobiledevice 的方法

    link: http://blog.boceto.fr/2012/05/05/libimobiledevice-for-macosx/ The objective of the day: Compil ...