blob: 5d027561bb6f52a4c5ff3821df27390a2bd9ba41 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gary Jennejohnc6dc7552008-11-06 15:04:23 +01002/*
3 * (C) Copyright 2008
4 * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
Gary Jennejohnc6dc7552008-11-06 15:04:23 +01005 */
6
7#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07008#include <console.h>
Gary Jennejohnc6dc7552008-11-06 15:04:23 +01009#include <serial.h>
10#include <malloc.h>
11
Simon Glassdfc25352017-01-16 07:03:26 -070012#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010013void iomux_printdevs(const int console)
14{
15 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020016 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010017
18 for (i = 0; i < cd_count[console]; i++) {
19 dev = console_devices[console][i];
20 printf("%s ", dev->name);
21 }
22 printf("\n");
23}
24
25/* This tries to preserve the old list if an error occurs. */
26int iomux_doenv(const int console, const char *arg)
27{
28 char *console_args, *temp, **start;
29 int i, j, k, io_flag, cs_idx, repeat;
Andy Shevchenkocb96e222020-12-21 14:30:08 +020030 struct stdio_dev **cons_set, **old_set;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020031 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010032
33 console_args = strdup(arg);
34 if (console_args == NULL)
35 return 1;
36 /*
37 * Check whether a comma separated list of devices was
38 * entered and count how many devices were entered.
39 * The array start[] has pointers to the beginning of
40 * each device name (up to MAX_CONSARGS devices).
41 *
42 * Have to do this twice - once to count the number of
43 * commas and then again to populate start.
44 */
45 i = 0;
46 temp = console_args;
47 for (;;) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010048 /* There's always one entry more than the number of commas. */
49 i++;
Andy Shevchenkoc9d642b2020-12-21 14:30:06 +020050
51 temp = strchr(temp, ',');
52 if (temp == NULL)
53 break;
54
55 temp++;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010056 }
57 start = (char **)malloc(i * sizeof(char *));
58 if (start == NULL) {
59 free(console_args);
60 return 1;
61 }
62 i = 0;
63 start[0] = console_args;
64 for (;;) {
65 temp = strchr(start[i++], ',');
66 if (temp == NULL)
67 break;
68 *temp = '\0';
69 start[i] = temp + 1;
70 }
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020071 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010072 if (cons_set == NULL) {
73 free(start);
74 free(console_args);
75 return 1;
76 }
77
Andy Shevchenko20a5c5e2021-02-11 17:09:40 +020078 io_flag = stdio_file_to_flags(console);
79 if (io_flag < 0) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010080 free(start);
81 free(console_args);
82 free(cons_set);
83 return 1;
84 }
85
86 cs_idx = 0;
87 for (j = 0; j < i; j++) {
88 /*
89 * Check whether the device exists and is valid.
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +020090 * console_assign() also calls console_search_dev(),
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010091 * but I need the pointer to the device.
92 */
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +020093 dev = console_search_dev(io_flag, start[j]);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010094 if (dev == NULL)
95 continue;
96 /*
97 * Prevent multiple entries for a device.
98 */
99 repeat = 0;
100 for (k = 0; k < cs_idx; k++) {
101 if (dev == cons_set[k]) {
102 repeat++;
103 break;
104 }
105 }
106 if (repeat)
107 continue;
108 /*
109 * Try assigning the specified device.
110 * This could screw up the console settings for apps.
111 */
112 if (console_assign(console, start[j]) < 0)
113 continue;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100114 cons_set[cs_idx++] = dev;
115 }
116 free(console_args);
117 free(start);
118 /* failed to set any console */
119 if (cs_idx == 0) {
120 free(cons_set);
121 return 1;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100122 }
Andy Shevchenko32e85982020-12-21 14:30:07 +0200123
Andy Shevchenkocb96e222020-12-21 14:30:08 +0200124 old_set = console_devices[console];
125 repeat = cd_count[console];
126
Andy Shevchenko32e85982020-12-21 14:30:07 +0200127 console_devices[console] = cons_set;
128 cd_count[console] = cs_idx;
Andy Shevchenkocb96e222020-12-21 14:30:08 +0200129
130 /* Stop dropped consoles */
131 for (i = 0; i < repeat; i++) {
132 for (j = 0; j < cs_idx; j++) {
133 if (old_set[i] == cons_set[j])
134 break;
135 }
136 if (j == cs_idx)
137 console_stop(console, old_set[i]);
138 }
139
140 free(old_set);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100141 return 0;
142}
Simon Glassdfc25352017-01-16 07:03:26 -0700143#endif /* CONSOLE_MUX */