Character Device Basics¶
A character device driver exposes a file-like interface to user space. User-space applications interact with the device through normal file operations such as open, read, write, poll, ioctl, and close.
What Problem It Solves¶
A character device is useful when a device or kernel service can be represented as a byte stream, command interface, or event source.
Typical examples in this learning repo include:
/dev/mygpio/dev/mypoll- event-driven GPIO drivers
- pollable timer/event drivers
Basic Registration Flow¶
alloc_chrdev_region()
↓
cdev_init()
↓
cdev_add()
↓
class_create()
↓
device_create()
↓
/dev/<device-name>
Key Concepts¶
| Concept | Purpose |
|---|---|
dev_t |
Encodes major and minor numbers |
struct cdev |
Connects a character device to file_operations |
struct file_operations |
Defines driver callbacks for user-space operations |
device_create() |
Creates a device node under /dev through udev/devtmpfs |
Minimal Driver Shape¶
static const struct file_operations mydev_fops = {
.owner = THIS_MODULE,
.open = mydev_open,
.read = mydev_read,
.write = mydev_write,
.release = mydev_release,
};
Common Pitfalls¶
Warning
Creating a cdev does not automatically create /dev/<name>. The driver also needs a device class and device_create(), or the node must be created manually with mknod.
Note
DEVICE_NAME is the user-space device node name. It is separate from the Device Tree node name and the compatible string.
Related Labs¶
- Day05 - Kernel Module
- Day06 - Character Device Source
- Day09 - GPIO Driver
- Day50 - Pollable Character Driver