字符驱动(用于控制pcie的bar0空间)

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>

#define DEVICE_NAME "pcidev0"
#define SHARED_MEMORY_ADDR 0xf32000000
#define SHARED_MEMORY_SIZE (2 * 1024 * 1024) // 2MB

static int majorNumber;
static char *shared_memory;

// Function prototypes
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char __user *, size_t, loff_t *);

// File operations structure
static struct file_operations fops = {
    .open = device_open,
    .read = device_read,
    .write = device_write,
    .release = device_release,
};

static int __init chardev_init(void) {
    majorNumber = register_chrdev(0, DEVICE_NAME, &fops);
    if (majorNumber < 0) {
        printk(KERN_ALERT "Failed to register a major number\n");
        return majorNumber;
    }

    printk(KERN_INFO "Registered a major number %d\n", majorNumber);
    printk(KERN_INFO "Device created with name: %s\n", DEVICE_NAME);

    return 0;
}

static void __exit chardev_exit(void) {
    unregister_chrdev(majorNumber, DEVICE_NAME);
    printk(KERN_INFO "Unregistered the major number\n");
}

static int device_open(struct inode *inode, struct file *file) {
    shared_memory = ioremap(SHARED_MEMORY_ADDR, SHARED_MEMORY_SIZE);
    if (!shared_memory) {
        printk(KERN_ALERT "Failed to remap shared memory\n");
        return -EFAULT;
    }
    
    // You can perform any open-specific operations here
    return 0;
}

static int device_release(struct inode *inode, struct file *file) {
    iounmap(shared_memory);
    // You can perform any release-specific operations here
    return 0;
}

static ssize_t device_read(struct file *file, char __user *buffer, size_t length, loff_t *offset) {
    int bytesRead = 0;
    if (*offset < SHARED_MEMORY_SIZE) {
        int bytesToRead = min(length, (size_t)(SHARED_MEMORY_SIZE - *offset));
        if (copy_to_user(buffer, shared_memory + *offset, bytesToRead) != 0) {
            return -EFAULT;
        }
        bytesRead = bytesToRead;
        *offset += bytesRead;
    }
    return bytesRead;
}

static ssize_t device_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset) {
    int bytesWritten = 0;
    if (*offset < SHARED_MEMORY_SIZE) {
        int bytesToWrite = min(length, (size_t)(SHARED_MEMORY_SIZE - *offset));
        if (copy_from_user(shared_memory + *offset, buffer, bytesToWrite) != 0) {
            return -EFAULT;
        }
        bytesWritten = bytesToWrite;
        *offset += bytesWritten;
    }
    return bytesWritten;
}

module_init(chardev_init);
module_exit(chardev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver for shared memory");
MODULE_VERSION("1.0");

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值