video.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <pci.h>
  25. #include <stdio_dev.h>
  26. #include <i8042.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/realmode.h>
  29. #include <asm/io.h>
  30. #include <asm/pci.h>
  31. /* basic textmode I/O from linux kernel */
  32. static char *vidmem = (char *)0xb8000;
  33. static int vidport;
  34. static int lines, cols;
  35. static int orig_x, orig_y;
  36. static void beep(int dur)
  37. {
  38. int i;
  39. outb_p(3, 0x61);
  40. for (i = 0; i < 10*dur; i++)
  41. udelay(1000);
  42. outb_p(0, 0x61);
  43. }
  44. static void scroll(void)
  45. {
  46. int i;
  47. memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
  48. for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
  49. vidmem[i] = ' ';
  50. }
  51. static void __video_putc(const char c, int *x, int *y)
  52. {
  53. if (c == '\n') {
  54. (*x) = 0;
  55. if (++(*y) >= lines) {
  56. scroll();
  57. (*y)--;
  58. }
  59. } else if (c == '\b') {
  60. if ((*x) != 0) {
  61. --(*x);
  62. vidmem[((*x) + cols * (*y)) * 2] = ' ';
  63. }
  64. } else if (c == '\r') {
  65. (*x) = 0;
  66. } else if (c == '\a') {
  67. beep(3);
  68. } else if (c == '\t') {
  69. __video_putc(' ', x, y);
  70. __video_putc(' ', x, y);
  71. __video_putc(' ', x, y);
  72. __video_putc(' ', x, y);
  73. __video_putc(' ', x, y);
  74. __video_putc(' ', x, y);
  75. __video_putc(' ', x, y);
  76. __video_putc(' ', x, y);
  77. } else if (c == '\v') {
  78. switch ((*x) % 8) {
  79. case 0:
  80. __video_putc(' ', x, y);
  81. case 7:
  82. __video_putc(' ', x, y);
  83. case 6:
  84. __video_putc(' ', x, y);
  85. case 5:
  86. __video_putc(' ', x, y);
  87. case 4:
  88. __video_putc(' ', x, y);
  89. case 3:
  90. __video_putc(' ', x, y);
  91. case 2:
  92. __video_putc(' ', x, y);
  93. case 1:
  94. __video_putc(' ', x, y);
  95. }
  96. } else if (c == '\f') {
  97. int i;
  98. for (i = 0; i < lines * cols * 2; i += 2)
  99. vidmem[i] = 0;
  100. (*x) = 0;
  101. (*y) = 0;
  102. } else {
  103. vidmem[((*x) + cols * (*y)) * 2] = c;
  104. if (++(*x) >= cols) {
  105. (*x) = 0;
  106. if (++(*y) >= lines) {
  107. scroll();
  108. (*y)--;
  109. }
  110. }
  111. }
  112. }
  113. static void video_putc(const char c)
  114. {
  115. int x,y,pos;
  116. x = orig_x;
  117. y = orig_y;
  118. __video_putc(c, &x, &y);
  119. orig_x = x;
  120. orig_y = y;
  121. pos = (x + cols * y) * 2; /* Update cursor position */
  122. outb_p(14, vidport);
  123. outb_p(0xff & (pos >> 9), vidport+1);
  124. outb_p(15, vidport);
  125. outb_p(0xff & (pos >> 1), vidport+1);
  126. }
  127. static void video_puts(const char *s)
  128. {
  129. int x,y,pos;
  130. char c;
  131. x = orig_x;
  132. y = orig_y;
  133. while ((c = *s++) != '\0')
  134. __video_putc(c, &x, &y);
  135. orig_x = x;
  136. orig_y = y;
  137. pos = (x + cols * y) * 2; /* Update cursor position */
  138. outb_p(14, vidport);
  139. outb_p(0xff & (pos >> 9), vidport+1);
  140. outb_p(15, vidport);
  141. outb_p(0xff & (pos >> 1), vidport+1);
  142. }
  143. int video_init(void)
  144. {
  145. u16 pos;
  146. static struct stdio_dev vga_dev;
  147. static struct stdio_dev kbd_dev;
  148. vidmem = (char *) 0xb8000;
  149. vidport = 0x3d4;
  150. lines = 25;
  151. cols = 80;
  152. outb_p(14, vidport);
  153. pos = inb_p(vidport+1);
  154. pos <<= 8;
  155. outb_p(15, vidport);
  156. pos |= inb_p(vidport+1);
  157. orig_x = pos%cols;
  158. orig_y = pos/cols;
  159. #if 0
  160. printf("pos %x %d %d\n", pos, orig_x, orig_y);
  161. #endif
  162. if (orig_y > lines)
  163. orig_x = orig_y =0;
  164. memset(&vga_dev, 0, sizeof(vga_dev));
  165. strcpy(vga_dev.name, "vga");
  166. vga_dev.ext = 0;
  167. vga_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
  168. vga_dev.putc = video_putc; /* 'putc' function */
  169. vga_dev.puts = video_puts; /* 'puts' function */
  170. vga_dev.tstc = NULL; /* 'tstc' function */
  171. vga_dev.getc = NULL; /* 'getc' function */
  172. if (stdio_register(&vga_dev) == 0)
  173. return 1;
  174. if (i8042_kbd_init())
  175. return 1;
  176. memset(&kbd_dev, 0, sizeof(kbd_dev));
  177. strcpy(kbd_dev.name, "kbd");
  178. kbd_dev.ext = 0;
  179. kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  180. kbd_dev.putc = NULL; /* 'putc' function */
  181. kbd_dev.puts = NULL; /* 'puts' function */
  182. kbd_dev.tstc = i8042_tstc; /* 'tstc' function */
  183. kbd_dev.getc = i8042_getc; /* 'getc' function */
  184. if (stdio_register(&kbd_dev) == 0)
  185. return 1;
  186. return 0;
  187. }
  188. int drv_video_init(void)
  189. {
  190. if (video_bios_init())
  191. return 1;
  192. return video_init();
  193. }