#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/kdev_t.h>
static int major_num,minor_num;
module_param(major_num,int,S_IRUSR);
module_param(minor_num,int,S_IRUSR);
static int hello_init(void)
{
//printk(“hello\r\n”);
dev_t dev_num;
int ret;
if(major_num)
{
dev_num=MKDEV(major_num,minor_num);
ret=register_chrdev_region(dev_num,1,"wlw");
if(ret<0)
{
printk("fail to register\r\n");
return 0;
}
printk("ok to register\r\n");
}
else
{
ret=alloc_chrdev_region(&dev_num,0,1,"wlw1");
if(ret<0)
{
printk("fail to register\r\n");
return 0;
}
printk("alloc to ok\r\n");
major_num=MAJOR(dev_num);
minor_num=MINOR(dev_num);
printk("major=%d\n",major_num);
printk("minor=%d\n",minor_num);
}
//printk("a=%d\r\n",a);
return 0;
}
static void hello_exit(void)
{
printk(“exit\r\n”);
unregister_chrdev_region(MKDEV(major_num,minor_num),1);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE(“GPL”);