// SPDX-License-Identifier: GPL-2.0 #include #include #include static int major; static ssize_t my_read (struct file *f, char __user *u, size_t l, loff_t *o) { pr_info("hello_cdev - Read is called"); return 0; } static struct file_operations fops = { .read = my_read }; /** * @brief This function is called when the module is loaded into the kernel */ static int __init my_init(void) { /** * First argument 0 auto allocates major device number * returns device number * Enter a number eg 64 to allocate all minor num 0 - 255 * 0 returned if successfull */ major = register_chrdev(0, "hello_cdev", &fops); if (major < 0) { pr_err("hello_cdev - Error registering chrdev\n"); return major; } pr_info("hello_cdev - Major device number: %d\n", major); return 0; } /** * @brief This function is called when the module is removed from the kernel */ static void __exit my_exit(void) { pr_info("Goodbye, Kernel\n"); unregister_chrdev(major, "hello_cdev"); } module_init(my_init); module_exit(my_exit); /* Meta Information */ MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("David D'Ulisse"); MODULE_DESCRIPTION("A simple character device driver");