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