Skip to content

alloc_chrdev_region()

Purpose

Allocates a dynamic major/minor device number range for a character device.

#include <linux/fs.h>

Prototype

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name);

Parameters

  • dev: output device number.
  • baseminor: first minor number.
  • count: number of minor numbers.
  • name: device name shown in /proc/devices.

Return Value

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

Minimal Example

dev_t devt;
ret = alloc_chrdev_region(&devt, 0, 1, "mydev");

Common Pitfalls

  • Call unregister_chrdev_region() during cleanup.
  • Store both major and minor numbers if needed.
  • Prefer dynamic allocation for learning drivers and reusable modules.