Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Linux 2.6.25 /dev entry problem

Status
Not open for further replies.

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.

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;
}
 

Thanx for answering..

I have used the same method and tried a lot of "googling" about this, but in vain.
I have to use mknod, don't know why..?

I have 2 drivers, one which will successfully creating the dev entry, but my driver with the similar flow of coding and functions is not creating the dev entry.
 

What kind of error with insmod. So how to solve that..?

Also if I use printk messages then the log messages are delivered correctly. "Device node created success" etc.....
 

Printk works.. I'm saying that there maybe linking problem in any other kernel modules when you r trying to create a dev node using insmod...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top