字符驱动之操作LED灯(一):大体框架

 1 头文件:
 2 #include <linux/module.h>
 3 #include <linux/kernel.h>
 4 #include <linux/fs.h>
 5 #include <linux/init.h>
 6 #include <linux/delay.h>
 7 #include <asm/uaccess.h>
 8 #include <asm/irq.h>
 9 #include <asm/io.h>
10 #include <asm/arch/regs-gpio.h>
11 #include <asm/hardware.h>
12 
13 定义变量
14 int major;
15 static struct class *firstdrv_class;
16 static struct class_device    *firstdrv_class_dev;
17 
18 
19 
20 
21 static int first_drv_open(struct inode *inode,struct file *file)
22 {
23     
24 
25     return 0;
26 }
27 
28 
29 static ssize_t first_drv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
30 {
31     printk("");
32     return 0;
33 }
34 
35 static int first_drv_init(void)
36 {
37     major = register_chrdev(0,"first_drv",&first_drv_fops);
38     //告诉内核,并填充驱动,把数组填充进去。但是系统信息未生成
39     
40     firstdrv_class = class_create(THIS_MODULE,"firstdrv");
41     //在sys下新建class,名叫firstdrv_class。
42     
43     firstdrv_class_dev = class_device(firstdrv_class,NULL,MKDEV(major,0),NULL,"xyz");
44     //在此类下面新建设备节点,设备号为major,节点叫做xyz。(/dev/xyz)
45 }
46 
47 在入口中新建,需要在出口中删除。
48 static void first_drv_exit(void)
49 {
50     unregister_chrdev(major,"first_drv");
51     class_device_unregister(firstdrv_class_dev);
52     class_destroy(firstdrv_class);
53 }
54 
55 结尾:
56 module_init(first_drv_init);
57 module_exit(first_drv_exit);
58 module_license("GPL");

 

posted @ 2018-01-22 19:08  梦提三尺剑  阅读(170)  评论(0编辑  收藏  举报