驱动程序分层概念

  写一下个人对驱动分层的理解,简单的说将驱动分层也就是将驱动中硬件操作的代码和软件处理的代码分为两部分,软件相关的代码较稳定,硬件相关的代码可能因硬件的变动而做出相应调整。这样做的好处是便于编写大型的驱动,好比我们的编程中写的函数将功能模块化,模块化的好处这里就不说了。

  以JZ2440开发板的点亮led的操作做一下总结,驱动分为led_dev.c,led_drv两部分,另外还有一个测试程序led_test.c

led_dev:硬件资源

  led_dev是和硬件设备相关的代码,其功能是包含、上报可供调度的硬件资源,完成以下几项

  1. 声明一个platform_device类型的结构体
    1. 该结构体中含有name、id、resource等内容
    2. dev和drv含有相同的.name(.name相同时dev和drv才能匹配)
  2. init函数注册上述结构体
  3. exit函数卸载上述结构体
  4. 修饰入口、出口函数

led_drv:硬件操作

  led_drv是负责处理硬件操作的软件,即为软件相关的代码,其层次和led_dev相似,只不过是多了一些硬件操作的函数

  1. 声明platform_driver类型的结构体
    1. 结构体中含有probe、remove函数和driver结构体
    2. driver结构体中含有和dev同名的.name
    3. probe为注册设备的函数
    4. remove为卸载设备的函数
  2. init函数注册上述结构体
  3. exit函数卸载上述结构体
  4. 修饰入口、出口函数

led_test:测试程序

  对测试程序不做过多解释了

下面分别附上3文件的个源码:

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

 

posted @ 2015-10-10 10:40  丿踏遍青山人未老  阅读(947)  评论(0编辑  收藏  举报