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);
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);
119 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
120 { 0x00, 0x00, 0x00 }, /* black */
121 { 0xc0, 0x00, 0x00 }, /* red */
122 { 0x00, 0xc0, 0x00 }, /* green */
123 { 0xc0, 0x60, 0x00 }, /* brown */
124 { 0x00, 0x00, 0xc0 }, /* blue */
125 { 0xc0, 0x00, 0xc0 }, /* magenta */
126 { 0x00, 0xc0, 0xc0 }, /* cyan */
127 { 0xc0, 0xc0, 0xc0 }, /* light gray */
128 { 0x80, 0x80, 0x80 }, /* gray */
129 { 0xff, 0x00, 0x00 }, /* bright red */
130 { 0x00, 0xff, 0x00 }, /* bright green */
131 { 0xff, 0xff, 0x00 }, /* yellow */
132 { 0x00, 0x00, 0xff }, /* bright blue */
133 { 0xff, 0x00, 0xff }, /* bright magenta */
134 { 0x00, 0xff, 0xff }, /* bright cyan */
135 { 0xff, 0xff, 0xff }, /* white */
138 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
140 switch (priv->bpix) {
142 return ((colors[idx].r >> 3) << 11) |
143 ((colors[idx].g >> 2) << 5) |
144 ((colors[idx].b >> 3) << 0);
146 return (colors[idx].r << 16) |
147 (colors[idx].g << 8) |
148 (colors[idx].b << 0);
151 * For unknown bit arrangements just support
155 return 0xffffff; /* white */
157 return 0x000000; /* black */
161 static char *parsenum(char *s, int *num)
164 *num = simple_strtol(s, &end, 10);
169 * Process a character while accumulating an escape string. Chars are
170 * accumulated into escape_buf until the end of escape sequence is
171 * found, at which point the sequence is parsed and processed.
173 static void vidconsole_escape_char(struct udevice *dev, char ch)
175 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
177 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
180 /* Sanity checking for bogus ESC sequences: */
181 if (priv->escape_len >= sizeof(priv->escape_buf))
183 if (priv->escape_len == 0 && ch != '[')
186 priv->escape_buf[priv->escape_len++] = ch;
189 * Escape sequences are terminated by a letter, so keep
190 * accumulating until we get one:
196 * clear escape mode first, otherwise things will get highly
197 * surprising if you hit any debug prints that come back to
206 char *s = priv->escape_buf;
209 * Set cursor position: [%d;%df or [%d;%dH
212 s = parsenum(s, &row);
214 s = parsenum(s, &col);
216 priv->ycur = row * priv->y_charsize;
217 priv->xcur_frac = priv->xstart_frac +
218 VID_TO_POS(col * priv->x_charsize);
226 * Clear part/all screen:
227 * [J or [0J - clear screen from cursor down
228 * [1J - clear screen from cursor up
229 * [2J - clear entire screen
231 * TODO we really only handle entire-screen case, others
232 * probably require some additions to video-uclass (and
233 * are not really needed yet by efi_console)
235 parsenum(priv->escape_buf + 1, &mode);
238 video_clear(dev->parent);
239 video_sync(dev->parent);
241 priv->xcur_frac = priv->xstart_frac;
243 debug("unsupported clear mode: %d\n", mode);
248 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
249 char *s = priv->escape_buf;
250 char *end = &priv->escape_buf[priv->escape_len];
253 * Set graphics mode: [%d;...;%dm
255 * Currently only supports the color attributes:
284 s = parsenum(s, &val);
289 /* all attributes off */
290 video_set_default_colors(vid_priv);
294 vid_priv->fg_col_idx |= 8;
295 vid_priv->colour_fg = vid_console_color(
296 vid_priv, vid_priv->fg_col_idx);
299 /* foreground color */
300 vid_priv->fg_col_idx &= ~7;
301 vid_priv->fg_col_idx |= val - 30;
302 vid_priv->colour_fg = vid_console_color(
303 vid_priv, vid_priv->fg_col_idx);
306 /* background color */
307 vid_priv->colour_bg = vid_console_color(
311 /* ignore unsupported SGR parameter */
319 debug("unrecognized escape sequence: %*s\n",
320 priv->escape_len, priv->escape_buf);
326 /* something went wrong, just revert to normal mode: */
330 int vidconsole_put_char(struct udevice *dev, char ch)
332 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
336 vidconsole_escape_char(dev, ch);
342 priv->escape_len = 0;
349 priv->xcur_frac = priv->xstart_frac;
352 vidconsole_newline(dev);
353 vidconsole_entry_start(dev);
355 case '\t': /* Tab (8 chars alignment) */
356 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
357 + 1) * priv->tab_width_frac;
359 if (priv->xcur_frac >= priv->xsize_frac)
360 vidconsole_newline(dev);
363 vidconsole_back(dev);
368 * Failure of this function normally indicates an unsupported
369 * colour depth. Check this and return an error to help with
372 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
373 if (ret == -EAGAIN) {
374 vidconsole_newline(dev);
375 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
380 priv->xcur_frac += ret;
382 if (priv->xcur_frac >= priv->xsize_frac)
383 vidconsole_newline(dev);
390 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
392 struct udevice *dev = sdev->priv;
394 vidconsole_put_char(dev, ch);
395 video_sync(dev->parent);
398 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
400 struct udevice *dev = sdev->priv;
403 vidconsole_put_char(dev, *s++);
404 video_sync(dev->parent);
407 /* Set up the number of rows and colours (rotated drivers override this) */
408 static int vidconsole_pre_probe(struct udevice *dev)
410 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
411 struct udevice *vid = dev->parent;
412 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
414 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
419 /* Register the device with stdio */
420 static int vidconsole_post_probe(struct udevice *dev)
422 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
423 struct stdio_dev *sdev = &priv->sdev;
425 if (!priv->tab_width_frac)
426 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
429 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
432 strcpy(sdev->name, "vidconsole");
435 sdev->flags = DEV_FLAGS_OUTPUT;
436 sdev->putc = vidconsole_putc;
437 sdev->puts = vidconsole_puts;
440 return stdio_register(sdev);
443 UCLASS_DRIVER(vidconsole) = {
444 .id = UCLASS_VIDEO_CONSOLE,
445 .name = "vidconsole0",
446 .pre_probe = vidconsole_pre_probe,
447 .post_probe = vidconsole_post_probe,
448 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
451 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
453 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
454 struct udevice *vid_dev = dev->parent;
455 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
457 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
458 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
461 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
464 unsigned int col, row;
468 return CMD_RET_USAGE;
470 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
471 return CMD_RET_FAILURE;
472 col = simple_strtoul(argv[1], NULL, 10);
473 row = simple_strtoul(argv[2], NULL, 10);
474 vidconsole_position_cursor(dev, col, row);
479 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
486 return CMD_RET_USAGE;
488 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
489 return CMD_RET_FAILURE;
490 for (s = argv[1]; *s; s++)
491 vidconsole_put_char(dev, *s);
493 video_sync(dev->parent);
499 setcurs, 3, 1, do_video_setcursor,
500 "set cursor position within screen",
501 " <col> <row> in character"
505 lcdputs, 2, 1, do_video_puts,
506 "print string on video framebuffer",