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.

Dependency Surface

Detected Declarations

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

Implementation Notes