tty.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Very simple screen and serial I/O
  13. */
  14. #include "boot.h"
  15. int early_serial_base;
  16. #define XMTRDY 0x20
  17. #define TXR 0 /* Transmit register (WRITE) */
  18. #define LSR 5 /* Line Status */
  19. /*
  20. * These functions are in .inittext so they can be used to signal
  21. * error during initialization.
  22. */
  23. static void __attribute__((section(".inittext"))) serial_putchar(int ch)
  24. {
  25. unsigned timeout = 0xffff;
  26. while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
  27. cpu_relax();
  28. outb(ch, early_serial_base + TXR);
  29. }
  30. static void __attribute__((section(".inittext"))) bios_putchar(int ch)
  31. {
  32. struct biosregs ireg;
  33. initregs(&ireg);
  34. ireg.bx = 0x0007;
  35. ireg.cx = 0x0001;
  36. ireg.ah = 0x0e;
  37. ireg.al = ch;
  38. intcall(0x10, &ireg, NULL);
  39. }
  40. void __attribute__((section(".inittext"))) putchar(int ch)
  41. {
  42. if (ch == '\n')
  43. putchar('\r'); /* \n -> \r\n */
  44. bios_putchar(ch);
  45. if (early_serial_base != 0)
  46. serial_putchar(ch);
  47. }
  48. void __attribute__((section(".inittext"))) puts(const char *str)
  49. {
  50. while (*str)
  51. putchar(*str++);
  52. }
  53. /*
  54. * Read the CMOS clock through the BIOS, and return the
  55. * seconds in BCD.
  56. */
  57. static u8 gettime(void)
  58. {
  59. struct biosregs ireg, oreg;
  60. initregs(&ireg);
  61. ireg.ah = 0x02;
  62. intcall(0x1a, &ireg, &oreg);
  63. return oreg.dh;
  64. }
  65. /*
  66. * Read from the keyboard
  67. */
  68. int getchar(void)
  69. {
  70. struct biosregs ireg, oreg;
  71. initregs(&ireg);
  72. /* ireg.ah = 0x00; */
  73. intcall(0x16, &ireg, &oreg);
  74. return oreg.al;
  75. }
  76. static int kbd_pending(void)
  77. {
  78. struct biosregs ireg, oreg;
  79. initregs(&ireg);
  80. ireg.ah = 0x01;
  81. intcall(0x16, &ireg, &oreg);
  82. return !(oreg.eflags & X86_EFLAGS_ZF);
  83. }
  84. void kbd_flush(void)
  85. {
  86. for (;;) {
  87. if (!kbd_pending())
  88. break;
  89. getchar();
  90. }
  91. }
  92. int getchar_timeout(void)
  93. {
  94. int cnt = 30;
  95. int t0, t1;
  96. t0 = gettime();
  97. while (cnt) {
  98. if (kbd_pending())
  99. return getchar();
  100. t1 = gettime();
  101. if (t0 != t1) {
  102. cnt--;
  103. t0 = t1;
  104. }
  105. }
  106. return 0; /* Timeout! */
  107. }