Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2015 Google, Inc
 * (C) Copyright 2001-2015
 * DENX Software Engineering -- wd@denx.de
 * Compulab Ltd - http://compulab.co.il/
 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
 */

#define LOG_CATEGORY UCLASS_VIDEO_CONSOLE

#include <abuf.h>
#include <charset.h>
#include <command.h>
#include <console.h>
#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <spl.h>
#include <video.h>
#include <video_console.h>
#include "vidconsole_internal.h"
#include <video_font.h>		/* Bitmap font for code page 437 */
#include <linux/ctype.h>

int vidconsole_putc_xy(struct udevice *dev, void *vctx, uint x, uint y, int ch)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ops->putc_xy)
		return -ENOSYS;
	return ops->putc_xy(dev, vctx ?: vidconsole_ctx_from_priv(priv), x, y,
			    ch);
}

int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
			 uint count)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ops->move_rows)
		return -ENOSYS;
	return ops->move_rows(dev, rowdst, rowsrc, count);
}

int vidconsole_set_row(struct udevice *dev, uint row, int clr)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ops->set_row)
		return -ENOSYS;
	return ops->set_row(dev, row, clr);
}

int vidconsole_entry_start(struct udevice *dev, void *ctx)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ctx)
		ctx = vidconsole_ctx_from_priv(dev_get_uclass_priv(dev));
	if (!ops->entry_start)
		return -ENOSYS;
	return ops->entry_start(dev, ctx);
}

/* Move backwards one space, ctx must be non-NULL */
static int vidconsole_back(struct udevice *dev, struct vidconsole_ctx *ctx)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
	int ret;

	if (ops->backspace) {
		ret = ops->backspace(dev, ctx);
		if (ret != -ENOSYS)
			return ret;
	}

	/* Hide cursor at old position if it's visible */
	vidconsole_hide_cursor(dev, ctx);

	ctx->xcur_frac -= VID_TO_POS(ctx->x_charsize);
	if (ctx->xcur_frac < ctx->xstart_frac) {
		ctx->xcur_frac = (ctx->cols - 1) *
			VID_TO_POS(ctx->x_charsize);
		ctx->ycur -= ctx->y_charsize;
		if (ctx->ycur < 0)
			ctx->ycur = 0;
	}
	assert(ctx->cli_index);
	cli_index_adjust(ctx, -1);

	return video_sync(dev->parent, false);
}

/*
 * Move to a newline, scrolling the display if necessary.
 * ctx must be non-NULL
 */
static void vidconsole_newline(struct udevice *dev, struct vidconsole_ctx *ctx)
{
	struct udevice *vid_dev = dev->parent;
	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
	const int rows = CONFIG_VAL(CONSOLE_SCROLL_LINES);
	int i, ret;

	ctx->xcur_frac = ctx->xstart_frac;
	ctx->ycur += ctx->y_charsize;

	/* Check if we need to scroll the terminal */
	if (vid_priv->rot % 2 ?
	    ctx->ycur + ctx->x_charsize > vid_priv->xsize :
	    ctx->ycur + ctx->y_charsize > vid_priv->ysize) {
		vidconsole_move_rows(dev, 0, rows, ctx->rows - rows);
		for (i = 0; i < rows; i++)
			vidconsole_set_row(dev, ctx->rows - i - 1,
					   vid_priv->colour_bg);
		ctx->ycur -= rows * ctx->y_charsize;
	}
	ctx->last_ch = 0;

	ret = video_sync(dev->parent, false);
	if (ret) {
#ifdef DEBUG
		err_puts(true, "[vc err: video_sync]");
#endif
	}
}

static char *parsenum(char *s, int *num)
{
	char *end;
	*num = simple_strtol(s, &end, 10);
	return end;
}

void vidconsole_set_cursor_pos(struct udevice *dev, void *vctx, int x, int y)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx_from_priv(priv);

	/* Hide cursor at old position if it's visible */
	vidconsole_hide_cursor(dev, ctx);

	ctx->xcur_frac = VID_TO_POS(x);
	ctx->xstart_frac = ctx->xcur_frac;
	ctx->ycur = y;

	/* make sure not to kern against the previous character */
	ctx->last_ch = 0;
	vidconsole_entry_start(dev, ctx);
}

