Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gary Jennejohn | 16a28ef | 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 | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 8 | #include <console.h> |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 9 | #include <serial.h> |
| 10 | #include <malloc.h> |
| 11 | |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 12 | #if CONFIG_IS_ENABLED(CONSOLE_MUX) |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 13 | void iomux_printdevs(const int console) |
| 14 | { |
| 15 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 16 | struct stdio_dev *dev; |
Gary Jennejohn | 16a28ef | 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; |
Jean-Christophe PLAGNIOL-VILLARD | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 30 | struct stdio_dev *dev; |
| 31 | struct stdio_dev **cons_set; |
Gary Jennejohn | 16a28ef | 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 | 16a28ef | 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 | c939e1c | 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 | 16a28ef | 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 | 52cb4d4 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 71 | cons_set = (struct stdio_dev **)calloc(i, sizeof(struct stdio_dev *)); |
Gary Jennejohn | 16a28ef | 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 | |
| 78 | switch (console) { |
| 79 | case stdin: |
| 80 | io_flag = DEV_FLAGS_INPUT; |
| 81 | break; |
| 82 | case stdout: |
| 83 | case stderr: |
| 84 | io_flag = DEV_FLAGS_OUTPUT; |
| 85 | break; |
| 86 | default: |
| 87 | free(start); |
| 88 | free(console_args); |
| 89 | free(cons_set); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | cs_idx = 0; |
| 94 | for (j = 0; j < i; j++) { |
| 95 | /* |
| 96 | * Check whether the device exists and is valid. |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 97 | * console_assign() also calls console_search_dev(), |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 98 | * but I need the pointer to the device. |
| 99 | */ |
Andy Shevchenko | 3232487 | 2020-12-21 14:30:03 +0200 | [diff] [blame] | 100 | dev = console_search_dev(io_flag, start[j]); |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 101 | if (dev == NULL) |
| 102 | continue; |
| 103 | /* |
| 104 | * Prevent multiple entries for a device. |
| 105 | */ |
| 106 | repeat = 0; |
| 107 | for (k = 0; k < cs_idx; k++) { |
| 108 | if (dev == cons_set[k]) { |
| 109 | repeat++; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (repeat) |
| 114 | continue; |
| 115 | /* |
| 116 | * Try assigning the specified device. |
| 117 | * This could screw up the console settings for apps. |
| 118 | */ |
| 119 | if (console_assign(console, start[j]) < 0) |
| 120 | continue; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 121 | cons_set[cs_idx++] = dev; |
| 122 | } |
| 123 | free(console_args); |
| 124 | free(start); |
| 125 | /* failed to set any console */ |
| 126 | if (cs_idx == 0) { |
| 127 | free(cons_set); |
| 128 | return 1; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 129 | } |
Andy Shevchenko | 420782c | 2020-12-21 14:30:07 +0200 | [diff] [blame^] | 130 | |
| 131 | /* Works even if console_devices[console] is NULL. */ |
| 132 | free(console_devices[console]); |
| 133 | console_devices[console] = cons_set; |
| 134 | cd_count[console] = cs_idx; |
Gary Jennejohn | 16a28ef | 2008-11-06 15:04:23 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
Simon Glass | b026542 | 2017-01-16 07:03:26 -0700 | [diff] [blame] | 137 | #endif /* CONSOLE_MUX */ |