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