/**
 * set_cursor_position() - set cursor position
 *
 * @priv:	private data of the video console
 * @row:	new row
 * @col:	new column
 */
static void set_cursor_position(struct udevice *dev, int row, int col)
{
	struct vidconsole_ctx *ctx = vidconsole_ctx(dev);

	/*
	 * Ensure we stay in the bounds of the screen.
	 */
	if (row >= ctx->rows)
		row = ctx->rows - 1;
	if (col >= ctx->cols)
		col = ctx->cols - 1;

	vidconsole_position_cursor(dev, col, row);
}

/**
 * get_cursor_position() - get cursor position
 *
 * @priv:	private data of the video console
 * @row:	row
 * @col:	column
 */
static void get_cursor_position(struct vidconsole_priv *priv,
				int *row, int *col)
{
	struct vidconsole_ctx *ctx = vidconsole_ctx_from_priv(priv);

	*row = ctx->ycur / ctx->y_charsize;
	*col = VID_TO_PIXEL(ctx->xcur_frac - ctx->xstart_frac) /
	       ctx->x_charsize;
}

/*
 * Process a character while accumulating an escape string.  Chars are
 * accumulated into escape_buf until the end of escape sequence is
 * found, at which point the sequence is parsed and processed.
 */
static void vidconsole_escape_char(struct udevice *dev,
				   struct vidconsole_ctx *ctx, char ch)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ansi *ansi = &ctx->ansi;

	if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
		goto error;

	/* Sanity checking for bogus ESC sequences: */
	if (ansi->escape_len >= sizeof(ansi->escape_buf))
		goto error;
	if (ansi->escape_len == 0) {
		switch (ch) {
		case '7':
			/* Save cursor position */
			get_cursor_position(priv, &ansi->row_saved,
					    &ansi->col_saved);
			ansi->escape = 0;

			return;
		case '8': {
			/* Restore cursor position */
			int row = ansi->row_saved;
			int col = ansi->col_saved;

			set_cursor_position(dev, row, col);
			ansi->escape = 0;
			return;
		}
		case '[':
			break;
		default:
			goto error;
		}
	}

	ansi->escape_buf[ansi->escape_len++] = ch;

	/*
	 * Escape sequences are terminated by a letter, so keep
	 * accumulating until we get one:
	 */
	if (!isalpha(ch))
		return;

	/*
	 * clear escape mode first, otherwise things will get highly
	 * surprising if you hit any debug prints that come back to
	 * this console.
	 */
	ansi->escape = 0;

	switch (ch) {
	case 'A':
	case 'B':
	case 'C':
	case 'D':
	case 'E':
	case 'F': {
		int row, col, num;
		char *s = ansi->escape_buf;

		/*
		 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
		 * Cursor left/right: [%dD, [%dC
		 */
		s++;    /* [ */
		s = parsenum(s, &num);
		if (num == 0)			/* No digit in sequence ... */
			num = 1;		/* ... means "move by 1". */

		get_cursor_position(priv, &row, &col);
		if (ch == 'A' || ch == 'F')
			row -= num;
		if (ch == 'C')
			col += num;
		if (ch == 'D')
			col -= num;
		if (ch == 'B' || ch == 'E')
			row += num;
		if (ch == 'E' || ch == 'F')
			col = 0;
		if (col < 0)
			col = 0;
		if (row < 0)
			row = 0;
		/* Right and bottom overflows are handled in the callee. */
		set_cursor_position(dev, row, col);
		break;
	}
	case 'H':
	case 'f': {
		int row, col;
		char *s = ansi->escape_buf;

		/*
		 * Set cursor position: [%d;%df or [%d;%dH
		 */
		s++;    /* [ */
		s = parsenum(s, &row);
		s++;    /* ; */
		s = parsenum(s, &col);

		/*
		 * Video origin is [0, 0], terminal origin is [1, 1].
		 */
		if (row)
			--row;
		if (col)
			--col;

		set_cursor_position(dev, row, col);

		break;
	}
	case 'J': {
		int mode;

		/*
		 * Clear part/all screen:
		 *   [J or [0J - clear screen from cursor down
		 *   [1J       - clear screen from cursor up
		 *   [2J       - clear entire screen
		 *
		 * TODO we really only handle entire-screen case, others
		 * probably require some additions to video-uclass (and
		 * are not really needed yet by efi_console)
		 */
		parsenum(ansi->escape_buf + 1, &mode);

		if (mode == 2) {
			int ret;

			video_clear(dev->parent);
			ret = video_sync(dev->parent, false);
			if (ret) {
#ifdef DEBUG
				err_puts(true, "[vc err: video_sync]");
#endif
			}
			ctx->ycur = 0;
			ctx->xcur_frac = ctx->xstart_frac;
		} else {
			debug("unsupported clear mode: %d\n", mode);
		}
		break;
	}
	case 'K': {
		struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
		int mode;

		/*
		 * Clear (parts of) current line
		 *   [0K       - clear line to end
		 *   [2K       - clear entire line
		 */
		parsenum(ansi->escape_buf + 1, &mode);

		if (mode == 2) {
			int row, col;

			get_cursor_position(priv, &row, &col);
			vidconsole_set_row(dev, row, vid_priv->colour_bg);
		}
		break;
	}
	case 'm': {
		struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
		char *s = ansi->escape_buf;
		char *end = &ansi->escape_buf[ansi->escape_len];

		/*
		 * Set graphics mode: [%d;...;%dm
		 *
		 * Currently only supports the color attributes:
		 *
		 * Foreground Colors:
		 *
		 *   30	Black
		 *   31	Red
		 *   32	Green
		 *   33	Yellow
		 *   34	Blue
		 *   35	Magenta
		 *   36	Cyan
		 *   37	White
		 *
		 * Background Colors:
		 *
		 *   40	Black
		 *   41	Red
		 *   42	Green
		 *   43	Yellow
		 *   44	Blue
		 *   45	Magenta
		 *   46	Cyan
		 *   47	White
		 */

		s++;    /* [ */
		while (s < end) {
			int val;

			s = parsenum(s, &val);
			s++;

			switch (val) {
			case 0:
				/* all attributes off */
				video_set_default_colors(dev->parent, false);
				break;
			case 1:
				/* bold */
				vid_priv->fg_col_idx |= 8;
				vid_priv->colour_fg = video_index_to_colour(
						vid_priv, vid_priv->fg_col_idx);
				break;
			case 7:
				/* reverse video */
				vid_priv->colour_fg = video_index_to_colour(
						vid_priv, vid_priv->bg_col_idx);
				vid_priv->colour_bg = video_index_to_colour(
						vid_priv, vid_priv->fg_col_idx);
				break;
			case 30 ... 37:
				/* foreground color */
				vid_priv->fg_col_idx &= ~7;
				vid_priv->fg_col_idx |= val - 30;
				vid_priv->colour_fg = video_index_to_colour(
						vid_priv, vid_priv->fg_col_idx);
				break;
			case 40 ... 47:
				/* background color, also mask the bold bit */
				vid_priv->bg_col_idx &= ~0xf;
				vid_priv->bg_col_idx |= val - 40;
				vid_priv->colour_bg = video_index_to_colour(
						vid_priv, vid_priv->bg_col_idx);
				break;
			default:
				/* ignore unsupported SGR parameter */
				break;
			}
		}

		break;
	}
	default:
		debug("unrecognized escape sequence: %*s\n",
		      ansi->escape_len, ansi->escape_buf);
	}

	return;

