console.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Device-independent console interface.
  19. */
  20. #ifndef KERN_CONSOLE_H
  21. #define KERN_CONSOLE_H
  22. #include <kern/cbuf.h>
  23. #include <kern/init.h>
  24. #include <kern/list.h>
  25. #include <kern/spinlock.h>
  26. #include <kern/thread.h>
  27. #define CONSOLE_SCROLL_UP 0x12 // DC2
  28. #define CONSOLE_SCROLL_DOWN 0x14 // DC4
  29. struct console;
  30. struct console_ops
  31. {
  32. void (*puts) (struct console *, const char *, size_t);
  33. };
  34. #define CONSOLE_BUF_SIZE 64
  35. #define CONSOLE_NAME_SIZE 16
  36. /*
  37. * Console device.
  38. *
  39. * This structure should be embedded in the hardware-specific console
  40. * objects. Calls to console operations are all serialized by this module
  41. * for each device. Interrupts are disabled when calling operations.
  42. */
  43. struct console
  44. {
  45. struct spinlock lock;
  46. const struct console_ops *ops;
  47. char buffer[CONSOLE_BUF_SIZE];
  48. struct cbuf recvbuf;
  49. struct list waiters;
  50. struct list node;
  51. char name[CONSOLE_NAME_SIZE];
  52. };
  53. // Console initialization.
  54. void console_init (struct console *console, const char *name,
  55. const struct console_ops *ops);
  56. /*
  57. * Register a console device.
  58. *
  59. * The given console must be initialized before calling this function.
  60. *
  61. * This function isn't thread-safe and can only be called during system
  62. * initialization.
  63. */
  64. void console_register (struct console *console);
  65. /*
  66. * Console interrupt handler.
  67. *
  68. * This function is meant to be used by low-level drivers to fill the
  69. * receive buffer.
  70. *
  71. * Interrupts must be disabled when calling this function.
  72. */
  73. void console_intr (struct console *console, const char *s);
  74. /*
  75. * Write/read a single character to all registered console devices.
  76. *
  77. * Writing may not block in order to allow printf functions to be used in any
  78. * context. Reading may block waiting for input.
  79. */
  80. void console_putchar (char c);
  81. char console_getchar (void);
  82. /*
  83. * Write/read a block of characters. These functions come in 2 versions: Those
  84. * that acquire the lock before and release it after, and those that don't.
  85. */
  86. void console_puts (const char *s, size_t size);
  87. size_t console_gets (char *s, size_t size);
  88. void console_puts_nolock (const char *s, size_t size);
  89. size_t console_gets_nolock (char *s, size_t size);
  90. // Acquire an exclusive lock on the console device.
  91. void console_lock (cpu_flags_t *flags);
  92. // Release the lock on the console.
  93. void console_unlock (cpu_flags_t flags);
  94. /*
  95. * This init operation provides :
  96. * - registration of consoles
  97. */
  98. INIT_OP_DECLARE (console_bootstrap);
  99. /*
  100. * This init operation provides :
  101. * - all consoles have been registered
  102. * - module fully initialized
  103. */
  104. INIT_OP_DECLARE (console_setup);
  105. #endif