Yes
This commit is contained in:
		
							parent
							
								
									83db8ec77e
								
							
						
					
					
						commit
						40295d6d45
					
				
					 1 changed files with 68 additions and 48 deletions
				
			
		| 
						 | 
					@ -1,9 +1,18 @@
 | 
				
			||||||
#include "linux/fs.h"
 | 
					#include "linux/fs.h"
 | 
				
			||||||
#include "linux/types.h"
 | 
					#include "linux/types.h"
 | 
				
			||||||
 | 
					#include <asm/io.h>
 | 
				
			||||||
 | 
					#include <asm/irq.h>
 | 
				
			||||||
 | 
					#include <asm/uaccess.h>
 | 
				
			||||||
 | 
					#include <linux/cdev.h>
 | 
				
			||||||
 | 
					#include <linux/delay.h>
 | 
				
			||||||
 | 
					#include <linux/device.h>
 | 
				
			||||||
 | 
					#include <linux/fs.h>
 | 
				
			||||||
#include <linux/gpio/consumer.h>
 | 
					#include <linux/gpio/consumer.h>
 | 
				
			||||||
#include <linux/init.h>
 | 
					#include <linux/init.h>
 | 
				
			||||||
 | 
					#include <linux/irq.h>
 | 
				
			||||||
#include <linux/kernel.h>
 | 
					#include <linux/kernel.h>
 | 
				
			||||||
#include <linux/module.h>
 | 
					#include <linux/module.h>
 | 
				
			||||||
 | 
					#include <linux/poll.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Last compiled with Linux 6.14.5-300.fc42.x86_64
 | 
					 * Last compiled with Linux 6.14.5-300.fc42.x86_64
 | 
				
			||||||
| 
						 | 
					@ -15,9 +24,12 @@ MODULE_VERSION("0.0.1");
 | 
				
			||||||
MODULE_AUTHOR("Imbus");
 | 
					MODULE_AUTHOR("Imbus");
 | 
				
			||||||
MODULE_LICENSE("GPL");
 | 
					MODULE_LICENSE("GPL");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define DEVICE_NAME "hello_cdev"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Since were dynamically assigned a major device number, we will store it here
 | 
					// Since were dynamically assigned a major device number, we will store it here
 | 
				
			||||||
static int           major;
 | 
					static int           major;
 | 
				
			||||||
static char          text[64];
 | 
					static char          text[64];
 | 
				
			||||||
 | 
					static struct class *cls;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Regular forward decls
 | 
					// Regular forward decls
 | 
				
			||||||
static ssize_t my_read(struct file *f, char __user *u, size_t l, loff_t *o);
 | 
					static ssize_t my_read(struct file *f, char __user *u, size_t l, loff_t *o);
 | 
				
			||||||
| 
						 | 
					@ -27,14 +39,16 @@ static int my_release(struct inode *, struct file *);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// See: https://elixir.bootlin.com/linux/v6.14.5/source/include/linux/fs.h#L2131
 | 
					// See: https://elixir.bootlin.com/linux/v6.14.5/source/include/linux/fs.h#L2131
 | 
				
			||||||
static struct file_operations fops = {
 | 
					static struct file_operations fops = {
 | 
				
			||||||
    .read = my_read, .write = my_write, .open = my_open, .release = my_release};
 | 
					    .read = my_read,
 | 
				
			||||||
 | 
					    .write = my_write,
 | 
				
			||||||
 | 
					    .open = my_open,
 | 
				
			||||||
 | 
					    .release = my_release,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ssize_t my_write(struct file *filp, const char __user *user_buf,
 | 
					static ssize_t my_write(struct file *filp, const char __user *user_buf, size_t len, loff_t *off) {
 | 
				
			||||||
                        size_t len, loff_t *off) {
 | 
					    pr_info("hello_cdev - Write is called by %s...\n", current->comm);
 | 
				
			||||||
  printk(KERN_INFO "hello_cdev - Write is called by %s...\n", current->comm);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int not_copied, delta,
 | 
					    int not_copied, delta, to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
 | 
				
			||||||
      to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (*off >= sizeof(text))
 | 
					    if (*off >= sizeof(text))
 | 
				
			||||||
        return 0;
 | 
					        return 0;
 | 
				
			||||||
| 
						 | 
					@ -50,12 +64,10 @@ static ssize_t my_write(struct file *filp, const char __user *user_buf,
 | 
				
			||||||
    return delta;
 | 
					    return delta;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ssize_t my_read(struct file *filp, char __user *user_buf, size_t len,
 | 
					static ssize_t my_read(struct file *filp, char __user *user_buf, size_t len, loff_t *off) {
 | 
				
			||||||
                       loff_t *off) {
 | 
					    pr_info("hello_cdev - Read is called\n");
 | 
				
			||||||
  printk(KERN_INFO "hello_cdev - Read is called\n");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int not_copied, delta,
 | 
					    int not_copied, delta, to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
 | 
				
			||||||
      to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (*off >= sizeof(text))
 | 
					    if (*off >= sizeof(text))
 | 
				
			||||||
        return 0;
 | 
					        return 0;
 | 
				
			||||||
| 
						 | 
					@ -73,7 +85,6 @@ static ssize_t my_read(struct file *filp, char __user *user_buf, size_t len,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int my_open(struct inode *inode, struct file *filp) {
 | 
					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 - 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_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_mode: 0x%x\n", filp->f_mode);
 | 
				
			||||||
    pr_info("hello-cdev - filp->f_flags: 0x%x\n", filp->f_flags);
 | 
					    pr_info("hello-cdev - filp->f_flags: 0x%x\n", filp->f_flags);
 | 
				
			||||||
| 
						 | 
					@ -90,14 +101,21 @@ static int my_release(struct inode *inode, struct file *filp) {
 | 
				
			||||||
 * @brief Called on load
 | 
					 * @brief Called on load
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static int __init demo_mod_init(void) {
 | 
					static int __init demo_mod_init(void) {
 | 
				
			||||||
  major = register_chrdev(0, "hello_cdev", &fops);
 | 
					    major = register_chrdev(0, DEVICE_NAME, &fops);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (major < 0) {
 | 
					    if (major < 0) {
 | 
				
			||||||
    printk(KERN_INFO "hello_cdev - Error registering chrdev\n");
 | 
					        pr_info("hello_cdev - Error registering chrdev\n");
 | 
				
			||||||
        return major;
 | 
					        return major;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  printk(KERN_INFO "hello_cdev - Major nbr %d loaded successfully...", major);
 | 
					    pr_info("I was assigned major number %d.\n", major);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cls = class_create(DEVICE_NAME);
 | 
				
			||||||
 | 
					    device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pr_info("Device created on /dev/%s\n", DEVICE_NAME);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pr_info("hello_cdev - Major nbr %d loaded successfully...", major);
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -105,8 +123,10 @@ static int __init demo_mod_init(void) {
 | 
				
			||||||
 * @brief Called on unload
 | 
					 * @brief Called on unload
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void __exit demo_mod_exit(void) {
 | 
					static void __exit demo_mod_exit(void) {
 | 
				
			||||||
  unregister_chrdev(major, "hello_cdev");
 | 
					    device_destroy(cls, MKDEV(major, 0));
 | 
				
			||||||
  printk(KERN_INFO "hello_cdev - unloaded successfully...");
 | 
					    class_destroy(cls);
 | 
				
			||||||
 | 
					    unregister_chrdev(major, DEVICE_NAME);
 | 
				
			||||||
 | 
					    pr_info("hello_cdev - unloaded successfully...");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module_init(demo_mod_init);
 | 
					module_init(demo_mod_init);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue