#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;
}