适应时代发展,将misc驱动封装到platfrom总线里面去,
platform平台总线模型,把设备和驱动分开,即一个东东要分成两个部分去写和去insmod,是不是有点麻烦。--对于固定于一个cpu平台的用户确实感觉比较麻烦,但是linux的目标是兼容所有的cpu平台,在换cpu平台时就会发现这种设计的优点。
先看一个platform的简单例子
先把Makefile列出

ifneq ($(KERNELRELEASE),)
obj-m :=  platform_dev.o  platform_drv.o
else
KDIR := /opt/FriendlyARM/mini2440/linux-2.6.32.2
#KDIR := /lib/modules/`uname -r`/build
all:  make -C $(KDIR) M=$(PWD) modules
clean:  rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif  

以下是设备

/******************platfrom_dev.c***************************/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/kernel.h>#define DEVICE_NAME "song_rfid"static struct resource my_resource[] = {[0] = {.start = 0x56000050,.end   = 0x56000050 + 0x10 - 1,.flags = IORESOURCE_MEM,},
/*
设备内存资源
GPFCON 0X56000050
GPFDAT 0X56000054
GPFUP  0X56000058
*/
/*[1] = {.start = 25,.end   = 25,.flags = IORESOURCE_IRQ,}*/
};static struct platform_device my_device = {.name       = DEVICE_NAME,//此处指定的名字要和驱动中的名字匹配,song_rfid.id         = -1,.num_resources    = ARRAY_SIZE(my_resource),.resource    = my_resource,
};static int __init my_init(void)
{int ret=0;ret = platform_device_register(&my_device);
/*注册设备,即想虚拟总线上添加此设备
也可以用platform_device_alloc分配一个现成的设备和用platform_device_add添加到总线上
*/if (ret == 0) {printk("Register %s\n",DEVICE_NAME);} else {printk("Register  error.\n");}return ret;
}static void __exit my_exit(void)
{platform_device_unregister(&my_device);printk("Unregister %s\n",DEVICE_NAME);
}module_init(my_init);
module_exit(my_exit);MODULE_LICENSE("GPL");
MODULE_VERSION("1.5");

以下是驱动

