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.
Workflow Overview
Section titled “Workflow Overview”1. Describe hardware → temperature_sensor.kdh2. Implement logic → temp_sensor_driver.kdc3. Lint → kdality lint4. Compile → kdality compile → TempSensorDriver.c + Makefile.kbuild5. 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.
Step 1 - Device Header
Section titled “Step 1 - Device Header”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; }}Key points
Section titled “Key points”- Every register has a type (
u8,u16,u32,u64orbitfield), 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 --strictreports W010 without it)- Signals declare a direction (
in,out) and a trigger (edge(...),level(...)) - A
configfield declares a type, a default and an allowed range
Step 2 - Driver Implementation
Section titled “Step 2 - Driver Implementation”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"); }}Event handler reference
Section titled “Event handler reference”| 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 |
Built-in operations
Section titled “Built-in operations”| 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 |
Step 3 - Lint
Section titled “Step 3 - Lint”kdality lint temperature_sensor.kdh --strictkdality lint temp_sensor_driver.kdc --strictThe 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.
Step 4 - Compile
Section titled “Step 4 - Compile”kdality compile -o build/ temp_sensor_driver.kdcThis produces:
build/TempSensorDriver.c- the module sourcebuild/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)”make -C /lib/modules/$(uname -r)/build M=$(pwd)/build modules
# Cross-compile for aarch64make -C $KDIR M=$(pwd)/build \ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modulesStep 6 - Load and Verify (after O7)
Section titled “Step 6 - Load and Verify (after O7)”sudo insmod build/TempSensorDriver.kokdality listkdality info TempSensorDriversudo rmmod TempSensorDriverCommon Patterns
Section titled “Common Patterns”These fragments go inside a driver block that imports the header above as
DEV; together they pass kdality lint and compile.
Register access with bitfields
Section titled “Register access with bitfields”let res = read(DEV.CONFIG.resolution);write(DEV.CONFIG.mode, 1);Conditional logic
Section titled “Conditional logic”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.
Bounded loops
Section titled “Bounded loops”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.
Timers
Section titled “Timers”probe { arm(DEV.data_ready, 1000);}
remove { cancel(DEV.data_ready);}
on timeout(DEV.data_ready) { log("no response within 1 second\n");}Generated Code
Section titled “Generated Code”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.