]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
16a28ef2 GJ |
2 | /* |
3 | * (C) Copyright 2008 | |
4 | * Gary Jennejohn, DENX Software Engineering GmbH, [email protected]. | |
16a28ef2 GJ |
5 | */ |
6 | ||
24b852a7 | 7 | #include <console.h> |
16a28ef2 GJ |
8 | #include <serial.h> |
9 | #include <malloc.h> | |
10 | ||
b0265429 | 11 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
16a28ef2 GJ |
12 | void iomux_printdevs(const int console) |
13 | { | |
14 | int i; | |
52cb4d4f | 15 | struct stdio_dev *dev; |
16a28ef2 | 16 | |
400797ca | 17 | for_each_console_dev(i, console, dev) |
16a28ef2 | 18 | printf("%s ", dev->name); |
16a28ef2 GJ |
19 | printf("\n"); |
20 | } | |
21 | ||
b672c161 AS |
22 | int iomux_match_device(struct stdio_dev **set, const int n, struct stdio_dev *sdev) |
23 | { | |
24 | int i; | |
25 | ||
26 | for (i = 0; i < n; i++) | |
27 | if (sdev == set[i]) | |
28 | return i; | |
29 | return -ENOENT; | |
30 | } | |
31 | ||
16a28ef2 GJ |
32 | /* This tries to preserve the old list if an error occurs. */ |
33 | int iomux_doenv(const int console, const char *arg) | |
34 | { | |
35 | char *console_args, *temp, **start; | |
b672c161 | 36 | int i, j, io_flag, cs_idx, repeat; |
70c2525c | 37 | struct stdio_dev **cons_set, **old_set; |
52cb4d4f | 38 | struct stdio_dev *dev; |
16a28ef2 GJ |
39 | |
40 | console_args = strdup(arg); | |
41 | if (console_args == NULL) | |
42 | return 1; | |
43 | /* | |
44 | * Check whether a comma separated list of devices was | |
45 | * entered and count how many devices were entered. | |
46 | * The array start[] has pointers to the beginning of | |
47 | * each device name (up to MAX_CONSARGS devices). | |
48 | * | |
49 | * Have to do this twice - once to count the number of | |
50 | * commas and then again to populate start. | |
51 | */ | |
52 | i = 0; | |
53 | temp = console_args; | |
54 | for (;;) { | |
16a28ef2 GJ |
55 | /* There's always one entry more than the number of commas. */ |
56 | i++; | |
c939e1ca AS |
57 | |
58 | temp = strchr(temp, ','); | |
59 | if (temp == NULL) | |
60 | break; | |
61 | ||
62 | temp++; | |
16a28ef2 GJ |
63 | } |
64 | start = (char **)malloc(i * sizeof(char *)); | |
65 | if (start == NULL) { | |
66 | free(console_args); | |
67 | return 1; | |
68 | } | |
69 | i = 0; | |
70 | start[0] = console_args; | |
71 | for (;;) { | |
72 | temp = strchr(start[i++], ','); | |
73 | if (temp == NULL) | |
74 | break; | |
75 | *temp = '\0'; | |
76 | start[i] = temp + 1; | |
77 | } | |
52cb4d4f | 78 | cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *)); |
16a28ef2 GJ |
79 | if (cons_set == NULL) { |
80 | free(start); | |
81 | free(console_args); | |
82 | return 1; | |
83 | } | |
84 | ||
658d6c58 AS |
85 | io_flag = stdio_file_to_flags(console); |
86 | if (io_flag < 0) { | |
16a28ef2 GJ |
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. | |
3232487d | 97 | * console_assign() also calls console_search_dev(), |
16a28ef2 GJ |
98 | * but I need the pointer to the device. |
99 | */ | |
3232487d | 100 | dev = console_search_dev(io_flag, start[j]); |
16a28ef2 GJ |
101 | if (dev == NULL) |
102 | continue; | |
103 | /* | |
104 | * Prevent multiple entries for a device. | |
105 | */ | |
b672c161 AS |
106 | repeat = iomux_match_device(cons_set, cs_idx, dev); |
107 | if (repeat >= 0) | |
16a28ef2 GJ |
108 | continue; |
109 | /* | |
110 | * Try assigning the specified device. | |
111 | * This could screw up the console settings for apps. | |
112 | */ | |
113 | if (console_assign(console, start[j]) < 0) | |
114 | continue; | |
16a28ef2 GJ |
115 | cons_set[cs_idx++] = dev; |
116 | } | |
117 | free(console_args); | |
118 | free(start); | |
119 | /* failed to set any console */ | |
120 | if (cs_idx == 0) { | |
121 | free(cons_set); | |
122 | return 1; | |
16a28ef2 | 123 | } |
420782c4 | 124 | |
70c2525c AS |
125 | old_set = console_devices[console]; |
126 | repeat = cd_count[console]; | |
127 | ||
420782c4 AS |
128 | console_devices[console] = cons_set; |
129 | cd_count[console] = cs_idx; | |
70c2525c AS |
130 | |
131 | /* Stop dropped consoles */ | |
132 | for (i = 0; i < repeat; i++) { | |
b672c161 | 133 | j = iomux_match_device(cons_set, cs_idx, old_set[i]); |
70c2525c AS |
134 | if (j == cs_idx) |
135 | console_stop(console, old_set[i]); | |
136 | } | |
137 | ||
138 | free(old_set); | |
16a28ef2 GJ |
139 | return 0; |
140 | } | |
694cd561 AS |
141 | |
142 | int iomux_replace_device(const int console, const char *old, const char *new) | |
143 | { | |
144 | struct stdio_dev *dev; | |
145 | char *arg = NULL; /* Initial empty list */ | |
146 | int size = 1; /* For NUL terminator */ | |
147 | int i, ret; | |
148 | ||
149 | for_each_console_dev(i, console, dev) { | |
150 | const char *name = strcmp(dev->name, old) ? dev->name : new; | |
151 | char *tmp; | |
152 | ||
153 | /* Append name with a ',' (comma) separator */ | |
154 | tmp = realloc(arg, size + strlen(name) + 1); | |
155 | if (!tmp) { | |
156 | free(arg); | |
157 | return -ENOMEM; | |
158 | } | |
159 | ||
77ed7a2a YG |
160 | if (arg) { |
161 | strcat(tmp, ","); | |
162 | strcat(tmp, name); | |
163 | } | |
164 | else | |
165 | strcpy(tmp, name); | |
694cd561 AS |
166 | |
167 | arg = tmp; | |
168 | size = strlen(tmp) + 1; | |
169 | } | |
170 | ||
171 | ret = iomux_doenv(console, arg); | |
172 | if (ret) | |
173 | ret = -EINVAL; | |
174 | ||
175 | free(arg); | |
176 | return ret; | |
177 | } | |
b0265429 | 178 | #endif /* CONSOLE_MUX */ |