blob: c690eceeaa7f7b70205dc1efdb77756ce0f9f178 [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>
Rob Clarka085aa12017-09-13 18:12:21 -040011#include <linux/ctype.h>
Simon Glass83510762016-01-18 19:52:17 -070012#include <dm.h>
13#include <video.h>
14#include <video_console.h>
Heinrich Schuchardt5fba5322018-03-02 20:50:17 +010015#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass83510762016-01-18 19:52:17 -070016
Heinrich Schuchardt5c30fbb2018-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 Glass83510762016-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 Glass58c733a2016-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 Glass83510762016-01-18 19:52:17 -070068/* Move backwards one space */
Simon Glass7b9f7e42016-01-14 18:10:41 -070069static int vidconsole_back(struct udevice *dev)
Simon Glass83510762016-01-18 19:52:17 -070070{
71 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7b9f7e42016-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 Glass83510762016-01-18 19:52:17 -070080
Simon Glassf2661782016-01-14 18:10:37 -070081 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070082 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-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 Glass83510762016-01-18 19:52:17 -070088 }
Simon Glass55d39912018-10-01 11:55:14 -060089 video_sync(dev->parent, false);
Simon Glass7b9f7e42016-01-14 18:10:41 -070090
91 return 0;
Simon Glass83510762016-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 Glassc5b77d02016-01-14 18:10:39 -0700103 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -0700104 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700105
106 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -0700107 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-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 Glassf2661782016-01-14 18:10:37 -0700112 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700113 }
Simon Glass58c733a2016-01-14 18:10:40 -0700114 priv->last_ch = 0;
115
Simon Glass55d39912018-10-01 11:55:14 -0600116 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700117}
118
Anatolij Gustschinca5655d2019-12-04 16:18:39 +0100119#if CONFIG_IS_ENABLED(VIDEO_BPP16) || CONFIG_IS_ENABLED(VIDEO_BPP32)
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};
Anatolij Gustschinca5655d2019-12-04 16:18:39 +0100138#endif
Rob Clark703d8852017-09-13 18:12:22 -0400139
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100140u32 vid_console_color(struct video_priv *priv, unsigned int idx)
Rob Clark703d8852017-09-13 18:12:22 -0400141{
142 switch (priv->bpix) {
Anatolij Gustschinca5655d2019-12-04 16:18:39 +0100143#if CONFIG_IS_ENABLED(VIDEO_BPP16)
Rob Clark703d8852017-09-13 18:12:22 -0400144 case VIDEO_BPP16:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100145 return ((colors[idx].r >> 3) << 11) |
146 ((colors[idx].g >> 2) << 5) |
147 ((colors[idx].b >> 3) << 0);
Anatolij Gustschinca5655d2019-12-04 16:18:39 +0100148#endif
149#if CONFIG_IS_ENABLED(VIDEO_BPP32)
Rob Clark703d8852017-09-13 18:12:22 -0400150 case VIDEO_BPP32:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100151 return (colors[idx].r << 16) |
152 (colors[idx].g << 8) |
153 (colors[idx].b << 0);
Anatolij Gustschinca5655d2019-12-04 16:18:39 +0100154#endif
Rob Clark703d8852017-09-13 18:12:22 -0400155 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100156 /*
157 * For unknown bit arrangements just support
158 * black and white.
159 */
160 if (idx)
161 return 0xffffff; /* white */
162 else
163 return 0x000000; /* black */
Rob Clark703d8852017-09-13 18:12:22 -0400164 }
165}
166
Rob Clarka085aa12017-09-13 18:12:21 -0400167static char *parsenum(char *s, int *num)
168{
169 char *end;
170 *num = simple_strtol(s, &end, 10);
171 return end;
172}
173
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200174/**
175 * set_cursor_position() - set cursor position
176 *
177 * @priv: private data of the video console
178 * @row: new row
179 * @col: new column
180 */
181static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
182{
183 /*
184 * Ensure we stay in the bounds of the screen.
185 */
186 if (row >= priv->rows)
187 row = priv->rows - 1;
188 if (col >= priv->cols)
189 col = priv->cols - 1;
190
191 priv->ycur = row * priv->y_charsize;
192 priv->xcur_frac = priv->xstart_frac +
193 VID_TO_POS(col * priv->x_charsize);
194}
195
196/**
197 * get_cursor_position() - get cursor position
198 *
199 * @priv: private data of the video console
200 * @row: row
201 * @col: column
202 */
203static void get_cursor_position(struct vidconsole_priv *priv,
204 int *row, int *col)
205{
206 *row = priv->ycur / priv->y_charsize;
207 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
208 priv->x_charsize;
209}
210
Rob Clarka085aa12017-09-13 18:12:21 -0400211/*
212 * Process a character while accumulating an escape string. Chars are
213 * accumulated into escape_buf until the end of escape sequence is
214 * found, at which point the sequence is parsed and processed.
215 */
216static void vidconsole_escape_char(struct udevice *dev, char ch)
217{
218 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
219
220 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
221 goto error;
222
223 /* Sanity checking for bogus ESC sequences: */
224 if (priv->escape_len >= sizeof(priv->escape_buf))
225 goto error;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200226 if (priv->escape_len == 0) {
227 switch (ch) {
228 case '7':
229 /* Save cursor position */
230 get_cursor_position(priv, &priv->row_saved,
231 &priv->col_saved);
232 priv->escape = 0;
233
234 return;
235 case '8': {
236 /* Restore cursor position */
237 int row = priv->row_saved;
238 int col = priv->col_saved;
239
240 set_cursor_position(priv, row, col);
241 priv->escape = 0;
242 return;
243 }
244 case '[':
245 break;
246 default:
247 goto error;
248 }
249 }
Rob Clarka085aa12017-09-13 18:12:21 -0400250
251 priv->escape_buf[priv->escape_len++] = ch;
252
253 /*
254 * Escape sequences are terminated by a letter, so keep
255 * accumulating until we get one:
256 */
257 if (!isalpha(ch))
258 return;
259
260 /*
261 * clear escape mode first, otherwise things will get highly
262 * surprising if you hit any debug prints that come back to
263 * this console.
264 */
265 priv->escape = 0;
266
267 switch (ch) {
Andre Przywara29c158b2019-03-23 01:29:57 +0000268 case 'A':
269 case 'B':
270 case 'C':
271 case 'D':
272 case 'E':
273 case 'F': {
274 int row, col, num;
275 char *s = priv->escape_buf;
276
277 /*
278 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
279 * Cursor left/right: [%dD, [%dC
280 */
281 s++; /* [ */
282 s = parsenum(s, &num);
283 if (num == 0) /* No digit in sequence ... */
284 num = 1; /* ... means "move by 1". */
285
286 get_cursor_position(priv, &row, &col);
287 if (ch == 'A' || ch == 'F')
288 row -= num;
289 if (ch == 'C')
290 col += num;
291 if (ch == 'D')
292 col -= num;
293 if (ch == 'B' || ch == 'E')
294 row += num;
295 if (ch == 'E' || ch == 'F')
296 col = 0;
297 if (col < 0)
298 col = 0;
299 if (row < 0)
300 row = 0;
301 /* Right and bottom overflows are handled in the callee. */
302 set_cursor_position(priv, row, col);
303 break;
304 }
Rob Clarka085aa12017-09-13 18:12:21 -0400305 case 'H':
306 case 'f': {
307 int row, col;
308 char *s = priv->escape_buf;
309
310 /*
311 * Set cursor position: [%d;%df or [%d;%dH
312 */
313 s++; /* [ */
314 s = parsenum(s, &row);
315 s++; /* ; */
316 s = parsenum(s, &col);
317
Heinrich Schuchardt118f0202018-11-10 19:55:48 +0100318 /*
319 * Video origin is [0, 0], terminal origin is [1, 1].
320 */
321 if (row)
322 --row;
323 if (col)
324 --col;
325
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200326 set_cursor_position(priv, row, col);
Rob Clarka085aa12017-09-13 18:12:21 -0400327
328 break;
329 }
330 case 'J': {
331 int mode;
332
333 /*
334 * Clear part/all screen:
335 * [J or [0J - clear screen from cursor down
336 * [1J - clear screen from cursor up
337 * [2J - clear entire screen
338 *
339 * TODO we really only handle entire-screen case, others
340 * probably require some additions to video-uclass (and
341 * are not really needed yet by efi_console)
342 */
343 parsenum(priv->escape_buf + 1, &mode);
344
345 if (mode == 2) {
346 video_clear(dev->parent);
Simon Glass55d39912018-10-01 11:55:14 -0600347 video_sync(dev->parent, false);
Rob Clarka085aa12017-09-13 18:12:21 -0400348 priv->ycur = 0;
349 priv->xcur_frac = priv->xstart_frac;
350 } else {
351 debug("unsupported clear mode: %d\n", mode);
352 }
353 break;
354 }
Andre Przywara44222942019-03-23 01:29:58 +0000355 case 'K': {
356 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
357 int mode;
358
359 /*
360 * Clear (parts of) current line
361 * [0K - clear line to end
362 * [2K - clear entire line
363 */
364 parsenum(priv->escape_buf + 1, &mode);
365
366 if (mode == 2) {
367 int row, col;
368
369 get_cursor_position(priv, &row, &col);
370 vidconsole_set_row(dev, row, vid_priv->colour_bg);
371 }
372 break;
373 }
Rob Clark703d8852017-09-13 18:12:22 -0400374 case 'm': {
375 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
376 char *s = priv->escape_buf;
377 char *end = &priv->escape_buf[priv->escape_len];
378
379 /*
380 * Set graphics mode: [%d;...;%dm
381 *
382 * Currently only supports the color attributes:
383 *
384 * Foreground Colors:
385 *
386 * 30 Black
387 * 31 Red
388 * 32 Green
389 * 33 Yellow
390 * 34 Blue
391 * 35 Magenta
392 * 36 Cyan
393 * 37 White
394 *
395 * Background Colors:
396 *
397 * 40 Black
398 * 41 Red
399 * 42 Green
400 * 43 Yellow
401 * 44 Blue
402 * 45 Magenta
403 * 46 Cyan
404 * 47 White
405 */
406
407 s++; /* [ */
408 while (s < end) {
409 int val;
410
411 s = parsenum(s, &val);
412 s++;
413
414 switch (val) {
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100415 case 0:
416 /* all attributes off */
Simon Glassb9f210a2018-11-06 15:21:36 -0700417 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100418 break;
419 case 1:
420 /* bold */
421 vid_priv->fg_col_idx |= 8;
422 vid_priv->colour_fg = vid_console_color(
423 vid_priv, vid_priv->fg_col_idx);
424 break;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000425 case 7:
426 /* reverse video */
427 vid_priv->colour_fg = vid_console_color(
428 vid_priv, vid_priv->bg_col_idx);
429 vid_priv->colour_bg = vid_console_color(
430 vid_priv, vid_priv->fg_col_idx);
431 break;
Rob Clark703d8852017-09-13 18:12:22 -0400432 case 30 ... 37:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100433 /* foreground color */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100434 vid_priv->fg_col_idx &= ~7;
435 vid_priv->fg_col_idx |= val - 30;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100436 vid_priv->colour_fg = vid_console_color(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100437 vid_priv, vid_priv->fg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400438 break;
439 case 40 ... 47:
Andre Przywaraeabb0722019-03-23 01:29:56 +0000440 /* background color, also mask the bold bit */
441 vid_priv->bg_col_idx &= ~0xf;
442 vid_priv->bg_col_idx |= val - 40;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100443 vid_priv->colour_bg = vid_console_color(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000444 vid_priv, vid_priv->bg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400445 break;
446 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100447 /* ignore unsupported SGR parameter */
Rob Clark703d8852017-09-13 18:12:22 -0400448 break;
449 }
450 }
451
452 break;
453 }
Rob Clarka085aa12017-09-13 18:12:21 -0400454 default:
455 debug("unrecognized escape sequence: %*s\n",
456 priv->escape_len, priv->escape_buf);
457 }
458
459 return;
460
461error:
462 /* something went wrong, just revert to normal mode: */
463 priv->escape = 0;
464}
465
Andre Przywara7035ec32019-03-23 01:29:59 +0000466/* Put that actual character on the screen (using the CP437 code page). */
467static int vidconsole_output_glyph(struct udevice *dev, char ch)
468{
469 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
470 int ret;
471
472 /*
473 * Failure of this function normally indicates an unsupported
474 * colour depth. Check this and return an error to help with
475 * diagnosis.
476 */
477 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
478 if (ret == -EAGAIN) {
479 vidconsole_newline(dev);
480 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
481 }
482 if (ret < 0)
483 return ret;
484 priv->xcur_frac += ret;
485 priv->last_ch = ch;
486 if (priv->xcur_frac >= priv->xsize_frac)
487 vidconsole_newline(dev);
488
489 return 0;
490}
491
Simon Glass83510762016-01-18 19:52:17 -0700492int vidconsole_put_char(struct udevice *dev, char ch)
493{
494 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
495 int ret;
496
Rob Clarka085aa12017-09-13 18:12:21 -0400497 if (priv->escape) {
498 vidconsole_escape_char(dev, ch);
499 return 0;
500 }
501
Simon Glass83510762016-01-18 19:52:17 -0700502 switch (ch) {
Rob Clarka085aa12017-09-13 18:12:21 -0400503 case '\x1b':
504 priv->escape_len = 0;
505 priv->escape = 1;
506 break;
Simon Glass5508f102016-01-14 18:10:38 -0700507 case '\a':
508 /* beep */
509 break;
Simon Glass83510762016-01-18 19:52:17 -0700510 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -0700511 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -0700512 break;
513 case '\n':
514 vidconsole_newline(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700515 vidconsole_entry_start(dev);
Simon Glass83510762016-01-18 19:52:17 -0700516 break;
517 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700518 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
519 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700520
Simon Glassf2661782016-01-14 18:10:37 -0700521 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700522 vidconsole_newline(dev);
523 break;
524 case '\b':
525 vidconsole_back(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700526 priv->last_ch = 0;
Simon Glass83510762016-01-18 19:52:17 -0700527 break;
528 default:
Andre Przywara7035ec32019-03-23 01:29:59 +0000529 ret = vidconsole_output_glyph(dev, ch);
Simon Glassf2661782016-01-14 18:10:37 -0700530 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700531 return ret;
Simon Glass83510762016-01-18 19:52:17 -0700532 break;
533 }
534
535 return 0;
536}
537
Marek Vasute63168a2019-05-17 20:22:31 +0200538int vidconsole_put_string(struct udevice *dev, const char *str)
539{
540 const char *s;
541 int ret;
542
543 for (s = str; *s; s++) {
544 ret = vidconsole_put_char(dev, *s);
545 if (ret)
546 return ret;
547 }
548
549 return 0;
550}
551
Simon Glass83510762016-01-18 19:52:17 -0700552static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
553{
554 struct udevice *dev = sdev->priv;
555
556 vidconsole_put_char(dev, ch);
Simon Glass55d39912018-10-01 11:55:14 -0600557 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700558}
559
560static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
561{
562 struct udevice *dev = sdev->priv;
563
Marek Vasute63168a2019-05-17 20:22:31 +0200564 vidconsole_put_string(dev, s);
Simon Glass55d39912018-10-01 11:55:14 -0600565 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700566}
567
568/* Set up the number of rows and colours (rotated drivers override this) */
569static int vidconsole_pre_probe(struct udevice *dev)
570{
571 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
572 struct udevice *vid = dev->parent;
573 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
574
Simon Glassf2661782016-01-14 18:10:37 -0700575 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700576
577 return 0;
578}
579
580/* Register the device with stdio */
581static int vidconsole_post_probe(struct udevice *dev)
582{
583 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
584 struct stdio_dev *sdev = &priv->sdev;
Simon Glass83510762016-01-18 19:52:17 -0700585
Simon Glassf2661782016-01-14 18:10:37 -0700586 if (!priv->tab_width_frac)
587 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
588
Simon Glassf1a12472016-01-21 19:44:51 -0700589 if (dev->seq) {
590 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
591 dev->seq);
592 } else {
593 strcpy(sdev->name, "vidconsole");
594 }
Simon Glassf2661782016-01-14 18:10:37 -0700595
Simon Glass83510762016-01-18 19:52:17 -0700596 sdev->flags = DEV_FLAGS_OUTPUT;
597 sdev->putc = vidconsole_putc;
598 sdev->puts = vidconsole_puts;
599 sdev->priv = dev;
Simon Glass83510762016-01-18 19:52:17 -0700600
Masahiro Yamada720873b2016-09-06 22:17:33 +0900601 return stdio_register(sdev);
Simon Glass83510762016-01-18 19:52:17 -0700602}
603
604UCLASS_DRIVER(vidconsole) = {
605 .id = UCLASS_VIDEO_CONSOLE,
606 .name = "vidconsole0",
607 .pre_probe = vidconsole_pre_probe,
608 .post_probe = vidconsole_post_probe,
609 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
610};
611
612void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
613{
614 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf2661782016-01-14 18:10:37 -0700615 struct udevice *vid_dev = dev->parent;
616 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass83510762016-01-18 19:52:17 -0700617
Simon Glass9949ee82018-10-01 12:22:47 -0600618 col *= priv->x_charsize;
619 row *= priv->y_charsize;
Simon Glassf2661782016-01-14 18:10:37 -0700620 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
621 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass83510762016-01-18 19:52:17 -0700622}
623
624static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
625 char *const argv[])
626{
627 unsigned int col, row;
628 struct udevice *dev;
629
630 if (argc != 3)
631 return CMD_RET_USAGE;
632
Simon Glass3f603cb2016-02-11 13:23:26 -0700633 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700634 return CMD_RET_FAILURE;
635 col = simple_strtoul(argv[1], NULL, 10);
636 row = simple_strtoul(argv[2], NULL, 10);
637 vidconsole_position_cursor(dev, col, row);
638
639 return 0;
640}
641
642static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
643 char *const argv[])
644{
645 struct udevice *dev;
646 const char *s;
647
648 if (argc != 2)
649 return CMD_RET_USAGE;
650
Simon Glass3f603cb2016-02-11 13:23:26 -0700651 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700652 return CMD_RET_FAILURE;
653 for (s = argv[1]; *s; s++)
654 vidconsole_put_char(dev, *s);
655
Simon Glass55d39912018-10-01 11:55:14 -0600656 video_sync(dev->parent, false);
Rob Clark889808d2017-09-13 18:12:20 -0400657
Simon Glass83510762016-01-18 19:52:17 -0700658 return 0;
659}
660
661U_BOOT_CMD(
662 setcurs, 3, 1, do_video_setcursor,
663 "set cursor position within screen",
664 " <col> <row> in character"
665);
666
667U_BOOT_CMD(
668 lcdputs, 2, 1, do_video_puts,
669 "print string on video framebuffer",
670 " <string>"
671);