zoukankan      html  css  js  c++  java
  • linux内核头文件 cdev.h 解析



    遇到一个内核API——cdev_init 就找到这里来了。



    #ifndef _LINUX_CDEV_H
    #define _LINUX_CDEV_H
    
    #include <linux/kobject.h
    #include <linux/kdev_t.h>
    #include <linux/list.h>
    
    struct file_operations;
    struct inode;
    struct module;
    
    struct cdev {
            struct kobject kobj;
            struct module *owner;
            const struct file_operations *ops;
            struct list_head list;
            dev_t dev;
            unsigned int count;
    };
    
    void cdev_init(struct cdev *, const struct file_operations *);
    //初始化字符设备能够进行的文件操作——file_operations 结构体记录了全部能够对cdev结构体描写叙述的字符设备进行的操作
    
    struct cdev *cdev_alloc(void);
    
    void cdev_put(struct cdev *p);
    
    int cdev_add(struct cdev *, dev_t, unsigned);
    
    void cdev_del(struct cdev *);
    
    void cd_forget(struct inode *);
    
    extern struct backing_dev_info directly_mappable_cdev_bdi;
    
    #endif


    作者也不写个API的说明。。。以后用到其它的API再update。。。no zuo no die...


    没想到一个小时之后就update 了哈。。。。。


    update:2014年07月29日 凌晨


    Char Device Registration

                        As we mentioned, the kernel uses structures of type struct cdev to represent char devices internally. Before the kernel invokes your device’s operations, you must allocate and register one or more of these structures.

    先申请cdev结构体
                        To do so, your code should include <linux/cdev.h>, where the structure and its associated helper functions are defined.

                        There are two ways of allocating and initializing one of these structures. If you wish to obtain a standal one cdev structure at runtime, you may do so with code such as:

    struct cdev *my_cdev = cdev_alloc();
    my_cdev->ops = &my_fops;
    初始化cdev结构体

                     Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you should initialize the structure that you have already allocated with:

    void cdev_init(struct cdev *cdev, struct file_operations *fops);

                     Either way, there is one other struct cdev field that you need to initialize. Like the file_operations structure, struct cdev has an owner field that should be set to THIS_MODULE .(这里是,比如,struct cdev* dev; dev->owener = THIS_MODULE.)


    cdev结构体初始化完事之后,就须要把设备增加到内核中了,调用cdev_add

                     Once the cdev  structure is set up, the final step is to tell the kernel about it with a call to: int

    cdev_add(struct cdev *dev, dev_t num, unsigned int count);

                     Here,dev is the cdev structure,num is the first device number to which this device responds, and count is the number of device numbers that should be associated with the device. Often count is one, but there are situations where it makes sense to have more than one device number correspond to a specific device. Consider, for example, the SCSI tape driver, which allows user space to select operating modes (such as density) by assigning multiple minor numbers to each physical device.



    ATTENTION!

                    There are a couple of important things to keep in mind when using cdev_add . The first is that this call can fail. If it returns a negative error code, your device has not been added to the system. It almost always succeeds, however, and that brings up the other point: as soon as cdev_add returns, your device is “live” and its operations
    can be called by the kernel. You should not call cdev_add until your driver is completely ready to handle operations on the device.


    不用cdev设备的话就调用cdev_dev

                   To remove a char device from the system, call:

    void cdev_del(struct cdev *dev);

    Clearly, you should not access the cdev structure after passing it to cdev_del .



    还有几个API没用到,有缘遇到再说吧。。。哈哈哈
















  • 相关阅读:
    2017-3-13 SQL server 表连接
    2017-3-13 SQL server 函数(聚合函数,数学函数,字符串函数,转换函数,时间日期函数)
    2017-3-10 SQL server T-sql语句 高级查询
    2017-3-10 SQL server 数据库 T--SQL语句
    layer框架使用的问题汇总
    pulic——功能性(自己写完测试的)list转树
    bootstrap的datetimepicker使用(1.将默认的英文设置为中文2.选择日月年的时候记录之前的操作)
    临时笔记0318
    最简单的多线程代码,一句话就可以使用
    scrollview滑动到某区域执行某种方法
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4209248.html
Copyright © 2011-2022 走看看