video.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/video.c
  12. *
  13. * Select video mode
  14. */
  15. #include "boot.h"
  16. #include "video.h"
  17. #include "vesa.h"
  18. /*
  19. * Mode list variables
  20. */
  21. static struct card_info cards[]; /* List of cards to probe for */
  22. /*
  23. * Common variables
  24. */
  25. int adapter; /* 0=CGA/MDA/HGC, 1=EGA, 2=VGA+ */
  26. u16 video_segment;
  27. int force_x, force_y; /* Don't query the BIOS for cols/rows */
  28. int do_restore = 0; /* Screen contents changed during mode flip */
  29. int graphic_mode; /* Graphic mode with linear frame buffer */
  30. static void store_cursor_position(void)
  31. {
  32. u16 curpos;
  33. u16 ax, bx;
  34. ax = 0x0300;
  35. bx = 0;
  36. asm(INT10
  37. : "=d" (curpos), "+a" (ax), "+b" (bx)
  38. : : "ecx", "esi", "edi");
  39. boot_params.screen_info.orig_x = curpos;
  40. boot_params.screen_info.orig_y = curpos >> 8;
  41. }
  42. static void store_video_mode(void)
  43. {
  44. u16 ax, page;
  45. /* N.B.: the saving of the video page here is a bit silly,
  46. since we pretty much assume page 0 everywhere. */
  47. ax = 0x0f00;
  48. asm(INT10
  49. : "+a" (ax), "=b" (page)
  50. : : "ecx", "edx", "esi", "edi");
  51. /* Not all BIOSes are clean with respect to the top bit */
  52. boot_params.screen_info.orig_video_mode = ax & 0x7f;
  53. boot_params.screen_info.orig_video_page = page >> 8;
  54. }
  55. /*
  56. * Store the video mode parameters for later usage by the kernel.
  57. * This is done by asking the BIOS except for the rows/columns
  58. * parameters in the default 80x25 mode -- these are set directly,
  59. * because some very obscure BIOSes supply insane values.
  60. */
  61. static void store_mode_params(void)
  62. {
  63. u16 font_size;
  64. int x, y;
  65. /* For graphics mode, it is up to the mode-setting driver
  66. (currently only video-vesa.c) to store the parameters */
  67. if (graphic_mode)
  68. return;
  69. store_cursor_position();
  70. store_video_mode();
  71. if (boot_params.screen_info.orig_video_mode == 0x07) {
  72. /* MDA, HGC, or VGA in monochrome mode */
  73. video_segment = 0xb000;
  74. } else {
  75. /* CGA, EGA, VGA and so forth */
  76. video_segment = 0xb800;
  77. }
  78. set_fs(0);
  79. font_size = rdfs16(0x485); /* Font size, BIOS area */
  80. boot_params.screen_info.orig_video_points = font_size;
  81. x = rdfs16(0x44a);
  82. y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
  83. if (force_x)
  84. x = force_x;
  85. if (force_y)
  86. y = force_y;
  87. boot_params.screen_info.orig_video_cols = x;
  88. boot_params.screen_info.orig_video_lines = y;
  89. }
  90. /* Probe the video drivers and have them generate their mode lists. */
  91. static void probe_cards(int unsafe)
  92. {
  93. struct card_info *card;
  94. static u8 probed[2];
  95. if (probed[unsafe])
  96. return;
  97. probed[unsafe] = 1;
  98. for (card = video_cards; card < video_cards_end; card++) {
  99. if (card->unsafe == unsafe) {
  100. if (card->probe)
  101. card->nmodes = card->probe();
  102. else
  103. card->nmodes = 0;
  104. }
  105. }
  106. }
  107. /* Test if a mode is defined */
  108. int mode_defined(u16 mode)
  109. {
  110. struct card_info *card;
  111. struct mode_info *mi;
  112. int i;
  113. for (card = video_cards; card < video_cards_end; card++) {
  114. mi = card->modes;
  115. for (i = 0; i < card->nmodes; i++, mi++) {
  116. if (mi->mode == mode)
  117. return 1;
  118. }
  119. }
  120. return 0;
  121. }
  122. /* Set mode (without recalc) */
  123. static int raw_set_mode(u16 mode, u16 *real_mode)
  124. {
  125. int nmode, i;
  126. struct card_info *card;
  127. struct mode_info *mi;
  128. /* Drop the recalc bit if set */
  129. mode &= ~VIDEO_RECALC;
  130. /* Scan for mode based on fixed ID, position, or resolution */
  131. nmode = 0;
  132. for (card = video_cards; card < video_cards_end; card++) {
  133. mi = card->modes;
  134. for (i = 0; i < card->nmodes; i++, mi++) {
  135. int visible = mi->x || mi->y;
  136. if ((mode == nmode && visible) ||
  137. mode == mi->mode ||
  138. mode == (mi->y << 8)+mi->x) {
  139. *real_mode = mi->mode;
  140. return card->set_mode(mi);
  141. }
  142. if (visible)
  143. nmode++;
  144. }
  145. }
  146. /* Nothing found? Is it an "exceptional" (unprobed) mode? */
  147. for (card = video_cards; card < video_cards_end; card++) {
  148. if (mode >= card->xmode_first &&
  149. mode < card->xmode_first+card->xmode_n) {
  150. struct mode_info mix;
  151. *real_mode = mix.mode = mode;
  152. mix.x = mix.y = 0;
  153. return card->set_mode(&mix);
  154. }
  155. }
  156. /* Otherwise, failure... */
  157. return -1;
  158. }
  159. /*
  160. * Recalculate the vertical video cutoff (hack!)
  161. */
  162. static void vga_recalc_vertical(void)
  163. {
  164. unsigned int font_size, rows;
  165. u16 crtc;
  166. u8 pt, ov;
  167. set_fs(0);
  168. font_size = rdfs8(0x485); /* BIOS: font size (pixels) */
  169. rows = force_y ? force_y : rdfs8(0x484)+1; /* Text rows */
  170. rows *= font_size; /* Visible scan lines */
  171. rows--; /* ... minus one */
  172. crtc = vga_crtc();
  173. pt = in_idx(crtc, 0x11);
  174. pt &= ~0x80; /* Unlock CR0-7 */
  175. out_idx(pt, crtc, 0x11);
  176. out_idx((u8)rows, crtc, 0x12); /* Lower height register */
  177. ov = in_idx(crtc, 0x07); /* Overflow register */
  178. ov &= 0xbd;
  179. ov |= (rows >> (8-1)) & 0x02;
  180. ov |= (rows >> (9-6)) & 0x40;
  181. out_idx(ov, crtc, 0x07);
  182. }
  183. /* Set mode (with recalc if specified) */
  184. static int set_mode(u16 mode)
  185. {
  186. int rv;
  187. u16 real_mode;
  188. /* Very special mode numbers... */
  189. if (mode == VIDEO_CURRENT_MODE)
  190. return 0; /* Nothing to do... */
  191. else if (mode == NORMAL_VGA)
  192. mode = VIDEO_80x25;
  193. else if (mode == EXTENDED_VGA)
  194. mode = VIDEO_8POINT;
  195. rv = raw_set_mode(mode, &real_mode);
  196. if (rv)
  197. return rv;
  198. if (mode & VIDEO_RECALC)
  199. vga_recalc_vertical();
  200. /* Save the canonical mode number for the kernel, not
  201. an alias, size specification or menu position */
  202. boot_params.hdr.vid_mode = real_mode;
  203. return 0;
  204. }
  205. static unsigned int get_entry(void)
  206. {
  207. char entry_buf[4];
  208. int i, len = 0;
  209. int key;
  210. unsigned int v;
  211. do {
  212. key = getchar();
  213. if (key == '\b') {
  214. if (len > 0) {
  215. puts("\b \b");
  216. len--;
  217. }
  218. } else if ((key >= '0' && key <= '9') ||
  219. (key >= 'A' && key <= 'Z') ||
  220. (key >= 'a' && key <= 'z')) {
  221. if (len < sizeof entry_buf) {
  222. entry_buf[len++] = key;
  223. putchar(key);
  224. }
  225. }
  226. } while (key != '\r');
  227. putchar('\n');
  228. if (len == 0)
  229. return VIDEO_CURRENT_MODE; /* Default */
  230. v = 0;
  231. for (i = 0; i < len; i++) {
  232. v <<= 4;
  233. key = entry_buf[i] | 0x20;
  234. v += (key > '9') ? key-'a'+10 : key-'0';
  235. }
  236. return v;
  237. }
  238. static void display_menu(void)
  239. {
  240. struct card_info *card;
  241. struct mode_info *mi;
  242. char ch;
  243. int i;
  244. puts("Mode: COLSxROWS:\n");
  245. ch = '0';
  246. for (card = video_cards; card < video_cards_end; card++) {
  247. mi = card->modes;
  248. for (i = 0; i < card->nmodes; i++, mi++) {
  249. int visible = mi->x && mi->y;
  250. u16 mode_id = mi->mode ? mi->mode :
  251. (mi->y << 8)+mi->x;
  252. if (!visible)
  253. continue; /* Hidden mode */
  254. printf("%c %04X %3dx%-3d %s\n",
  255. ch, mode_id, mi->x, mi->y, card->card_name);
  256. if (ch == '9')
  257. ch = 'a';
  258. else if (ch == 'z' || ch == ' ')
  259. ch = ' '; /* Out of keys... */
  260. else
  261. ch++;
  262. }
  263. }
  264. }
  265. #define H(x) ((x)-'a'+10)
  266. #define SCAN ((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
  267. static unsigned int mode_menu(void)
  268. {
  269. int key;
  270. unsigned int sel;
  271. puts("Press <ENTER> to see video modes available, "
  272. "<SPACE> to continue, or wait 30 sec\n");
  273. kbd_flush();
  274. while (1) {
  275. key = getchar_timeout();
  276. if (key == ' ' || key == 0)
  277. return VIDEO_CURRENT_MODE; /* Default */
  278. if (key == '\r')
  279. break;
  280. putchar('\a'); /* Beep! */
  281. }
  282. for (;;) {
  283. display_menu();
  284. puts("Enter a video mode or \"scan\" to scan for "
  285. "additional modes: ");
  286. sel = get_entry();
  287. if (sel != SCAN)
  288. return sel;
  289. probe_cards(1);
  290. }
  291. }
  292. #ifdef CONFIG_VIDEO_RETAIN
  293. /* Save screen content to the heap */
  294. struct saved_screen {
  295. int x, y;
  296. int curx, cury;
  297. u16 *data;
  298. } saved;
  299. static void save_screen(void)
  300. {
  301. /* Should be called after store_mode_params() */
  302. saved.x = boot_params.screen_info.orig_video_cols;
  303. saved.y = boot_params.screen_info.orig_video_lines;
  304. saved.curx = boot_params.screen_info.orig_x;
  305. saved.cury = boot_params.screen_info.orig_y;
  306. if (heap_free() < saved.x*saved.y*sizeof(u16)+512)
  307. return; /* Not enough heap to save the screen */
  308. saved.data = GET_HEAP(u16, saved.x*saved.y);
  309. set_fs(video_segment);
  310. copy_from_fs(saved.data, 0, saved.x*saved.y*sizeof(u16));
  311. }
  312. static void restore_screen(void)
  313. {
  314. /* Should be called after store_mode_params() */
  315. int xs = boot_params.screen_info.orig_video_cols;
  316. int ys = boot_params.screen_info.orig_video_lines;
  317. int y;
  318. addr_t dst = 0;
  319. u16 *src = saved.data;
  320. u16 ax, bx, dx;
  321. if (graphic_mode)
  322. return; /* Can't restore onto a graphic mode */
  323. if (!src)
  324. return; /* No saved screen contents */
  325. /* Restore screen contents */
  326. set_fs(video_segment);
  327. for (y = 0; y < ys; y++) {
  328. int npad;
  329. if (y < saved.y) {
  330. int copy = (xs < saved.x) ? xs : saved.x;
  331. copy_to_fs(dst, src, copy*sizeof(u16));
  332. dst += copy*sizeof(u16);
  333. src += saved.x;
  334. npad = (xs < saved.x) ? 0 : xs-saved.x;
  335. } else {
  336. npad = xs;
  337. }
  338. /* Writes "npad" blank characters to
  339. video_segment:dst and advances dst */
  340. asm volatile("pushw %%es ; "
  341. "movw %2,%%es ; "
  342. "shrw %%cx ; "
  343. "jnc 1f ; "
  344. "stosw \n\t"
  345. "1: rep;stosl ; "
  346. "popw %%es"
  347. : "+D" (dst), "+c" (npad)
  348. : "bdS" (video_segment),
  349. "a" (0x07200720));
  350. }
  351. /* Restore cursor position */
  352. ax = 0x0200; /* Set cursor position */
  353. bx = 0; /* Page number (<< 8) */
  354. dx = (saved.cury << 8)+saved.curx;
  355. asm volatile(INT10
  356. : "+a" (ax), "+b" (bx), "+d" (dx)
  357. : : "ecx", "esi", "edi");
  358. }
  359. #else
  360. #define save_screen() ((void)0)
  361. #define restore_screen() ((void)0)
  362. #endif
  363. void set_video(void)
  364. {
  365. u16 mode = boot_params.hdr.vid_mode;
  366. RESET_HEAP();
  367. store_mode_params();
  368. save_screen();
  369. probe_cards(0);
  370. for (;;) {
  371. if (mode == ASK_VGA)
  372. mode = mode_menu();
  373. if (!set_mode(mode))
  374. break;
  375. printf("Undefined video mode number: %x\n", mode);
  376. mode = ASK_VGA;
  377. }
  378. vesa_store_edid();
  379. store_mode_params();
  380. if (do_restore)
  381. restore_screen();
  382. }