/******************platfrom_drv.c***************************/
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>#define DRIVER_NAME "song_rfid"
static void __iomem *my_kv_base;//内核虚拟地址
static struct resource  *my_mem;static int  my_probe(struct platform_device *pdev)
{struct resource *res;int size;printk("driver find device : %s which can handle\n",DRIVER_NAME);res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
/*获取内存资源
如果先insmod设备内核就会把设备挂到平台总线上,之后在insmod驱动时,平台核心会遍历整个平台总线上的设备名字,找到和该驱动名字匹配的设备,
然后调用驱动的probe函数同时把该设备的struct platform_device当做参数pdev传递给probe函数,
这样驱动就可以使用platform_get_resource(pdev,,)获取设备的各个资源如果先insmod驱动,情况与上类似如果找到总线上某个设备可以和该驱动匹配,会成功调用驱动的probe
*/if (res == NULL) {printk("no memory resource specified\n");return -ENOENT;}size = (res->end - res->start) + 1;my_mem = request_mem_region(res->start, size, pdev->name);//向内核申请内存资源if (my_mem == NULL) {printk("failed to get memory region\n");return -ENOENT;}my_kv_base = ioremap(res->start, size);//得到虚拟地址,之后再去操作...return 0;
}static int  my_remove(struct platform_device *pdev)
{printk("driver found device : %s unpluged\n",DRIVER_NAME);return 0;
}static struct platform_driver my_driver = {.probe         = my_probe,.remove     = my_remove,.driver    = {.owner  = THIS_MODULE,.name    = DRIVER_NAME,//此处指定的名字要和设备中的名字匹配,song_rfid},
};static int __init my_init(void)
{printk("Register my_driver.\n");return platform_driver_register(&my_driver);
/*注册驱动
即向平台总线上添加一个驱动*/
}static void __exit my_exit(void)
{printk("Unregister my_driver.\n");platform_driver_unregister(&my_driver);
}module_init(my_init);
module_exit(my_exit);MODULE_LICENSE("GPL");

驱动代码my_exit()函数里面还需要释放资源-------2011-11-18
release_resource(my_mem);
kfree(my_mem) ;

[root@FriendlyARM plg]# insmod platform_dev.ko //插入设备
Register song_rfid
[root@FriendlyARM plg]# cat /proc/iomem   //insmo设备后就会在iomem有了登记
....
56000050-5600005f : song_rfid56000050-5600005f : song_rfid
...
[root@FriendlyARM plg]# insmod platform_drv.ko //插入驱动
Register my_driver.
driver find device : song_rfid which can handle[root@FriendlyARM plg]# ls /sys/bus/platform/devices/
dm9000             s3c2410-rtc        s3c2440-sdi        s3c24xx_uda134x.0
regulatory.0       s3c2410-spi.0      s3c2440-uart.0     soc-audio
s3c2410-iis        s3c2410-wdt        s3c2440-uart.1     song_rfid
s3c2410-lcd        s3c2440-i2c        s3c2440-uart.2
s3c2410-ohci       s3c2440-nand       s3c2440-usbgadget
[root@FriendlyARM plg]# ls /sys/bus/platform/drivers
dm9000           s3c2410-ohci     s3c2440-uart     song_rfid
s3c-i2c          s3c2410-rtc      s3c24xx-nand
s3c-sdi          s3c2410-spi      s3c24xx_uda134x
s3c2410-lcd      s3c2412-lcd      soc-audio
/*
另外,已经有许多按照平台模式写的驱动设备被编译进内核,在march-mini2440.c中
static struct platform_device *mini2440_devices[] __initdata = {&s3c_device_usb,&s3c_device_rtc,&s3c_device_lcd,&s3c_device_wdt,&s3c_device_i2c0,&s3c_device_spi0,&s3c_device_iis,&mini2440_device_eth,&s3c24xx_uda134x,&s3c_device_nand,&s3c_device_sdi,&s3c_device_usbgadget,
};
这些都是设备,大部分定义在arch/arm/plat-s3c24xx/Devs.c中
比如 Watchdog
static struct resource s3c_wdt_resource[] = {[0] = {.start = S3C24XX_PA_WATCHDOG,.end   = S3C24XX_PA_WATCHDOG + S3C24XX_SZ_WATCHDOG - 1,.flags = IORESOURCE_MEM,},[1] = {.start = IRQ_WDT,.end   = IRQ_WDT,.flags = IORESOURCE_IRQ,}};struct platform_device s3c_device_wdt = {.name          = "s3c2410-wdt",.id          = -1,.num_resources      = ARRAY_SIZE(s3c_wdt_resource),.resource      = s3c_wdt_resource,
};内核启动时会执行这个函数,在mach-mini2440.c中,将这些设备挂到平台总线上。platform_add_devices(mini2440_devices, ARRAY_SIZE(mini2440_devices));至于这些平台设备对应的驱动,则分散在drivers目录下各个文件中(如果有的话,但大部分都有)。如果有,则可以在make memuconfig时配置,或者编译进内核或者
编译成模块;如果没有则要自己写了。
*/

转载于:https://www.cnblogs.com/-song/archive/2011/10/25/3331942.html

rfid4-写成platform驱动相关推荐

  1. rfid3-micro2440,linux2.6.32.2,写成misc驱动

    接上文的进度,将keil下已经成功的读卡程序写成linux驱动的形式 采用misc来写比较方便简单,仅是为了方便测试,好多都在驱动中实现. 主文件是rfid.c #include "rc52 ...

  2. rfid5-写成platform驱动

    上文platform驱动虽然可用,但内层要要写misc驱动,所以不使用设备提供的资源会更加简便.可以如下改一下 Makefile,同上文 ifneq ($(KERNELRELEASE),) obj-m ...

  3. rfid6-写成platform驱动

    将前面rfid的misc挂到platform平台上,因为misc驱动都有了,所以很简单,代码复制过来就好了 设备侧就用上文的那个就好 /******************platfrom_dev.c ...

  4. linux platform 驱动模型分析

    一. 概述     platform设备和驱动与linux设备模型密切相关.platform在linux设备模型中,其实就是一种虚拟总线没有对应的硬件结构.它的主要作用就是管理系统的外设资源,比如io ...

  5. platform驱动模型使用总结

    platform平台总线驱动的编写主要分为两个部分: 一个是platform_device部分,主要是提供设备本身对驱动处理所要求的参数. 另一个是platform_driver部分,主要是利用pla ...

  6. 驱动程序开发:无设备树和有设备树的platform驱动

    1.Linux 驱动的分离与分层   对与对IO进行最简单的读写操作,无需考虑太多的怎么使它重用性强,而像I2C. SPI.LCD 等这些复杂外设的驱动,Linux 系统要考虑到驱动的可重用性,因此提 ...

  7. platform驱动

    目录 1.Linux驱动的分离与分层 1)驱动的分隔与分离 2)驱动的分层 2.platform平台驱动模型简介 1)platform总线 2)platform驱动 3)platform设备 3.试验 ...

  8. <Linux开发>驱动开发 -之-platform 驱动

    <Linux开发>驱动开发 -之-platform 驱动 交叉编译环境搭建: <Linux开发> linux开发工具-之-交叉编译环境搭建 uboot移植可参考以下: < ...

  9. Linux dts设备树和platform驱动详解

    概念 小麦大叔 2019-05-06 22:56:31 12603 收藏 135 什么是设备树 dts(device tree)? 设备树(Device Tree)是描述计算机的特定硬件设备信息的数据 ...

最新文章

  1. golang 字符串排序_Golang操作数据库Redis
  2. git 使用及常用命令
  3. 从滴滴出行业务中台实践聊聊如何构建大中台架构
  4. pbl和sbl_综合运用PBL和SBL教学法提高眼科带教质量的探索
  5. 梯度下降法、最速下降法
  6. 微信小程序 body属性的问题
  7. 51单片机扩展io口实验c语言,【51单片机】普通I/O口模拟SPI口C语言程序
  8. 王者荣耀女性机器人面世;深圳中院受理金立破产案;Firefox 64 发布 | 极客头条...
  9. linux 自带多路径工具,RHEL6使用系统自带多路径软件配置多路径
  10. 单片机C语言,从小白到菜鸟进阶教程(超详细代码解读)
  11. Win 10 Edge不能上网,微软商店打不开的问题
  12. php 表格内边距,CSS 内边距
  13. 我的tudo日记2(关于babel和postcss)
  14. 关于 蓝天显卡 异形卡 的改inf文件上驱动说明
  15. CHERRY 键盘 alt 组合键失灵或开始菜单键失灵
  16. java 时间减法(结束时间-开始时间,时分秒)
  17. 用Navicat连接MySQL的安装及配置
  18. Collectors
  19. SSM框架的介绍与搭建
  20. 三大统计软件:SAS、Stata与SPSS比较

热门文章

  1. 使用Leopard Jdbc
  2. 疯狂ios讲义疯狂连载之实现游戏视图控制器
  3. 大亚DP607超级密码,破解方法
  4. 13.7 手柄(Handle)
  5. oracle trace文件解读
  6. category android:name=android.intent.category.DEFAULT / 惹的祸
  7. Android ListView滑动后背景变黑
  8. Android studio 查找文件在电脑路径位置
  9. 【Redis】12.Redis删除策略
  10. C++实现两个矩阵相乘