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函数

猜你喜欢

转载自www.cnblogs.com/Suzkfly/p/11768813.html