Linux驱动程序3

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/mutex.h>

#define DEVICE_NAME "custom_dev"
#define BUFFER_SIZE 32 // 可调大小

static int major_num;
static char buffer[BUFFER_SIZE];
static int pRead = 0;
static int pWrite = 0;
static int bytes_in_buffer = 0;

static DECLARE_WAIT_QUEUE_HEAD(read_queue);
static DECLARE_WAIT_QUEUE_HEAD(write_queue);
static DEFINE_MUTEX(buffer_mutex);

static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);

static struct file_operations fops = {
    .open = dev_open,
    .release = dev_release,
    .read = dev_read,
    .write = dev_write,
};

static int __init char_dev_init(void) {
    major_num = register_chrdev(0, DEVICE_NAME, &fops);
    if (major_num < 0) {
        printk(KERN_ALERT "Failed to register a major number\n");
        return major_num;
    }
    printk(KERN_INFO "Registered correctly with major number %d\n", major_num);
    return 0;
}

static void __exit char_dev_exit(void) {
    unregister_chrdev(major_num, DEVICE_NAME);
    printk(KERN_INFO "Unregistered correctly\n");
}

static int dev_open(struct inode *inodep, struct file *filep) {
    printk(KERN_INFO "Device opened\n");
    return 0;
}

static ssize_t dev_read(struct file *filep, char *user_buffer, size_t len, loff_t *offset) {
    int bytes_to_read;
    int ret;

    if (len == 0) {
        return 0;
    }

    wait_event_interruptible(read_queue, bytes_in_buffer > 0);

    if (mutex_lock_interruptible(&buffer_mutex)) {
        return -ERESTARTSYS;
    }

    bytes_to_read = min(len, (size_t)bytes_in_buffer);

    if (copy_to_user(user_buffer, &buffer[pRead], bytes_to_read) != 0) {
        mutex_unlock(&buffer_mutex);
        return -EFAULT;
    }

    pRead = (pRead + bytes_to_read) % BUFFER_SIZE;
    bytes_in_buffer -= bytes_to_read;

    mutex_unlock(&buffer_mutex);

    wake_up_interruptible(&write_queue);

    return bytes_to_read;
}

static ssize_t dev_write(struct file *filep, const char *user_buffer, size_t len, loff_t *offset) {
    int space_available;
    int bytes_to_write;

    if (len == 0) {
        return 0;
    }

    wait_event_interruptible(write_queue, bytes_in_buffer < BUFFER_SIZE);

    if (mutex_lock_interruptible(&buffer_mutex)) {
        return -ERESTARTSYS;
    }

    space_available = BUFFER_SIZE - bytes_in_buffer;
    bytes_to_write = min(len, (size_t)space_available);

    if (copy_from_user(&buffer[pWrite], user_buffer, bytes_to_write) != 0) {
        mutex_unlock(&buffer_mutex);
        return -EFAULT;
    }

    pWrite = (pWrite + bytes_to_write) % BUFFER_SIZE;
    bytes_in_buffer += bytes_to_write;

    mutex_unlock(&buffer_mutex);

    wake_up_interruptible(&read_queue);

    return bytes_to_write;
}

static int dev_release(struct inode *inodep, struct file *filep) {
    printk(KERN_INFO "Device successfully closed\n");
    return 0;
}

module_init(char_dev_init);
module_exit(char_dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver with adjustable buffer size and blocking I/O");

write.c

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define DEVICE_PATH "/dev/custom_dev"
#define BUFFER_SIZE 32

int main() {
    int fd;
    char buffer[BUFFER_SIZE];
    int bytes_written;

    fd = open(DEVICE_PATH, O_WRONLY);
    if (fd < 0) {
        perror("Failed to open the device");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < BUFFER_SIZE; i++) {
        buffer[i] = 'A' + (i % 26); // Fill the buffer with some data
    }

    bytes_written = write(fd, buffer, BUFFER_SIZE);
    if (bytes_written < 0) {
        perror("Failed to write to the device");
        close(fd);
        return EXIT_FAILURE;
    }

    printf("Wrote %d bytes to the device\n", bytes_written);

    close(fd);
    return EXIT_SUCCESS;
}

read

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define DEVICE_PATH "/dev/custom_dev"
#define BUFFER_SIZE 32

int main() {
    int fd;
    char buffer[BUFFER_SIZE];
    int bytes_read;

    fd = open(DEVICE_PATH, O_RDONLY);
    if (fd < 0) {
        perror("Failed to open the device");
        return EXIT_FAILURE;
    }

    bytes_read = read(fd, buffer, BUFFER_SIZE);
    if (bytes_read < 0) {
        perror("Failed to read from the device");
        close(fd);
        return EXIT_FAILURE;
    }

    printf("Read %d bytes from the device: %.*s\n", bytes_read, bytes_read, buffer);

    close(fd);
    return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值