Skip to content

cdev_add()

Purpose

Registers a struct cdev with the VFS so file operations can be reached through a device node.

#include <linux/cdev.h>

Prototype

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

Parameters

  • p: initialized struct cdev.
  • dev: first device number.
  • count: number of device numbers handled.

Return Value

  • Success: returns 0.
  • Failure: returns a negative errno value.

Minimal Example

cdev_init(&mydev->cdev, &mydev_fops);
mydev->cdev.owner = THIS_MODULE;
ret = cdev_add(&mydev->cdev, devt, 1);

Common Pitfalls

  • Call cdev_del() during cleanup.
  • Initialize file_operations before cdev_add().
  • Do not expose the device node before the driver state is ready.