vidconsole-uclass.c 15 KB

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