1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2001-2015
5 * DENX Software Engineering -- wd@denx.de
6 * Compulab Ltd - http://compulab.co.il/
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
10 #define LOG_CATEGORY UCLASS_VIDEO_CONSOLE
18 #include <video_console.h>
19 #include <video_font.h> /* Bitmap font for code page 437 */
20 #include <linux/ctype.h>
23 * Structure to describe a console color
31 /* By default we scroll by a single line */
32 #ifndef CONFIG_CONSOLE_SCROLL_LINES
33 #define CONFIG_CONSOLE_SCROLL_LINES 1
36 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
38 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
42 return ops->putc_xy(dev, x, y, ch);
45 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
48 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
52 return ops->move_rows(dev, rowdst, rowsrc, count);
55 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
57 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
61 return ops->set_row(dev, row, clr);
64 static int vidconsole_entry_start(struct udevice *dev)
66 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
68 if (!ops->entry_start)
70 return ops->entry_start(dev);
73 /* Move backwards one space */
74 static int vidconsole_back(struct udevice *dev)
76 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
77 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
81 ret = ops->backspace(dev);
86 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
87 if (priv->xcur_frac < priv->xstart_frac) {
88 priv->xcur_frac = (priv->cols - 1) *
89 VID_TO_POS(priv->x_charsize);
90 priv->ycur -= priv->y_charsize;
94 return video_sync(dev->parent, false);
97 /* Move to a newline, scrolling the display if necessary */
98 static void vidconsole_newline(struct udevice *dev)
100 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
101 struct udevice *vid_dev = dev->parent;
102 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
103 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
106 priv->xcur_frac = priv->xstart_frac;
107 priv->ycur += priv->y_charsize;
109 /* Check if we need to scroll the terminal */
110 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
111 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
112 for (i = 0; i < rows; i++)
113 vidconsole_set_row(dev, priv->rows - i - 1,
114 vid_priv->colour_bg);
115 priv->ycur -= rows * priv->y_charsize;
119 ret = video_sync(dev->parent, false);
122 console_puts_select_stderr(true, "[vc err: video_sync]");
127 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
128 { 0x00, 0x00, 0x00 }, /* black */
129 { 0xc0, 0x00, 0x00 }, /* red */
130 { 0x00, 0xc0, 0x00 }, /* green */
131 { 0xc0, 0x60, 0x00 }, /* brown */
132 { 0x00, 0x00, 0xc0 }, /* blue */
133 { 0xc0, 0x00, 0xc0 }, /* magenta */
134 { 0x00, 0xc0, 0xc0 }, /* cyan */
135 { 0xc0, 0xc0, 0xc0 }, /* light gray */
136 { 0x80, 0x80, 0x80 }, /* gray */
137 { 0xff, 0x00, 0x00 }, /* bright red */
138 { 0x00, 0xff, 0x00 }, /* bright green */
139 { 0xff, 0xff, 0x00 }, /* yellow */
140 { 0x00, 0x00, 0xff }, /* bright blue */
141 { 0xff, 0x00, 0xff }, /* bright magenta */
142 { 0x00, 0xff, 0xff }, /* bright cyan */
143 { 0xff, 0xff, 0xff }, /* white */
146 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
148 switch (priv->bpix) {
150 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
151 return ((colors[idx].r >> 3) << 11) |
152 ((colors[idx].g >> 2) << 5) |
153 ((colors[idx].b >> 3) << 0);
157 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
158 return (colors[idx].r << 16) |
159 (colors[idx].g << 8) |
160 (colors[idx].b << 0);
168 * For unknown bit arrangements just support
172 return 0xffffff; /* white */
174 return 0x000000; /* black */
177 static char *parsenum(char *s, int *num)
180 *num = simple_strtol(s, &end, 10);
185 * set_cursor_position() - set cursor position
187 * @priv: private data of the video console
191 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
194 * Ensure we stay in the bounds of the screen.
196 if (row >= priv->rows)
197 row = priv->rows - 1;
198 if (col >= priv->cols)
199 col = priv->cols - 1;
201 priv->ycur = row * priv->y_charsize;
202 priv->xcur_frac = priv->xstart_frac +
203 VID_TO_POS(col * priv->x_charsize);
207 * get_cursor_position() - get cursor position
209 * @priv: private data of the video console
213 static void get_cursor_position(struct vidconsole_priv *priv,
216 *row = priv->ycur / priv->y_charsize;
217 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
222 * Process a character while accumulating an escape string. Chars are
223 * accumulated into escape_buf until the end of escape sequence is
224 * found, at which point the sequence is parsed and processed.
226 static void vidconsole_escape_char(struct udevice *dev, char ch)
228 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
230 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
233 /* Sanity checking for bogus ESC sequences: */
234 if (priv->escape_len >= sizeof(priv->escape_buf))
236 if (priv->escape_len == 0) {
239 /* Save cursor position */
240 get_cursor_position(priv, &priv->row_saved,
246 /* Restore cursor position */
247 int row = priv->row_saved;
248 int col = priv->col_saved;
250 set_cursor_position(priv, row, col);
261 priv->escape_buf[priv->escape_len++] = ch;
264 * Escape sequences are terminated by a letter, so keep
265 * accumulating until we get one:
271 * clear escape mode first, otherwise things will get highly
272 * surprising if you hit any debug prints that come back to
285 char *s = priv->escape_buf;
288 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
289 * Cursor left/right: [%dD, [%dC
292 s = parsenum(s, &num);
293 if (num == 0) /* No digit in sequence ... */
294 num = 1; /* ... means "move by 1". */
296 get_cursor_position(priv, &row, &col);
297 if (ch == 'A' || ch == 'F')
303 if (ch == 'B' || ch == 'E')
305 if (ch == 'E' || ch == 'F')
311 /* Right and bottom overflows are handled in the callee. */
312 set_cursor_position(priv, row, col);
318 char *s = priv->escape_buf;
321 * Set cursor position: [%d;%df or [%d;%dH
324 s = parsenum(s, &row);
326 s = parsenum(s, &col);
329 * Video origin is [0, 0], terminal origin is [1, 1].
336 set_cursor_position(priv, row, col);
344 * Clear part/all screen:
345 * [J or [0J - clear screen from cursor down
346 * [1J - clear screen from cursor up
347 * [2J - clear entire screen
349 * TODO we really only handle entire-screen case, others
350 * probably require some additions to video-uclass (and
351 * are not really needed yet by efi_console)
353 parsenum(priv->escape_buf + 1, &mode);
358 video_clear(dev->parent);
359 ret = video_sync(dev->parent, false);
362 console_puts_select_stderr(true, "[vc err: video_sync]");
366 priv->xcur_frac = priv->xstart_frac;
368 debug("unsupported clear mode: %d\n", mode);
373 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
377 * Clear (parts of) current line
378 * [0K - clear line to end
379 * [2K - clear entire line
381 parsenum(priv->escape_buf + 1, &mode);
386 get_cursor_position(priv, &row, &col);
387 vidconsole_set_row(dev, row, vid_priv->colour_bg);
392 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
393 char *s = priv->escape_buf;
394 char *end = &priv->escape_buf[priv->escape_len];
397 * Set graphics mode: [%d;...;%dm
399 * Currently only supports the color attributes:
428 s = parsenum(s, &val);
433 /* all attributes off */
434 video_set_default_colors(dev->parent, false);
438 vid_priv->fg_col_idx |= 8;
439 vid_priv->colour_fg = vid_console_color(
440 vid_priv, vid_priv->fg_col_idx);
444 vid_priv->colour_fg = vid_console_color(
445 vid_priv, vid_priv->bg_col_idx);
446 vid_priv->colour_bg = vid_console_color(
447 vid_priv, vid_priv->fg_col_idx);
450 /* foreground color */
451 vid_priv->fg_col_idx &= ~7;
452 vid_priv->fg_col_idx |= val - 30;
453 vid_priv->colour_fg = vid_console_color(
454 vid_priv, vid_priv->fg_col_idx);
457 /* background color, also mask the bold bit */
458 vid_priv->bg_col_idx &= ~0xf;
459 vid_priv->bg_col_idx |= val - 40;
460 vid_priv->colour_bg = vid_console_color(
461 vid_priv, vid_priv->bg_col_idx);
464 /* ignore unsupported SGR parameter */
472 debug("unrecognized escape sequence: %*s\n",
473 priv->escape_len, priv->escape_buf);
479 /* something went wrong, just revert to normal mode: */
483 /* Put that actual character on the screen (using the CP437 code page). */
484 static int vidconsole_output_glyph(struct udevice *dev, char ch)
486 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
490 * Failure of this function normally indicates an unsupported
491 * colour depth. Check this and return an error to help with
494 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
495 if (ret == -EAGAIN) {
496 vidconsole_newline(dev);
497 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
501 priv->xcur_frac += ret;
503 if (priv->xcur_frac >= priv->xsize_frac)
504 vidconsole_newline(dev);
509 int vidconsole_put_char(struct udevice *dev, char ch)
511 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
515 vidconsole_escape_char(dev, ch);
521 priv->escape_len = 0;
528 priv->xcur_frac = priv->xstart_frac;
531 vidconsole_newline(dev);
532 vidconsole_entry_start(dev);
534 case '\t': /* Tab (8 chars alignment) */
535 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
536 + 1) * priv->tab_width_frac;
538 if (priv->xcur_frac >= priv->xsize_frac)
539 vidconsole_newline(dev);
542 vidconsole_back(dev);
546 ret = vidconsole_output_glyph(dev, ch);
555 int vidconsole_put_string(struct udevice *dev, const char *str)
560 for (s = str; *s; s++) {
561 ret = vidconsole_put_char(dev, *s);
569 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
571 struct udevice *dev = sdev->priv;
574 ret = vidconsole_put_char(dev, ch);
577 console_puts_select_stderr(true, "[vc err: putc]");
580 ret = video_sync(dev->parent, false);
583 console_puts_select_stderr(true, "[vc err: video_sync]");
588 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
590 struct udevice *dev = sdev->priv;
593 ret = vidconsole_put_string(dev, s);
598 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
599 console_puts_select_stderr(true, str);
602 ret = video_sync(dev->parent, false);
605 console_puts_select_stderr(true, "[vc err: video_sync]");
610 /* Set up the number of rows and colours (rotated drivers override this) */
611 static int vidconsole_pre_probe(struct udevice *dev)
613 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
614 struct udevice *vid = dev->parent;
615 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
617 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
622 /* Register the device with stdio */
623 static int vidconsole_post_probe(struct udevice *dev)
625 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
626 struct stdio_dev *sdev = &priv->sdev;
628 if (!priv->tab_width_frac)
629 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
632 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
635 strcpy(sdev->name, "vidconsole");
638 sdev->flags = DEV_FLAGS_OUTPUT;
639 sdev->putc = vidconsole_putc;
640 sdev->puts = vidconsole_puts;
643 return stdio_register(sdev);
646 UCLASS_DRIVER(vidconsole) = {
647 .id = UCLASS_VIDEO_CONSOLE,
648 .name = "vidconsole0",
649 .pre_probe = vidconsole_pre_probe,
650 .post_probe = vidconsole_post_probe,
651 .per_device_auto = sizeof(struct vidconsole_priv),
654 #ifdef CONFIG_VIDEO_COPY
655 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
657 struct udevice *vid = dev_get_parent(dev);
659 return video_sync_copy(vid, from, to);
662 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
665 memmove(dst, src, size);
666 return vidconsole_sync_copy(dev, dst, dst + size);
670 #if CONFIG_IS_ENABLED(CMD_VIDCONSOLE)
671 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
673 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
674 struct udevice *vid_dev = dev->parent;
675 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
677 col *= priv->x_charsize;
678 row *= priv->y_charsize;
679 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
680 priv->xstart_frac = priv->xcur_frac;
681 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
684 static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
687 unsigned int col, row;
691 return CMD_RET_USAGE;
693 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
694 return CMD_RET_FAILURE;
695 col = simple_strtoul(argv[1], NULL, 10);
696 row = simple_strtoul(argv[2], NULL, 10);
697 vidconsole_position_cursor(dev, col, row);
702 static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
709 return CMD_RET_USAGE;
711 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
712 return CMD_RET_FAILURE;
713 for (s = argv[1]; *s; s++)
714 vidconsole_put_char(dev, *s);
716 return video_sync(dev->parent, false);
720 setcurs, 3, 1, do_video_setcursor,
721 "set cursor position within screen",
722 " <col> <row> in character"
726 lcdputs, 2, 1, do_video_puts,
727 "print string on video framebuffer",
730 #endif /* CONFIG_IS_ENABLED(CMD_VIDCONSOLE) */