blob: dd6786ffae4e9d5032f0f3c12f0c4db7af548ac2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass83510762016-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 Glass83510762016-01-18 19:52:17 -07008 */
9
10#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060011#include <command.h>
Rob Clarka085aa12017-09-13 18:12:21 -040012#include <linux/ctype.h>
Simon Glass83510762016-01-18 19:52:17 -070013#include <dm.h>
14#include <video.h>
15#include <video_console.h>
Heinrich Schuchardt5fba5322018-03-02 20:50:17 +010016#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass83510762016-01-18 19:52:17 -070017
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +010018/*
19 * Structure to describe a console color
20 */
21struct vid_rgb {
22 u32 r;
23 u32 g;
24 u32 b;
25};
26
Simon Glass83510762016-01-18 19:52:17 -070027/* By default we scroll by a single line */
28#ifndef CONFIG_CONSOLE_SCROLL_LINES
29#define CONFIG_CONSOLE_SCROLL_LINES 1
30#endif
31
32int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
33{
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35
36 if (!ops->putc_xy)
37 return -ENOSYS;
38 return ops->putc_xy(dev, x, y, ch);
39}
40
41int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
42 uint count)
43{
44 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
45
46 if (!ops->move_rows)
47 return -ENOSYS;
48 return ops->move_rows(dev, rowdst, rowsrc, count);
49}
50
51int vidconsole_set_row(struct udevice *dev, uint row, int clr)
52{
53 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
54
55 if (!ops->set_row)
56 return -ENOSYS;
57 return ops->set_row(dev, row, clr);
58}
59
Simon Glass58c733a2016-01-14 18:10:40 -070060static int vidconsole_entry_start(struct udevice *dev)
61{
62 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
63
64 if (!ops->entry_start)
65 return -ENOSYS;
66 return ops->entry_start(dev);
67}
68
Simon Glass83510762016-01-18 19:52:17 -070069/* Move backwards one space */
Simon Glass7b9f7e42016-01-14 18:10:41 -070070static int vidconsole_back(struct udevice *dev)
Simon Glass83510762016-01-18 19:52:17 -070071{
72 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7b9f7e42016-01-14 18:10:41 -070073 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
74 int ret;
75
76 if (ops->backspace) {
77 ret = ops->backspace(dev);
78 if (ret != -ENOSYS)
79 return ret;
80 }
Simon Glass83510762016-01-18 19:52:17 -070081
Simon Glassf2661782016-01-14 18:10:37 -070082 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070083 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-01-14 18:10:37 -070084 priv->xcur_frac = (priv->cols - 1) *
85 VID_TO_POS(priv->x_charsize);
86 priv->ycur -= priv->y_charsize;
87 if (priv->ycur < 0)
88 priv->ycur = 0;
Simon Glass83510762016-01-18 19:52:17 -070089 }
Simon Glass55d39912018-10-01 11:55:14 -060090 video_sync(dev->parent, false);
Simon Glass7b9f7e42016-01-14 18:10:41 -070091
92 return 0;
Simon Glass83510762016-01-18 19:52:17 -070093}
94
95/* Move to a newline, scrolling the display if necessary */
96static void vidconsole_newline(struct udevice *dev)
97{
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;
102 int i;
103
Simon Glassc5b77d02016-01-14 18:10:39 -0700104 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -0700105 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700106
107 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -0700108 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-01-18 19:52:17 -0700109 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);
Simon Glassf2661782016-01-14 18:10:37 -0700113 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700114 }
Simon Glass58c733a2016-01-14 18:10:40 -0700115 priv->last_ch = 0;
116
Simon Glass55d39912018-10-01 11:55:14 -0600117 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700118}
119
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100120static const struct vid_rgb colors[VID_COLOR_COUNT] = {
Rob Clark703d8852017-09-13 18:12:22 -0400121 { 0x00, 0x00, 0x00 }, /* black */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100122 { 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 */
Rob Clark703d8852017-09-13 18:12:22 -0400132 { 0xff, 0xff, 0x00 }, /* yellow */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100133 { 0x00, 0x00, 0xff }, /* bright blue */
134 { 0xff, 0x00, 0xff }, /* bright magenta */
135 { 0x00, 0xff, 0xff }, /* bright cyan */
Rob Clark703d8852017-09-13 18:12:22 -0400136 { 0xff, 0xff, 0xff }, /* white */
137};
138
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100139u32 vid_console_color(struct video_priv *priv, unsigned int idx)
Rob Clark703d8852017-09-13 18:12:22 -0400140{
141 switch (priv->bpix) {
142 case VIDEO_BPP16:
Simon Glass775d3322019-12-20 18:10:36 -0700143 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
144 return ((colors[idx].r >> 3) << 11) |
145 ((colors[idx].g >> 2) << 5) |
146 ((colors[idx].b >> 3) << 0);
147 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100148 break;
Rob Clark703d8852017-09-13 18:12:22 -0400149 case VIDEO_BPP32:
Simon Glass775d3322019-12-20 18:10:36 -0700150 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
151 return (colors[idx].r << 16) |
152 (colors[idx].g << 8) |
153 (colors[idx].b << 0);
154 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100155 break;
Rob Clark703d8852017-09-13 18:12:22 -0400156 default:
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100157 break;
Rob Clark703d8852017-09-13 18:12:22 -0400158 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100159
160 /*
161 * For unknown bit arrangements just support
162 * black and white.
163 */
164 if (idx)
165 return 0xffffff; /* white */
166
167 return 0x000000; /* black */
Rob Clark703d8852017-09-13 18:12:22 -0400168}
169
Rob Clarka085aa12017-09-13 18:12:21 -0400170static char *parsenum(char *s, int *num)
171{
172 char *end;
173 *num = simple_strtol(s, &end, 10);
174 return end;
175}
176
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200177/**
178 * set_cursor_position() - set cursor position
179 *
180 * @priv: private data of the video console
181 * @row: new row
182 * @col: new column
183 */
184static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
185{
186 /*
187 * Ensure we stay in the bounds of the screen.
188 */
189 if (row >= priv->rows)
190 row = priv->rows - 1;
191 if (col >= priv->cols)
192 col = priv->cols - 1;
193
194 priv->ycur = row * priv->y_charsize;
195 priv->xcur_frac = priv->xstart_frac +
196 VID_TO_POS(col * priv->x_charsize);
197}
198
199/**
200 * get_cursor_position() - get cursor position
201 *
202 * @priv: private data of the video console
203 * @row: row
204 * @col: column
205 */
206static void get_cursor_position(struct vidconsole_priv *priv,
207 int *row, int *col)
208{
209 *row = priv->ycur / priv->y_charsize;
210 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
211 priv->x_charsize;
212}
213
Rob Clarka085aa12017-09-13 18:12:21 -0400214/*
215 * Process a character while accumulating an escape string. Chars are
216 * accumulated into escape_buf until the end of escape sequence is
217 * found, at which point the sequence is parsed and processed.
218 */
219static void vidconsole_escape_char(struct udevice *dev, char ch)
220{
221 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
222
223 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
224 goto error;
225
226 /* Sanity checking for bogus ESC sequences: */
227 if (priv->escape_len >= sizeof(priv->escape_buf))
228 goto error;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200229 if (priv->escape_len == 0) {
230 switch (ch) {
231 case '7':
232 /* Save cursor position */
233 get_cursor_position(priv, &priv->row_saved,
234 &priv->col_saved);
235 priv->escape = 0;
236
237 return;
238 case '8': {
239 /* Restore cursor position */
240 int row = priv->row_saved;
241 int col = priv->col_saved;
242
243 set_cursor_position(priv, row, col);
244 priv->escape = 0;
245 return;
246 }
247 case '[':
248 break;
249 default:
250 goto error;
251 }
252 }
Rob Clarka085aa12017-09-13 18:12:21 -0400253
254 priv->escape_buf[priv->escape_len++] = ch;
255
256 /*
257 * Escape sequences are terminated by a letter, so keep
258 * accumulating until we get one:
259 */
260 if (!isalpha(ch))
261 return;
262
263 /*
264 * clear escape mode first, otherwise things will get highly
265 * surprising if you hit any debug prints that come back to
266 * this console.
267 */
268 priv->escape = 0;
269
270 switch (ch) {
Andre Przywara29c158b2019-03-23 01:29:57 +0000271 case 'A':
272 case 'B':
273 case 'C':
274 case 'D':
275 case 'E':
276 case 'F': {
277 int row, col, num;
278 char *s = priv->escape_buf;
279
280 /*
281 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
282 * Cursor left/right: [%dD, [%dC
283 */
284 s++; /* [ */
285 s = parsenum(s, &num);
286 if (num == 0) /* No digit in sequence ... */
287 num = 1; /* ... means "move by 1". */
288
289 get_cursor_position(priv, &row, &col);
290 if (ch == 'A' || ch == 'F')
291 row -= num;
292 if (ch == 'C')
293 col += num;
294 if (ch == 'D')
295 col -= num;
296 if (ch == 'B' || ch == 'E')
297 row += num;
298 if (ch == 'E' || ch == 'F')
299 col = 0;
300 if (col < 0)
301 col = 0;
302 if (row < 0)
303 row = 0;
304 /* Right and bottom overflows are handled in the callee. */
305 set_cursor_position(priv, row, col);
306 break;
307 }
Rob Clarka085aa12017-09-13 18:12:21 -0400308 case 'H':
309 case 'f': {
310 int row, col;
311 char *s = priv->escape_buf;
312
313 /*
314 * Set cursor position: [%d;%df or [%d;%dH
315 */
316 s++; /* [ */
317 s = parsenum(s, &row);
318 s++; /* ; */
319 s = parsenum(s, &col);
320
Heinrich Schuchardt118f0202018-11-10 19:55:48 +0100321 /*
322 * Video origin is [0, 0], terminal origin is [1, 1].
323 */
324 if (row)
325 --row;
326 if (col)
327 --col;
328
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200329 set_cursor_position(priv, row, col);
Rob Clarka085aa12017-09-13 18:12:21 -0400330
331 break;
332 }
333 case 'J': {
334 int mode;
335
336 /*
337 * Clear part/all screen:
338 * [J or [0J - clear screen from cursor down
339 * [1J - clear screen from cursor up
340 * [2J - clear entire screen
341 *
342 * TODO we really only handle entire-screen case, others
343 * probably require some additions to video-uclass (and
344 * are not really needed yet by efi_console)
345 */
346 parsenum(priv->escape_buf + 1, &mode);
347
348 if (mode == 2) {
349 video_clear(dev->parent);
Simon Glass55d39912018-10-01 11:55:14 -0600350 video_sync(dev->parent, false);
Rob Clarka085aa12017-09-13 18:12:21 -0400351 priv->ycur = 0;
352 priv->xcur_frac = priv->xstart_frac;
353 } else {
354 debug("unsupported clear mode: %d\n", mode);
355 }
356 break;
357 }
Andre Przywara44222942019-03-23 01:29:58 +0000358 case 'K': {
359 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
360 int mode;
361
362 /*
363 * Clear (parts of) current line
364 * [0K - clear line to end
365 * [2K - clear entire line
366 */
367 parsenum(priv->escape_buf + 1, &mode);
368
369 if (mode == 2) {
370 int row, col;
371
372 get_cursor_position(priv, &row, &col);
373 vidconsole_set_row(dev, row, vid_priv->colour_bg);
374 }
375 break;
376 }
Rob Clark703d8852017-09-13 18:12:22 -0400377 case 'm': {
378 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
379 char *s = priv->escape_buf;
380 char *end = &priv->escape_buf[priv->escape_len];
381
382 /*
383 * Set graphics mode: [%d;...;%dm
384 *
385 * Currently only supports the color attributes:
386 *
387 * Foreground Colors:
388 *
389 * 30 Black
390 * 31 Red
391 * 32 Green
392 * 33 Yellow
393 * 34 Blue
394 * 35 Magenta
395 * 36 Cyan
396 * 37 White
397 *
398 * Background Colors:
399 *
400 * 40 Black
401 * 41 Red
402 * 42 Green
403 * 43 Yellow
404 * 44 Blue
405 * 45 Magenta
406 * 46 Cyan
407 * 47 White
408 */
409
410 s++; /* [ */
411 while (s < end) {
412 int val;
413
414 s = parsenum(s, &val);
415 s++;
416
417 switch (val) {
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100418 case 0:
419 /* all attributes off */
Simon Glassb9f210a2018-11-06 15:21:36 -0700420 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100421 break;
422 case 1:
423 /* bold */
424 vid_priv->fg_col_idx |= 8;
425 vid_priv->colour_fg = vid_console_color(
426 vid_priv, vid_priv->fg_col_idx);
427 break;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000428 case 7:
429 /* reverse video */
430 vid_priv->colour_fg = vid_console_color(
431 vid_priv, vid_priv->bg_col_idx);
432 vid_priv->colour_bg = vid_console_color(
433 vid_priv, vid_priv->fg_col_idx);
434 break;
Rob Clark703d8852017-09-13 18:12:22 -0400435 case 30 ... 37:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100436 /* foreground color */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100437 vid_priv->fg_col_idx &= ~7;
438 vid_priv->fg_col_idx |= val - 30;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100439 vid_priv->colour_fg = vid_console_color(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100440 vid_priv, vid_priv->fg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400441 break;
442 case 40 ... 47:
Andre Przywaraeabb0722019-03-23 01:29:56 +0000443 /* background color, also mask the bold bit */
444 vid_priv->bg_col_idx &= ~0xf;
445 vid_priv->bg_col_idx |= val - 40;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100446 vid_priv->colour_bg = vid_console_color(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000447 vid_priv, vid_priv->bg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400448 break;
449 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100450 /* ignore unsupported SGR parameter */
Rob Clark703d8852017-09-13 18:12:22 -0400451 break;
452 }
453 }
454
455 break;
456 }
Rob Clarka085aa12017-09-13 18:12:21 -0400457 default:
458 debug("unrecognized escape sequence: %*s\n",
459 priv->escape_len, priv->escape_buf);
460 }
461
462 return;
463
464error:
465 /* something went wrong, just revert to normal mode: */
466 priv->escape = 0;
467}
468
Andre Przywara7035ec32019-03-23 01:29:59 +0000469/* Put that actual character on the screen (using the CP437 code page). */
470static int vidconsole_output_glyph(struct udevice *dev, char ch)
471{
472 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
473 int ret;
474
475 /*
476 * Failure of this function normally indicates an unsupported
477 * colour depth. Check this and return an error to help with
478 * diagnosis.
479 */
480 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
481 if (ret == -EAGAIN) {
482 vidconsole_newline(dev);
483 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
484 }
485 if (ret < 0)
486 return ret;
487 priv->xcur_frac += ret;
488 priv->last_ch = ch;
489 if (priv->xcur_frac >= priv->xsize_frac)
490 vidconsole_newline(dev);
491
492 return 0;
493}
494
Simon Glass83510762016-01-18 19:52:17 -0700495int vidconsole_put_char(struct udevice *dev, char ch)
496{
497 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
498 int ret;
499
Rob Clarka085aa12017-09-13 18:12:21 -0400500 if (priv->escape) {
501 vidconsole_escape_char(dev, ch);
502 return 0;
503 }
504
Simon Glass83510762016-01-18 19:52:17 -0700505 switch (ch) {
Rob Clarka085aa12017-09-13 18:12:21 -0400506 case '\x1b':
507 priv->escape_len = 0;
508 priv->escape = 1;
509 break;
Simon Glass5508f102016-01-14 18:10:38 -0700510 case '\a':
511 /* beep */
512 break;
Simon Glass83510762016-01-18 19:52:17 -0700513 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -0700514 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -0700515 break;
516 case '\n':
517 vidconsole_newline(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700518 vidconsole_entry_start(dev);
Simon Glass83510762016-01-18 19:52:17 -0700519 break;
520 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700521 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
522 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700523
Simon Glassf2661782016-01-14 18:10:37 -0700524 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700525 vidconsole_newline(dev);
526 break;
527 case '\b':
528 vidconsole_back(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700529 priv->last_ch = 0;
Simon Glass83510762016-01-18 19:52:17 -0700530 break;
531 default:
Andre Przywara7035ec32019-03-23 01:29:59 +0000532 ret = vidconsole_output_glyph(dev, ch);
Simon Glassf2661782016-01-14 18:10:37 -0700533 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700534 return ret;
Simon Glass83510762016-01-18 19:52:17 -0700535 break;
536 }
537
538 return 0;
539}
540
Marek Vasute63168a2019-05-17 20:22:31 +0200541int vidconsole_put_string(struct udevice *dev, const char *str)
542{
543 const char *s;
544 int ret;
545
546 for (s = str; *s; s++) {
547 ret = vidconsole_put_char(dev, *s);
548 if (ret)
549 return ret;
550 }
551
552 return 0;
553}
554
Simon Glass83510762016-01-18 19:52:17 -0700555static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
556{
557 struct udevice *dev = sdev->priv;
558
559 vidconsole_put_char(dev, ch);
Simon Glass55d39912018-10-01 11:55:14 -0600560 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700561}
562
563static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
564{
565 struct udevice *dev = sdev->priv;
566
Marek Vasute63168a2019-05-17 20:22:31 +0200567 vidconsole_put_string(dev, s);
Simon Glass55d39912018-10-01 11:55:14 -0600568 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700569}
570
571/* Set up the number of rows and colours (rotated drivers override this) */
572static int vidconsole_pre_probe(struct udevice *dev)
573{
574 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
575 struct udevice *vid = dev->parent;
576 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
577
Simon Glassf2661782016-01-14 18:10:37 -0700578 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700579
580 return 0;
581}
582
583/* Register the device with stdio */
584static int vidconsole_post_probe(struct udevice *dev)
585{
586 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
587 struct stdio_dev *sdev = &priv->sdev;
Simon Glass83510762016-01-18 19:52:17 -0700588
Simon Glassf2661782016-01-14 18:10:37 -0700589 if (!priv->tab_width_frac)
590 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
591
Simon Glassf1a12472016-01-21 19:44:51 -0700592 if (dev->seq) {
593 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
594 dev->seq);
595 } else {
596 strcpy(sdev->name, "vidconsole");
597 }
Simon Glassf2661782016-01-14 18:10:37 -0700598
Simon Glass83510762016-01-18 19:52:17 -0700599 sdev->flags = DEV_FLAGS_OUTPUT;
600 sdev->putc = vidconsole_putc;
601 sdev->puts = vidconsole_puts;
602 sdev->priv = dev;
Simon Glass83510762016-01-18 19:52:17 -0700603
Masahiro Yamada720873b2016-09-06 22:17:33 +0900604 return stdio_register(sdev);
Simon Glass83510762016-01-18 19:52:17 -0700605}
606
607UCLASS_DRIVER(vidconsole) = {
608 .id = UCLASS_VIDEO_CONSOLE,
609 .name = "vidconsole0",
610 .pre_probe = vidconsole_pre_probe,
611 .post_probe = vidconsole_post_probe,
612 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
613};
614
615void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
616{
617 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf2661782016-01-14 18:10:37 -0700618 struct udevice *vid_dev = dev->parent;
619 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass83510762016-01-18 19:52:17 -0700620
Simon Glass9949ee82018-10-01 12:22:47 -0600621 col *= priv->x_charsize;
622 row *= priv->y_charsize;
Simon Glassf2661782016-01-14 18:10:37 -0700623 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
624 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass83510762016-01-18 19:52:17 -0700625}
626
Simon Glass09140112020-05-10 11:40:03 -0600627static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glass83510762016-01-18 19:52:17 -0700628 char *const argv[])
629{
630 unsigned int col, row;
631 struct udevice *dev;
632
633 if (argc != 3)
634 return CMD_RET_USAGE;
635
Simon Glass3f603cb2016-02-11 13:23:26 -0700636 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700637 return CMD_RET_FAILURE;
638 col = simple_strtoul(argv[1], NULL, 10);
639 row = simple_strtoul(argv[2], NULL, 10);
640 vidconsole_position_cursor(dev, col, row);
641
642 return 0;
643}
644
Simon Glass09140112020-05-10 11:40:03 -0600645static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glass83510762016-01-18 19:52:17 -0700646 char *const argv[])
647{
648 struct udevice *dev;
649 const char *s;
650
651 if (argc != 2)
652 return CMD_RET_USAGE;
653
Simon Glass3f603cb2016-02-11 13:23:26 -0700654 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700655 return CMD_RET_FAILURE;
656 for (s = argv[1]; *s; s++)
657 vidconsole_put_char(dev, *s);
658
Simon Glass55d39912018-10-01 11:55:14 -0600659 video_sync(dev->parent, false);
Rob Clark889808d2017-09-13 18:12:20 -0400660
Simon Glass83510762016-01-18 19:52:17 -0700661 return 0;
662}
663
664U_BOOT_CMD(
665 setcurs, 3, 1, do_video_setcursor,
666 "set cursor position within screen",
667 " <col> <row> in character"
668);
669
670U_BOOT_CMD(
671 lcdputs, 2, 1, do_video_puts,
672 "print string on video framebuffer",
673 " <string>"
674);