c
/*
* Custom Hardware Peripheral (CHW) Memory-Mapped I/O Driver
*
* This is a Linux kernel module implementing a character device driver for a
* hypothetical custom hardware peripheral using memory-mapped I/O (MMIO).
*
* Hardware Assumptions (Custom Peripheral):
* - Base address: 0x10000000 (configurable via module param)
* - Register map (32-bit registers, little-endian):
* - 0x00: DATA_RW (RW) - Data register (e.g., write value to control output)
* - 0x04: CTRL_RW (RW) - Control register:
* | Bit 31: Reset (1 = reset peripheral)
* | Bit 0: Enable (1 = enable)
* - 0x08: STATUS_RO (RO) - Status register:
* | Bit 0: Ready (1 = peripheral ready)
* | Bit 1: Busy (1 = peripheral busy)
* - Register size: 0x0C bytes total.
*
* Usage:
* 1. insmod chw_mmio_driver.ko base_addr=0x10000000
* 2. mknod /dev/chw0 c 500 0 # major=500
* 3. echo "0x1234" > /dev/chw0 # Write to DATA
* 4. cat /dev/chw0 # Read STATUS
* 5. echo "enable" > /dev/chw0 # Write string commands to CTRL
* 6. rmmod chw_mmio_driver
*
* Compile: make
* Makefile snippet:
* obj-m += chw_mmio_driver.o
* all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
*
* Author: Sherlock (Trailblazer Labs AI)
* License: GPL v2
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/mm.h>
#define DRIVER_NAME "chw_mmio"
#define DEVICE_NAME "chw"
#define CLASS_NAME "chw_class"
#define REG_SIZE 0x0C
/* Register structure */
struct chw_regs {
volatile uint32_t data_rw; /* 0x00 RW: Data */
volatile uint32_t ctrl_rw; /* 0x04 RW: Control */
volatile uint32_t status_ro; /* 0x08 RO: Status */
};
/* Device private data */
struct chw_dev {
struct cdev cdev;
dev_t dev_no;
struct class *class;
struct device *device;
void __iomem *regs; /* MMIO base */
unsigned long base_addr;
};
/* Global driver data */
static struct chw_dev *chw_device;
static dev_t chw_major;
static struct class *chw_class;
/* File operations */
static int chw_open(struct inode *inode, struct file *file);
static int chw_release(struct inode *inode, struct file *file);
static ssize_t chw_read(struct file *file, char __user *buf, size_t len, loff_t *off);
static ssize_t chw_write(struct file *file, const char __user *buf, size_t len, loff_t *off);
static long chw_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static const struct file_operations chw_fops = {
.owner = THIS_MODULE,
.open = chw_open,
.release = chw_release,
.read = chw_read,
.write = chw_write,
.unlocked_ioctl = chw_ioctl,
};
/* IOCTL commands (example) */
#define CHW_IOCTL_READ_DATA _IOR('c', 1, uint32_t)
#define CHW_IOCTL_WRITE_CTRL _IOW('c', 2, uint32_t)
module_param_named(base_addr, chw_device->base_addr, ulong, 0644);
MODULE_PARM_DESC(base_addr, "Base address of CHW peripheral (default 0x10000000)");
static int chw_open(struct inode *inode, struct file *file)
{
file->private_data = chw_device;
return 0;
}
static int chw_release(struct inode *inode, struct file *file)
{
return 0;
}
static ssize_t chw_read(struct file *file, char __user *buf, size_t len, loff_t *off)
{
struct chw_dev *dev = file->private_data;
struct chw_regs __iomem *regs = dev->regs;
uint32_t status;
char status_str[32];
status = ioread32(®s->status_ro);
len = min(len, sizeof(status_str));
snprintf(status_str, sizeof(status_str), "0x%08x (ready:%d busy:%d)\n",
status, !!(status & 0x1), !!(status & 0x2));
if (*off >= strlen(status_str))
return 0;
if (*off + len > strlen(status_str))
len = strlen(status_str) - *off;
if (copy_to_user(buf, status_str + *off, len))
return -EFAULT;
*off += len;
return len;
}
static ssize_t chw_write(struct file *file, const char __user *buf, size_t len, loff_t *off)
{
struct chw_dev *dev = file->private_data;
struct chw_regs __iomem *regs = dev->regs;
char kbuf[64];
uint32_t val;
unsigned long parsed;
if (len >= sizeof(kbuf))
return -EINVAL;
if (copy_from_user(kbuf, buf, len))
return -EFAULT;
kbuf[len] = '\0';
/* Parse as hex or string command */
if (len > 2 && kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) {
if (kstrtoul(kbuf, 0, &parsed))
return -EINVAL;
val = parsed;
} else {
/* String commands for CTRL */
if (strncmp(kbuf, "enable", 6) == 0) {
iowrite32(1, ®s->ctrl_rw);
return len;
} else if (strncmp(kbuf, "disable", 7) == 0) {
iowrite32(0, ®s->ctrl_rw);
return len;
} else if (strncmp(kbuf, "reset", 5) == 0) {
iowrite32(0x80000000, ®s->ctrl_rw); /* Set reset bit */
iowrite32(0, ®s->ctrl_rw); /* Clear it */
return len;
}
return -EINVAL;
}
/* Default: write to DATA */
iowrite32(val, ®s->data_rw);
return len;
}
static long chw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct chw_dev *dev = file->private_data;
struct chw_regs __iomem *regs = dev->regs;
uint32_t val;
switch (cmd) {
case CHW_IOCTL_READ_DATA:
val = ioread32(®s->data_rw);
if (copy_to_user((uint32_t __user *)arg, &val, sizeof(val)))
return -EFAULT;
break;
case CHW_IOCTL_WRITE_CTRL:
if (copy_from_user(&val, (uint32_t __user *)arg, sizeof(val)))
return -EFAULT;
iowrite32(val, ®s->ctrl_rw);
break;
default:
return -ENOTTY;
}
return 0;
}
static int __init chw_init(void)
{
int ret;
struct chw_dev *dev;
/* Allocate device struct */
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -ENOMEM;
chw_device = dev;
/* Register char device */
ret = alloc_chrdev_region(&dev->dev_no, 0, 1, DEVICE_NAME);
if (ret < 0)
goto err_alloc;
chw_major = MAJOR(dev->dev_no);
/* Init cdev */
cdev_init(&dev->cdev, &chw_fops);
dev->cdev.owner = THIS_MODULE;
ret = cdev_add(&dev->cdev, dev->dev_no, 1);
if (ret < 0)
goto err_cdev;
/* Create class and device */
chw_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(chw_class)) {
ret = PTR_ERR(chw_class);
goto err_class;
}
dev->device = device_create(chw_class, NULL, dev->dev_no, NULL, DEVICE_NAME "%d", 0);
if (IS_ERR(dev->device)) {
ret = PTR_ERR(dev->device);
goto err_device;
}
/* Default base if not set */
if (!dev->base_addr)
dev->base_addr = 0x10000000;
/* MMIO setup */
if (!request_mem_region(dev->base_addr, REG_SIZE, DRIVER_NAME)) {
ret = -EBUSY;
goto err_region;
}
dev->regs = ioremap(dev->base_addr, REG_SIZE);
if (!dev->regs) {
ret = -ENOMEM;
goto err_ioremap;
}
pr_info("CHW MMIO driver loaded. Base: 0x%lx, Device: /dev/%s0\n",
dev->base_addr, DEVICE_NAME);
return 0;
err_ioremap:
release_mem_region(dev->base_addr, REG_SIZE);
err_region:
device_destroy(chw_class, dev->dev_no);
err_device:
class_destroy(chw_class);
err_class:
cdev_del(&dev->cdev);
err_cdev:
unregister_chrdev_region(dev->dev_no, 1);
err_alloc:
kfree(dev);
return ret;
}
static void __exit chw_exit(void)
{
struct chw_dev *dev = chw_device;
if (dev) {
iounmap(dev->regs);
release_mem_region(dev->base_addr, REG_SIZE);
device_destroy(chw_class, dev->dev_no);
class_destroy(chw_class);
cdev_del(&dev->cdev);
unregister_chrdev_region(dev->dev_no, 1);
kfree(dev);
}
pr_info("CHW MMIO driver unloaded.\n");
}
module_init(chw_init);
module_exit(chw_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Sherlock <Trailblazer Labs>");
MODULE_DESCRIPTION("Memory-mapped I/O driver for custom hardware peripheral");
MODULE_VERSION("1.0");
If you provide specifics (e.g., exact register map, architecture, bare-metal vs. kernel), I can refine this. 🕵️