linux - 设备驱动程序不工作

标签 linux linux-kernel linux-device-driver

我为“硬币”设备编写了一个小型设备驱动程序。我在/drivers/char/Kconfig 中创建一个条目 和相应的Makefile,然后在menuconfig中选择内置选项。内核编译良好(创建了built-in.o 文件)。但我仍然无法访问该设备(/dev/coin 未创建),并且/proc/devices 下没有条目。 请帮忙!!

我正在为 powerpc 进行交叉编译

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/random.h>
#include <linux/debugfs.h>
#include <linux/init.h>

#define DEVNAME "coin"
#define LEN  20
enum values {HEAD, TAIL};

struct dentry *dir, *file;
int file_value;
int stats[2] = {0, 0};
char *msg[2] = {"head\n", "tail\n"};

static int major;
static struct class *class_coin;
static struct device *dev_coin;

static ssize_t r_coin(struct file *f, char __user *b,
                      size_t cnt, loff_t *lf)
{
        char *ret;
        u32 value = random32() % 2;
        ret = msg[value];
        stats[value]++;
        return simple_read_from_buffer(b, cnt,
                                       lf, ret,
                                       strlen(ret));
}

static struct file_operations fops = { .read = r_coin };

#ifdef CONFIG_COIN_STAT
static ssize_t r_stat(struct file *f, char __user *b,
                         size_t cnt, loff_t *lf)
{
        char buf[LEN];
        snprintf(buf, LEN, "head=%d tail=%d\n",
                 stats[HEAD], stats[TAIL]);
        return simple_read_from_buffer(b, cnt,
                                       lf, buf,
                                       strlen(buf));
} 

static struct file_operations fstat = { .read = r_stat };
#endif

static int __init coin_init(void)
{
        void *ptr_err;
        major = register_chrdev(0, DEVNAME, &fops);
        if (major < 0)
                return major;

        class_coin = class_create(THIS_MODULE,
                                  DEVNAME);
        if (IS_ERR(class_coin)) {
                ptr_err = class_coin;
                goto err_class;
        }

        dev_coin = device_create(class_coin, NULL,
                                 MKDEV(major, 0),
                                 NULL, DEVNAME);
        if (IS_ERR(dev_coin))
                goto err_dev;

#ifdef CONFIG_COIN_STAT
        dir = debugfs_create_dir("coin", NULL);
        file = debugfs_create_file("stats", 0644,
                                   dir, &file_value,
                                   &fstat);
#endif

        return 0;
err_dev:
        ptr_err = class_coin;
        class_destroy(class_coin);
err_class:
        unregister_chrdev(major, DEVNAME);
        return PTR_ERR(ptr_err);
}

static void __exit coin_exit(void)
{
    #ifdef CONFIG_COIN_STAT
    debugfs_remove(file);
    debugfs_remove(dir);
    #endif

    device_destroy(class_coin, MKDEV(major, 0));
    class_destroy(class_coin);
    return unregister_chrdev(major, DEVNAME);
}

module_init(coin_init);
module_exit(coin_exit);

最佳答案

如果使用 insmod 手动将模块插入内核会怎样?有效吗? dmesg 中有消息吗?

我记得/dev (/dev/coin) 中的条目应该使用 mknod 手动创建,但您需要注册设备的主要数量。只需在 register_chrdev() 之后 printk 即可。

关于linux - 设备驱动程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21669851/

相关文章:

linux - zImage 可以作为 uImage 的包装器并仍然成功启动吗?

python - 如何从同样动态创建 salt 状态的循环中要求动态创建的 salt 状态?

c - 在发送数据包之前读取 TCP 序列号

linux-kernel - Linux内核中的人类可读时间戳

linux-kernel - 从 DMA 地址 (dma_addr_t) 获取 PFN?

c++ - 无法调试 C++ eclipse makefile 项目

c - 为什么在 Linux 字符驱动读取调用中 size 总是 = 4096?

linux - 在 64 位系统上向 Linux Kernel 3.13 添加新的系统调用

linux - 如何更改 Linux 显示驱动程序中支持的刷新率?

linux - 这是 linux 内核中有关写入/proc/self/loginuid 的错误吗?