error:
	/* something went wrong, just revert to normal mode: */
	ansi->escape = 0;
}

/* Put that actual character on the screen (using the UTF-32 code points). */
static int vidconsole_output_glyph(struct udevice *dev,
				   struct vidconsole_ctx *ctx, int ch)
{
	int ret;

	if (_DEBUG)
		err_printf(true, "glyph last_ch '%c': ch '%c' (%02x): ",
			   ctx->last_ch, ch >= ' ' ? ch : ' ', ch);

	/*
	 * Failure of this function normally indicates an unsupported
	 * colour depth. Check this and return an error to help with
	 * diagnosis.
	 */
	ret = vidconsole_putc_xy(dev, ctx, ctx->xcur_frac, ctx->ycur, ch);
	if (ret == -EAGAIN) {
		vidconsole_newline(dev, ctx);
		ret = vidconsole_putc_xy(dev, ctx, ctx->xcur_frac, ctx->ycur,
					 ch);
	}
	if (ret < 0)
		return ret;
	ctx->xcur_frac += ret;
	ctx->last_ch = ch;
	if (ctx->xcur_frac >= ctx->xsize_frac)
		vidconsole_newline(dev, ctx);
	cli_index_adjust(ctx, 1);

	return 0;
}

int vidconsole_put_char(struct udevice *dev, void *vctx, char ch)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx_from_priv(priv);
	struct vidconsole_ansi *ansi = &ctx->ansi;
	int cp, ret;

	/* Hide cursor to avoid artifacts */
	vidconsole_hide_cursor(dev, ctx);

	if (ansi->escape) {
		vidconsole_escape_char(dev, ctx, ch);
		return 0;
	}

	switch (ch) {
	case '\x1b':
		ansi->escape_len = 0;
		ansi->escape = 1;
		break;
	case '\a':
		/* beep */
		break;
	case '\r':
		ctx->xcur_frac = ctx->xstart_frac;
		break;
	case '\n':
		vidconsole_newline(dev, ctx);
		vidconsole_entry_start(dev, ctx);
		break;
	case '\t':	/* Tab (8 chars alignment) */
		ctx->xcur_frac = ((ctx->xcur_frac / ctx->tab_width_frac)
				+ 1) * ctx->tab_width_frac;

		if (ctx->xcur_frac >= ctx->xsize_frac)
			vidconsole_newline(dev, ctx);
		break;
	case '\b':
		vidconsole_back(dev, ctx);
		ctx->last_ch = 0;
		break;
	default:
		if (CONFIG_IS_ENABLED(CHARSET)) {
			cp = utf8_to_utf32_stream(ch, ctx->utf8_buf);
			if (cp == 0)
				return 0;
		} else {
			cp = ch;
		}
		ret = vidconsole_output_glyph(dev, ctx, cp);
		if (ret < 0)
			return ret;
		break;
	}

	return 0;
}

