serial.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <modules/serial.h>
  23. #include <arch/io.h>
  24. #include <arch/types.h>
  25. int serial_putc(char);
  26. struct tty serial0_tty = {
  27. .dev_name = "ttyS0",
  28. .priv_data = COM0,
  29. .flags = 0,
  30. .putc = serial_putc,
  31. .getc = NULL
  32. };
  33. static int com_init(__io_t p) {
  34. outb(0x80, p + 3);
  35. outb(0x30, p + 0);
  36. outb(0x00, p + 1);
  37. outb(0x03, p + 3);
  38. outb(0x0b, p + 4);
  39. outb(0x0d, p + 1);
  40. inb(p);
  41. return 0;
  42. }
  43. /* as i know, this only runs on x86 */
  44. int __init init_serial(void) {
  45. com_init(COM0);
  46. return tty_register(&serial0_tty);
  47. }
  48. int serial_write(__io_t p, char c) {
  49. while (!(inb(p + 5) & 0x20));
  50. outb(c, p);
  51. return 0;
  52. }
  53. int serial_putc(char c) {
  54. return serial_write(COM0, c);
  55. }
  56. module_init(init_serial);