设备驱动程序不工作

时间:2014-02-10 05:40:44

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

我为“硬币”设备写了一个小设备驱动程序。我在/ drivers / char / Kconfig中创建了一个条目 和相应的Makefile,然后在menuconfig中选择内置选项。内核编译正常(内置.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);

1 个答案:

答案 0 :(得分:0)

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

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

相关问题