我正在尝试创建一个char设备驱动程序,该驱动程序可以在特定的物理地址(例如0x1000-0000)上写入缓冲区。有人可以帮忙吗?
这是我编写的驱动程序示例。
//包含文件
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
//设备名称
#define test_NAME“测试”
//need to store this memory at a physical location
static char device_memory[100];
//打开功能
int test_open (struct inode *my_inode, struct file *my_file)
{
printk(KERN_INFO "Inside the test_open function...\n");
return 0;
}
//关闭设备功能
int test_close (struct inode *my_inode, struct file *my_file)
{
printk(KERN_INFO "Inside the test_close function...\n");
return 0;
}
//读取功能
ssize_t test_read (struct file *my_file, char __user *userbuff, size_t
nRead, loff_t *nReadOffset)
{
int notCopied;
notCopied = copy_to_user(userbuff, device_memory, nRead);
printk("Inside the test_read function...\n");
return (nRead - notCopied);
}
//写入功能
ssize_t test_write (struct file *my_file, const char __user *userbuff,
size_t nWrite, loff_t *nWriteOffset)
{
int notCopied;
notCopied = copy_from_user(device_memory, userbuff, nWrite);
printk("Inside the test_write function...\n");
return (nWrite - notCopied);
}
static struct file_operations test_fops = {
.owner = THIS_MODULE,
.open = test_open,
.release = test_close,
.read = test_read,
.write = test_write,
};
int init_module(void)
{
int retval;
retval = register_chrdev(120, test_NAME, &test_fops);
if (retval != 0)
{
printk(KERN_INFO "Failed to register test driver...\n");
return -1;
}
printk(KERN_INFO "test registration succeeded...\n");
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(120, test_NAME);
printk(KERN_INFO "test unregistered successfully...\n");
return;
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("SK110");
MODULE_DESCRIPTION("Driver for a test (virtual) device.");