码迷,mamicode.com
首页 > 系统相关 > 详细

linux字符设备-自动创建设备号和设备节点

时间:2016-02-22 00:03:59      阅读:524      评论:0      收藏:0      [点我收藏+]

标签:

Linux字符设备-自动创建设备号和设备节点

 

先写一个自动分配字符设备号和设备节点的例子及APP

手动安装步骤:

Insmod my_char_dev.ko

 技术分享

不需要再安装设备节点

 

然后是测试app

./my_char_dev_app 1

 

 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include <linux/io.h>
 4 #include <linux/fs.h>
 5 #include <asm/device.h>  //下面这三个头文件是由于动态创建需要加的
 6 #include <linux/device.h>
 7 #include <linux/cdev.h>
 8 #include "my_cdev.h"
 9 struct cdev cdev;
10 dev_t devno;//这里是动态分配设备号和动态创建设备结点需要用到的
11 struct class *cdev_class;
12 
13 int my_cdev_open(struct inode *node,struct file *filp)
14 {
15     printk("my_cdev_open sucess!\n");
16     return 0;
17 }
18 
19 long my_cdev_ioctl(struct file *filp ,unsigned int cmd ,unsigned long arg)
20 {
21     switch(cmd)
22     {
23         case LED_ON:
24             printk("LED_ON is set!\n");
25             return 0;
26         case LED_OFF:
27             printk("LED_OFF is set!\n");
28             return 0;
29         default :
30             return -EINVAL;
31     }
32 }
33 
34 struct file_operations my_cdev_fops=
35 {
36     .open = my_cdev_open,
37     .unlocked_ioctl = my_cdev_ioctl,
38 
39 };
40 
41 static int my_cdev_init(void)
42 {
43     int ret;
44     /**动态分配设备号*/
45     ret = alloc_chrdev_region(&devno,0,1,"my_chardev");
46     if(ret)
47     {
48         printk("alloc_chrdev_region fail!\n");
49         unregister_chrdev_region(devno,1);
50         return ret;
51     }
52     else
53     {
54         printk("alloc_chrdev_region sucess!\n");
55     }
56     /**描述结构初始化*/
57     cdev_init(&cdev,&my_cdev_fops);
58     /**描述结构注册*/
59     ret = cdev_add(&cdev,devno,1);
60     if(ret)
61     {
62         printk("cdev add fail.\n");
63         unregister_chrdev_region(devno,1);
64         return ret;
65     }
66     else
67     {
68         printk("cdev add sucess!\n");
69     }
70 
71     cdev_class = class_create(THIS_MODULE,"my_chardev");
72     if(IS_ERR(cdev_class))
73     {
74         printk("Create class fail!\n");
75         unregister_chrdev_region(devno,1);
76         return -1;
77     }
78     else
79     {
80         printk("Create class sucess!\n");
81     }
82 
83     device_create(cdev_class,NULL,devno,0,"my_chardev");
84     
85     return 0;
86 }
87 static void my_cdev_exit(void)
88 {
89     device_destroy(cdev_class,devno);
90     class_destroy(cdev_class);
91     cdev_del(&cdev);
92     unregister_chrdev_region(devno,1);
93     printk("my_cdev_exit sucess!\n");
94 }
95 module_init(my_cdev_init);
96 module_exit(my_cdev_exit);
97 MODULE_LICENSE("GPL");
98 MODULE_AUTHOR("YEFEI");
99 MODULE_DESCRIPTION("YEFEI Driver");

 

1 #ifndef __MY_CDEV_H__
2 #define __MY_CDEV_H__
3 
4 #define LED_MAGIC ‘L‘
5 #define LED_ON _IO(LED_MAGIC,0)
6 #define LED_OFF _IO(LED_MAGIC,1)
7 
8 #endif

 

 1 #include <sys/stat.h>
 2 #include <sys/types.h>
 3 #include <sys/ioctl.h>
 4 #include <fcntl.h>
 5 #include <stdio.h>
 6 #include "my_cdev.h"
 7 
 8 int main(int argc,char *argv[])
 9 {
10     int fd;
11     int cmd;
12     if(argc < 2)
13     {
14         printf("Please enter secend param!\n");
15         return 0;
16     }
17     cmd = atoi(argv[1]);
18     fd = open("/dev/my_chardev",O_RDWR);
19     if(fd < 0)
20     {
21         printf("Open dev/my_chardev fail!\n");
22         close(fd);
23         return 0;
24     }
25     switch(cmd)
26     {
27         case 1:
28             ioctl(fd,LED_ON);
29             break;
30         case 2:
31             ioctl(fd,LED_OFF);
32             break;
33         default:
34             break;
35     }
36     close(fd);
37     return 0;
38 }

 

1 obj-m := my_char_dev.o
2 KDIR := /home/win/dn377org/trunk/bcm7252/linux/
3 all:
4     make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm
5 clean:
6     rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order

 

linux字符设备-自动创建设备号和设备节点

标签:

原文地址:http://www.cnblogs.com/love-linux/p/5205751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!