vgacon.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /*
  2. * linux/drivers/video/vgacon.c -- Low level VGA based console driver
  3. *
  4. * Created 28 Sep 1997 by Geert Uytterhoeven
  5. *
  6. * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
  7. *
  8. * This file is based on the old console.c, vga.c and vesa_blank.c drivers.
  9. *
  10. * Copyright (C) 1991, 1992 Linus Torvalds
  11. * 1995 Jay Estabrook
  12. *
  13. * User definable mapping table and font loading by Eugene G. Crosser,
  14. * <crosser@average.org>
  15. *
  16. * Improved loadable font/UTF-8 support by H. Peter Anvin
  17. * Feb-Sep 1995 <peter.anvin@linux.org>
  18. *
  19. * Colour palette handling, by Simon Tatham
  20. * 17-Jun-95 <sgt20@cam.ac.uk>
  21. *
  22. * if 512 char mode is already enabled don't re-enable it,
  23. * because it causes screen to flicker, by Mitja Horvat
  24. * 5-May-96 <mitja.horvat@guest.arnes.si>
  25. *
  26. * Use 2 outw instead of 4 outb_p to reduce erroneous text
  27. * flashing on RHS of screen during heavy console scrolling .
  28. * Oct 1996, Paul Gortmaker.
  29. *
  30. *
  31. * This file is subject to the terms and conditions of the GNU General Public
  32. * License. See the file COPYING in the main directory of this archive for
  33. * more details.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/types.h>
  37. #include <linux/fs.h>
  38. #include <linux/kernel.h>
  39. #include <linux/console.h>
  40. #include <linux/string.h>
  41. #include <linux/kd.h>
  42. #include <linux/slab.h>
  43. #include <linux/vt_kern.h>
  44. #include <linux/sched.h>
  45. #include <linux/selection.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/ioport.h>
  48. #include <linux/init.h>
  49. #include <linux/screen_info.h>
  50. #include <video/vga.h>
  51. #include <asm/io.h>
  52. static DEFINE_RAW_SPINLOCK(vga_lock);
  53. static int cursor_size_lastfrom;
  54. static int cursor_size_lastto;
  55. static u32 vgacon_xres;
  56. static u32 vgacon_yres;
  57. static struct vgastate vgastate;
  58. #define BLANK 0x0020
  59. #define CAN_LOAD_EGA_FONTS /* undefine if the user must not do this */
  60. #define CAN_LOAD_PALETTE /* undefine if the user must not do this */
  61. /* You really do _NOT_ want to define this, unless you have buggy
  62. * Trident VGA which will resize cursor when moving it between column
  63. * 15 & 16. If you define this and your VGA is OK, inverse bug will
  64. * appear.
  65. */
  66. #undef TRIDENT_GLITCH
  67. #define VGA_FONTWIDTH 8 /* VGA does not support fontwidths != 8 */
  68. /*
  69. * Interface used by the world
  70. */
  71. static const char *vgacon_startup(void);
  72. static void vgacon_init(struct vc_data *c, int init);
  73. static void vgacon_deinit(struct vc_data *c);
  74. static void vgacon_cursor(struct vc_data *c, int mode);
  75. static int vgacon_switch(struct vc_data *c);
  76. static int vgacon_blank(struct vc_data *c, int blank, int mode_switch);
  77. static int vgacon_set_palette(struct vc_data *vc, unsigned char *table);
  78. static int vgacon_scrolldelta(struct vc_data *c, int lines);
  79. static int vgacon_set_origin(struct vc_data *c);
  80. static void vgacon_save_screen(struct vc_data *c);
  81. static int vgacon_scroll(struct vc_data *c, int t, int b, int dir,
  82. int lines);
  83. static void vgacon_invert_region(struct vc_data *c, u16 * p, int count);
  84. static struct uni_pagedir *vgacon_uni_pagedir;
  85. static int vgacon_refcount;
  86. /* Description of the hardware situation */
  87. static int vga_init_done __read_mostly;
  88. static unsigned long vga_vram_base __read_mostly; /* Base of video memory */
  89. static unsigned long vga_vram_end __read_mostly; /* End of video memory */
  90. static unsigned int vga_vram_size __read_mostly; /* Size of video memory */
  91. static u16 vga_video_port_reg __read_mostly; /* Video register select port */
  92. static u16 vga_video_port_val __read_mostly; /* Video register value port */
  93. static unsigned int vga_video_num_columns; /* Number of text columns */
  94. static unsigned int vga_video_num_lines; /* Number of text lines */
  95. static int vga_can_do_color __read_mostly; /* Do we support colors? */
  96. static unsigned int vga_default_font_height __read_mostly; /* Height of default screen font */
  97. static unsigned char vga_video_type __read_mostly; /* Card type */
  98. static unsigned char vga_hardscroll_enabled __read_mostly;
  99. static unsigned char vga_hardscroll_user_enable __read_mostly = 1;
  100. static unsigned char vga_font_is_default = 1;
  101. static int vga_vesa_blanked;
  102. static int vga_palette_blanked;
  103. static int vga_is_gfx;
  104. static int vga_512_chars;
  105. static int vga_video_font_height;
  106. static int vga_scan_lines __read_mostly;
  107. static unsigned int vga_rolled_over;
  108. static int vgacon_text_mode_force;
  109. bool vgacon_text_force(void)
  110. {
  111. return vgacon_text_mode_force ? true : false;
  112. }
  113. EXPORT_SYMBOL(vgacon_text_force);
  114. static int __init text_mode(char *str)
  115. {
  116. vgacon_text_mode_force = 1;
  117. return 1;
  118. }
  119. /* force text mode - used by kernel modesetting */
  120. __setup("nomodeset", text_mode);
  121. static int __init no_scroll(char *str)
  122. {
  123. /*
  124. * Disabling scrollback is required for the Braillex ib80-piezo
  125. * Braille reader made by F.H. Papenmeier (Germany).
  126. * Use the "no-scroll" bootflag.
  127. */
  128. vga_hardscroll_user_enable = vga_hardscroll_enabled = 0;
  129. return 1;
  130. }
  131. __setup("no-scroll", no_scroll);
  132. /*
  133. * By replacing the four outb_p with two back to back outw, we can reduce
  134. * the window of opportunity to see text mislocated to the RHS of the
  135. * console during heavy scrolling activity. However there is the remote
  136. * possibility that some pre-dinosaur hardware won't like the back to back
  137. * I/O. Since the Xservers get away with it, we should be able to as well.
  138. */
  139. static inline void write_vga(unsigned char reg, unsigned int val)
  140. {
  141. unsigned int v1, v2;
  142. unsigned long flags;
  143. /*
  144. * ddprintk might set the console position from interrupt
  145. * handlers, thus the write has to be IRQ-atomic.
  146. */
  147. raw_spin_lock_irqsave(&vga_lock, flags);
  148. #ifndef SLOW_VGA
  149. v1 = reg + (val & 0xff00);
  150. v2 = reg + 1 + ((val << 8) & 0xff00);
  151. outw(v1, vga_video_port_reg);
  152. outw(v2, vga_video_port_reg);
  153. #else
  154. outb_p(reg, vga_video_port_reg);
  155. outb_p(val >> 8, vga_video_port_val);
  156. outb_p(reg + 1, vga_video_port_reg);
  157. outb_p(val & 0xff, vga_video_port_val);
  158. #endif
  159. raw_spin_unlock_irqrestore(&vga_lock, flags);
  160. }
  161. static inline void vga_set_mem_top(struct vc_data *c)
  162. {
  163. write_vga(12, (c->vc_visible_origin - vga_vram_base) / 2);
  164. }
  165. #ifdef CONFIG_VGACON_SOFT_SCROLLBACK
  166. /* software scrollback */
  167. static void *vgacon_scrollback;
  168. static int vgacon_scrollback_tail;
  169. static int vgacon_scrollback_size;
  170. static int vgacon_scrollback_rows;
  171. static int vgacon_scrollback_cnt;
  172. static int vgacon_scrollback_cur;
  173. static int vgacon_scrollback_save;
  174. static int vgacon_scrollback_restore;
  175. static void vgacon_scrollback_init(int pitch)
  176. {
  177. int rows = CONFIG_VGACON_SOFT_SCROLLBACK_SIZE * 1024/pitch;
  178. if (vgacon_scrollback) {
  179. vgacon_scrollback_cnt = 0;
  180. vgacon_scrollback_tail = 0;
  181. vgacon_scrollback_cur = 0;
  182. vgacon_scrollback_rows = rows - 1;
  183. vgacon_scrollback_size = rows * pitch;
  184. }
  185. }
  186. static void vgacon_scrollback_startup(void)
  187. {
  188. vgacon_scrollback = kcalloc(CONFIG_VGACON_SOFT_SCROLLBACK_SIZE, 1024, GFP_NOWAIT);
  189. vgacon_scrollback_init(vga_video_num_columns * 2);
  190. }
  191. static void vgacon_scrollback_update(struct vc_data *c, int t, int count)
  192. {
  193. void *p;
  194. if (!vgacon_scrollback_size || c->vc_num != fg_console)
  195. return;
  196. p = (void *) (c->vc_origin + t * c->vc_size_row);
  197. while (count--) {
  198. scr_memcpyw(vgacon_scrollback + vgacon_scrollback_tail,
  199. p, c->vc_size_row);
  200. vgacon_scrollback_cnt++;
  201. p += c->vc_size_row;
  202. vgacon_scrollback_tail += c->vc_size_row;
  203. if (vgacon_scrollback_tail >= vgacon_scrollback_size)
  204. vgacon_scrollback_tail = 0;
  205. if (vgacon_scrollback_cnt > vgacon_scrollback_rows)
  206. vgacon_scrollback_cnt = vgacon_scrollback_rows;
  207. vgacon_scrollback_cur = vgacon_scrollback_cnt;
  208. }
  209. }
  210. static void vgacon_restore_screen(struct vc_data *c)
  211. {
  212. vgacon_scrollback_save = 0;
  213. if (!vga_is_gfx && !vgacon_scrollback_restore) {
  214. scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
  215. c->vc_screenbuf_size > vga_vram_size ?
  216. vga_vram_size : c->vc_screenbuf_size);
  217. vgacon_scrollback_restore = 1;
  218. vgacon_scrollback_cur = vgacon_scrollback_cnt;
  219. }
  220. }
  221. static int vgacon_scrolldelta(struct vc_data *c, int lines)
  222. {
  223. int start, end, count, soff;
  224. if (!lines) {
  225. c->vc_visible_origin = c->vc_origin;
  226. vga_set_mem_top(c);
  227. return 1;
  228. }
  229. if (!vgacon_scrollback)
  230. return 1;
  231. if (!vgacon_scrollback_save) {
  232. vgacon_cursor(c, CM_ERASE);
  233. vgacon_save_screen(c);
  234. vgacon_scrollback_save = 1;
  235. }
  236. vgacon_scrollback_restore = 0;
  237. start = vgacon_scrollback_cur + lines;
  238. end = start + abs(lines);
  239. if (start < 0)
  240. start = 0;
  241. if (start > vgacon_scrollback_cnt)
  242. start = vgacon_scrollback_cnt;
  243. if (end < 0)
  244. end = 0;
  245. if (end > vgacon_scrollback_cnt)
  246. end = vgacon_scrollback_cnt;
  247. vgacon_scrollback_cur = start;
  248. count = end - start;
  249. soff = vgacon_scrollback_tail - ((vgacon_scrollback_cnt - end) *
  250. c->vc_size_row);
  251. soff -= count * c->vc_size_row;
  252. if (soff < 0)
  253. soff += vgacon_scrollback_size;
  254. count = vgacon_scrollback_cnt - start;
  255. if (count > c->vc_rows)
  256. count = c->vc_rows;
  257. if (count) {
  258. int copysize;
  259. int diff = c->vc_rows - count;
  260. void *d = (void *) c->vc_origin;
  261. void *s = (void *) c->vc_screenbuf;
  262. count *= c->vc_size_row;
  263. /* how much memory to end of buffer left? */
  264. copysize = min(count, vgacon_scrollback_size - soff);
  265. scr_memcpyw(d, vgacon_scrollback + soff, copysize);
  266. d += copysize;
  267. count -= copysize;
  268. if (count) {
  269. scr_memcpyw(d, vgacon_scrollback, count);
  270. d += count;
  271. }
  272. if (diff)
  273. scr_memcpyw(d, s, diff * c->vc_size_row);
  274. } else
  275. vgacon_cursor(c, CM_MOVE);
  276. return 1;
  277. }
  278. #else
  279. #define vgacon_scrollback_startup(...) do { } while (0)
  280. #define vgacon_scrollback_init(...) do { } while (0)
  281. #define vgacon_scrollback_update(...) do { } while (0)
  282. static void vgacon_restore_screen(struct vc_data *c)
  283. {
  284. if (c->vc_origin != c->vc_visible_origin)
  285. vgacon_scrolldelta(c, 0);
  286. }
  287. static int vgacon_scrolldelta(struct vc_data *c, int lines)
  288. {
  289. if (!lines) /* Turn scrollback off */
  290. c->vc_visible_origin = c->vc_origin;
  291. else {
  292. int margin = c->vc_size_row * 4;
  293. int ul, we, p, st;
  294. if (vga_rolled_over >
  295. (c->vc_scr_end - vga_vram_base) + margin) {
  296. ul = c->vc_scr_end - vga_vram_base;
  297. we = vga_rolled_over + c->vc_size_row;
  298. } else {
  299. ul = 0;
  300. we = vga_vram_size;
  301. }
  302. p = (c->vc_visible_origin - vga_vram_base - ul + we) % we +
  303. lines * c->vc_size_row;
  304. st = (c->vc_origin - vga_vram_base - ul + we) % we;
  305. if (st < 2 * margin)
  306. margin = 0;
  307. if (p < margin)
  308. p = 0;
  309. if (p > st - margin)
  310. p = st;
  311. c->vc_visible_origin = vga_vram_base + (p + ul) % we;
  312. }
  313. vga_set_mem_top(c);
  314. return 1;
  315. }
  316. #endif /* CONFIG_VGACON_SOFT_SCROLLBACK */
  317. static const char *vgacon_startup(void)
  318. {
  319. const char *display_desc = NULL;
  320. u16 saved1, saved2;
  321. volatile u16 *p;
  322. if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB ||
  323. screen_info.orig_video_isVGA == VIDEO_TYPE_EFI) {
  324. no_vga:
  325. #ifdef CONFIG_DUMMY_CONSOLE
  326. conswitchp = &dummy_con;
  327. return conswitchp->con_startup();
  328. #else
  329. return NULL;
  330. #endif
  331. }
  332. /* boot_params.screen_info initialized? */
  333. if ((screen_info.orig_video_mode == 0) &&
  334. (screen_info.orig_video_lines == 0) &&
  335. (screen_info.orig_video_cols == 0))
  336. goto no_vga;
  337. /* VGA16 modes are not handled by VGACON */
  338. if ((screen_info.orig_video_mode == 0x0D) || /* 320x200/4 */
  339. (screen_info.orig_video_mode == 0x0E) || /* 640x200/4 */
  340. (screen_info.orig_video_mode == 0x10) || /* 640x350/4 */
  341. (screen_info.orig_video_mode == 0x12) || /* 640x480/4 */
  342. (screen_info.orig_video_mode == 0x6A)) /* 800x600/4 (VESA) */
  343. goto no_vga;
  344. vga_video_num_lines = screen_info.orig_video_lines;
  345. vga_video_num_columns = screen_info.orig_video_cols;
  346. vgastate.vgabase = NULL;
  347. if (screen_info.orig_video_mode == 7) {
  348. /* Monochrome display */
  349. vga_vram_base = 0xb0000;
  350. vga_video_port_reg = VGA_CRT_IM;
  351. vga_video_port_val = VGA_CRT_DM;
  352. if ((screen_info.orig_video_ega_bx & 0xff) != 0x10) {
  353. static struct resource ega_console_resource =
  354. { .name = "ega",
  355. .flags = IORESOURCE_IO,
  356. .start = 0x3B0,
  357. .end = 0x3BF };
  358. vga_video_type = VIDEO_TYPE_EGAM;
  359. vga_vram_size = 0x8000;
  360. display_desc = "EGA+";
  361. request_resource(&ioport_resource,
  362. &ega_console_resource);
  363. } else {
  364. static struct resource mda1_console_resource =
  365. { .name = "mda",
  366. .flags = IORESOURCE_IO,
  367. .start = 0x3B0,
  368. .end = 0x3BB };
  369. static struct resource mda2_console_resource =
  370. { .name = "mda",
  371. .flags = IORESOURCE_IO,
  372. .start = 0x3BF,
  373. .end = 0x3BF };
  374. vga_video_type = VIDEO_TYPE_MDA;
  375. vga_vram_size = 0x2000;
  376. display_desc = "*MDA";
  377. request_resource(&ioport_resource,
  378. &mda1_console_resource);
  379. request_resource(&ioport_resource,
  380. &mda2_console_resource);
  381. vga_video_font_height = 14;
  382. }
  383. } else {
  384. /* If not, it is color. */
  385. vga_can_do_color = 1;
  386. vga_vram_base = 0xb8000;
  387. vga_video_port_reg = VGA_CRT_IC;
  388. vga_video_port_val = VGA_CRT_DC;
  389. if ((screen_info.orig_video_ega_bx & 0xff) != 0x10) {
  390. int i;
  391. vga_vram_size = 0x8000;
  392. if (!screen_info.orig_video_isVGA) {
  393. static struct resource ega_console_resource =
  394. { .name = "ega",
  395. .flags = IORESOURCE_IO,
  396. .start = 0x3C0,
  397. .end = 0x3DF };
  398. vga_video_type = VIDEO_TYPE_EGAC;
  399. display_desc = "EGA";
  400. request_resource(&ioport_resource,
  401. &ega_console_resource);
  402. } else {
  403. static struct resource vga_console_resource =
  404. { .name = "vga+",
  405. .flags = IORESOURCE_IO,
  406. .start = 0x3C0,
  407. .end = 0x3DF };
  408. vga_video_type = VIDEO_TYPE_VGAC;
  409. display_desc = "VGA+";
  410. request_resource(&ioport_resource,
  411. &vga_console_resource);
  412. #ifdef VGA_CAN_DO_64KB
  413. /*
  414. * get 64K rather than 32K of video RAM.
  415. * This doesn't actually work on all "VGA"
  416. * controllers (it seems like setting MM=01
  417. * and COE=1 isn't necessarily a good idea)
  418. */
  419. vga_vram_base = 0xa0000;
  420. vga_vram_size = 0x10000;
  421. outb_p(6, VGA_GFX_I);
  422. outb_p(6, VGA_GFX_D);
  423. #endif
  424. /*
  425. * Normalise the palette registers, to point
  426. * the 16 screen colours to the first 16
  427. * DAC entries.
  428. */
  429. for (i = 0; i < 16; i++) {
  430. inb_p(VGA_IS1_RC);
  431. outb_p(i, VGA_ATT_W);
  432. outb_p(i, VGA_ATT_W);
  433. }
  434. outb_p(0x20, VGA_ATT_W);
  435. /*
  436. * Now set the DAC registers back to their
  437. * default values
  438. */
  439. for (i = 0; i < 16; i++) {
  440. outb_p(color_table[i], VGA_PEL_IW);
  441. outb_p(default_red[i], VGA_PEL_D);
  442. outb_p(default_grn[i], VGA_PEL_D);
  443. outb_p(default_blu[i], VGA_PEL_D);
  444. }
  445. }
  446. } else {
  447. static struct resource cga_console_resource =
  448. { .name = "cga",
  449. .flags = IORESOURCE_IO,
  450. .start = 0x3D4,
  451. .end = 0x3D5 };
  452. vga_video_type = VIDEO_TYPE_CGA;
  453. vga_vram_size = 0x2000;
  454. display_desc = "*CGA";
  455. request_resource(&ioport_resource,
  456. &cga_console_resource);
  457. vga_video_font_height = 8;
  458. }
  459. }
  460. vga_vram_base = VGA_MAP_MEM(vga_vram_base, vga_vram_size);
  461. vga_vram_end = vga_vram_base + vga_vram_size;
  462. /*
  463. * Find out if there is a graphics card present.
  464. * Are there smarter methods around?
  465. */
  466. p = (volatile u16 *) vga_vram_base;
  467. saved1 = scr_readw(p);
  468. saved2 = scr_readw(p + 1);
  469. scr_writew(0xAA55, p);
  470. scr_writew(0x55AA, p + 1);
  471. if (scr_readw(p) != 0xAA55 || scr_readw(p + 1) != 0x55AA) {
  472. scr_writew(saved1, p);
  473. scr_writew(saved2, p + 1);
  474. goto no_vga;
  475. }
  476. scr_writew(0x55AA, p);
  477. scr_writew(0xAA55, p + 1);
  478. if (scr_readw(p) != 0x55AA || scr_readw(p + 1) != 0xAA55) {
  479. scr_writew(saved1, p);
  480. scr_writew(saved2, p + 1);
  481. goto no_vga;
  482. }
  483. scr_writew(saved1, p);
  484. scr_writew(saved2, p + 1);
  485. if (vga_video_type == VIDEO_TYPE_EGAC
  486. || vga_video_type == VIDEO_TYPE_VGAC
  487. || vga_video_type == VIDEO_TYPE_EGAM) {
  488. vga_hardscroll_enabled = vga_hardscroll_user_enable;
  489. vga_default_font_height = screen_info.orig_video_points;
  490. vga_video_font_height = screen_info.orig_video_points;
  491. /* This may be suboptimal but is a safe bet - go with it */
  492. vga_scan_lines =
  493. vga_video_font_height * vga_video_num_lines;
  494. }
  495. vgacon_xres = screen_info.orig_video_cols * VGA_FONTWIDTH;
  496. vgacon_yres = vga_scan_lines;
  497. if (!vga_init_done) {
  498. vgacon_scrollback_startup();
  499. vga_init_done = 1;
  500. }
  501. return display_desc;
  502. }
  503. static void vgacon_init(struct vc_data *c, int init)
  504. {
  505. struct uni_pagedir *p;
  506. /*
  507. * We cannot be loaded as a module, therefore init is always 1,
  508. * but vgacon_init can be called more than once, and init will
  509. * not be 1.
  510. */
  511. c->vc_can_do_color = vga_can_do_color;
  512. /* set dimensions manually if init != 0 since vc_resize() will fail */
  513. if (init) {
  514. c->vc_cols = vga_video_num_columns;
  515. c->vc_rows = vga_video_num_lines;
  516. } else
  517. vc_resize(c, vga_video_num_columns, vga_video_num_lines);
  518. c->vc_scan_lines = vga_scan_lines;
  519. c->vc_font.height = vga_video_font_height;
  520. c->vc_complement_mask = 0x7700;
  521. if (vga_512_chars)
  522. c->vc_hi_font_mask = 0x0800;
  523. p = *c->vc_uni_pagedir_loc;
  524. if (c->vc_uni_pagedir_loc != &vgacon_uni_pagedir) {
  525. con_free_unimap(c);
  526. c->vc_uni_pagedir_loc = &vgacon_uni_pagedir;
  527. vgacon_refcount++;
  528. }
  529. if (!vgacon_uni_pagedir && p)
  530. con_set_default_unimap(c);
  531. /* Only set the default if the user didn't deliberately override it */
  532. if (global_cursor_default == -1)
  533. global_cursor_default =
  534. !(screen_info.flags & VIDEO_FLAGS_NOCURSOR);
  535. }
  536. static void vgacon_deinit(struct vc_data *c)
  537. {
  538. /* When closing the active console, reset video origin */
  539. if (CON_IS_VISIBLE(c)) {
  540. c->vc_visible_origin = vga_vram_base;
  541. vga_set_mem_top(c);
  542. }
  543. if (!--vgacon_refcount)
  544. con_free_unimap(c);
  545. c->vc_uni_pagedir_loc = &c->vc_uni_pagedir;
  546. con_set_default_unimap(c);
  547. }
  548. static u8 vgacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
  549. u8 blink, u8 underline, u8 reverse, u8 italic)
  550. {
  551. u8 attr = color;
  552. if (vga_can_do_color) {
  553. if (italic)
  554. attr = (attr & 0xF0) | c->vc_itcolor;
  555. else if (underline)
  556. attr = (attr & 0xf0) | c->vc_ulcolor;
  557. else if (intensity == 0)
  558. attr = (attr & 0xf0) | c->vc_halfcolor;
  559. }
  560. if (reverse)
  561. attr =
  562. ((attr) & 0x88) | ((((attr) >> 4) | ((attr) << 4)) &
  563. 0x77);
  564. if (blink)
  565. attr ^= 0x80;
  566. if (intensity == 2)
  567. attr ^= 0x08;
  568. if (!vga_can_do_color) {
  569. if (italic)
  570. attr = (attr & 0xF8) | 0x02;
  571. else if (underline)
  572. attr = (attr & 0xf8) | 0x01;
  573. else if (intensity == 0)
  574. attr = (attr & 0xf0) | 0x08;
  575. }
  576. return attr;
  577. }
  578. static void vgacon_invert_region(struct vc_data *c, u16 * p, int count)
  579. {
  580. int col = vga_can_do_color;
  581. while (count--) {
  582. u16 a = scr_readw(p);
  583. if (col)
  584. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
  585. (((a) & 0x0700) << 4);
  586. else
  587. a ^= ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
  588. scr_writew(a, p++);
  589. }
  590. }
  591. static void vgacon_set_cursor_size(int xpos, int from, int to)
  592. {
  593. unsigned long flags;
  594. int curs, cure;
  595. #ifdef TRIDENT_GLITCH
  596. if (xpos < 16)
  597. from--, to--;
  598. #endif
  599. if ((from == cursor_size_lastfrom) && (to == cursor_size_lastto))
  600. return;
  601. cursor_size_lastfrom = from;
  602. cursor_size_lastto = to;
  603. raw_spin_lock_irqsave(&vga_lock, flags);
  604. if (vga_video_type >= VIDEO_TYPE_VGAC) {
  605. outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
  606. curs = inb_p(vga_video_port_val);
  607. outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
  608. cure = inb_p(vga_video_port_val);
  609. } else {
  610. curs = 0;
  611. cure = 0;
  612. }
  613. curs = (curs & 0xc0) | from;
  614. cure = (cure & 0xe0) | to;
  615. outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
  616. outb_p(curs, vga_video_port_val);
  617. outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
  618. outb_p(cure, vga_video_port_val);
  619. raw_spin_unlock_irqrestore(&vga_lock, flags);
  620. }
  621. static void vgacon_cursor(struct vc_data *c, int mode)
  622. {
  623. if (c->vc_mode != KD_TEXT)
  624. return;
  625. vgacon_restore_screen(c);
  626. switch (mode) {
  627. case CM_ERASE:
  628. write_vga(14, (c->vc_pos - vga_vram_base) / 2);
  629. if (vga_video_type >= VIDEO_TYPE_VGAC)
  630. vgacon_set_cursor_size(c->vc_x, 31, 30);
  631. else
  632. vgacon_set_cursor_size(c->vc_x, 31, 31);
  633. break;
  634. case CM_MOVE:
  635. case CM_DRAW:
  636. write_vga(14, (c->vc_pos - vga_vram_base) / 2);
  637. switch (c->vc_cursor_type & 0x0f) {
  638. case CUR_UNDERLINE:
  639. vgacon_set_cursor_size(c->vc_x,
  640. c->vc_font.height -
  641. (c->vc_font.height <
  642. 10 ? 2 : 3),
  643. c->vc_font.height -
  644. (c->vc_font.height <
  645. 10 ? 1 : 2));
  646. break;
  647. case CUR_TWO_THIRDS:
  648. vgacon_set_cursor_size(c->vc_x,
  649. c->vc_font.height / 3,
  650. c->vc_font.height -
  651. (c->vc_font.height <
  652. 10 ? 1 : 2));
  653. break;
  654. case CUR_LOWER_THIRD:
  655. vgacon_set_cursor_size(c->vc_x,
  656. (c->vc_font.height * 2) / 3,
  657. c->vc_font.height -
  658. (c->vc_font.height <
  659. 10 ? 1 : 2));
  660. break;
  661. case CUR_LOWER_HALF:
  662. vgacon_set_cursor_size(c->vc_x,
  663. c->vc_font.height / 2,
  664. c->vc_font.height -
  665. (c->vc_font.height <
  666. 10 ? 1 : 2));
  667. break;
  668. case CUR_NONE:
  669. if (vga_video_type >= VIDEO_TYPE_VGAC)
  670. vgacon_set_cursor_size(c->vc_x, 31, 30);
  671. else
  672. vgacon_set_cursor_size(c->vc_x, 31, 31);
  673. break;
  674. default:
  675. vgacon_set_cursor_size(c->vc_x, 1,
  676. c->vc_font.height);
  677. break;
  678. }
  679. break;
  680. }
  681. }
  682. static int vgacon_doresize(struct vc_data *c,
  683. unsigned int width, unsigned int height)
  684. {
  685. unsigned long flags;
  686. unsigned int scanlines = height * c->vc_font.height;
  687. u8 scanlines_lo = 0, r7 = 0, vsync_end = 0, mode, max_scan;
  688. raw_spin_lock_irqsave(&vga_lock, flags);
  689. vgacon_xres = width * VGA_FONTWIDTH;
  690. vgacon_yres = height * c->vc_font.height;
  691. if (vga_video_type >= VIDEO_TYPE_VGAC) {
  692. outb_p(VGA_CRTC_MAX_SCAN, vga_video_port_reg);
  693. max_scan = inb_p(vga_video_port_val);
  694. if (max_scan & 0x80)
  695. scanlines <<= 1;
  696. outb_p(VGA_CRTC_MODE, vga_video_port_reg);
  697. mode = inb_p(vga_video_port_val);
  698. if (mode & 0x04)
  699. scanlines >>= 1;
  700. scanlines -= 1;
  701. scanlines_lo = scanlines & 0xff;
  702. outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
  703. r7 = inb_p(vga_video_port_val) & ~0x42;
  704. if (scanlines & 0x100)
  705. r7 |= 0x02;
  706. if (scanlines & 0x200)
  707. r7 |= 0x40;
  708. /* deprotect registers */
  709. outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
  710. vsync_end = inb_p(vga_video_port_val);
  711. outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
  712. outb_p(vsync_end & ~0x80, vga_video_port_val);
  713. }
  714. outb_p(VGA_CRTC_H_DISP, vga_video_port_reg);
  715. outb_p(width - 1, vga_video_port_val);
  716. outb_p(VGA_CRTC_OFFSET, vga_video_port_reg);
  717. outb_p(width >> 1, vga_video_port_val);
  718. if (vga_video_type >= VIDEO_TYPE_VGAC) {
  719. outb_p(VGA_CRTC_V_DISP_END, vga_video_port_reg);
  720. outb_p(scanlines_lo, vga_video_port_val);
  721. outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
  722. outb_p(r7,vga_video_port_val);
  723. /* reprotect registers */
  724. outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
  725. outb_p(vsync_end, vga_video_port_val);
  726. }
  727. raw_spin_unlock_irqrestore(&vga_lock, flags);
  728. return 0;
  729. }
  730. static int vgacon_switch(struct vc_data *c)
  731. {
  732. int x = c->vc_cols * VGA_FONTWIDTH;
  733. int y = c->vc_rows * c->vc_font.height;
  734. int rows = screen_info.orig_video_lines * vga_default_font_height/
  735. c->vc_font.height;
  736. /*
  737. * We need to save screen size here as it's the only way
  738. * we can spot the screen has been resized and we need to
  739. * set size of freshly allocated screens ourselves.
  740. */
  741. vga_video_num_columns = c->vc_cols;
  742. vga_video_num_lines = c->vc_rows;
  743. /* We can only copy out the size of the video buffer here,
  744. * otherwise we get into VGA BIOS */
  745. if (!vga_is_gfx) {
  746. scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
  747. c->vc_screenbuf_size > vga_vram_size ?
  748. vga_vram_size : c->vc_screenbuf_size);
  749. if ((vgacon_xres != x || vgacon_yres != y) &&
  750. (!(vga_video_num_columns % 2) &&
  751. vga_video_num_columns <= screen_info.orig_video_cols &&
  752. vga_video_num_lines <= rows))
  753. vgacon_doresize(c, c->vc_cols, c->vc_rows);
  754. }
  755. vgacon_scrollback_init(c->vc_size_row);
  756. return 0; /* Redrawing not needed */
  757. }
  758. static void vga_set_palette(struct vc_data *vc, unsigned char *table)
  759. {
  760. int i, j;
  761. vga_w(vgastate.vgabase, VGA_PEL_MSK, 0xff);
  762. for (i = j = 0; i < 16; i++) {
  763. vga_w(vgastate.vgabase, VGA_PEL_IW, table[i]);
  764. vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
  765. vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
  766. vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
  767. }
  768. }
  769. static int vgacon_set_palette(struct vc_data *vc, unsigned char *table)
  770. {
  771. #ifdef CAN_LOAD_PALETTE
  772. if (vga_video_type != VIDEO_TYPE_VGAC || vga_palette_blanked
  773. || !CON_IS_VISIBLE(vc))
  774. return -EINVAL;
  775. vga_set_palette(vc, table);
  776. return 0;
  777. #else
  778. return -EINVAL;
  779. #endif
  780. }
  781. /* structure holding original VGA register settings */
  782. static struct {
  783. unsigned char SeqCtrlIndex; /* Sequencer Index reg. */
  784. unsigned char CrtCtrlIndex; /* CRT-Contr. Index reg. */
  785. unsigned char CrtMiscIO; /* Miscellaneous register */
  786. unsigned char HorizontalTotal; /* CRT-Controller:00h */
  787. unsigned char HorizDisplayEnd; /* CRT-Controller:01h */
  788. unsigned char StartHorizRetrace; /* CRT-Controller:04h */
  789. unsigned char EndHorizRetrace; /* CRT-Controller:05h */
  790. unsigned char Overflow; /* CRT-Controller:07h */
  791. unsigned char StartVertRetrace; /* CRT-Controller:10h */
  792. unsigned char EndVertRetrace; /* CRT-Controller:11h */
  793. unsigned char ModeControl; /* CRT-Controller:17h */
  794. unsigned char ClockingMode; /* Seq-Controller:01h */
  795. } vga_state;
  796. static void vga_vesa_blank(struct vgastate *state, int mode)
  797. {
  798. /* save original values of VGA controller registers */
  799. if (!vga_vesa_blanked) {
  800. raw_spin_lock_irq(&vga_lock);
  801. vga_state.SeqCtrlIndex = vga_r(state->vgabase, VGA_SEQ_I);
  802. vga_state.CrtCtrlIndex = inb_p(vga_video_port_reg);
  803. vga_state.CrtMiscIO = vga_r(state->vgabase, VGA_MIS_R);
  804. raw_spin_unlock_irq(&vga_lock);
  805. outb_p(0x00, vga_video_port_reg); /* HorizontalTotal */
  806. vga_state.HorizontalTotal = inb_p(vga_video_port_val);
  807. outb_p(0x01, vga_video_port_reg); /* HorizDisplayEnd */
  808. vga_state.HorizDisplayEnd = inb_p(vga_video_port_val);
  809. outb_p(0x04, vga_video_port_reg); /* StartHorizRetrace */
  810. vga_state.StartHorizRetrace = inb_p(vga_video_port_val);
  811. outb_p(0x05, vga_video_port_reg); /* EndHorizRetrace */
  812. vga_state.EndHorizRetrace = inb_p(vga_video_port_val);
  813. outb_p(0x07, vga_video_port_reg); /* Overflow */
  814. vga_state.Overflow = inb_p(vga_video_port_val);
  815. outb_p(0x10, vga_video_port_reg); /* StartVertRetrace */
  816. vga_state.StartVertRetrace = inb_p(vga_video_port_val);
  817. outb_p(0x11, vga_video_port_reg); /* EndVertRetrace */
  818. vga_state.EndVertRetrace = inb_p(vga_video_port_val);
  819. outb_p(0x17, vga_video_port_reg); /* ModeControl */
  820. vga_state.ModeControl = inb_p(vga_video_port_val);
  821. vga_state.ClockingMode = vga_rseq(state->vgabase, VGA_SEQ_CLOCK_MODE);
  822. }
  823. /* assure that video is enabled */
  824. /* "0x20" is VIDEO_ENABLE_bit in register 01 of sequencer */
  825. raw_spin_lock_irq(&vga_lock);
  826. vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, vga_state.ClockingMode | 0x20);
  827. /* test for vertical retrace in process.... */
  828. if ((vga_state.CrtMiscIO & 0x80) == 0x80)
  829. vga_w(state->vgabase, VGA_MIS_W, vga_state.CrtMiscIO & 0xEF);
  830. /*
  831. * Set <End of vertical retrace> to minimum (0) and
  832. * <Start of vertical Retrace> to maximum (incl. overflow)
  833. * Result: turn off vertical sync (VSync) pulse.
  834. */
  835. if (mode & VESA_VSYNC_SUSPEND) {
  836. outb_p(0x10, vga_video_port_reg); /* StartVertRetrace */
  837. outb_p(0xff, vga_video_port_val); /* maximum value */
  838. outb_p(0x11, vga_video_port_reg); /* EndVertRetrace */
  839. outb_p(0x40, vga_video_port_val); /* minimum (bits 0..3) */
  840. outb_p(0x07, vga_video_port_reg); /* Overflow */
  841. outb_p(vga_state.Overflow | 0x84, vga_video_port_val); /* bits 9,10 of vert. retrace */
  842. }
  843. if (mode & VESA_HSYNC_SUSPEND) {
  844. /*
  845. * Set <End of horizontal retrace> to minimum (0) and
  846. * <Start of horizontal Retrace> to maximum
  847. * Result: turn off horizontal sync (HSync) pulse.
  848. */
  849. outb_p(0x04, vga_video_port_reg); /* StartHorizRetrace */
  850. outb_p(0xff, vga_video_port_val); /* maximum */
  851. outb_p(0x05, vga_video_port_reg); /* EndHorizRetrace */
  852. outb_p(0x00, vga_video_port_val); /* minimum (0) */
  853. }
  854. /* restore both index registers */
  855. vga_w(state->vgabase, VGA_SEQ_I, vga_state.SeqCtrlIndex);
  856. outb_p(vga_state.CrtCtrlIndex, vga_video_port_reg);
  857. raw_spin_unlock_irq(&vga_lock);
  858. }
  859. static void vga_vesa_unblank(struct vgastate *state)
  860. {
  861. /* restore original values of VGA controller registers */
  862. raw_spin_lock_irq(&vga_lock);
  863. vga_w(state->vgabase, VGA_MIS_W, vga_state.CrtMiscIO);
  864. outb_p(0x00, vga_video_port_reg); /* HorizontalTotal */
  865. outb_p(vga_state.HorizontalTotal, vga_video_port_val);
  866. outb_p(0x01, vga_video_port_reg); /* HorizDisplayEnd */
  867. outb_p(vga_state.HorizDisplayEnd, vga_video_port_val);
  868. outb_p(0x04, vga_video_port_reg); /* StartHorizRetrace */
  869. outb_p(vga_state.StartHorizRetrace, vga_video_port_val);
  870. outb_p(0x05, vga_video_port_reg); /* EndHorizRetrace */
  871. outb_p(vga_state.EndHorizRetrace, vga_video_port_val);
  872. outb_p(0x07, vga_video_port_reg); /* Overflow */
  873. outb_p(vga_state.Overflow, vga_video_port_val);
  874. outb_p(0x10, vga_video_port_reg); /* StartVertRetrace */
  875. outb_p(vga_state.StartVertRetrace, vga_video_port_val);
  876. outb_p(0x11, vga_video_port_reg); /* EndVertRetrace */
  877. outb_p(vga_state.EndVertRetrace, vga_video_port_val);
  878. outb_p(0x17, vga_video_port_reg); /* ModeControl */
  879. outb_p(vga_state.ModeControl, vga_video_port_val);
  880. /* ClockingMode */
  881. vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, vga_state.ClockingMode);
  882. /* restore index/control registers */
  883. vga_w(state->vgabase, VGA_SEQ_I, vga_state.SeqCtrlIndex);
  884. outb_p(vga_state.CrtCtrlIndex, vga_video_port_reg);
  885. raw_spin_unlock_irq(&vga_lock);
  886. }
  887. static void vga_pal_blank(struct vgastate *state)
  888. {
  889. int i;
  890. vga_w(state->vgabase, VGA_PEL_MSK, 0xff);
  891. for (i = 0; i < 16; i++) {
  892. vga_w(state->vgabase, VGA_PEL_IW, i);
  893. vga_w(state->vgabase, VGA_PEL_D, 0);
  894. vga_w(state->vgabase, VGA_PEL_D, 0);
  895. vga_w(state->vgabase, VGA_PEL_D, 0);
  896. }
  897. }
  898. static int vgacon_blank(struct vc_data *c, int blank, int mode_switch)
  899. {
  900. switch (blank) {
  901. case 0: /* Unblank */
  902. if (vga_vesa_blanked) {
  903. vga_vesa_unblank(&vgastate);
  904. vga_vesa_blanked = 0;
  905. }
  906. if (vga_palette_blanked) {
  907. vga_set_palette(c, color_table);
  908. vga_palette_blanked = 0;
  909. return 0;
  910. }
  911. vga_is_gfx = 0;
  912. /* Tell console.c that it has to restore the screen itself */
  913. return 1;
  914. case 1: /* Normal blanking */
  915. case -1: /* Obsolete */
  916. if (!mode_switch && vga_video_type == VIDEO_TYPE_VGAC) {
  917. vga_pal_blank(&vgastate);
  918. vga_palette_blanked = 1;
  919. return 0;
  920. }
  921. vgacon_set_origin(c);
  922. scr_memsetw((void *) vga_vram_base, BLANK,
  923. c->vc_screenbuf_size);
  924. if (mode_switch)
  925. vga_is_gfx = 1;
  926. return 1;
  927. default: /* VESA blanking */
  928. if (vga_video_type == VIDEO_TYPE_VGAC) {
  929. vga_vesa_blank(&vgastate, blank - 1);
  930. vga_vesa_blanked = blank;
  931. }
  932. return 0;
  933. }
  934. }
  935. /*
  936. * PIO_FONT support.
  937. *
  938. * The font loading code goes back to the codepage package by
  939. * Joel Hoffman (joel@wam.umd.edu). (He reports that the original
  940. * reference is: "From: p. 307 of _Programmer's Guide to PC & PS/2
  941. * Video Systems_ by Richard Wilton. 1987. Microsoft Press".)
  942. *
  943. * Change for certain monochrome monitors by Yury Shevchuck
  944. * (sizif@botik.yaroslavl.su).
  945. */
  946. #ifdef CAN_LOAD_EGA_FONTS
  947. #define colourmap 0xa0000
  948. /* Pauline Middelink <middelin@polyware.iaf.nl> reports that we
  949. should use 0xA0000 for the bwmap as well.. */
  950. #define blackwmap 0xa0000
  951. #define cmapsz 8192
  952. static int vgacon_do_font_op(struct vgastate *state,char *arg,int set,int ch512)
  953. {
  954. unsigned short video_port_status = vga_video_port_reg + 6;
  955. int font_select = 0x00, beg, i;
  956. char *charmap;
  957. bool clear_attribs = false;
  958. if (vga_video_type != VIDEO_TYPE_EGAM) {
  959. charmap = (char *) VGA_MAP_MEM(colourmap, 0);
  960. beg = 0x0e;
  961. #ifdef VGA_CAN_DO_64KB
  962. if (vga_video_type == VIDEO_TYPE_VGAC)
  963. beg = 0x06;
  964. #endif
  965. } else {
  966. charmap = (char *) VGA_MAP_MEM(blackwmap, 0);
  967. beg = 0x0a;
  968. }
  969. #ifdef BROKEN_GRAPHICS_PROGRAMS
  970. /*
  971. * All fonts are loaded in slot 0 (0:1 for 512 ch)
  972. */
  973. if (!arg)
  974. return -EINVAL; /* Return to default font not supported */
  975. vga_font_is_default = 0;
  976. font_select = ch512 ? 0x04 : 0x00;
  977. #else
  978. /*
  979. * The default font is kept in slot 0 and is never touched.
  980. * A custom font is loaded in slot 2 (256 ch) or 2:3 (512 ch)
  981. */
  982. if (set) {
  983. vga_font_is_default = !arg;
  984. if (!arg)
  985. ch512 = 0; /* Default font is always 256 */
  986. font_select = arg ? (ch512 ? 0x0e : 0x0a) : 0x00;
  987. }
  988. if (!vga_font_is_default)
  989. charmap += 4 * cmapsz;
  990. #endif
  991. raw_spin_lock_irq(&vga_lock);
  992. /* First, the Sequencer */
  993. vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
  994. /* CPU writes only to map 2 */
  995. vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x04);
  996. /* Sequential addressing */
  997. vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x07);
  998. /* Clear synchronous reset */
  999. vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x03);
  1000. /* Now, the graphics controller, select map 2 */
  1001. vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x02);
  1002. /* disable odd-even addressing */
  1003. vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x00);
  1004. /* map start at A000:0000 */
  1005. vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x00);
  1006. raw_spin_unlock_irq(&vga_lock);
  1007. if (arg) {
  1008. if (set)
  1009. for (i = 0; i < cmapsz; i++) {
  1010. vga_writeb(arg[i], charmap + i);
  1011. cond_resched();
  1012. }
  1013. else
  1014. for (i = 0; i < cmapsz; i++) {
  1015. arg[i] = vga_readb(charmap + i);
  1016. cond_resched();
  1017. }
  1018. /*
  1019. * In 512-character mode, the character map is not contiguous if
  1020. * we want to remain EGA compatible -- which we do
  1021. */
  1022. if (ch512) {
  1023. charmap += 2 * cmapsz;
  1024. arg += cmapsz;
  1025. if (set)
  1026. for (i = 0; i < cmapsz; i++) {
  1027. vga_writeb(arg[i], charmap + i);
  1028. cond_resched();
  1029. }
  1030. else
  1031. for (i = 0; i < cmapsz; i++) {
  1032. arg[i] = vga_readb(charmap + i);
  1033. cond_resched();
  1034. }
  1035. }
  1036. }
  1037. raw_spin_lock_irq(&vga_lock);
  1038. /* First, the sequencer, Synchronous reset */
  1039. vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x01);
  1040. /* CPU writes to maps 0 and 1 */
  1041. vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x03);
  1042. /* odd-even addressing */
  1043. vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x03);
  1044. /* Character Map Select */
  1045. if (set)
  1046. vga_wseq(state->vgabase, VGA_SEQ_CHARACTER_MAP, font_select);
  1047. /* clear synchronous reset */
  1048. vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x03);
  1049. /* Now, the graphics controller, select map 0 for CPU */
  1050. vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x00);
  1051. /* enable even-odd addressing */
  1052. vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x10);
  1053. /* map starts at b800:0 or b000:0 */
  1054. vga_wgfx(state->vgabase, VGA_GFX_MISC, beg);
  1055. /* if 512 char mode is already enabled don't re-enable it. */
  1056. if ((set) && (ch512 != vga_512_chars)) {
  1057. vga_512_chars = ch512;
  1058. /* 256-char: enable intensity bit
  1059. 512-char: disable intensity bit */
  1060. inb_p(video_port_status); /* clear address flip-flop */
  1061. /* color plane enable register */
  1062. vga_wattr(state->vgabase, VGA_ATC_PLANE_ENABLE, ch512 ? 0x07 : 0x0f);
  1063. /* Wilton (1987) mentions the following; I don't know what
  1064. it means, but it works, and it appears necessary */
  1065. inb_p(video_port_status);
  1066. vga_wattr(state->vgabase, VGA_AR_ENABLE_DISPLAY, 0);
  1067. clear_attribs = true;
  1068. }
  1069. raw_spin_unlock_irq(&vga_lock);
  1070. if (clear_attribs) {
  1071. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1072. struct vc_data *c = vc_cons[i].d;
  1073. if (c && c->vc_sw == &vga_con) {
  1074. /* force hi font mask to 0, so we always clear
  1075. the bit on either transition */
  1076. c->vc_hi_font_mask = 0x00;
  1077. clear_buffer_attributes(c);
  1078. c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
  1079. }
  1080. }
  1081. }
  1082. return 0;
  1083. }
  1084. /*
  1085. * Adjust the screen to fit a font of a certain height
  1086. */
  1087. static int vgacon_adjust_height(struct vc_data *vc, unsigned fontheight)
  1088. {
  1089. unsigned char ovr, vde, fsr;
  1090. int rows, maxscan, i;
  1091. rows = vc->vc_scan_lines / fontheight; /* Number of video rows we end up with */
  1092. maxscan = rows * fontheight - 1; /* Scan lines to actually display-1 */
  1093. /* Reprogram the CRTC for the new font size
  1094. Note: the attempt to read the overflow register will fail
  1095. on an EGA, but using 0xff for the previous value appears to
  1096. be OK for EGA text modes in the range 257-512 scan lines, so I
  1097. guess we don't need to worry about it.
  1098. The same applies for the spill bits in the font size and cursor
  1099. registers; they are write-only on EGA, but it appears that they
  1100. are all don't care bits on EGA, so I guess it doesn't matter. */
  1101. raw_spin_lock_irq(&vga_lock);
  1102. outb_p(0x07, vga_video_port_reg); /* CRTC overflow register */
  1103. ovr = inb_p(vga_video_port_val);
  1104. outb_p(0x09, vga_video_port_reg); /* Font size register */
  1105. fsr = inb_p(vga_video_port_val);
  1106. raw_spin_unlock_irq(&vga_lock);
  1107. vde = maxscan & 0xff; /* Vertical display end reg */
  1108. ovr = (ovr & 0xbd) + /* Overflow register */
  1109. ((maxscan & 0x100) >> 7) + ((maxscan & 0x200) >> 3);
  1110. fsr = (fsr & 0xe0) + (fontheight - 1); /* Font size register */
  1111. raw_spin_lock_irq(&vga_lock);
  1112. outb_p(0x07, vga_video_port_reg); /* CRTC overflow register */
  1113. outb_p(ovr, vga_video_port_val);
  1114. outb_p(0x09, vga_video_port_reg); /* Font size */
  1115. outb_p(fsr, vga_video_port_val);
  1116. outb_p(0x12, vga_video_port_reg); /* Vertical display limit */
  1117. outb_p(vde, vga_video_port_val);
  1118. raw_spin_unlock_irq(&vga_lock);
  1119. vga_video_font_height = fontheight;
  1120. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1121. struct vc_data *c = vc_cons[i].d;
  1122. if (c && c->vc_sw == &vga_con) {
  1123. if (CON_IS_VISIBLE(c)) {
  1124. /* void size to cause regs to be rewritten */
  1125. cursor_size_lastfrom = 0;
  1126. cursor_size_lastto = 0;
  1127. c->vc_sw->con_cursor(c, CM_DRAW);
  1128. }
  1129. c->vc_font.height = fontheight;
  1130. vc_resize(c, 0, rows); /* Adjust console size */
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. static int vgacon_font_set(struct vc_data *c, struct console_font *font, unsigned flags)
  1136. {
  1137. unsigned charcount = font->charcount;
  1138. int rc;
  1139. if (vga_video_type < VIDEO_TYPE_EGAM)
  1140. return -EINVAL;
  1141. if (font->width != VGA_FONTWIDTH ||
  1142. (charcount != 256 && charcount != 512))
  1143. return -EINVAL;
  1144. rc = vgacon_do_font_op(&vgastate, font->data, 1, charcount == 512);
  1145. if (rc)
  1146. return rc;
  1147. if (!(flags & KD_FONT_FLAG_DONT_RECALC))
  1148. rc = vgacon_adjust_height(c, font->height);
  1149. return rc;
  1150. }
  1151. static int vgacon_font_get(struct vc_data *c, struct console_font *font)
  1152. {
  1153. if (vga_video_type < VIDEO_TYPE_EGAM)
  1154. return -EINVAL;
  1155. font->width = VGA_FONTWIDTH;
  1156. font->height = c->vc_font.height;
  1157. font->charcount = vga_512_chars ? 512 : 256;
  1158. if (!font->data)
  1159. return 0;
  1160. return vgacon_do_font_op(&vgastate, font->data, 0, vga_512_chars);
  1161. }
  1162. #else
  1163. #define vgacon_font_set NULL
  1164. #define vgacon_font_get NULL
  1165. #endif
  1166. static int vgacon_resize(struct vc_data *c, unsigned int width,
  1167. unsigned int height, unsigned int user)
  1168. {
  1169. if (width % 2 || width > screen_info.orig_video_cols ||
  1170. height > (screen_info.orig_video_lines * vga_default_font_height)/
  1171. c->vc_font.height)
  1172. /* let svgatextmode tinker with video timings and
  1173. return success */
  1174. return (user) ? 0 : -EINVAL;
  1175. if (CON_IS_VISIBLE(c) && !vga_is_gfx) /* who knows */
  1176. vgacon_doresize(c, width, height);
  1177. return 0;
  1178. }
  1179. static int vgacon_set_origin(struct vc_data *c)
  1180. {
  1181. if (vga_is_gfx || /* We don't play origin tricks in graphic modes */
  1182. (console_blanked && !vga_palette_blanked)) /* Nor we write to blanked screens */
  1183. return 0;
  1184. c->vc_origin = c->vc_visible_origin = vga_vram_base;
  1185. vga_set_mem_top(c);
  1186. vga_rolled_over = 0;
  1187. return 1;
  1188. }
  1189. static void vgacon_save_screen(struct vc_data *c)
  1190. {
  1191. static int vga_bootup_console = 0;
  1192. if (!vga_bootup_console) {
  1193. /* This is a gross hack, but here is the only place we can
  1194. * set bootup console parameters without messing up generic
  1195. * console initialization routines.
  1196. */
  1197. vga_bootup_console = 1;
  1198. c->vc_x = screen_info.orig_x;
  1199. c->vc_y = screen_info.orig_y;
  1200. }
  1201. /* We can't copy in more than the size of the video buffer,
  1202. * or we'll be copying in VGA BIOS */
  1203. if (!vga_is_gfx)
  1204. scr_memcpyw((u16 *) c->vc_screenbuf, (u16 *) c->vc_origin,
  1205. c->vc_screenbuf_size > vga_vram_size ? vga_vram_size : c->vc_screenbuf_size);
  1206. }
  1207. static int vgacon_scroll(struct vc_data *c, int t, int b, int dir,
  1208. int lines)
  1209. {
  1210. unsigned long oldo;
  1211. unsigned int delta;
  1212. if (t || b != c->vc_rows || vga_is_gfx || c->vc_mode != KD_TEXT)
  1213. return 0;
  1214. if (!vga_hardscroll_enabled || lines >= c->vc_rows / 2)
  1215. return 0;
  1216. vgacon_restore_screen(c);
  1217. oldo = c->vc_origin;
  1218. delta = lines * c->vc_size_row;
  1219. if (dir == SM_UP) {
  1220. vgacon_scrollback_update(c, t, lines);
  1221. if (c->vc_scr_end + delta >= vga_vram_end) {
  1222. scr_memcpyw((u16 *) vga_vram_base,
  1223. (u16 *) (oldo + delta),
  1224. c->vc_screenbuf_size - delta);
  1225. c->vc_origin = vga_vram_base;
  1226. vga_rolled_over = oldo - vga_vram_base;
  1227. } else
  1228. c->vc_origin += delta;
  1229. scr_memsetw((u16 *) (c->vc_origin + c->vc_screenbuf_size -
  1230. delta), c->vc_video_erase_char,
  1231. delta);
  1232. } else {
  1233. if (oldo - delta < vga_vram_base) {
  1234. scr_memmovew((u16 *) (vga_vram_end -
  1235. c->vc_screenbuf_size +
  1236. delta), (u16 *) oldo,
  1237. c->vc_screenbuf_size - delta);
  1238. c->vc_origin = vga_vram_end - c->vc_screenbuf_size;
  1239. vga_rolled_over = 0;
  1240. } else
  1241. c->vc_origin -= delta;
  1242. c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
  1243. scr_memsetw((u16 *) (c->vc_origin), c->vc_video_erase_char,
  1244. delta);
  1245. }
  1246. c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
  1247. c->vc_visible_origin = c->vc_origin;
  1248. vga_set_mem_top(c);
  1249. c->vc_pos = (c->vc_pos - oldo) + c->vc_origin;
  1250. return 1;
  1251. }
  1252. /*
  1253. * The console `switch' structure for the VGA based console
  1254. */
  1255. static int vgacon_dummy(struct vc_data *c)
  1256. {
  1257. return 0;
  1258. }
  1259. #define DUMMY (void *) vgacon_dummy
  1260. const struct consw vga_con = {
  1261. .owner = THIS_MODULE,
  1262. .con_startup = vgacon_startup,
  1263. .con_init = vgacon_init,
  1264. .con_deinit = vgacon_deinit,
  1265. .con_clear = DUMMY,
  1266. .con_putc = DUMMY,
  1267. .con_putcs = DUMMY,
  1268. .con_cursor = vgacon_cursor,
  1269. .con_scroll = vgacon_scroll,
  1270. .con_bmove = DUMMY,
  1271. .con_switch = vgacon_switch,
  1272. .con_blank = vgacon_blank,
  1273. .con_font_set = vgacon_font_set,
  1274. .con_font_get = vgacon_font_get,
  1275. .con_resize = vgacon_resize,
  1276. .con_set_palette = vgacon_set_palette,
  1277. .con_scrolldelta = vgacon_scrolldelta,
  1278. .con_set_origin = vgacon_set_origin,
  1279. .con_save_screen = vgacon_save_screen,
  1280. .con_build_attr = vgacon_build_attr,
  1281. .con_invert_region = vgacon_invert_region,
  1282. };
  1283. EXPORT_SYMBOL(vga_con);
  1284. MODULE_LICENSE("GPL");