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
11 #include <linux/ctype.h>
14 #include <video_console.h>
15 #include <video_font.h> /* Bitmap font for code page 437 */
18 * Structure to describe a console color
26 /* By default we scroll by a single line */
27 #ifndef CONFIG_CONSOLE_SCROLL_LINES
28 #define CONFIG_CONSOLE_SCROLL_LINES 1
31 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
33 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
37 return ops->putc_xy(dev, x, y, ch);
40 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47 return ops->move_rows(dev, rowdst, rowsrc, count);
50 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
56 return ops->set_row(dev, row, clr);
59 static int vidconsole_entry_start(struct udevice *dev)
61 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
63 if (!ops->entry_start)
65 return ops->entry_start(dev);
68 /* Move backwards one space */
69 static int vidconsole_back(struct udevice *dev)
71 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
72 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
76 ret = ops->backspace(dev);
81 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
82 if (priv->xcur_frac < priv->xstart_frac) {
83 priv->xcur_frac = (priv->cols - 1) *
84 VID_TO_POS(priv->x_charsize);
85 priv->ycur -= priv->y_charsize;
89 video_sync(dev->parent, false);
94 /* Move to a newline, scrolling the display if necessary */
95 static void vidconsole_newline(struct udevice *dev)
97 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
98 struct udevice *vid_dev = dev->parent;
99 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
100 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
103 priv->xcur_frac = priv->xstart_frac;
104 priv->ycur += priv->y_charsize;
106 /* Check if we need to scroll the terminal */
107 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
108 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
109 for (i = 0; i < rows; i++)
110 vidconsole_set_row(dev, priv->rows - i - 1,
111 vid_priv->colour_bg);
112 priv->ycur -= rows * priv->y_charsize;
116 video_sync(dev->parent, false);
119 #if CONFIG_IS_ENABLED(VIDEO_BPP16) || CONFIG_IS_ENABLED(VIDEO_BPP32)
120 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
121 { 0x00, 0x00, 0x00 }, /* black */
122 { 0xc0, 0x00, 0x00 }, /* red */
123 { 0x00, 0xc0, 0x00 }, /* green */
124 { 0xc0, 0x60, 0x00 }, /* brown */
125 { 0x00, 0x00, 0xc0 }, /* blue */
126 { 0xc0, 0x00, 0xc0 }, /* magenta */
127 { 0x00, 0xc0, 0xc0 }, /* cyan */
128 { 0xc0, 0xc0, 0xc0 }, /* light gray */
129 { 0x80, 0x80, 0x80 }, /* gray */
130 { 0xff, 0x00, 0x00 }, /* bright red */
131 { 0x00, 0xff, 0x00 }, /* bright green */
132 { 0xff, 0xff, 0x00 }, /* yellow */
133 { 0x00, 0x00, 0xff }, /* bright blue */
134 { 0xff, 0x00, 0xff }, /* bright magenta */
135 { 0x00, 0xff, 0xff }, /* bright cyan */
136 { 0xff, 0xff, 0xff }, /* white */
140 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
142 switch (priv->bpix) {
143 #if CONFIG_IS_ENABLED(VIDEO_BPP16)
145 return ((colors[idx].r >> 3) << 11) |
146 ((colors[idx].g >> 2) << 5) |
147 ((colors[idx].b >> 3) << 0);
149 #if CONFIG_IS_ENABLED(VIDEO_BPP32)
151 return (colors[idx].r << 16) |
152 (colors[idx].g << 8) |
153 (colors[idx].b << 0);
157 * For unknown bit arrangements just support
161 return 0xffffff; /* white */
163 return 0x000000; /* black */
167 static char *parsenum(char *s, int *num)
170 *num = simple_strtol(s, &end, 10);
175 * set_cursor_position() - set cursor position
177 * @priv: private data of the video console
181 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
184 * Ensure we stay in the bounds of the screen.
186 if (row >= priv->rows)
187 row = priv->rows - 1;
188 if (col >= priv->cols)
189 col = priv->cols - 1;
191 priv->ycur = row * priv->y_charsize;
192 priv->xcur_frac = priv->xstart_frac +
193 VID_TO_POS(col * priv->x_charsize);
197 * get_cursor_position() - get cursor position
199 * @priv: private data of the video console
203 static void get_cursor_position(struct vidconsole_priv *priv,
206 *row = priv->ycur / priv->y_charsize;
207 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
212 * Process a character while accumulating an escape string. Chars are
213 * accumulated into escape_buf until the end of escape sequence is
214 * found, at which point the sequence is parsed and processed.
216 static void vidconsole_escape_char(struct udevice *dev, char ch)
218 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
220 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
223 /* Sanity checking for bogus ESC sequences: */
224 if (priv->escape_len >= sizeof(priv->escape_buf))
226 if (priv->escape_len == 0) {
229 /* Save cursor position */
230 get_cursor_position(priv, &priv->row_saved,
236 /* Restore cursor position */
237 int row = priv->row_saved;
238 int col = priv->col_saved;
240 set_cursor_position(priv, row, col);
251 priv->escape_buf[priv->escape_len++] = ch;
254 * Escape sequences are terminated by a letter, so keep
255 * accumulating until we get one:
261 * clear escape mode first, otherwise things will get highly
262 * surprising if you hit any debug prints that come back to
275 char *s = priv->escape_buf;
278 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
279 * Cursor left/right: [%dD, [%dC
282 s = parsenum(s, &num);
283 if (num == 0) /* No digit in sequence ... */
284 num = 1; /* ... means "move by 1". */
286 get_cursor_position(priv, &row, &col);
287 if (ch == 'A' || ch == 'F')
293 if (ch == 'B' || ch == 'E')
295 if (ch == 'E' || ch == 'F')
301 /* Right and bottom overflows are handled in the callee. */
302 set_cursor_position(priv, row, col);
308 char *s = priv->escape_buf;
311 * Set cursor position: [%d;%df or [%d;%dH
314 s = parsenum(s, &row);
316 s = parsenum(s, &col);
319 * Video origin is [0, 0], terminal origin is [1, 1].
326 set_cursor_position(priv, row, col);
334 * Clear part/all screen:
335 * [J or [0J - clear screen from cursor down
336 * [1J - clear screen from cursor up
337 * [2J - clear entire screen
339 * TODO we really only handle entire-screen case, others
340 * probably require some additions to video-uclass (and
341 * are not really needed yet by efi_console)
343 parsenum(priv->escape_buf + 1, &mode);
346 video_clear(dev->parent);
347 video_sync(dev->parent, false);
349 priv->xcur_frac = priv->xstart_frac;
351 debug("unsupported clear mode: %d\n", mode);
356 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
360 * Clear (parts of) current line
361 * [0K - clear line to end
362 * [2K - clear entire line
364 parsenum(priv->escape_buf + 1, &mode);
369 get_cursor_position(priv, &row, &col);
370 vidconsole_set_row(dev, row, vid_priv->colour_bg);
375 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
376 char *s = priv->escape_buf;
377 char *end = &priv->escape_buf[priv->escape_len];
380 * Set graphics mode: [%d;...;%dm
382 * Currently only supports the color attributes:
411 s = parsenum(s, &val);
416 /* all attributes off */
417 video_set_default_colors(dev->parent, false);
421 vid_priv->fg_col_idx |= 8;
422 vid_priv->colour_fg = vid_console_color(
423 vid_priv, vid_priv->fg_col_idx);
427 vid_priv->colour_fg = vid_console_color(
428 vid_priv, vid_priv->bg_col_idx);
429 vid_priv->colour_bg = vid_console_color(
430 vid_priv, vid_priv->fg_col_idx);
433 /* foreground color */
434 vid_priv->fg_col_idx &= ~7;
435 vid_priv->fg_col_idx |= val - 30;
436 vid_priv->colour_fg = vid_console_color(
437 vid_priv, vid_priv->fg_col_idx);
440 /* background color, also mask the bold bit */
441 vid_priv->bg_col_idx &= ~0xf;
442 vid_priv->bg_col_idx |= val - 40;
443 vid_priv->colour_bg = vid_console_color(
444 vid_priv, vid_priv->bg_col_idx);
447 /* ignore unsupported SGR parameter */
455 debug("unrecognized escape sequence: %*s\n",
456 priv->escape_len, priv->escape_buf);
462 /* something went wrong, just revert to normal mode: */
466 /* Put that actual character on the screen (using the CP437 code page). */
467 static int vidconsole_output_glyph(struct udevice *dev, char ch)
469 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
473 * Failure of this function normally indicates an unsupported
474 * colour depth. Check this and return an error to help with
477 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
478 if (ret == -EAGAIN) {
479 vidconsole_newline(dev);
480 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
484 priv->xcur_frac += ret;
486 if (priv->xcur_frac >= priv->xsize_frac)
487 vidconsole_newline(dev);
492 int vidconsole_put_char(struct udevice *dev, char ch)
494 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
498 vidconsole_escape_char(dev, ch);
504 priv->escape_len = 0;
511 priv->xcur_frac = priv->xstart_frac;
514 vidconsole_newline(dev);
515 vidconsole_entry_start(dev);
517 case '\t': /* Tab (8 chars alignment) */
518 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
519 + 1) * priv->tab_width_frac;
521 if (priv->xcur_frac >= priv->xsize_frac)
522 vidconsole_newline(dev);
525 vidconsole_back(dev);
529 ret = vidconsole_output_glyph(dev, ch);
538 int vidconsole_put_string(struct udevice *dev, const char *str)
543 for (s = str; *s; s++) {
544 ret = vidconsole_put_char(dev, *s);
552 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
554 struct udevice *dev = sdev->priv;
556 vidconsole_put_char(dev, ch);
557 video_sync(dev->parent, false);
560 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
562 struct udevice *dev = sdev->priv;
564 vidconsole_put_string(dev, s);
565 video_sync(dev->parent, false);
568 /* Set up the number of rows and colours (rotated drivers override this) */
569 static int vidconsole_pre_probe(struct udevice *dev)
571 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
572 struct udevice *vid = dev->parent;
573 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
575 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
580 /* Register the device with stdio */
581 static int vidconsole_post_probe(struct udevice *dev)
583 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
584 struct stdio_dev *sdev = &priv->sdev;
586 if (!priv->tab_width_frac)
587 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
590 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
593 strcpy(sdev->name, "vidconsole");
596 sdev->flags = DEV_FLAGS_OUTPUT;
597 sdev->putc = vidconsole_putc;
598 sdev->puts = vidconsole_puts;
601 return stdio_register(sdev);
604 UCLASS_DRIVER(vidconsole) = {
605 .id = UCLASS_VIDEO_CONSOLE,
606 .name = "vidconsole0",
607 .pre_probe = vidconsole_pre_probe,
608 .post_probe = vidconsole_post_probe,
609 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
612 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
614 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
615 struct udevice *vid_dev = dev->parent;
616 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
618 col *= priv->x_charsize;
619 row *= priv->y_charsize;
620 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
621 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
624 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
627 unsigned int col, row;
631 return CMD_RET_USAGE;
633 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
634 return CMD_RET_FAILURE;
635 col = simple_strtoul(argv[1], NULL, 10);
636 row = simple_strtoul(argv[2], NULL, 10);
637 vidconsole_position_cursor(dev, col, row);
642 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
649 return CMD_RET_USAGE;
651 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
652 return CMD_RET_FAILURE;
653 for (s = argv[1]; *s; s++)
654 vidconsole_put_char(dev, *s);
656 video_sync(dev->parent, false);
662 setcurs, 3, 1, do_video_setcursor,
663 "set cursor position within screen",
664 " <col> <row> in character"
668 lcdputs, 2, 1, do_video_puts,
669 "print string on video framebuffer",