video-mode.c 3.8 KB

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