【问题标题】:Device driver not working设备驱动程序不工作
【发布时间】:2014-03-07 08:24:08
【问题描述】:

我为“硬币”设备编写了一个小型设备驱动程序。我在 /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);

【问题讨论】:

  • 如果我们不知道您的程序的来源,我们如何为您提供帮助?向我们展示您的来源。
  • @user2699113: 我已经添加了代码
  • CONFIG_COIN_STAT 在内核的 .config 中设置为 y??

标签: linux linux-kernel linux-device-driver


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多