dm: spi: Fix spi_free_slave() freed memory write
[platform/kernel/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 <console.h>
13 #include <log.h>
14 #include <dm.h>
15 #include <video.h>
16 #include <video_console.h>
17 #include <video_font.h>         /* Bitmap font for code page 437 */
18 #include <linux/ctype.h>
19
20 /*
21  * Structure to describe a console color
22  */
23 struct vid_rgb {
24         u32 r;
25         u32 g;
26         u32 b;
27 };
28
29 /* By default we scroll by a single line */
30 #ifndef CONFIG_CONSOLE_SCROLL_LINES
31 #define CONFIG_CONSOLE_SCROLL_LINES 1
32 #endif
33
34 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
35 {
36         struct vidconsole_ops *ops = vidconsole_get_ops(dev);
37
38         if (!ops->putc_xy)
39                 return -ENOSYS;
40         return ops->putc_xy(dev, x, y, ch);
41 }
42
43 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
44                          uint count)
45 {
46         struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47
48         if (!ops->move_rows)
49                 return -ENOSYS;
50         return ops->move_rows(dev, rowdst, rowsrc, count);
51 }
52
53 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
54 {
55         struct vidconsole_ops *ops = vidconsole_get_ops(dev);
56
57         if (!ops->set_row)
58                 return -ENOSYS;
59         return ops->set_row(dev, row, clr);
60 }
61
62 static int vidconsole_entry_start(struct udevice *dev)
63 {
64         struct vidconsole_ops *ops = vidconsole_get_ops(dev);
65
66         if (!ops->entry_start)
67                 return -ENOSYS;
68         return ops->entry_start(dev);
69 }
70
71 /* Move backwards one space */
72 static int vidconsole_back(struct udevice *dev)
73 {
74         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
75         struct vidconsole_ops *ops = vidconsole_get_ops(dev);
76         int ret;
77
78         if (ops->backspace) {
79                 ret = ops->backspace(dev);
80                 if (ret != -ENOSYS)
81                         return ret;
82         }
83
84         priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
85         if (priv->xcur_frac < priv->xstart_frac) {
86                 priv->xcur_frac = (priv->cols - 1) *
87                         VID_TO_POS(priv->x_charsize);
88                 priv->ycur -= priv->y_charsize;
89                 if (priv->ycur < 0)
90                         priv->ycur = 0;
91         }
92         video_sync(dev->parent, false);
93
94         return 0;
95 }
96
97 /* Move to a newline, scrolling the display if necessary */
98 static void vidconsole_newline(struct udevice *dev)
99 {
100         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
101         struct udevice *vid_dev = dev->parent;
102         struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
103         const int rows = CONFIG_CONSOLE_SCROLL_LINES;
104         int i;
105
106         priv->xcur_frac = priv->xstart_frac;
107         priv->ycur += priv->y_charsize;
108
109         /* Check if we need to scroll the terminal */
110         if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
111                 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
112                 for (i = 0; i < rows; i++)
113                         vidconsole_set_row(dev, priv->rows - i - 1,
114                                            vid_priv->colour_bg);
115                 priv->ycur -= rows * priv->y_charsize;
116         }
117         priv->last_ch = 0;
118
119         video_sync(dev->parent, false);
120 }
121
122 static const struct vid_rgb colors[VID_COLOR_COUNT] = {
123         { 0x00, 0x00, 0x00 },  /* black */
124         { 0xc0, 0x00, 0x00 },  /* red */
125         { 0x00, 0xc0, 0x00 },  /* green */
126         { 0xc0, 0x60, 0x00 },  /* brown */
127         { 0x00, 0x00, 0xc0 },  /* blue */
128         { 0xc0, 0x00, 0xc0 },  /* magenta */
129         { 0x00, 0xc0, 0xc0 },  /* cyan */
130         { 0xc0, 0xc0, 0xc0 },  /* light gray */
131         { 0x80, 0x80, 0x80 },  /* gray */
132         { 0xff, 0x00, 0x00 },  /* bright red */
133         { 0x00, 0xff, 0x00 },  /* bright green */
134         { 0xff, 0xff, 0x00 },  /* yellow */
135         { 0x00, 0x00, 0xff },  /* bright blue */
136         { 0xff, 0x00, 0xff },  /* bright magenta */
137         { 0x00, 0xff, 0xff },  /* bright cyan */
138         { 0xff, 0xff, 0xff },  /* white */
139 };
140
141 u32 vid_console_color(struct video_priv *priv, unsigned int idx)
142 {
143         switch (priv->bpix) {
144         case VIDEO_BPP16:
145                 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
146                         return ((colors[idx].r >> 3) << 11) |
147                                ((colors[idx].g >> 2) <<  5) |
148                                ((colors[idx].b >> 3) <<  0);
149                 }
150                 break;
151         case VIDEO_BPP32:
152                 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
153                         return (colors[idx].r << 16) |
154                                (colors[idx].g <<  8) |
155                                (colors[idx].b <<  0);
156                 }
157                 break;
158         default:
159                 break;
160         }
161
162         /*
163          * For unknown bit arrangements just support
164          * black and white.
165          */
166         if (idx)
167                 return 0xffffff; /* white */
168
169         return 0x000000; /* black */
170 }
171
172 static char *parsenum(char *s, int *num)
173 {
174         char *end;
175         *num = simple_strtol(s, &end, 10);
176         return end;
177 }
178
179 /**
180  * set_cursor_position() - set cursor position
181  *
182  * @priv:       private data of the video console
183  * @row:        new row
184  * @col:        new column
185  */
186 static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
187 {
188         /*
189          * Ensure we stay in the bounds of the screen.
190          */
191         if (row >= priv->rows)
192                 row = priv->rows - 1;
193         if (col >= priv->cols)
194                 col = priv->cols - 1;
195
196         priv->ycur = row * priv->y_charsize;
197         priv->xcur_frac = priv->xstart_frac +
198                           VID_TO_POS(col * priv->x_charsize);
199 }
200
201 /**
202  * get_cursor_position() - get cursor position
203  *
204  * @priv:       private data of the video console
205  * @row:        row
206  * @col:        column
207  */
208 static void get_cursor_position(struct vidconsole_priv *priv,
209                                 int *row, int *col)
210 {
211         *row = priv->ycur / priv->y_charsize;
212         *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
213                priv->x_charsize;
214 }
215
216 /*
217  * Process a character while accumulating an escape string.  Chars are
218  * accumulated into escape_buf until the end of escape sequence is
219  * found, at which point the sequence is parsed and processed.
220  */
221 static void vidconsole_escape_char(struct udevice *dev, char ch)
222 {
223         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
224
225         if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
226                 goto error;
227
228         /* Sanity checking for bogus ESC sequences: */
229         if (priv->escape_len >= sizeof(priv->escape_buf))
230                 goto error;
231         if (priv->escape_len == 0) {
232                 switch (ch) {
233                 case '7':
234                         /* Save cursor position */
235                         get_cursor_position(priv, &priv->row_saved,
236                                             &priv->col_saved);
237                         priv->escape = 0;
238
239                         return;
240                 case '8': {
241                         /* Restore cursor position */
242                         int row = priv->row_saved;
243                         int col = priv->col_saved;
244
245                         set_cursor_position(priv, row, col);
246                         priv->escape = 0;
247                         return;
248                 }
249                 case '[':
250                         break;
251                 default:
252                         goto error;
253                 }
254         }
255
256         priv->escape_buf[priv->escape_len++] = ch;
257
258         /*
259          * Escape sequences are terminated by a letter, so keep
260          * accumulating until we get one:
261          */
262         if (!isalpha(ch))
263                 return;
264
265         /*
266          * clear escape mode first, otherwise things will get highly
267          * surprising if you hit any debug prints that come back to
268          * this console.
269          */
270         priv->escape = 0;
271
272         switch (ch) {
273         case 'A':
274         case 'B':
275         case 'C':
276         case 'D':
277         case 'E':
278         case 'F': {
279                 int row, col, num;
280                 char *s = priv->escape_buf;
281
282                 /*
283                  * Cursor up/down: [%dA, [%dB, [%dE, [%dF
284                  * Cursor left/right: [%dD, [%dC
285                  */
286                 s++;    /* [ */
287                 s = parsenum(s, &num);
288                 if (num == 0)                   /* No digit in sequence ... */
289                         num = 1;                /* ... means "move by 1". */
290
291                 get_cursor_position(priv, &row, &col);
292                 if (ch == 'A' || ch == 'F')
293                         row -= num;
294                 if (ch == 'C')
295                         col += num;
296                 if (ch == 'D')
297                         col -= num;
298                 if (ch == 'B' || ch == 'E')
299                         row += num;
300                 if (ch == 'E' || ch == 'F')
301                         col = 0;
302                 if (col < 0)
303                         col = 0;
304                 if (row < 0)
305                         row = 0;
306                 /* Right and bottom overflows are handled in the callee. */
307                 set_cursor_position(priv, row, col);
308                 break;
309         }
310         case 'H':
311         case 'f': {
312                 int row, col;
313                 char *s = priv->escape_buf;
314
315                 /*
316                  * Set cursor position: [%d;%df or [%d;%dH
317                  */
318                 s++;    /* [ */
319                 s = parsenum(s, &row);
320                 s++;    /* ; */
321                 s = parsenum(s, &col);
322
323                 /*
324                  * Video origin is [0, 0], terminal origin is [1, 1].
325                  */
326                 if (row)
327                         --row;
328                 if (col)
329                         --col;
330
331                 set_cursor_position(priv, row, col);
332
333                 break;
334         }
335         case 'J': {
336                 int mode;
337
338                 /*
339                  * Clear part/all screen:
340                  *   [J or [0J - clear screen from cursor down
341                  *   [1J       - clear screen from cursor up
342                  *   [2J       - clear entire screen
343                  *
344                  * TODO we really only handle entire-screen case, others
345                  * probably require some additions to video-uclass (and
346                  * are not really needed yet by efi_console)
347                  */
348                 parsenum(priv->escape_buf + 1, &mode);
349
350                 if (mode == 2) {
351                         video_clear(dev->parent);
352                         video_sync(dev->parent, false);
353                         priv->ycur = 0;
354                         priv->xcur_frac = priv->xstart_frac;
355                 } else {
356                         debug("unsupported clear mode: %d\n", mode);
357                 }
358                 break;
359         }
360         case 'K': {
361                 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
362                 int mode;
363
364                 /*
365                  * Clear (parts of) current line
366                  *   [0K       - clear line to end
367                  *   [2K       - clear entire line
368                  */
369                 parsenum(priv->escape_buf + 1, &mode);
370
371                 if (mode == 2) {
372                         int row, col;
373
374                         get_cursor_position(priv, &row, &col);
375                         vidconsole_set_row(dev, row, vid_priv->colour_bg);
376                 }
377                 break;
378         }
379         case 'm': {
380                 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
381                 char *s = priv->escape_buf;
382                 char *end = &priv->escape_buf[priv->escape_len];
383
384                 /*
385                  * Set graphics mode: [%d;...;%dm
386                  *
387                  * Currently only supports the color attributes:
388                  *
389                  * Foreground Colors:
390                  *
391                  *   30 Black
392                  *   31 Red
393                  *   32 Green
394                  *   33 Yellow
395                  *   34 Blue
396                  *   35 Magenta
397                  *   36 Cyan
398                  *   37 White
399                  *
400                  * Background Colors:
401                  *
402                  *   40 Black
403                  *   41 Red
404                  *   42 Green
405                  *   43 Yellow
406                  *   44 Blue
407                  *   45 Magenta
408                  *   46 Cyan
409                  *   47 White
410                  */
411
412                 s++;    /* [ */
413                 while (s < end) {
414                         int val;
415
416                         s = parsenum(s, &val);
417                         s++;
418
419                         switch (val) {
420                         case 0:
421                                 /* all attributes off */
422                                 video_set_default_colors(dev->parent, false);
423                                 break;
424                         case 1:
425                                 /* bold */
426                                 vid_priv->fg_col_idx |= 8;
427                                 vid_priv->colour_fg = vid_console_color(
428                                                 vid_priv, vid_priv->fg_col_idx);
429                                 break;
430                         case 7:
431                                 /* reverse video */
432                                 vid_priv->colour_fg = vid_console_color(
433                                                 vid_priv, vid_priv->bg_col_idx);
434                                 vid_priv->colour_bg = vid_console_color(
435                                                 vid_priv, vid_priv->fg_col_idx);
436                                 break;
437                         case 30 ... 37:
438                                 /* foreground color */
439                                 vid_priv->fg_col_idx &= ~7;
440                                 vid_priv->fg_col_idx |= val - 30;
441                                 vid_priv->colour_fg = vid_console_color(
442                                                 vid_priv, vid_priv->fg_col_idx);
443                                 break;
444                         case 40 ... 47:
445                                 /* background color, also mask the bold bit */
446                                 vid_priv->bg_col_idx &= ~0xf;
447                                 vid_priv->bg_col_idx |= val - 40;
448                                 vid_priv->colour_bg = vid_console_color(
449                                                 vid_priv, vid_priv->bg_col_idx);
450                                 break;
451                         default:
452                                 /* ignore unsupported SGR parameter */
453                                 break;
454                         }
455                 }
456
457                 break;
458         }
459         default:
460                 debug("unrecognized escape sequence: %*s\n",
461                       priv->escape_len, priv->escape_buf);
462         }
463
464         return;
465
466 error:
467         /* something went wrong, just revert to normal mode: */
468         priv->escape = 0;
469 }
470
471 /* Put that actual character on the screen (using the CP437 code page). */
472 static int vidconsole_output_glyph(struct udevice *dev, char ch)
473 {
474         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
475         int ret;
476
477         /*
478          * Failure of this function normally indicates an unsupported
479          * colour depth. Check this and return an error to help with
480          * diagnosis.
481          */
482         ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
483         if (ret == -EAGAIN) {
484                 vidconsole_newline(dev);
485                 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
486         }
487         if (ret < 0)
488                 return ret;
489         priv->xcur_frac += ret;
490         priv->last_ch = ch;
491         if (priv->xcur_frac >= priv->xsize_frac)
492                 vidconsole_newline(dev);
493
494         return 0;
495 }
496
497 int vidconsole_put_char(struct udevice *dev, char ch)
498 {
499         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
500         int ret;
501
502         if (priv->escape) {
503                 vidconsole_escape_char(dev, ch);
504                 return 0;
505         }
506
507         switch (ch) {
508         case '\x1b':
509                 priv->escape_len = 0;
510                 priv->escape = 1;
511                 break;
512         case '\a':
513                 /* beep */
514                 break;
515         case '\r':
516                 priv->xcur_frac = priv->xstart_frac;
517                 break;
518         case '\n':
519                 vidconsole_newline(dev);
520                 vidconsole_entry_start(dev);
521                 break;
522         case '\t':      /* Tab (8 chars alignment) */
523                 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
524                                 + 1) * priv->tab_width_frac;
525
526                 if (priv->xcur_frac >= priv->xsize_frac)
527                         vidconsole_newline(dev);
528                 break;
529         case '\b':
530                 vidconsole_back(dev);
531                 priv->last_ch = 0;
532                 break;
533         default:
534                 ret = vidconsole_output_glyph(dev, ch);
535                 if (ret < 0)
536                         return ret;
537                 break;
538         }
539
540         return 0;
541 }
542
543 int vidconsole_put_string(struct udevice *dev, const char *str)
544 {
545         const char *s;
546         int ret;
547
548         for (s = str; *s; s++) {
549                 ret = vidconsole_put_char(dev, *s);
550                 if (ret)
551                         return ret;
552         }
553
554         return 0;
555 }
556
557 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
558 {
559         struct udevice *dev = sdev->priv;
560         int ret;
561
562         ret = vidconsole_put_char(dev, ch);
563         if (ret) {
564 #ifdef DEBUG
565                 console_puts_select_stderr(true, "[vc err: putc]");
566 #endif
567         }
568         video_sync(dev->parent, false);
569 }
570
571 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
572 {
573         struct udevice *dev = sdev->priv;
574         int ret;
575
576         ret = vidconsole_put_string(dev, s);
577         if (ret) {
578 #ifdef DEBUG
579                 char str[30];
580
581                 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
582                 console_puts_select_stderr(true, str);
583 #endif
584         }
585         video_sync(dev->parent, false);
586 }
587
588 /* Set up the number of rows and colours (rotated drivers override this) */
589 static int vidconsole_pre_probe(struct udevice *dev)
590 {
591         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
592         struct udevice *vid = dev->parent;
593         struct video_priv *vid_priv = dev_get_uclass_priv(vid);
594
595         priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
596
597         return 0;
598 }
599
600 /* Register the device with stdio */
601 static int vidconsole_post_probe(struct udevice *dev)
602 {
603         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
604         struct stdio_dev *sdev = &priv->sdev;
605
606         if (!priv->tab_width_frac)
607                 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
608
609         if (dev_seq(dev)) {
610                 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
611                          dev_seq(dev));
612         } else {
613                 strcpy(sdev->name, "vidconsole");
614         }
615
616         sdev->flags = DEV_FLAGS_OUTPUT;
617         sdev->putc = vidconsole_putc;
618         sdev->puts = vidconsole_puts;
619         sdev->priv = dev;
620
621         return stdio_register(sdev);
622 }
623
624 UCLASS_DRIVER(vidconsole) = {
625         .id             = UCLASS_VIDEO_CONSOLE,
626         .name           = "vidconsole0",
627         .pre_probe      = vidconsole_pre_probe,
628         .post_probe     = vidconsole_post_probe,
629         .per_device_auto        = sizeof(struct vidconsole_priv),
630 };
631
632 #ifdef CONFIG_VIDEO_COPY
633 int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
634 {
635         struct udevice *vid = dev_get_parent(dev);
636
637         return video_sync_copy(vid, from, to);
638 }
639
640 int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
641                        int size)
642 {
643         memmove(dst, src, size);
644         return vidconsole_sync_copy(dev, dst, dst + size);
645 }
646 #endif
647
648 #if CONFIG_IS_ENABLED(CMD_VIDCONSOLE)
649 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
650 {
651         struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
652         struct udevice *vid_dev = dev->parent;
653         struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
654
655         col *= priv->x_charsize;
656         row *= priv->y_charsize;
657         priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
658         priv->xstart_frac = priv->xcur_frac;
659         priv->ycur = min_t(short, row, vid_priv->ysize - 1);
660 }
661
662 static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
663                               char *const argv[])
664 {
665         unsigned int col, row;
666         struct udevice *dev;
667
668         if (argc != 3)
669                 return CMD_RET_USAGE;
670
671         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
672                 return CMD_RET_FAILURE;
673         col = simple_strtoul(argv[1], NULL, 10);
674         row = simple_strtoul(argv[2], NULL, 10);
675         vidconsole_position_cursor(dev, col, row);
676
677         return 0;
678 }
679
680 static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
681                          char *const argv[])
682 {
683         struct udevice *dev;
684         const char *s;
685
686         if (argc != 2)
687                 return CMD_RET_USAGE;
688
689         if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
690                 return CMD_RET_FAILURE;
691         for (s = argv[1]; *s; s++)
692                 vidconsole_put_char(dev, *s);
693
694         video_sync(dev->parent, false);
695
696         return 0;
697 }
698
699 U_BOOT_CMD(
700         setcurs, 3,     1,      do_video_setcursor,
701         "set cursor position within screen",
702         "    <col> <row> in character"
703 );
704
705 U_BOOT_CMD(
706         lcdputs, 2,     1,      do_video_puts,
707         "print string on video framebuffer",
708         "    <string>"
709 );
710 #endif /* CONFIG_IS_ENABLED(CMD_VIDCONSOLE) */