char驱动程序节点未打开

时间:2017-02-27 10:51:20

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

我为我的beaglebone内核写了一个简单的char驱动程序

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/fs.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("mrigendra.chaubey@gmail.com");


#define  DEVICE_NAME "experm"
#define  CLASS_NAME  "exp"

static struct class*  myclass  = NULL; ///< The device-driver class struct pointer
static struct device* mychardevice = NULL; ///< The device-driver device struct pointer
static int myopen(struct inode *, struct file *);
static int release(struct inode *, struct file *);
static int myioctl(struct inode *, struct file *, unsigned int cmd, unsigned long arg);
static size_t myread(struct file *,char * , size_t,  loff_t *);
static size_t mywrite(struct file *,char * , size_t,  loff_t *);


static dev_t mydev;

static int myopen(struct inode *nd, struct file *fp)
{
    printk(KERN_INFO "myopen\n");
    return 0;
}

static int myrelease(struct inode *nd, struct file *fp)
{
    printk(KERN_INFO "myrelease\n");
    return 0;
}

static int myioctl(struct inode *nd, struct file *fp, unsigned int cmd, unsigned long arg)
{
    printk(KERN_INFO "myioctl\n");
    return 0;
}

static size_t myread(struct file *fp, char *buf, size_t len, loff_t *ofs)
{
    printk(KERN_INFO "myread\n");
    return 0;
}

static size_t mywrite(struct file *fp, char *buf, size_t len, loff_t *ofs)
{
    printk(KERN_INFO "mywrite\n");
    return 0;
}


static struct file_operations fops = {
    .open = myopen,
    .release = myrelease,
    .read = myread,
    .write = mywrite,
    .unlocked_ioctl = myioctl,
};

static int __init myinit(void)
{
    int err;
    //extern int alloc_chrdev_region(dev_t *, unsigned minor number, unsigned total, const char *);
    err = alloc_chrdev_region(&mydev, 0, 1, "expermdev");
    if(err<0)
    {
        printk(KERN_INFO "major and minor can't be created, err = %d\n", err);
        return err;
    }

    //struct class * class_create ( struct module *owner, const char *name);
    myclass = class_create(THIS_MODULE, CLASS_NAME);
    if(IS_ERR(myclass))
    {
        unregister_chrdev(MAJOR(mydev), "expermdev");
        printk(KERN_ALERT "Failed to register device class\n");
        return PTR_ERR(myclass); 
    }

    //struct device *device_create(struct class *cls, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...);
    //This function can be used by char device classes. A struct device will be created in sysfs, registered to the specified class. 
    mychardevice = device_create(myclass, NULL, mydev, NULL, "expermdev");
    if(IS_ERR(mychardevice))
    {
        class_destroy(myclass);
        unregister_chrdev(MAJOR(mydev), "expermdev");
        printk(KERN_ALERT "Failed to create the device\n");
        return PTR_ERR(mychardevice);
    }

    printk(KERN_INFO "my device created correctly\n");
    return 0;
}

static void __exit myexit(void)
{
    device_destroy(mychardevice, mydev);
    class_unregister(myclass);
    class_destroy(myclass);
    unregister_chrdev(MAJOR(mydev), "expermdev");
    printk(KERN_INFO "exited\n");
}

module_init(myinit);
module_exit(myexit);

和app.c文件

#include <stdio.h>
#include<fcntl.h>

int main()
{
    int fp;

    fp = open ("/dev/expermdev", O_RDWR);
    if(fp < 0)
        printf("file can't be opened\n");
    else
        printf("file opened\n");
    return 0;
}

我正在将驱动程序编译为模块并insmod,也使用相同的交叉编译器编译app.c,并将此二进制文件放在bin目录中。我运行了这个bin文件,但它说

file can't be opened

我做错了什么?

1 个答案:

答案 0 :(得分:0)

知道了。我没有使用cdev struture并添加了fops。之后我必须告诉内核。

foreach ($ds in (get-datastore | where {$_.FreeSpaceGB -lt 2})){"dosomething"}

在取消注册驱动程序之前,还要在退出函数中删除此cdev。 在register_chrdev_region中我们同时做两件事,但在主要和次要数字的动态分配中,我们必须使用cdev来完成剩下的工作。