{"record":{"id":"a4016a8efb7dcb81","repo":"rust-embedded/rust-raspberrypi-OS-tutorials","slug":"error-initializing-driver","errorCode":null,"errorMessage":"Error initializing driver: {}: {}","messagePattern":"Error initializing driver: (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"20_timer_callbacks/kernel/src/driver.rs","lineNumber":132,"sourceCode":"    }\n\n    /// Register a device driver with the kernel.\n    pub fn register_driver(&self, descriptor: DeviceDriverDescriptor<T>) {\n        self.descriptors\n            .write(|descriptors| descriptors.push(descriptor));\n    }\n\n    /// Fully initialize all drivers and their interrupts handlers.\n    ///\n    /// # Safety\n    ///\n    /// - During init, drivers might do stuff with system-wide impact.\n    pub unsafe fn init_drivers_and_irqs(&self) {\n        self.descriptors.read(|descriptors| {\n            for descriptor in descriptors {\n                // 1. Initialize driver.\n                if let Err(x) = descriptor.device_driver.init() {\n                    panic!(\n                        \"Error initializing driver: {}: {}\",\n                        descriptor.device_driver.compatible(),\n                        x\n                    );\n                }\n\n                // 2. Call corresponding post init callback.\n                if let Some(callback) = &descriptor.post_init_callback {\n                    if let Err(x) = callback() {\n                        panic!(\n                            \"Error during driver post-init callback: {}: {}\",\n                            descriptor.device_driver.compatible(),\n                            x\n                        );\n                    }\n                }\n            }\n","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/blob/644474cc09f755249f9c55d99a5d1e07a2562fc7/20_timer_callbacks/kernel/src/driver.rs#L114-L150","documentation":"During init_drivers_and_irqs, the driver manager iterates its registered driver descriptors and calls init() on each device driver. If any driver's init() returns Err, the manager panics with the driver's compatible name and the driver's own error string. Because init runs early in the kernel bring-up with system-wide impact, failures are treated as unrecoverable.","triggerScenarios":"A registered driver's DeviceDriver::init() implementation returns Err(&str) while init_drivers_and_irqs() walks the descriptor list.","commonSituations":"Hardware probing fails at boot (missing device, wrong MMIO base address in config); an init routine detects an incompatible peripheral version; a driver tries to access a resource (clock, power domain, register) that is not yet available.","solutions":["Read the compatible name and error string in the panic message to identify which driver failed and why.","Fix the driver's init() implementation so it succeeds on the target hardware (correct base address, clock, sequence).","If the failing driver should not be present, remove its registration from the driver manager instead of letting init fail.","Consider degrading to a warning for optional drivers whose init failure should not halt boot."],"exampleFix":"// before\nfn init(&self) -> Result<(), &'static str> {\n    let base = 0x0; // wrong MMIO base\n    self.probe(base)\n}\n\n// after\nfn init(&self) -> Result<(), &'static str> {\n    let base = self.mmio_base(); // correct per-board base\n    self.probe(base)\n}","handlingStrategy":"try-catch","validationCode":"// Rust panics abort the kernel, so guard before calling:\n// run driver self-checks before registering:\nassert!(driver.is_probed(), \"driver {} not probed before init\", driver.compatible());","typeGuard":null,"tryCatchPattern":"// panic! cannot be caught in kernel; instead make init() fallible and log:\nif let Err(x) = descriptor.device_driver.init() {\n    log_warn(\"driver init failed: {}: {}\", descriptor.device_driver.compatible(), x);\n    continue; // skip optional driver instead of panicking\n}","preventionTips":["Validate MMIO base addresses and clocks against the board's device tree/config before init.","Keep driver init idempotent and order drivers so dependencies initialize first.","Test init_drivers_and_irqs on the actual hardware target in CI, not only in QEMU."],"tags":["rust","driver","init","panic","boot"],"backgroundTag":"module-init-failed","analyzedSha":"644474cc09f755249f9c55d99a5d1e07a2562fc7","analyzedAt":"2026-09-06T09:25:56.584Z","contentChangedAt":"2026-09-06T09:25:56.584Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}