9
0
Fork 0
barebox/common/console.c

347 lines
7.0 KiB
C
Raw Normal View History

2002-11-03 00:01:44 +00:00
/*
* (C) Copyright 2000
* Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-07-05 16:01:46 +00:00
#include <config.h>
2002-11-03 00:01:44 +00:00
#include <common.h>
#include <stdarg.h>
#include <malloc.h>
#include <param.h>
2002-11-03 00:01:44 +00:00
#include <console.h>
#include <driver.h>
#include <fs.h>
2007-07-12 07:31:07 +00:00
#include <init.h>
2007-09-21 19:30:59 +00:00
#include <clock.h>
#include <kfifo.h>
#include <module.h>
#include <poller.h>
#include <linux/list.h>
#include <linux/stringify.h>
#include <debug_ll.h>
2002-11-03 00:01:44 +00:00
LIST_HEAD(console_list);
EXPORT_SYMBOL(console_list);
2007-07-12 07:31:07 +00:00
#define CONSOLE_UNINITIALIZED 0
#define CONSOLE_INITIALIZED_BUFFER 1
#define CONSOLE_INIT_FULL 2
2007-07-12 07:31:07 +00:00
#define to_console_dev(d) container_of(d, struct console_device, class_dev)
static int initialized = 0;
#define CONSOLE_BUFFER_SIZE 1024
static char console_input_buffer[CONSOLE_BUFFER_SIZE];
static char console_output_buffer[CONSOLE_BUFFER_SIZE];
static struct kfifo __console_input_fifo;
static struct kfifo __console_output_fifo;
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)
{
struct console_device *cdev = to_console_dev(dev);
char active[4];
unsigned int flag = 0, i = 0;
if (val) {
if (strchr(val, 'i') && cdev->f_caps & CONSOLE_STDIN) {
active[i++] = 'i';
flag |= CONSOLE_STDIN;
}
if (strchr(val, 'o') && cdev->f_caps & CONSOLE_STDOUT) {
active[i++] = 'o';
flag |= CONSOLE_STDOUT;
}
if (strchr(val, 'e') && cdev->f_caps & CONSOLE_STDERR) {
active[i++] = 'e';
flag |= CONSOLE_STDERR;
}
}
if (flag && !cdev->f_active) {
/* The device is being activated, set its baudrate */
if (cdev->setbrg)
cdev->setbrg(cdev, cdev->baudrate);
}
active[i] = 0;
cdev->f_active = flag;
dev_param_set_generic(dev, param, active);
if (initialized < CONSOLE_INIT_FULL) {
char ch;
initialized = CONSOLE_INIT_FULL;
puts_ll("Switch to console [");
puts_ll(dev_name(dev));
puts_ll("]\n");
barebox_banner();
while (kfifo_getc(console_output_fifo, &ch) == 0)
console_putc(CONSOLE_STDOUT, ch);
}
return 0;
}
static int console_baudrate_set(struct param_d *param, void *priv)
{
struct console_device *cdev = priv;
unsigned char c;
/*
* 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);
mdelay(50);
cdev->setbrg(cdev, cdev->baudrate);
mdelay(50);
do {
c = getc();
} while (c != '\r' && c != '\n');
}
return 0;
}
static void console_init_early(void)
{
kfifo_init(console_input_fifo, console_input_buffer,
CONSOLE_BUFFER_SIZE);
kfifo_init(console_output_fifo, console_output_buffer,
CONSOLE_BUFFER_SIZE);
initialized = CONSOLE_INITIALIZED_BUFFER;
}
int console_register(struct console_device *newcdev)
2002-11-03 00:01:44 +00:00
{
struct device_d *dev = &newcdev->class_dev;
int activate = 0;
if (initialized == CONSOLE_UNINITIALIZED)
console_init_early();
dev->id = DEVICE_ID_DYNAMIC;
strcpy(dev->name, "cs");
if (newcdev->dev)
drivers/base: fix corrupt device tree dev_add_child is a very unsafe function. If called multiple times it allows setting the same device to different parents thus corrupting the siblings list. This happens regularly since: | commit c2e568d19c5c34a05a1002d25280bf113b72b752 | Author: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | Date: Sat Nov 3 16:11:05 2012 +0100 | | bus: add bus device | | automatically add it as parent of any bus device if none already specified | | we have now a nice output per bus If for example a FATfs is mounted this nice output per bus often ends with: > `---- fat0 > `---- 0 > `---- 0x86f0000087020031-0x86f000410df27124: /dev/<NULL> > `---- sram00 > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > `---- 0x00000000-0xffffffffffffffff: /dev/<NULL> > unable to handle NULL pointer dereference at address 0x0000000c > pc : [<87f08a20>] lr : [<87f08a04>] > sp : 86eff8c0 ip : 87f3fbde fp : ffffffff > r10: ffffffff r9 : 00000000 r8 : 00000003 > r7 : 86f075b8 r6 : 00000002 r5 : ffffffec r4 : 86f07544 > r3 : 00000000 r2 : 43f900b4 r1 : 00000020 r0 : 00000005 > Flags: Nzcv IRQs off FIQs off Mode SVC_32 > [<87f08a20>] (do_devinfo_subtree+0x90/0x130) from [<87f08a90>] (do_devinfo_subtree+0x100/0x130) > > [<87f3e070>] (unwind_backtrace+0x0/0x90) from [<87f28514>] (panic+0x28/0x3c) > [<87f28514>] (panic+0x28/0x3c) from [<87f3e4b8>] (do_exception+0x10/0x14) > [<87f3e4b8>] (do_exception+0x10/0x14) from [<87f3e544>] (do_data_abort+0x2c/0x38) > [<87f3e544>] (do_data_abort+0x2c/0x38) from [<87f3e268>] (data_abort+0x48/0x60) This patch fixes this by adding a device to its parents children list in register_device so that dev_add_child is no longer needed. This function is removed from the tree. Now callers of register_device have to clearly set the parent *before* registering a device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Reported-by: Jan Lübbe <jlu@pengutronix.de>
2012-12-12 13:55:40 +00:00
dev->parent = newcdev->dev;
platform_device_register(dev);
2007-07-05 16:02:18 +00:00
if (newcdev->setbrg) {
newcdev->baudrate = CONFIG_BAUDRATE;
dev_add_param_int(dev, "baudrate", console_baudrate_set,
NULL, &newcdev->baudrate, "%u", newcdev);
}
dev_add_param(dev, "active", console_std_set, NULL, 0);
if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_FIRST)) {
if (list_empty(&console_list))
activate = 1;
} else if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_ALL)) {
activate = 1;
}
if (newcdev->dev && of_device_is_stdout_path(newcdev->dev))
activate = 1;
list_add_tail(&newcdev->list, &console_list);
if (activate)
dev_set_param(dev, "active", "ioe");
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_register);
2002-11-03 00:01:44 +00:00
int console_unregister(struct console_device *cdev)
{
struct device_d *dev = &cdev->class_dev;
int status;
list_del(&cdev->list);
if (list_empty(&console_list))
initialized = CONSOLE_UNINITIALIZED;
status = unregister_device(dev);
if (!status)
memset(cdev, 0, sizeof(*cdev));
return status;
}
EXPORT_SYMBOL(console_unregister);
static int getc_raw(void)
{
struct console_device *cdev;
int active = 0;
while (1) {
for_each_console(cdev) {
if (!(cdev->f_active & CONSOLE_STDIN))
continue;
active = 1;
if (cdev->tstc(cdev))
return cdev->getc(cdev);
}
if (!active)
/* no active console found. bail out */
return -1;
}
}
2002-11-03 00:01:44 +00:00
static int tstc_raw(void)
{
struct console_device *cdev;
for_each_console(cdev) {
if (!(cdev->f_active & CONSOLE_STDIN))
continue;
if (cdev->tstc(cdev))
return 1;
}
return 0;
}
2007-09-21 19:30:59 +00:00
int getc(void)
{
unsigned char ch;
uint64_t start;
/*
* For 100us we read the characters from the serial driver
* into a kfifo. This helps us not to lose characters
* in small hardware fifos.
*/
2007-09-21 19:30:59 +00:00
start = get_time_ns();
while (1) {
if (tstc_raw()) {
kfifo_putc(console_input_fifo, getc_raw());
2007-09-21 19:30:59 +00:00
start = get_time_ns();
}
if (is_timeout(start, 100 * USECOND) &&
kfifo_len(console_input_fifo))
2007-09-21 19:30:59 +00:00
break;
}
kfifo_getc(console_input_fifo, &ch);
2007-09-21 19:30:59 +00:00
return ch;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(getc);
2007-09-21 19:30:59 +00:00
int fgetc(int fd)
{
char c;
2002-11-03 00:01:44 +00:00
if (!fd)
return getc();
return read(fd, &c, 1);
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(fgetc);
2002-11-03 00:01:44 +00:00
int tstc(void)
2002-11-03 00:01:44 +00:00
{
return kfifo_len(console_input_fifo) || tstc_raw();
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(tstc);
2002-11-03 00:01:44 +00:00
void console_putc(unsigned int ch, char c)
2002-11-03 00:01:44 +00:00
{
struct console_device *cdev;
int init = initialized;
2007-07-12 07:31:07 +00:00
switch (init) {
case CONSOLE_UNINITIALIZED:
console_init_early();
/* fall through */
case CONSOLE_INITIALIZED_BUFFER:
kfifo_putc(console_output_fifo, c);
putc_ll(c);
2007-07-12 07:31:07 +00:00
return;
case CONSOLE_INIT_FULL:
for_each_console(cdev) {
2007-07-12 07:31:07 +00:00
if (cdev->f_active & ch) {
if (c == '\n')
cdev->putc(cdev, '\r');
cdev->putc(cdev, c);
2007-07-12 07:31:07 +00:00
}
}
2007-07-12 07:31:07 +00:00
return;
default:
/* If we have problems inititalizing our data
* get them early
*/
hang();
}
2002-11-03 00:01:44 +00:00
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_putc);
2002-11-03 00:01:44 +00:00
int console_puts(unsigned int ch, const char *str)
2002-11-03 00:01:44 +00:00
{
2007-07-12 07:31:07 +00:00
const char *s = str;
int n = 0;
2007-07-12 07:31:07 +00:00
while (*s) {
if (*s == '\n') {
2007-07-12 07:31:07 +00:00
console_putc(ch, '\r');
n++;
}
console_putc(ch, *s);
n++;
2007-07-12 07:31:07 +00:00
s++;
}
return n;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(console_puts);
void console_flush(void)
{
struct console_device *cdev;
for_each_console(cdev) {
if (cdev->flush)
cdev->flush(cdev);
}
}
EXPORT_SYMBOL(console_flush);
#ifndef ARCH_HAS_CTRLC
2002-11-03 00:01:44 +00:00
/* test if ctrl-c was pressed */
int ctrlc (void)
{
poller_call();
if (tstc() && getc() == 3)
return 1;
2002-11-03 00:01:44 +00:00
return 0;
}
2007-10-07 12:27:24 +00:00
EXPORT_SYMBOL(ctrlc);
#endif /* ARCH_HAS_CTRC */