gpt4 book ai didi

c - insmod : ERROR: Could not insert module : No such device

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:57 37 4
gpt4 key购买 nike

我正在尝试在 Linux 中用 C 语言实现字符设备驱动程序。我的代码如下:

#include<linux/device.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/err.h>
#include<asm/uaccess.h>
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80

MODULE_LICENSE("GPL");

static int Major;
static char msg[BUF_LEN]={0};
static short s_o_msg;
static int Device_Open = 0;
static struct class* chardevClass = NULL;
static struct device* chardevDevice = NULL;
static char *msg_Ptr;

static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);



static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};

static int __init chardev_init(void){
Major = register_chrdev(0, DEVICE_NAME, &fops);

if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}

chardevDevice = device_create(chardevClass, NULL, MKDEV(Major,0), NULL, DEVICE_NAME);
if (IS_ERR(chardevDevice)) {
class_destroy(chardevClass);
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_ALERT
"Failed to create the device\n");
return PTR_ERR(chardevDevice);
}

printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
printk(KERN_INFO "the driver, create a dev file with\n");
printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
printk(KERN_INFO "the device file.\n");
printk(KERN_INFO "Remove the device file and module when done.\n");

return SUCCESS;
}

static void __exit chardev_exit(void){
device_destroy(chardevClass, MKDEV(Major, 0));
class_unregister(chardevClass);
class_destroy(chardevClass);
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "Goodbye!\n");
}

static int device_open(struct inode *inodep, struct file *filep)
{
static int counter = 0;

if (Device_Open)
return -EBUSY;

Device_Open++;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);

return SUCCESS;
}

static ssize_t device_read(struct file *filep, char *buffer, size_t length, loff_t * offset){

int bytes_read = 0;

if (*msg_Ptr == 0)
return 0;

while (length && *msg_Ptr) {

put_user(*(msg_Ptr++), buffer++);

length--;
bytes_read++;
}

return bytes_read;
}

static int device_release(struct inode *inodep, struct file *filep)
{
Device_Open--;

module_put(THIS_MODULE);

return 0;
}



static ssize_t device_write(struct file *filep, const char *buffer, size_t length, loff_t *offset){
sprintf(msg, "%s(%zu letters)", buffer, length);
s_o_msg = strlen(msg);
printk(KERN_INFO "Received %zu characters from the user\n", length);
return length;
}

module_init(chardev_init);
module_exit(chardev_exit);

然后,我用下面的命令编译它,到现在为止一切看起来都很好:

obj-m := memory.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

但是当我尝试运行这个模块时

sudo /sbin/insmod memory.ko

我得到一个错误:

insmod: ERROR: Could not insert module : No such device

你能解释一下我做错了什么吗?我应该怎么做才能正确运行这个模块?

非常感谢。

最佳答案

您忘记在 device_create() 之前创建类 (class_create) 因为如果您看到

root@achal:/sys/class# ls
.. there are so many different class..

您的设备也应该属于一个类,这就是为什么要使用class_create();

创建一个类

在您的代码中添加以下行。

chardevClass = class_create(THIS_MODULE, "overflow");

chardevDevice = device_create(chardevClass, NULL, MKDEV(Major,0), NULL, DEVICE_NAME);

root 模式需要插入一个模块,编译插入和运行一个模块的顺序是:首先做make

root@root:~/s_flow# make

然后执行insmod并分析dmesg输出

 root@root:~/s_flow# insmod memory.ko
root@root:~/s_flow# dmesg
..
.. check here whether __init chardev_init() is invoked or not

或者你也可以查看modinfo

root@root:~/s_flow# modifno memory.ko

希望对你有帮助。

关于c - insmod : ERROR: Could not insert module : No such device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47733023/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com