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> /* Get font data, width and height */
18 /* By default we scroll by a single line */
19 #ifndef CONFIG_CONSOLE_SCROLL_LINES
20 #define CONFIG_CONSOLE_SCROLL_LINES 1
23 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
25 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
29 return ops->putc_xy(dev, x, y, ch);
32 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
35 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
39 return ops->move_rows(dev, rowdst, rowsrc, count);
42 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
44 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
48 return ops->set_row(dev, row, clr);
51 static int vidconsole_entry_start(struct udevice *dev)
53 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
55 if (!ops->entry_start)
57 return ops->entry_start(dev);
60 /* Move backwards one space */
61 static int vidconsole_back(struct udevice *dev)
63 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
64 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
68 ret = ops->backspace(dev);
73 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
74 if (priv->xcur_frac < priv->xstart_frac) {
75 priv->xcur_frac = (priv->cols - 1) *
76 VID_TO_POS(priv->x_charsize);
77 priv->ycur -= priv->y_charsize;
81 video_sync(dev->parent);
86 /* Move to a newline, scrolling the display if necessary */
87 static void vidconsole_newline(struct udevice *dev)
89 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
90 struct udevice *vid_dev = dev->parent;
91 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
92 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
95 priv->xcur_frac = priv->xstart_frac;
96 priv->ycur += priv->y_charsize;
98 /* Check if we need to scroll the terminal */
99 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
100 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
101 for (i = 0; i < rows; i++)
102 vidconsole_set_row(dev, priv->rows - i - 1,
103 vid_priv->colour_bg);
104 priv->ycur -= rows * priv->y_charsize;
108 video_sync(dev->parent);
111 static const struct {
116 { 0x00, 0x00, 0x00 }, /* black */
117 { 0xff, 0x00, 0x00 }, /* red */
118 { 0x00, 0xff, 0x00 }, /* green */
119 { 0xff, 0xff, 0x00 }, /* yellow */
120 { 0x00, 0x00, 0xff }, /* blue */
121 { 0xff, 0x00, 0xff }, /* magenta */
122 { 0x00, 0xff, 0xff }, /* cyan */
123 { 0xff, 0xff, 0xff }, /* white */
126 static void set_color(struct video_priv *priv, unsigned idx, unsigned *c)
128 switch (priv->bpix) {
130 *c = ((colors[idx].r >> 3) << 11) |
131 ((colors[idx].g >> 2) << 5) |
132 ((colors[idx].b >> 3) << 0);
135 *c = (colors[idx].r << 16) |
136 (colors[idx].g << 8) |
137 (colors[idx].b << 0);
140 /* unsupported, leave current color in place */
145 static char *parsenum(char *s, int *num)
148 *num = simple_strtol(s, &end, 10);
153 * Process a character while accumulating an escape string. Chars are
154 * accumulated into escape_buf until the end of escape sequence is
155 * found, at which point the sequence is parsed and processed.
157 static void vidconsole_escape_char(struct udevice *dev, char ch)
159 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
161 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
164 /* Sanity checking for bogus ESC sequences: */
165 if (priv->escape_len >= sizeof(priv->escape_buf))
167 if (priv->escape_len == 0 && ch != '[')
170 priv->escape_buf[priv->escape_len++] = ch;
173 * Escape sequences are terminated by a letter, so keep
174 * accumulating until we get one:
180 * clear escape mode first, otherwise things will get highly
181 * surprising if you hit any debug prints that come back to
190 char *s = priv->escape_buf;
193 * Set cursor position: [%d;%df or [%d;%dH
196 s = parsenum(s, &row);
198 s = parsenum(s, &col);
200 priv->ycur = row * priv->y_charsize;
201 priv->xcur_frac = priv->xstart_frac +
202 VID_TO_POS(col * priv->x_charsize);
210 * Clear part/all screen:
211 * [J or [0J - clear screen from cursor down
212 * [1J - clear screen from cursor up
213 * [2J - clear entire screen
215 * TODO we really only handle entire-screen case, others
216 * probably require some additions to video-uclass (and
217 * are not really needed yet by efi_console)
219 parsenum(priv->escape_buf + 1, &mode);
222 video_clear(dev->parent);
223 video_sync(dev->parent);
225 priv->xcur_frac = priv->xstart_frac;
227 debug("unsupported clear mode: %d\n", mode);
232 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
233 char *s = priv->escape_buf;
234 char *end = &priv->escape_buf[priv->escape_len];
237 * Set graphics mode: [%d;...;%dm
239 * Currently only supports the color attributes:
268 s = parsenum(s, &val);
274 set_color(vid_priv, val - 30,
275 (unsigned *)&vid_priv->colour_fg);
279 set_color(vid_priv, val - 40,
280 (unsigned *)&vid_priv->colour_bg);
283 /* unknown/unsupported */
291 debug("unrecognized escape sequence: %*s\n",
292 priv->escape_len, priv->escape_buf);
298 /* something went wrong, just revert to normal mode: */
302 int vidconsole_put_char(struct udevice *dev, char ch)
304 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
308 vidconsole_escape_char(dev, ch);
314 priv->escape_len = 0;
321 priv->xcur_frac = priv->xstart_frac;
324 vidconsole_newline(dev);
325 vidconsole_entry_start(dev);
327 case '\t': /* Tab (8 chars alignment) */
328 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
329 + 1) * priv->tab_width_frac;
331 if (priv->xcur_frac >= priv->xsize_frac)
332 vidconsole_newline(dev);
335 vidconsole_back(dev);
340 * Failure of this function normally indicates an unsupported
341 * colour depth. Check this and return an error to help with
344 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
345 if (ret == -EAGAIN) {
346 vidconsole_newline(dev);
347 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
352 priv->xcur_frac += ret;
354 if (priv->xcur_frac >= priv->xsize_frac)
355 vidconsole_newline(dev);
362 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
364 struct udevice *dev = sdev->priv;
366 vidconsole_put_char(dev, ch);
367 video_sync(dev->parent);
370 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
372 struct udevice *dev = sdev->priv;
375 vidconsole_put_char(dev, *s++);
376 video_sync(dev->parent);
379 /* Set up the number of rows and colours (rotated drivers override this) */
380 static int vidconsole_pre_probe(struct udevice *dev)
382 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
383 struct udevice *vid = dev->parent;
384 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
386 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
391 /* Register the device with stdio */
392 static int vidconsole_post_probe(struct udevice *dev)
394 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
395 struct stdio_dev *sdev = &priv->sdev;
397 if (!priv->tab_width_frac)
398 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
401 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
404 strcpy(sdev->name, "vidconsole");
407 sdev->flags = DEV_FLAGS_OUTPUT;
408 sdev->putc = vidconsole_putc;
409 sdev->puts = vidconsole_puts;
412 return stdio_register(sdev);
415 UCLASS_DRIVER(vidconsole) = {
416 .id = UCLASS_VIDEO_CONSOLE,
417 .name = "vidconsole0",
418 .pre_probe = vidconsole_pre_probe,
419 .post_probe = vidconsole_post_probe,
420 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
423 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
425 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
426 struct udevice *vid_dev = dev->parent;
427 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
429 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
430 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
433 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
436 unsigned int col, row;
440 return CMD_RET_USAGE;
442 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
443 return CMD_RET_FAILURE;
444 col = simple_strtoul(argv[1], NULL, 10);
445 row = simple_strtoul(argv[2], NULL, 10);
446 vidconsole_position_cursor(dev, col, row);
451 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
458 return CMD_RET_USAGE;
460 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
461 return CMD_RET_FAILURE;
462 for (s = argv[1]; *s; s++)
463 vidconsole_put_char(dev, *s);
465 video_sync(dev->parent);
471 setcurs, 3, 1, do_video_setcursor,
472 "set cursor position within screen",
473 " <col> <row> in character"
477 lcdputs, 2, 1, do_video_puts,
478 "print string on video framebuffer",