diff mbox series

[1/5] mmc-utils: lsmmc: Reuse do_read_reg

Message ID 20230926100006.1789276-2-avri.altman@wdc.com
State New
Headers show
Series Revamp CSD register parsing | expand

Commit Message

Avri Altman Sept. 26, 2023, 10 a.m. UTC
All 3 register parsing: CID, CSD, and CSR follow the same structure -
reuse it instead of duplicating the code 3 times. No functional change.

Signed-off-by: Avri Altman <avri.altman@wdc.com>
---
 lsmmc.c | 59 +++++++++++++++++++--------------------------------------
 1 file changed, 19 insertions(+), 40 deletions(-)

Comments

Ulf Hansson Sept. 27, 2023, 11:05 a.m. UTC | #1
On Tue, 26 Sept 2023 at 12:02, Avri Altman <avri.altman@wdc.com> wrote:
>
> All 3 register parsing: CID, CSD, and CSR follow the same structure -
> reuse it instead of duplicating the code 3 times. No functional change.
>
> Signed-off-by: Avri Altman <avri.altman@wdc.com>

Applied to git.kernel.org/pub/scm/utils/mmc/mmc-utils.git master, thanks!

Kind regards
Uffe


> ---
>  lsmmc.c | 59 +++++++++++++++++++--------------------------------------
>  1 file changed, 19 insertions(+), 40 deletions(-)
>
> diff --git a/lsmmc.c b/lsmmc.c
> index 54c3167..85779bb 100644
> --- a/lsmmc.c
> +++ b/lsmmc.c
> @@ -2373,7 +2373,6 @@ int lsmmc_main(struct config *config, int argc, char **argv)
>  {
>         int ret;
>
> -       memset(config, 0, sizeof(*config));
>         config->mmc_ids = calloc(IDS_MAX, sizeof(char *));
>         config->sd_ids = calloc(IDS_MAX, sizeof(char *));
>         if (!config->mmc_ids || !config->sd_ids) {
> @@ -2395,71 +2394,51 @@ void lsmmc_free(struct config *config)
>         free(config->dir);
>  }
>
> -int do_read_csd(int argc, char **argv)
> +static int do_read_reg(int argc, char **argv, enum REG_TYPE reg)
>  {
> -       struct config config;
> +       struct config cfg = {};
>         int ret;
>
> -       if (argc != 2 && argc != 3) {
> -               fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
> -               exit(1);
> -       }
> -
> -       ret = lsmmc_main(&config, argc, argv);
> +       ret = lsmmc_main(&cfg, argc, argv);
>         if (ret)
>                 goto out;
>
> -       if (config.dir)
> -               ret = process_dir(&config, CSD);
> +       if (cfg.dir)
> +               ret = process_dir(&cfg, reg);
>
>  out:
> -       lsmmc_free(&config);
> +       lsmmc_free(&cfg);
>
>         return ret;
> +
>  }
>
> -int do_read_cid(int argc, char **argv)
> +int do_read_csd(int argc, char **argv)
>  {
> -       struct config config;
> -       int ret;
> -
>         if (argc != 2 && argc != 3) {
> -               fprintf(stderr, "Usage: Print CID data from <device path>.\n");
> +               fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
>                 exit(1);
>         }
>
> -       ret = lsmmc_main(&config, argc, argv);
> -       if (ret)
> -               goto out;
> -
> -       if (config.dir)
> -               ret = process_dir(&config, CID);
> +       return do_read_reg(argc, argv, CSD);
> +}
>
> -out:
> -       lsmmc_free(&config);
> +int do_read_cid(int argc, char **argv)
> +{
> +       if (argc != 2 && argc != 3) {
> +               fprintf(stderr, "Usage: Print CID data from <device path>.\n");
> +               exit(1);
> +       }
>
> -       return ret;
> +       return do_read_reg(argc, argv, CID);
>  }
>
>  int do_read_scr(int argc, char **argv)
>  {
> -       struct config config;
> -       int ret;
> -
>         if (argc != 2 && argc != 3) {
>                 fprintf(stderr, "Usage: Print SCR data from <device path>.\n");
>                 exit(1);
>         }
>
> -       ret = lsmmc_main(&config, argc, argv);
> -       if (ret)
> -               goto out;
> -
> -       if (config.dir)
> -               ret = process_dir(&config, SCR);
> -
> -out:
> -       lsmmc_free(&config);
> -
> -       return ret;
> +       return do_read_reg(argc, argv, SCR);
>  }
> --
> 2.42.0
>
diff mbox series

Patch

diff --git a/lsmmc.c b/lsmmc.c
index 54c3167..85779bb 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -2373,7 +2373,6 @@  int lsmmc_main(struct config *config, int argc, char **argv)
 {
 	int ret;
 
-	memset(config, 0, sizeof(*config));
 	config->mmc_ids = calloc(IDS_MAX, sizeof(char *));
 	config->sd_ids = calloc(IDS_MAX, sizeof(char *));
 	if (!config->mmc_ids || !config->sd_ids) {
@@ -2395,71 +2394,51 @@  void lsmmc_free(struct config *config)
 	free(config->dir);
 }
 
-int do_read_csd(int argc, char **argv)
+static int do_read_reg(int argc, char **argv, enum REG_TYPE reg)
 {
-	struct config config;
+	struct config cfg = {};
 	int ret;
 
-	if (argc != 2 && argc != 3) {
-		fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
-		exit(1);
-	}
-
-	ret = lsmmc_main(&config, argc, argv);
+	ret = lsmmc_main(&cfg, argc, argv);
 	if (ret)
 		goto out;
 
-	if (config.dir)
-		ret = process_dir(&config, CSD);
+	if (cfg.dir)
+		ret = process_dir(&cfg, reg);
 
 out:
-	lsmmc_free(&config);
+	lsmmc_free(&cfg);
 
 	return ret;
+
 }
 
-int do_read_cid(int argc, char **argv)
+int do_read_csd(int argc, char **argv)
 {
-	struct config config;
-	int ret;
-
 	if (argc != 2 && argc != 3) {
-		fprintf(stderr, "Usage: Print CID data from <device path>.\n");
+		fprintf(stderr, "Usage: Print CSD data from <device path>.\n");
 		exit(1);
 	}
 
-	ret = lsmmc_main(&config, argc, argv);
-	if (ret)
-		goto out;
-
-	if (config.dir)
-		ret = process_dir(&config, CID);
+	return do_read_reg(argc, argv, CSD);
+}
 
-out:
-	lsmmc_free(&config);
+int do_read_cid(int argc, char **argv)
+{
+	if (argc != 2 && argc != 3) {
+		fprintf(stderr, "Usage: Print CID data from <device path>.\n");
+		exit(1);
+	}
 
-	return ret;
+	return do_read_reg(argc, argv, CID);
 }
 
 int do_read_scr(int argc, char **argv)
 {
-	struct config config;
-	int ret;
-
 	if (argc != 2 && argc != 3) {
 		fprintf(stderr, "Usage: Print SCR data from <device path>.\n");
 		exit(1);
 	}
 
-	ret = lsmmc_main(&config, argc, argv);
-	if (ret)
-		goto out;
-
-	if (config.dir)
-		ret = process_dir(&config, SCR);
-
-out:
-	lsmmc_free(&config);
-
-	return ret;
+	return do_read_reg(argc, argv, SCR);
 }