blob: 6a757041f6af98a9052aa90313260a8d0ae6c5ea [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <serial.h>
26#include <malloc.h>
27
28#ifdef CONFIG_CONSOLE_MUX
29void iomux_printdevs(const int console)
30{
31 int i;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020032 struct stdio_dev *dev;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010033
34 for (i = 0; i < cd_count[console]; i++) {
35 dev = console_devices[console][i];
36 printf("%s ", dev->name);
37 }
38 printf("\n");
39}
40
41/* This tries to preserve the old list if an error occurs. */
42int iomux_doenv(const int console, const char *arg)
43{
44 char *console_args, *temp, **start;
45 int i, j, k, io_flag, cs_idx, repeat;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020046 struct stdio_dev *dev;
47 struct stdio_dev **cons_set;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010048
49 console_args = strdup(arg);
50 if (console_args == NULL)
51 return 1;
52 /*
53 * Check whether a comma separated list of devices was
54 * entered and count how many devices were entered.
55 * The array start[] has pointers to the beginning of
56 * each device name (up to MAX_CONSARGS devices).
57 *
58 * Have to do this twice - once to count the number of
59 * commas and then again to populate start.
60 */
61 i = 0;
62 temp = console_args;
63 for (;;) {
64 temp = strchr(temp, ',');
65 if (temp != NULL) {
66 i++;
67 temp++;
68 continue;
69 }
70 /* There's always one entry more than the number of commas. */
71 i++;
72 break;
73 }
74 start = (char **)malloc(i * sizeof(char *));
75 if (start == NULL) {
76 free(console_args);
77 return 1;
78 }
79 i = 0;
80 start[0] = console_args;
81 for (;;) {
82 temp = strchr(start[i++], ',');
83 if (temp == NULL)
84 break;
85 *temp = '\0';
86 start[i] = temp + 1;
87 }
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020088 cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +010089 if (cons_set == NULL) {
90 free(start);
91 free(console_args);
92 return 1;
93 }
94
95 switch (console) {
96 case stdin:
97 io_flag = DEV_FLAGS_INPUT;
98 break;
99 case stdout:
100 case stderr:
101 io_flag = DEV_FLAGS_OUTPUT;
102 break;
103 default:
104 free(start);
105 free(console_args);
106 free(cons_set);
107 return 1;
108 }
109
110 cs_idx = 0;
111 for (j = 0; j < i; j++) {
112 /*
113 * Check whether the device exists and is valid.
114 * console_assign() also calls search_device(),
115 * but I need the pointer to the device.
116 */
117 dev = search_device(io_flag, start[j]);
118 if (dev == NULL)
119 continue;
120 /*
121 * Prevent multiple entries for a device.
122 */
123 repeat = 0;
124 for (k = 0; k < cs_idx; k++) {
125 if (dev == cons_set[k]) {
126 repeat++;
127 break;
128 }
129 }
130 if (repeat)
131 continue;
132 /*
133 * Try assigning the specified device.
134 * This could screw up the console settings for apps.
135 */
136 if (console_assign(console, start[j]) < 0)
137 continue;
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100138 cons_set[cs_idx++] = dev;
139 }
140 free(console_args);
141 free(start);
142 /* failed to set any console */
143 if (cs_idx == 0) {
144 free(cons_set);
145 return 1;
146 } else {
147 /* Works even if console_devices[console] is NULL. */
148 console_devices[console] =
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200149 (struct stdio_dev **)realloc(console_devices[console],
150 cs_idx * sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100151 if (console_devices[console] == NULL) {
152 free(cons_set);
153 return 1;
154 }
155 memcpy(console_devices[console], cons_set, cs_idx *
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200156 sizeof(struct stdio_dev *));
Gary Jennejohnc6dc7552008-11-06 15:04:23 +0100157
158 cd_count[console] = cs_idx;
159 }
160 free(cons_set);
161 return 0;
162}
163#endif /* CONFIG_CONSOLE_MUX */