Hi, <br>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.<br>
My code is below, Please help. Thanx in advance.<br><br>#include&lt;linux/init.h&gt;<br>#include&lt;linux/device.h&gt;<br>#include&lt;linux/fs.h&gt;<br>#include&lt;asm-generic/uaccess.h&gt;<br>#include&lt;linux/module.h&gt;<br>
#include&lt;linux/kernel.h&gt;<br>#include&lt;linux/cdev.h&gt;<br>#include&lt;linux/string.h&gt;<br>MODULE_LICENSE(&quot;Dual BSD/GPL&quot;);<br>#define DEVICE_NAME &quot;niroj_char&quot;<br><br>static const int success=0;<br>
static struct cdev *mydevice;<br>struct class *myclass;<br>static char Buffer[]=&quot;My Name is Niroj&quot;;<br>static ssize_t device_read(struct file *,char *,size_t,loff_t*);<br>static ssize_t device_write(struct file *,const char *,size_t,loff_t*);<br>
static int device_open(struct inode *,struct file *);<br>static int device_release(struct inode *,struct file *);<br>static struct device *my_device;<br>static struct file_operations fops=<br>{    <br>    .read = device_read,<br>
    .write = device_write,<br>    .open = device_open,<br>    .release = device_release,<br>};<br>static int hello_init(void)<br>{<br>    int rvalue;<br>    int errorcode;<br>    rvalue=success;<br>    mydevice=cdev_alloc();<br>
    errorcode=alloc_chrdev_region(&amp;(mydevice-&gt;dev),0,1,DEVICE_NAME);<br>    if(!errorcode)<br>    {<br>        printk(KERN_ALERT &quot;\nSuccessfully done so far now we have to create node and other things.&quot;);<br>
        mydevice-&gt;ops=&amp;fops;<br>        mydevice-&gt;owner=THIS_MODULE;<br>        cdev_add(mydevice,mydevice-&gt;dev,1);<br>        myclass=class_create(THIS_MODULE,DEVICE_NAME);<br>        my_device=device_create(myclass,NULL,mydevice-&gt;dev,NULL,DEVICE_NAME);<br>
        if(my_device==NULL)<br>            printk(KERN_ALERT &quot;Device Node hasn&#39;t been created successfully.&quot;);        <br>    }<br>    else<br>    {<br>        rvalue=errorcode;<br>        printk(KERN_ALERT &quot;\nUnsuccessful allocation of chrdev&quot;);<br>
    }<br>    printk(KERN_ALERT &quot;Bye Bye Init&quot;);<br>    return 0;<br>}<br>static void hello_exit(void)<br>{<br>    printk(KERN_ALERT &quot;Enter the exit function&quot;);<br>    device_destroy(myclass,mydevice-&gt;dev);<br>
    class_destroy(myclass);<br>    unregister_chrdev_region(mydevice-&gt;dev,1);<br>    cdev_del(mydevice);<br>    printk(KERN_ALERT &quot;Exit the exit function&quot;);<br>}<br>static int device_open(struct inode *inode, struct file *filp)<br>
{<br>    printk(KERN_ALERT &quot;device opened successfully.&quot;);<br>    return success;<br>}<br>static int device_release(struct inode *inode,struct file *filp)<br>{<br>    printk(KERN_ALERT &quot;device released successfully.&quot;);<br>
    return success;<br>}<br>static ssize_t device_read(struct file *file,char *buffer,size_t lenght,loff_t *offset)<br>{<br>    ssize_t totalbytes;<br>    ssize_t  bytesnotcopied;<br>    totalbytes=strlen(Buffer);<br>    bytesnotcopied=copy_to_user(buffer,Buffer,totalbytes);<br>
    return (ssize_t)(totalbytes-bytesnotcopied);<br>}<br>static ssize_t device_write(struct file *file,const char *buffer,size_t length,loff_t *offset)<br>{<br>    size_t bytesnotcopied;<br>    bytesnotcopied=copy_from_user(Buffer,buffer,length);<br>
    printk(KERN_ALERT &quot;%s&quot;,Buffer);<br>    return (ssize_t)(length-bytesnotcopied);<br>}<br>module_init(hello_init);<br>module_exit(hello_exit);<br>    <br><br>Similarly, my user space program is as follows :<br>
<br>#include&lt;stdio.h&gt;<br>#include&lt;string.h&gt;<br>#include&lt;fcntl.h&gt;<br>#define DEVICE_PATH &quot;/dev/niroj_char&quot;<br>static char Buffer[256];<br>int main()<br>{<br>    int fd;<br>    if((fd=open(DEVICE_PATH,O_RDWR,0x777))&lt;=0)<br>
        printf(&quot;\nDevice Open Failed %d &quot;,fd);<br>    if(read(fd,Buffer,sizeof(Buffer))==0)<br>        printf(&quot;\nThe data hasn&#39;t been read&quot;);<br>    else<br>        printf(&quot;\n The copied string is %s &quot;,Buffer);<br>
    strcpy(Buffer,&quot;Niroj you are a dude&quot;);<br>    if(write(fd,Buffer,sizeof(Buffer))==0)<br>        printf(&quot;\nThe data hasn&#39;t been written&quot;);<br>    return 0;<br>}<br><br>Output: Device Open Failed -1<br clear="all">
<br>-- <br>Niroj Pokhrel<br>Software Engineer,<br>Samsung India Software Operations<br><br>