LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2009, 04:35 AM   #1
parthi_s
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Rep: Reputation: 0
can't read from the device driver using read() function.


Hi,

I was writing a sample character device driver, when I try to test it with the sample user application I was successfully able to write to the device but while trying to read from the device it flashing error “bad file descriptor”. Does anybody have some idea about this?

My driver read and write code is as below


ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) {

ssize_t retval = 0;

printk(KERN_INFO MODULE_NAME":- The device file is read\n");

struct mem_dev *dev = filp->private_data;

printk(KERN_INFO MODULE_NAME":- the dev->size value is: %ld\n", dev->size);

if (*f_pos >= dev->size)
goto out;
if (*f_pos + count > dev->size)
count = dev->size - *f_pos;

/* Transfering data to user space */
if (copy_to_user(buf, memory_buffer, count)) {
retval = -EFAULT;
goto out;
}

*f_pos += count;
retval = count;

return retval;

out:
return retval;
}

ssize_t memory_write( struct file *filp, char *buf, size_t count, loff_t *f_pos) {

printk(KERN_INFO MODULE_NAME":- The device file is Written\n");

struct mem_dev *dev = filp->private_data;

ssize_t retval = -ENOMEM; /* value used in "goto out" statements */

if (copy_from_user(memory_buffer, buf, count)) {
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
/* update the size */
if (dev->size < *f_pos)
dev->size = *f_pos;

printk(KERN_INFO MODULE_NAME":- the return value is: %ld\n", retval);

return retval;

out:
return retval;

}

my sample user application is as follows.

fp = open("/dev/test_memory",777);

if(fp < 0) {
printf("Unable to open the device\n");
return 1;
} else {
printf("Device open success\n");
buf = (char *)malloc(100);

printf("Enter the characters to write to device file: ");
scanf("%s", buf);

ret = write(fp,buf,strlen(buf));

if(ret < 0){
printf("Unable to write to device, response: %d\n",ret);
} else {
out_buf = (char *)malloc(100);
ret = read(fp,out_buf,strlen(buf));
if(ret < 0){
printf("Unable to read from device, response: %d\n",ret);
} else {
printf("The device content read is: %s\n",out_buf);
}
free(out_buf);
}
free(buf);
close(fp);
}
return 0;
}

TIA
 
Old 03-23-2009, 06:16 AM   #2
parthi_s
LQ Newbie
 
Registered: Mar 2009
Posts: 3

Original Poster
Rep: Reputation: 0
i found the bug:
fp = open("/dev/test_memory",777); must be as fp = open("/dev/test_memory",O_RDWR); and include the necessary .h files.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Easiest way to read from device inside another kernel driver ineya Programming 8 09-06-2008 10:34 AM
How to write a character device driver to read the processor's GDT kiranshadow Linux - General 1 05-10-2008 11:28 AM
bash read value function mcandy Programming 11 02-15-2008 11:47 AM
Help with the read() function in C smoothdogg00 Programming 4 09-25-2006 08:44 PM
read and pipe function dummyagain Programming 13 09-24-2003 10:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration