summaryrefslogtreecommitdiff
path: root/drivers/misc/misc_sandbox.c
blob: f7c5b2e25fa03510f73425c5f675c9c4c8cdb1c2 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2018
 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
 */

#include <common.h>
#include <dm.h>
#include <misc.h>

struct misc_sandbox_priv {
	u8 mem[128];
	ulong last_ioctl;
	bool enabled;
};

int misc_sandbox_read(struct udevice *dev, int offset, void *buf, int size)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	memcpy(buf, priv->mem + offset, size);

	return size;
}

int misc_sandbox_write(struct udevice *dev, int offset, const void *buf,
		       int size)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	memcpy(priv->mem + offset, buf, size);

	return size;
}

int misc_sandbox_ioctl(struct udevice *dev, unsigned long request, void *buf)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	priv->last_ioctl = request;

	return 0;
}

int misc_sandbox_call(struct udevice *dev, int msgid, void *tx_msg,
		      int tx_size, void *rx_msg, int rx_size)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	if (msgid == 0) {
		int num = *(int *)tx_msg;

		switch (num) {
		case 0:
			strncpy(rx_msg, "Zero", rx_size);
			break;
		case 1:
			strncpy(rx_msg, "One", rx_size);
			break;
		case 2:
			strncpy(rx_msg, "Two", rx_size);
			break;
		default:
			return -EINVAL;
		}
	}

	if (msgid == 1) {
		int num = *(int *)tx_msg;

		switch (num) {
		case 0:
			strncpy(rx_msg, "Forty", rx_size);
			break;
		case 1:
			strncpy(rx_msg, "Forty-one", rx_size);
			break;
		case 2:
			strncpy(rx_msg, "Forty-two", rx_size);
			break;
		default:
			return -EINVAL;
		}
	}

	if (msgid == 2)
		memcpy(rx_msg, &priv->last_ioctl, sizeof(priv->last_ioctl));

	if (msgid == 3)
		memcpy(rx_msg, &priv->enabled, sizeof(priv->enabled));

	return 0;
}

int misc_sandbox_set_enabled(struct udevice *dev, bool val)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	priv->enabled = !priv->enabled;

	return 0;
}

static const struct misc_ops misc_sandbox_ops = {
	.read = misc_sandbox_read,
	.write = misc_sandbox_write,
	.ioctl = misc_sandbox_ioctl,
	.call = misc_sandbox_call,
	.set_enabled = misc_sandbox_set_enabled,
};

int misc_sandbox_probe(struct udevice *dev)
{
	struct misc_sandbox_priv *priv = dev_get_priv(dev);

	priv->enabled = true;

	return 0;
}

static const struct udevice_id misc_sandbox_ids[] = {
	{ .compatible = "sandbox,misc_sandbox" },
	{ }
};

U_BOOT_DRIVER(misc_sandbox) = {
	.name           = "misc_sandbox",
	.id             = UCLASS_MISC,
	.ops		= &misc_sandbox_ops,
	.of_match       = misc_sandbox_ids,
	.probe          = misc_sandbox_probe,
	.priv_auto_alloc_size = sizeof(struct misc_sandbox_priv),
};