下面以一个按键的实验作为驱动分离时间简单学习:

  1. #include <linux/module.h>
  2. #include <linux/version.h>
  3.  
  4. #include <linux/init.h>
  5.  
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/list.h>
  10. #include <linux/timer.h>
  11. #include <linux/init.h>
  12. #include <linux/serial_core.h>
  13. #include <linux/platform_device.h>
  14.  
  15. /* 分配/设置/注册一个platform_device */
  16.  
  17. static struct resource led_resource[] = {
  18. [] = {
  19. .start = 0x56000050,
  20. .end = 0x56000050 + - ,
  21. .flags = IORESOURCE_MEM,
  22. },
  23. [] = {
  24. .start = ,
  25. .end = ,
  26. .flags = IORESOURCE_IRQ,
  27. }
  28.  
  29. };
  30.  
  31. static void led_release(struct device * dev)
  32. {
  33. }
  34.  
  35. static struct platform_device led_dev = {
  36. .name = "myled",
  37. .id = -,
  38. .num_resources = ARRAY_SIZE(led_resource),
  39. .resource = led_resource,
  40. .dev = {
  41. .release = led_release,
  42. },
  43. };
  44.  
  45. static int led_dev_init(void)
  46. {
  47. platform_device_register(&led_dev);
  48. return ;
  49. }
  50.  
  51. static void led_dev_exit(void)
  52. {
  53. platform_device_unregister(&led_dev);
  54. }
  55.  
  56. module_init(led_dev_init);
  57. module_exit(led_dev_exit);
  58.  
  59. MODULE_LICENSE("GPL");

led_dev.c

  1. /* 分配/设置/注册一个platform_driver */
  2.  
  3. #include <linux/module.h>
  4. #include <linux/version.h>
  5.  
  6. #include <linux/init.h>
  7. #include <linux/fs.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/sched.h>
  11. #include <linux/pm.h>
  12. #include <linux/sysctl.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/input.h>
  17. #include <linux/irq.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/io.h>
  20.  
  21. static int major;
  22.  
  23. static struct class *cls;
  24. static volatile unsigned long *gpio_con;
  25. static volatile unsigned long *gpio_dat;
  26. static int pin;
  27.  
  28. static int led_open(struct inode *inode, struct file *file)
  29. {
  30. //printk("first_drv_open\n");
  31. /* 配置为输出 */
  32. *gpio_con &= ~(0x3<<(pin*));
  33. *gpio_con |= (0x1<<(pin*));
  34. return ;
  35. }
  36.  
  37. static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  38. {
  39. int val;
  40.  
  41. //printk("first_drv_write\n");
  42.  
  43. copy_from_user(&val, buf, count); // copy_to_user();
  44.  
  45. if (val == )
  46. {
  47. // 点灯
  48. *gpio_dat &= ~(<<pin);
  49. }
  50. else
  51. {
  52. // 灭灯
  53. *gpio_dat |= (<<pin);
  54. }
  55.  
  56. return ;
  57. }
  58.  
  59. static struct file_operations led_fops = {
  60. .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  61. .open = led_open,
  62. .write = led_write,
  63. };
  64.  
  65. static int led_probe(struct platform_device *pdev)
  66. {
  67. struct resource *res;
  68.  
  69. /* 根据platform_device的资源进行ioremap */
  70. res = platform_get_resource(pdev, IORESOURCE_MEM, );
  71. gpio_con = ioremap(res->start, res->end - res->start + );
  72. gpio_dat = gpio_con + ;
  73.  
  74. res = platform_get_resource(pdev, IORESOURCE_IRQ, );
  75. pin = res->start;
  76.  
  77. /* 注册字符设备驱动程序 */
  78.  
  79. printk("led_probe, found led\n");
  80.  
  81. major = register_chrdev(, "myled", &led_fops);
  82.  
  83. cls = class_create(THIS_MODULE, "myled");
  84.  
  85. class_device_create(cls, NULL, MKDEV(major, ), NULL, "led"); /* /dev/led */
  86.  
  87. return ;
  88. }
  89.  
  90. static int led_remove(struct platform_device *pdev)
  91. {
  92. /* 卸载字符设备驱动程序 */
  93. /* iounmap */
  94. printk("led_remove, remove led\n");
  95.  
  96. class_device_destroy(cls, MKDEV(major, ));
  97. class_destroy(cls);
  98. unregister_chrdev(major, "myled");
  99. iounmap(gpio_con);
  100.  
  101. return ;
  102. }
  103.  
  104. struct platform_driver led_drv = {
  105. .probe = led_probe,
  106. .remove = led_remove,
  107. .driver = {
  108. .name = "myled",
  109. }
  110. };
  111.  
  112. static int led_drv_init(void)
  113. {
  114. platform_driver_register(&led_drv);
  115. return ;
  116. }
  117.  
  118. static void led_drv_exit(void)
  119. {
  120. platform_driver_unregister(&led_drv);
  121. }
  122.  
  123. module_init(led_drv_init);
  124. module_exit(led_drv_exit);
  125.  
  126. MODULE_LICENSE("GPL");

