[U-Boot] [PATCH 3/7] dm: serial: consolidate common code
Masahiro Yamada
yamada.m at jp.panasonic.com
Wed Oct 22 11:13:58 CEST 2014
Before the console is available, the functions serial_*()
are used, while serial_stub_*() are called after the console
is ready.
Functions in those two groups are almost the same except
how udevice is passed; serial_*() pass "cur_dev" whereas
serial_stub_*() pass sdev->priv.
This commit merges the duplicated code; common lines are put into
_serlal_*().
Signed-off-by: Masahiro Yamada <yamada.m at jp.panasonic.com>
---
drivers/serial/serial-uclass.c | 98 ++++++++++++++++++++----------------------
1 file changed, 47 insertions(+), 51 deletions(-)
diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c
index 163308b..6ee097d 100644
--- a/drivers/serial/serial-uclass.c
+++ b/drivers/serial/serial-uclass.c
@@ -71,52 +71,72 @@ void serial_initialize(void)
serial_find_console_or_panic();
}
-void serial_putc(char ch)
+static void _serial_putc(struct udevice *dev, const char ch)
{
- struct dm_serial_ops *ops = serial_get_ops(cur_dev);
- int err;
+ struct dm_serial_ops *ops = serial_get_ops(dev);
+ int res;
do {
- err = ops->putc(cur_dev, ch);
- } while (err == -EAGAIN);
+ res = ops->putc(cur_dev, ch);
+ } while (res == -EAGAIN);
if (ch == '\n')
- serial_putc('\r');
+ _serial_putc(dev, '\r');
}
-void serial_setbrg(void)
+static void _serial_puts(struct udevice *dev, const char *str)
{
- struct dm_serial_ops *ops = serial_get_ops(cur_dev);
-
- if (ops->setbrg)
- ops->setbrg(cur_dev, gd->baudrate);
+ while (*str)
+ _serial_putc(dev, *str++);
}
-void serial_puts(const char *str)
+static int _serial_getc(struct udevice *dev)
{
- while (*str)
- serial_putc(*str++);
+ struct dm_serial_ops *ops = serial_get_ops(dev);
+ int res;
+
+ do {
+ res = ops->getc(dev);
+ } while (res == -EAGAIN);
+
+ return res >= 0 ? res : 0;
}
-int serial_tstc(void)
+static int _serial_tstc(struct udevice *dev)
{
- struct dm_serial_ops *ops = serial_get_ops(cur_dev);
+ struct dm_serial_ops *ops = serial_get_ops(dev);
if (ops->pending)
- return ops->pending(cur_dev, true);
+ return ops->pending(dev, true);
return 1;
}
+void serial_putc(char ch)
+{
+ _serial_putc(cur_dev, ch);
+}
+
+void serial_puts(const char *str)
+{
+ _serial_puts(cur_dev, str);
+}
+
int serial_getc(void)
{
- struct dm_serial_ops *ops = serial_get_ops(cur_dev);
- int err;
+ return _serial_getc(cur_dev);
+}
- do {
- err = ops->getc(cur_dev);
- } while (err == -EAGAIN);
+int serial_tstc(void)
+{
+ return _serial_tstc(cur_dev);
+}
+
+void serial_setbrg(void)
+{
+ struct dm_serial_ops *ops = serial_get_ops(cur_dev);
- return err >= 0 ? err : 0;
+ if (ops->setbrg)
+ ops->setbrg(cur_dev, gd->baudrate);
}
void serial_stdio_init(void)
@@ -125,46 +145,22 @@ void serial_stdio_init(void)
void serial_stub_putc(struct stdio_dev *sdev, const char ch)
{
- struct udevice *dev = sdev->priv;
- struct dm_serial_ops *ops = serial_get_ops(dev);
- int err;
-
- do {
- err = ops->putc(cur_dev, ch);
- } while (err == -EAGAIN);
- if (ch == '\n')
- serial_putc('\r');
+ _serial_putc(sdev->priv, ch);
}
void serial_stub_puts(struct stdio_dev *sdev, const char *str)
{
- while (*str)
- serial_stub_putc(sdev, *str++);
+ _serial_puts(sdev->priv, str);
}
int serial_stub_getc(struct stdio_dev *sdev)
{
- struct udevice *dev = sdev->priv;
- struct dm_serial_ops *ops = serial_get_ops(dev);
-
- int err;
-
- do {
- err = ops->getc(dev);
- } while (err == -EAGAIN);
-
- return err >= 0 ? err : 0;
+ return _serial_getc(sdev->priv);
}
int serial_stub_tstc(struct stdio_dev *sdev)
{
- struct udevice *dev = sdev->priv;
- struct dm_serial_ops *ops = serial_get_ops(dev);
-
- if (ops->pending)
- return ops->pending(dev, true);
-
- return 1;
+ return _serial_tstc(sdev->priv);
}
static int serial_post_probe(struct udevice *dev)
--
1.9.1
More information about the U-Boot
mailing list