video-mode.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007-2008 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/video-mode.c
  12. *
  13. * Set the video mode. This is separated out into a different
  14. * file in order to be shared with the ACPI wakeup code.
  15. */
  16. #include "boot.h"
  17. #include "video.h"
  18. #include "vesa.h"
  19. /*
  20. * Common variables
  21. */
  22. int adapter; /* 0=CGA/MDA/HGC, 1=EGA, 2=VGA+ */
  23. int force_x, force_y; /* Don't query the BIOS for cols/rows */
  24. int do_restore; /* Screen contents changed during mode flip */
  25. int graphic_mode; /* Graphic mode with linear frame buffer */
  26. /* Probe the video drivers and have them generate their mode lists. */
  27. void probe_cards(int unsafe)
  28. {
  29. struct card_info *card;
  30. static u8 probed[2];
  31. if (probed[unsafe])
  32. return;
  33. probed[unsafe] = 1;
  34. for (card = video_cards; card < video_cards_end; card++) {
  35. if (card->unsafe == unsafe) {
  36. if (card->probe)
  37. card->nmodes = card->probe();
  38. else
  39. card->nmodes = 0;
  40. }
  41. }
  42. }
  43. /* Test if a mode is defined */
  44. int mode_defined(u16 mode)
  45. {
  46. struct card_info *card;
  47. struct mode_info *mi;
  48. int i;
  49. for (card = video_cards; card < video_cards_end; card++) {
  50. mi = card->modes;
  51. for (i = 0; i < card->nmodes; i++, mi++) {
  52. if (mi->mode == mode)
  53. return 1;
  54. }
  55. }
  56. return 0;
  57. }
  58. /* Set mode (without recalc) */
  59. static int raw_set_mode(u16 mode, u16 *real_mode)
  60. {
  61. int nmode, i;
  62. struct card_info *card;
  63. struct mode_info *mi;
  64. /* Drop the recalc bit if set */
  65. mode &= ~VIDEO_RECALC;
  66. /* Scan for mode based on fixed ID, position, or resolution */
  67. nmode = 0;
  68. for (card = video_cards; card < video_cards_end; card++) {
  69. mi = card->modes;
  70. for (i = 0; i < card->nmodes; i++, mi++) {
  71. int visible = mi->x || mi->y;
  72. if ((mode == nmode && visible) ||
  73. mode == mi->mode ||
  74. mode == (mi->y << 8)+mi->x) {
  75. *real_mode = mi->mode;
  76. return card->set_mode(mi);
  77. }
  78. if (visible)
  79. nmode++;
  80. }
  81. }
  82. /* Nothing found? Is it an "exceptional" (unprobed) mode? */
  83. for (card = video_cards; card < video_cards_end; card++) {
  84. if (mode >= card->xmode_first &&
  85. mode < card->xmode_first+card->xmode_n) {
  86. struct mode_info mix;
  87. *real_mode = mix.mode = mode;
  88. mix.x = mix.y = 0;
  89. return card->set_mode(&mix);
  90. }
  91. }
  92. /* Otherwise, failure... */
  93. return -1;
  94. }
  95. /*
  96. * Recalculate the vertical video cutoff (hack!)
  97. */
  98. static void vga_recalc_vertical(void)
  99. {
  100. unsigned int font_size, rows;
  101. u16 crtc;
  102. u8 pt, ov;
  103. set_fs(0);
  104. font_size = rdfs8(0x485); /* BIOS: font size (pixels) */
  105. rows = force_y ? force_y : rdfs8(0x484)+1; /* Text rows */
  106. rows *= font_size; /* Visible scan lines */
  107. rows--; /* ... minus one */
  108. crtc = vga_crtc();
  109. pt = in_idx(crtc, 0x11);
  110. pt &= ~0x80; /* Unlock CR0-7 */
  111. out_idx(pt, crtc, 0x11);
  112. out_idx((u8)rows, crtc, 0x12); /* Lower height register */
  113. ov = in_idx(crtc, 0x07); /* Overflow register */
  114. ov &= 0xbd;
  115. ov |= (rows >> (8-1)) & 0x02;
  116. ov |= (rows >> (9-6)) & 0x40;
  117. out_idx(ov, crtc, 0x07);
  118. }
  119. /* Set mode (with recalc if specified) */
  120. int set_mode(u16 mode)
  121. {
  122. int rv;
  123. u16 real_mode;
  124. /* Very special mode numbers... */
  125. if (mode == VIDEO_CURRENT_MODE)
  126. return 0; /* Nothing to do... */
  127. else if (mode == NORMAL_VGA)
  128. mode = VIDEO_80x25;
  129. else if (mode == EXTENDED_VGA)
  130. mode = VIDEO_8POINT;
  131. rv = raw_set_mode(mode, &real_mode);
  132. if (rv)
  133. return rv;
  134. if (mode & VIDEO_RECALC)
  135. vga_recalc_vertical();
  136. /* Save the canonical mode number for the kernel, not
  137. an alias, size specification or menu position */
  138. #ifndef _WAKEUP
  139. boot_params.hdr.vid_mode = real_mode;
  140. #endif
  141. return 0;
  142. }