vidconsole-uclass.c 15 KB

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