summaryrefslogtreecommitdiffstats
path: root/drivers/hw_random/dev-random.c
blob: 2170db7437ced60fc94b417158518975b6ed37cd (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2019 Ahmad Fatoum, Pengutronix
 */

#include <common.h>
#include <init.h>
#include <linux/hw_random.h>
#include <mach/linux.h>

struct devrandom_rnd {
	struct hwrng hwrng;
	devrandom_t *priv;
};

static inline struct devrandom_rnd *to_rnd(struct hwrng *hwrng)
{
	return container_of(hwrng, struct devrandom_rnd, hwrng);
}

static int devrandom_rnd_read(struct hwrng *hwrng, void *buf, size_t max, bool wait)
{
	return devrandom_read(to_rnd(hwrng)->priv, buf, max, wait);
}

static int devrandom_rnd_init(struct hwrng *hwrng)
{
	devrandom_t *devrandom = devrandom_init();
	if (IS_ERR(devrandom))
		return PTR_ERR(devrandom);

	to_rnd(hwrng)->priv = devrandom;
	return 0;
}

static int devrandom_rnd_probe(struct device_d *dev)
{
	struct devrandom_rnd *rnd;
	int ret;

	rnd = xzalloc(sizeof(*rnd));

	rnd->hwrng.name = dev->name;
	rnd->hwrng.read = devrandom_rnd_read;
	rnd->hwrng.init = devrandom_rnd_init;

	ret = hwrng_register(dev, &rnd->hwrng);
	if (ret) {
		dev_err(dev, "failed to register: %s\n", strerror(-ret));

		return ret;
	}

	dev_info(dev, "Registered.\n");

	return 0;
}

static struct driver_d devrandom_rnd_driver = {
	.name	= "devrandom",
	.probe	= devrandom_rnd_probe,
};
device_platform_driver(devrandom_rnd_driver);