blob: a21fde0e1d46e859fb578fdea18ed6024fef5c96 [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +020010#define LOG_CATEGORY UCLASS_VIDEO_CONSOLE
11
Simon Glass83510762016-01-18 19:52:17 -070012#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Simon Glass8b763df2020-07-02 21:12:14 -060014#include <console.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass83510762016-01-18 19:52:17 -070016#include <dm.h>
17#include <video.h>
18#include <video_console.h>
Heinrich Schuchardt5fba5322018-03-02 20:50:17 +010019#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass8b763df2020-07-02 21:12:14 -060020#include <linux/ctype.h>
Simon Glass83510762016-01-18 19:52:17 -070021
Simon Glass83510762016-01-18 19:52:17 -070022int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
23{
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
25
26 if (!ops->putc_xy)
27 return -ENOSYS;
28 return ops->putc_xy(dev, x, y, ch);
29}
30
31int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
32 uint count)
33{
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35
36 if (!ops->move_rows)
37 return -ENOSYS;
38 return ops->move_rows(dev, rowdst, rowsrc, count);
39}
40
41int vidconsole_set_row(struct udevice *dev, uint row, int clr)
42{
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->set_row)
46 return -ENOSYS;
47 return ops->set_row(dev, row, clr);
48}
49
Simon Glass58c733a2016-01-14 18:10:40 -070050static int vidconsole_entry_start(struct udevice *dev)
51{
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->entry_start)
55 return -ENOSYS;
56 return ops->entry_start(dev);
57}
58
Simon Glass83510762016-01-18 19:52:17 -070059/* Move backwards one space */
Simon Glass7b9f7e42016-01-14 18:10:41 -070060static int vidconsole_back(struct udevice *dev)
Simon Glass83510762016-01-18 19:52:17 -070061{
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7b9f7e42016-01-14 18:10:41 -070063 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 int ret;
65
66 if (ops->backspace) {
67 ret = ops->backspace(dev);
68 if (ret != -ENOSYS)
69 return ret;
70 }
Simon Glass83510762016-01-18 19:52:17 -070071
Simon Glassf2661782016-01-14 18:10:37 -070072 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070073 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-01-14 18:10:37 -070074 priv->xcur_frac = (priv->cols - 1) *
75 VID_TO_POS(priv->x_charsize);
76 priv->ycur -= priv->y_charsize;
77 if (priv->ycur < 0)
78 priv->ycur = 0;
Simon Glass83510762016-01-18 19:52:17 -070079 }
Michal Simek9de731f2020-12-14 08:47:52 +010080 return video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -070081}
82
83/* Move to a newline, scrolling the display if necessary */
84static void vidconsole_newline(struct udevice *dev)
85{
86 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
87 struct udevice *vid_dev = dev->parent;
88 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Nikhil M Jain86fbee62023-04-20 17:41:08 +053089 const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
Michal Simek9de731f2020-12-14 08:47:52 +010090 int i, ret;
Simon Glass83510762016-01-18 19:52:17 -070091
Simon Glassc5b77d02016-01-14 18:10:39 -070092 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -070093 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -070094
95 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -070096 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-01-18 19:52:17 -070097 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
98 for (i = 0; i < rows; i++)
99 vidconsole_set_row(dev, priv->rows - i - 1,
100 vid_priv->colour_bg);
Simon Glassf2661782016-01-14 18:10:37 -0700101 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700102 }
Simon Glass58c733a2016-01-14 18:10:40 -0700103 priv->last_ch = 0;
104
Michal Simek9de731f2020-12-14 08:47:52 +0100105 ret = video_sync(dev->parent, false);
106 if (ret) {
107#ifdef DEBUG
108 console_puts_select_stderr(true, "[vc err: video_sync]");
109#endif
110 }
Simon Glass83510762016-01-18 19:52:17 -0700111}
112
Rob Clarka085aa12017-09-13 18:12:21 -0400113static char *parsenum(char *s, int *num)
114{
115 char *end;
116 *num = simple_strtol(s, &end, 10);
117 return end;
118}
119
Simon Glass6b6dc0d2022-10-06 08:36:04 -0600120void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y)
121{
122 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
123
124 priv->xcur_frac = VID_TO_POS(x);
125 priv->xstart_frac = priv->xcur_frac;
126 priv->ycur = y;
127}
128
Tom Rinif6546c72023-03-15 11:58:58 -0400129/**
130 * set_cursor_position() - set cursor position
131 *
132 * @priv: private data of the video console
133 * @row: new row
134 * @col: new column
135 */
136static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200137{
Tom Rinif6546c72023-03-15 11:58:58 -0400138 /*
139 * Ensure we stay in the bounds of the screen.
140 */
141 if (row >= priv->rows)
142 row = priv->rows - 1;
143 if (col >= priv->cols)
144 col = priv->cols - 1;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200145
Tom Rinif6546c72023-03-15 11:58:58 -0400146 priv->ycur = row * priv->y_charsize;
147 priv->xcur_frac = priv->xstart_frac +
148 VID_TO_POS(col * priv->x_charsize);
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200149}
150
151/**
152 * get_cursor_position() - get cursor position
153 *
154 * @priv: private data of the video console
155 * @row: row
156 * @col: column
157 */
158static void get_cursor_position(struct vidconsole_priv *priv,
159 int *row, int *col)
160{
161 *row = priv->ycur / priv->y_charsize;
162 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
163 priv->x_charsize;
164}
165
Rob Clarka085aa12017-09-13 18:12:21 -0400166/*
167 * Process a character while accumulating an escape string. Chars are
168 * accumulated into escape_buf until the end of escape sequence is
169 * found, at which point the sequence is parsed and processed.
170 */
171static void vidconsole_escape_char(struct udevice *dev, char ch)
172{
173 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
174
175 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
176 goto error;
177
178 /* Sanity checking for bogus ESC sequences: */
179 if (priv->escape_len >= sizeof(priv->escape_buf))
180 goto error;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200181 if (priv->escape_len == 0) {
182 switch (ch) {
183 case '7':
184 /* Save cursor position */
185 get_cursor_position(priv, &priv->row_saved,
186 &priv->col_saved);
187 priv->escape = 0;
188
189 return;
190 case '8': {
191 /* Restore cursor position */
192 int row = priv->row_saved;
193 int col = priv->col_saved;
194
Tom Rinif6546c72023-03-15 11:58:58 -0400195 set_cursor_position(priv, row, col);
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200196 priv->escape = 0;
197 return;
198 }
199 case '[':
200 break;
201 default:
202 goto error;
203 }
204 }
Rob Clarka085aa12017-09-13 18:12:21 -0400205
206 priv->escape_buf[priv->escape_len++] = ch;
207
208 /*
209 * Escape sequences are terminated by a letter, so keep
210 * accumulating until we get one:
211 */
212 if (!isalpha(ch))
213 return;
214
215 /*
216 * clear escape mode first, otherwise things will get highly
217 * surprising if you hit any debug prints that come back to
218 * this console.
219 */
220 priv->escape = 0;
221
222 switch (ch) {
Andre Przywara29c158b2019-03-23 01:29:57 +0000223 case 'A':
224 case 'B':
225 case 'C':
226 case 'D':
227 case 'E':
228 case 'F': {
229 int row, col, num;
230 char *s = priv->escape_buf;
231
232 /*
233 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
234 * Cursor left/right: [%dD, [%dC
235 */
236 s++; /* [ */
237 s = parsenum(s, &num);
238 if (num == 0) /* No digit in sequence ... */
239 num = 1; /* ... means "move by 1". */
240
241 get_cursor_position(priv, &row, &col);
242 if (ch == 'A' || ch == 'F')
243 row -= num;
244 if (ch == 'C')
245 col += num;
246 if (ch == 'D')
247 col -= num;
248 if (ch == 'B' || ch == 'E')
249 row += num;
250 if (ch == 'E' || ch == 'F')
251 col = 0;
252 if (col < 0)
253 col = 0;
254 if (row < 0)
255 row = 0;
256 /* Right and bottom overflows are handled in the callee. */
Tom Rinif6546c72023-03-15 11:58:58 -0400257 set_cursor_position(priv, row, col);
Andre Przywara29c158b2019-03-23 01:29:57 +0000258 break;
259 }
Rob Clarka085aa12017-09-13 18:12:21 -0400260 case 'H':
261 case 'f': {
262 int row, col;
263 char *s = priv->escape_buf;
264
265 /*
266 * Set cursor position: [%d;%df or [%d;%dH
267 */
268 s++; /* [ */
269 s = parsenum(s, &row);
270 s++; /* ; */
271 s = parsenum(s, &col);
272
Heinrich Schuchardt118f0202018-11-10 19:55:48 +0100273 /*
274 * Video origin is [0, 0], terminal origin is [1, 1].
275 */
276 if (row)
277 --row;
278 if (col)
279 --col;
280
Tom Rinif6546c72023-03-15 11:58:58 -0400281 set_cursor_position(priv, row, col);
Rob Clarka085aa12017-09-13 18:12:21 -0400282
283 break;
284 }
285 case 'J': {
286 int mode;
287
288 /*
289 * Clear part/all screen:
290 * [J or [0J - clear screen from cursor down
291 * [1J - clear screen from cursor up
292 * [2J - clear entire screen
293 *
294 * TODO we really only handle entire-screen case, others
295 * probably require some additions to video-uclass (and
296 * are not really needed yet by efi_console)
297 */
298 parsenum(priv->escape_buf + 1, &mode);
299
300 if (mode == 2) {
Michal Simek9de731f2020-12-14 08:47:52 +0100301 int ret;
302
Rob Clarka085aa12017-09-13 18:12:21 -0400303 video_clear(dev->parent);
Michal Simek9de731f2020-12-14 08:47:52 +0100304 ret = video_sync(dev->parent, false);
305 if (ret) {
306#ifdef DEBUG
307 console_puts_select_stderr(true, "[vc err: video_sync]");
308#endif
309 }
Rob Clarka085aa12017-09-13 18:12:21 -0400310 priv->ycur = 0;
311 priv->xcur_frac = priv->xstart_frac;
312 } else {
313 debug("unsupported clear mode: %d\n", mode);
314 }
315 break;
316 }
Andre Przywara44222942019-03-23 01:29:58 +0000317 case 'K': {
318 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
319 int mode;
320
321 /*
322 * Clear (parts of) current line
323 * [0K - clear line to end
324 * [2K - clear entire line
325 */
326 parsenum(priv->escape_buf + 1, &mode);
327
328 if (mode == 2) {
329 int row, col;
330
331 get_cursor_position(priv, &row, &col);
332 vidconsole_set_row(dev, row, vid_priv->colour_bg);
333 }
334 break;
335 }
Rob Clark703d8852017-09-13 18:12:22 -0400336 case 'm': {
337 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
338 char *s = priv->escape_buf;
339 char *end = &priv->escape_buf[priv->escape_len];
340
341 /*
342 * Set graphics mode: [%d;...;%dm
343 *
344 * Currently only supports the color attributes:
345 *
346 * Foreground Colors:
347 *
348 * 30 Black
349 * 31 Red
350 * 32 Green
351 * 33 Yellow
352 * 34 Blue
353 * 35 Magenta
354 * 36 Cyan
355 * 37 White
356 *
357 * Background Colors:
358 *
359 * 40 Black
360 * 41 Red
361 * 42 Green
362 * 43 Yellow
363 * 44 Blue
364 * 45 Magenta
365 * 46 Cyan
366 * 47 White
367 */
368
369 s++; /* [ */
370 while (s < end) {
371 int val;
372
373 s = parsenum(s, &val);
374 s++;
375
376 switch (val) {
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100377 case 0:
378 /* all attributes off */
Simon Glassb9f210a2018-11-06 15:21:36 -0700379 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100380 break;
381 case 1:
382 /* bold */
383 vid_priv->fg_col_idx |= 8;
Simon Glassa032e4b2022-10-06 08:36:03 -0600384 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100385 vid_priv, vid_priv->fg_col_idx);
386 break;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000387 case 7:
388 /* reverse video */
Simon Glassa032e4b2022-10-06 08:36:03 -0600389 vid_priv->colour_fg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000390 vid_priv, vid_priv->bg_col_idx);
Simon Glassa032e4b2022-10-06 08:36:03 -0600391 vid_priv->colour_bg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000392 vid_priv, vid_priv->fg_col_idx);
393 break;
Rob Clark703d8852017-09-13 18:12:22 -0400394 case 30 ... 37:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100395 /* foreground color */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100396 vid_priv->fg_col_idx &= ~7;
397 vid_priv->fg_col_idx |= val - 30;
Simon Glassa032e4b2022-10-06 08:36:03 -0600398 vid_priv->colour_fg = video_index_to_colour(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100399 vid_priv, vid_priv->fg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400400 break;
401 case 40 ... 47:
Andre Przywaraeabb0722019-03-23 01:29:56 +0000402 /* background color, also mask the bold bit */
403 vid_priv->bg_col_idx &= ~0xf;
404 vid_priv->bg_col_idx |= val - 40;
Simon Glassa032e4b2022-10-06 08:36:03 -0600405 vid_priv->colour_bg = video_index_to_colour(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000406 vid_priv, vid_priv->bg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400407 break;
408 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100409 /* ignore unsupported SGR parameter */
Rob Clark703d8852017-09-13 18:12:22 -0400410 break;
411 }
412 }
413
414 break;
415 }
Rob Clarka085aa12017-09-13 18:12:21 -0400416 default:
417 debug("unrecognized escape sequence: %*s\n",
418 priv->escape_len, priv->escape_buf);
419 }
420
421 return;
422
423error:
424 /* something went wrong, just revert to normal mode: */
425 priv->escape = 0;
426}
427
Andre Przywara7035ec32019-03-23 01:29:59 +0000428/* Put that actual character on the screen (using the CP437 code page). */
429static int vidconsole_output_glyph(struct udevice *dev, char ch)
430{
431 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
432 int ret;
433
434 /*
435 * Failure of this function normally indicates an unsupported
436 * colour depth. Check this and return an error to help with
437 * diagnosis.
438 */
439 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
440 if (ret == -EAGAIN) {
441 vidconsole_newline(dev);
442 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
443 }
444 if (ret < 0)
445 return ret;
446 priv->xcur_frac += ret;
447 priv->last_ch = ch;
448 if (priv->xcur_frac >= priv->xsize_frac)
449 vidconsole_newline(dev);
450
451 return 0;
452}
453
Simon Glass83510762016-01-18 19:52:17 -0700454int vidconsole_put_char(struct udevice *dev, char ch)
455{
456 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
457 int ret;
458
Rob Clarka085aa12017-09-13 18:12:21 -0400459 if (priv->escape) {
460 vidconsole_escape_char(dev, ch);
461 return 0;
462 }
463
Simon Glass83510762016-01-18 19:52:17 -0700464 switch (ch) {
Rob Clarka085aa12017-09-13 18:12:21 -0400465 case '\x1b':
466 priv->escape_len = 0;
467 priv->escape = 1;
468 break;
Simon Glass5508f102016-01-14 18:10:38 -0700469 case '\a':
470 /* beep */
471 break;
Simon Glass83510762016-01-18 19:52:17 -0700472 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -0700473 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -0700474 break;
475 case '\n':
476 vidconsole_newline(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700477 vidconsole_entry_start(dev);
Simon Glass83510762016-01-18 19:52:17 -0700478 break;
479 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700480 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
481 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700482
Simon Glassf2661782016-01-14 18:10:37 -0700483 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700484 vidconsole_newline(dev);
485 break;
486 case '\b':
487 vidconsole_back(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700488 priv->last_ch = 0;
Simon Glass83510762016-01-18 19:52:17 -0700489 break;
490 default:
Andre Przywara7035ec32019-03-23 01:29:59 +0000491 ret = vidconsole_output_glyph(dev, ch);
Simon Glassf2661782016-01-14 18:10:37 -0700492 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700493 return ret;
Simon Glass83510762016-01-18 19:52:17 -0700494 break;
495 }
496
497 return 0;
498}
499
Marek Vasute63168a2019-05-17 20:22:31 +0200500int vidconsole_put_string(struct udevice *dev, const char *str)
501{
502 const char *s;
503 int ret;
504
505 for (s = str; *s; s++) {
506 ret = vidconsole_put_char(dev, *s);
507 if (ret)
508 return ret;
509 }
510
511 return 0;
512}
513
Simon Glass83510762016-01-18 19:52:17 -0700514static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
515{
516 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600517 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700518
Simon Glass8b763df2020-07-02 21:12:14 -0600519 ret = vidconsole_put_char(dev, ch);
520 if (ret) {
521#ifdef DEBUG
522 console_puts_select_stderr(true, "[vc err: putc]");
523#endif
524 }
Michal Simek9de731f2020-12-14 08:47:52 +0100525 ret = video_sync(dev->parent, false);
526 if (ret) {
527#ifdef DEBUG
528 console_puts_select_stderr(true, "[vc err: video_sync]");
529#endif
530 }
Simon Glass83510762016-01-18 19:52:17 -0700531}
532
533static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
534{
535 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600536 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700537
Simon Glass8b763df2020-07-02 21:12:14 -0600538 ret = vidconsole_put_string(dev, s);
539 if (ret) {
540#ifdef DEBUG
541 char str[30];
542
543 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
544 console_puts_select_stderr(true, str);
545#endif
546 }
Michal Simek9de731f2020-12-14 08:47:52 +0100547 ret = video_sync(dev->parent, false);
548 if (ret) {
549#ifdef DEBUG
550 console_puts_select_stderr(true, "[vc err: video_sync]");
551#endif
552 }
Simon Glass83510762016-01-18 19:52:17 -0700553}
554
Simon Glass0e38bd82023-01-06 08:52:32 -0600555void vidconsole_list_fonts(struct udevice *dev)
556{
557 struct vidfont_info info;
558 int ret, i;
559
560 for (i = 0, ret = 0; !ret; i++) {
561 ret = vidconsole_get_font(dev, i, &info);
562 if (!ret)
563 printf("%s\n", info.name);
564 }
565}
566
567int vidconsole_get_font(struct udevice *dev, int seq,
568 struct vidfont_info *info)
569{
570 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
571
572 if (!ops->get_font)
573 return -ENOSYS;
574
575 return ops->get_font(dev, seq, info);
576}
577
Dzmitry Sankouski4f6e3482023-03-07 13:21:15 +0300578int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep)
579{
580 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
581
582 if (!ops->get_font_size)
583 return -ENOSYS;
584
585 *name = ops->get_font_size(dev, sizep);
586 return 0;
587}
588
Simon Glass0e38bd82023-01-06 08:52:32 -0600589int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
590{
591 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
592
593 if (!ops->select_font)
594 return -ENOSYS;
595
596 return ops->select_font(dev, name, size);
597}
598
Simon Glass83510762016-01-18 19:52:17 -0700599/* Set up the number of rows and colours (rotated drivers override this) */
600static int vidconsole_pre_probe(struct udevice *dev)
601{
602 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
603 struct udevice *vid = dev->parent;
604 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
605
Simon Glassf2661782016-01-14 18:10:37 -0700606 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700607
608 return 0;
609}
610
611/* Register the device with stdio */
612static int vidconsole_post_probe(struct udevice *dev)
613{
614 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
615 struct stdio_dev *sdev = &priv->sdev;
Simon Glass83510762016-01-18 19:52:17 -0700616
Simon Glassf2661782016-01-14 18:10:37 -0700617 if (!priv->tab_width_frac)
618 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
619
Simon Glass8b85dfc2020-12-16 21:20:07 -0700620 if (dev_seq(dev)) {
Simon Glassf1a12472016-01-21 19:44:51 -0700621 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700622 dev_seq(dev));
Simon Glassf1a12472016-01-21 19:44:51 -0700623 } else {
624 strcpy(sdev->name, "vidconsole");
625 }
Simon Glassf2661782016-01-14 18:10:37 -0700626
Simon Glass83510762016-01-18 19:52:17 -0700627 sdev->flags = DEV_FLAGS_OUTPUT;
628 sdev->putc = vidconsole_putc;
629 sdev->puts = vidconsole_puts;
630 sdev->priv = dev;
Simon Glass83510762016-01-18 19:52:17 -0700631
Masahiro Yamada720873b2016-09-06 22:17:33 +0900632 return stdio_register(sdev);
Simon Glass83510762016-01-18 19:52:17 -0700633}
634
635UCLASS_DRIVER(vidconsole) = {
636 .id = UCLASS_VIDEO_CONSOLE,
637 .name = "vidconsole0",
638 .pre_probe = vidconsole_pre_probe,
639 .post_probe = vidconsole_post_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700640 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass83510762016-01-18 19:52:17 -0700641};
642
Simon Glass8c0b5d22020-07-02 21:12:23 -0600643#ifdef CONFIG_VIDEO_COPY
644int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
645{
646 struct udevice *vid = dev_get_parent(dev);
647
648 return video_sync_copy(vid, from, to);
649}
650
651int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
652 int size)
653{
654 memmove(dst, src, size);
655 return vidconsole_sync_copy(dev, dst, dst + size);
656}
657#endif
Simon Glassa76b60f2023-03-10 12:47:21 -0800658
659int vidconsole_clear_and_reset(struct udevice *dev)
660{
661 int ret;
662
663 ret = video_clear(dev_get_parent(dev));
664 if (ret)
665 return ret;
666 vidconsole_position_cursor(dev, 0, 0);
667
668 return 0;
669}
Tom Rinif6546c72023-03-15 11:58:58 -0400670
671void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
672{
673 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
674 struct udevice *vid_dev = dev->parent;
675 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
676 short x, y;
677
678 x = min_t(short, col * priv->x_charsize, vid_priv->xsize - 1);
679 y = min_t(short, row * priv->y_charsize, vid_priv->ysize - 1);
680 vidconsole_set_cursor_pos(dev, x, y);
681}