2 * Copyright (c) 2015 Google, Inc
3 * (C) Copyright 2001-2015
4 * DENX Software Engineering -- wd@denx.de
5 * Compulab Ltd - http://compulab.co.il/
6 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/ctype.h>
15 #include <video_console.h>
16 #include <video_font.h> /* Bitmap font for code page 437 */
19 * Structure to describe a console color
27 /* By default we scroll by a single line */
28 #ifndef CONFIG_CONSOLE_SCROLL_LINES
29 #define CONFIG_CONSOLE_SCROLL_LINES 1
32 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
38 return ops->putc_xy(dev, x, y, ch);
41 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
44 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
48 return ops->move_rows(dev, rowdst, rowsrc, count);
51 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
53 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
57 return ops->set_row(dev, row, clr);
60 static int vidconsole_entry_start(struct udevice *dev)
62 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 if (!ops->entry_start)
66 return ops->entry_start(dev);
69 /* Move backwards one space */
70 static int vidconsole_back(struct udevice *dev)
72 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
73 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
77 ret = ops->backspace(dev);
82 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
83 if (priv->xcur_frac < priv->xstart_frac) {
84 priv->xcur_frac = (priv->cols - 1) *
85 VID_TO_POS(priv->x_charsize);
86 priv->ycur -= priv->y_charsize;
90 video_sync(dev->parent);
95 /* Move to a newline, scrolling the display if necessary */
96 static void vidconsole_newline(struct udevice *dev)
98 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
99 struct udevice *vid_dev = dev->parent;
100 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
101 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
104 priv->xcur_frac = priv->xstart_frac;
105 priv->ycur += priv->y_charsize;
107 /* Check if we need to scroll the terminal */
108 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
109 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
110 for (i = 0; i < rows; i++)
111 vidconsole_set_row(dev, priv->rows - i - 1,
112 vid_priv->colour_bg);
113 priv->ycur -= rows * priv->y_charsize;
117 video_sync(dev->parent);
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 */
139 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
141 switch (priv->bpix) {
143 return ((colors[idx].r >> 3) << 11) |
144 ((colors[idx].g >> 2) << 5) |
145 ((colors[idx].b >> 3) << 0);
147 return (colors[idx].r << 16) |
148 (colors[idx].g << 8) |
149 (colors[idx].b << 0);
152 * For unknown bit arrangements just support
156 return 0xffffff; /* white */
158 return 0x000000; /* black */
162 static char *parsenum(char *s, int *num)
165 *num = simple_strtol(s, &end, 10);
170 * Process a character while accumulating an escape string. Chars are
171 * accumulated into escape_buf until the end of escape sequence is
172 * found, at which point the sequence is parsed and processed.
174 static void vidconsole_escape_char(struct udevice *dev, char ch)
176 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
178 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
181 /* Sanity checking for bogus ESC sequences: */
182 if (priv->escape_len >= sizeof(priv->escape_buf))
184 if (priv->escape_len == 0 && ch != '[')
187 priv->escape_buf[priv->escape_len++] = ch;
190 * Escape sequences are terminated by a letter, so keep
191 * accumulating until we get one:
197 * clear escape mode first, otherwise things will get highly
198 * surprising if you hit any debug prints that come back to
207 char *s = priv->escape_buf;
210 * Set cursor position: [%d;%df or [%d;%dH
213 s = parsenum(s, &row);
215 s = parsenum(s, &col);
217 priv->ycur = row * priv->y_charsize;
218 priv->xcur_frac = priv->xstart_frac +
219 VID_TO_POS(col * priv->x_charsize);
227 * Clear part/all screen:
228 * [J or [0J - clear screen from cursor down
229 * [1J - clear screen from cursor up
230 * [2J - clear entire screen
232 * TODO we really only handle entire-screen case, others
233 * probably require some additions to video-uclass (and
234 * are not really needed yet by efi_console)
236 parsenum(priv->escape_buf + 1, &mode);
239 video_clear(dev->parent);
240 video_sync(dev->parent);
242 priv->xcur_frac = priv->xstart_frac;
244 debug("unsupported clear mode: %d\n", mode);
249 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
250 char *s = priv->escape_buf;
251 char *end = &priv->escape_buf[priv->escape_len];
254 * Set graphics mode: [%d;...;%dm
256 * Currently only supports the color attributes:
285 s = parsenum(s, &val);
290 /* all attributes off */
291 video_set_default_colors(vid_priv);
295 vid_priv->fg_col_idx |= 8;
296 vid_priv->colour_fg = vid_console_color(
297 vid_priv, vid_priv->fg_col_idx);
300 /* foreground color */
301 vid_priv->fg_col_idx &= ~7;
302 vid_priv->fg_col_idx |= val - 30;
303 vid_priv->colour_fg = vid_console_color(
304 vid_priv, vid_priv->fg_col_idx);
307 /* background color */
308 vid_priv->colour_bg = vid_console_color(
312 /* ignore unsupported SGR parameter */
320 debug("unrecognized escape sequence: %*s\n",
321 priv->escape_len, priv->escape_buf);
327 /* something went wrong, just revert to normal mode: */
331 int vidconsole_put_char(struct udevice *dev, char ch)
333 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
337 vidconsole_escape_char(dev, ch);
343 priv->escape_len = 0;
350 priv->xcur_frac = priv->xstart_frac;
353 vidconsole_newline(dev);
354 vidconsole_entry_start(dev);
356 case '\t': /* Tab (8 chars alignment) */
357 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
358 + 1) * priv->tab_width_frac;
360 if (priv->xcur_frac >= priv->xsize_frac)
361 vidconsole_newline(dev);
364 vidconsole_back(dev);
369 * Failure of this function normally indicates an unsupported
370 * colour depth. Check this and return an error to help with
373 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
374 if (ret == -EAGAIN) {
375 vidconsole_newline(dev);
376 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
381 priv->xcur_frac += ret;
383 if (priv->xcur_frac >= priv->xsize_frac)
384 vidconsole_newline(dev);
391 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
393 struct udevice *dev = sdev->priv;
395 vidconsole_put_char(dev, ch);
396 video_sync(dev->parent);
399 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
401 struct udevice *dev = sdev->priv;
404 vidconsole_put_char(dev, *s++);
405 video_sync(dev->parent);
408 /* Set up the number of rows and colours (rotated drivers override this) */
409 static int vidconsole_pre_probe(struct udevice *dev)
411 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
412 struct udevice *vid = dev->parent;
413 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
415 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
420 /* Register the device with stdio */
421 static int vidconsole_post_probe(struct udevice *dev)
423 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
424 struct stdio_dev *sdev = &priv->sdev;
426 if (!priv->tab_width_frac)
427 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
430 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
433 strcpy(sdev->name, "vidconsole");
436 sdev->flags = DEV_FLAGS_OUTPUT;
437 sdev->putc = vidconsole_putc;
438 sdev->puts = vidconsole_puts;
441 return stdio_register(sdev);
444 UCLASS_DRIVER(vidconsole) = {
445 .id = UCLASS_VIDEO_CONSOLE,
446 .name = "vidconsole0",
447 .pre_probe = vidconsole_pre_probe,
448 .post_probe = vidconsole_post_probe,
449 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
452 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
454 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
455 struct udevice *vid_dev = dev->parent;
456 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
458 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
459 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
462 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
465 unsigned int col, row;
469 return CMD_RET_USAGE;
471 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
472 return CMD_RET_FAILURE;
473 col = simple_strtoul(argv[1], NULL, 10);
474 row = simple_strtoul(argv[2], NULL, 10);
475 vidconsole_position_cursor(dev, col, row);
480 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
487 return CMD_RET_USAGE;
489 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
490 return CMD_RET_FAILURE;
491 for (s = argv[1]; *s; s++)
492 vidconsole_put_char(dev, *s);
494 video_sync(dev->parent);
500 setcurs, 3, 1, do_video_setcursor,
501 "set cursor position within screen",
502 " <col> <row> in character"
506 lcdputs, 2, 1, do_video_puts,
507 "print string on video framebuffer",