mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 3/7] console: make it work without malloc
Date: Thu, 26 Jan 2012 14:26:49 +0100	[thread overview]
Message-ID: <1327584413-23055-4-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1327584413-23055-1-git-send-email-s.hauer@pengutronix.de>

This changes the dynamically allocated kfifos to statically initialized
ones. This makes the console work without malloc and thus safe to be
called before malloc is initialized.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/console.c |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/common/console.c b/common/console.c
index 88c4010..abc0627 100644
--- a/common/console.c
+++ b/common/console.c
@@ -40,8 +40,9 @@
 LIST_HEAD(console_list);
 EXPORT_SYMBOL(console_list);
 
-#define CONSOLE_UNINITIALIZED	0
-#define CONSOLE_INIT_FULL	1
+#define CONSOLE_UNINITIALIZED		0
+#define CONSOLE_INITIALIZED_BUFFER	1
+#define CONSOLE_INIT_FULL		2
 
 static int initialized = 0;
 
@@ -109,17 +110,25 @@ static int console_baudrate_set(struct device_d *dev, struct param_d *param,
 	return 0;
 }
 
-static struct kfifo *console_input_buffer;
-static struct kfifo *console_output_buffer;
+#define CONSOLE_BUFFER_SIZE	1024
 
-static int getc_buffer_flush(void)
+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 void console_init_early(void)
 {
-	console_input_buffer = kfifo_alloc(1024);
-	console_output_buffer = kfifo_alloc(1024);
-	return 0;
-}
+	kfifo_init(console_input_fifo, console_input_buffer,
+			CONSOLE_BUFFER_SIZE);
+	kfifo_init(console_output_fifo, console_output_buffer,
+			CONSOLE_BUFFER_SIZE);
 
-postcore_initcall(getc_buffer_flush);
+	initialized = CONSOLE_INITIALIZED_BUFFER;
+}
 
 int console_register(struct console_device *newcdev)
 {
@@ -127,6 +136,9 @@ int console_register(struct console_device *newcdev)
 	int first = 0;
 	char ch;
 
+	if (initialized == CONSOLE_UNINITIALIZED)
+		console_init_early();
+
 	dev->id = -1;
 	strcpy(dev->name, "cs");
 	dev->type_data = newcdev;
@@ -154,8 +166,7 @@ int console_register(struct console_device *newcdev)
 
 	list_add_tail(&newcdev->list, &console_list);
 
-
-	while (kfifo_getc(console_output_buffer, &ch) == 0)
+	while (kfifo_getc(console_output_fifo, &ch) == 0)
 		console_putc(CONSOLE_STDOUT, ch);
 	if (first)
 		barebox_banner();
@@ -226,16 +237,16 @@ int getc(void)
 	start = get_time_ns();
 	while (1) {
 		if (tstc_raw()) {
-			kfifo_putc(console_input_buffer, getc_raw());
+			kfifo_putc(console_input_fifo, getc_raw());
 
 			start = get_time_ns();
 		}
 		if (is_timeout(start, 100 * USECOND) &&
-				kfifo_len(console_input_buffer))
+				kfifo_len(console_input_fifo))
 			break;
 	}
 
-	kfifo_getc(console_input_buffer, &ch);
+	kfifo_getc(console_input_fifo, &ch);
 	return ch;
 }
 EXPORT_SYMBOL(getc);
@@ -252,7 +263,7 @@ EXPORT_SYMBOL(fgetc);
 
 int tstc(void)
 {
-	return kfifo_len(console_input_buffer) || tstc_raw();
+	return kfifo_len(console_input_fifo) || tstc_raw();
 }
 EXPORT_SYMBOL(tstc);
 
@@ -263,7 +274,11 @@ void console_putc(unsigned int ch, char c)
 
 	switch (init) {
 	case CONSOLE_UNINITIALIZED:
-		kfifo_putc(console_output_buffer, c);
+		console_init_early();
+		/* fall through */
+
+	case CONSOLE_INITIALIZED_BUFFER:
+		kfifo_putc(console_output_fifo, c);
 		return;
 
 	case CONSOLE_INIT_FULL:
-- 
1.7.8.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2012-01-26 13:27 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26 13:26 some debug patches Sascha Hauer
2012-01-26 13:26 ` [PATCH 1/7] console: remove unused function prototypes Sascha Hauer
2012-01-26 13:26 ` [PATCH 2/7] kfifo: change kfifo_init to work with a preallocated fifo Sascha Hauer
2012-01-26 13:26 ` Sascha Hauer [this message]
2012-01-26 13:26 ` [PATCH 4/7] ARM: panic on div 0 Sascha Hauer
2012-01-26 13:26 ` [PATCH 5/7] initcalls: do not hang if an initcall fails Sascha Hauer
2012-01-26 13:26 ` [PATCH 6/7] startup: use regular debug statements in initcall debugging Sascha Hauer
2012-01-26 13:26 ` [PATCH 7/7] Add dump_stack function Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1327584413-23055-4-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox