blob: 1224c15eb718b5e78f625f57274000114d85c7de [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01002/*
3 * (C) Copyright 2008
4 * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01005 */
6
Simon Glass24b852a2015-11-08 23:47:45 -07007#include <console.h>
Gary Jennejohn16a28ef2008-11-06 15:04:23 +01008#include <serial.h>
9#include <malloc.h>
10
Simon Glassb0265422017-01-16 07:03:26 -070011#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010012void iomux_printdevs(const int console)
13{
14 int i;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020015 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010016
Andy Shevchenko400797c2021-02-11 17:09:42 +020017 for_each_console_dev(i, console, dev)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010018 printf("%s ", dev->name);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010019 printf("\n");
20}
21
Andy Shevchenkob672c162021-02-11 17:09:41 +020022int 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
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010032/* This tries to preserve the old list if an error occurs. */
33int iomux_doenv(const int console, const char *arg)
34{
35 char *console_args, *temp, **start;
Andy Shevchenkob672c162021-02-11 17:09:41 +020036 int i, j, io_flag, cs_idx, repeat;
Andy Shevchenko70c25252020-12-21 14:30:08 +020037 struct stdio_dev **cons_set, **old_set;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020038 struct stdio_dev *dev;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010039
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 (;;) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010055 /* There's always one entry more than the number of commas. */
56 i++;
Andy Shevchenkoc939e1c2020-12-21 14:30:06 +020057
58 temp = strchr(temp, ',');
59 if (temp == NULL)
60 break;
61
62 temp++;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010063 }
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 }
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020078 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010079 if (cons_set == NULL) {
80 free(start);
81 free(console_args);
82 return 1;
83 }
84
Andy Shevchenko658d6c52021-02-11 17:09:40 +020085 io_flag = stdio_file_to_flags(console);
86 if (io_flag < 0) {
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010087 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.
Andy Shevchenko32324872020-12-21 14:30:03 +020097 * console_assign() also calls console_search_dev(),
Gary Jennejohn16a28ef2008-11-06 15:04:23 +010098 * but I need the pointer to the device.
99 */
Andy Shevchenko32324872020-12-21 14:30:03 +0200100 dev = console_search_dev(io_flag, start[j]);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100101 if (dev == NULL)
102 continue;
103 /*
104 * Prevent multiple entries for a device.
105 */
Andy Shevchenkob672c162021-02-11 17:09:41 +0200106 repeat = iomux_match_device(cons_set, cs_idx, dev);
107 if (repeat >= 0)
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100108 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;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100115 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;
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100123 }
Andy Shevchenko420782c2020-12-21 14:30:07 +0200124
Andy Shevchenko70c25252020-12-21 14:30:08 +0200125 old_set = console_devices[console];
126 repeat = cd_count[console];
127
Andy Shevchenko420782c2020-12-21 14:30:07 +0200128 console_devices[console] = cons_set;
129 cd_count[console] = cs_idx;
Andy Shevchenko70c25252020-12-21 14:30:08 +0200130
131 /* Stop dropped consoles */
132 for (i = 0; i < repeat; i++) {
Andy Shevchenkob672c162021-02-11 17:09:41 +0200133 j = iomux_match_device(cons_set, cs_idx, old_set[i]);
Andy Shevchenko70c25252020-12-21 14:30:08 +0200134 if (j == cs_idx)
135 console_stop(console, old_set[i]);
136 }
137
138 free(old_set);
Gary Jennejohn16a28ef2008-11-06 15:04:23 +0100139 return 0;
140}
Andy Shevchenko694cd562021-02-11 17:09:43 +0200141
142int 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
Yuichiro Goto77ed7a22021-04-26 08:08:03 +0900160 if (arg) {
161 strcat(tmp, ",");
162 strcat(tmp, name);
163 }
164 else
165 strcpy(tmp, name);
Andy Shevchenko694cd562021-02-11 17:09:43 +0200166
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}
Simon Glassb0265422017-01-16 07:03:26 -0700178#endif /* CONSOLE_MUX */