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 | |
Andy Shevchenko | e29d019 | 2021-02-11 17:09:42 +0200 | [diff] [blame] | 18 | for_each_console_dev(i, console, dev) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 19 | printf("%s ", dev->name); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 20 | printf("\n"); |
| 21 | } |
| 22 | |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 23 | int iomux_match_device(struct stdio_dev **set, const int n, struct stdio_dev *sdev) |
| 24 | { |
| 25 | int i; |
| 26 | |
| 27 | for (i = 0; i < n; i++) |
| 28 | if (sdev == set[i]) |
| 29 | return i; |
| 30 | return -ENOENT; |
| 31 | } |
| 32 | |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 33 | /* This tries to preserve the old list if an error occurs. */ |
| 34 | int iomux_doenv(const int console, const char *arg) |
| 35 | { |
| 36 | char *console_args, *temp, **start; |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 37 | int i, j, io_flag, cs_idx, repeat; |
Andy Shevchenko | cb96e22 | 2020-12-21 14:30:08 +0200 | [diff] [blame] | 38 | struct stdio_dev **cons_set, **old_set; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 39 | struct stdio_dev *dev; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 40 | |
| 41 | console_args = strdup(arg); |
| 42 | if (console_args == NULL) |
| 43 | return 1; |
| 44 | /* |
| 45 | * Check whether a comma separated list of devices was |
| 46 | * entered and count how many devices were entered. |
| 47 | * The array start[] has pointers to the beginning of |
| 48 | * each device name (up to MAX_CONSARGS devices). |
| 49 | * |
| 50 | * Have to do this twice - once to count the number of |
| 51 | * commas and then again to populate start. |
| 52 | */ |
| 53 | i = 0; |
| 54 | temp = console_args; |
| 55 | for (;;) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 56 | /* There's always one entry more than the number of commas. */ |
| 57 | i++; |
Andy Shevchenko | c9d642b | 2020-12-21 14:30:06 +0200 | [diff] [blame] | 58 | |
| 59 | temp = strchr(temp, ','); |
| 60 | if (temp == NULL) |
| 61 | break; |
| 62 | |
| 63 | temp++; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 64 | } |
| 65 | start = (char **)malloc(i * sizeof(char *)); |
| 66 | if (start == NULL) { |
| 67 | free(console_args); |
| 68 | return 1; |
| 69 | } |
| 70 | i = 0; |
| 71 | start[0] = console_args; |
| 72 | for (;;) { |
| 73 | temp = strchr(start[i++], ','); |
| 74 | if (temp == NULL) |
| 75 | break; |
| 76 | *temp = '\0'; |
| 77 | start[i] = temp + 1; |
| 78 | } |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 79 | cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *)); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 80 | if (cons_set == NULL) { |
| 81 | free(start); |
| 82 | free(console_args); |
| 83 | return 1; |
| 84 | } |
| 85 | |
Andy Shevchenko | 20a5c5e | 2021-02-11 17:09:40 +0200 | [diff] [blame] | 86 | io_flag = stdio_file_to_flags(console); |
| 87 | if (io_flag < 0) { |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 88 | free(start); |
| 89 | free(console_args); |
| 90 | free(cons_set); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | cs_idx = 0; |
| 95 | for (j = 0; j < i; j++) { |
| 96 | /* |
| 97 | * Check whether the device exists and is valid. |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 98 | * console_assign() also calls console_search_dev(), |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 99 | * but I need the pointer to the device. |
| 100 | */ |
Andy Shevchenko | cfaed7f | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 101 | dev = console_search_dev(io_flag, start[j]); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 102 | if (dev == NULL) |
| 103 | continue; |
| 104 | /* |
| 105 | * Prevent multiple entries for a device. |
| 106 | */ |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 107 | repeat = iomux_match_device(cons_set, cs_idx, dev); |
| 108 | if (repeat >= 0) |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 109 | continue; |
| 110 | /* |
| 111 | * Try assigning the specified device. |
| 112 | * This could screw up the console settings for apps. |
| 113 | */ |
| 114 | if (console_assign(console, start[j]) < 0) |
| 115 | continue; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 116 | cons_set[cs_idx++] = dev; |
| 117 | } |
| 118 | free(console_args); |
| 119 | free(start); |
| 120 | /* failed to set any console */ |
| 121 | if (cs_idx == 0) { |
| 122 | free(cons_set); |
| 123 | return 1; |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 124 | } |
Andy Shevchenko | 32e8598 | 2020-12-21 14:30:07 +0200 | [diff] [blame] | 125 | |
Andy Shevchenko | cb96e22 | 2020-12-21 14:30:08 +0200 | [diff] [blame] | 126 | old_set = console_devices[console]; |
| 127 | repeat = cd_count[console]; |
| 128 | |
Andy Shevchenko | 32e8598 | 2020-12-21 14:30:07 +0200 | [diff] [blame] | 129 | console_devices[console] = cons_set; |
| 130 | cd_count[console] = cs_idx; |
Andy Shevchenko | cb96e22 | 2020-12-21 14:30:08 +0200 | [diff] [blame] | 131 | |
| 132 | /* Stop dropped consoles */ |
| 133 | for (i = 0; i < repeat; i++) { |
Andy Shevchenko | 75039f5 | 2021-02-11 17:09:41 +0200 | [diff] [blame] | 134 | j = iomux_match_device(cons_set, cs_idx, old_set[i]); |
Andy Shevchenko | cb96e22 | 2020-12-21 14:30:08 +0200 | [diff] [blame] | 135 | if (j == cs_idx) |
| 136 | console_stop(console, old_set[i]); |
| 137 | } |
| 138 | |
| 139 | free(old_set); |
Gary Jennejohn | c6dc755 | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | } |
Andy Shevchenko | 8ee40d3 | 2021-02-11 17:09:43 +0200 | [diff] [blame] | 142 | |
| 143 | int iomux_replace_device(const int console, const char *old, const char *new) |
| 144 | { |
| 145 | struct stdio_dev *dev; |
| 146 | char *arg = NULL; /* Initial empty list */ |
| 147 | int size = 1; /* For NUL terminator */ |
| 148 | int i, ret; |
| 149 | |
| 150 | for_each_console_dev(i, console, dev) { |
| 151 | const char *name = strcmp(dev->name, old) ? dev->name : new; |
| 152 | char *tmp; |
| 153 | |
| 154 | /* Append name with a ',' (comma) separator */ |
| 155 | tmp = realloc(arg, size + strlen(name) + 1); |
| 156 | if (!tmp) { |
| 157 | free(arg); |
| 158 | return -ENOMEM; |
| 159 | } |
| 160 | |
| 161 | strcat(tmp, ","); |
| 162 | strcat(tmp, name); |
| 163 | |
| 164 | arg = tmp; |
| 165 | size = strlen(tmp) + 1; |
| 166 | } |
| 167 | |
| 168 | ret = iomux_doenv(console, arg); |
| 169 | if (ret) |
| 170 | ret = -EINVAL; |
| 171 | |
| 172 | free(arg); |
| 173 | return ret; |
| 174 | } |
Simon Glass | dfc2535 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 175 | #endif /* CONSOLE_MUX */ |