Skip to content

Writing Drivers

This guide walks through writing a KDAL driver using the two-file model: a device header (.kdh) that declares hardware resources and a driver implementation (.kdc) that contains event-handler logic. The language and compiler are frozen since 2026-11-01.

1. Describe hardware → temperature_sensor.kdh
2. Implement logic → temp_sensor_driver.kdc
3. Lint → kdality lint
4. Compile → kdality compile → TempSensorDriver.c + Makefile.kbuild
5. Build → make -C $KDIR M=$PWD/build modules (fails today: O7)
6. Load → insmod TempSensorDriver.ko (after O7)

kdality simulate runs the handlers without a kernel at any point after step 2.

Create temperature_sensor.kdh, which models the hardware’s registers, signals, capabilities, power states and configuration:

kdal_version: "0.1";
device class TemperatureSensor {
compatible "acme,temp-sensor";
registers {
CTRL : u8 @ 0x00 rw = 0x00;
STATUS : u8 @ 0x01 ro;
TEMP_DATA : u16 @ 0x02 ro;
CONFIG : bitfield @ 0x04 rw {
resolution : 0..1 = 2;
mode : 2..3 = 0;
enable : 4 = false;
};
}
signals {
data_ready : in edge(rising);
alert : out level(high);
}
capabilities {
max_sample_rate = 100;
resolution_bits = 12;
}
power {
on : allowed;
off : allowed;
suspend : allowed;
default : on;
}
config {
sample_rate : u32 = 50 in 1..100;
}
}
  • Every register has a type (u8, u16, u32, u64 or bitfield), an offset (@ 0x..) and an access qualifier (ro, wo, rw); a reset value (= ...) is optional
  • A bitfield’s bits are a single bit (4) or a range written low to high (0..1); a range written high to low (1..0) is an error
  • compatible "vendor,name"; gives the Device Tree compatible string (kdality lint --strict reports W010 without it)
  • Signals declare a direction (in, out) and a trigger (edge(...), level(...))
  • A config field declares a type, a default and an allowed range

Create temp_sensor_driver.kdc, which imports the header under an alias and handles events; every name from the header is written through the alias (DEV.CTRL, DEV.CONFIG.enable):

kdal_version: "0.1";
import "temperature_sensor.kdh" as DEV;
backend mmio {
base_addr: 0x10000000;
}
driver TempSensorDriver for DEV.TemperatureSensor {
config {
sample_rate = 50;
}
probe {
write(DEV.CONFIG.enable, true);
write(DEV.CONFIG.resolution, 3);
write(DEV.CTRL, 0x01);
log("TempSensor: probed\n");
}
remove {
write(DEV.CTRL, 0x00);
log("TempSensor: removed\n");
}
on read(DEV.TEMP_DATA) {
wait(DEV.data_ready, 100);
}
on signal(DEV.data_ready) {
let status = read(DEV.STATUS);
if (status == 0) {
log("TempSensor: no data\n");
} else {
let temp = read(DEV.TEMP_DATA);
log("TempSensor: reading %d\n", temp);
emit(DEV.alert, temp);
}
}
on power(on -> suspend) {
write(DEV.CTRL, 0x00);
log("TempSensor: suspended\n");
}
on power(suspend -> on) {
write(DEV.CTRL, 0x01);
log("TempSensor: resumed\n");
}
}
Handler Trigger
probe {} Device bind
remove {} Device unbind
on read(reg) {} Userspace read request
on write(reg, value) {} Userspace write request
on signal(name) {} Hardware interrupt
on power(from -> to) {} Power state transition
on timeout(name) {} An armed timer expires
Operation Description
read(reg) Read a register or bitfield
write(reg, value) Write a register or bitfield
emit(signal [, val]) Raise an output signal
wait(signal, ms) Wait for a signal, with a timeout
arm(name, ms) Start a timer
cancel(name) Cancel a timer
log(fmt, ...) Kernel debug message (dev_dbg)
let name = expr; A local binding in the handler
Terminal window
kdality lint temperature_sensor.kdh --strict
kdality lint temp_sensor_driver.kdc --strict

The codes are listed on the kdality page. Two limits of the frozen tool: lint on a .kdh prints the compiler’s errors (for example a bitfield range written high to low) but still reports “OK (0 warnings)” and exits 0; and compile also exits 0 when an imported header does not parse. Read the messages, not only the exit status.

Terminal window
kdality compile -o build/ temp_sensor_driver.kdc

This produces:

  • build/TempSensorDriver.c - the module source
  • build/Makefile.kbuild - the kbuild fragment

Step 5 - Build the Kernel Module (fails today: O7)

Section titled “Step 5 - Build the Kernel Module (fails today: O7)”
Terminal window
make -C /lib/modules/$(uname -r)/build M=$(pwd)/build modules
# Cross-compile for aarch64
make -C $KDIR M=$(pwd)/build \
ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules
Terminal window
sudo insmod build/TempSensorDriver.ko
kdality list
kdality info TempSensorDriver
sudo rmmod TempSensorDriver

These fragments go inside a driver block that imports the header above as DEV; together they pass kdality lint and compile.

let res = read(DEV.CONFIG.resolution);
write(DEV.CONFIG.mode, 1);
on read(DEV.TEMP_DATA) {
let status = read(DEV.STATUS);
if (status == 1) {
log("ready\n");
} elif (status == 2) {
log("busy\n");
} else {
log("sensor not ready\n");
}
}

Only structured control flow exists: no while, goto, break or continue.

probe {
for i in 0..8 {
write(DEV.CTRL, i);
}
}

A for range is meant to be a compile-time constant, so a handler always terminates; the compiler warns, without stopping, when it cannot tell.

probe {
arm(DEV.data_ready, 1000);
}
remove {
cancel(DEV.data_ready);
}
on timeout(DEV.data_ready) {
log("no response within 1 second\n");
}

The probe that kdality compile generates for the driver above, as is:

static int TempSensorDriver_probe(struct platform_device *pdev)
{
struct TempSensorDriver_ctx *ctx;
struct resource *res;
ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
if (!ctx) return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
ctx->base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(ctx->base)) return PTR_ERR(ctx->base);
ctx->kdal.dev = &pdev->dev;
ctx->kdal.ops = &TempSensorDriver_ops;
platform_set_drvdata(pdev, ctx);
kdal_reg_write(&ctx->kdal, ctx->regs.CONFIG.enable, 1);
kdal_reg_write(&ctx->kdal, ctx->regs.CONFIG.resolution, 3);
kdal_reg_write(&ctx->kdal, ctx->regs.CTRL, 1);
dev_dbg(ctx->kdal.dev, "TempSensor: probed\n");
return kdal_driver_register(&ctx->kdal);
}

kdal_reg_write and the regs member it reads are among what O7 found undefined.