1 // SPDX-License-Identifier: GPL-2.0+
4 * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
12 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
13 void iomux_printdevs(const int console)
16 struct stdio_dev *dev;
18 for_each_console_dev(i, console, dev)
19 printf("%s ", dev->name);
23 int iomux_match_device(struct stdio_dev **set, const int n, struct stdio_dev *sdev)
27 for (i = 0; i < n; i++)
33 /* This tries to preserve the old list if an error occurs. */
34 int iomux_doenv(const int console, const char *arg)
36 char *console_args, *temp, **start;
37 int i, j, io_flag, cs_idx, repeat;
38 struct stdio_dev **cons_set, **old_set;
39 struct stdio_dev *dev;
41 console_args = strdup(arg);
42 if (console_args == NULL)
45 * Check whether a comma separated list of devices was
46 * entered and count how many devices were entered.
47 * The array start[] has pointers to the beginning of
48 * each device name (up to MAX_CONSARGS devices).
50 * Have to do this twice - once to count the number of
51 * commas and then again to populate start.
56 /* There's always one entry more than the number of commas. */
59 temp = strchr(temp, ',');
65 start = (char **)malloc(i * sizeof(char *));
71 start[0] = console_args;
73 temp = strchr(start[i++], ',');
79 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
80 if (cons_set == NULL) {
86 io_flag = stdio_file_to_flags(console);
95 for (j = 0; j < i; j++) {
97 * Check whether the device exists and is valid.
98 * console_assign() also calls console_search_dev(),
99 * but I need the pointer to the device.
101 dev = console_search_dev(io_flag, start[j]);
105 * Prevent multiple entries for a device.
107 repeat = iomux_match_device(cons_set, cs_idx, dev);
111 * Try assigning the specified device.
112 * This could screw up the console settings for apps.
114 if (console_assign(console, start[j]) < 0)
116 cons_set[cs_idx++] = dev;
120 /* failed to set any console */
126 old_set = console_devices[console];
127 repeat = cd_count[console];
129 console_devices[console] = cons_set;
130 cd_count[console] = cs_idx;
132 /* Stop dropped consoles */
133 for (i = 0; i < repeat; i++) {
134 j = iomux_match_device(cons_set, cs_idx, old_set[i]);
136 console_stop(console, old_set[i]);
143 int iomux_replace_device(const int console, const char *old, const char *new)
145 struct stdio_dev *dev;
146 char *arg = NULL; /* Initial empty list */
147 int size = 1; /* For NUL terminator */
150 for_each_console_dev(i, console, dev) {
151 const char *name = strcmp(dev->name, old) ? dev->name : new;
154 /* Append name with a ',' (comma) separator */
155 tmp = realloc(arg, size + strlen(name) + 1);
169 size = strlen(tmp) + 1;
172 ret = iomux_doenv(console, arg);
179 #endif /* CONSOLE_MUX */