linux设备注册

Posted Suzkfly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux设备注册相关的知识,希望对你有一定的参考价值。

一、分配cdev

  cdev表示字符设备,使用cdev_alloc函数,cdev_alloc函数原型如下;

/**
 * cdev_alloc() - allocate a cdev structure
 *
 * Allocates and returns a cdev structure, or NULL on failure.
 */
struct cdev *cdev_alloc(void)

  得到cdev指针

二、初始化cdev

  使用cdev_init函数,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)

  将字符设备和设备的操作集合绑定在一起。

三、注册cdev

  注册cdev用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)

四、注销cdev

  用cdev_del函数

以上是关于linux设备注册的主要内容,如果未能解决你的问题,请参考以下文章

一张图掌握 Linux 字符设备驱动架构!建议收藏

一张图掌握 Linux 字符设备驱动架构!建议收藏

Linux——Linux驱动之总线设备驱动注册流程分析及详细操作步骤(上)

Linux——Linux驱动之总线设备驱动注册流程分析及详细操作步骤(上)

Linux字符设备驱动

Linux设备驱动-内核如何管理设备号