linux miscdevice 驱动程序

时间:2014-07-22 00:17:34   收藏:0   阅读:415

1  在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述)。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。 所有的miscdevice设备形成了一个链表,对设备访问时内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。 在内核中用struct miscdevice表示miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。miscdevice的API实现在drivers/char/misc.c中。

struct miscdevice  

{

int minor;

const char *name;

const struct file_operations *fops;

struct list_head list;

struct device *parent;

struct device *this_device;

const char *nodename;

mode_t mode;

};

minor是这个混杂设备的次设备号,若由系统自动配置,则可以设置为

MISC_DYNANIC_MINOR,name是设备名 


misc->this_device = device_create(misc_class, misc->parent, dev, misc, "%s", misc->name);//说明了混杂设备驱动内部有自动设备驱动,可自动创建设备节点

#define MISC_DYNAMIC_MINOR      255  动态分配次设备号。

structfile_operations{

        struct module *owner; 

        // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES

        loff_t (*llseek) (struct file *, loff_t, int);

        // llseek用来修改文件当前的读写位置,返回新位置

        // loff_t为一个"长偏移量"。当此函数指针为空,seek调用将会以不可预期的方式修改file结构中的位置计数器。

        ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

        // 从设备中同步读取数据。读取成功返回读取的字节数。设置为NULL,调用时返回-EINVAL

        ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);

        // 初始化一个异步的读取操作,为NULL时全部通过read处理

        ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

        // 向设备发送数据。

        ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);

        // 初始化一个异步的写入操作。

        int (*readdir) (struct file *, void *, filldir_t);

        // 仅用于读取目录,对于设备文件,该字段为 NULL

        unsigned int (*poll) (struct file *, struct poll_table_struct *);

        // 返回一个位掩码,用来指出非阻塞的读取或写入是否可能。

        // 将pool定义为 NULL,设备会被认为即可读也可写。

        int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);

        // 提供一种执行设备特殊命令的方法。不设置入口点,返回-ENOTTY

        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

        // 不使用BLK的文件系统,将使用此种函数指针代替ioctl

        long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

        // 在64位系统上,32位的ioctl调用,将使用此函数指针代替

        int (*mmap) (struct file *, struct vm_area_struct *);

        // 用于请求将设备内存映射到进程地址空间。如果无此方法,将访问-ENODEV。

        int (*open) (struct inode *, struct file *);

        // 如果为空,设备的打开操作永远成功,但系统不会通知驱动程序

        // 由VFS调用,当VFS打开一个文件,即建立了一个新的"struct file",之后调用open方法分配文件结构。open属于struct inode_operations。

        int (*flush) (struct file *);

        // 发生在进程关闭设备文件描述符副本,执行并等待,若设置为NULL,内核将忽略用户应用程序的请求。

        int (*release) (struct inode *, struct file *);

        // file结构释放时,将调用此指针函数,release与open相同可设置为NULL

        int (*fsync) (struct file *, struct dentry *, int datasync);

        // 刷新待处理的数据,如果驱动程序没有实现,fsync调用将返回-EINVAL

        int (*aio_fsync) (struct kiocb *, int datasync);

        // 异步fsync

        int (*fasync) (int, struct file *, int);

        // 通知设备FASYNC标志发生变化,如果设备不支持异步通知,该字段可以为NULL

        int (*lock) (struct file *, int, struct file_lock *);

        // 实现文件锁,设备驱动常不去实现此lock

        ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);

        ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);

        // readv和writev 分散/聚集型的读写操作,实现进行涉及多个内存区域的单次读或写操作。

        ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);

        // 实现sendfile调用的读取部分,将数据从一个文件描述符移到另一个,设备驱动通常将其设置为 NULL

        ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);

        // 实现sendfile调用的另一部分,内核调用将其数据发送到对应文件,每次一个数据页,设备驱动通常将其设置为NULL

        unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);

        // 在进程地址空间找到一个合适的位置,以便将底层设备中的内存段映射到该位置。大部分驱动可将其设置为NULL

        int (*check_flags)(int);

        // 允许模块检查传递给fcntl(F_SETEL...)调用的标志

        int (*dir_notify)(struct file *filp, unsigned long arg);

        // 应用程序使用fcntl来请求目录改变通知时,调用该方法。仅对文件系统有效,驱动程序不必实现。

        int (*flock) (struct file *, int, struct file_lock *);

        // 实现文件锁

};

3  在linux内核源码miscdevice.h里杂项设备描述结构体定义:

struct miscdevice  {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};

extern int misc_register(struct miscdevice * misc); 
extern int misc_deregister(struct miscdevice *misc);

miscdevice设备框架:
static ssize_t mydev_read(struct file *filp, char __user * buf, size_t count, loff_t *f_pos)
{
}
static int mydev_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
return 0;
}

static struct file_operations mydev_fops = {     //虚拟文件系统
.owner = THIS_MODULE,
.ioctl = mydev_ioctl,
.read = mydev_read,
};

static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &mydev_fops,
};

static int mydev_init(void)
{
    int ret;
    ret = misc_register(&misc);
    return ret;
}

static void mydev_exit(void)
{
    misc_deregister(&misc);
}


module_init(mydev_init);   //模块加载函数
module_exit(mydev_exit);   //模块卸载函数
MODULE_LICENSE("Dual BSD/GPL");  //模块声明

杂项设备是特殊的字符设备,但是编写这类驱动程序省去了创建设备节点的麻烦,因此更加方便。

linux miscdevice 驱动程序,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!