video.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <devices.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. }
  43. outb_p(0, 0x61);
  44. }
  45. static void scroll(void)
  46. {
  47. int i;
  48. memcpy ( vidmem, vidmem + cols * 2, ( lines - 1 ) * cols * 2 );
  49. for ( i = ( lines - 1 ) * cols * 2; i < lines * cols * 2; i += 2 )
  50. vidmem[i] = ' ';
  51. }
  52. static void __video_putc(const char c, int *x, int *y)
  53. {
  54. if (c == '\n') {
  55. (*x) = 0;
  56. if ( ++(*y) >= lines ) {
  57. scroll();
  58. (*y)--;
  59. }
  60. } else if (c == '\b') {
  61. if ((*x) != 0) {
  62. --(*x);
  63. vidmem [ ( (*x) + cols * (*y) ) * 2 ] = ' ';
  64. }
  65. } else if (c == '\r') {
  66. (*x) = 0;
  67. } else if (c == '\a') {
  68. beep(3);
  69. } else if (c == '\t') {
  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. __video_putc(' ', x, y);
  78. } else if (c == '\v') {
  79. switch ((*x) % 8) {
  80. case 0:
  81. __video_putc(' ', x, y);
  82. case 7:
  83. __video_putc(' ', x, y);
  84. case 6:
  85. __video_putc(' ', x, y);
  86. case 5:
  87. __video_putc(' ', x, y);
  88. case 4:
  89. __video_putc(' ', x, y);
  90. case 3:
  91. __video_putc(' ', x, y);
  92. case 2:
  93. __video_putc(' ', x, y);
  94. case 1:
  95. __video_putc(' ', x, y);
  96. }
  97. } else if (c == '\f') {
  98. int i;
  99. for (i=0;i<lines*cols*2;i+=2) {
  100. vidmem[i] = 0;
  101. }
  102. (*x) = 0;
  103. (*y) = 0;
  104. } else {
  105. vidmem [ ( (*x) + cols * (*y) ) * 2 ] = c;
  106. if ( ++(*x) >= cols ) {
  107. (*x) = 0;
  108. if ( ++(*y) >= lines ) {
  109. scroll();
  110. (*y)--;
  111. }
  112. }
  113. }
  114. }
  115. static void video_putc(const char c)
  116. {
  117. int x,y,pos;
  118. x = orig_x;
  119. y = orig_y;
  120. __video_putc(c, &x, &y);
  121. orig_x = x;
  122. orig_y = y;
  123. pos = (x + cols * y) * 2; /* Update cursor position */
  124. outb_p(14, vidport);
  125. outb_p(0xff & (pos >> 9), vidport+1);
  126. outb_p(15, vidport);
  127. outb_p(0xff & (pos >> 1), vidport+1);
  128. }
  129. static void video_puts(const char *s)
  130. {
  131. int x,y,pos;
  132. char c;
  133. x = orig_x;
  134. y = orig_y;
  135. while ( ( c = *s++ ) != '\0' ) {
  136. __video_putc(c, &x, &y);
  137. }
  138. orig_x = x;
  139. orig_y = y;
  140. pos = (x + cols * y) * 2; /* Update cursor position */
  141. outb_p(14, vidport);
  142. outb_p(0xff & (pos >> 9), vidport+1);
  143. outb_p(15, vidport);
  144. outb_p(0xff & (pos >> 1), vidport+1);
  145. }
  146. int video_init(void)
  147. {
  148. u16 pos;
  149. static device_t vga_dev;
  150. static device_t kbd_dev;
  151. vidmem = (char *) 0xb8000;
  152. vidport = 0x3d4;
  153. lines = 25;
  154. cols = 80;
  155. outb_p(14, vidport);
  156. pos = inb_p(vidport+1);
  157. pos <<= 8;
  158. outb_p(15, vidport);
  159. pos |= inb_p(vidport+1);
  160. orig_x = pos%cols;
  161. orig_y = pos/cols;
  162. #if 0
  163. printf("pos %x %d %d\n", pos, orig_x, orig_y);
  164. #endif
  165. if (orig_y > lines) {
  166. orig_x = orig_y =0;
  167. }
  168. memset(&vga_dev, 0, sizeof(vga_dev));
  169. strcpy(vga_dev.name, "vga");
  170. vga_dev.ext = 0;
  171. vga_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
  172. vga_dev.putc = video_putc; /* 'putc' function */
  173. vga_dev.puts = video_puts; /* 'puts' function */
  174. vga_dev.tstc = NULL; /* 'tstc' function */
  175. vga_dev.getc = NULL; /* 'getc' function */
  176. if (device_register(&vga_dev) == 0) {
  177. return 1;
  178. }
  179. if (i8042_kbd_init()) {
  180. return 1;
  181. }
  182. memset(&kbd_dev, 0, sizeof(kbd_dev));
  183. strcpy(kbd_dev.name, "kbd");
  184. kbd_dev.ext = 0;
  185. kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  186. kbd_dev.putc = NULL; /* 'putc' function */
  187. kbd_dev.puts = NULL; /* 'puts' function */
  188. kbd_dev.tstc = i8042_tstc; /* 'tstc' function */
  189. kbd_dev.getc = i8042_getc; /* 'getc' function */
  190. if (device_register(&kbd_dev) == 0) {
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. int drv_video_init(void)
  196. {
  197. if (video_bios_init()) {
  198. return 1;
  199. }
  200. return video_init();
  201. }