You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

console.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Central console switch. Various console devices can be selected
  3. * via the build options CONSOLE_FIRMWARE, CONSOLE_SERIAL etc.
  4. * config.c picks up on these definitions and drags in the relevant
  5. * objects. The linker compiles the console_drivers table for us; we
  6. * simply delegate to each console_driver that we find in the table.
  7. *
  8. * Doing it this way allows for changing CONSOLE_XXX without
  9. * rebuilding anything other than config.o. This is extremely useful
  10. * for rom-o-matic.
  11. */
  12. #include "stddef.h"
  13. #include "console.h"
  14. /* FIXME: we need a cleaner way to pick up cpu_nap(). It makes a
  15. * real-mode call, and so we don't want to use it with LinuxBIOS.
  16. */
  17. #include "bios.h"
  18. extern struct console_driver console_drivers[];
  19. extern struct console_driver console_drivers_end[];
  20. /*****************************************************************************
  21. * putchar : write a single character to each console
  22. *****************************************************************************
  23. */
  24. void putchar ( int character ) {
  25. struct console_driver *console;
  26. /* Automatic LF -> CR,LF translation */
  27. if ( character == '\n' )
  28. putchar ( '\r' );
  29. for ( console = console_drivers; console < console_drivers_end ;
  30. console++ ) {
  31. if ( ( ! console->disabled ) && console->putchar )
  32. console->putchar ( character );
  33. }
  34. }
  35. /*****************************************************************************
  36. * has_input : check to see if any input is available on any console,
  37. * and return a pointer to the console device if so
  38. *****************************************************************************
  39. */
  40. static struct console_driver * has_input ( void ) {
  41. struct console_driver *console;
  42. for ( console = console_drivers; console < console_drivers_end ;
  43. console++ ) {
  44. if ( ( ! console->disabled ) && console->iskey ) {
  45. if ( console->iskey () )
  46. return console;
  47. }
  48. }
  49. return NULL;
  50. }
  51. /*****************************************************************************
  52. * getchar : read a single character from any console
  53. *
  54. * NOTE : this function does not echo the character, and it does block
  55. *****************************************************************************
  56. */
  57. int getchar ( void ) {
  58. struct console_driver *console;
  59. int character = 256;
  60. while ( character == 256 ) {
  61. /* Doze for a while (until the next interrupt). This works
  62. * fine, because the keyboard is interrupt-driven, and the
  63. * timer interrupt (approx. every 50msec) takes care of the
  64. * serial port, which is read by polling. This reduces the
  65. * power dissipation of a modern CPU considerably, and also
  66. * makes Etherboot waiting for user interaction waste a lot
  67. * less CPU time in a VMware session.
  68. */
  69. cpu_nap();
  70. console = has_input();
  71. if ( console && console->getchar )
  72. character = console->getchar ();
  73. }
  74. /* CR -> LF translation */
  75. if ( character == '\r' )
  76. character = '\n';
  77. return character;
  78. }
  79. /*****************************************************************************
  80. * iskey : check to see if any input is available on any console
  81. *****************************************************************************
  82. */
  83. int iskey ( void ) {
  84. return has_input() ? 1 : 0;
  85. }