sandbox.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /*
  7. * This provide a test serial port. It provides an emulated serial port where
  8. * a test program and read out the serial output and inject serial input for
  9. * U-Boot.
  10. */
  11. #include <common.h>
  12. #include <lcd.h>
  13. #include <os.h>
  14. #include <serial.h>
  15. #include <linux/compiler.h>
  16. #include <asm/state.h>
  17. /*
  18. *
  19. * serial_buf: A buffer that holds keyboard characters for the
  20. * Sandbox U-boot.
  21. *
  22. * invariants:
  23. * serial_buf_write == serial_buf_read -> empty buffer
  24. * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
  25. */
  26. static char serial_buf[16];
  27. static unsigned int serial_buf_write;
  28. static unsigned int serial_buf_read;
  29. static int sandbox_serial_init(void)
  30. {
  31. struct sandbox_state *state = state_get_current();
  32. if (state->term_raw != STATE_TERM_COOKED)
  33. os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
  34. return 0;
  35. }
  36. static void sandbox_serial_setbrg(void)
  37. {
  38. }
  39. static void sandbox_serial_putc(const char ch)
  40. {
  41. os_write(1, &ch, 1);
  42. }
  43. static void sandbox_serial_puts(const char *str)
  44. {
  45. os_write(1, str, strlen(str));
  46. }
  47. static unsigned int increment_buffer_index(unsigned int index)
  48. {
  49. return (index + 1) % ARRAY_SIZE(serial_buf);
  50. }
  51. static int sandbox_serial_tstc(void)
  52. {
  53. const unsigned int next_index =
  54. increment_buffer_index(serial_buf_write);
  55. ssize_t count;
  56. os_usleep(100);
  57. #ifdef CONFIG_LCD
  58. lcd_sync();
  59. #endif
  60. if (next_index == serial_buf_read)
  61. return 1; /* buffer full */
  62. count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
  63. if (count == 1)
  64. serial_buf_write = next_index;
  65. return serial_buf_write != serial_buf_read;
  66. }
  67. static int sandbox_serial_getc(void)
  68. {
  69. int result;
  70. while (!sandbox_serial_tstc())
  71. ; /* buffer empty */
  72. result = serial_buf[serial_buf_read];
  73. serial_buf_read = increment_buffer_index(serial_buf_read);
  74. return result;
  75. }
  76. static struct serial_device sandbox_serial_drv = {
  77. .name = "sandbox_serial",
  78. .start = sandbox_serial_init,
  79. .stop = NULL,
  80. .setbrg = sandbox_serial_setbrg,
  81. .putc = sandbox_serial_putc,
  82. .puts = sandbox_serial_puts,
  83. .getc = sandbox_serial_getc,
  84. .tstc = sandbox_serial_tstc,
  85. };
  86. void sandbox_serial_initialize(void)
  87. {
  88. serial_register(&sandbox_serial_drv);
  89. }
  90. __weak struct serial_device *default_serial_console(void)
  91. {
  92. return &sandbox_serial_drv;
  93. }