Merge tag 'dm-pull-8jan20' of git://git.denx.de/u-boot-dm
[oweals/u-boot.git] / drivers / video / vidconsole-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
8  */
9
10 #include <common.h>
11 #include <linux/ctype.h>
12 #include <dm.h>
13 #include <video.h>
14 #include <video_console.h>
15 #include <video_font.h>         /* Bitmap font for code page 437 */
16
17 /*
18  * Structure to describe a console color
19  */
20 struct vid_rgb {
21         u32 r;
22         u32 g;
23         u32 b;
24 };
25
26 /* By default we scroll by a single line */
27 #ifndef CONFIG_CONSOLE_SCROLL_LINES
28 #define CONFIG_CONSOLE_SCROLL_LINES 1
29 #endif
30
31 int 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
40 int 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
50 int 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
59 static 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
68 /* Move backwards one space */
69 static int vidconsole_back(struct udevice *dev)
70 {
71         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
72         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         }
80
81         priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
82         if (priv->xcur_frac < priv->xstart_frac) {
83                 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;
88         }
89         video_sync(dev->parent, false);
90
91         return 0;
92 }
93
94 /* Move to a newline, scrolling the display if necessary */
95 static 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
103         priv->xcur_frac = priv->xstart_frac;
104         priv->ycur += priv->y_charsize;
105
106         /* Check if we need to scroll the terminal */
107         if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
108                 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);
112                 priv->ycur -= rows * priv->y_charsize;
113         }
114         priv->last_ch = 0;
115
116         video_sync(dev->parent, false);
117 }
118
119 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
120         { 0x00, 0x00, 0x00 },  /* black */
121         { 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 */
131         { 0xff, 0xff, 0x00 },  /* yellow */
132         { 0x00, 0x00, 0xff },  /* bright blue */
133         { 0xff, 0x00, 0xff },  /* bright magenta */
134         { 0x00, 0xff, 0xff },  /* bright cyan */
135         { 0xff, 0xff, 0xff },  /* white */
136 };
137
138 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
139 {
140         switch (priv->bpix) {
141         case VIDEO_BPP16:
142                 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                 }
147         case VIDEO_BPP32:
148                 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
149                         return (colors[idx].r << 16) |
150                                (colors[idx].g <<  8) |
151                                (colors[idx].b <<  0);
152                 }
153         default:
154                 /*
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 */
162         }
163 }
164
165 static char *parsenum(char *s, int *num)
166 {
167         char *end;
168         *num = simple_strtol(s, &end, 10);
169         return end;
170 }
171
172 /**
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  */
179 static 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  */
201 static 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
209 /*
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  */
214 static 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;
224         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         }
248
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) {
266         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         }
303         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
316                 /*
317                  * Video origin is [0, 0], terminal origin is [1, 1].
318                  */
319                 if (row)
320                         --row;
321                 if (col)
322                         --col;
323
324                 set_cursor_position(priv, row, col);
325
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);
345                         video_sync(dev->parent, false);
346                         priv->ycur = 0;
347                         priv->xcur_frac = priv->xstart_frac;
348                 } else {
349                         debug("unsupported clear mode: %d\n", mode);
350                 }
351                 break;
352         }
353         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         }
372         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) {
413                         case 0:
414                                 /* all attributes off */
415                                 video_set_default_colors(dev->parent, false);
416                                 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;
423                         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;
430                         case 30 ... 37:
431                                 /* foreground color */
432                                 vid_priv->fg_col_idx &= ~7;
433                                 vid_priv->fg_col_idx |= val - 30;
434                                 vid_priv->colour_fg = vid_console_color(
435                                                 vid_priv, vid_priv->fg_col_idx);
436                                 break;
437                         case 40 ... 47:
438                                 /* background color, also mask the bold bit */
439                                 vid_priv->bg_col_idx &= ~0xf;
440                                 vid_priv->bg_col_idx |= val - 40;
441                                 vid_priv->colour_bg = vid_console_color(
442                                                 vid_priv, vid_priv->bg_col_idx);
443                                 break;
444                         default:
445                                 /* ignore unsupported SGR parameter */
446                                 break;
447                         }
448                 }
449
450                 break;
451         }
452         default:
453                 debug("unrecognized escape sequence: %*s\n",
454                       priv->escape_len, priv->escape_buf);
455         }
456
457         return;
458
459 error:
460         /* something went wrong, just revert to normal mode: */
461         priv->escape = 0;
462 }
463
464 /* Put that actual character on the screen (using the CP437 code page). */
465 static 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
490 int vidconsole_put_char(struct udevice *dev, char ch)
491 {
492         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
493         int ret;
494
495         if (priv->escape) {
496                 vidconsole_escape_char(dev, ch);
497                 return 0;
498         }
499
500         switch (ch) {
501         case '\x1b':
502                 priv->escape_len = 0;
503                 priv->escape = 1;
504                 break;
505         case '\a':
506                 /* beep */
507                 break;
508         case '\r':
509                 priv->xcur_frac = priv->xstart_frac;
510                 break;
511         case '\n':
512                 vidconsole_newline(dev);
513                 vidconsole_entry_start(dev);
514                 break;
515         case '\t':      /* Tab (8 chars alignment) */
516                 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
517                                 + 1) * priv->tab_width_frac;
518
519                 if (priv->xcur_frac >= priv->xsize_frac)
520                         vidconsole_newline(dev);
521                 break;
522         case '\b':
523                 vidconsole_back(dev);
524                 priv->last_ch = 0;
525                 break;
526         default:
527                 ret = vidconsole_output_glyph(dev, ch);
528                 if (ret < 0)
529                         return ret;
530                 break;
531         }
532
533         return 0;
534 }
535
536 int 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
550 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
551 {
552         struct udevice *dev = sdev->priv;
553
554         vidconsole_put_char(dev, ch);
555         video_sync(dev->parent, false);
556 }
557
558 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
559 {
560         struct udevice *dev = sdev->priv;
561
562         vidconsole_put_string(dev, s);
563         video_sync(dev->parent, false);
564 }
565
566 /* Set up the number of rows and colours (rotated drivers override this) */
567 static 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
573         priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
574
575         return 0;
576 }
577
578 /* Register the device with stdio */
579 static 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;
583
584         if (!priv->tab_width_frac)
585                 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
586
587         if (dev->seq) {
588                 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
589                          dev->seq);
590         } else {
591                 strcpy(sdev->name, "vidconsole");
592         }
593
594         sdev->flags = DEV_FLAGS_OUTPUT;
595         sdev->putc = vidconsole_putc;
596         sdev->puts = vidconsole_puts;
597         sdev->priv = dev;
598
599         return stdio_register(sdev);
600 }
601
602 UCLASS_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
610 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
611 {
612         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
613         struct udevice *vid_dev = dev->parent;
614         struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
615
616         col *= priv->x_charsize;
617         row *= priv->y_charsize;
618         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);
620 }
621
622 static 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
631         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
632                 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
640 static 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
649         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
650                 return CMD_RET_FAILURE;
651         for (s = argv[1]; *s; s++)
652                 vidconsole_put_char(dev, *s);
653
654         video_sync(dev->parent, false);
655
656         return 0;
657 }
658
659 U_BOOT_CMD(
660         setcurs, 3,     1,      do_video_setcursor,
661         "set cursor position within screen",
662         "    <col> <row> in character"
663 );
664
665 U_BOOT_CMD(
666         lcdputs, 2,     1,      do_video_puts,
667         "print string on video framebuffer",
668         "    <string>"
669 );