command: Remove the cmd_tbl_t typedef
[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 <command.h>
12 #include <linux/ctype.h>
13 #include <dm.h>
14 #include <video.h>
15 #include <video_console.h>
16 #include <video_font.h>         /* Bitmap font for code page 437 */
17
18 /*
19  * Structure to describe a console color
20  */
21 struct vid_rgb {
22         u32 r;
23         u32 g;
24         u32 b;
25 };
26
27 /* By default we scroll by a single line */
28 #ifndef CONFIG_CONSOLE_SCROLL_LINES
29 #define CONFIG_CONSOLE_SCROLL_LINES 1
30 #endif
31
32 int 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
41 int 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
51 int 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
60 static 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
69 /* Move backwards one space */
70 static int vidconsole_back(struct udevice *dev)
71 {
72         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
73         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         }
81
82         priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
83         if (priv->xcur_frac < priv->xstart_frac) {
84                 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;
89         }
90         video_sync(dev->parent, false);
91
92         return 0;
93 }
94
95 /* Move to a newline, scrolling the display if necessary */
96 static 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
104         priv->xcur_frac = priv->xstart_frac;
105         priv->ycur += priv->y_charsize;
106
107         /* Check if we need to scroll the terminal */
108         if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
109                 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);
113                 priv->ycur -= rows * priv->y_charsize;
114         }
115         priv->last_ch = 0;
116
117         video_sync(dev->parent, false);
118 }
119
120 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
121         { 0x00, 0x00, 0x00 },  /* black */
122         { 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 */
132         { 0xff, 0xff, 0x00 },  /* yellow */
133         { 0x00, 0x00, 0xff },  /* bright blue */
134         { 0xff, 0x00, 0xff },  /* bright magenta */
135         { 0x00, 0xff, 0xff },  /* bright cyan */
136         { 0xff, 0xff, 0xff },  /* white */
137 };
138
139 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
140 {
141         switch (priv->bpix) {
142         case VIDEO_BPP16:
143                 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                 }
148                 break;
149         case VIDEO_BPP32:
150                 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
151                         return (colors[idx].r << 16) |
152                                (colors[idx].g <<  8) |
153                                (colors[idx].b <<  0);
154                 }
155                 break;
156         default:
157                 break;
158         }
159
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 */
168 }
169
170 static char *parsenum(char *s, int *num)
171 {
172         char *end;
173         *num = simple_strtol(s, &end, 10);
174         return end;
175 }
176
177 /**
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  */
184 static 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  */
206 static 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
214 /*
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  */
219 static 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;
229         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         }
253
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) {
271         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         }
308         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
321                 /*
322                  * Video origin is [0, 0], terminal origin is [1, 1].
323                  */
324                 if (row)
325                         --row;
326                 if (col)
327                         --col;
328
329                 set_cursor_position(priv, row, col);
330
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);
350                         video_sync(dev->parent, false);
351                         priv->ycur = 0;
352                         priv->xcur_frac = priv->xstart_frac;
353                 } else {
354                         debug("unsupported clear mode: %d\n", mode);
355                 }
356                 break;
357         }
358         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         }
377         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) {
418                         case 0:
419                                 /* all attributes off */
420                                 video_set_default_colors(dev->parent, false);
421                                 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;
428                         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;
435                         case 30 ... 37:
436                                 /* foreground color */
437                                 vid_priv->fg_col_idx &= ~7;
438                                 vid_priv->fg_col_idx |= val - 30;
439                                 vid_priv->colour_fg = vid_console_color(
440                                                 vid_priv, vid_priv->fg_col_idx);
441                                 break;
442                         case 40 ... 47:
443                                 /* background color, also mask the bold bit */
444                                 vid_priv->bg_col_idx &= ~0xf;
445                                 vid_priv->bg_col_idx |= val - 40;
446                                 vid_priv->colour_bg = vid_console_color(
447                                                 vid_priv, vid_priv->bg_col_idx);
448                                 break;
449                         default:
450                                 /* ignore unsupported SGR parameter */
451                                 break;
452                         }
453                 }
454
455                 break;
456         }
457         default:
458                 debug("unrecognized escape sequence: %*s\n",
459                       priv->escape_len, priv->escape_buf);
460         }
461
462         return;
463
464 error:
465         /* something went wrong, just revert to normal mode: */
466         priv->escape = 0;
467 }
468
469 /* Put that actual character on the screen (using the CP437 code page). */
470 static 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
495 int vidconsole_put_char(struct udevice *dev, char ch)
496 {
497         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
498         int ret;
499
500         if (priv->escape) {
501                 vidconsole_escape_char(dev, ch);
502                 return 0;
503         }
504
505         switch (ch) {
506         case '\x1b':
507                 priv->escape_len = 0;
508                 priv->escape = 1;
509                 break;
510         case '\a':
511                 /* beep */
512                 break;
513         case '\r':
514                 priv->xcur_frac = priv->xstart_frac;
515                 break;
516         case '\n':
517                 vidconsole_newline(dev);
518                 vidconsole_entry_start(dev);
519                 break;
520         case '\t':      /* Tab (8 chars alignment) */
521                 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
522                                 + 1) * priv->tab_width_frac;
523
524                 if (priv->xcur_frac >= priv->xsize_frac)
525                         vidconsole_newline(dev);
526                 break;
527         case '\b':
528                 vidconsole_back(dev);
529                 priv->last_ch = 0;
530                 break;
531         default:
532                 ret = vidconsole_output_glyph(dev, ch);
533                 if (ret < 0)
534                         return ret;
535                 break;
536         }
537
538         return 0;
539 }
540
541 int 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
555 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
556 {
557         struct udevice *dev = sdev->priv;
558
559         vidconsole_put_char(dev, ch);
560         video_sync(dev->parent, false);
561 }
562
563 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
564 {
565         struct udevice *dev = sdev->priv;
566
567         vidconsole_put_string(dev, s);
568         video_sync(dev->parent, false);
569 }
570
571 /* Set up the number of rows and colours (rotated drivers override this) */
572 static 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
578         priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
579
580         return 0;
581 }
582
583 /* Register the device with stdio */
584 static 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;
588
589         if (!priv->tab_width_frac)
590                 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
591
592         if (dev->seq) {
593                 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
594                          dev->seq);
595         } else {
596                 strcpy(sdev->name, "vidconsole");
597         }
598
599         sdev->flags = DEV_FLAGS_OUTPUT;
600         sdev->putc = vidconsole_putc;
601         sdev->puts = vidconsole_puts;
602         sdev->priv = dev;
603
604         return stdio_register(sdev);
605 }
606
607 UCLASS_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
615 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
616 {
617         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
618         struct udevice *vid_dev = dev->parent;
619         struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
620
621         col *= priv->x_charsize;
622         row *= priv->y_charsize;
623         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);
625 }
626
627 static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
628                               char *const argv[])
629 {
630         unsigned int col, row;
631         struct udevice *dev;
632
633         if (argc != 3)
634                 return CMD_RET_USAGE;
635
636         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
637                 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
645 static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
646                          char *const argv[])
647 {
648         struct udevice *dev;
649         const char *s;
650
651         if (argc != 2)
652                 return CMD_RET_USAGE;
653
654         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
655                 return CMD_RET_FAILURE;
656         for (s = argv[1]; *s; s++)
657                 vidconsole_put_char(dev, *s);
658
659         video_sync(dev->parent, false);
660
661         return 0;
662 }
663
664 U_BOOT_CMD(
665         setcurs, 3,     1,      do_video_setcursor,
666         "set cursor position within screen",
667         "    <col> <row> in character"
668 );
669
670 U_BOOT_CMD(
671         lcdputs, 2,     1,      do_video_puts,
672         "print string on video framebuffer",
673         "    <string>"
674 );