iomux.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  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. #include <common.h>
  24. #include <serial.h>
  25. #include <malloc.h>
  26. #ifdef CONFIG_CONSOLE_MUX
  27. void iomux_printdevs(const int console)
  28. {
  29. int i;
  30. struct stdio_dev *dev;
  31. for (i = 0; i < cd_count[console]; i++) {
  32. dev = console_devices[console][i];
  33. printf("%s ", dev->name);
  34. }
  35. printf("\n");
  36. }
  37. /* This tries to preserve the old list if an error occurs. */
  38. int iomux_doenv(const int console, const char *arg)
  39. {
  40. char *console_args, *temp, **start;
  41. int i, j, k, io_flag, cs_idx, repeat;
  42. struct stdio_dev *dev;
  43. struct stdio_dev **cons_set;
  44. console_args = strdup(arg);
  45. if (console_args == NULL)
  46. return 1;
  47. /*
  48. * Check whether a comma separated list of devices was
  49. * entered and count how many devices were entered.
  50. * The array start[] has pointers to the beginning of
  51. * each device name (up to MAX_CONSARGS devices).
  52. *
  53. * Have to do this twice - once to count the number of
  54. * commas and then again to populate start.
  55. */
  56. i = 0;
  57. temp = console_args;
  58. for (;;) {
  59. temp = strchr(temp, ',');
  60. if (temp != NULL) {
  61. i++;
  62. temp++;
  63. continue;
  64. }
  65. /* There's always one entry more than the number of commas. */
  66. i++;
  67. break;
  68. }
  69. start = (char **)malloc(i * sizeof(char *));
  70. if (start == NULL) {
  71. free(console_args);
  72. return 1;
  73. }
  74. i = 0;
  75. start[0] = console_args;
  76. for (;;) {
  77. temp = strchr(start[i++], ',');
  78. if (temp == NULL)
  79. break;
  80. *temp = '\0';
  81. start[i] = temp + 1;
  82. }
  83. cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
  84. if (cons_set == NULL) {
  85. free(start);
  86. free(console_args);
  87. return 1;
  88. }
  89. switch (console) {
  90. case stdin:
  91. io_flag = DEV_FLAGS_INPUT;
  92. break;
  93. case stdout:
  94. case stderr:
  95. io_flag = DEV_FLAGS_OUTPUT;
  96. break;
  97. default:
  98. free(start);
  99. free(console_args);
  100. free(cons_set);
  101. return 1;
  102. }
  103. cs_idx = 0;
  104. for (j = 0; j < i; j++) {
  105. /*
  106. * Check whether the device exists and is valid.
  107. * console_assign() also calls search_device(),
  108. * but I need the pointer to the device.
  109. */
  110. dev = search_device(io_flag, start[j]);
  111. if (dev == NULL)
  112. continue;
  113. /*
  114. * Prevent multiple entries for a device.
  115. */
  116. repeat = 0;
  117. for (k = 0; k < cs_idx; k++) {
  118. if (dev == cons_set[k]) {
  119. repeat++;
  120. break;
  121. }
  122. }
  123. if (repeat)
  124. continue;
  125. /*
  126. * Try assigning the specified device.
  127. * This could screw up the console settings for apps.
  128. */
  129. if (console_assign(console, start[j]) < 0)
  130. continue;
  131. #ifdef CONFIG_SERIAL_MULTI
  132. /*
  133. * This was taken from common/cmd_nvedit.c.
  134. * This will never work because serial_assign() returns
  135. * 1 upon error, not -1.
  136. * This would almost always return an error anyway because
  137. * serial_assign() expects the name of a serial device, like
  138. * serial_smc, but the user generally only wants to set serial.
  139. */
  140. if (serial_assign(start[j]) < 0)
  141. continue;
  142. #endif
  143. cons_set[cs_idx++] = dev;
  144. }
  145. free(console_args);
  146. free(start);
  147. /* failed to set any console */
  148. if (cs_idx == 0) {
  149. free(cons_set);
  150. return 1;
  151. } else {
  152. /* Works even if console_devices[console] is NULL. */
  153. console_devices[console] =
  154. (struct stdio_dev **)realloc(console_devices[console],
  155. cs_idx * sizeof(struct stdio_dev *));
  156. if (console_devices[console] == NULL) {
  157. free(cons_set);
  158. return 1;
  159. }
  160. memcpy(console_devices[console], cons_set, cs_idx *
  161. sizeof(struct stdio_dev *));
  162. cd_count[console] = cs_idx;
  163. }
  164. free(cons_set);
  165. return 0;
  166. }
  167. #endif /* CONFIG_CONSOLE_MUX */