驅動程序分層概念

  寫一下我的對驅動分層的理解,簡單的說將驅動分層也就是將驅動中硬件操做的代碼和軟件處理的代碼分爲兩部分,軟件相關的代碼較穩定,硬件相關的代碼可能因硬件的變更而作出相應調整。這樣作的好處是便於編寫大型的驅動,比如咱們的編程中寫的函數將功能模塊化,模塊化的好處這裏就不說了。node

  以JZ2440開發板的點亮led的操做作一下總結,驅動分爲led_dev.c,led_drv兩部分,另外還有一個測試程序led_test.clinux

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:測試程序this

  對測試程序不作過多解釋了spa

下面分別附上3文件的個源碼:code

 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 }
相關文章
相關標籤/搜索