Linux字符设备学习,总结

 注册字符驱动的一种老方法:

注册一个字符设备的经典方法是使用:
int register_chrdev(unsigned int major, const char *name, structfile_operations *fops);
这里, major 的主编号, name 是驱动的名子(出现在 /proc/devices),
fops   file_operations 结构. 一个对 register_chrdev 的调用为给定的主编号注册 0 - 255 的次编号, 并且为每一个建立一个缺省的 cdev 结构。使用这个接口的驱动必须准备好处理对所有 256 个次编号的 open 调用( 不管它们是否对应真实设备 ), 它们不能使用大于 255 的主或次编号。
如果你使用 register_chrdev, 从系统中去除你的设备的正确的函数是:

int unregister_chrdev(unsigned int major, const char *name);
major 和 name 必须和传递给 register_chrdev 的相同, 否则调用会失败.

 

现在常用的方法,cdev

包含 <linux/cdev.h>, 这个结构和它的关联帮助函数定义在这里.

/**
 * 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;
}

struct cdev 有一个拥有者成员, 应当设置为THIS_MODULE.

一旦 cdev 结构建立, 最后的步骤是把它告诉内核, 调用:
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

/**
 * 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); 
}

 这里, dev 是 cdev 结构, dev 是这个设备响应的第一个设备号, count 是应当关联到设备的设备号的数目. 常常 count 是 1

在使用 cdev_add 是有几个重要事情要记住. 除非你的驱动完全准备好处理设备上的操作, 否则你不应当调用 cdev_add。

第一个是这个调用可能失败. 如果它返回一个负的错误码, 你的设备没有增加到系统中.

它几乎会一直成功,但是, 并且带起了其他的点: cdev_add 返回, 你的设备就是"活的",并且内核可以调用它的操作. 

 

从系统去除一个字符设备, 调用:
void cdev_del(struct cdev *dev);

/**
 * 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);
}

显然, 你不应当在传递给 cdev_del 后存取 cdev 结构。

 引入 2 个核心函数来管理 Linux 内核中的内存. 这些函数, 定义
在 <linux/slab.h>, 是:
void *kmalloc(size_t size, int flags);

void kfree(void *ptr);

 

 

 1 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 2 
 3 /**
 4  * container_of - cast a member of a structure out to the containing structure
 5  * @ptr:        the pointer to the member.
 6  * @type:       the type of the container struct this is embedded in.
 7  * @member:     the name of the member within the struct.
 8  *
 9  */
10 #define container_of(ptr, type, member) ({                      \
11         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
12         (type *)( (char *)__mptr - offsetof(type,member) );})
container_of的功能,通过结构体成员的地址,获得结构体的首地址。

以下是简单用法:

 

转载于:https://www.cnblogs.com/jason-linux/p/10466303.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值