Simon Glass | 3a8ee3d | 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 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <sysinfo.h> |
| 10 | |
| 11 | #include "sandbox.h" |
| 12 | |
| 13 | struct sysinfo_sandbox_priv { |
| 14 | bool called_detect; |
| 15 | int test_i1; |
| 16 | int test_i2; |
| 17 | }; |
| 18 | |
| 19 | char vacation_spots[][64] = {"R'lyeh", "Dreamlands", "Plateau of Leng", |
| 20 | "Carcosa", "Yuggoth", "The Nameless City"}; |
| 21 | |
| 22 | int 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 | |
| 32 | int 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 | |
| 46 | int 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 | |
| 66 | int 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 | |
| 83 | static const struct udevice_id sysinfo_sandbox_ids[] = { |
| 84 | { .compatible = "sandbox,sysinfo-sandbox" }, |
| 85 | { /* sentinel */ } |
| 86 | }; |
| 87 | |
| 88 | static 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 | |
| 95 | int sysinfo_sandbox_probe(struct udevice *dev) |
| 96 | { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | U_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 | }; |