Opening a device driver file.

Niroj Pokhrel nirojpokhrel at gmail.com
Mon Dec 17 21:23:44 EST 2012


Hi,
I have been trying to open a device driver after inserting it in the kernel
from the userspace. Everything seems to be fine, the class is created and
is also exported to the device directory but there seems to be some problem
as every time I try to open it, the open process fails.
My code is below, Please help. Thanx in advance.

#include<linux/init.h>
#include<linux/device.h>
#include<linux/fs.h>
#include<asm-generic/uaccess.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/cdev.h>
#include<linux/string.h>
MODULE_LICENSE("Dual BSD/GPL");
#define DEVICE_NAME "niroj_char"

static const int success=0;
static struct cdev *mydevice;
struct class *myclass;
static char Buffer[]="My Name is Niroj";
static ssize_t device_read(struct file *,char *,size_t,loff_t*);
static ssize_t device_write(struct file *,const char *,size_t,loff_t*);
static int device_open(struct inode *,struct file *);
static int device_release(struct inode *,struct file *);
static struct device *my_device;
static struct file_operations fops=
{
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release,
};
static int hello_init(void)
{
    int rvalue;
    int errorcode;
    rvalue=success;
    mydevice=cdev_alloc();
    errorcode=alloc_chrdev_region(&(mydevice->dev),0,1,DEVICE_NAME);
    if(!errorcode)
    {
        printk(KERN_ALERT "\nSuccessfully done so far now we have to create
node and other things.");
        mydevice->ops=&fops;
        mydevice->owner=THIS_MODULE;
        cdev_add(mydevice,mydevice->dev,1);
        myclass=class_create(THIS_MODULE,DEVICE_NAME);

my_device=device_create(myclass,NULL,mydevice->dev,NULL,DEVICE_NAME);
        if(my_device==NULL)
            printk(KERN_ALERT "Device Node hasn't been created
successfully.");
    }
    else
    {
        rvalue=errorcode;
        printk(KERN_ALERT "\nUnsuccessful allocation of chrdev");
    }
    printk(KERN_ALERT "Bye Bye Init");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Enter the exit function");
    device_destroy(myclass,mydevice->dev);
    class_destroy(myclass);
    unregister_chrdev_region(mydevice->dev,1);
    cdev_del(mydevice);
    printk(KERN_ALERT "Exit the exit function");
}
static int device_open(struct inode *inode, struct file *filp)
{
    printk(KERN_ALERT "device opened successfully.");
    return success;
}
static int device_release(struct inode *inode,struct file *filp)
{
    printk(KERN_ALERT "device released successfully.");
    return success;
}
static ssize_t device_read(struct file *file,char *buffer,size_t
lenght,loff_t *offset)
{
    ssize_t totalbytes;
    ssize_t  bytesnotcopied;
    totalbytes=strlen(Buffer);
    bytesnotcopied=copy_to_user(buffer,Buffer,totalbytes);
    return (ssize_t)(totalbytes-bytesnotcopied);
}
static ssize_t device_write(struct file *file,const char *buffer,size_t
length,loff_t *offset)
{
    size_t bytesnotcopied;
    bytesnotcopied=copy_from_user(Buffer,buffer,length);
    printk(KERN_ALERT "%s",Buffer);
    return (ssize_t)(length-bytesnotcopied);
}
module_init(hello_init);
module_exit(hello_exit);


Similarly, my user space program is as follows :

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#define DEVICE_PATH "/dev/niroj_char"
static char Buffer[256];
int main()
{
    int fd;
    if((fd=open(DEVICE_PATH,O_RDWR,0x777))<=0)
        printf("\nDevice Open Failed %d ",fd);
    if(read(fd,Buffer,sizeof(Buffer))==0)
        printf("\nThe data hasn't been read");
    else
        printf("\n The copied string is %s ",Buffer);
    strcpy(Buffer,"Niroj you are a dude");
    if(write(fd,Buffer,sizeof(Buffer))==0)
        printf("\nThe data hasn't been written");
    return 0;
}

Output: Device Open Failed -1

-- 
Niroj Pokhrel
Software Engineer,
Samsung India Software Operations
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20121218/e98b0b8a/attachment.html 


More information about the Kernelnewbies mailing list