lib/kunit/platform.c

Source file repositories/reference/linux-study-clean/lib/kunit/platform.c

File Facts

System
Linux kernel
Corpus path
lib/kunit/platform.c
Extension
.c
Size
10362 bytes
Lines
367
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

struct kunit_platform_device_alloc_params {
	const char *name;
	int id;
};

static int kunit_platform_device_alloc_init(struct kunit_resource *res, void *context)
{
	struct kunit_platform_device_alloc_params *params = context;
	struct platform_device *pdev;

	pdev = platform_device_alloc(params->name, params->id);
	if (!pdev)
		return -ENOMEM;

	res->data = pdev;

	return 0;
}

static void kunit_platform_device_alloc_exit(struct kunit_resource *res)
{
	struct platform_device *pdev = res->data;

	platform_device_put(pdev);
}

/**
 * kunit_platform_device_alloc() - Allocate a KUnit test managed platform device
 * @test: test context
 * @name: device name of platform device to alloc
 * @id: identifier of platform device to alloc.
 *
 * Allocate a test managed platform device. The device is put when the test completes.
 *
 * Return: Allocated platform device on success, NULL on failure.
 */
struct platform_device *
kunit_platform_device_alloc(struct kunit *test, const char *name, int id)
{
	struct kunit_platform_device_alloc_params params = {
		.name = name,
		.id = id,
	};

	return kunit_alloc_resource(test,
				    kunit_platform_device_alloc_init,
				    kunit_platform_device_alloc_exit,
				    GFP_KERNEL, &params);
}
EXPORT_SYMBOL_GPL(kunit_platform_device_alloc);

static void kunit_platform_device_add_exit(struct kunit_resource *res)
{
	struct platform_device *pdev = res->data;

	platform_device_unregister(pdev);
}

static bool
kunit_platform_device_alloc_match(struct kunit *test,
				  struct kunit_resource *res, void *match_data)
{
	struct platform_device *pdev = match_data;

	return res->data == pdev && res->free == kunit_platform_device_alloc_exit;
}

KUNIT_DEFINE_ACTION_WRAPPER(platform_device_unregister_wrapper,
			    platform_device_unregister, struct platform_device *);
/**
 * kunit_platform_device_add() - Register a KUnit test managed platform device
 * @test: test context
 * @pdev: platform device to add
 *
 * Register a test managed platform device. The device is unregistered when the
 * test completes.
 *
 * Return: 0 on success, negative errno on failure.
 */
int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev)
{
	struct kunit_resource *res;
	int ret;

	ret = platform_device_add(pdev);
	if (ret)
		return ret;

	res = kunit_find_resource(test, kunit_platform_device_alloc_match, pdev);
	if (res) {

Annotation

Implementation Notes