int vidconsole_put_stringn(struct udevice *dev, void *ctx, const char *str,
			   int maxlen)
{
	const char *s, *end = NULL;
	int ret;

	if (maxlen != -1)
		end = str + maxlen;
	for (s = str; *s && (maxlen == -1 || s < end); s++) {
		ret = vidconsole_put_char(dev, ctx, *s);
		if (ret)
			return ret;
	}

	return 0;
}

int vidconsole_put_string(struct udevice *dev, void *ctx, const char *str)
{
	return vidconsole_put_stringn(dev, ctx, str, -1);
}

static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
{
	struct udevice *dev = sdev->priv;
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	int ret;

	if (priv->quiet)
		return;
	ret = vidconsole_put_char(dev, NULL, ch);
	if (ret) {
#ifdef DEBUG
		err_puts(true, "[vc err: putc]");
#endif
	}
	ret = video_sync(dev->parent, false);
	if (ret) {
#ifdef DEBUG
		err_puts(true, "[vc err: video_sync]");
#endif
	}
}

static void vidconsole_putsn(struct stdio_dev *sdev, const char *s, int len)
{
	struct udevice *dev = sdev->priv;
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	int ret;

	if (priv->quiet)
		return;
	ret = vidconsole_put_stringn(dev, NULL, s, len);
	if (ret) {
#ifdef DEBUG
		char str[30];

		snprintf(str, sizeof(str), "[vc err: putsn %d]", ret);
		err_puts(true, str);
#endif
	}
	ret = video_sync(dev->parent, false);
	if (ret) {
#ifdef DEBUG
		err_puts(true, "[vc err: video_sync]");
#endif
	}
}

static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
{
	vidconsole_putsn(sdev, s, strlen(s));
}

void vidconsole_list_fonts(struct udevice *dev)
{
	struct vidfont_info info;
	int ret, i;

	for (i = 0, ret = 0; !ret; i++) {
		ret = vidconsole_get_font(dev, i, &info);
		if (!ret)
			printf("%s\n", info.name);
	}
}

int vidconsole_get_font(struct udevice *dev, int seq,
			struct vidfont_info *info)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ops->get_font)
		return -ENOSYS;

	return ops->get_font(dev, seq, info);
}

int vidconsole_get_font_size(struct udevice *dev, void *ctx, const char **name,
			     uint *sizep)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ctx)
		ctx = vidconsole_ctx(dev);
	if (!ops->get_font_size)
		return -ENOSYS;

	*name = ops->get_font_size(dev, ctx, sizep);
	return 0;
}

