[U-Boot] [PATCH 3/6] stdio: dm: Make stdio_devices[] local

Andreas Bießmann
2012-09-01T23:11:43Z

Dear Marek Vasut,

On 01.09.12 00:44, Marek Vasut wrote:
> Use stdio_get_fd() and stdio_set_fd() instead of direct access
> to the array. This allows making stdio_devices[] local to stdio.c
>
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Wolfgang Denk <wd@denx.de>
> ---
> common/cmd_console.c | 8 +++----
> common/cmd_terminal.c | 9 ++++++--
> common/console.c | 60 ++++++++++++++++++++++++++++++++-----------------
> common/fdt_support.c | 8 ++++++-
> common/stdio.c | 2 +-
> include/stdio_dev.h | 1 -
> 6 files changed, 58 insertions(+), 30 deletions(-)
>
> diff --git a/common/cmd_console.c b/common/cmd_console.c
> index d8cad6b..343bc28 100644
> --- a/common/cmd_console.c
> +++ b/common/cmd_console.c
> @@ -34,7 +34,7 @@ int do_coninfo (cmd_tbl_t * cmd, int flag, int argc, char * const argv[])
> int l;
> struct list_head *list = stdio_get_list();
> struct list_head *pos;
> - struct stdio_dev *dev;
> + struct stdio_dev *dev, *sio;
>
> /* Scan for valid output and input devices */
>
> @@ -51,9 +51,9 @@ int do_coninfo (cmd_tbl_t * cmd, int flag, int argc, char * const argv[])
> (dev->flags & DEV_FLAGS_OUTPUT) ? 'O' : '.');
>
> for (l = 0; l < MAX_FILES; l++) {
> - if (stdio_devices[l] == dev) {
> - printf ("%s ", stdio_names[l]);
> - }
> + sio = stdio_get_fd(l);
> + if (sio == dev)
> + printf("%s ", stdio_names[l]);
> }
> putc ('\n');
> }
> diff --git a/common/cmd_terminal.c b/common/cmd_terminal.c
> index 7cc1a6c..ba34033 100644
> --- a/common/cmd_terminal.c
> +++ b/common/cmd_terminal.c
> @@ -33,6 +33,7 @@ int do_terminal(cmd_tbl_t * cmd, int flag, int argc, char * const argv[])
> {
> int last_tilde = 0;
> struct stdio_dev *dev = NULL;
> + struct stdio_dev *sio;
>
> if (argc < 1)
> return -1;
> @@ -46,12 +47,16 @@ int do_terminal(cmd_tbl_t * cmd, int flag, int argc, char * const argv[])
> printf("Entering terminal mode for port %s\n", dev->name);
> puts("Use '~.' to leave the terminal and get back to u-boot\n");
>
> + sio = stdio_get_dev(stdin);
> + if (!sio)
> + return -1;
> +
> while (1) {
> int c;
>
> /* read from console and display on serial port */
> - if (stdio_devices[0]->tstc()) {
> - c = stdio_devices[0]->getc();
> + if (sio->tstc()) {
> + c = sio->getc();
> if (last_tilde == 1) {
> if (c == '.') {
> putc(c);
> diff --git a/common/console.c b/common/console.c
> index df03cef..4a1938a 100644
> --- a/common/console.c
> +++ b/common/console.c
> @@ -64,7 +64,7 @@ static int console_setfile(int file, struct stdio_dev * dev)
> }
>
> /* Assign the new device (leaving the existing one started) */
> - stdio_devices[file] = dev;
> + stdio_set_fd(file, dev);
>
> /*
> * Update monitor functions
> @@ -170,27 +170,39 @@ static inline void console_doenv(int file, struct stdio_dev *dev)
> #else
> static inline int console_getc(int file)
> {
> - return stdio_devices[file]->getc();
> + struct stdio_dev *dev = stdio_get_fd(file);
> + if (dev)
> + return dev->getc();
> + return 0;
> }
>
> static inline int console_tstc(int file)
> {
> - return stdio_devices[file]->tstc();
> + struct stdio_dev *dev = stdio_get_fd(file);
> + if (dev)
> + return dev->tstc();
> + return 0;
> }
>
> static inline void console_putc(int file, const char c)
> {
> - stdio_devices[file]->putc(c);
> + struct stdio_dev *dev = stdio_get_fd(file);
> + if (dev)
> + return dev->putc(c);

return is not required here ..

> }
>
> static inline void console_puts(int file, const char *s)
> {
> - stdio_devices[file]->puts(s);
> + struct stdio_dev *dev = stdio_get_fd(file);
> + if (dev)
> + return dev->puts(s);

.. and here

> }
>
> static inline void console_printdevs(int file)
> {
> - printf("%s\n", stdio_devices[file]->name);
> + struct stdio_dev *dev = stdio_get_fd(file);
> + if (dev)
> + printf("%s\n", dev->name);
> }
>
> static inline void console_doenv(int file, struct stdio_dev *dev)
> @@ -592,27 +604,28 @@ int console_init_f(void)
> void stdio_print_current_devices(void)
> {
> #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
> + struct stdio_dev *sio;
> /* Print information */
> puts("In: ");
> - if (stdio_devices[stdin] == NULL) {
> + sio = stdio_get_fd(stdin);
> + if (sio == NULL)
> puts("No input devices available!\n");
> - } else {
> - printf ("%s\n", stdio_devices[stdin]->name);
> - }
> + else
> + printf("%s\n", sio->name);
>
> puts("Out: ");
> - if (stdio_devices[stdout] == NULL) {
> + sio = stdio_get_fd(stdout);

Isn't sio still set properly here ...

> + if (sio == NULL)
> puts("No output devices available!\n");
> - } else {
> - printf ("%s\n", stdio_devices[stdout]->name);
> - }
> + else
> + printf("%s\n", sio->name);
>
> puts("Err: ");
> - if (stdio_devices[stderr] == NULL) {
> + sio = stdio_get_fd(stderr);

.. and here?

> + if (sio == NULL)

Side note: in your newly introduced checks you use just

if (sio) {
...

Why check explicitly for NULL here? I personally favor 'if (!sio)'.

> puts("No error devices available!\n");
> - } else {
> - printf ("%s\n", stdio_devices[stderr]->name);
> - }
> + else
> + printf("%s\n", sio->name);
> #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
> }
>
> @@ -622,6 +635,7 @@ int console_init_r(void)
> {
> char *stdinname, *stdoutname, *stderrname;
> struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
> + struct stdio_dev *sio;
> #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
> int i;
> #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
> @@ -690,7 +704,9 @@ done:
> #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
> /* set the environment variables (will overwrite previous env settings) */
> for (i = 0; i < 3; i++) {
> - setenv(stdio_names[i], stdio_devices[i]->name);
> + sio = stdio_get_fd(i);
> + if (sio)
> + setenv(stdio_names[i], sio->name);
> }
> #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
>
> @@ -706,7 +722,7 @@ int console_init_r(void)
> int i;
> struct list_head *list = stdio_get_list();
> struct list_head *pos;
> - struct stdio_dev *dev;
> + struct stdio_dev *dev, *sio;
>
> #ifdef CONFIG_SPLASH_SCREEN
> /*
> @@ -759,7 +775,9 @@ int console_init_r(void)
>
> /* Setting environment variables */
> for (i = 0; i < 3; i++) {
> - setenv(stdio_names[i], stdio_devices[i]->name);
> + sio = stdio_get_fd(i);
> + if (sio)
> + setenv(stdio_names[i], sio->name);
> }
>
> return 0;
> diff --git a/common/fdt_support.c b/common/fdt_support.c
> index 593f16c..7727359 100644
> --- a/common/fdt_support.c
> +++ b/common/fdt_support.c
> @@ -97,7 +97,13 @@ int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
> #ifdef CONFIG_SERIAL_MULTI
> static void fdt_fill_multisername(char *sername, size_t maxlen)
> {
> - const char *outname = stdio_devices[stdout]->name;
> + struct stdio_dev *sio = stdio_get_fd(stdout);
> + const char *outname;
> +
> + if (!sio)
> + return;
> +
> + outname = sio->name;
>
> if (strcmp(outname, "serial") > 0)
> strncpy(sername, outname, maxlen);
> diff --git a/common/stdio.c b/common/stdio.c
> index 6c8ac73..ba5e2fc 100644
> --- a/common/stdio.c
> +++ b/common/stdio.c
> @@ -38,7 +38,7 @@
> DECLARE_GLOBAL_DATA_PTR;
>
> static struct stdio_dev devs;
> -struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
> +static struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
> char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
>
> #if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
> diff --git a/include/stdio_dev.h b/include/stdio_dev.h
> index 86b2a4f..554708a 100644
> --- a/include/stdio_dev.h
> +++ b/include/stdio_dev.h
> @@ -83,7 +83,6 @@ typedef struct {
> /*
> * VARIABLES
> */
> -extern struct stdio_dev *stdio_devices[];
> extern char *stdio_names[MAX_FILES];
>
> /*
>

Best regards

Andreas Bie?mann