diff --git a/commands/loadb.c b/commands/loadb.c index 7424bcc..be5830d 100644 --- a/commands/loadb.c +++ b/commands/loadb.c @@ -604,9 +604,8 @@ ulong offset = 0; ulong addr; int load_baudrate = 0, current_baudrate; - int rcode = 0; + int rcode = 0, ret; int opt; - int open_mode = O_WRONLY; char *output_file = NULL; struct console_device *cdev = NULL; @@ -621,9 +620,6 @@ case 'o': offset = (int)simple_strtoul(optarg, NULL, 10); break; - case 'c': - open_mode |= O_CREAT; - break; default: perror(argv[0]); return 1; @@ -635,7 +631,7 @@ printf("%s:No console device with STDIN and STDOUT\n", argv[0]); return -ENODEV; } - current_baudrate = (int)simple_strtoul(dev_get_param(&cdev->class_dev, "baudrate"), NULL, 10); + current_baudrate = console_get_baudrate(cdev); /* Load Defaults */ if (load_baudrate == 0) @@ -644,7 +640,7 @@ output_file = DEF_FILE; /* File should exist */ - ofd = open(output_file, open_mode); + ofd = open(output_file, O_WRONLY | O_CREAT); if (ofd < 0) { perror(argv[0]); return 3; @@ -660,17 +656,9 @@ } } - if (load_baudrate != current_baudrate) { - printf("## Switch baudrate to %d bps and press ENTER ...\n", - load_baudrate); - udelay(50000); - cdev->setbrg(cdev, load_baudrate); - udelay(50000); - for (;;) { - if (getc() == '\r') - break; - } - } + ret = console_set_baudrate(cdev, load_baudrate); + if (ret) + return ret; printf("## Ready for binary (kermit) download " "to 0x%08lX offset on %s device at %d bps...\n", offset, @@ -681,17 +669,9 @@ rcode = 1; } - if (load_baudrate != current_baudrate) { - printf("## Switch baudrate to %d bps and press ESC ...\n", - current_baudrate); - udelay(50000); - cdev->setbrg(cdev, current_baudrate); - udelay(50000); - for (;;) { - if (getc() == 0x1B) /* ESC */ - break; - } - } + ret = console_set_baudrate(cdev, current_baudrate); + if (ret) + return ret; close(ofd); ofd = 0; @@ -704,7 +684,6 @@ BAREBOX_CMD_HELP_OPT("-f FILE", "download to FILE (default image.bin") BAREBOX_CMD_HELP_OPT("-o OFFS", "destination file OFFSet (default 0)") BAREBOX_CMD_HELP_OPT("-b BAUD", "baudrate for download (default: console baudrate") -BAREBOX_CMD_HELP_OPT("-c", "create file if not present") BAREBOX_CMD_HELP_END BAREBOX_CMD_START(loadb) diff --git a/commands/loadxy.c b/commands/loadxy.c index 1e65cb6..a4b1bec 100644 --- a/commands/loadxy.c +++ b/commands/loadxy.c @@ -40,45 +40,6 @@ #define DEF_FILE "image.bin" -static int console_change_speed(struct console_device *cdev, int baudrate) -{ - int current_baudrate; - const char *bstr; - - bstr = dev_get_param(&cdev->class_dev, "baudrate"); - current_baudrate = bstr ? (int)simple_strtoul(bstr, NULL, 10) : 0; - if (baudrate && baudrate != current_baudrate) { - printf("## Switch baudrate from %d to %d bps and press ENTER ...\n", - current_baudrate, baudrate); - mdelay(50); - cdev->setbrg(cdev, baudrate); - mdelay(50); - } - return current_baudrate; -} - -static struct console_device *get_named_console(const char *cname) -{ - struct console_device *cdev; - const char *target; - - /* - * Assumption to have BOTH CONSOLE_STDIN AND STDOUT in the - * same output console - */ - for_each_console(cdev) { - target = dev_id(&cdev->class_dev); - if (strlen(target) != strlen(cname)) - continue; - printf("RJK: looking for %s in console name %s\n", - cname, target); - if ((cdev->f_active & (CONSOLE_STDIN | CONSOLE_STDOUT)) - && !strcmp(cname, target)) - return cdev; - } - return NULL; -} - /** * @brief provide the loady(Y-Modem or Y-Modem/G) support * @@ -112,7 +73,7 @@ } if (cname) - cdev = get_named_console(cname); + cdev = console_get_by_name(cname); else cdev = console_get_first_active(); if (!cdev) { @@ -121,7 +82,15 @@ return -ENODEV; } - current_baudrate = console_change_speed(cdev, load_baudrate); + current_baudrate = console_get_baudrate(cdev); + + if (!load_baudrate) + load_baudrate = current_baudrate; + + rc = console_set_baudrate(cdev, load_baudrate); + if (rc) + return rc; + printf("## Ready for binary (ymodem) download at %d bps...\n", load_baudrate ? load_baudrate : current_baudrate); @@ -135,7 +104,9 @@ rcode = 1; } - console_change_speed(cdev, current_baudrate); + rc = console_set_baudrate(cdev, current_baudrate); + if (rc) + return rc; return rcode; } @@ -167,8 +138,7 @@ static int do_loadx(int argc, char *argv[]) { ulong offset = 0; - int load_baudrate = 0, current_baudrate, ofd, opt, rcode = 0; - int open_mode = O_WRONLY; + int load_baudrate = 0, current_baudrate, rc, ofd, opt, rcode = 0; char *output_file = NULL, *cname = NULL; struct console_device *cdev = NULL; @@ -183,9 +153,6 @@ case 'o': offset = (int)simple_strtoul(optarg, NULL, 10); break; - case 'c': - open_mode |= O_CREAT; - break; case 't': cname = optarg; break; @@ -196,7 +163,7 @@ } if (cname) - cdev = get_named_console(cname); + cdev = console_get_by_name(cname); else cdev = console_get_first_active(); if (!cdev) { @@ -210,7 +177,7 @@ output_file = DEF_FILE; /* File should exist */ - ofd = open(output_file, open_mode); + ofd = open(output_file, O_WRONLY | O_CREAT); if (ofd < 0) { perror(argv[0]); return 3; @@ -226,7 +193,15 @@ } } - current_baudrate = console_change_speed(cdev, load_baudrate); + current_baudrate = console_get_baudrate(cdev); + + if (!load_baudrate) + load_baudrate = current_baudrate; + + rc = console_set_baudrate(cdev, load_baudrate); + if (rc) + return rc; + printf("## Ready for binary (xmodem) download " "to 0x%08lX offset on %s device at %d bps...\n", offset, output_file, load_baudrate ? load_baudrate : current_baudrate); @@ -235,7 +210,10 @@ printf("## Binary (xmodem) download aborted (%d)\n", rcode); rcode = 1; } - console_change_speed(cdev, current_baudrate); + + rc = console_set_baudrate(cdev, current_baudrate); + if (rc) + return rc; return rcode; } @@ -246,7 +224,6 @@ BAREBOX_CMD_HELP_OPT("-o OFFS", "destination file OFFSet (default 0)") BAREBOX_CMD_HELP_OPT("-b BAUD", "baudrate for download (default: console baudrate") BAREBOX_CMD_HELP_OPT("-t NAME", "console name to use (default: current)") -BAREBOX_CMD_HELP_OPT("-c", "create file if not present") BAREBOX_CMD_HELP_END BAREBOX_CMD_START(loadx) diff --git a/common/console.c b/common/console.c index 0c32f06..0a6fc3e 100644 --- a/common/console.c +++ b/common/console.c @@ -58,32 +58,14 @@ static struct kfifo *console_input_fifo = &__console_input_fifo; static struct kfifo *console_output_fifo = &__console_output_fifo; -static int console_std_set(struct device_d *dev, struct param_d *param, - const char *val) +int console_set_active(struct console_device *cdev, unsigned flag) { - struct console_device *cdev = to_console_dev(dev); - char active[4]; - unsigned int flag = 0, i = 0; - int ret; + int ret, i; - if (val) { - if (strchr(val, 'i') && cdev->getc) { - active[i++] = 'i'; - flag |= CONSOLE_STDIN; - } - - if (cdev->putc) { - if (strchr(val, 'o')) { - active[i++] = 'o'; - flag |= CONSOLE_STDOUT; - } - - if (strchr(val, 'e')) { - active[i++] = 'e'; - flag |= CONSOLE_STDERR; - } - } - } + if (!cdev->getc) + flag &= ~CONSOLE_STDIN; + if (!cdev->putc) + flag &= ~(CONSOLE_STDOUT | CONSOLE_STDERR); if (flag && !cdev->f_active) { /* The device is being activated, set its baudrate */ @@ -97,16 +79,25 @@ return ret; } - active[i] = 0; cdev->f_active = flag; - dev_param_set_generic(dev, param, active); + if (IS_ENABLED(CONFIG_PARAMETER)) { + i = 0; + + if (flag & CONSOLE_STDIN) + cdev->active[i++] = 'i'; + if (flag & CONSOLE_STDOUT) + cdev->active[i++] = 'o'; + if (flag & CONSOLE_STDERR) + cdev->active[i++] = 'e'; + cdev->active[i] = 0; + } if (initialized < CONSOLE_INIT_FULL) { char ch; initialized = CONSOLE_INIT_FULL; puts_ll("Switch to console ["); - puts_ll(dev_name(dev)); + puts_ll(dev_name(&cdev->class_dev)); puts_ll("]\n"); barebox_banner(); while (kfifo_getc(console_output_fifo, &ch) == 0) @@ -116,29 +107,87 @@ return 0; } -static int console_baudrate_set(struct param_d *param, void *priv) +unsigned console_get_active(struct console_device *cdev) { - struct console_device *cdev = priv; + return cdev->f_active; +} + +static int console_active_set(struct device_d *dev, struct param_d *param, + const char *val) +{ + struct console_device *cdev = to_console_dev(dev); + unsigned int flag = 0; + + if (val) { + if (strchr(val, 'i')) + flag |= CONSOLE_STDIN; + if (strchr(val, 'o')) + flag |= CONSOLE_STDOUT; + if (strchr(val, 'e')) + flag |= CONSOLE_STDERR; + } + + return console_set_active(cdev, flag); +} + +static const char *console_active_get(struct device_d *dev, + struct param_d *param) +{ + struct console_device *cdev = to_console_dev(dev); + + return cdev->active; +} + +int console_set_baudrate(struct console_device *cdev, unsigned baudrate) +{ + int ret; unsigned char c; + if (!cdev->setbrg) + return -ENOSYS; + + if (cdev->baudrate == baudrate) + return 0; + /* * If the device is already active, change its baudrate. * The baudrate of an inactive device will be set at activation time. */ if (cdev->f_active) { - printf("## Switch baudrate to %d bps and press ENTER ...\n", - cdev->baudrate); + printf("## Switch baudrate on console %s to %d bps and press ENTER ...\n", + dev_name(&cdev->class_dev), baudrate); mdelay(50); - cdev->setbrg(cdev, cdev->baudrate); + } + + ret = cdev->setbrg(cdev, baudrate); + if (ret) + return ret; + + if (cdev->f_active) { mdelay(50); do { c = getc(); } while (c != '\r' && c != '\n'); } + cdev->baudrate = baudrate; + cdev->baudrate_param = baudrate; + return 0; } +unsigned console_get_baudrate(struct console_device *cdev) +{ + return cdev->baudrate; +} + +static int console_baudrate_set(struct param_d *param, void *priv) +{ + struct console_device *cdev = priv; + + return console_set_baudrate(cdev, cdev->baudrate_param); +} + static void console_init_early(void) { kfifo_init(console_input_fifo, console_input_buffer, @@ -208,13 +257,13 @@ if (newcdev->setbrg) { newcdev->baudrate = CONFIG_BAUDRATE; dev_add_param_int(dev, "baudrate", console_baudrate_set, - NULL, &newcdev->baudrate, "%u", newcdev); + NULL, &newcdev->baudrate_param, "%u", newcdev); } if (newcdev->putc && !newcdev->puts) newcdev->puts = __console_puts; - dev_add_param(dev, "active", console_std_set, NULL, 0); + dev_add_param(dev, "active", console_active_set, console_active_get, 0); if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_FIRST)) { if (list_empty(&console_list)) @@ -230,12 +279,9 @@ list_add_tail(&newcdev->list, &console_list); - if (activate) { - if (IS_ENABLED(CONFIG_PARAMETER)) - dev_set_param(dev, "active", "ioe"); - else - console_std_set(dev, NULL, "ioe"); - } + if (activate) + console_set_active(newcdev, CONSOLE_STDIN | + CONSOLE_STDOUT | CONSOLE_STDERR); return 0; } diff --git a/common/console_common.c b/common/console_common.c index 41a6929..1e362ab 100644 --- a/common/console_common.c +++ b/common/console_common.c @@ -260,6 +260,19 @@ } EXPORT_SYMBOL(console_get_by_dev); +struct console_device *console_get_by_name(const char *name) +{ + struct console_device *cdev; + + for_each_console(cdev) { + if (!strcmp(cdev->devname, name)) + return cdev; + } + + return NULL; +} +EXPORT_SYMBOL(console_get_by_name); + /* * @brief returns current used console device * diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c index 1e5e809..2b0faf3 100644 --- a/drivers/usb/gadget/u_serial.c +++ b/drivers/usb/gadget/u_serial.c @@ -536,7 +536,8 @@ if (status) goto fail_out; - dev_set_param(&cdev->class_dev, "active", "ioe"); + console_set_active(cdev, CONSOLE_STDIN | CONSOLE_STDOUT | + CONSOLE_STDERR); /* REVISIT if waiting on "carrier detect", signal. */ diff --git a/include/console.h b/include/console.h index 72b4a44..839ec17 100644 --- a/include/console.h +++ b/include/console.h @@ -52,8 +52,10 @@ struct list_head list; unsigned char f_active; + char active[4]; unsigned int baudrate; + unsigned int baudrate_param; const char *linux_console_name; }; @@ -62,6 +64,7 @@ int console_unregister(struct console_device *cdev); struct console_device *console_get_by_dev(struct device_d *dev); +struct console_device *console_get_by_name(const char *name); extern struct list_head console_list; #define for_each_console(console) list_for_each_entry(console, &console_list, list) @@ -75,4 +78,9 @@ struct console_device *console_get_first_active(void); +int console_set_active(struct console_device *cdev, unsigned active); +unsigned console_get_active(struct console_device *cdev); +int console_set_baudrate(struct console_device *cdev, unsigned baudrate); +unsigned console_get_baudrate(struct console_device *cdev); + #endif