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>
22 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
28 return ops->putc_xy(dev, x, y, ch);
31 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
38 return ops->move_rows(dev, rowdst, rowsrc, count);
41 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47 return ops->set_row(dev, row, clr);
50 static int vidconsole_entry_start(struct udevice *dev)
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
54 if (!ops->entry_start)
56 return ops->entry_start(dev);
59 /* Move backwards one space */
60 static int vidconsole_back(struct udevice *dev)
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
63 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
67 ret = ops->backspace(dev);
72 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
73 if (priv->xcur_frac < priv->xstart_frac) {
74 priv->xcur_frac = (priv->cols - 1) *
75 VID_TO_POS(priv->x_charsize);
76 priv->ycur -= priv->y_charsize;
80 return video_sync(dev->parent, false);
83 /* Move to a newline, scrolling the display if necessary */
84 static void vidconsole_newline(struct udevice *dev)
86 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
87 struct udevice *vid_dev = dev->parent;
88 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
89 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
92 priv->xcur_frac = priv->xstart_frac;
93 priv->ycur += priv->y_charsize;
95 /* Check if we need to scroll the terminal */
96 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
97 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
98 for (i = 0; i < rows; i++)
99 vidconsole_set_row(dev, priv->rows - i - 1,
100 vid_priv->colour_bg);
101 priv->ycur -= rows * priv->y_charsize;
105 ret = video_sync(dev->parent, false);
108 console_puts_select_stderr(true, "[vc err: video_sync]");
113 static char *parsenum(char *s, int *num)
116 *num = simple_strtol(s, &end, 10);
120 void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
122 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
124 priv->xcur_frac = VID_TO_POS(x);
125 priv->xstart_frac = priv->xcur_frac;
130 * set_cursor_position() - set cursor position
132 * @priv: private data of the video console
136 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
139 * Ensure we stay in the bounds of the screen.
141 if (row >= priv->rows)
142 row = priv->rows - 1;
143 if (col >= priv->cols)
144 col = priv->cols - 1;
146 priv->ycur = row * priv->y_charsize;
147 priv->xcur_frac = priv->xstart_frac +
148 VID_TO_POS(col * priv->x_charsize);
152 * get_cursor_position() - get cursor position
154 * @priv: private data of the video console
158 static void get_cursor_position(struct vidconsole_priv *priv,
161 *row = priv->ycur / priv->y_charsize;
162 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
167 * Process a character while accumulating an escape string. Chars are
168 * accumulated into escape_buf until the end of escape sequence is
169 * found, at which point the sequence is parsed and processed.
171 static void vidconsole_escape_char(struct udevice *dev, char ch)
173 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
175 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
178 /* Sanity checking for bogus ESC sequences: */
179 if (priv->escape_len >= sizeof(priv->escape_buf))
181 if (priv->escape_len == 0) {
184 /* Save cursor position */
185 get_cursor_position(priv, &priv->row_saved,
191 /* Restore cursor position */
192 int row = priv->row_saved;
193 int col = priv->col_saved;
195 set_cursor_position(priv, row, col);
206 priv->escape_buf[priv->escape_len++] = ch;
209 * Escape sequences are terminated by a letter, so keep
210 * accumulating until we get one:
216 * clear escape mode first, otherwise things will get highly
217 * surprising if you hit any debug prints that come back to
230 char *s = priv->escape_buf;
233 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
234 * Cursor left/right: [%dD, [%dC
237 s = parsenum(s, &num);
238 if (num == 0) /* No digit in sequence ... */
239 num = 1; /* ... means "move by 1". */
241 get_cursor_position(priv, &row, &col);
242 if (ch == 'A' || ch == 'F')
248 if (ch == 'B' || ch == 'E')
250 if (ch == 'E' || ch == 'F')
256 /* Right and bottom overflows are handled in the callee. */
257 set_cursor_position(priv, row, col);
263 char *s = priv->escape_buf;
266 * Set cursor position: [%d;%df or [%d;%dH
269 s = parsenum(s, &row);
271 s = parsenum(s, &col);
274 * Video origin is [0, 0], terminal origin is [1, 1].
281 set_cursor_position(priv, row, col);
289 * Clear part/all screen:
290 * [J or [0J - clear screen from cursor down
291 * [1J - clear screen from cursor up
292 * [2J - clear entire screen
294 * TODO we really only handle entire-screen case, others
295 * probably require some additions to video-uclass (and
296 * are not really needed yet by efi_console)
298 parsenum(priv->escape_buf + 1, &mode);
303 video_clear(dev->parent);
304 ret = video_sync(dev->parent, false);
307 console_puts_select_stderr(true, "[vc err: video_sync]");
311 priv->xcur_frac = priv->xstart_frac;
313 debug("unsupported clear mode: %d\n", mode);
318 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
322 * Clear (parts of) current line
323 * [0K - clear line to end
324 * [2K - clear entire line
326 parsenum(priv->escape_buf + 1, &mode);
331 get_cursor_position(priv, &row, &col);
332 vidconsole_set_row(dev, row, vid_priv->colour_bg);
337 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
338 char *s = priv->escape_buf;
339 char *end = &priv->escape_buf[priv->escape_len];
342 * Set graphics mode: [%d;...;%dm
344 * Currently only supports the color attributes:
373 s = parsenum(s, &val);
378 /* all attributes off */
379 video_set_default_colors(dev->parent, false);
383 vid_priv->fg_col_idx |= 8;
384 vid_priv->colour_fg = video_index_to_colour(
385 vid_priv, vid_priv->fg_col_idx);
389 vid_priv->colour_fg = video_index_to_colour(
390 vid_priv, vid_priv->bg_col_idx);
391 vid_priv->colour_bg = video_index_to_colour(
392 vid_priv, vid_priv->fg_col_idx);
395 /* foreground color */
396 vid_priv->fg_col_idx &= ~7;
397 vid_priv->fg_col_idx |= val - 30;
398 vid_priv->colour_fg = video_index_to_colour(
399 vid_priv, vid_priv->fg_col_idx);
402 /* background color, also mask the bold bit */
403 vid_priv->bg_col_idx &= ~0xf;
404 vid_priv->bg_col_idx |= val - 40;
405 vid_priv->colour_bg = video_index_to_colour(
406 vid_priv, vid_priv->bg_col_idx);
409 /* ignore unsupported SGR parameter */
417 debug("unrecognized escape sequence: %*s\n",
418 priv->escape_len, priv->escape_buf);
424 /* something went wrong, just revert to normal mode: */
428 /* Put that actual character on the screen (using the CP437 code page). */
429 static int vidconsole_output_glyph(struct udevice *dev, char ch)
431 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
435 * Failure of this function normally indicates an unsupported
436 * colour depth. Check this and return an error to help with
439 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
440 if (ret == -EAGAIN) {
441 vidconsole_newline(dev);
442 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
446 priv->xcur_frac += ret;
448 if (priv->xcur_frac >= priv->xsize_frac)
449 vidconsole_newline(dev);
454 int vidconsole_put_char(struct udevice *dev, char ch)
456 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
460 vidconsole_escape_char(dev, ch);
466 priv->escape_len = 0;
473 priv->xcur_frac = priv->xstart_frac;
476 vidconsole_newline(dev);
477 vidconsole_entry_start(dev);
479 case '\t': /* Tab (8 chars alignment) */
480 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
481 + 1) * priv->tab_width_frac;
483 if (priv->xcur_frac >= priv->xsize_frac)
484 vidconsole_newline(dev);
487 vidconsole_back(dev);
491 ret = vidconsole_output_glyph(dev, ch);
500 int vidconsole_put_string(struct udevice *dev, const char *str)
505 for (s = str; *s; s++) {
506 ret = vidconsole_put_char(dev, *s);
514 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
516 struct udevice *dev = sdev->priv;
519 ret = vidconsole_put_char(dev, ch);
522 console_puts_select_stderr(true, "[vc err: putc]");
525 ret = video_sync(dev->parent, false);
528 console_puts_select_stderr(true, "[vc err: video_sync]");
533 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
535 struct udevice *dev = sdev->priv;
538 ret = vidconsole_put_string(dev, s);
543 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
544 console_puts_select_stderr(true, str);
547 ret = video_sync(dev->parent, false);
550 console_puts_select_stderr(true, "[vc err: video_sync]");
555 void vidconsole_list_fonts(struct udevice *dev)
557 struct vidfont_info info;
560 for (i = 0, ret = 0; !ret; i++) {
561 ret = vidconsole_get_font(dev, i, &info);
563 printf("%s\n", info.name);
567 int vidconsole_get_font(struct udevice *dev, int seq,
568 struct vidfont_info *info)
570 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
575 return ops->get_font(dev, seq, info);
578 int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
580 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
582 if (!ops->select_font)
585 return ops->select_font(dev, name, size);
588 /* Set up the number of rows and colours (rotated drivers override this) */
589 static int vidconsole_pre_probe(struct udevice *dev)
591 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
592 struct udevice *vid = dev->parent;
593 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
595 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
600 /* Register the device with stdio */
601 static int vidconsole_post_probe(struct udevice *dev)
603 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
604 struct stdio_dev *sdev = &priv->sdev;
606 if (!priv->tab_width_frac)
607 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
610 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
613 strcpy(sdev->name, "vidconsole");
616 sdev->flags = DEV_FLAGS_OUTPUT;
617 sdev->putc = vidconsole_putc;
618 sdev->puts = vidconsole_puts;
621 return stdio_register(sdev);
624 UCLASS_DRIVER(vidconsole) = {
625 .id = UCLASS_VIDEO_CONSOLE,
626 .name = "vidconsole0",
627 .pre_probe = vidconsole_pre_probe,
628 .post_probe = vidconsole_post_probe,
629 .per_device_auto = sizeof(struct vidconsole_priv),
632 #ifdef CONFIG_VIDEO_COPY
633 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
635 struct udevice *vid = dev_get_parent(dev);
637 return video_sync_copy(vid, from, to);
640 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
643 memmove(dst, src, size);
644 return vidconsole_sync_copy(dev, dst, dst + size);
648 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
650 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
651 struct udevice *vid_dev = dev->parent;
652 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
655 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
656 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
657 vidconsole_set_cursor_pos(dev, x, y);