Merge branch '2021-01-15-assorted-improvements'
[platform/kernel/u-boot.git] / common / iomux.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008
4  * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
5  */
6
7 #include <common.h>
8 #include <console.h>
9 #include <serial.h>
10 #include <malloc.h>
11
12 #if CONFIG_IS_ENABLED(CONSOLE_MUX)
13 void iomux_printdevs(const int console)
14 {
15         int i;
16         struct stdio_dev *dev;
17
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. */
26 int 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;
30         struct stdio_dev **cons_set, **old_set;
31         struct stdio_dev *dev;
32
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 (;;) {
48                 /* There's always one entry more than the number of commas. */
49                 i++;
50
51                 temp = strchr(temp, ',');
52                 if (temp == NULL)
53                         break;
54
55                 temp++;
56         }
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         }
71         cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
72         if (cons_set == NULL) {
73                 free(start);
74                 free(console_args);
75                 return 1;
76         }
77
78         switch (console) {
79         case stdin:
80                 io_flag = DEV_FLAGS_INPUT;
81                 break;
82         case stdout:
83         case stderr:
84                 io_flag = DEV_FLAGS_OUTPUT;
85                 break;
86         default:
87                 free(start);
88                 free(console_args);
89                 free(cons_set);
90                 return 1;
91         }
92
93         cs_idx = 0;
94         for (j = 0; j < i; j++) {
95                 /*
96                  * Check whether the device exists and is valid.
97                  * console_assign() also calls console_search_dev(),
98                  * but I need the pointer to the device.
99                  */
100                 dev = console_search_dev(io_flag, start[j]);
101                 if (dev == NULL)
102                         continue;
103                 /*
104                  * Prevent multiple entries for a device.
105                  */
106                  repeat = 0;
107                  for (k = 0; k < cs_idx; k++) {
108                         if (dev == cons_set[k]) {
109                                 repeat++;
110                                 break;
111                         }
112                  }
113                  if (repeat)
114                         continue;
115                 /*
116                  * Try assigning the specified device.
117                  * This could screw up the console settings for apps.
118                  */
119                 if (console_assign(console, start[j]) < 0)
120                         continue;
121                 cons_set[cs_idx++] = dev;
122         }
123         free(console_args);
124         free(start);
125         /* failed to set any console */
126         if (cs_idx == 0) {
127                 free(cons_set);
128                 return 1;
129         }
130
131         old_set = console_devices[console];
132         repeat = cd_count[console];
133
134         console_devices[console] = cons_set;
135         cd_count[console] = cs_idx;
136
137         /* Stop dropped consoles */
138         for (i = 0; i < repeat; i++) {
139                 for (j = 0; j < cs_idx; j++) {
140                         if (old_set[i] == cons_set[j])
141                                 break;
142                 }
143                 if (j == cs_idx)
144                         console_stop(console, old_set[i]);
145         }
146
147         free(old_set);
148         return 0;
149 }
150 #endif /* CONSOLE_MUX */