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