tty.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Very simple screen I/O
  12. * XXX: Probably should add very simple serial I/O?
  13. */
  14. #include "boot.h"
  15. /*
  16. * These functions are in .inittext so they can be used to signal
  17. * error during initialization.
  18. */
  19. void __attribute__((section(".inittext"))) putchar(int ch)
  20. {
  21. unsigned char c = ch;
  22. if (c == '\n')
  23. putchar('\r'); /* \n -> \r\n */
  24. /* int $0x10 is known to have bugs involving touching registers
  25. it shouldn't. Be extra conservative... */
  26. asm volatile("pushal; pushw %%ds; int $0x10; popw %%ds; popal"
  27. : : "b" (0x0007), "c" (0x0001), "a" (0x0e00|ch));
  28. }
  29. void __attribute__((section(".inittext"))) puts(const char *str)
  30. {
  31. int n = 0;
  32. while (*str) {
  33. putchar(*str++);
  34. n++;
  35. }
  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. u16 ax = 0x0200;
  44. u16 cx, dx;
  45. asm volatile("int $0x1a"
  46. : "+a" (ax), "=c" (cx), "=d" (dx)
  47. : : "ebx", "esi", "edi");
  48. return dx >> 8;
  49. }
  50. /*
  51. * Read from the keyboard
  52. */
  53. int getchar(void)
  54. {
  55. u16 ax = 0;
  56. asm volatile("int $0x16" : "+a" (ax));
  57. return ax & 0xff;
  58. }
  59. static int kbd_pending(void)
  60. {
  61. u8 pending;
  62. asm volatile("int $0x16; setnz %0"
  63. : "=qm" (pending)
  64. : "a" (0x0100));
  65. return pending;
  66. }
  67. void kbd_flush(void)
  68. {
  69. for (;;) {
  70. if (!kbd_pending())
  71. break;
  72. getchar();
  73. }
  74. }
  75. int getchar_timeout(void)
  76. {
  77. int cnt = 30;
  78. int t0, t1;
  79. t0 = gettime();
  80. while (cnt) {
  81. if (kbd_pending())
  82. return getchar();
  83. t1 = gettime();
  84. if (t0 != t1) {
  85. cnt--;
  86. t0 = t1;
  87. }
  88. }
  89. return 0; /* Timeout! */
  90. }