tty.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 I/O
  13. * XXX: Probably should add very simple serial I/O?
  14. */
  15. #include "boot.h"
  16. /*
  17. * These functions are in .inittext so they can be used to signal
  18. * error during initialization.
  19. */
  20. void __attribute__((section(".inittext"))) putchar(int ch)
  21. {
  22. struct biosregs ireg;
  23. if (ch == '\n')
  24. putchar('\r'); /* \n -> \r\n */
  25. initregs(&ireg);
  26. ireg.bx = 0x0007;
  27. ireg.cx = 0x0001;
  28. ireg.ah = 0x0e;
  29. ireg.al = ch;
  30. intcall(0x10, &ireg, NULL);
  31. }
  32. void __attribute__((section(".inittext"))) puts(const char *str)
  33. {
  34. while (*str)
  35. putchar(*str++);
  36. }
  37. /*
  38. * Read the CMOS clock through the BIOS, and return the
  39. * seconds in BCD.
  40. */
  41. static u8 gettime(void)
  42. {
  43. struct biosregs ireg, oreg;
  44. initregs(&ireg);
  45. ireg.ah = 0x02;
  46. intcall(0x1a, &ireg, &oreg);
  47. return oreg.dh;
  48. }
  49. /*
  50. * Read from the keyboard
  51. */
  52. int getchar(void)
  53. {
  54. struct biosregs ireg, oreg;
  55. initregs(&ireg);
  56. /* ireg.ah = 0x00; */
  57. intcall(0x16, &ireg, &oreg);
  58. return oreg.al;
  59. }
  60. static int kbd_pending(void)
  61. {
  62. struct biosregs ireg, oreg;
  63. initregs(&ireg);
  64. ireg.ah = 0x01;
  65. intcall(0x16, &ireg, &oreg);
  66. return !(oreg.eflags & X86_EFLAGS_ZF);
  67. }
  68. void kbd_flush(void)
  69. {
  70. for (;;) {
  71. if (!kbd_pending())
  72. break;
  73. getchar();
  74. }
  75. }
  76. int getchar_timeout(void)
  77. {
  78. int cnt = 30;
  79. int t0, t1;
  80. t0 = gettime();
  81. while (cnt) {
  82. if (kbd_pending())
  83. return getchar();
  84. t1 = gettime();
  85. if (t0 != t1) {
  86. cnt--;
  87. t0 = t1;
  88. }
  89. }
  90. return 0; /* Timeout! */
  91. }