【Linux-驱动】简单字符设备驱动结构和初始化

(1)在编写简单字符设备驱动的时候,首先要申请一个设备结构struct cdev:

struct cdev {
    struct kobject kobj;
    struct module *owner;              /* 所属模块 */
    const struct file_operations *ops; /* 对应的操作函数集 */
    struct list_head list;             /* 对应的链表 */
    dev_t dev;                         /* 设备号 */
    unsigned int count;                /* 设备计数 */
};

申请完成后首先要使用函数cdev_init进行初始化这个cdev结构体:

/**
 * cdev_init() - initialize a cdev structure
 * @cdev: 要初始化的字符设备结构体
 * @fops: 设备操作函数集
 */
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
    memset(cdev, 0, sizeof *cdev);                  /* 将整个结构置0 */
    INIT_LIST_HEAD(&cdev->list);                    /* 将cdev->list的前后都指向自己 */                    
    kobject_init(&cdev->kobj, &ktype_cdev_default); /* 初始化kobj */
    cdev->ops = fops;                               /* 将操作函数集赋值给这个结构体 */
}

 

posted @ 2017-03-23 07:12  LinuxRookie  阅读(418)  评论(0编辑  收藏  举报