sandbox.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. /*
  22. * This provide a test serial port. It provides an emulated serial port where
  23. * a test program and read out the serial output and inject serial input for
  24. * U-Boot.
  25. */
  26. #include <common.h>
  27. #include <os.h>
  28. #include <serial.h>
  29. #include <linux/compiler.h>
  30. /*
  31. *
  32. * serial_buf: A buffer that holds keyboard characters for the
  33. * Sandbox U-boot.
  34. *
  35. * invariants:
  36. * serial_buf_write == serial_buf_read -> empty buffer
  37. * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
  38. */
  39. static char serial_buf[16];
  40. static unsigned int serial_buf_write;
  41. static unsigned int serial_buf_read;
  42. static int sandbox_serial_init(void)
  43. {
  44. os_tty_raw(0);
  45. return 0;
  46. }
  47. static void sandbox_serial_setbrg(void)
  48. {
  49. }
  50. static void sandbox_serial_putc(const char ch)
  51. {
  52. os_write(1, &ch, 1);
  53. }
  54. static void sandbox_serial_puts(const char *str)
  55. {
  56. os_write(1, str, strlen(str));
  57. }
  58. static unsigned int increment_buffer_index(unsigned int index)
  59. {
  60. return (index + 1) % ARRAY_SIZE(serial_buf);
  61. }
  62. static int sandbox_serial_tstc(void)
  63. {
  64. const unsigned int next_index =
  65. increment_buffer_index(serial_buf_write);
  66. ssize_t count;
  67. os_usleep(100);
  68. if (next_index == serial_buf_read)
  69. return 1; /* buffer full */
  70. count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
  71. if (count == 1)
  72. serial_buf_write = next_index;
  73. return serial_buf_write != serial_buf_read;
  74. }
  75. static int sandbox_serial_getc(void)
  76. {
  77. int result;
  78. while (!sandbox_serial_tstc())
  79. ; /* buffer empty */
  80. result = serial_buf[serial_buf_read];
  81. serial_buf_read = increment_buffer_index(serial_buf_read);
  82. return result;
  83. }
  84. static struct serial_device sandbox_serial_drv = {
  85. .name = "sandbox_serial",
  86. .start = sandbox_serial_init,
  87. .stop = NULL,
  88. .setbrg = sandbox_serial_setbrg,
  89. .putc = sandbox_serial_putc,
  90. .puts = sandbox_serial_puts,
  91. .getc = sandbox_serial_getc,
  92. .tstc = sandbox_serial_tstc,
  93. };
  94. void sandbox_serial_initialize(void)
  95. {
  96. serial_register(&sandbox_serial_drv);
  97. }
  98. __weak struct serial_device *default_serial_console(void)
  99. {
  100. return &sandbox_serial_drv;
  101. }