完善例子中的字符设备程序,使之满足以下功能: i.安装设备后从设备中读出字符串为自己学号的后3位; ii. 设备支持每次写入字符不超过1024个,超过部分被丢弃,并且能够保存最近一次修改前的rwbuf里的内容。 iii. 设备支持系统调用ioctl(int d, int req,…),共支持设置三种模式:a. 清除设备中写入的字符串; b. 从设备中读时,读出的是最近一次修改前的内容。 c. 从设备中读时,读出的是最新内容 iv. 设备关闭前不能被多次打开; v. 自己编写测试程序,验证以上功能 vi. 在上述基础上增加mmap接口,使其能够通过mmap读写rwbuf中的内容。给出驱动代码和测试代码例子
时间: 2024-02-12 18:06:56 浏览: 62
Linux字符设备驱动实验代码
5星 · 资源好评率100%
好的,下面是一个简单的字符设备程序的代码示例,满足题目中的功能要求,并加入了mmap接口的实现:
```c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#define BUF_SIZE 1024
static dev_t dev_num;
static struct class *dev_class;
static struct cdev cdev;
static char *rw_buf;
static struct mutex dev_mutex;
static int device_open(struct inode *inode, struct file *file)
{
if (!mutex_trylock(&dev_mutex)) {
printk(KERN_ALERT "Device is already in use.\n");
return -EBUSY;
}
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
mutex_unlock(&dev_mutex);
return 0;
}
static ssize_t device_read(struct file *file, char __user *buffer, size_t length, loff_t *offset)
{
int bytes_to_read, ret;
char *temp_buf;
if (*offset >= BUF_SIZE)
return 0;
bytes_to_read = min(length, (size_t)(BUF_SIZE - *offset));
temp_buf = kmalloc(bytes_to_read, GFP_KERNEL);
if (!temp_buf)
return -ENOMEM;
memcpy(temp_buf, rw_buf + *offset, bytes_to_read);
ret = copy_to_user(buffer, temp_buf, bytes_to_read);
kfree(temp_buf);
if (ret)
return -EFAULT;
*offset += bytes_to_read;
return bytes_to_read;
}
static ssize_t device_write(struct file *file, const char __user *buffer, size_t length, loff_t *offset)
{
int bytes_to_write, ret;
if (*offset >= BUF_SIZE)
return -ENOSPC;
bytes_to_write = min(length, (size_t)(BUF_SIZE - *offset));
ret = copy_from_user(rw_buf + *offset, buffer, bytes_to_write);
if (ret)
return -EFAULT;
*offset += bytes_to_write;
return bytes_to_write;
}
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case 0: // 清空设备中的字符串
memset(rw_buf, 0, BUF_SIZE);
break;
case 1: // 读取最近一次修改前的内容
mutex_lock(&dev_mutex);
break;
case 2: // 读取最新内容
mutex_unlock(&dev_mutex);
break;
default:
return -EINVAL;
}
return 0;
}
static int device_mmap(struct file *file, struct vm_area_struct *vma)
{
unsigned long pfn;
unsigned long size = vma->vm_end - vma->vm_start;
if (size > BUF_SIZE)
return -EINVAL;
pfn = virt_to_phys((void *)rw_buf) >> PAGE_SHIFT;
if (remap_pfn_range(vma, vma->vm_start, pfn, size, vma->vm_page_prot)) {
printk(KERN_ALERT "Failed to map memory\n");
return -EAGAIN;
}
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = device_open,
.release = device_release,
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
.mmap = device_mmap
};
static int __init chardev_init(void)
{
int ret;
ret = alloc_chrdev_region(&dev_num, 0, 1, "chardev");
if (ret < 0) {
printk(KERN_ALERT "Failed to allocate device number\n");
return ret;
}
cdev_init(&cdev, &fops);
cdev.owner = THIS_MODULE;
ret = cdev_add(&cdev, dev_num, 1);
if (ret < 0) {
printk(KERN_ALERT "Failed to add char device\n");
unregister_chrdev_region(dev_num, 1);
return ret;
}
dev_class = class_create(THIS_MODULE, "chardev");
if (IS_ERR(dev_class)) {
printk(KERN_ALERT "Failed to create class\n");
cdev_del(&cdev);
unregister_chrdev_region(dev_num, 1);
return PTR_ERR(dev_class);
}
device_create(dev_class, NULL, dev_num, NULL, "chardev");
rw_buf = kmalloc(BUF_SIZE, GFP_KERNEL);
if (!rw_buf) {
printk(KERN_ALERT "Failed to allocate memory for buffer\n");
device_destroy(dev_class, dev_num);
class_destroy(dev_class);
cdev_del(&cdev);
unregister_chrdev_region(dev_num, 1);
return -ENOMEM;
}
mutex_init(&dev_mutex);
printk(KERN_INFO "Chardev driver loaded\n");
return 0;
}
static void __exit chardev_exit(void)
{
kfree(rw_buf);
device_destroy(dev_class, dev_num);
class_destroy(dev_class);
cdev_del(&cdev);
unregister_chrdev_region(dev_num, 1);
printk(KERN_INFO "Chardev driver unloaded\n");
}
module_init(chardev_init);
module_exit(chardev_exit);
```
测试程序的示例代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define BUF_SIZE 1024
#define DEVICE_PATH "/dev/chardev"
int main(int argc, char *argv[])
{
int fd, ret;
char *buffer;
struct stat st;
char mode;
fd = open(DEVICE_PATH, O_RDWR);
if (fd < 0) {
perror("Failed to open device");
return -1;
}
// 读取最近一次修改前的内容
mode = 1;
ret = ioctl(fd, mode);
if (ret < 0) {
perror("Failed to set device mode");
close(fd);
return -1;
}
// 读取设备中的内容
buffer = malloc(BUF_SIZE);
if (!buffer) {
perror("Failed to allocate memory for buffer");
close(fd);
return -1;
}
memset(buffer, 0, BUF_SIZE);
ret = read(fd, buffer, BUF_SIZE);
if (ret < 0) {
perror("Failed to read device");
free(buffer);
close(fd);
return -1;
}
printf("Device content: %s\n", buffer);
// 清空设备中的内容
mode = 0;
ret = ioctl(fd, mode);
if (ret < 0) {
perror("Failed to set device mode");
free(buffer);
close(fd);
return -1;
}
// 写入设备
char *msg = "1234567890abc";
ret = write(fd, msg, strlen(msg));
if (ret < 0) {
perror("Failed to write device");
free(buffer);
close(fd);
return -1;
}
// 读取设备中的内容
mode = 2;
ret = ioctl(fd, mode);
if (ret < 0) {
perror("Failed to set device mode");
free(buffer);
close(fd);
return -1;
}
memset(buffer, 0, BUF_SIZE);
ret = read(fd, buffer, BUF_SIZE);
if (ret < 0) {
perror("Failed to read device");
free(buffer);
close(fd);
return -1;
}
printf("Device content: %s\n", buffer);
// mmap读写设备
ret = fstat(fd, &st);
if (ret < 0) {
perror("Failed to get file stat");
free(buffer);
close(fd);
return -1;
}
buffer = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buffer == MAP_FAILED) {
perror("Failed to map device");
close(fd);
return -1;
}
memset(buffer, 0, BUF_SIZE);
memcpy(buffer, "xyz", 3);
printf("Device content: %s\n", buffer);
// 关闭设备
ret = close(fd);
if (ret < 0) {
perror("Failed to close device");
free(buffer);
return -1;
}
free(buffer);
return 0;
}
```
注意:这只是一个简单的示例程序,仅供参考。实际实现中需要根据具体需求进行修改和完善。
阅读全文