Skip to main content
skill CX Codex

writing-char-drivers — Claude Code skill: Linux /dev drivers

writing-char-drivers — a skill for writing Linux character drivers: cdev registration, file_operations implementation, safe userspace copies, ioctl and mmap.

MIT 378 installs ★ 298 updated 2026-06-27

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a claude-code
Open on GitHub

What writing-char-drivers does

Teaches the agent to create Linux character devices with full lifecycle: allocate major/minor via alloc_chrdev_region, initialize cdev, populate file_operations structure with open, read, write, unlocked_ioctl methods. Shows safe data transfer between kernel and userspace using copy_to_user and copy_from_user, defines ioctl commands with _IO, _IOW, _IOWR macros and a fixed magic number, maps device physical memory into process address space via remap_pfn_range with pgprot_noncached.

Includes a table of typical errors: -EFAULT on invalid userspace pointer, ENOTTY on magic mismatch in ioctl, static major conflict, SIGSEGV in mmap due to cached mapping of MMIO regions. Activates on mentions of cdev, file_operations, copy_from_user, ioctl, kernel mmap. Code requires kernel headers (<linux/fs.h>, <linux/cdev.h>, <linux/uaccess.h>) to compile.

What it helps with

  • register a character device with dynamic major allocation
  • implement read and write with safe userspace copies
  • add unlocked_ioctl with commands via _IOW and _IOWR
  • map device MMIO region to userspace via mmap
  • handle -EFAULT and ENOTTY in a driver

Example prompts for the agent

  • Add SET_SPEED unlocked_ioctl to the existing platform driver
  • Register cdev with dynamic major and create /dev/mydev
  • Implement my_read with copy_to_user and offset check

How to install writing-char-drivers

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a claude-code

2Manually: put the files in place

~/.claude/skills/writing-char-drivers/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/writing-char-drivers/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about writing-char-drivers

How to install writing-char-drivers in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a claude-code. The skill will appear in ~/.claude/skills/writing-char-drivers/SKILL.md.

What does writing-char-drivers do?

The skill teaches the agent to write Linux character drivers: register cdev, populate file_operations, transfer data via copy_to_user and copy_from_user, create ioctl commands and map device memory to userspace.

What does writing-char-drivers require?

Linux kernel headers (<linux/fs.h>, <linux/cdev.h>, <linux/uaccess.h>) and a kernel module build environment. Code will not compile without them.

Source text

SKILL.md · MIT
---
name: writing-char-drivers
description: Character driver skill for Linux kernel char devices. Use when implementing file_operations, cdev, copy_to_user, ioctl, or mmap for userspace interfaces. Activates on queries about char device, cdev, file_operations, copy_from_user, ioctl, or kernel mmap.
---

# Writing Character Drivers

## Purpose

Guide agents through Linux character device implementation: `struct file_operations`, `cdev` registration, safe userspace copies, `ioctl` design, and basic `mmap` — focused depth beyond `skills/kernel/device-drivers`.

## When to Use

- Exposing hardware to `/dev/mydev`
- Implementing `read`/`write`/`poll` from kernel
- Defining `ioctl` commands with type-safe macros
- Mapping device MMIO to userspace (carefully)

## Workflow

### 1. Char device registration

```c
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>

#define MY_MAJOR 0   /* 0 = dynamic alloc */
#define MY_MINOR 0

static dev_t devno;
static struct cdev my_cdev;
static struct class *class;

static const struct file_operations my_fops = {
    .owner          = THIS_MODULE,
    .open           = my_open,
    .release        = my_release,
    .read           = my_read,
    .write          = my_write,
    .unlocked_ioctl = my_ioctl,
    .llseek         = no_llseek,
};

static int __init my_init(void)
{
    int ret = alloc_chrdev_region(&devno, MY_MINOR, 1, "mydev");
    if (ret)
        return ret;

    cdev_init(&my_cdev, &my_fops);
    ret = cdev_add(&my_cdev, devno, 1);
    if (ret)
        goto err_cdev;

    class = class_create("mydev");
    device_create(class, NULL, devno, NULL, "mydev");
    return 0;

err_cdev:
    unregister_chrdev_region(devno, 1);
    return ret;
}
```

Modern drivers often use `devm_*` variants inside `probe`.

### 2. Safe userspace I/O

