嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
平台设备驱动:
包含BUS(总线)、DEVICE、DRIVER。
DEVICE:硬件相关的代码
DRIVER:比较稳定的代码
BUS有一个driver链表和device链表。
①把device放入bus的device链表中
②从bus的drv链表中取出每一个drv,用bus的match函数判断drv能否支持dev。
③如果可以执行,则调用probe函数。
driver和device类似。
device驱动程序代码:
/**
* file name: led_dev.c
*/
#include <linux/platform_device.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h> /* 分配注册一个平台device结构体 */
static struct resource led_resources[] = {
[] = {
.start= 0x56000050,
.end= 0x56000050 + - ,
.flags= IORESOURCE_MEM,
},
[] = {
.start= , //第4个引脚
.end= ,
.flags= IORESOURCE_IRQ,
},
}; void led_release(struct device * dev)
{ } static struct platform_device led_dev = {
.name= "myled",
.id= -,
.resource= led_resources,
.num_resources= ARRAY_SIZE(led_resources),
.dev = {.release = led_release,},
}; static int led_dev_init(void)
{
platform_device_register(&led_dev); return ;
} static void led_dev_exit(void)
{
platform_device_unregister(&led_dev);
return ;
} module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
driver驱动程序代码:
/**
* file name:led_driver.c
*/
#include <linux/platform_device.h>
#include <asm/mach/arch.h>
#include <asm/mach/map.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <linux/fs.h> static int major; struct class *led_class;
struct class_device *led_class_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 &= ~(0x3<<(pin*));
*gpio_con |= 0x1<<(pin*);
return ;
} ssize_t led_write(struct file *file, const char *buff, size_t len, loff_t *pos )
{
int val;
copy_from_user(&val, buff, );
val &= 0x1;
*gpio_dat &= ~(<<pin);
*gpio_dat |= (val<<pin); return ;
} 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; printk("led drv probe\n"); res = platform_get_resource(pdev,IORESOURCE_MEM,);
gpio_con = ioremap(res->start,res->end-res->start + );
gpio_dat = gpio_con + ; res = platform_get_resource(pdev,IORESOURCE_IRQ,);
pin = res->start; major = register_chrdev(,"myled", &led_fops );
led_class = class_create(THIS_MODULE,"myled");
led_class_dev = class_device_create(led_class,NULL,MKDEV(major,),NULL,"led%d",pin); return ;
} static int led_remove(struct platform_device *pdev)
{
iounmap(gpio_con);
class_device_unregister(led_class_dev);
class_destroy(led_class);
unregister_chrdev( major,"myled");
printk("led remove\n");
return ;
} struct platform_driver led_drv = {
.driver={
.owner=THIS_MODULE,
.name = "myled",
},
.probe = led_probe,
.remove= led_remove,
}; static int led_drv_init(void)
{
platform_driver_register(&led_drv);
return ;
} static void led_drv_exit(void)
{
platform_driver_unregister(&led_drv);
} module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
sd
嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动的更多相关文章
- 驱动程序分层分离概念_总线驱动设备模型_P
分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...
- 嵌入式linux的学习之路[转]
我认为的一条学习嵌入式Linux的路: 1)学习 Linux系统安装. 常用命令.应用程序安装. 2) 学习 Linux 下的 C 编程.这本书必学<UNIX 环境高级编程>.<UN ...
- linux字符设备驱动学习笔记(一):简单的字符设备驱动
最近在鼓捣lnux字符设备驱动,在网上搜集的各种关于linux设备驱动的代码和注释,要么是针对2.4的,要么是错误百出,根本就不能运行成功,真希望大家在发博客的时候能认真核对下代码的正确性,特别是要把 ...
- Linux-2.6驱动程序分层分离概念
下面以一个按键的实验作为驱动分离时间简单学习: #include <linux/module.h> #include <linux/version.h> #include &l ...
- 【Linux高级驱动】linux设备驱动模型之平台设备驱动机制
[1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数 a.申请主设备号 register_chrdev(major,name,file_operations); b.创 ...
- Linux驱动之平台设备驱动模型简析(驱动分离分层概念的建立)
Linux设备模型的目的:为内核建立一个统一的设备模型,从而有一个对系统结构的一般性抽象描述.换句话说,Linux设备模型提取了设备操作的共同属性,进行抽象,并将这部分共同的属性在内核中实现,而为需要 ...
- Linux Platform devices 平台设备驱动
设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196 本文主要参考:http://www.wowotech.net/devi ...
- Qt 学习之路 2(29):绘制设备
Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...
- 【Linux高级驱动】平台设备驱动机制的编程流程与编译进内核
[平台设备驱动机制的编程流程] [如何将驱动静态的编译进内核镜像] 1.添加资源(dev-led.c) 1.1:一般来说,系统习惯上将资源放在arch/arm/plat-samsung/目录中 cp ...
随机推荐
- php 实现设计模式之 享元模式
<?php class Book{ public $title; public $author; public function __construct($title,$author){ $th ...
- 使用Enyim.Caching访问阿里云的OCS
阿里云的开放式分布式缓存(OCS)简化了缓存的运维管理,使用起来很方便,官方推荐的.NET访问客户端类库为 Enyim.Caching,下面对此做一个封装. 首先引用最新版本 Enyim.Cachin ...
- 【原创-算法-实现】异步HTTP请求操作
一.说明 1) 这个类 是我 在真实项目中,优化解决真实问题 时,不参考第三方代码,完全由自己查阅MSDN官方文档 , 完成的一个真实生产环境中使用的功能类 2) 读者在使用此类时,请尊重原创,在代码 ...
- [转载]C#使用Interlocked进行原子操作
原文链接:王旭博客 » C# 使用Interlocked进行原子操作 什么是原子操作? 原子(atom)本意是"不能被进一步分割的最小粒子",而原子操作(atomic operat ...
- 深入理解及应用Position
position俗称定位,主要取值及作用如下: static 默认值.没有定位,出现在正常文档流中 absolute 绝对定位,相对于position为absolute.relative.fixed的 ...
- angular源码分析:angular中脏活累活承担者之$parse
我们在上一期中讲 $rootscope时,看到$rootscope是依赖$prase,其实不止是$rootscope,翻看angular的源码随便翻翻就可以发现很多地方是依赖于$parse的.而$pa ...
- webpack初体验
本人菜鸟一枚,最近一直在研究webpack的使用,记录下自己的学习体会,由于网上关于webpack的资源(技术博客)太多,对于初学webpack的新手来说,看着五花八门的技术博客,真是头晕眼花(可能是 ...
- CRM基于.NET的增删改查
一.准备工作: 1.添加 microsoft.crm.sdk.proxy.dll和microsoft.xrm.sdk.dll 引用到项目中!并引用以下using! using Microsoft.Xr ...
- centos 6.0用yum安装中文输入法
Centos6.2代码 CentOS 6.0没有默认没有装语言支持(Language Support),因此很不方面. 终于发现了有效的方法: su root yum install "@C ...
- AE影视后期之跳跃音符制作
制作跳动音符 新建项目 a.打开AE b.新建项目打开一张图片 c.新建合成将图片拖动到左下角的合成面板 新建文本图层. a.找到图层选项里面的新建text b.在里面输入IIIIIIIIIIIIII ...