blob: 8e0fc7f3ecf1cb90875fadda2f0445cc9580b934 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass84c7fb32016-01-18 19:52:17 -07002/*
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
Simon Glass84c7fb32016-01-18 19:52:17 -07008 */
9
10#include <common.h>
Rob Clark06e7a0d2017-09-13 18:12:21 -040011#include <linux/ctype.h>
Simon Glass84c7fb32016-01-18 19:52:17 -070012#include <dm.h>
13#include <video.h>
14#include <video_console.h>
Heinrich Schuchardt2172fa42018-03-02 20:50:17 +010015#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass84c7fb32016-01-18 19:52:17 -070016
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +010017/*
18 * Structure to describe a console color
19 */
20struct vid_rgb {
21 u32 r;
22 u32 g;
23 u32 b;
24};
25
Simon Glass84c7fb32016-01-18 19:52:17 -070026/* By default we scroll by a single line */
27#ifndef CONFIG_CONSOLE_SCROLL_LINES
28#define CONFIG_CONSOLE_SCROLL_LINES 1
29#endif
30
31int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
32{
33 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
34
35 if (!ops->putc_xy)
36 return -ENOSYS;
37 return ops->putc_xy(dev, x, y, ch);
38}
39
40int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
41 uint count)
42{
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->move_rows)
46 return -ENOSYS;
47 return ops->move_rows(dev, rowdst, rowsrc, count);
48}
49
50int vidconsole_set_row(struct udevice *dev, uint row, int clr)
51{
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->set_row)
55 return -ENOSYS;
56 return ops->set_row(dev, row, clr);
57}
58
Simon Glassafee7432016-01-14 18:10:40 -070059static int vidconsole_entry_start(struct udevice *dev)
60{
61 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
62
63 if (!ops->entry_start)
64 return -ENOSYS;
65 return ops->entry_start(dev);
66}
67
Simon Glass84c7fb32016-01-18 19:52:17 -070068/* Move backwards one space */
Simon Glass33bd3b62016-01-14 18:10:41 -070069static int vidconsole_back(struct udevice *dev)
Simon Glass84c7fb32016-01-18 19:52:17 -070070{
71 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass33bd3b62016-01-14 18:10:41 -070072 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
73 int ret;
74
75 if (ops->backspace) {
76 ret = ops->backspace(dev);
77 if (ret != -ENOSYS)
78 return ret;
79 }
Simon Glass84c7fb32016-01-18 19:52:17 -070080
Simon Glass52c10c52016-01-14 18:10:37 -070081 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassa74451d2016-01-14 18:10:39 -070082 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glass52c10c52016-01-14 18:10:37 -070083 priv->xcur_frac = (priv->cols - 1) *
84 VID_TO_POS(priv->x_charsize);
85 priv->ycur -= priv->y_charsize;
86 if (priv->ycur < 0)
87 priv->ycur = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070088 }
Simon Glass0806dcc2018-10-01 11:55:14 -060089 video_sync(dev->parent, false);
Simon Glass33bd3b62016-01-14 18:10:41 -070090
91 return 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070092}
93
94/* Move to a newline, scrolling the display if necessary */
95static void vidconsole_newline(struct udevice *dev)
96{
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;
101 int i;
102
Simon Glassa74451d2016-01-14 18:10:39 -0700103 priv->xcur_frac = priv->xstart_frac;
Simon Glass52c10c52016-01-14 18:10:37 -0700104 priv->ycur += priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -0700105
106 /* Check if we need to scroll the terminal */
Simon Glass52c10c52016-01-14 18:10:37 -0700107 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass84c7fb32016-01-18 19:52:17 -0700108 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);
Simon Glass52c10c52016-01-14 18:10:37 -0700112 priv->ycur -= rows * priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -0700113 }
Simon Glassafee7432016-01-14 18:10:40 -0700114 priv->last_ch = 0;
115
Simon Glass0806dcc2018-10-01 11:55:14 -0600116 video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -0700117}
118
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100119static const struct vid_rgb colors[VID_COLOR_COUNT] = {
Rob Clark50509bb2017-09-13 18:12:22 -0400120 { 0x00, 0x00, 0x00 }, /* black */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100121 { 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 */
Rob Clark50509bb2017-09-13 18:12:22 -0400131 { 0xff, 0xff, 0x00 }, /* yellow */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100132 { 0x00, 0x00, 0xff }, /* bright blue */
133 { 0xff, 0x00, 0xff }, /* bright magenta */
134 { 0x00, 0xff, 0xff }, /* bright cyan */
Rob Clark50509bb2017-09-13 18:12:22 -0400135 { 0xff, 0xff, 0xff }, /* white */
136};
137
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100138u32 vid_console_color(struct video_priv *priv, unsigned int idx)
Rob Clark50509bb2017-09-13 18:12:22 -0400139{
140 switch (priv->bpix) {
141 case VIDEO_BPP16:
Simon Glassa4a43c62019-12-20 18:10:36 -0700142 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
143 return ((colors[idx].r >> 3) << 11) |
144 ((colors[idx].g >> 2) << 5) |
145 ((colors[idx].b >> 3) << 0);
146 }
Anatolij Gustschin0db23e02020-01-06 23:00:38 +0100147 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400148 case VIDEO_BPP32:
Simon Glassa4a43c62019-12-20 18:10:36 -0700149 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
150 return (colors[idx].r << 16) |
151 (colors[idx].g << 8) |
152 (colors[idx].b << 0);
153 }
Anatolij Gustschin0db23e02020-01-06 23:00:38 +0100154 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400155 default:
Anatolij Gustschin0db23e02020-01-06 23:00:38 +0100156 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400157 }
Anatolij Gustschin0db23e02020-01-06 23:00:38 +0100158
159 /*
160 * For unknown bit arrangements just support
161 * black and white.
162 */
163 if (idx)
164 return 0xffffff; /* white */
165
166 return 0x000000; /* black */
Rob Clark50509bb2017-09-13 18:12:22 -0400167}
168
Rob Clark06e7a0d2017-09-13 18:12:21 -0400169static char *parsenum(char *s, int *num)
170{
171 char *end;
172 *num = simple_strtol(s, &end, 10);
173 return end;
174}
175
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200176/**
177 * set_cursor_position() - set cursor position
178 *
179 * @priv: private data of the video console
180 * @row: new row
181 * @col: new column
182 */
183static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
184{
185 /*
186 * Ensure we stay in the bounds of the screen.
187 */
188 if (row >= priv->rows)
189 row = priv->rows - 1;
190 if (col >= priv->cols)
191 col = priv->cols - 1;
192
193 priv->ycur = row * priv->y_charsize;
194 priv->xcur_frac = priv->xstart_frac +
195 VID_TO_POS(col * priv->x_charsize);
196}
197
198/**
199 * get_cursor_position() - get cursor position
200 *
201 * @priv: private data of the video console
202 * @row: row
203 * @col: column
204 */
205static void get_cursor_position(struct vidconsole_priv *priv,
206 int *row, int *col)
207{
208 *row = priv->ycur / priv->y_charsize;
209 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
210 priv->x_charsize;
211}
212
Rob Clark06e7a0d2017-09-13 18:12:21 -0400213/*
214 * Process a character while accumulating an escape string. Chars are
215 * accumulated into escape_buf until the end of escape sequence is
216 * found, at which point the sequence is parsed and processed.
217 */
218static void vidconsole_escape_char(struct udevice *dev, char ch)
219{
220 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
221
222 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
223 goto error;
224
225 /* Sanity checking for bogus ESC sequences: */
226 if (priv->escape_len >= sizeof(priv->escape_buf))
227 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200228 if (priv->escape_len == 0) {
229 switch (ch) {
230 case '7':
231 /* Save cursor position */
232 get_cursor_position(priv, &priv->row_saved,
233 &priv->col_saved);
234 priv->escape = 0;
235
236 return;
237 case '8': {
238 /* Restore cursor position */
239 int row = priv->row_saved;
240 int col = priv->col_saved;
241
242 set_cursor_position(priv, row, col);
243 priv->escape = 0;
244 return;
245 }
246 case '[':
247 break;
248 default:
249 goto error;
250 }
251 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400252
253 priv->escape_buf[priv->escape_len++] = ch;
254
255 /*
256 * Escape sequences are terminated by a letter, so keep
257 * accumulating until we get one:
258 */
259 if (!isalpha(ch))
260 return;
261
262 /*
263 * clear escape mode first, otherwise things will get highly
264 * surprising if you hit any debug prints that come back to
265 * this console.
266 */
267 priv->escape = 0;
268
269 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000270 case 'A':
271 case 'B':
272 case 'C':
273 case 'D':
274 case 'E':
275 case 'F': {
276 int row, col, num;
277 char *s = priv->escape_buf;
278
279 /*
280 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
281 * Cursor left/right: [%dD, [%dC
282 */
283 s++; /* [ */
284 s = parsenum(s, &num);
285 if (num == 0) /* No digit in sequence ... */
286 num = 1; /* ... means "move by 1". */
287
288 get_cursor_position(priv, &row, &col);
289 if (ch == 'A' || ch == 'F')
290 row -= num;
291 if (ch == 'C')
292 col += num;
293 if (ch == 'D')
294 col -= num;
295 if (ch == 'B' || ch == 'E')
296 row += num;
297 if (ch == 'E' || ch == 'F')
298 col = 0;
299 if (col < 0)
300 col = 0;
301 if (row < 0)
302 row = 0;
303 /* Right and bottom overflows are handled in the callee. */
304 set_cursor_position(priv, row, col);
305 break;
306 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400307 case 'H':
308 case 'f': {
309 int row, col;
310 char *s = priv->escape_buf;
311
312 /*
313 * Set cursor position: [%d;%df or [%d;%dH
314 */
315 s++; /* [ */
316 s = parsenum(s, &row);
317 s++; /* ; */
318 s = parsenum(s, &col);
319
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100320 /*
321 * Video origin is [0, 0], terminal origin is [1, 1].
322 */
323 if (row)
324 --row;
325 if (col)
326 --col;
327
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200328 set_cursor_position(priv, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400329
330 break;
331 }
332 case 'J': {
333 int mode;
334
335 /*
336 * Clear part/all screen:
337 * [J or [0J - clear screen from cursor down
338 * [1J - clear screen from cursor up
339 * [2J - clear entire screen
340 *
341 * TODO we really only handle entire-screen case, others
342 * probably require some additions to video-uclass (and
343 * are not really needed yet by efi_console)
344 */
345 parsenum(priv->escape_buf + 1, &mode);
346
347 if (mode == 2) {
348 video_clear(dev->parent);
Simon Glass0806dcc2018-10-01 11:55:14 -0600349 video_sync(dev->parent, false);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400350 priv->ycur = 0;
351 priv->xcur_frac = priv->xstart_frac;
352 } else {
353 debug("unsupported clear mode: %d\n", mode);
354 }
355 break;
356 }
Andre Przywara918622a2019-03-23 01:29:58 +0000357 case 'K': {
358 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
359 int mode;
360
361 /*
362 * Clear (parts of) current line
363 * [0K - clear line to end
364 * [2K - clear entire line
365 */
366 parsenum(priv->escape_buf + 1, &mode);
367
368 if (mode == 2) {
369 int row, col;
370
371 get_cursor_position(priv, &row, &col);
372 vidconsole_set_row(dev, row, vid_priv->colour_bg);
373 }
374 break;
375 }
Rob Clark50509bb2017-09-13 18:12:22 -0400376 case 'm': {
377 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
378 char *s = priv->escape_buf;
379 char *end = &priv->escape_buf[priv->escape_len];
380
381 /*
382 * Set graphics mode: [%d;...;%dm
383 *
384 * Currently only supports the color attributes:
385 *
386 * Foreground Colors:
387 *
388 * 30 Black
389 * 31 Red
390 * 32 Green
391 * 33 Yellow
392 * 34 Blue
393 * 35 Magenta
394 * 36 Cyan
395 * 37 White
396 *
397 * Background Colors:
398 *
399 * 40 Black
400 * 41 Red
401 * 42 Green
402 * 43 Yellow
403 * 44 Blue
404 * 45 Magenta
405 * 46 Cyan
406 * 47 White
407 */
408
409 s++; /* [ */
410 while (s < end) {
411 int val;
412
413 s = parsenum(s, &val);
414 s++;
415
416 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100417 case 0:
418 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700419 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100420 break;
421 case 1:
422 /* bold */
423 vid_priv->fg_col_idx |= 8;
424 vid_priv->colour_fg = vid_console_color(
425 vid_priv, vid_priv->fg_col_idx);
426 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000427 case 7:
428 /* reverse video */
429 vid_priv->colour_fg = vid_console_color(
430 vid_priv, vid_priv->bg_col_idx);
431 vid_priv->colour_bg = vid_console_color(
432 vid_priv, vid_priv->fg_col_idx);
433 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400434 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100435 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100436 vid_priv->fg_col_idx &= ~7;
437 vid_priv->fg_col_idx |= val - 30;
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100438 vid_priv->colour_fg = vid_console_color(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100439 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400440 break;
441 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000442 /* background color, also mask the bold bit */
443 vid_priv->bg_col_idx &= ~0xf;
444 vid_priv->bg_col_idx |= val - 40;
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100445 vid_priv->colour_bg = vid_console_color(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000446 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400447 break;
448 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100449 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400450 break;
451 }
452 }
453
454 break;
455 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400456 default:
457 debug("unrecognized escape sequence: %*s\n",
458 priv->escape_len, priv->escape_buf);
459 }
460
461 return;
462
463error:
464 /* something went wrong, just revert to normal mode: */
465 priv->escape = 0;
466}
467
Andre Przywarade86baf2019-03-23 01:29:59 +0000468/* Put that actual character on the screen (using the CP437 code page). */
469static int vidconsole_output_glyph(struct udevice *dev, char ch)
470{
471 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
472 int ret;
473
474 /*
475 * Failure of this function normally indicates an unsupported
476 * colour depth. Check this and return an error to help with
477 * diagnosis.
478 */
479 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
480 if (ret == -EAGAIN) {
481 vidconsole_newline(dev);
482 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
483 }
484 if (ret < 0)
485 return ret;
486 priv->xcur_frac += ret;
487 priv->last_ch = ch;
488 if (priv->xcur_frac >= priv->xsize_frac)
489 vidconsole_newline(dev);
490
491 return 0;
492}
493
Simon Glass84c7fb32016-01-18 19:52:17 -0700494int vidconsole_put_char(struct udevice *dev, char ch)
495{
496 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
497 int ret;
498
Rob Clark06e7a0d2017-09-13 18:12:21 -0400499 if (priv->escape) {
500 vidconsole_escape_char(dev, ch);
501 return 0;
502 }
503
Simon Glass84c7fb32016-01-18 19:52:17 -0700504 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400505 case '\x1b':
506 priv->escape_len = 0;
507 priv->escape = 1;
508 break;
Simon Glass37b80202016-01-14 18:10:38 -0700509 case '\a':
510 /* beep */
511 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700512 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700513 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700514 break;
515 case '\n':
516 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700517 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700518 break;
519 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700520 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
521 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700522
Simon Glass52c10c52016-01-14 18:10:37 -0700523 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700524 vidconsole_newline(dev);
525 break;
526 case '\b':
527 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700528 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700529 break;
530 default:
Andre Przywarade86baf2019-03-23 01:29:59 +0000531 ret = vidconsole_output_glyph(dev, ch);
Simon Glass52c10c52016-01-14 18:10:37 -0700532 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700533 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700534 break;
535 }
536
537 return 0;
538}
539
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200540int vidconsole_put_string(struct udevice *dev, const char *str)
541{
542 const char *s;
543 int ret;
544
545 for (s = str; *s; s++) {
546 ret = vidconsole_put_char(dev, *s);
547 if (ret)
548 return ret;
549 }
550
551 return 0;
552}
553
Simon Glass84c7fb32016-01-18 19:52:17 -0700554static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
555{
556 struct udevice *dev = sdev->priv;
557
558 vidconsole_put_char(dev, ch);
Simon Glass0806dcc2018-10-01 11:55:14 -0600559 video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -0700560}
561
562static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
563{
564 struct udevice *dev = sdev->priv;
565
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200566 vidconsole_put_string(dev, s);
Simon Glass0806dcc2018-10-01 11:55:14 -0600567 video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -0700568}
569
570/* Set up the number of rows and colours (rotated drivers override this) */
571static int vidconsole_pre_probe(struct udevice *dev)
572{
573 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
574 struct udevice *vid = dev->parent;
575 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
576
Simon Glass52c10c52016-01-14 18:10:37 -0700577 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700578
579 return 0;
580}
581
582/* Register the device with stdio */
583static int vidconsole_post_probe(struct udevice *dev)
584{
585 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
586 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700587
Simon Glass52c10c52016-01-14 18:10:37 -0700588 if (!priv->tab_width_frac)
589 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
590
Simon Glass798ff502016-01-21 19:44:51 -0700591 if (dev->seq) {
592 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
593 dev->seq);
594 } else {
595 strcpy(sdev->name, "vidconsole");
596 }
Simon Glass52c10c52016-01-14 18:10:37 -0700597
Simon Glass84c7fb32016-01-18 19:52:17 -0700598 sdev->flags = DEV_FLAGS_OUTPUT;
599 sdev->putc = vidconsole_putc;
600 sdev->puts = vidconsole_puts;
601 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700602
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900603 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700604}
605
606UCLASS_DRIVER(vidconsole) = {
607 .id = UCLASS_VIDEO_CONSOLE,
608 .name = "vidconsole0",
609 .pre_probe = vidconsole_pre_probe,
610 .post_probe = vidconsole_post_probe,
611 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
612};
613
614void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
615{
616 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass52c10c52016-01-14 18:10:37 -0700617 struct udevice *vid_dev = dev->parent;
618 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700619
Simon Glass59f44212018-10-01 12:22:47 -0600620 col *= priv->x_charsize;
621 row *= priv->y_charsize;
Simon Glass52c10c52016-01-14 18:10:37 -0700622 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
623 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass84c7fb32016-01-18 19:52:17 -0700624}
625
626static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
627 char *const argv[])
628{
629 unsigned int col, row;
630 struct udevice *dev;
631
632 if (argc != 3)
633 return CMD_RET_USAGE;
634
Simon Glassc7298e72016-02-11 13:23:26 -0700635 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass84c7fb32016-01-18 19:52:17 -0700636 return CMD_RET_FAILURE;
637 col = simple_strtoul(argv[1], NULL, 10);
638 row = simple_strtoul(argv[2], NULL, 10);
639 vidconsole_position_cursor(dev, col, row);
640
641 return 0;
642}
643
644static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
645 char *const argv[])
646{
647 struct udevice *dev;
648 const char *s;
649
650 if (argc != 2)
651 return CMD_RET_USAGE;
652
Simon Glassc7298e72016-02-11 13:23:26 -0700653 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass84c7fb32016-01-18 19:52:17 -0700654 return CMD_RET_FAILURE;
655 for (s = argv[1]; *s; s++)
656 vidconsole_put_char(dev, *s);
657
Simon Glass0806dcc2018-10-01 11:55:14 -0600658 video_sync(dev->parent, false);
Rob Clarke9594612017-09-13 18:12:20 -0400659
Simon Glass84c7fb32016-01-18 19:52:17 -0700660 return 0;
661}
662
663U_BOOT_CMD(
664 setcurs, 3, 1, do_video_setcursor,
665 "set cursor position within screen",
666 " <col> <row> in character"
667);
668
669U_BOOT_CMD(
670 lcdputs, 2, 1, do_video_puts,
671 "print string on video framebuffer",
672 " <string>"
673);