blob: 62a1cb4ac65f51e47f1a163c880d627cd6eb6157 [file] [log] [blame]
Simon Glass458b66a2020-11-05 06:32:05 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <sysinfo.h>
10
11#include "sandbox.h"
12
13struct sysinfo_sandbox_priv {
14 bool called_detect;
15 int test_i1;
16 int test_i2;
17};
18
19char vacation_spots[][64] = {"R'lyeh", "Dreamlands", "Plateau of Leng",
20 "Carcosa", "Yuggoth", "The Nameless City"};
21
22int sysinfo_sandbox_detect(struct udevice *dev)
23{
24 struct sysinfo_sandbox_priv *priv = dev_get_priv(dev);
25
26 priv->called_detect = true;
27 priv->test_i2 = 100;
28
29 return 0;
30}
31
32int sysinfo_sandbox_get_bool(struct udevice *dev, int id, bool *val)
33{
34 struct sysinfo_sandbox_priv *priv = dev_get_priv(dev);
35
36 switch (id) {
37 case BOOL_CALLED_DETECT:
38 /* Checks if the dectect method has been called */
39 *val = priv->called_detect;
40 return 0;
41 }
42
43 return -ENOENT;
44}
45
46int sysinfo_sandbox_get_int(struct udevice *dev, int id, int *val)
47{
48 struct sysinfo_sandbox_priv *priv = dev_get_priv(dev);
49
50 switch (id) {
51 case INT_TEST1:
52 *val = priv->test_i1;
53 /* Increments with every call */
54 priv->test_i1++;
55 return 0;
56 case INT_TEST2:
57 *val = priv->test_i2;
58 /* Decrements with every call */
59 priv->test_i2--;
60 return 0;
61 }
62
63 return -ENOENT;
64}
65
66int sysinfo_sandbox_get_str(struct udevice *dev, int id, size_t size, char *val)
67{
68 struct sysinfo_sandbox_priv *priv = dev_get_priv(dev);
69 int i1 = priv->test_i1;
70 int i2 = priv->test_i2;
71 int index = (i1 * i2) % ARRAY_SIZE(vacation_spots);
72
73 switch (id) {
74 case STR_VACATIONSPOT:
75 /* Picks a vacation spot depending on i1 and i2 */
76 snprintf(val, size, vacation_spots[index]);
77 return 0;
78 }
79
80 return -ENOENT;
81}
82
83static const struct udevice_id sysinfo_sandbox_ids[] = {
84 { .compatible = "sandbox,sysinfo-sandbox" },
85 { /* sentinel */ }
86};
87
88static const struct sysinfo_ops sysinfo_sandbox_ops = {
89 .detect = sysinfo_sandbox_detect,
90 .get_bool = sysinfo_sandbox_get_bool,
91 .get_int = sysinfo_sandbox_get_int,
92 .get_str = sysinfo_sandbox_get_str,
93};
94
95int sysinfo_sandbox_probe(struct udevice *dev)
96{
97 return 0;
98}
99
100U_BOOT_DRIVER(sysinfo_sandbox) = {
101 .name = "sysinfo_sandbox",
102 .id = UCLASS_SYSINFO,
103 .of_match = sysinfo_sandbox_ids,
104 .ops = &sysinfo_sandbox_ops,
105 .priv_auto_alloc_size = sizeof(struct sysinfo_sandbox_priv),
106 .probe = sysinfo_sandbox_probe,
107};