int vidconsole_select_font(struct udevice *dev, void *ctx, const char *name,
			   uint size)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (!ctx)
		ctx = vidconsole_ctx(dev);
	if (!ops->select_font)
		return -ENOSYS;

	return ops->select_font(dev, ctx, name, size);
}

int vidconsole_measure(struct udevice *dev, const char *name, uint size,
		       const char *text, int len, int limit,
		       struct vidconsole_bbox *bbox, struct alist *lines)
{
	struct vidconsole_ctx *ctx = vidconsole_ctx(dev);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
	int ret;

	if (ops->measure) {
		if (lines)
			alist_empty(lines);
		ret = ops->measure(dev, name, size, text, len, limit, bbox,
				   lines);
		if (ret != -ENOSYS)
			return ret;
	}

	bbox->valid = true;
	bbox->x0 = 0;
	bbox->y0 = 0;
	bbox->x1 = ctx->x_charsize * (len < 0 ? strlen(text) : len);
	bbox->y1 = ctx->y_charsize;

	return 0;
}

int vidconsole_nominal(struct udevice *dev, const char *name, uint size,
		       uint num_chars, struct vidconsole_bbox *bbox)
{
	struct vidconsole_ctx *ctx = vidconsole_ctx(dev);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
	int ret;

	if (ops->nominal) {
		ret = ops->nominal(dev, name, size, num_chars, bbox);
		if (ret != -ENOSYS)
			return ret;
	}

	bbox->valid = true;
	bbox->x0 = 0;
	bbox->y0 = 0;
	bbox->x1 = ctx->x_charsize * num_chars;
	bbox->y1 = ctx->y_charsize;

	return 0;
}

int vidconsole_ctx_new(struct udevice *dev, void **ctxp)
{
	struct udevice *vid = dev->parent;
	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
	struct vidconsole_uc_plat *plat = dev_get_uclass_plat(dev);
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
	struct vidconsole_ctx *ctx;
	int ret = -ENOMEM, size;
	void **ptr;

	if (!ops->ctx_new)
		return -ENOSYS;

	/* reserve space first so ctx_new failure doesn't need cleanup */
	ptr = alist_add_placeholder(&priv->ctx_list);
	if (!ptr)
		return -ENOMEM;

	size = plat->ctx_size ?: sizeof(struct vidconsole_ctx);
	ctx = calloc(1, size);
	if (!ctx)
		goto err_alloc;
	*ptr = ctx;

	if (CONFIG_IS_ENABLED(CURSOR) && xpl_phase() == PHASE_BOARD_R) {
		ret = console_alloc_cursor(dev, &ctx->curs);
		if (ret)
			goto err_curs;
	}

	ctx->xsize_frac = VID_TO_POS(vid_priv->xsize);

	ret = ops->ctx_new(dev, ctx);
	if (ret)
		goto err_new;
	*ctxp = ctx;

	return 0;

err_new:
	console_free_cursor(&ctx->curs);
err_curs:
err_alloc:
	priv->ctx_list.count--;
	free(ctx);

	return ret;
}

/**
 * vidconsole_free_ctx() - Free a context without removing it from the list
 *
 * This calls the driver's ctx_dispose method and frees the save_data, but
 * does not remove the context from the alist.
 *
 * @dev: Vidconsole device
 * @ctx: Context to free
 */
static void vidconsole_free_ctx(struct udevice *dev, struct vidconsole_ctx *ctx)
{
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	if (ops->ctx_dispose)
		ops->ctx_dispose(dev, ctx);
	console_free_cursor(&ctx->curs);
	free(ctx);
}

int vidconsole_ctx_dispose(struct udevice *dev, void *vctx)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx;
	void **ptr, **from;

	if (!ctx)
		return 0;

	/* remove the context from the list */
	alist_for_each_filter(ptr, from, &priv->ctx_list) {
		if (*ptr != ctx)
			*from++ = *ptr;
	}
	alist_update_end(&priv->ctx_list, from);

	vidconsole_free_ctx(dev, ctx);

	return 0;
}

