blob: 75c7e25095d21c3d400a71f36993563887b4b7c7 [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 }
Rob Clark50509bb2017-09-13 18:12:22 -0400147 case VIDEO_BPP32:
Simon Glassa4a43c62019-12-20 18:10:36 -0700148 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
149 return (colors[idx].r << 16) |
150 (colors[idx].g << 8) |
151 (colors[idx].b << 0);
152 }
Rob Clark50509bb2017-09-13 18:12:22 -0400153 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100154 /*
155 * For unknown bit arrangements just support
156 * black and white.
157 */
158 if (idx)
159 return 0xffffff; /* white */
160 else
161 return 0x000000; /* black */
Rob Clark50509bb2017-09-13 18:12:22 -0400162 }
163}
164
Rob Clark06e7a0d2017-09-13 18:12:21 -0400165static char *parsenum(char *s, int *num)
166{
167 char *end;
168 *num = simple_strtol(s, &end, 10);
169 return end;
170}
171
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200172/**
173 * set_cursor_position() - set cursor position
174 *
175 * @priv: private data of the video console
176 * @row: new row
177 * @col: new column
178 */
179static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
180{
181 /*
182 * Ensure we stay in the bounds of the screen.
183 */
184 if (row >= priv->rows)
185 row = priv->rows - 1;
186 if (col >= priv->cols)
187 col = priv->cols - 1;
188
189 priv->ycur = row * priv->y_charsize;
190 priv->xcur_frac = priv->xstart_frac +
191 VID_TO_POS(col * priv->x_charsize);
192}
193
194/**
195 * get_cursor_position() - get cursor position
196 *
197 * @priv: private data of the video console
198 * @row: row
199 * @col: column
200 */
201static void get_cursor_position(struct vidconsole_priv *priv,
202 int *row, int *col)
203{
204 *row = priv->ycur / priv->y_charsize;
205 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
206 priv->x_charsize;
207}
208
Rob Clark06e7a0d2017-09-13 18:12:21 -0400209/*
210 * Process a character while accumulating an escape string. Chars are
211 * accumulated into escape_buf until the end of escape sequence is
212 * found, at which point the sequence is parsed and processed.
213 */
214static void vidconsole_escape_char(struct udevice *dev, char ch)
215{
216 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
217
218 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
219 goto error;
220
221 /* Sanity checking for bogus ESC sequences: */
222 if (priv->escape_len >= sizeof(priv->escape_buf))
223 goto error;
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200224 if (priv->escape_len == 0) {
225 switch (ch) {
226 case '7':
227 /* Save cursor position */
228 get_cursor_position(priv, &priv->row_saved,
229 &priv->col_saved);
230 priv->escape = 0;
231
232 return;
233 case '8': {
234 /* Restore cursor position */
235 int row = priv->row_saved;
236 int col = priv->col_saved;
237
238 set_cursor_position(priv, row, col);
239 priv->escape = 0;
240 return;
241 }
242 case '[':
243 break;
244 default:
245 goto error;
246 }
247 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400248
249 priv->escape_buf[priv->escape_len++] = ch;
250
251 /*
252 * Escape sequences are terminated by a letter, so keep
253 * accumulating until we get one:
254 */
255 if (!isalpha(ch))
256 return;
257
258 /*
259 * clear escape mode first, otherwise things will get highly
260 * surprising if you hit any debug prints that come back to
261 * this console.
262 */
263 priv->escape = 0;
264
265 switch (ch) {
Andre Przywarad4a294c2019-03-23 01:29:57 +0000266 case 'A':
267 case 'B':
268 case 'C':
269 case 'D':
270 case 'E':
271 case 'F': {
272 int row, col, num;
273 char *s = priv->escape_buf;
274
275 /*
276 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
277 * Cursor left/right: [%dD, [%dC
278 */
279 s++; /* [ */
280 s = parsenum(s, &num);
281 if (num == 0) /* No digit in sequence ... */
282 num = 1; /* ... means "move by 1". */
283
284 get_cursor_position(priv, &row, &col);
285 if (ch == 'A' || ch == 'F')
286 row -= num;
287 if (ch == 'C')
288 col += num;
289 if (ch == 'D')
290 col -= num;
291 if (ch == 'B' || ch == 'E')
292 row += num;
293 if (ch == 'E' || ch == 'F')
294 col = 0;
295 if (col < 0)
296 col = 0;
297 if (row < 0)
298 row = 0;
299 /* Right and bottom overflows are handled in the callee. */
300 set_cursor_position(priv, row, col);
301 break;
302 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400303 case 'H':
304 case 'f': {
305 int row, col;
306 char *s = priv->escape_buf;
307
308 /*
309 * Set cursor position: [%d;%df or [%d;%dH
310 */
311 s++; /* [ */
312 s = parsenum(s, &row);
313 s++; /* ; */
314 s = parsenum(s, &col);
315
Heinrich Schuchardtc3c69302018-11-10 19:55:48 +0100316 /*
317 * Video origin is [0, 0], terminal origin is [1, 1].
318 */
319 if (row)
320 --row;
321 if (col)
322 --col;
323
Heinrich Schuchardt9e933f12018-09-19 21:31:48 +0200324 set_cursor_position(priv, row, col);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400325
326 break;
327 }
328 case 'J': {
329 int mode;
330
331 /*
332 * Clear part/all screen:
333 * [J or [0J - clear screen from cursor down
334 * [1J - clear screen from cursor up
335 * [2J - clear entire screen
336 *
337 * TODO we really only handle entire-screen case, others
338 * probably require some additions to video-uclass (and
339 * are not really needed yet by efi_console)
340 */
341 parsenum(priv->escape_buf + 1, &mode);
342
343 if (mode == 2) {
344 video_clear(dev->parent);
Simon Glass0806dcc2018-10-01 11:55:14 -0600345 video_sync(dev->parent, false);
Rob Clark06e7a0d2017-09-13 18:12:21 -0400346 priv->ycur = 0;
347 priv->xcur_frac = priv->xstart_frac;
348 } else {
349 debug("unsupported clear mode: %d\n", mode);
350 }
351 break;
352 }
Andre Przywara918622a2019-03-23 01:29:58 +0000353 case 'K': {
354 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
355 int mode;
356
357 /*
358 * Clear (parts of) current line
359 * [0K - clear line to end
360 * [2K - clear entire line
361 */
362 parsenum(priv->escape_buf + 1, &mode);
363
364 if (mode == 2) {
365 int row, col;
366
367 get_cursor_position(priv, &row, &col);
368 vidconsole_set_row(dev, row, vid_priv->colour_bg);
369 }
370 break;
371 }
Rob Clark50509bb2017-09-13 18:12:22 -0400372 case 'm': {
373 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
374 char *s = priv->escape_buf;
375 char *end = &priv->escape_buf[priv->escape_len];
376
377 /*
378 * Set graphics mode: [%d;...;%dm
379 *
380 * Currently only supports the color attributes:
381 *
382 * Foreground Colors:
383 *
384 * 30 Black
385 * 31 Red
386 * 32 Green
387 * 33 Yellow
388 * 34 Blue
389 * 35 Magenta
390 * 36 Cyan
391 * 37 White
392 *
393 * Background Colors:
394 *
395 * 40 Black
396 * 41 Red
397 * 42 Green
398 * 43 Yellow
399 * 44 Blue
400 * 45 Magenta
401 * 46 Cyan
402 * 47 White
403 */
404
405 s++; /* [ */
406 while (s < end) {
407 int val;
408
409 s = parsenum(s, &val);
410 s++;
411
412 switch (val) {
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100413 case 0:
414 /* all attributes off */
Simon Glass2b063b82018-11-06 15:21:36 -0700415 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100416 break;
417 case 1:
418 /* bold */
419 vid_priv->fg_col_idx |= 8;
420 vid_priv->colour_fg = vid_console_color(
421 vid_priv, vid_priv->fg_col_idx);
422 break;
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000423 case 7:
424 /* reverse video */
425 vid_priv->colour_fg = vid_console_color(
426 vid_priv, vid_priv->bg_col_idx);
427 vid_priv->colour_bg = vid_console_color(
428 vid_priv, vid_priv->fg_col_idx);
429 break;
Rob Clark50509bb2017-09-13 18:12:22 -0400430 case 30 ... 37:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100431 /* foreground color */
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100432 vid_priv->fg_col_idx &= ~7;
433 vid_priv->fg_col_idx |= val - 30;
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100434 vid_priv->colour_fg = vid_console_color(
Heinrich Schuchardt2a436db2018-02-08 21:47:12 +0100435 vid_priv, vid_priv->fg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400436 break;
437 case 40 ... 47:
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000438 /* background color, also mask the bold bit */
439 vid_priv->bg_col_idx &= ~0xf;
440 vid_priv->bg_col_idx |= val - 40;
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100441 vid_priv->colour_bg = vid_console_color(
Andre Przywara4ed5bc82019-03-23 01:29:56 +0000442 vid_priv, vid_priv->bg_col_idx);
Rob Clark50509bb2017-09-13 18:12:22 -0400443 break;
444 default:
Heinrich Schuchardt290e1d82018-02-08 21:47:11 +0100445 /* ignore unsupported SGR parameter */
Rob Clark50509bb2017-09-13 18:12:22 -0400446 break;
447 }
448 }
449
450 break;
451 }
Rob Clark06e7a0d2017-09-13 18:12:21 -0400452 default:
453 debug("unrecognized escape sequence: %*s\n",
454 priv->escape_len, priv->escape_buf);
455 }
456
457 return;
458
459error:
460 /* something went wrong, just revert to normal mode: */
461 priv->escape = 0;
462}
463
Andre Przywarade86baf2019-03-23 01:29:59 +0000464/* Put that actual character on the screen (using the CP437 code page). */
465static int vidconsole_output_glyph(struct udevice *dev, char ch)
466{
467 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
468 int ret;
469
470 /*
471 * Failure of this function normally indicates an unsupported
472 * colour depth. Check this and return an error to help with
473 * diagnosis.
474 */
475 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
476 if (ret == -EAGAIN) {
477 vidconsole_newline(dev);
478 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
479 }
480 if (ret < 0)
481 return ret;
482 priv->xcur_frac += ret;
483 priv->last_ch = ch;
484 if (priv->xcur_frac >= priv->xsize_frac)
485 vidconsole_newline(dev);
486
487 return 0;
488}
489
Simon Glass84c7fb32016-01-18 19:52:17 -0700490int vidconsole_put_char(struct udevice *dev, char ch)
491{
492 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
493 int ret;
494
Rob Clark06e7a0d2017-09-13 18:12:21 -0400495 if (priv->escape) {
496 vidconsole_escape_char(dev, ch);
497 return 0;
498 }
499
Simon Glass84c7fb32016-01-18 19:52:17 -0700500 switch (ch) {
Rob Clark06e7a0d2017-09-13 18:12:21 -0400501 case '\x1b':
502 priv->escape_len = 0;
503 priv->escape = 1;
504 break;
Simon Glass37b80202016-01-14 18:10:38 -0700505 case '\a':
506 /* beep */
507 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700508 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700509 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700510 break;
511 case '\n':
512 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700513 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700514 break;
515 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700516 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
517 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700518
Simon Glass52c10c52016-01-14 18:10:37 -0700519 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700520 vidconsole_newline(dev);
521 break;
522 case '\b':
523 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700524 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700525 break;
526 default:
Andre Przywarade86baf2019-03-23 01:29:59 +0000527 ret = vidconsole_output_glyph(dev, ch);
Simon Glass52c10c52016-01-14 18:10:37 -0700528 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700529 return ret;
Simon Glass84c7fb32016-01-18 19:52:17 -0700530 break;
531 }
532
533 return 0;
534}
535
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200536int vidconsole_put_string(struct udevice *dev, const char *str)
537{
538 const char *s;
539 int ret;
540
541 for (s = str; *s; s++) {
542 ret = vidconsole_put_char(dev, *s);
543 if (ret)
544 return ret;
545 }
546
547 return 0;
548}
549
Simon Glass84c7fb32016-01-18 19:52:17 -0700550static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
551{
552 struct udevice *dev = sdev->priv;
553
554 vidconsole_put_char(dev, ch);
Simon Glass0806dcc2018-10-01 11:55:14 -0600555 video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -0700556}
557
558static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
559{
560 struct udevice *dev = sdev->priv;
561
Marek Vasuta89f9cb2019-05-17 20:22:31 +0200562 vidconsole_put_string(dev, s);
Simon Glass0806dcc2018-10-01 11:55:14 -0600563 video_sync(dev->parent, false);
Simon Glass84c7fb32016-01-18 19:52:17 -0700564}
565
566/* Set up the number of rows and colours (rotated drivers override this) */
567static int vidconsole_pre_probe(struct udevice *dev)
568{
569 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
570 struct udevice *vid = dev->parent;
571 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
572
Simon Glass52c10c52016-01-14 18:10:37 -0700573 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700574
575 return 0;
576}
577
578/* Register the device with stdio */
579static int vidconsole_post_probe(struct udevice *dev)
580{
581 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
582 struct stdio_dev *sdev = &priv->sdev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700583
Simon Glass52c10c52016-01-14 18:10:37 -0700584 if (!priv->tab_width_frac)
585 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
586
Simon Glass798ff502016-01-21 19:44:51 -0700587 if (dev->seq) {
588 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
589 dev->seq);
590 } else {
591 strcpy(sdev->name, "vidconsole");
592 }
Simon Glass52c10c52016-01-14 18:10:37 -0700593
Simon Glass84c7fb32016-01-18 19:52:17 -0700594 sdev->flags = DEV_FLAGS_OUTPUT;
595 sdev->putc = vidconsole_putc;
596 sdev->puts = vidconsole_puts;
597 sdev->priv = dev;
Simon Glass84c7fb32016-01-18 19:52:17 -0700598
Masahiro Yamadabf528cd2016-09-06 22:17:33 +0900599 return stdio_register(sdev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700600}
601
602UCLASS_DRIVER(vidconsole) = {
603 .id = UCLASS_VIDEO_CONSOLE,
604 .name = "vidconsole0",
605 .pre_probe = vidconsole_pre_probe,
606 .post_probe = vidconsole_post_probe,
607 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
608};
609
610void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
611{
612 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass52c10c52016-01-14 18:10:37 -0700613 struct udevice *vid_dev = dev->parent;
614 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700615
Simon Glass59f44212018-10-01 12:22:47 -0600616 col *= priv->x_charsize;
617 row *= priv->y_charsize;
Simon Glass52c10c52016-01-14 18:10:37 -0700618 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
619 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass84c7fb32016-01-18 19:52:17 -0700620}
621
622static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
623 char *const argv[])
624{
625 unsigned int col, row;
626 struct udevice *dev;
627
628 if (argc != 3)
629 return CMD_RET_USAGE;
630
Simon Glassc7298e72016-02-11 13:23:26 -0700631 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass84c7fb32016-01-18 19:52:17 -0700632 return CMD_RET_FAILURE;
633 col = simple_strtoul(argv[1], NULL, 10);
634 row = simple_strtoul(argv[2], NULL, 10);
635 vidconsole_position_cursor(dev, col, row);
636
637 return 0;
638}
639
640static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
641 char *const argv[])
642{
643 struct udevice *dev;
644 const char *s;
645
646 if (argc != 2)
647 return CMD_RET_USAGE;
648
Simon Glassc7298e72016-02-11 13:23:26 -0700649 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass84c7fb32016-01-18 19:52:17 -0700650 return CMD_RET_FAILURE;
651 for (s = argv[1]; *s; s++)
652 vidconsole_put_char(dev, *s);
653
Simon Glass0806dcc2018-10-01 11:55:14 -0600654 video_sync(dev->parent, false);
Rob Clarke9594612017-09-13 18:12:20 -0400655
Simon Glass84c7fb32016-01-18 19:52:17 -0700656 return 0;
657}
658
659U_BOOT_CMD(
660 setcurs, 3, 1, do_video_setcursor,
661 "set cursor position within screen",
662 " <col> <row> in character"
663);
664
665U_BOOT_CMD(
666 lcdputs, 2, 1, do_video_puts,
667 "print string on video framebuffer",
668 " <string>"
669);