iomux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008
  4. * Gary Jennejohn, DENX Software Engineering GmbH, garyj@denx.de.
  5. */
  6. #include <common.h>
  7. #include <console.h>
  8. #include <serial.h>
  9. #include <malloc.h>
  10. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  11. void iomux_printdevs(const int console)
  12. {
  13. int i;
  14. struct stdio_dev *dev;
  15. for (i = 0; i < cd_count[console]; i++) {
  16. dev = console_devices[console][i];
  17. printf("%s ", dev->name);
  18. }
  19. printf("\n");
  20. }
  21. /* This tries to preserve the old list if an error occurs. */
  22. int iomux_doenv(const int console, const char *arg)
  23. {
  24. char *console_args, *temp, **start;
  25. int i, j, k, io_flag, cs_idx, repeat;
  26. struct stdio_dev **cons_set, **old_set;
  27. struct stdio_dev *dev;
  28. console_args = strdup(arg);
  29. if (console_args == NULL)
  30. return 1;
  31. /*
  32. * Check whether a comma separated list of devices was
  33. * entered and count how many devices were entered.
  34. * The array start[] has pointers to the beginning of
  35. * each device name (up to MAX_CONSARGS devices).
  36. *
  37. * Have to do this twice - once to count the number of
  38. * commas and then again to populate start.
  39. */
  40. i = 0;
  41. temp = console_args;
  42. for (;;) {
  43. /* There's always one entry more than the number of commas. */
  44. i++;
  45. temp = strchr(temp, ',');
  46. if (temp == NULL)
  47. break;
  48. temp++;
  49. }
  50. start = (char **)malloc(i * sizeof(char *));
  51. if (start == NULL) {
  52. free(console_args);
  53. return 1;
  54. }
  55. i = 0;
  56. start[0] = console_args;
  57. for (;;) {
  58. temp = strchr(start[i++], ',');
  59. if (temp == NULL)
  60. break;
  61. *temp = '\0';
  62. start[i] = temp + 1;
  63. }
  64. cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *));
  65. if (cons_set == NULL) {
  66. free(start);
  67. free(console_args);
  68. return 1;
  69. }
  70. switch (console) {
  71. case stdin:
  72. io_flag = DEV_FLAGS_INPUT;
  73. break;
  74. case stdout:
  75. case stderr:
  76. io_flag = DEV_FLAGS_OUTPUT;
  77. break;
  78. default:
  79. free(start);
  80. free(console_args);
  81. free(cons_set);
  82. return 1;
  83. }
  84. cs_idx = 0;
  85. for (j = 0; j < i; j++) {
  86. /*
  87. * Check whether the device exists and is valid.
  88. * console_assign() also calls console_search_dev(),
  89. * but I need the pointer to the device.
  90. */
  91. dev = console_search_dev(io_flag, start[j]);
  92. if (dev == NULL)
  93. continue;
  94. /*
  95. * Prevent multiple entries for a device.
  96. */
  97. repeat = 0;
  98. for (k = 0; k < cs_idx; k++) {
  99. if (dev == cons_set[k]) {
  100. repeat++;
  101. break;
  102. }
  103. }
  104. if (repeat)
  105. continue;
  106. /*
  107. * Try assigning the specified device.
  108. * This could screw up the console settings for apps.
  109. */
  110. if (console_assign(console, start[j]) < 0)
  111. continue;
  112. cons_set[cs_idx++] = dev;
  113. }
  114. free(console_args);
  115. free(start);
  116. /* failed to set any console */
  117. if (cs_idx == 0) {
  118. free(cons_set);
  119. return 1;
  120. }
  121. old_set = console_devices[console];
  122. repeat = cd_count[console];
  123. console_devices[console] = cons_set;
  124. cd_count[console] = cs_idx;
  125. /* Stop dropped consoles */
  126. for (i = 0; i < repeat; i++) {
  127. for (j = 0; j < cs_idx; j++) {
  128. if (old_set[i] == cons_set[j])
  129. break;
  130. }
  131. if (j == cs_idx)
  132. console_stop(console, old_set[i]);
  133. }
  134. free(old_set);
  135. return 0;
  136. }
  137. #endif /* CONSOLE_MUX */