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<linux/init.h><br>#include<linux/device.h><br>#include<linux/fs.h><br>#include<asm-generic/uaccess.h><br>#include<linux/module.h><br>
#include<linux/kernel.h><br>#include<linux/cdev.h><br>#include<linux/string.h><br>MODULE_LICENSE("Dual BSD/GPL");<br>#define DEVICE_NAME "niroj_char"<br><br>static const int success=0;<br>
static struct cdev *mydevice;<br>struct class *myclass;<br>static char Buffer[]="My Name is Niroj";<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(&(mydevice->dev),0,1,DEVICE_NAME);<br> if(!errorcode)<br> {<br> printk(KERN_ALERT "\nSuccessfully done so far now we have to create node and other things.");<br>
mydevice->ops=&fops;<br> mydevice->owner=THIS_MODULE;<br> cdev_add(mydevice,mydevice->dev,1);<br> myclass=class_create(THIS_MODULE,DEVICE_NAME);<br> my_device=device_create(myclass,NULL,mydevice->dev,NULL,DEVICE_NAME);<br>
if(my_device==NULL)<br> printk(KERN_ALERT "Device Node hasn't been created successfully."); <br> }<br> else<br> {<br> rvalue=errorcode;<br> printk(KERN_ALERT "\nUnsuccessful allocation of chrdev");<br>
}<br> printk(KERN_ALERT "Bye Bye Init");<br> return 0;<br>}<br>static void hello_exit(void)<br>{<br> printk(KERN_ALERT "Enter the exit function");<br> device_destroy(myclass,mydevice->dev);<br>
class_destroy(myclass);<br> unregister_chrdev_region(mydevice->dev,1);<br> cdev_del(mydevice);<br> printk(KERN_ALERT "Exit the exit function");<br>}<br>static int device_open(struct inode *inode, struct file *filp)<br>
{<br> printk(KERN_ALERT "device opened successfully.");<br> return success;<br>}<br>static int device_release(struct inode *inode,struct file *filp)<br>{<br> printk(KERN_ALERT "device released successfully.");<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 "%s",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<stdio.h><br>#include<string.h><br>#include<fcntl.h><br>#define DEVICE_PATH "/dev/niroj_char"<br>static char Buffer[256];<br>int main()<br>{<br> int fd;<br> if((fd=open(DEVICE_PATH,O_RDWR,0x777))<=0)<br>
printf("\nDevice Open Failed %d ",fd);<br> if(read(fd,Buffer,sizeof(Buffer))==0)<br> printf("\nThe data hasn't been read");<br> else<br> printf("\n The copied string is %s ",Buffer);<br>
strcpy(Buffer,"Niroj you are a dude");<br> if(write(fd,Buffer,sizeof(Buffer))==0)<br> printf("\nThe data hasn't been written");<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>