#ifdef CONFIG_CURSOR
int vidconsole_show_cursor(struct udevice *dev, void *vctx)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx_from_priv(priv);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
	struct vidconsole_cursor *curs = &ctx->curs;
	int ret;

	/* find out where the cursor should be drawn */
	if (!ops->get_cursor_info)
		return -ENOSYS;

	ret = ops->get_cursor_info(dev, ctx);
	if (ret)
		return ret;

	/* If the driver stored cursor line and height, use them for drawing */
	if (curs->height) {
		struct udevice *vid = dev_get_parent(dev);
		struct video_priv *vid_priv = dev_get_uclass_priv(vid);

		/*
		 * avoid drawing off the display - we assume that the driver
		 * ensures that curs->y < vid_priv->ysize
		 */
		curs->height = min(curs->height, vid_priv->ysize - curs->y);

		ret = cursor_show(curs, vid_priv, NORMAL_DIRECTION);
		if (ret)
			return ret;

		/* Update display damage for cursor area */
		video_damage(vid, curs->x, curs->y, VIDCONSOLE_CURSOR_WIDTH,
			     curs->height);
	}

	curs->visible = true;

	return 0;
}

int vidconsole_hide_cursor(struct udevice *dev, void *vctx)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx_from_priv(priv);
	struct vidconsole_cursor *curs = &ctx->curs;
	int ret;

	if (!curs->visible)
		return 0;

	/* If the driver stored cursor line and height, use them for drawing */
	if (curs->height) {
		struct udevice *vid = dev_get_parent(dev);
		struct video_priv *vid_priv = dev_get_uclass_priv(vid);

		ret = cursor_hide(curs, vid_priv, NORMAL_DIRECTION);
		if (ret)
			return ret;

		/* Update display damage for cursor area */
		video_damage(vid, curs->x, curs->y, VIDCONSOLE_CURSOR_WIDTH,
			     curs->height);
	}

	curs->visible = false;

	return 0;
}
#endif /* CONFIG_CURSOR */

int vidconsole_mark_start(struct udevice *dev, void *vctx)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx_from_priv(priv);
	struct vidconsole_ops *ops = vidconsole_get_ops(dev);

	ctx->xmark_frac = ctx->xcur_frac;
	ctx->ymark = ctx->ycur;
	ctx->cli_index = 0;
	if (ops->mark_start) {
		int ret;

		ret = ops->mark_start(dev, ctx);
		if (ret != -ENOSYS)
			return ret;
	}

	return 0;
}

void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
			    enum colour_idx bg, struct vidconsole_colour *old)
{
	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);

	old->colour_fg = vid_priv->colour_fg;
	old->colour_bg = vid_priv->colour_bg;

	vid_priv->colour_fg = video_index_to_colour(vid_priv, fg);
	vid_priv->colour_bg = video_index_to_colour(vid_priv, bg);
}

void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old)
{
	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);

	vid_priv->colour_fg = old->colour_fg;
	vid_priv->colour_bg = old->colour_bg;
}

/* Set up the number of rows and colours (rotated drivers override this) */
static int vidconsole_pre_probe(struct udevice *dev)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);

	alist_init_struct(&priv->ctx_list, void *);

	return 0;
}

/* Register the device with stdio */
static int vidconsole_post_probe(struct udevice *dev)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct stdio_dev *sdev = &priv->sdev;
	struct vidconsole_ctx *ctx;
	int ret;

	ret = vidconsole_ctx_new(dev, (void **)&ctx);
	if (ret)
		return ret;
	priv->ctx = ctx;

	if (!ctx->tab_width_frac)
		ctx->tab_width_frac = VID_TO_POS(ctx->x_charsize) * 8;

	if (dev_seq(dev)) {
		snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
			 dev_seq(dev));
	} else {
		strcpy(sdev->name, "vidconsole");
	}

	sdev->flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_DM;
	sdev->putc = vidconsole_putc;
	sdev->puts = vidconsole_puts;
	sdev->putsn = vidconsole_putsn;
	sdev->priv = dev;

	return stdio_register(sdev);
}

static int vidconsole_pre_remove(struct udevice *dev)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	void **ptr;

	/* free all contexts in the list, including the default ctx */
	alist_for_each(ptr, &priv->ctx_list)
		vidconsole_free_ctx(dev, *ptr);
	alist_uninit(&priv->ctx_list);
	priv->ctx = NULL;

	return 0;
}

