[英]Can a Linux kernel module use UIO if it does not use any physical hardware?
我正計划構建一個Linux內核模塊,該模塊將需要與用戶空間設備驅動程序接口,並且需要將數據導出到用戶空間。 經過一番閱讀,我發現可能是我需要的UIO接口。
我看了一些示例,這些示例均基於以下假設:內核模塊本身將直接與硬件交互,並引用諸如設備結構,中斷等內容。
是否可以編寫僅軟件的內核模塊並仍然使用UIO庫? 還是直接使用sysfs會是更好的方法?
編輯:我附上我正在工作的一些測試代碼。 目的是嘗試通過UIO界面從用戶空間讀取字符串,但是我認為這不起作用,因為我看不到如何正確啟動uio_register_device
所需的struct device
。
#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // Needed for KERN_ALERT
#include <linux/uio_driver.h>
#include <linux/slab.h> // GFP_ defs
#include <linux/device.h>
char test_data[] = "This is some test data to read in user-space via UIO\n";
int init_module(void)
{
struct uio_info *info;
struct device *dev;
info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
if (!info)
return -ENOMEM;
// need to use struct device for uio_register_device
dev = kzalloc(sizeof(struct device), GFP_KERNEL);
dev->parent = 0;
dev->init_name = "UIO test driver";
info->name = "uio_test";
info->version = "0.0.1";
info->mem[0].size = sizeof(test_data);
info->mem[0].memtype = UIO_MEM_LOGICAL;
info->mem[0].addr = (phys_addr_t) kmalloc(sizeof(test_data), GFP_KERNEL);
snprintf((char *) info->mem[0].addr, sizeof(test_data), "%s", test_data);
info->irq = UIO_IRQ_NONE;
// now we need to register the device for it to create /dev/uioN and sysfs files
if (uio_register_device(dev, info)) {
printk(KERN_ALERT "uio_test: couldn't register UIO device\n");
kfree(dev);
kfree((char *) info->mem[0].addr);
kfree(info);
return -ENODEV;
}
printk(KERN_ALERT "uio_test: init complete\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "uio_test: exit\n");
}
MODULE_LICENSE("GPL");
內核驅動程序背后的全部要點是與硬件對話。 如果您沒有任何硬件,那么您可能根本不需要內核驅動程序。
如果不與硬件通訊,內核模塊在做什么? 從哪里獲取數據? 要回答您的問題,完全有可能編寫一個內核驅動程序,該內核驅動程序實際上並不與硬件對話,而仍與UIO對話,但是我不確定它會說些什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.