Problem
I am learning about Linux kernel module development. I am writing a misc char device driver which can write to the buffer. When the character device node is written to, the data sent to the kernel needs to be checked. If it matches the assigned id, then it should return a correct write return value else "invalid value" error value.
How to catch or handle the return value of write
function when the device is registered from miscdevice
?
Code
static ssize_t write(struct file * file, const char * buf, size_t count, loff_t *ppos){ ssize_t len; char *hello_str = "test"; char *msg = kmalloc (count + 1, GFP_KERNEL); if (!msg) return -ENOMEM; if (strlen(hello_str) != count-1) return -EINVAL; len = simple_write_to_buffer(msg, count, ppos, buf, count); if (strncmp(msg, hello_str, strlen(hello_str)) != 0) return -EINVAL; return len;}static const struct file_operations file_ops = { .owner = THIS_MODULE, .write = hello_write,};static struct miscdevice misc_dev = { MISC_DYNAMIC_MINOR, //Dynamic number for testing"test_device",&file_ops};static int helloworld(void){ pr_debug("Hello World!\n"); return misc_register(&misc_dev);}module_init(helloworld);