UCLASS_DRIVER(vidconsole) = {
	.id		= UCLASS_VIDEO_CONSOLE,
	.name		= "vidconsole0",
	.pre_probe	= vidconsole_pre_probe,
	.post_probe	= vidconsole_post_probe,
	.pre_remove	= vidconsole_pre_remove,
	.per_device_auto	= sizeof(struct vidconsole_priv),
	.per_device_plat_auto	= sizeof(struct vidconsole_uc_plat),
};

int vidconsole_clear_and_reset(struct udevice *dev)
{
	int ret;

	ret = video_clear(dev_get_parent(dev));
	if (ret)
		return ret;
	vidconsole_position_cursor(dev, 0, 0);

	return 0;
}

void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
{
	struct vidconsole_ctx *ctx = vidconsole_ctx(dev);
	struct udevice *vid_dev = dev->parent;
	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
	short x, y;

	x = min_t(short, col * ctx->x_charsize, vid_priv->xsize - 1);
	y = min_t(short, row * ctx->y_charsize, vid_priv->ysize - 1);
	vidconsole_set_cursor_pos(dev, NULL, x, y);
}

void vidconsole_set_quiet(struct udevice *dev, bool quiet)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);

	priv->quiet = quiet;
}

void vidconsole_set_bitmap_font(struct udevice *dev, struct vidconsole_ctx *ctx,
				struct video_fontdata *fontdata)
{
	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);

	log_debug("console_simple: setting %s font\n", fontdata->name);
	log_debug("width: %d\n", fontdata->width);
	log_debug("byte width: %d\n", fontdata->byte_width);
	log_debug("height: %d\n", fontdata->height);

	ctx->x_charsize = fontdata->width;
	ctx->y_charsize = fontdata->height;
	if (vid_priv->rot % 2) {
		ctx->cols = vid_priv->ysize / fontdata->width;
		ctx->rows = vid_priv->xsize / fontdata->height;
		ctx->xsize_frac = VID_TO_POS(vid_priv->ysize);
	} else {
		ctx->cols = vid_priv->xsize / fontdata->width;
		ctx->rows = vid_priv->ysize / fontdata->height;
		ctx->xsize_frac = VID_TO_POS(vid_priv->xsize);
	}
	ctx->xstart_frac = 0;
}

static void vidconsole_idle_ctx(struct udevice *dev, struct vidconsole_ctx *ctx)
{
	struct vidconsole_cursor *curs = &ctx->curs;

	/* Only handle cursor if it's enabled */
	if (curs->enabled && !curs->visible) {
		/*
		 * TODO(sjg@chromium.org): We are using a saved position here,
		 * but vidconsole_show_cursor() calls get_cursor_info() to
		 * recalc the position anyway.
		 */
		vidconsole_show_cursor(dev, ctx);
	}
}

void vidconsole_idle(struct udevice *dev)
{
	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
	struct vidconsole_ctx **ctxp;

	alist_for_each(ctxp, &priv->ctx_list)
		vidconsole_idle_ctx(dev, *ctxp);
}

#ifdef CONFIG_CURSOR
void vidconsole_readline_start(struct udevice *dev, void *vctx, bool indent)
{
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx(dev);

	ctx->curs.indent = indent;
	ctx->curs.enabled = true;
	vidconsole_mark_start(dev, ctx);
}

void vidconsole_readline_end(struct udevice *dev, void *vctx)
{
	struct vidconsole_ctx *ctx = vctx ?: vidconsole_ctx(dev);

	ctx->curs.enabled = false;
}

void vidconsole_readline_start_all(bool indent)
{
	struct uclass *uc;
	struct udevice *dev;

	uclass_id_foreach_dev(UCLASS_VIDEO_CONSOLE, dev, uc)
		vidconsole_readline_start(dev, NULL, indent);
}

void vidconsole_readline_end_all(void)
{
	struct uclass *uc;
	struct udevice *dev;

	uclass_id_foreach_dev(UCLASS_VIDEO_CONSOLE, dev, uc)
		vidconsole_readline_end(dev, NULL);
}
#endif /* CURSOR */

void *vidconsole_ctx(struct udevice *dev)
{
	struct vidconsole_priv *uc_priv = dev_get_uclass_priv(dev);

	return uc_priv->ctx;
}