全部博文(61)
分类: LINUX
2008-11-16 21:40:18
register_chrdev_region(dev_t dev, unsigned count, const char *name)
alloc_chrdev_region(dev_t dev, unsigned baseminor, unsigned count, const char *name);
这两个函数都是用来注册一个设备号的 第一个用于注册一个已经确定的设备号, 而第二个是动态申请一个设备号, 这样就不会造成设备号重复的冲突。
下面介绍一个用到的结构体吧
struct cdev {
struct kobject kobj;
struct module *owner; //所属模块
const struct file_operations *ops;
//文件操作结构,在写驱动时,其结构体内的大部分函数要被实现
struct list_head list;
dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号
unsigned int count;
};
可以使用如下宏调用来获得主、次设备号:
MAJOR(dev_t dev)
MINOR(dev_t dev)
MKDEV(int major,int minor) //通过主次设备号来生成dev_t
以上宏调用在内核源码中如此定义:
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
//(1<<20 -1) 此操作后,MINORMASK宏的低20位为1,高12位为0
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
struct file_operations fops;这个结构体主要 就是对设备操作了
由于成员太多了 这里就不详细介绍了;
my_cdev = alloc_cdev(); //动态申请一个cdev存贮空间
cdev_put(struct cdev *p);//释放一个cdev
cdev_add(struct cdev *, dev_t unsigned);//注销设备,通过发生在驱动模块的加载函数中
cdev_del(struct cdev *)//注销设备,通常发生在模块的卸载函数中
unsigned_chrdev_region(dev_t dev, unsigned count );
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 MODULE_LICENSE("Dual BSD/GPL");
8 static struct file_operations fops = {NULL};
9 static struct cdev *my_cdev = {NULL};
10 static dev_t dev;
11 static int testdev_init(void)
12
13 {
14 static dev_t major_no;
16 int res;
17
18 alloc_chrdev_region(major_no, 1,1,"cdev_login" );
19
20 if( major_no < 0)
21 {
22 printk("the registerchar dev region is error\n");
23 return -1;
24 }
25
26 dev = MKDEV(major_no, 1);
27 my_cdev = cdev_alloc();
28
29 cdev_init(my_cdev,&fops);
30 my_cdev->owner = THIS_MODULE;
31
32 res = cdev_add(my_cdev, dev, 1);
33 if( res )
34 printk("the my_cdev add is error\n");
35 printk("the cdev register success\n");
36 return 0;
37 }
38
39 static int testdev_exit(void)
40 {
41 cdev_del(my_cdev);
unsigned_chrdev_region(dev, 1);
42 }
43 module_init(testdev_init);
44 module_exit(testdev_exit);