blob: a8be1ac7d8ab4a23ae73321bb8f00982f80b6cb4 [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
Andy Shevchenko75039f52021-02-11 17:09:41 +020025int iomux_match_device(struct stdio_dev **set, const int n, struct stdio_dev *sdev)
26{
27 int i;
28
29 for (i = 0; i < n; i++)
30 if (sdev == set[i])
31 return i;
32 return -ENOENT;
33}
34
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010035/* This tries to preserve the old list if an error occurs. */
36int iomux_doenv(const int console, const char *arg)
37{
38 char *console_args, *temp, **start;
Andy Shevchenko75039f52021-02-11 17:09:41 +020039 int i, j, io_flag, cs_idx, repeat;
Andy Shevchenkocb96e222020-12-21 14:30:08 +020040 struct stdio_dev **cons_set, **old_set;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020041 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010042
43 console_args = strdup(arg);
44 if (console_args == NULL)
45 return 1;
46 /*
47 * Check whether a comma separated list of devices was
48 * entered and count how many devices were entered.
49 * The array start[] has pointers to the beginning of
50 * each device name (up to MAX_CONSARGS devices).
51 *
52 * Have to do this twice - once to count the number of
53 * commas and then again to populate start.
54 */
55 i = 0;
56 temp = console_args;
57 for (;;) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010058 /* There's always one entry more than the number of commas. */
59 i++;
Andy Shevchenkoc9d642b2020-12-21 14:30:06 +020060
61 temp = strchr(temp, ',');
62 if (temp == NULL)
63 break;
64
65 temp++;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010066 }
67 start = (char **)malloc(i * sizeof(char *));
68 if (start == NULL) {
69 free(console_args);
70 return 1;
71 }
72 i = 0;
73 start[0] = console_args;
74 for (;;) {
75 temp = strchr(start[i++], ',');
76 if (temp == NULL)
77 break;
78 *temp = '\0';
79 start[i] = temp + 1;
80 }
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020081 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010082 if (cons_set == NULL) {
83 free(start);
84 free(console_args);
85 return 1;
86 }
87
Andy Shevchenko20a5c5e2021-02-11 17:09:40 +020088 io_flag = stdio_file_to_flags(console);
89 if (io_flag < 0) {
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010090 free(start);
91 free(console_args);
92 free(cons_set);
93 return 1;
94 }
95
96 cs_idx = 0;
97 for (j = 0; j < i; j++) {
98 /*
99 * Check whether the device exists and is valid.
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +0200100 * console_assign() also calls console_search_dev(),
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100101 * but I need the pointer to the device.
102 */
Andy Shevchenkocfaed7f2020-12-21 14:30:03 +0200103 dev = console_search_dev(io_flag, start[j]);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100104 if (dev == NULL)
105 continue;
106 /*
107 * Prevent multiple entries for a device.
108 */
Andy Shevchenko75039f52021-02-11 17:09:41 +0200109 repeat = iomux_match_device(cons_set, cs_idx, dev);
110 if (repeat >= 0)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100111 continue;
112 /*
113 * Try assigning the specified device.
114 * This could screw up the console settings for apps.
115 */
116 if (console_assign(console, start[j]) < 0)
117 continue;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100118 cons_set[cs_idx++] = dev;
119 }
120 free(console_args);
121 free(start);
122 /* failed to set any console */
123 if (cs_idx == 0) {
124 free(cons_set);
125 return 1;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100126 }
Andy Shevchenko32e85982020-12-21 14:30:07 +0200127
Andy Shevchenkocb96e222020-12-21 14:30:08 +0200128 old_set = console_devices[console];
129 repeat = cd_count[console];
130
Andy Shevchenko32e85982020-12-21 14:30:07 +0200131 console_devices[console] = cons_set;
132 cd_count[console] = cs_idx;
Andy Shevchenkocb96e222020-12-21 14:30:08 +0200133
134 /* Stop dropped consoles */
135 for (i = 0; i < repeat; i++) {
Andy Shevchenko75039f52021-02-11 17:09:41 +0200136 j = iomux_match_device(cons_set, cs_idx, old_set[i]);
Andy Shevchenkocb96e222020-12-21 14:30:08 +0200137 if (j == cs_idx)
138 console_stop(console, old_set[i]);
139 }
140
141 free(old_set);
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100142 return 0;
143}
Simon Glassdfc25352017-01-16 07:03:26 -0700144#endif /* CONSOLE_MUX */