Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [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 | |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <misc.h> |
| 9 | |
| 10 | struct misc_sandbox_priv { |
| 11 | u8 mem[128]; |
| 12 | ulong last_ioctl; |
| 13 | bool enabled; |
| 14 | }; |
| 15 | |
| 16 | int misc_sandbox_read(struct udevice *dev, int offset, void *buf, int size) |
| 17 | { |
| 18 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
| 19 | |
| 20 | memcpy(buf, priv->mem + offset, size); |
| 21 | |
Simon Glass | 587dc40 | 2018-11-06 15:21:39 -0700 | [diff] [blame] | 22 | return size; |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int misc_sandbox_write(struct udevice *dev, int offset, const void *buf, |
| 26 | int size) |
| 27 | { |
| 28 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
| 29 | |
| 30 | memcpy(priv->mem + offset, buf, size); |
| 31 | |
Simon Glass | 587dc40 | 2018-11-06 15:21:39 -0700 | [diff] [blame] | 32 | return size; |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int misc_sandbox_ioctl(struct udevice *dev, unsigned long request, void *buf) |
| 36 | { |
| 37 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
| 38 | |
| 39 | priv->last_ioctl = request; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int misc_sandbox_call(struct udevice *dev, int msgid, void *tx_msg, |
| 45 | int tx_size, void *rx_msg, int rx_size) |
| 46 | { |
| 47 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
| 48 | |
| 49 | if (msgid == 0) { |
| 50 | int num = *(int *)tx_msg; |
| 51 | |
| 52 | switch (num) { |
| 53 | case 0: |
| 54 | strncpy(rx_msg, "Zero", rx_size); |
| 55 | break; |
| 56 | case 1: |
| 57 | strncpy(rx_msg, "One", rx_size); |
| 58 | break; |
| 59 | case 2: |
| 60 | strncpy(rx_msg, "Two", rx_size); |
| 61 | break; |
| 62 | default: |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (msgid == 1) { |
| 68 | int num = *(int *)tx_msg; |
| 69 | |
| 70 | switch (num) { |
| 71 | case 0: |
| 72 | strncpy(rx_msg, "Forty", rx_size); |
| 73 | break; |
| 74 | case 1: |
| 75 | strncpy(rx_msg, "Forty-one", rx_size); |
| 76 | break; |
| 77 | case 2: |
| 78 | strncpy(rx_msg, "Forty-two", rx_size); |
| 79 | break; |
| 80 | default: |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (msgid == 2) |
| 86 | memcpy(rx_msg, &priv->last_ioctl, sizeof(priv->last_ioctl)); |
| 87 | |
| 88 | if (msgid == 3) |
| 89 | memcpy(rx_msg, &priv->enabled, sizeof(priv->enabled)); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int misc_sandbox_set_enabled(struct udevice *dev, bool val) |
| 95 | { |
| 96 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
| 97 | |
| 98 | priv->enabled = !priv->enabled; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static const struct misc_ops misc_sandbox_ops = { |
| 104 | .read = misc_sandbox_read, |
| 105 | .write = misc_sandbox_write, |
| 106 | .ioctl = misc_sandbox_ioctl, |
| 107 | .call = misc_sandbox_call, |
| 108 | .set_enabled = misc_sandbox_set_enabled, |
| 109 | }; |
| 110 | |
| 111 | int misc_sandbox_probe(struct udevice *dev) |
| 112 | { |
| 113 | struct misc_sandbox_priv *priv = dev_get_priv(dev); |
Sean Anderson | 13652b8 | 2022-05-05 13:11:44 -0400 | [diff] [blame] | 114 | /* For eth5 */ |
| 115 | const u8 mac[] = { 0x02, 0x00, 0x11, 0x22, 0x33, 0x46 }; |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 116 | |
| 117 | priv->enabled = true; |
Sean Anderson | 13652b8 | 2022-05-05 13:11:44 -0400 | [diff] [blame] | 118 | memcpy(&priv->mem[16], mac, sizeof(mac)); |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static const struct udevice_id misc_sandbox_ids[] = { |
| 124 | { .compatible = "sandbox,misc_sandbox" }, |
| 125 | { } |
| 126 | }; |
| 127 | |
| 128 | U_BOOT_DRIVER(misc_sandbox) = { |
| 129 | .name = "misc_sandbox", |
| 130 | .id = UCLASS_MISC, |
| 131 | .ops = &misc_sandbox_ops, |
| 132 | .of_match = misc_sandbox_ids, |
| 133 | .probe = misc_sandbox_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 134 | .priv_auto = sizeof(struct misc_sandbox_priv), |
Mario Six | a8ce0ee | 2018-07-31 14:24:14 +0200 | [diff] [blame] | 135 | }; |