tty.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * arch/i386/boot/tty.c
  12. *
  13. * Very simple screen I/O
  14. * XXX: Probably should add very simple serial I/O?
  15. */
  16. #include "boot.h"
  17. /*
  18. * These functions are in .inittext so they can be used to signal
  19. * error during initialization.
  20. */
  21. void __attribute__((section(".inittext"))) putchar(int ch)
  22. {
  23. unsigned char c = ch;
  24. if (c == '\n')
  25. putchar('\r'); /* \n -> \r\n */
  26. /* int $0x10 is known to have bugs involving touching registers
  27. it shouldn't. Be extra conservative... */
  28. asm volatile("pushal; pushw %%ds; int $0x10; popw %%ds; popal"
  29. : : "b" (0x0007), "c" (0x0001), "a" (0x0e00|ch));
  30. }
  31. void __attribute__((section(".inittext"))) puts(const char *str)
  32. {
  33. int n = 0;
  34. while (*str) {
  35. putchar(*str++);
  36. n++;
  37. }
  38. }
  39. /*
  40. * Read the CMOS clock through the BIOS, and return the
  41. * seconds in BCD.
  42. */
  43. static u8 gettime(void)
  44. {
  45. u16 ax = 0x0200;
  46. u16 cx, dx;
  47. asm volatile("int $0x1a"
  48. : "+a" (ax), "=c" (cx), "=d" (dx)
  49. : : "ebx", "esi", "edi");
  50. return dx >> 8;
  51. }
  52. /*
  53. * Read from the keyboard
  54. */
  55. int getchar(void)
  56. {
  57. u16 ax = 0;
  58. asm volatile("int $0x16" : "+a" (ax));
  59. return ax & 0xff;
  60. }
  61. static int kbd_pending(void)
  62. {
  63. u8 pending;
  64. asm volatile("int $0x16; setnz %0"
  65. : "=rm" (pending)
  66. : "a" (0x0100));
  67. return pending;
  68. }
  69. void kbd_flush(void)
  70. {
  71. for (;;) {
  72. if (!kbd_pending())
  73. break;
  74. getchar();
  75. }
  76. }
  77. int getchar_timeout(void)
  78. {
  79. int cnt = 30;
  80. int t0, t1;
  81. t0 = gettime();
  82. while (cnt) {
  83. if (kbd_pending())
  84. return getchar();
  85. t1 = gettime();
  86. if (t0 != t1) {
  87. cnt--;
  88. t0 = t1;
  89. }
  90. }
  91. return 0; /* Timeout! */
  92. }