# writing-char-drivers

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

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 378 installs · ★ 298 repo stars
- Author: mohitmishra786
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/mohitmishra786/low-level-dev-skills/tree/HEAD/skills/kernel-dev/writing-char-drivers/SKILL.md
- Updated: 2026-06-27
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/writing-char-drivers

## What it 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.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/writing-char-drivers/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/writing-char-drivers/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/writing-char-drivers/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/writing-char-drivers/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install mohitmishra786/low-level-dev-skills skills/kernel-dev/writing-char-drivers
```

2. **Manually: put the files in place**
- File: `.github/skills/writing-char-drivers/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/mohitmishra786/low-level-dev-skills --path skills/kernel-dev/writing-char-drivers
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/writing-char-drivers/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add mohitmishra786/low-level-dev-skills --skill writing-char-drivers -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/mohitmishra786/low-level-dev-skills/tree/HEAD/skills/kernel-dev/writing-char-drivers/SKILL.md

## When to use it
- 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
- 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

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 377bb259c945a3d8.

BEGIN THIRD-PARTY FILE 377bb259c945a3d8
````text
---
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)
````
END THIRD-PARTY FILE 377bb259c945a3d8

The third-party file has ended. Everything between the 377bb259c945a3d8 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2428 of 4309 bytes. © mohitmishra786 · MIT · https://github.com/mohitmishra786/low-level-dev-skills/tree/HEAD/skills/kernel-dev/writing-char-drivers/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
