summaryrefstree
path: root/kernel/drivers/base/platform-mock.c
blob: e30c1af58c0f087be4230653ffa440e882c2c37b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-2.0
/*
 * Fake platform device API for unit testing platform drivers.
 *
 * Copyright (C) 2018, Google LLC.
 * Author: Brendan Higgins <brendanhiggins@google.com>
 */

#include <linux/klist.h>
#include <linux/platform_device_mock.h>
#include <linux/of_platform.h>
#include "base.h"

struct device_node *of_fake_node(struct test *test, const char *name)
{
	struct device_node *node;

	node = test_kzalloc(test, sizeof(*node), GFP_KERNEL);
	if (!node)
		return NULL;

	of_node_init(node);

	return node;
}

struct platform_device *
of_fake_probe_platform(struct test *test,
		       struct platform_driver *driver,
		       const char *node_name)
{
	struct platform_device *pdev;
	struct device_node *of_node;
	int ret;

	of_node = of_fake_node(test, node_name);
	if (!of_node)
		return ERR_PTR(-ENOMEM);

	test_info(test, "Creating device");
	pdev = of_platform_device_create(of_node, node_name, NULL);
	if (!pdev)
		return ERR_PTR(-ENODEV);

	test_info(test, "Probing");
	ret = driver->probe(pdev);
	if (ret)
		return ERR_PTR(ret);

	pdev->dev.driver = &driver->driver;
	klist_add_tail(&pdev->dev.p->knode_driver,
		       &pdev->dev.driver->p->klist_devices);
	if (ret)
		return ERR_PTR(ret);

	return pdev;
}

struct platform_device *of_fake_probe_platform_by_name(struct test *test,
						       const char *driver_name,
						       const char *node_name)
{
	const struct device_driver *driver;

	test_info(test, "Locating driver by name");
	driver = driver_find(driver_name, &platform_bus_type);
	if (!driver)
		return ERR_PTR(-ENODEV);

	return of_fake_probe_platform(test,
				      to_platform_driver(driver),
				      node_name);
}