led_drv.c

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5.  
  6. /* led_test on
  7. * led_test off
  8. */
  9. int main(int argc, char **argv)
  10. {
  11. int fd;
  12. int val = ;
  13. fd = open("/dev/led", O_RDWR);
  14. if (fd < )
  15. {
  16. printf("can't open!\n");
  17. }
  18. if (argc != )
  19. {
  20. printf("Usage :\n");
  21. printf("%s <on|off>\n", argv[]);
  22. return ;
  23. }
  24.  
  25. if (strcmp(argv[], "on") == )
  26. {
  27. val = ;
  28. }
  29. else
  30. {
  31. val = ;
  32. }
  33.  
  34. write(fd, &val, );
  35. return ;
  36. }

led_test.c

Linux-2.6驱动程序分层分离概念的更多相关文章

  1. 驱动程序分层分离概念_总线驱动设备模型_P

    分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...

  2. 嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动

    平台设备驱动: 包含BUS(总线).DEVICE.DRIVER. DEVICE:硬件相关的代码 DRIVER:比较稳定的代码 BUS有一个driver链表和device链表. ①把device放入bu ...

  3. linux驱动分离分层的概念

    这个分离分层的概念和输入子系统有点像,但不是完全一样的.为什么会再弄一个这个模型出来我也没有搞懂,现在我的学习还停留在把知识学懂的层面上.至于为什么会产生这种知识,现在我还无从解释,还需时日成长. 这 ...

  4. 【linux】驱动-5-驱动框架分层分离&实战

    目录 前言 5. 分离分层 5.1 回顾-设备驱动实现 5.2 分离分层 5.3 设备 5.4 驱动 5.5 系统,模块 5.6 Makefile 参考: 前言 5. 分离分层 本章节记录实现LED驱 ...

  5. python selenium自动化测试之路(1)--分层测试概念、selenium工具介绍

    1.分层自动化测试概念 传统的自动化市场更关注产品UI层的自动化测试,而分层的自动化测试倡导产品开发的不同阶段都需要自动化测试 大多公司与研发团队其实是忽略了单元测试与集成测试阶段的自动化测试工作,所 ...

  6. linux设备驱动的分层设计思想--input子系统及RTC

    转自:linux设备驱动的分层设计思想 宋宝华 http://blog.csdn.net/21cnbao/article/details/5615493 1.1 设备驱动核心层和例化 在面向对象的程序 ...

  7. Linux DM9000网卡驱动程序完全分析

    Linux DM9000网卡驱动程序完全分析http://blog.csdn.net/ypoflyer/article/details/6209922

  8. Linux下触摸屏驱动程序分析

    [摘要: 本文以linux3.5--Exynos4412仄台,剖析触摸屏驱动焦点内容.Linux下触摸屏驱动(以ft5x06_ts为例)须要懂得以下学问: 1. I2C协定 2. Exynos4412 ...

  9. Linux学习之CentOS(三)--初识linux的文件系统以及用户组等概念

    Linux学习之CentOS(三)--初识linux的文件系统以及用户组等概念 进入到了Linux学习之CentOS第三篇了,这篇文章主要记录下对linux文件系统的初步认识,以及用户组.用户权限.文 ...

随机推荐

  1. 【转】用JS完成手机短信验证按键点击事件

    原地址:https://gitee.com/RainVanilla/codes/i7jske4wdogvnb0apmfx571 试了一下,效果还可以,留着备用! <!DOCTYPE html&g ...

  2. 0 Linux下Java使用ProcessBuilder执行命令与直接Bash执行命令之间的不同(环境变量方面)

    0 问题发生 xiaojietest.java package tasks; import java.io.BufferedReader; import java.io.BufferedWriter; ...

  3. easyUI扩展组件

    $.parser.plugins.push("aa"); //注册扩展组件 $.fn.aa= function (options, param) {//定义扩展组件 //当opti ...

  4. 用path动画绘制水波纹

    用path动画绘制水波纹 效果 源码 // // ViewController.m // PathAnimation // // Created by YouXianMing on 15/7/3. / ...

  5. 用block将UIAlertView与UIActionSheet统一起来

    用block将UIAlertView与UIActionSheet统一起来 效果 1. 将代理方法的实例对象方法转换成了类方法使用 2. 要注意单例block不要长期持有,用完就释放掉 源码 https ...

  6. windows:nginx配置http、https反向代理

    一.下载 Windows 版本的 nginx nnginx下载:http://nginx.org/en/download.html 推荐稳定版本.下载完成后,解压得到 nginx-1.14.0 ,我把 ...

  7. 剑指offer 08跳台阶

    一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). java版本: public class Solution { public s ...

  8. 解决windows10下无法安装.net framework 3.5,错误代码0x800F081F

    1.下载 NET Framework 3.5的安装包netfx3.cab: http://download.windowsupdate.com/d/msdownload/update/software ...

  9. 如何创建一个Quartz.NET的工作,需要注射autofac

    问题: 使用 Quartz.Net 做定时任务时,实现IJob对象的服务,Autofac不会自动注入,使用构造函数会直接出现异常,无法执行Execute方法. 解决方式 方法一: 使用 Autofac ...

  10. (1)访问控制 (2)final关键字 (3)对象创建的过程 (4)多态

    1.访问控制(笔试题)1.1 常用的访问控制符 public - 公有的 protected - 保护的 啥也不写 - 默认的 private - 私有的 1.2 访问控制符的比较 访问控制符 访问权 ...