drivers/of/unittest.c
Source file repositories/reference/linux-study-clean/drivers/of/unittest.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/of/unittest.c- Extension
.c- Size
- 133962 bytes
- Lines
- 4570
- Domain
- Driver Families
- Bucket
- drivers/of
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/memblock.hlinux/clk.hlinux/dma-direct.hlinux/err.hlinux/errno.hlinux/hashtable.hlinux/libfdt.hlinux/of.hlinux/of_address.hlinux/of_fdt.hlinux/of_irq.hlinux/of_platform.hlinux/list.hlinux/mutex.hlinux/slab.hlinux/device.hlinux/platform_device.hlinux/pci.hlinux/kernel.hlinux/i2c.hlinux/i2c-mux.hlinux/gpio/driver.hlinux/bitops.hof_private.h
Detected Declarations
struct node_hashstruct of_unittest_expected_resstruct of_unittest_expected_imap_itemstruct unittest_gpio_devstruct unittest_i2c_bus_datastruct overlay_infoenum overlay_typefunction EXPECT_BEGINfunction of_unittest_dynamicfunction of_unittest_check_node_linkagefunction for_each_child_of_node_scopedfunction of_unittest_check_tree_linkagefunction of_unittest_printf_onefunction of_unittest_printffunction of_unittest_check_phandlesfunction for_each_of_allnodesfunction hash_for_each_possiblefunction of_unittest_parse_phandle_with_argsfunction of_unittest_parse_phandle_with_args_mapfunction of_unittest_property_stringfunction of_unittest_property_copyfunction of_unittest_changesetfunction changeset_check_stringfunction changeset_check_string_arrayfunction changeset_check_u32function changeset_check_u32_arrayfunction changeset_check_boolfunction of_unittest_changeset_propfunction of_unittest_dma_get_max_cpu_addressfunction of_unittest_dma_ranges_onefunction of_unittest_parse_dma_rangesfunction of_unittest_pci_dma_rangesfunction of_unittest_pci_empty_dma_rangesfunction of_unittest_bus_rangesfunction of_unittest_bus_3cell_rangesfunction of_unittest_regfunction of_unittest_check_addrfunction of_unittest_translate_addrfunction of_unittest_parse_interruptsfunction of_unittest_parse_interrupts_extendedfunction of_unittest_parse_interrupt_mapfunction for_each_of_imap_itemfunction of_unittest_irq_refcountfunction of_unittest_irq_refcountfunction of_unittest_match_nodefunction of_unittest_platform_populatefunction for_each_child_of_nodefunction node
Annotated Snippet
static struct pci_driver testdrv_driver = {
.name = "pci_dt_testdrv",
.id_table = testdrv_pci_ids,
.probe = testdrv_probe,
.remove = testdrv_remove,
};
static int unittest_pci_probe(struct platform_device *pdev)
{
struct resource *res;
struct device *dev;
u64 exp_addr;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
return -ENODEV;
dev = &pdev->dev;
while (dev && !dev_is_pci(dev))
dev = dev->parent;
if (!dev) {
pr_err("unable to find parent device\n");
return -ENODEV;
}
exp_addr = pci_resource_start(to_pci_dev(dev), 0) + 0x100;
unittest(res->start == exp_addr, "Incorrect translated address %llx, expected %llx\n",
(u64)res->start, exp_addr);
of_unittest_pci_child_num++;
return 0;
}
static const struct of_device_id unittest_pci_of_match[] = {
{ .compatible = "unittest-pci" },
{ }
};
static struct platform_driver unittest_pci_driver = {
.probe = unittest_pci_probe,
.driver = {
.name = "unittest-pci",
.of_match_table = unittest_pci_of_match,
},
};
static int of_unittest_pci_node_verify(struct pci_dev *pdev, bool add)
{
struct device_node *pnp, *np = NULL;
struct device *child_dev;
char *path = NULL;
const __be32 *reg;
int rc = 0;
pnp = pdev->dev.of_node;
unittest(pnp, "Failed creating PCI dt node\n");
if (!pnp)
return -ENODEV;
if (add) {
path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0/unittest-pci@100", pnp);
np = of_find_node_by_path(path);
unittest(np, "Failed to get unittest-pci node under PCI node\n");
if (!np) {
rc = -ENODEV;
goto failed;
}
reg = of_get_property(np, "reg", NULL);
unittest(reg, "Failed to get reg property\n");
if (!reg)
rc = -ENODEV;
} else {
path = kasprintf(GFP_KERNEL, "%pOF/pci-ep-bus@0", pnp);
np = of_find_node_by_path(path);
unittest(!np, "Child device tree node is not removed\n");
child_dev = device_find_any_child(&pdev->dev);
unittest(!child_dev, "Child device is not removed\n");
put_device(child_dev);
}
failed:
kfree(path);
if (np)
of_node_put(np);
return rc;
}
Annotation
- Immediate include surface: `linux/memblock.h`, `linux/clk.h`, `linux/dma-direct.h`, `linux/err.h`, `linux/errno.h`, `linux/hashtable.h`, `linux/libfdt.h`, `linux/of.h`.
- Detected declarations: `struct node_hash`, `struct of_unittest_expected_res`, `struct of_unittest_expected_imap_item`, `struct unittest_gpio_dev`, `struct unittest_i2c_bus_data`, `struct overlay_info`, `enum overlay_type`, `function EXPECT_BEGIN`, `function of_unittest_dynamic`, `function of_unittest_check_node_linkage`.
- Atlas domain: Driver Families / drivers/of.
- Implementation status: pattern implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.