errakeshpillai
Full Member level 5
- Joined
- Oct 17, 2011
- Messages
- 255
- Helped
- 64
- Reputation
- 128
- Reaction score
- 59
- Trophy points
- 1,328
- Location
- Chennai, India
- Activity points
- 2,738
Hi all, I am making a small test device driver which will allocate 64 bytes of memory and allow to read/write the memory bytes. When I load the device driver using insmod, I'm getting the /sys/class and /proc entries properly, but the /dev entry is missing.
I googled about this but all the answers I found suggest the use of mknod to create the /dev node ourself. My requirement is that the /dev node also should be created automatically when we load the driver using insmod.
I googled about this but all the answers I found suggest the use of mknod to create the /dev node ourself. My requirement is that the /dev node also should be created automatically when we load the driver using insmod.
Code:
static char *g_mem_buff;
static struct mutex rw_lock;
static struct proc_dir_entry *proc_mem,*config_mem;
static int dev_major,dev_init_status;
static struct class *cl_mem;
static struct proc_dir_entry *mem_proc;
static int mem_driver_init(void);
static void mem_driver_exit(void);
static int mem_driver_open(struct inode *in, struct file *fp);
static int mem_driver_release(struct inode *in, struct file *fp);
static int __init mem_driver_init(void)
{
struct device *mem_dev;
return_t ret;
printk(KERN_INFO "Initializing Driver.\n");
/* Register the driver. */
printk(KERN_INFO "Registering Memory Driver.\n");
dev_major = register_chrdev (0,DEV_ENTRY,&mem_fops);
if(dev_major < 0){
printk(KERN_INFO "Driver registeration failed.\n");
return dev_major;
}
printk(KERN_NOTICE "My driver registered successfully with major number: %d\n",dev_major);
/* Reserving memory for the buffer. */
g_mem_buff = kmalloc(BUFF_SIZE,GFP_KERNEL);
if(!g_mem_buff){
ret = EKMALLOC;
goto mem_failed;
}
memset(g_mem_buff,0,BUFF_SIZE);
printk(KERN_INFO "Mem Driver: Memory allocated and cleared.\n");
/* Mutex Initialization. */
mutex_init(&rw_lock);
/* creating /sys/class interface */
cl_mem = class_create(THIS_MODULE,CLASS_NAME);
if(cl_mem == NULL){
printk(KERN_NOTICE "Class creation failed.\n");
ret = ECLASS_CREATE;
goto class_failed;
}
printk(KERN_EMERG "Class created successfully.\n");
/* Creating /dev interface. */
mem_dev = device_create(cl_mem, NULL,MKDEV(dev_major,0), DEV_ENTRY);
if(mem_dev == NULL){
printk(KERN_NOTICE "Creating device node failed.\n");
ret = EDEV_CREATE;
goto dev_failed;
}
printk(KERN_EMERG "Dev node created successfully.\n");
/* Creating /proc interface. */
proc_ipmac = proc_mkdir("Dev_IPMAC",NULL);
if(proc_ipmac == NULL){
printk(KERN_NOTICE "Creating proc entry failed.\n");
ret = EPROC_DIR_CREATE;
goto proc_dir_failed;
}
mem_proc = create_proc_entry(PROC_ENTRY,0666,NULL);
if(mem_proc == NULL){
printk(KERN_NOTICE "My driver: Config proc failed.\n");
ret = EPROC_CREATE;
goto proc_entry_failed;
}
printk(KERN_INFO "My Driver: Proc init success.\n");
mem_proc->read_proc = mem_read_proc;
mem_proc->write_proc = mem_write_proc;
dev_init_status = SUCCESS;
return dev_init_status;
proc_entry_failed:
device_destroy(cl_mem,MKDEV(dev_major,0));
dev_failed:
class_destroy(cl_mem);
class_failed:
kfree(g_mem_buff);
mem_alloc_failed:
unregister_chrdev(dev_major,DEV_ENTRY);
printk("Mem Driver: Driver unregistered.\n");
dev_init_status = ret;
return dev_init_status;
}