cdev结构体及相关函数

cdev结构体及相关函数

① 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结构体

② cdev_init()函数

/**

 * cdev_init() - initialize a cdev structure

 * @cdev: the structure to initialize

 * @fops: the file_operations for this device

 *

 * Initializes @cdev, remembering @fops, making it ready to add to the

 * system with cdev_add().

 */

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

{

memset(cdev, 0, sizeof *cdev);

INIT_LIST_HEAD(&cdev->list);

kobject_init(&cdev->kobj, &ktype_cdev_default);

cdev->ops = fops;

}

③ cdev_add()  向系统注册一个字符设备

/**

 * cdev_add() - add a char device to the system

 * @p: the cdev structure for the device

 * @dev: the first device number for which this device is responsible

 * @count: the number of consecutive minor numbers corresponding to this

 *         device

 *

 * cdev_add() adds the device represented by @p to the system, making it

 * live immediately.  A negative error code is returned on failure.

 */

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

{

p->dev = dev;

p->count = count;

return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);

}

④ cdev_del() 从系统删除一个字符设备

/**

 * cdev_del() - remove a cdev from the system

 * @p: the cdev structure to be removed

 *

 * cdev_del() removes @p from the system, possibly freeing the structure

 * itself.

 */

void cdev_del(struct cdev *p) //指针

{

cdev_unmap(p->dev, p->count);

kobject_put(&p->kobj);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值