// SPDX-License-Identifier: GPL-2.0 #include #include #include static int major; static int my_open(struct inode *inode, struct file *filp) { pr_info("hello_cdev - Major: %d, Minor %d\n", imajor(inode), iminor(inode)); pr_info("hello_cdev - filp->f_pos: %lld\n", filp->f_pos); pr_info("hello_cdev - filp->f_mode: 0x%x\n", filp->f_mode); pr_info("hello_cdev - filp->f_flags: 0x%x\n", filp->f_flags); return 0; } static int my_release(struct inode *inode, struct file *filp) { pr_info("hello_cdev - File is closed\n"); return 0; } static struct file_operations fops = { .open = my_open, .release = my_release }; /** * @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");