</>EmbeddedInterview

How do you write a character driver?

MediumLinux Kernel & Device Drivers

Short answer: Allocate a device number (alloc_chrdev_region), set up a struct cdev with your file_operations and add it (cdev_init, cdev_add), create a class and device so udev creates the /dev node (class_create, device_create), implement the operations (open, release, read, write, unlocked_ioctl, poll, mmap), and undo everything in reverse order on exit and on error paths. For a single simple device, misc_register() does the number, cdev and node creation in one call.

Minimal misc-device driver with a buffer

#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>

#define BUF_SZ 256
static char buf[BUF_SZ];
static size_t len;
static DEFINE_MUTEX(lock);

static ssize_t demo_read(struct file *f, char __user *ubuf, size_t n, loff_t *pos)
{
    ssize_t ret;
    mutex_lock(&lock);
    ret = simple_read_from_buffer(ubuf, n, pos, buf, len);   /* handles *pos and copy_to_user */
    mutex_unlock(&lock);
    return ret;
}

static ssize_t demo_write(struct file *f, const char __user *ubuf, size_t n, loff_t *pos)
{
    if (n > BUF_SZ) n = BUF_SZ;
    mutex_lock(&lock);
    if (copy_from_user(buf, ubuf, n)) { mutex_unlock(&lock); return -EFAULT; }
    len = n;
    mutex_unlock(&lock);
    return n;
}

static const struct file_operations demo_fops = {
    .owner = THIS_MODULE,
    .read  = demo_read,
    .write = demo_write,
    .llseek = default_llseek,
};

static struct miscdevice demo_dev = {
    .minor = MISC_DYNAMIC_MINOR,
    .name  = "demo",                 /* creates /dev/demo */
    .fops  = &demo_fops,
    .mode  = 0666,
};
module_misc_device(demo_dev);
MODULE_LICENSE("GPL");

Test: echo hello > /dev/demo; cat /dev/demo.

Full cdev path (for multiple minors or more control)

static dev_t devt; static struct cdev cdev; static struct class *cls;

static int __init my_init(void)
{
    int ret = alloc_chrdev_region(&devt, 0, 1, "mydev");
    if (ret) return ret;
    cdev_init(&cdev, &demo_fops);
    ret = cdev_add(&cdev, devt, 1);
    if (ret) goto err_region;
    cls = class_create("mydev");                     /* 6.4+: no THIS_MODULE argument */
    if (IS_ERR(cls)) { ret = PTR_ERR(cls); goto err_cdev; }
    if (IS_ERR(device_create(cls, NULL, devt, NULL, "mydev0"))) { ret = -ENOMEM; goto err_class; }
    return 0;
err_class:  class_destroy(cls);
err_cdev:   cdev_del(&cdev);
err_region: unregister_chrdev_region(devt, 1);
    return ret;
}

Important details

  • read/write return the number of bytes transferred, 0 for end-of-file, or a negative error code (-EFAULT, -EAGAIN for non-blocking with no data, -ERESTARTSYS if interrupted by a signal).
  • Update *pos for seekable devices.
  • Use file->private_data (set in open) to reach per-device state; with a real device, use container_of(inode->i_cdev, struct mydev, cdev).
  • Protect shared state: several processes can call read/write concurrently.
  • In real hardware drivers, create the device from probe() so it exists only when the hardware does.