1. Prepare the test setup
Reuse the wiring and adxl345_id.c from Chapter 07. If you switched to the IIO driver, restore the lab compatible string.
compatible = "example,adxl345-id";
Rebuild and deploy the overlay, then reboot. Replace spi0.0 in the examples if the device has a different name under /sys/bus/spi/devices/.
2. Add a controlled failure path
Add this header to adxl345_id.c.
#include <linux/device.h>
Add this parameter and cleanup callback before adxl345_id_probe().
static bool fail_probe;
module_param(fail_probe, bool, 0644);
MODULE_PARM_DESC(fail_probe, "Fail probe after reading a valid device ID");
static void adxl345_id_cleanup(void *data)
{
struct device *dev = data;
dev_info(dev, "cleanup action ran\n");
}
Replace the previous probe function with the version below. Keep the existing registration code, Device Tree match table, and Makefile.
static int adxl345_id_probe(struct spi_device *spi)
{
struct device *dev = &spi->dev;
struct regmap *map;
unsigned int id;
int ret;
map = devm_regmap_init_spi(spi, &adxl345_id_regmap);
if (IS_ERR(map))
return dev_err_probe(dev, PTR_ERR(map),
"cannot initialize SPI regmap\n");
ret = devm_add_action_or_reset(dev, adxl345_id_cleanup, dev);
if (ret)
return dev_err_probe(dev, ret, "cannot register cleanup action\n");
ret = regmap_read(map, ADXL345_REG_DEVID, &id);
if (ret)
return dev_err_probe(dev, ret, "cannot read device ID\n");
if (id != ADXL345_DEVICE_ID) {
dev_err(dev, "unexpected device ID: 0x%02x\n", id);
return -ENODEV;
}
dev_info(dev, "read device ID: 0x%02x\n", id);
if (fail_probe) {
dev_info(dev, "injecting probe failure\n");
return -EIO;
}
dev_info(dev, "probe succeeded\n");
return 0;
}
The regmap is device-managed. The extra cleanup action prints a message so you can see when cleanup occurs. Linux runs the action and releases the managed resources after a failed probe or an unbind.
3. Verify the failure path
Rebuild against the target kernel. If the previous module is loaded, remove it with sudo rmmod adxl345_id before loading the new build.
make -C "/lib/modules/$(uname -r)/build" M="$PWD" modules
sudo insmod ./adxl345_id.ko fail_probe=1
Check the kernel log and binding status.
device=/sys/bus/spi/devices/spi0.0
sudo dmesg | grep -E 'device ID|injecting probe|cleanup action' | tail -n 8
if [ -L "$device/driver" ]; then
readlink "$device/driver"
else
echo 'spi0.0 is unbound'
fi
Expect this sequence.
read device ID: 0xe5
injecting probe failure
cleanup action ran
Module insertion may still succeed even though the device’s probe failed. Use the spi0.0/driver link and the probe log to determine binding status.
4. Disable fault injection and bind manually
Keep the module loaded and use the same terminal.
driver=/sys/bus/spi/drivers/adxl345-id
printf '0\n' | sudo tee /sys/module/adxl345_id/parameters/fail_probe >/dev/null
printf 'spi0.0\n' | sudo tee "$driver/bind" >/dev/null
readlink "$device/driver"
sudo dmesg | grep -E 'device ID|probe succeeded' | tail -n 4
Expect probe succeeded and a driver link that points to adxl345-id. Binding reruns probe(), recreates the regmap, and reads the ID again.
5. Verify unbind and rebind
printf 'spi0.0\n' | sudo tee "$driver/unbind" >/dev/null
test ! -L "$device/driver" && echo 'spi0.0 is unbound'
printf 'spi0.0\n' | sudo tee "$driver/bind" >/dev/null
readlink "$device/driver"
sudo dmesg | grep -E 'device ID|probe succeeded|cleanup action' | tail -n 8
| Operation | Expected result |
|---|---|
| Forced probe failure | ID is read, cleanup runs, and the device remains unbound |
| First bind | ID is read again and a driver link appears |
| Unbind | Cleanup runs and the driver link disappears |
| Rebind | Resources are acquired again and binding succeeds |
Unload with sudo rmmod adxl345_id when done.
Extension
Apply the same tests to /dev/demo-events. Extend the driver so unbinding wakes readers waiting in read() or poll(). Define the behavior after the FIFO drains, such as returning -ENODEV from read() and reporting POLLHUP from poll(). Rebind and verify that a newly opened reader receives events.