serial.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* modules/console.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <sys/types.h>
  19. #include <kernel/init.h>
  20. #include <kernel/module.h>
  21. #include <kernel/tty.h>
  22. #include <kernel/kmlog.h>
  23. #include <modules/serial.h>
  24. #include <arch/io.h>
  25. #include <arch/types.h>
  26. int serial_putc(char);
  27. static int put_kmlog(void);
  28. struct tty serial0_tty = {
  29. .dev_name = "ttyS0",
  30. .priv_data = COM0,
  31. .flags = 0,
  32. .putc = serial_putc,
  33. .getc = NULL
  34. };
  35. static int com_init(__io_t p) {
  36. outb(0x80, p + 3);
  37. outb(0x30, p + 0);
  38. outb(0x00, p + 1);
  39. outb(0x03, p + 3);
  40. outb(0x0b, p + 4);
  41. outb(0x0d, p + 1);
  42. inb(p);
  43. return 0;
  44. }
  45. /* as i know, this only runs on x86 */
  46. int __init init_serial(void) {
  47. com_init(COM0);
  48. put_kmlog();
  49. return tty_register(&serial0_tty);
  50. }
  51. int serial_write(__io_t p, char c) {
  52. while (!(inb(p + 5) & 0x20));
  53. outb(c, p);
  54. return 0;
  55. }
  56. int serial_putc(char c) {
  57. if (c == '\n')
  58. serial_write(COM0, '\r');
  59. return serial_write(COM0, c);
  60. }
  61. static int put_kmlog(void) {
  62. char *s;
  63. for (s = kmlog_buf; *s; s++) {
  64. if (s > kmlog_buf + KMLOG_SIZE)
  65. s = kmlog_buf;
  66. serial_putc(*s);
  67. }
  68. return 0;
  69. }
  70. module_init(init_serial);