```c
static ssize_t my_read(struct file *filp, char __user *buf,
                       size_t count, loff_t *ppos)
{
    char kbuf[128];
    ssize_t len;

    if (*ppos >= sizeof(kbuf))
        return 0;
    len = min(count, sizeof(kbuf) - *ppos);
    memcpy(kbuf, "data", 4);
    if (copy_to_user(buf, kbuf + *ppos, len))
        return -EFAULT;
    *ppos += len;
    return len;
}
```

Never dereference `__user` pointers directly.

### 3. ioctl pattern

```c
#include <linux/ioctl.h>

#define MY_IOC_MAGIC 'k'
#define MY_IOC_RESET  _IO(MY_IOC_MAGIC, 0)
#define MY_IOC_SET    _IOW(MY_IOC_MAGIC, 1, int)

static long my_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
    case MY_IOC_RESET:
        return 0;
    case MY_IOC_SET: {
        int val;
        if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
            return -EFAULT;
        return 0;
    }
    default:
        return -ENOTTY;
    }
}
```

Use `_IOWR` with fixed-size structs; prefer `compat_ioctl` on bi-arch.

### 4. mmap (device memory)

```c
static int my_mmap(struct file *filp, struct vm_area_struct *vma)
{
    unsigned long size = vma->vm_end - vma->vm_start;
    phys_addr_t phys = device_phys_base;

    vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
    return remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
                           size, vma->vm_page_prot);
}
```

Prefer `mmap` of DMA buffers only with explicit size limits and permission checks.

### 5. poll / async I/O

Implement `poll` + `wake_up_interruptible` for blocking reads; use `fasync_helper` for SIGIO.

### 6. Agent usage

```
/writing-char-drivers Add unlocked_ioctl SET_SPEED to existing platform driver
```

## Common Problems

| Symptom | Cause | Fix |
|---------|-------|-----|
| `-EFAULT` | Bad user pointer | Validate `access_ok` (older) / rely on `copy_*` |
| `ENOTTY` | Wrong ioctl magic | Match userspace `ioctl.h` |
| Major conflict | Static major taken | Use `alloc_chrdev_region` |
| mmap SIGSEGV | Cached mapping to device | `pgprot_noncached` |
| Sleep in ioctl | Holding spinlock | Drop lock before blocking |

## Related Skills

- `skills/kernel-dev/platform-device-model` — probe context
- `skills/kernel/device-drivers` — full driver lifecycle
- `skills/kernel/kernel-concurrency` — locking in file ops
- `skills/kernel-dev/kernel-debugging-advanced` — trace ioctl path
- `skills/low-level-programming/linux-kernel-modules` — module boilerplate

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Backend and APIs →

From the same set140

The whole set →
os-dev-scratch os-dev-scratch is a Claude Code skill for building a minimal x86-64 OS, from boot stages to drivers and context switching. It activates on questions about kernel boot, interrupts, page tables, or QEMU. CX Codex +1 381installs kernel-security kernel-security — a skill for SELinux and AppArmor policies, seccomp-bpf sandboxing, KASLR, Intel CET, ARM PAC and BTI. Write LSM rules, triage kernel CVEs, and configure kernel mitigations. CX Codex +1 381installs peripherals-from-datasheet peripherals-from-datasheet guides driver development directly from MCU reference manuals. Helpful when writing bare-metal register code without a vendor HAL or migrating peripherals across chips. CX Codex +1 380installs platform-device-model The platform-device-model skill explains Linux platform-driver binding, probe and remove, sysfs, device properties, and deferred probing for kernel work. CX Codex +1 378installs adc-dac-baremetal adc-dac-baremetal — a skill for bare-metal ADC and DAC setup on MCUs: channel selection, sampling time, calibration, DMA buffers, and voltage output. Essential when working with analog sensors, audio signals, or control voltages. CX Codex +1 378installs resource-optimization-lowend The resource-optimization-lowend skill helps trim flash and RAM use in MCU firmware, inspect linker maps, and measure stack depth when embedded resources are tight. CX Codex +1 376installs llvm-passes llvm-passes guides coding agents in creating, building, and verifying LLVM optimization passes with the New Pass Manager. CX Codex +1 373installs openmp Teaches the agent to write and fix OpenMP code: parallel loops, reductions, tasks, SIMD hints and GPU offload. CX Codex +1 371installs

Project sponsors

Companies that keep this analytics open