thank you for taking a look at this question.
I am writing a simple misc char device with read and write operations. Please keep reading ...
The following code prints the required text in TEXT only once when i do
cat /dev/mydevice01
This is the code to the read function.
static ssize_t my_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
{
char *print_str = TEXT;
if (*ppos != 0)
return 0;
if ((count < TEXT_LENGTH) || (copy_to_user(buff, print_str, TLF_ID_LENGTH)))
return -EINVAL;
*ppos += count;
return count;
}
TEXT is defined using #define and so is TEXT_LENGTH
I felt this code was a little too complicated and hence, I tried to modify it. This is the modified code, but that keeps printing the required text until I use a SIGINT (Ctrl+C) on the process:
static ssize_t my_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
{
char *print_str = TLF_ID;
if (copy_to_user(buff, print_str, TLF_ID_LENGTH))
return -EINVAL;
return count;
}
I am new to Linux kernel programming. I want to know what is going wrong with the code. And if possible, a single line of code that can implement the same functionality. Thanks in advance!
module_init(my_init)which in turn callsmisc_register()for astruct miscdevicewhich has anfops .readattribute which ends up in this function.