blob: 0e4e6803e7a832fe0ced120beb8d665ab3969762 [file] [log] [blame]
Gary Jennejohnc6dc7552008-11-06 15:04:23 +01001/*
2 * (C) Copyright 2008
3 * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
Gary Jennejohnc6dc7552008-11-06 15:04:23 +01006 */
7
8#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010010#include <serial.h>
11#include <malloc.h>
12
Simon Glassdfc25352017-01-16 07:03:26 -070013#if CONFIG_IS_ENABLED(CONSOLE_MUX)
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010014void iomux_printdevs(const int console)
15{
16 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020017 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010018
19 for (i = 0; i < cd_count[console]; i++) {
20 dev = console_devices[console][i];
21 printf("%s ", dev->name);
22 }
23 printf("\n");
24}
25
26/* This tries to preserve the old list if an error occurs. */
27int iomux_doenv(const int console, const char *arg)
28{
29 char *console_args, *temp, **start;
30 int i, j, k, io_flag, cs_idx, repeat;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020031 struct stdio_dev *dev;
32 struct stdio_dev **cons_set;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010033
34 console_args = strdup(arg);
35 if (console_args == NULL)
36 return 1;
37 /*
38 * Check whether a comma separated list of devices was
39 * entered and count how many devices were entered.
40 * The array start[] has pointers to the beginning of
41 * each device name (up to MAX_CONSARGS devices).
42 *
43 * Have to do this twice - once to count the number of
44 * commas and then again to populate start.
45 */
46 i = 0;
47 temp = console_args;
48 for (;;) {
49 temp = strchr(temp, ',');
50 if (temp != NULL) {
51 i++;
52 temp++;
53 continue;
54 }
55 /* There's always one entry more than the number of commas. */
56 i++;
57 break;
58 }
59 start = (char **)malloc(i * sizeof(char *));
60 if (start == NULL) {
61 free(console_args);
62 return 1;
63 }
64 i = 0;
65 start[0] = console_args;
66 for (;;) {
67 temp = strchr(start[i++], ',');
68 if (temp == NULL)
69 break;
70 *temp = '\0';
71 start[i] = temp + 1;
72 }
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020073 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010074 if (cons_set == NULL) {
75 free(start);
76 free(console_args);
77 return 1;
78 }
79
80 switch (console) {
81 case stdin:
82 io_flag = DEV_FLAGS_INPUT;
83 break;
84 case stdout:
85 case stderr:
86 io_flag = DEV_FLAGS_OUTPUT;
87 break;
88 default:
89 free(start);
90 free(console_args);
91 free(cons_set);
92 return 1;
93 }
94
95 cs_idx = 0;
96 for (j = 0; j < i; j++) {
97 /*
98 * Check whether the device exists and is valid.
99 * console_assign() also calls search_device(),
100 * but I need the pointer to the device.
101 */
102 dev = search_device(io_flag, start[j]);
103 if (dev == NULL)
104 continue;
105 /*
106 * Prevent multiple entries for a device.
107 */
108 repeat = 0;
109 for (k = 0; k < cs_idx; k++) {
110 if (dev == cons_set[k]) {
111 repeat++;
112 break;
113 }
114 }
115 if (repeat)
116 continue;
117 /*
118 * Try assigning the specified device.
119 * This could screw up the console settings for apps.
120 */
121 if (console_assign(console, start[j]) < 0)
122 continue;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100123 cons_set[cs_idx++] = dev;
124 }
125 free(console_args);
126 free(start);
127 /* failed to set any console */
128 if (cs_idx == 0) {
129 free(cons_set);
130 return 1;
131 } else {
132 /* Works even if console_devices[console] is NULL. */
133 console_devices[console] =
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200134 (struct stdio_dev **)realloc(console_devices[console],
135 cs_idx * sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100136 if (console_devices[console] == NULL) {
137 free(cons_set);
138 return 1;
139 }
140 memcpy(console_devices[console], cons_set, cs_idx *
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200141 sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100142
143 cd_count[console] = cs_idx;
144 }
145 free(cons_set);
146 return 0;
147}
Simon Glassdfc25352017-01-16 07:03:26 -0700148#endif /* CONSOLE_MUX */