From: "Marek Behún" <kabel@kernel.org>
To: Tom Rini <trini@konsulko.com>,
u-boot@lists.denx.de, Stefan Roese <sr@denx.de>
Cc: "Simon Glass" <sjg@chromium.org>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Nikita Kiryanov" <nikita@compulab.co.il>,
"Marek Behún" <kabel@kernel.org>
Subject: [PATCH 09/11] cmd: eeprom: Refactor command execution into function by action
Date: Tue, 21 May 2024 09:13:33 +0200 [thread overview]
Message-ID: <20240521071335.4193-10-kabel@kernel.org> (raw)
In-Reply-To: <20240521071335.4193-1-kabel@kernel.org>
Refactor the eeprom_execute_command() function into separate functions
do_eeprom_rw(), do_eeprom_print() and do_eeprom_update().
Signed-off-by: Marek Behún <kabel@kernel.org>
---
cmd/eeprom.c | 111 ++++++++++++++++++++++++++++++++-------------------
1 file changed, 71 insertions(+), 40 deletions(-)
diff --git a/cmd/eeprom.c b/cmd/eeprom.c
index e29639780d..c76cf43157 100644
--- a/cmd/eeprom.c
+++ b/cmd/eeprom.c
@@ -288,61 +288,75 @@ static enum eeprom_action parse_action(char *cmd)
return EEPROM_ACTION_INVALID;
}
-static int eeprom_execute_command(enum eeprom_action action,
- struct eeprom_dev_spec *dev,
- int layout_ver, char *key, char *value,
- ulong addr, ulong off, ulong cnt)
+static int do_eeprom_rw(struct eeprom_dev_spec *dev, bool read,
+ ulong addr, ulong off, ulong cnt)
{
- int rcode = 0;
const char *const fmt =
"\nEEPROM @0x%lX %s: addr 0x%08lx off 0x%04lx count %ld ... ";
+ uchar *memloc = (uchar *)addr;
+ int ret;
+
+ printf(fmt, dev->i2c_addr, read ? "read" : "write", addr, off, cnt);
+ if (read)
+ ret = eeprom_read(dev->i2c_addr, off, memloc, cnt);
+ else
+ ret = eeprom_write(dev->i2c_addr, off, memloc, cnt);
+ puts("done\n");
+
+ return ret;
+}
+
#ifdef CONFIG_CMD_EEPROM_LAYOUT
- struct eeprom_layout layout;
-#endif
- if (action == EEPROM_ACTION_INVALID)
- return CMD_RET_USAGE;
+static int do_eeprom_layout(struct eeprom_dev_spec *dev, int layout_ver,
+ struct eeprom_layout *layout)
+{
+ int ret;
- eeprom_init(dev->i2c_bus);
- if (action == EEPROM_READ) {
- printf(fmt, dev->i2c_addr, "read", addr, off, cnt);
+ ret = eeprom_read(dev->i2c_addr, 0, eeprom_buf, CONFIG_SYS_EEPROM_SIZE);
+ if (ret)
+ return ret;
- rcode = eeprom_read(dev->i2c_addr, off, (uchar *)addr, cnt);
+ eeprom_layout_setup(layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
+ layout_ver);
- puts("done\n");
- return rcode;
- } else if (action == EEPROM_WRITE) {
- printf(fmt, dev->i2c_addr, "write", addr, off, cnt);
+ return 0;
+}
- rcode = eeprom_write(dev->i2c_addr, off, (uchar *)addr, cnt);
+static int do_eeprom_print(struct eeprom_dev_spec *dev, int layout_ver)
+{
+ struct eeprom_layout layout;
+ int ret;
- puts("done\n");
- return rcode;
- }
+ ret = do_eeprom_layout(dev, layout_ver, &layout);
+ if (ret)
+ return ret;
-#ifdef CONFIG_CMD_EEPROM_LAYOUT
- rcode = eeprom_read(dev->i2c_addr, 0, eeprom_buf,
- CONFIG_SYS_EEPROM_SIZE);
- if (rcode < 0)
- return rcode;
+ layout.print(&layout);
- eeprom_layout_setup(&layout, eeprom_buf, CONFIG_SYS_EEPROM_SIZE,
- layout_ver);
+ return 0;
+}
- if (action == EEPROM_PRINT) {
- layout.print(&layout);
- return 0;
- }
+static int do_eeprom_update(struct eeprom_dev_spec *dev, int layout_ver,
+ char *key, char *value)
+{
+ struct eeprom_layout layout;
+ int ret;
- layout.update(&layout, key, value);
+ ret = do_eeprom_layout(dev, layout_ver, &layout);
+ if (ret)
+ return ret;
- rcode = eeprom_write(dev->i2c_addr, 0, layout.data,
- CONFIG_SYS_EEPROM_SIZE);
-#endif
+ ret = layout.update(&layout, key, value);
+ if (ret)
+ return CMD_RET_FAILURE;
- return rcode;
+ return eeprom_write(dev->i2c_addr, 0, layout.data,
+ CONFIG_SYS_EEPROM_SIZE);
}
+#endif
+
static int eeprom_action_expected_argc(enum eeprom_action action)
{
switch (action) {
@@ -361,13 +375,15 @@ static int eeprom_action_expected_argc(enum eeprom_action action)
#define NEXT_PARAM(argc, index) { (argc)--; (index)++; }
int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
- int layout_ver = LAYOUT_VERSION_AUTODETECT;
enum eeprom_action action = EEPROM_ACTION_INVALID;
struct eeprom_dev_spec dev;
ulong addr = 0, cnt = 0, off = 0;
int ret, index = 0;
+#ifdef CONFIG_CMD_EEPROM_LAYOUT
char *field_name = "";
char *field_value = "";
+ int layout_ver = LAYOUT_VERSION_AUTODETECT;
+#endif
if (argc <= 1)
return CMD_RET_USAGE;
@@ -416,8 +432,23 @@ int do_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
#endif
- return eeprom_execute_command(action, &dev, layout_ver, field_name,
- field_value, addr, off, cnt);
+ eeprom_init(dev.i2c_bus);
+
+ switch (action) {
+ case EEPROM_READ:
+ case EEPROM_WRITE:
+ return do_eeprom_rw(&dev, action == EEPROM_READ,
+ addr, off, cnt);
+#ifdef CONFIG_CMD_EEPROM_LAYOUT
+ case EEPROM_PRINT:
+ return do_eeprom_print(&dev, layout_ver);
+ case EEPROM_UPDATE:
+ return do_eeprom_update(&dev, layout_ver,
+ field_name, field_value);
+#endif
+ default:
+ return CMD_RET_USAGE;
+ }
}
#ifdef CONFIG_EEPROM_LAYOUT_VERSIONS
--
2.44.1
next prev parent reply other threads:[~2024-05-21 7:15 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-21 7:13 [PATCH 00/11] 'eeprom' command improvements Marek Behún
2024-05-21 7:13 ` [PATCH 01/11] common: eeprom_layout: Assign default layout methods and parameters before specific ones Marek Behún
2024-05-21 7:13 ` [PATCH 02/11] common: eeprom_layout: Split field finding code from the field update function Marek Behún
2024-05-21 7:13 ` [PATCH 03/11] common: eeprom_field: Fix updating binary field Marek Behún
2024-05-21 7:13 ` [PATCH 04/11] common: eeprom_field: Drop unnecessary comparison Marek Behún
2024-05-21 7:13 ` [PATCH 05/11] cmd: eeprom: Fix usage help for the eeprom command Marek Behún
2024-05-21 7:13 ` [PATCH 06/11] cmd: eeprom: Hide eeprom layout versioning behind a Kconfig option Marek Behún
2024-05-21 7:13 ` [PATCH 07/11] cmd: eeprom: Deduplicate parse_i2c_bus_addr() calls Marek Behún
2024-05-21 7:13 ` [PATCH 08/11] cmd: eeprom: Refactor eeprom device specifier parsing Marek Behún
2024-05-21 7:13 ` Marek Behún [this message]
2024-05-21 7:13 ` [PATCH 10/11] cmd: eeprom: Don't read/write whole EEPROM if not necessary Marek Behún
2024-05-21 7:13 ` [PATCH 11/11] cmd: eeprom: Extend to EEPROMs probed via driver model Marek Behún
2024-06-07 18:57 ` [PATCH 00/11] 'eeprom' command improvements Tom Rini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240521071335.4193-10-kabel@kernel.org \
--to=kabel@kernel.org \
--cc=ilias.apalodimas@linaro.org \
--cc=nikita@compulab.co.il \
--cc=sjg@chromium.org \
--cc=sr@denx.de \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox