Linux-2.6驱动程序分层分离概念
下面以一个按键的实验作为驱动分离时间简单学习:
- #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>
- /* 分配/设置/注册一个platform_device */
- static struct resource led_resource[] = {
- [] = {
- .start = 0x56000050,
- .end = 0x56000050 + - ,
- .flags = IORESOURCE_MEM,
- },
- [] = {
- .start = ,
- .end = ,
- .flags = IORESOURCE_IRQ,
- }
- };
- static void led_release(struct device * dev)
- {
- }
- static struct platform_device led_dev = {
- .name = "myled",
- .id = -,
- .num_resources = ARRAY_SIZE(led_resource),
- .resource = led_resource,
- .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);
- }
- module_init(led_dev_init);
- module_exit(led_dev_exit);
- MODULE_LICENSE("GPL");
led_dev.c
- /* 分配/设置/注册一个platform_driver */
- #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 int major;
- static struct class *cls;
- 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)
- {
- //printk("first_drv_open\n");
- /* 配置为输出 */
- *gpio_con &= ~(0x3<<(pin*));
- *gpio_con |= (0x1<<(pin*));
- return ;
- }
- static ssize_t led_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 == )
- {
- // 点灯
- *gpio_dat &= ~(<<pin);
- }
- else
- {
- // 灭灯
- *gpio_dat |= (<<pin);
- }
- return ;
- }
- static struct file_operations led_fops = {
- .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
- .open = led_open,
- .write = led_write,
- };
- static int led_probe(struct platform_device *pdev)
- {
- struct resource *res;
- /* 根据platform_device的资源进行ioremap */
- 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;
- /* 注册字符设备驱动程序 */
- printk("led_probe, found led\n");
- major = register_chrdev(, "myled", &led_fops);
- cls = class_create(THIS_MODULE, "myled");
- class_device_create(cls, NULL, MKDEV(major, ), NULL, "led"); /* /dev/led */
- return ;
- }
- static int led_remove(struct platform_device *pdev)
- {
- /* 卸载字符设备驱动程序 */
- /* iounmap */
- printk("led_remove, remove led\n");
- class_device_destroy(cls, MKDEV(major, ));
- class_destroy(cls);
- unregister_chrdev(major, "myled");
- iounmap(gpio_con);
- return ;
- }
- struct platform_driver led_drv = {
- .probe = led_probe,
- .remove = led_remove,
- .driver = {
- .name = "myled",
- }
- };
- 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");
led_drv.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- /* led_test on
- * led_test off
- */
- int main(int argc, char **argv)
- {
- int fd;
- int val = ;
- fd = open("/dev/led", 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 ;
- }
led_test.c
Linux-2.6驱动程序分层分离概念的更多相关文章
- 驱动程序分层分离概念_总线驱动设备模型_P
分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...
- 嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
平台设备驱动: 包含BUS(总线).DEVICE.DRIVER. DEVICE:硬件相关的代码 DRIVER:比较稳定的代码 BUS有一个driver链表和device链表. ①把device放入bu ...
- linux驱动分离分层的概念
这个分离分层的概念和输入子系统有点像,但不是完全一样的.为什么会再弄一个这个模型出来我也没有搞懂,现在我的学习还停留在把知识学懂的层面上.至于为什么会产生这种知识,现在我还无从解释,还需时日成长. 这 ...
- 【linux】驱动-5-驱动框架分层分离&实战
目录 前言 5. 分离分层 5.1 回顾-设备驱动实现 5.2 分离分层 5.3 设备 5.4 驱动 5.5 系统,模块 5.6 Makefile 参考: 前言 5. 分离分层 本章节记录实现LED驱 ...
- python selenium自动化测试之路(1)--分层测试概念、selenium工具介绍
1.分层自动化测试概念 传统的自动化市场更关注产品UI层的自动化测试,而分层的自动化测试倡导产品开发的不同阶段都需要自动化测试 大多公司与研发团队其实是忽略了单元测试与集成测试阶段的自动化测试工作,所 ...
- linux设备驱动的分层设计思想--input子系统及RTC
转自:linux设备驱动的分层设计思想 宋宝华 http://blog.csdn.net/21cnbao/article/details/5615493 1.1 设备驱动核心层和例化 在面向对象的程序 ...
- Linux DM9000网卡驱动程序完全分析
Linux DM9000网卡驱动程序完全分析http://blog.csdn.net/ypoflyer/article/details/6209922
- Linux下触摸屏驱动程序分析
[摘要: 本文以linux3.5--Exynos4412仄台,剖析触摸屏驱动焦点内容.Linux下触摸屏驱动(以ft5x06_ts为例)须要懂得以下学问: 1. I2C协定 2. Exynos4412 ...
- Linux学习之CentOS(三)--初识linux的文件系统以及用户组等概念
Linux学习之CentOS(三)--初识linux的文件系统以及用户组等概念 进入到了Linux学习之CentOS第三篇了,这篇文章主要记录下对linux文件系统的初步认识,以及用户组.用户权限.文 ...
随机推荐
- 【转】用JS完成手机短信验证按键点击事件
原地址:https://gitee.com/RainVanilla/codes/i7jske4wdogvnb0apmfx571 试了一下,效果还可以,留着备用! <!DOCTYPE html&g ...
- 0 Linux下Java使用ProcessBuilder执行命令与直接Bash执行命令之间的不同(环境变量方面)
0 问题发生 xiaojietest.java package tasks; import java.io.BufferedReader; import java.io.BufferedWriter; ...
- easyUI扩展组件
$.parser.plugins.push("aa"); //注册扩展组件 $.fn.aa= function (options, param) {//定义扩展组件 //当opti ...
- 用path动画绘制水波纹
用path动画绘制水波纹 效果 源码 // // ViewController.m // PathAnimation // // Created by YouXianMing on 15/7/3. / ...
- 用block将UIAlertView与UIActionSheet统一起来
用block将UIAlertView与UIActionSheet统一起来 效果 1. 将代理方法的实例对象方法转换成了类方法使用 2. 要注意单例block不要长期持有,用完就释放掉 源码 https ...
- windows:nginx配置http、https反向代理
一.下载 Windows 版本的 nginx nnginx下载:http://nginx.org/en/download.html 推荐稳定版本.下载完成后,解压得到 nginx-1.14.0 ,我把 ...
- 剑指offer 08跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). java版本: public class Solution { public s ...
- 解决windows10下无法安装.net framework 3.5,错误代码0x800F081F
1.下载 NET Framework 3.5的安装包netfx3.cab: http://download.windowsupdate.com/d/msdownload/update/software ...
- 如何创建一个Quartz.NET的工作,需要注射autofac
问题: 使用 Quartz.Net 做定时任务时,实现IJob对象的服务,Autofac不会自动注入,使用构造函数会直接出现异常,无法执行Execute方法. 解决方式 方法一: 使用 Autofac ...
- (1)访问控制 (2)final关键字 (3)对象创建的过程 (4)多态
1.访问控制(笔试题)1.1 常用的访问控制符 public - 公有的 protected - 保护的 啥也不写 - 默认的 private - 私有的 1.2 访问控制符的比较 访问控制符 访问权 ...