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 /* By default we scroll by a single line */
23 #ifndef CONFIG_CONSOLE_SCROLL_LINES
24 #define CONFIG_CONSOLE_SCROLL_LINES 1
27 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
29 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
33 return ops->putc_xy(dev, x, y, ch);
36 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
39 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
43 return ops->move_rows(dev, rowdst, rowsrc, count);
46 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
48 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
52 return ops->set_row(dev, row, clr);
55 static int vidconsole_entry_start(struct udevice *dev)
57 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
59 if (!ops->entry_start)
61 return ops->entry_start(dev);
64 /* Move backwards one space */
65 static int vidconsole_back(struct udevice *dev)
67 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
68 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
72 ret = ops->backspace(dev);
77 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
78 if (priv->xcur_frac < priv->xstart_frac) {
79 priv->xcur_frac = (priv->cols - 1) *
80 VID_TO_POS(priv->x_charsize);
81 priv->ycur -= priv->y_charsize;
85 return video_sync(dev->parent, false);
88 /* Move to a newline, scrolling the display if necessary */
89 static void vidconsole_newline(struct udevice *dev)
91 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
92 struct udevice *vid_dev = dev->parent;
93 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
94 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
97 priv->xcur_frac = priv->xstart_frac;
98 priv->ycur += priv->y_charsize;
100 /* Check if we need to scroll the terminal */
101 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
102 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
103 for (i = 0; i < rows; i++)
104 vidconsole_set_row(dev, priv->rows - i - 1,
105 vid_priv->colour_bg);
106 priv->ycur -= rows * priv->y_charsize;
110 ret = video_sync(dev->parent, false);
113 console_puts_select_stderr(true, "[vc err: video_sync]");
118 static char *parsenum(char *s, int *num)
121 *num = simple_strtol(s, &end, 10);
125 void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
127 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
129 priv->xcur_frac = VID_TO_POS(x);
130 priv->xstart_frac = priv->xcur_frac;
135 * set_cursor_position() - set cursor position
137 * @priv: private data of the video console
141 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
144 * Ensure we stay in the bounds of the screen.
146 if (row >= priv->rows)
147 row = priv->rows - 1;
148 if (col >= priv->cols)
149 col = priv->cols - 1;
151 priv->ycur = row * priv->y_charsize;
152 priv->xcur_frac = priv->xstart_frac +
153 VID_TO_POS(col * priv->x_charsize);
157 * get_cursor_position() - get cursor position
159 * @priv: private data of the video console
163 static void get_cursor_position(struct vidconsole_priv *priv,
166 *row = priv->ycur / priv->y_charsize;
167 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
172 * Process a character while accumulating an escape string. Chars are
173 * accumulated into escape_buf until the end of escape sequence is
174 * found, at which point the sequence is parsed and processed.
176 static void vidconsole_escape_char(struct udevice *dev, char ch)
178 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
180 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
183 /* Sanity checking for bogus ESC sequences: */
184 if (priv->escape_len >= sizeof(priv->escape_buf))
186 if (priv->escape_len == 0) {
189 /* Save cursor position */
190 get_cursor_position(priv, &priv->row_saved,
196 /* Restore cursor position */
197 int row = priv->row_saved;
198 int col = priv->col_saved;
200 set_cursor_position(priv, row, col);
211 priv->escape_buf[priv->escape_len++] = ch;
214 * Escape sequences are terminated by a letter, so keep
215 * accumulating until we get one:
221 * clear escape mode first, otherwise things will get highly
222 * surprising if you hit any debug prints that come back to
235 char *s = priv->escape_buf;
238 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
239 * Cursor left/right: [%dD, [%dC
242 s = parsenum(s, &num);
243 if (num == 0) /* No digit in sequence ... */
244 num = 1; /* ... means "move by 1". */
246 get_cursor_position(priv, &row, &col);
247 if (ch == 'A' || ch == 'F')
253 if (ch == 'B' || ch == 'E')
255 if (ch == 'E' || ch == 'F')
261 /* Right and bottom overflows are handled in the callee. */
262 set_cursor_position(priv, row, col);
268 char *s = priv->escape_buf;
271 * Set cursor position: [%d;%df or [%d;%dH
274 s = parsenum(s, &row);
276 s = parsenum(s, &col);
279 * Video origin is [0, 0], terminal origin is [1, 1].
286 set_cursor_position(priv, row, col);
294 * Clear part/all screen:
295 * [J or [0J - clear screen from cursor down
296 * [1J - clear screen from cursor up
297 * [2J - clear entire screen
299 * TODO we really only handle entire-screen case, others
300 * probably require some additions to video-uclass (and
301 * are not really needed yet by efi_console)
303 parsenum(priv->escape_buf + 1, &mode);
308 video_clear(dev->parent);
309 ret = video_sync(dev->parent, false);
312 console_puts_select_stderr(true, "[vc err: video_sync]");
316 priv->xcur_frac = priv->xstart_frac;
318 debug("unsupported clear mode: %d\n", mode);
323 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
327 * Clear (parts of) current line
328 * [0K - clear line to end
329 * [2K - clear entire line
331 parsenum(priv->escape_buf + 1, &mode);
336 get_cursor_position(priv, &row, &col);
337 vidconsole_set_row(dev, row, vid_priv->colour_bg);
342 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
343 char *s = priv->escape_buf;
344 char *end = &priv->escape_buf[priv->escape_len];
347 * Set graphics mode: [%d;...;%dm
349 * Currently only supports the color attributes:
378 s = parsenum(s, &val);
383 /* all attributes off */
384 video_set_default_colors(dev->parent, false);
388 vid_priv->fg_col_idx |= 8;
389 vid_priv->colour_fg = video_index_to_colour(
390 vid_priv, vid_priv->fg_col_idx);
394 vid_priv->colour_fg = video_index_to_colour(
395 vid_priv, vid_priv->bg_col_idx);
396 vid_priv->colour_bg = video_index_to_colour(
397 vid_priv, vid_priv->fg_col_idx);
400 /* foreground color */
401 vid_priv->fg_col_idx &= ~7;
402 vid_priv->fg_col_idx |= val - 30;
403 vid_priv->colour_fg = video_index_to_colour(
404 vid_priv, vid_priv->fg_col_idx);
407 /* background color, also mask the bold bit */
408 vid_priv->bg_col_idx &= ~0xf;
409 vid_priv->bg_col_idx |= val - 40;
410 vid_priv->colour_bg = video_index_to_colour(
411 vid_priv, vid_priv->bg_col_idx);
414 /* ignore unsupported SGR parameter */
422 debug("unrecognized escape sequence: %*s\n",
423 priv->escape_len, priv->escape_buf);
429 /* something went wrong, just revert to normal mode: */
433 /* Put that actual character on the screen (using the CP437 code page). */
434 static int vidconsole_output_glyph(struct udevice *dev, char ch)
436 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
440 * Failure of this function normally indicates an unsupported
441 * colour depth. Check this and return an error to help with
444 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
445 if (ret == -EAGAIN) {
446 vidconsole_newline(dev);
447 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
451 priv->xcur_frac += ret;
453 if (priv->xcur_frac >= priv->xsize_frac)
454 vidconsole_newline(dev);
459 int vidconsole_put_char(struct udevice *dev, char ch)
461 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
465 vidconsole_escape_char(dev, ch);
471 priv->escape_len = 0;
478 priv->xcur_frac = priv->xstart_frac;
481 vidconsole_newline(dev);
482 vidconsole_entry_start(dev);
484 case '\t': /* Tab (8 chars alignment) */
485 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
486 + 1) * priv->tab_width_frac;
488 if (priv->xcur_frac >= priv->xsize_frac)
489 vidconsole_newline(dev);
492 vidconsole_back(dev);
496 ret = vidconsole_output_glyph(dev, ch);
505 int vidconsole_put_string(struct udevice *dev, const char *str)
510 for (s = str; *s; s++) {
511 ret = vidconsole_put_char(dev, *s);
519 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
521 struct udevice *dev = sdev->priv;
524 ret = vidconsole_put_char(dev, ch);
527 console_puts_select_stderr(true, "[vc err: putc]");
530 ret = video_sync(dev->parent, false);
533 console_puts_select_stderr(true, "[vc err: video_sync]");
538 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
540 struct udevice *dev = sdev->priv;
543 ret = vidconsole_put_string(dev, s);
548 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
549 console_puts_select_stderr(true, str);
552 ret = video_sync(dev->parent, false);
555 console_puts_select_stderr(true, "[vc err: video_sync]");
560 /* Set up the number of rows and colours (rotated drivers override this) */
561 static int vidconsole_pre_probe(struct udevice *dev)
563 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
564 struct udevice *vid = dev->parent;
565 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
567 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
572 /* Register the device with stdio */
573 static int vidconsole_post_probe(struct udevice *dev)
575 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
576 struct stdio_dev *sdev = &priv->sdev;
578 if (!priv->tab_width_frac)
579 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
582 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
585 strcpy(sdev->name, "vidconsole");
588 sdev->flags = DEV_FLAGS_OUTPUT;
589 sdev->putc = vidconsole_putc;
590 sdev->puts = vidconsole_puts;
593 return stdio_register(sdev);
596 UCLASS_DRIVER(vidconsole) = {
597 .id = UCLASS_VIDEO_CONSOLE,
598 .name = "vidconsole0",
599 .pre_probe = vidconsole_pre_probe,
600 .post_probe = vidconsole_post_probe,
601 .per_device_auto = sizeof(struct vidconsole_priv),
604 #ifdef CONFIG_VIDEO_COPY
605 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
607 struct udevice *vid = dev_get_parent(dev);
609 return video_sync_copy(vid, from, to);
612 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
615 memmove(dst, src, size);
616 return vidconsole_sync_copy(dev, dst, dst + size);
620 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
622 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
623 struct udevice *vid_dev = dev->parent;
624 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
627 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
628 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
629 vidconsole_set_cursor_pos(dev, x, y);