vesa.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. /*
  2. Copyright (c) 2000 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <kdrive-config.h>
  21. #endif
  22. #include "vesa.h"
  23. #include "vga.h"
  24. #include "vbe.h"
  25. #include <randrstr.h>
  26. static int vesa_video_mode = 0;
  27. static Bool vesa_force_mode = FALSE;
  28. static Bool vesa_swap_rgb = FALSE;
  29. static Bool vesa_shadow = TRUE;
  30. static Bool vesa_linear_fb = TRUE;
  31. static Bool vesa_verbose = FALSE;
  32. static Bool vesa_force_text = FALSE;
  33. static Bool vesa_restore_font = TRUE;
  34. static Bool vesa_map_holes = TRUE;
  35. static Bool vesa_boot = FALSE;
  36. #define VesaPriv(scr) ((VesaScreenPrivPtr) (scr)->driver)
  37. #define vesaWidth(scr,vmib) ((vmib)->XResolution)
  38. #define vesaHeight(scr,vmib) ((vmib)->YResolution)
  39. static Bool vesaComputeFramebufferMapping(KdScreenInfo * screen);
  40. static Bool vesaMapFramebuffer(KdScreenInfo * screen);
  41. static Bool vesaModeSupportable(VesaModePtr mode, Bool complain)
  42. {
  43. if ((mode->ModeAttributes & 0x10) == 0) {
  44. if (complain)
  45. ErrorF("Text mode specified.\n");
  46. return FALSE;
  47. }
  48. if (mode->MemoryModel != 0x06 && mode->MemoryModel != 0x04
  49. && mode->MemoryModel != 0x03) {
  50. if (complain)
  51. ErrorF("Unsupported memory model 0x%X\n",
  52. mode->MemoryModel);
  53. return FALSE;
  54. }
  55. if ((mode->ModeAttributes & 0x80) == 0) {
  56. if ((mode->ModeAttributes & 0x40) != 0) {
  57. if (complain)
  58. ErrorF
  59. ("Neither linear nor windowed framebuffer available in this mode\n");
  60. return FALSE;
  61. }
  62. }
  63. if (!(mode->ModeAttributes & 1)) {
  64. if (complain)
  65. ErrorF("Mode not supported on this hardware\n");
  66. return FALSE;
  67. }
  68. if (mode->BitsPerPixel < 5) {
  69. if (complain)
  70. ErrorF("Skipping low color mode\n");
  71. return FALSE;
  72. }
  73. return TRUE;
  74. }
  75. static Bool
  76. vesaModeSupported(VesaCardPrivPtr priv, VesaModePtr mode, Bool complain)
  77. {
  78. if (!priv->vbeInfo && mode->vbe) {
  79. if (complain)
  80. ErrorF("VBE bios mode not usable.\n");
  81. return FALSE;
  82. }
  83. return vesaModeSupportable(mode, complain);
  84. }
  85. static int vesaDepth(VesaModePtr mode)
  86. {
  87. if (mode->MemoryModel == MEMORY_DIRECT)
  88. return (mode->RedMaskSize +
  89. mode->GreenMaskSize + mode->BlueMaskSize);
  90. else
  91. return mode->BitsPerPixel;
  92. }
  93. static void vesaReportMode(VesaModePtr mode)
  94. {
  95. int supported = (mode->ModeAttributes & MODE_SUPPORTED) ? 1 : 0;
  96. int colour = (mode->ModeAttributes & MODE_COLOUR) ? 1 : 0;
  97. int graphics = (mode->ModeAttributes & MODE_GRAPHICS) ? 1 : 0;
  98. int vga_compatible = !((mode->ModeAttributes & MODE_VGA) ? 1 : 0);
  99. int linear_fb = (mode->ModeAttributes & MODE_LINEAR) ? 1 : 0;
  100. ErrorF("0x%04X: %dx%dx%d%s%s%s%s",
  101. (unsigned)mode->mode,
  102. (int)mode->XResolution, (int)mode->YResolution,
  103. vesaDepth(mode),
  104. colour ? "" : " (monochrome)",
  105. graphics ? "" : " (graphics)",
  106. vga_compatible ? "" : " (vga compatible)",
  107. linear_fb ? "" : " (linear frame buffer)");
  108. switch (mode->MemoryModel) {
  109. case MEMORY_TEXT:
  110. ErrorF(" text mode");
  111. break;
  112. case MEMORY_CGA:
  113. ErrorF(" CGA graphics");
  114. break;
  115. case MEMORY_HERCULES:
  116. ErrorF(" Hercules graphics");
  117. break;
  118. case MEMORY_PLANAR:
  119. ErrorF(" Planar (%d planes)", mode->NumberOfPlanes);
  120. break;
  121. case MEMORY_PSEUDO:
  122. ErrorF(" PseudoColor");
  123. break;
  124. case MEMORY_NONCHAIN:
  125. ErrorF(" Non-chain 4, 256 colour");
  126. break;
  127. case MEMORY_DIRECT:
  128. if (mode->DirectColorModeInfo & MODE_DIRECT)
  129. ErrorF(" DirectColor");
  130. else
  131. ErrorF(" TrueColor");
  132. ErrorF(" [%d:%d:%d:%d]",
  133. mode->RedMaskSize, mode->GreenMaskSize,
  134. mode->BlueMaskSize, mode->RsvdMaskSize);
  135. if (mode->DirectColorModeInfo & 2)
  136. ErrorF(" (reserved bits are reserved)");
  137. break;
  138. case MEMORY_YUV:
  139. ErrorF("YUV");
  140. break;
  141. default:
  142. ErrorF("unknown MemoryModel 0x%X ", mode->MemoryModel);
  143. }
  144. if (!supported)
  145. ErrorF(" (unsupported)");
  146. else if (!linear_fb)
  147. ErrorF(" (no linear framebuffer)");
  148. ErrorF("\n");
  149. }
  150. static VesaModePtr vesaGetModes(Vm86InfoPtr vi, int *ret_nmode)
  151. {
  152. VesaModePtr modes;
  153. int nmode, nmodeVbe, nmodeVga;
  154. int code;
  155. code = VgaGetNmode(vi);
  156. if (code <= 0)
  157. nmodeVga = 0;
  158. else
  159. nmodeVga = code;
  160. code = VbeGetNmode(vi);
  161. if (code <= 0)
  162. nmodeVbe = 0;
  163. else
  164. nmodeVbe = code;
  165. nmode = nmodeVga + nmodeVbe;
  166. if (nmode <= 0)
  167. return 0;
  168. modes = malloc(nmode * sizeof(VesaModeRec));
  169. memset(modes, '\0', nmode * sizeof(VesaModeRec));
  170. if (nmodeVga) {
  171. code = VgaGetModes(vi, modes, nmodeVga);
  172. if (code <= 0)
  173. nmodeVga = 0;
  174. else
  175. nmodeVga = code;
  176. }
  177. if (nmodeVbe) {
  178. code = VbeGetModes(vi, modes + nmodeVga, nmodeVbe);
  179. if (code <= 0)
  180. nmodeVbe = 0;
  181. else
  182. nmodeVbe = code;
  183. }
  184. nmode = nmodeVga + nmodeVbe;
  185. if (nmode == 0) {
  186. free(modes);
  187. modes = 0;
  188. return 0;
  189. }
  190. *ret_nmode = nmode;
  191. return modes;
  192. }
  193. static Bool vesaInitialize(KdCardInfo * card, VesaCardPrivPtr priv)
  194. {
  195. priv->vi = Vm86Setup(vesa_map_holes);
  196. if (!priv->vi)
  197. goto fail;
  198. if (vesa_boot)
  199. VbeBoot(priv->vi);
  200. priv->modes = vesaGetModes(priv->vi, &priv->nmode);
  201. if (!priv->modes)
  202. goto fail;
  203. priv->vbeInfo = VbeInit(priv->vi);
  204. card->driver = priv;
  205. return TRUE;
  206. fail:
  207. if (priv->vi)
  208. Vm86Cleanup(priv->vi);
  209. return FALSE;
  210. }
  211. static void vesaListModes(void)
  212. {
  213. Vm86InfoPtr vi;
  214. VesaModePtr modes;
  215. int nmode;
  216. int n;
  217. vi = Vm86Setup(vesa_map_holes);
  218. if (!vi) {
  219. ErrorF("Can't setup vm86\n");
  220. } else {
  221. modes = vesaGetModes(vi, &nmode);
  222. if (!modes) {
  223. ErrorF("No modes available\n");
  224. } else {
  225. VbeReportInfo(vi);
  226. for (n = 0; n < nmode; n++) {
  227. if (vesa_force_mode
  228. || vesaModeSupportable(modes + n, 0))
  229. vesaReportMode(modes + n);
  230. }
  231. free(modes);
  232. }
  233. Vm86Cleanup(vi);
  234. }
  235. }
  236. static void vesaTestMode(void)
  237. {
  238. Vm86InfoPtr vi;
  239. VesaModePtr modes;
  240. VesaModePtr mode;
  241. VbeInfoPtr vbeInfo;
  242. int nmode;
  243. int n;
  244. vi = Vm86Setup(vesa_map_holes);
  245. if (!vi) {
  246. ErrorF("Can't setup vm86\n");
  247. return;
  248. }
  249. modes = vesaGetModes(vi, &nmode);
  250. if (!modes) {
  251. ErrorF("No modes available\n");
  252. return;
  253. }
  254. VbeReportInfo(vi);
  255. vbeInfo = VbeInit(vi);
  256. for (n = 0; n < nmode; n++) {
  257. if (modes[n].mode == vesa_video_mode)
  258. break;
  259. }
  260. if (n == nmode) {
  261. ErrorF("no mode specified\n");
  262. return;
  263. }
  264. mode = &modes[n];
  265. if (mode->vbe) {
  266. ErrorF("Enable VBE mode 0x%x\n", mode->mode);
  267. VbeSetMode(vi, vbeInfo, mode->mode, FALSE, FALSE);
  268. } else {
  269. ErrorF("Enable BIOS mode 0x%x\n", mode->mode);
  270. VgaSetMode(vi, mode->mode);
  271. }
  272. sleep(2);
  273. ErrorF("Restore BIOS mode 0x%x\n", 3);
  274. VgaSetMode(vi, 3);
  275. free(modes);
  276. Vm86Cleanup(vi);
  277. }
  278. Bool vesaCardInit(KdCardInfo * card)
  279. {
  280. VesaCardPrivPtr priv;
  281. priv = malloc(sizeof(VesaCardPrivRec));
  282. if (!priv)
  283. return FALSE;
  284. if (!vesaInitialize(card, priv)) {
  285. free(priv);
  286. return FALSE;
  287. }
  288. return TRUE;
  289. }
  290. static Bool vesaModeGood(KdScreenInfo * screen, VesaModePtr a)
  291. {
  292. if (vesaWidth(screen, a) <= screen->width &&
  293. vesaHeight(screen, a) <= screen->height &&
  294. vesaDepth(a) >= screen->fb.depth) {
  295. return TRUE;
  296. }
  297. return FALSE;
  298. }
  299. #define vabs(a) ((a) >= 0 ? (a) : -(a))
  300. static int vesaSizeError(KdScreenInfo * screen, VesaModePtr a)
  301. {
  302. int xdist, ydist;
  303. xdist = vabs(screen->width - vesaWidth(screen, a));
  304. ydist = vabs(screen->height - vesaHeight(screen, a));
  305. return xdist * xdist + ydist * ydist;
  306. }
  307. static Bool vesaModeBetter(KdScreenInfo * screen, VesaModePtr a, VesaModePtr b)
  308. {
  309. int aerr, berr;
  310. if (vesaModeGood(screen, a)) {
  311. if (!vesaModeGood(screen, b))
  312. return TRUE;
  313. } else {
  314. if (vesaModeGood(screen, b))
  315. return FALSE;
  316. }
  317. aerr = vesaSizeError(screen, a);
  318. berr = vesaSizeError(screen, b);
  319. if (aerr < berr)
  320. return TRUE;
  321. if (berr < aerr)
  322. return FALSE;
  323. if (vabs(screen->fb.depth - vesaDepth(a)) <
  324. vabs(screen->fb.depth - vesaDepth(b)))
  325. return TRUE;
  326. if (a->BitsPerPixel == 32 && b->BitsPerPixel == 24)
  327. return TRUE;
  328. return FALSE;
  329. }
  330. static VesaModePtr vesaSelectMode(KdScreenInfo * screen)
  331. {
  332. VesaCardPrivPtr priv = screen->card->driver;
  333. int i, best;
  334. if (vesa_video_mode) {
  335. for (best = 0; best < priv->nmode; best++)
  336. if (priv->modes[best].mode == vesa_video_mode &&
  337. (vesaModeSupported(priv, &priv->modes[best], FALSE)
  338. || vesa_force_mode))
  339. return &priv->modes[best];
  340. }
  341. for (best = 0; best < priv->nmode; best++) {
  342. if (vesaModeSupported(priv, &priv->modes[best], FALSE))
  343. break;
  344. }
  345. if (best == priv->nmode)
  346. return 0;
  347. for (i = best + 1; i < priv->nmode; i++)
  348. if (vesaModeSupported(priv, &priv->modes[i], FALSE) &&
  349. vesaModeBetter(screen, &priv->modes[i], &priv->modes[best]))
  350. best = i;
  351. return &priv->modes[best];
  352. }
  353. static Bool vesaScreenInitialize(KdScreenInfo * screen, VesaScreenPrivPtr pscr)
  354. {
  355. VesaModePtr mode;
  356. screen->driver = pscr;
  357. if (!screen->width || !screen->height) {
  358. screen->width = 800;
  359. screen->height = 600;
  360. }
  361. if (!screen->fb.depth)
  362. screen->fb.depth = 16;
  363. if (vesa_verbose)
  364. ErrorF("Mode requested %dx%dx%d\n",
  365. screen->width, screen->height, screen->fb.depth);
  366. mode = vesaSelectMode(screen);
  367. if (!mode) {
  368. if (vesa_verbose)
  369. ErrorF("No selectable mode\n");
  370. return FALSE;
  371. }
  372. pscr->mode = *mode;
  373. if (vesa_verbose) {
  374. ErrorF("\t");
  375. vesaReportMode(&pscr->mode);
  376. }
  377. pscr->randr = screen->randr;
  378. pscr->shadow = vesa_shadow;
  379. pscr->origDepth = screen->fb.depth;
  380. /*
  381. * Compute visual support for the selected depth
  382. */
  383. switch (pscr->mode.MemoryModel) {
  384. case MEMORY_DIRECT:
  385. /* TrueColor or DirectColor */
  386. screen->fb.visuals = (1 << TrueColor);
  387. screen->fb.redMask =
  388. FbStipMask(pscr->mode.RedFieldPosition,
  389. pscr->mode.RedMaskSize);
  390. screen->fb.greenMask =
  391. FbStipMask(pscr->mode.GreenFieldPosition,
  392. pscr->mode.GreenMaskSize);
  393. screen->fb.blueMask =
  394. FbStipMask(pscr->mode.BlueFieldPosition,
  395. pscr->mode.BlueMaskSize);
  396. break;
  397. case MEMORY_PSEUDO:
  398. /* PseudoColor */
  399. screen->fb.visuals = ((1 << StaticGray) |
  400. (1 << GrayScale) |
  401. (1 << StaticColor) |
  402. (1 << PseudoColor) |
  403. (1 << TrueColor) | (1 << DirectColor));
  404. screen->fb.blueMask = 0x00;
  405. screen->fb.greenMask = 0x00;
  406. screen->fb.redMask = 0x00;
  407. break;
  408. case MEMORY_PLANAR:
  409. /* 4 plane planar */
  410. if (pscr->mode.ModeAttributes & MODE_COLOUR)
  411. screen->fb.visuals = (1 << StaticColor);
  412. else
  413. screen->fb.visuals = (1 << StaticGray);
  414. screen->fb.blueMask = 0x00;
  415. screen->fb.greenMask = 0x00;
  416. screen->fb.redMask = 0x00;
  417. break;
  418. default:
  419. ErrorF("Unsupported VESA MemoryModel 0x%02X\n",
  420. pscr->mode.MemoryModel);
  421. return FALSE;
  422. }
  423. screen->rate = 72;
  424. if (!vesaComputeFramebufferMapping(screen))
  425. return FALSE;
  426. if (!vesaMapFramebuffer(screen))
  427. return FALSE;
  428. return TRUE;
  429. }
  430. Bool vesaScreenInit(KdScreenInfo * screen)
  431. {
  432. VesaScreenPrivPtr pscr;
  433. pscr = calloc(1, sizeof(VesaScreenPrivRec));
  434. if (!pscr)
  435. return FALSE;
  436. if (!vesaScreenInitialize(screen, pscr))
  437. return FALSE;
  438. return TRUE;
  439. }
  440. static void *vesaSetWindowPlanar(ScreenPtr pScreen,
  441. CARD32 row, CARD32 offset, int mode, CARD32 * size)
  442. {
  443. KdScreenPriv(pScreen);
  444. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  445. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  446. static int plane;
  447. int winSize;
  448. void *base;
  449. plane = offset & 3;
  450. VgaSetWritePlaneMask(priv->vi, (1 << plane));
  451. offset = offset >> 2;
  452. if (pscr->mode.vbe) {
  453. base = VbeSetWindow(priv->vi,
  454. priv->vbeInfo,
  455. pscr->mode.BytesPerScanLine * row + offset,
  456. mode, &winSize);
  457. } else {
  458. base = VgaSetWindow(priv->vi,
  459. pscr->mode.mode,
  460. pscr->mode.BytesPerScanLine * row + offset,
  461. mode, &winSize);
  462. }
  463. *size = (CARD32) winSize;
  464. return base;
  465. }
  466. static void *vesaSetWindowLinear(ScreenPtr pScreen,
  467. CARD32 row, CARD32 offset, int mode, CARD32 * size)
  468. {
  469. KdScreenPriv(pScreen);
  470. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  471. *size = pscr->mode.BytesPerScanLine;
  472. return (CARD8 *) pscr->fb + row * pscr->mode.BytesPerScanLine + offset;
  473. }
  474. static void *vesaSetWindowWindowed(ScreenPtr pScreen,
  475. CARD32 row, CARD32 offset, int mode, CARD32 * size)
  476. {
  477. KdScreenPriv(pScreen);
  478. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  479. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  480. int winSize;
  481. void *base;
  482. if (pscr->mode.vbe) {
  483. base = VbeSetWindow(priv->vi,
  484. priv->vbeInfo,
  485. pscr->mode.BytesPerScanLine * row + offset,
  486. mode, &winSize);
  487. } else {
  488. base = VgaSetWindow(priv->vi,
  489. pscr->mode.mode,
  490. pscr->mode.BytesPerScanLine * row + offset,
  491. mode, &winSize);
  492. }
  493. *size = (CARD32) winSize;
  494. return base;
  495. }
  496. static void *vesaWindowPlanar(ScreenPtr pScreen,
  497. CARD32 row,
  498. CARD32 offset, int mode, CARD32 * size, void *closure)
  499. {
  500. KdScreenPriv(pScreen);
  501. if (!pScreenPriv->enabled)
  502. return 0;
  503. return vesaSetWindowPlanar(pScreen, row, offset, mode, size);
  504. }
  505. static void *vesaWindowLinear(ScreenPtr pScreen,
  506. CARD32 row,
  507. CARD32 offset, int mode, CARD32 * size, void *closure)
  508. {
  509. KdScreenPriv(pScreen);
  510. if (!pScreenPriv->enabled)
  511. return 0;
  512. return vesaSetWindowLinear(pScreen, row, offset, mode, size);
  513. }
  514. static void *vesaWindowWindowed(ScreenPtr pScreen,
  515. CARD32 row,
  516. CARD32 offset, int mode, CARD32 * size, void *closure)
  517. {
  518. KdScreenPriv(pScreen);
  519. if (!pScreenPriv->enabled)
  520. return 0;
  521. return vesaSetWindowWindowed(pScreen, row, offset, mode, size);
  522. }
  523. #define vesaInvertBits32(v) { \
  524. v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555); \
  525. v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333); \
  526. v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f); \
  527. }
  528. static void *vesaWindowCga(ScreenPtr pScreen,
  529. CARD32 row,
  530. CARD32 offset, int mode, CARD32 * size, void *closure)
  531. {
  532. KdScreenPriv(pScreen);
  533. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  534. int line;
  535. if (!pScreenPriv->enabled)
  536. return 0;
  537. *size = pscr->mode.BytesPerScanLine;
  538. line = ((row & 1) << 13) + (row >> 1) * pscr->mode.BytesPerScanLine;
  539. return (CARD8 *) pscr->fb + line + offset;
  540. }
  541. static void vesaUpdateMono(ScreenPtr pScreen, shadowBufPtr pBuf)
  542. {
  543. RegionPtr damage = shadowDamage(pBuf);
  544. PixmapPtr pShadow = pBuf->pPixmap;
  545. int nbox = REGION_NUM_RECTS(damage);
  546. BoxPtr pbox = REGION_RECTS(damage);
  547. FbBits *shaBase, *shaLine, *sha;
  548. FbStride shaStride;
  549. int scrBase, scrLine, scr;
  550. int shaBpp;
  551. int shaXoff _X_UNUSED, shaYoff _X_UNUSED; /* XXX assumed to be zero */
  552. int x, y, w, h, width;
  553. int i;
  554. FbBits *winBase = 0, *win;
  555. CARD32 winSize;
  556. FbBits bits;
  557. fbGetDrawable(&pShadow->drawable, shaBase, shaStride, shaBpp, shaXoff,
  558. shaYoff);
  559. while (nbox--) {
  560. x = pbox->x1 * shaBpp;
  561. y = pbox->y1;
  562. w = (pbox->x2 - pbox->x1) * shaBpp;
  563. h = pbox->y2 - pbox->y1;
  564. scrLine = (x >> FB_SHIFT);
  565. shaLine = shaBase + y * shaStride + (x >> FB_SHIFT);
  566. x &= FB_MASK;
  567. w = (w + x + FB_MASK) >> FB_SHIFT;
  568. while (h--) {
  569. winSize = 0;
  570. scrBase = 0;
  571. width = w;
  572. scr = scrLine;
  573. sha = shaLine;
  574. while (width) {
  575. /* how much remains in this window */
  576. i = scrBase + winSize - scr;
  577. if (i <= 0 || scr < scrBase) {
  578. winBase =
  579. (FbBits *) (*pBuf->window) (pScreen,
  580. y,
  581. scr *
  582. sizeof
  583. (FbBits),
  584. SHADOW_WINDOW_WRITE,
  585. &winSize,
  586. pBuf->closure);
  587. if (!winBase)
  588. return;
  589. scrBase = scr;
  590. winSize /= sizeof(FbBits);
  591. i = winSize;
  592. }
  593. win = winBase + (scr - scrBase);
  594. if (i > width)
  595. i = width;
  596. width -= i;
  597. scr += i;
  598. while (i--) {
  599. bits = *sha++;
  600. vesaInvertBits32(bits);
  601. *win++ = bits;
  602. }
  603. }
  604. shaLine += shaStride;
  605. y++;
  606. }
  607. pbox++;
  608. }
  609. }
  610. static const CARD16 vga16Colors[16][3] = {
  611. {0, 0, 0,}, /* 0 */
  612. {0, 0, 0xAA,}, /* 1 */
  613. {0, 0xAA, 0,}, /* 2 */
  614. {0, 0xAA, 0xAA,}, /* 3 */
  615. {0xAA, 0, 0,}, /* 4 */
  616. {0xAA, 0, 0xAA,}, /* 5 */
  617. {0xAA, 0x55, 0,}, /* 6 */
  618. {0xAA, 0xAA, 0xAA,}, /* 7 */
  619. {0x55, 0x55, 0x55,}, /* 8 */
  620. {0x55, 0x55, 0xFF,}, /* 9 */
  621. {0x55, 0xFF, 0x55,}, /* 10 */
  622. {0x55, 0xFF, 0xFF,}, /* 11 */
  623. {0xFF, 0x55, 0x55,}, /* 12 */
  624. {0xFF, 0x55, 0xFF,}, /* 13 */
  625. {0xFF, 0xFF, 0x55,}, /* 14 */
  626. {0xFF, 0xFF, 0xFF,}, /* 15 */
  627. };
  628. static Bool vesaCreateColormap16(ColormapPtr pmap)
  629. {
  630. int i, j;
  631. if (pmap->pVisual->ColormapEntries == 16)
  632. for (i = 0; i < pmap->pVisual->ColormapEntries; i++) {
  633. j = i & 0xf;
  634. pmap->red[i].co.local.red =
  635. (vga16Colors[j][0] << 8) | vga16Colors[j][0];
  636. pmap->red[i].co.local.green =
  637. (vga16Colors[j][1] << 8) | vga16Colors[j][1];
  638. pmap->red[i].co.local.blue =
  639. (vga16Colors[j][2] << 8) | vga16Colors[j][2];
  640. }
  641. return TRUE;
  642. }
  643. static void vesaSetScreenSizes(ScreenPtr pScreen)
  644. {
  645. KdScreenPriv(pScreen);
  646. KdScreenInfo *screen = pScreenPriv->screen;
  647. VesaScreenPrivPtr pscr = screen->driver;
  648. if (pscr->randr & (RR_Rotate_0 | RR_Rotate_180)) {
  649. pScreen->width = pscr->mode.XResolution;
  650. pScreen->height = pscr->mode.YResolution;
  651. pScreen->mmWidth = screen->width_mm;
  652. pScreen->mmHeight = screen->height_mm;
  653. } else {
  654. pScreen->width = pscr->mode.YResolution;
  655. pScreen->height = pscr->mode.XResolution;
  656. pScreen->mmWidth = screen->height_mm;
  657. pScreen->mmHeight = screen->width_mm;
  658. }
  659. }
  660. static Bool vesaSetShadow(ScreenPtr pScreen)
  661. {
  662. KdScreenPriv(pScreen);
  663. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  664. ShadowUpdateProc update;
  665. ShadowWindowProc window = 0;
  666. if (pscr->randr != RR_Rotate_0)
  667. update = shadowUpdateRotatePacked;
  668. else
  669. update = shadowUpdatePacked;
  670. switch (pscr->mapping) {
  671. case VESA_LINEAR:
  672. window = vesaWindowLinear;
  673. break;
  674. case VESA_WINDOWED:
  675. window = vesaWindowWindowed;
  676. break;
  677. case VESA_PLANAR:
  678. if (pScreenPriv->screen->fb.bitsPerPixel == 8)
  679. update = shadowUpdatePlanar4x8;
  680. else
  681. update = shadowUpdatePlanar4;
  682. window = vesaWindowPlanar;
  683. break;
  684. case VESA_MONO:
  685. update = vesaUpdateMono;
  686. if (pscr->mode.mode < 8)
  687. window = vesaWindowCga;
  688. else
  689. window = vesaWindowLinear;
  690. break;
  691. }
  692. return KdShadowSet(pScreen, pscr->randr, update, window);
  693. }
  694. static Bool vesaComputeFramebufferMapping(KdScreenInfo * screen)
  695. {
  696. VesaScreenPrivPtr pscr = screen->driver;
  697. int depth, bpp, fbbpp;
  698. Pixel allbits;
  699. KdMouseMatrix m;
  700. if (vesa_linear_fb) {
  701. pscr->mapping = VESA_LINEAR;
  702. pscr->shadow = FALSE;
  703. } else {
  704. pscr->mapping = VESA_WINDOWED;
  705. pscr->shadow = TRUE;
  706. }
  707. depth = vesaDepth(&pscr->mode);
  708. bpp = pscr->mode.BitsPerPixel;
  709. if (bpp > 24)
  710. bpp = 32;
  711. else if (bpp > 16)
  712. bpp = 24;
  713. else if (bpp > 8)
  714. bpp = 16;
  715. else if (bpp > 4)
  716. bpp = 8;
  717. else if (bpp > 1)
  718. bpp = 4;
  719. else
  720. bpp = 1;
  721. fbbpp = bpp;
  722. switch (pscr->mode.MemoryModel) {
  723. case MEMORY_DIRECT:
  724. allbits = (screen->fb.redMask |
  725. screen->fb.greenMask | screen->fb.blueMask);
  726. depth = 32;
  727. while (depth && !(allbits & (1 << (depth - 1))))
  728. depth--;
  729. if (vesa_verbose)
  730. ErrorF
  731. ("\tTrue Color %d/%d red 0x%x green 0x%x blue 0x%x\n",
  732. bpp, depth, screen->fb.redMask,
  733. screen->fb.greenMask, screen->fb.blueMask);
  734. break;
  735. case MEMORY_PSEUDO:
  736. if (vesa_verbose)
  737. ErrorF("\tPseudo Color bpp %d depth %d\n", bpp, depth);
  738. break;
  739. case MEMORY_PLANAR:
  740. if (bpp == 4) {
  741. bpp = screen->fb.bitsPerPixel;
  742. if (bpp != 8)
  743. bpp = 4;
  744. depth = bpp;
  745. }
  746. if (bpp == 1) {
  747. pscr->mapping = VESA_MONO;
  748. if (vesa_verbose)
  749. ErrorF("\tMonochrome\n");
  750. } else {
  751. pscr->mapping = VESA_PLANAR;
  752. if (vesa_verbose)
  753. ErrorF("\tStatic color bpp %d depth %d\n",
  754. bpp, depth);
  755. }
  756. pscr->randr = RR_Rotate_0;
  757. pscr->shadow = TRUE;
  758. break;
  759. default:
  760. return FALSE;
  761. }
  762. switch (fbbpp) {
  763. case 8:
  764. case 16:
  765. case 32:
  766. break;
  767. default:
  768. pscr->randr = RR_Rotate_0;
  769. break;
  770. }
  771. if (pscr->randr != RR_Rotate_0)
  772. pscr->shadow = TRUE;
  773. if (vesa_shadow)
  774. pscr->shadow = vesa_shadow;
  775. if (pscr->mapping == VESA_LINEAR
  776. && !(pscr->mode.ModeAttributes & MODE_LINEAR)) {
  777. pscr->mapping = VESA_WINDOWED;
  778. pscr->shadow = TRUE;
  779. }
  780. KdComputeMouseMatrix(&m, pscr->randr,
  781. pscr->mode.XResolution, pscr->mode.YResolution);
  782. KdSetMouseMatrix(&m);
  783. screen->width = pscr->mode.XResolution;
  784. screen->height = pscr->mode.YResolution;
  785. screen->fb.depth = depth;
  786. screen->fb.bitsPerPixel = bpp;
  787. return TRUE;
  788. }
  789. static Bool vesaMapFramebuffer(KdScreenInfo * screen)
  790. {
  791. VesaCardPrivPtr priv = screen->card->driver;
  792. VesaScreenPrivPtr pscr = screen->driver;
  793. if (pscr->mapped)
  794. return TRUE;
  795. switch (pscr->mapping) {
  796. case VESA_MONO:
  797. case VESA_LINEAR:
  798. if (pscr->mode.vbe)
  799. pscr->fb = VbeMapFramebuffer(priv->vi, priv->vbeInfo,
  800. pscr->mode.mode,
  801. &pscr->fb_size,
  802. &pscr->fb_phys);
  803. else
  804. pscr->fb = VgaMapFramebuffer(priv->vi,
  805. pscr->mode.mode,
  806. &pscr->fb_size,
  807. &pscr->fb_phys);
  808. if (!pscr->fb)
  809. return FALSE;
  810. break;
  811. case VESA_WINDOWED:
  812. pscr->fb = NULL;
  813. break;
  814. case VESA_PLANAR:
  815. pscr->fb = NULL;
  816. break;
  817. }
  818. screen->memory_base = pscr->fb;
  819. screen->memory_size = pscr->fb_size;
  820. if (pscr->shadow) {
  821. if (!KdShadowFbAlloc(screen,
  822. pscr->randr & (RR_Rotate_90 |
  823. RR_Rotate_270)))
  824. return FALSE;
  825. screen->off_screen_base = screen->memory_size;
  826. } else {
  827. screen->fb.frameBuffer = (CARD8 *) (pscr->fb);
  828. screen->fb.byteStride = pscr->mode.BytesPerScanLine;
  829. screen->fb.pixelStride = ((pscr->mode.BytesPerScanLine * 8) /
  830. screen->fb.bitsPerPixel);
  831. screen->off_screen_base =
  832. screen->fb.byteStride * screen->height;
  833. }
  834. pscr->mapped = TRUE;
  835. return TRUE;
  836. }
  837. static void vesaUnmapFramebuffer(KdScreenInfo * screen)
  838. {
  839. VesaCardPrivPtr priv = screen->card->driver;
  840. VesaScreenPrivPtr pscr = screen->driver;
  841. if (!pscr->mapped)
  842. return;
  843. pscr->mapped = FALSE;
  844. KdShadowFbFree(screen);
  845. if (pscr->fb) {
  846. if (pscr->mode.vbe)
  847. VbeUnmapFramebuffer(priv->vi, priv->vbeInfo,
  848. pscr->mode.mode, pscr->fb);
  849. else
  850. VgaUnmapFramebuffer(priv->vi);
  851. pscr->fb = 0;
  852. }
  853. }
  854. static Bool vesaSetMode(ScreenPtr pScreen, VesaModePtr mode);
  855. static Bool vesaRandRGetInfo(ScreenPtr pScreen, Rotation * rotations)
  856. {
  857. KdScreenPriv(pScreen);
  858. VesaModePtr modes, mode;
  859. KdScreenInfo *screen = pScreenPriv->screen;
  860. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  861. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  862. int nmode;
  863. int n;
  864. RRScreenSizePtr pSize;
  865. *rotations =
  866. (RR_Rotate_0 | RR_Rotate_90 | RR_Rotate_180 | RR_Rotate_270 |
  867. RR_Reflect_X | RR_Reflect_Y);
  868. /*
  869. * Get mode information from BIOS -- every time in case
  870. * something changes, like an external monitor is plugged in
  871. */
  872. modes = vesaGetModes(priv->vi, &nmode);
  873. if (!modes)
  874. return FALSE;
  875. if (priv->modes)
  876. free(priv->modes);
  877. priv->modes = modes;
  878. priv->nmode = nmode;
  879. for (n = 0; n < nmode; n++) {
  880. mode = &priv->modes[n];
  881. if (vesaModeSupported(priv, mode, FALSE)) {
  882. /*
  883. * XXX limit reported modes to those matching the current
  884. * format
  885. */
  886. if (mode->NumberOfPlanes == pscr->mode.NumberOfPlanes &&
  887. mode->BitsPerPixel == pscr->mode.BitsPerPixel &&
  888. mode->MemoryModel == pscr->mode.MemoryModel &&
  889. mode->RedMaskSize == pscr->mode.RedMaskSize &&
  890. mode->RedFieldPosition ==
  891. pscr->mode.RedFieldPosition
  892. && mode->GreenMaskSize == pscr->mode.GreenMaskSize
  893. && mode->GreenFieldPosition ==
  894. pscr->mode.GreenFieldPosition
  895. && mode->BlueMaskSize == pscr->mode.BlueMaskSize
  896. && mode->BlueFieldPosition ==
  897. pscr->mode.BlueFieldPosition) {
  898. int width, height, width_mm, height_mm;
  899. if (screen->randr &
  900. (RR_Rotate_0 | RR_Rotate_180)) {
  901. width = mode->XResolution;
  902. height = mode->YResolution;
  903. width_mm = screen->width_mm;
  904. height_mm = screen->height_mm;
  905. } else {
  906. width = mode->YResolution;
  907. height = mode->XResolution;
  908. width_mm = screen->height_mm;
  909. height_mm = screen->width_mm;
  910. }
  911. pSize = RRRegisterSize(pScreen,
  912. width, height,
  913. width_mm, height_mm);
  914. if (mode->XResolution == screen->width &&
  915. mode->YResolution == screen->height) {
  916. int randr = KdSubRotation(pscr->randr,
  917. screen->
  918. randr);
  919. RRSetCurrentConfig(pScreen, randr, 0,
  920. pSize);
  921. }
  922. }
  923. }
  924. }
  925. return TRUE;
  926. }
  927. static Bool
  928. vesaRandRSetConfig(ScreenPtr pScreen,
  929. Rotation randr, int rate, RRScreenSizePtr pSize)
  930. {
  931. KdScreenPriv(pScreen);
  932. VesaModePtr mode = 0;
  933. KdScreenInfo *screen = pScreenPriv->screen;
  934. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  935. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  936. int n;
  937. Bool wasEnabled = pScreenPriv->enabled;
  938. Bool ret = FALSE;
  939. VesaScreenPrivRec oldscr;
  940. int oldwidth;
  941. int oldheight;
  942. int oldmmwidth;
  943. int oldmmheight;
  944. int newwidth, newheight;
  945. if (screen->randr & (RR_Rotate_0 | RR_Rotate_180)) {
  946. newwidth = pSize->width;
  947. newheight = pSize->height;
  948. } else {
  949. newwidth = pSize->height;
  950. newheight = pSize->width;
  951. }
  952. for (n = 0; n < priv->nmode; n++) {
  953. mode = &priv->modes[n];
  954. if (vesaModeSupported(priv, mode, FALSE)) {
  955. /*
  956. * XXX all we have to match is the size
  957. */
  958. if (mode->XResolution == newwidth &&
  959. mode->YResolution == newheight &&
  960. mode->NumberOfPlanes == pscr->mode.NumberOfPlanes &&
  961. mode->BitsPerPixel == pscr->mode.BitsPerPixel &&
  962. mode->RedMaskSize == pscr->mode.RedMaskSize &&
  963. mode->RedFieldPosition ==
  964. pscr->mode.RedFieldPosition
  965. && mode->GreenMaskSize == pscr->mode.GreenMaskSize
  966. && mode->GreenFieldPosition ==
  967. pscr->mode.GreenFieldPosition
  968. && mode->BlueMaskSize == pscr->mode.BlueMaskSize
  969. && mode->BlueFieldPosition ==
  970. pscr->mode.BlueFieldPosition)
  971. break;
  972. }
  973. }
  974. if (n == priv->nmode)
  975. goto bail0;
  976. if (wasEnabled)
  977. KdDisableScreen(pScreen);
  978. if (mode->mode != pscr->mode.mode) {
  979. ret = vesaSetMode(pScreen, mode);
  980. if (!ret)
  981. goto bail1;
  982. }
  983. oldscr = *pscr;
  984. oldwidth = screen->width;
  985. oldheight = screen->height;
  986. oldmmwidth = pScreen->mmWidth;
  987. oldmmheight = pScreen->mmHeight;
  988. /*
  989. * Set new configuration
  990. */
  991. pscr->mode = *mode;
  992. pscr->randr = KdAddRotation(screen->randr, randr);
  993. /*
  994. * Can't rotate some formats
  995. */
  996. switch (screen->fb.bitsPerPixel) {
  997. case 8:
  998. case 16:
  999. case 32:
  1000. break;
  1001. default:
  1002. if (pscr->randr)
  1003. goto bail2;
  1004. break;
  1005. }
  1006. vesaUnmapFramebuffer(screen);
  1007. if (!vesaComputeFramebufferMapping(screen))
  1008. goto bail3;
  1009. if (!vesaMapFramebuffer(screen))
  1010. goto bail3;
  1011. vesaSetScreenSizes(screen->pScreen);
  1012. if (!vesaSetShadow(screen->pScreen))
  1013. goto bail4;
  1014. /*
  1015. * Set frame buffer mapping
  1016. */
  1017. (*pScreen->ModifyPixmapHeader) (fbGetScreenPixmap(pScreen),
  1018. pScreen->width,
  1019. pScreen->height,
  1020. screen->fb.depth,
  1021. screen->fb.bitsPerPixel,
  1022. screen->fb.byteStride,
  1023. screen->fb.frameBuffer);
  1024. /* set the subpixel order */
  1025. KdSetSubpixelOrder(pScreen, pscr->randr);
  1026. if (wasEnabled)
  1027. KdEnableScreen(pScreen);
  1028. return TRUE;
  1029. bail4:
  1030. vesaUnmapFramebuffer(screen);
  1031. *pscr = oldscr;
  1032. (void)vesaComputeFramebufferMapping(screen);
  1033. (void)vesaMapFramebuffer(screen);
  1034. bail3:
  1035. pScreen->width = oldwidth;
  1036. pScreen->height = oldheight;
  1037. pScreen->mmWidth = oldmmwidth;
  1038. pScreen->mmHeight = oldmmheight;
  1039. bail2:
  1040. *pscr = oldscr;
  1041. (void)vesaSetMode(pScreen, &pscr->mode);
  1042. bail1:
  1043. if (wasEnabled)
  1044. KdEnableScreen(pScreen);
  1045. bail0:
  1046. return FALSE;
  1047. }
  1048. static Bool vesaRandRInit(ScreenPtr pScreen)
  1049. {
  1050. rrScrPrivPtr pScrPriv;
  1051. if (!RRScreenInit(pScreen))
  1052. return FALSE;
  1053. pScrPriv = rrGetScrPriv(pScreen);
  1054. pScrPriv->rrGetInfo = vesaRandRGetInfo;
  1055. pScrPriv->rrSetConfig = vesaRandRSetConfig;
  1056. return TRUE;
  1057. }
  1058. Bool vesaInitScreen(ScreenPtr pScreen)
  1059. {
  1060. KdScreenPriv(pScreen);
  1061. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1062. switch (pscr->mapping) {
  1063. case VESA_PLANAR:
  1064. pScreen->CreateColormap = vesaCreateColormap16;
  1065. break;
  1066. }
  1067. return TRUE;
  1068. }
  1069. Bool vesaFinishInitScreen(ScreenPtr pScreen)
  1070. {
  1071. if (!shadowSetup(pScreen))
  1072. return FALSE;
  1073. if (!vesaRandRInit(pScreen))
  1074. return FALSE;
  1075. return TRUE;
  1076. }
  1077. Bool vesaCreateResources(ScreenPtr pScreen)
  1078. {
  1079. return vesaSetShadow(pScreen);
  1080. }
  1081. static Bool vesaSetMode(ScreenPtr pScreen, VesaModePtr mode)
  1082. {
  1083. KdScreenPriv(pScreen);
  1084. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1085. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1086. int code;
  1087. if (mode->vbe) {
  1088. if (vesa_verbose)
  1089. ErrorF("Enable VBE mode 0x%x\n", mode->mode);
  1090. code = VbeSetMode(priv->vi, priv->vbeInfo, mode->mode,
  1091. pscr->mapping == VESA_LINEAR,
  1092. mode->MemoryModel == MEMORY_DIRECT);
  1093. } else {
  1094. if (vesa_verbose)
  1095. ErrorF("Enable BIOS mode 0x%x\n", mode->mode);
  1096. code = VgaSetMode(priv->vi, mode->mode);
  1097. }
  1098. if (code < 0)
  1099. return FALSE;
  1100. return TRUE;
  1101. }
  1102. Bool vesaEnable(ScreenPtr pScreen)
  1103. {
  1104. KdScreenPriv(pScreen);
  1105. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1106. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1107. KdScreenInfo *screen = pScreenPriv->screen;
  1108. int i;
  1109. CARD32 size;
  1110. char *p;
  1111. Bool was_mapped = pscr->mapped;
  1112. if (!vesaMapFramebuffer(screen))
  1113. return FALSE;
  1114. if (!vesaSetMode(pScreen, &pscr->mode))
  1115. return FALSE;
  1116. switch (pscr->mapping) {
  1117. case VESA_MONO:
  1118. VgaSetWritePlaneMask(priv->vi, 0x1);
  1119. case VESA_LINEAR:
  1120. if (vesa_restore_font)
  1121. memcpy(priv->text, pscr->fb, VESA_TEXT_SAVE);
  1122. break;
  1123. case VESA_WINDOWED:
  1124. if (vesa_restore_font) {
  1125. for (i = 0; i < VESA_TEXT_SAVE;) {
  1126. p = vesaSetWindowWindowed(pScreen, 0, i,
  1127. VBE_WINDOW_READ,
  1128. &size);
  1129. if (!p) {
  1130. ErrorF
  1131. ("Couldn't set window for saving VGA font\n");
  1132. break;
  1133. }
  1134. if (i + size > VESA_TEXT_SAVE)
  1135. size = VESA_TEXT_SAVE - i;
  1136. memcpy(((char *)priv->text) + i, p, size);
  1137. i += size;
  1138. }
  1139. }
  1140. break;
  1141. case VESA_PLANAR:
  1142. if (vesa_restore_font) {
  1143. for (i = 0; i < 4; i++) {
  1144. p = vesaSetWindowPlanar(pScreen, 0, i,
  1145. VBE_WINDOW_READ, &size);
  1146. memcpy(((char *)priv->text) +
  1147. i * (VESA_TEXT_SAVE / 4), p,
  1148. (VESA_TEXT_SAVE / 4));
  1149. }
  1150. }
  1151. break;
  1152. }
  1153. if (!was_mapped) {
  1154. (*pScreen->ModifyPixmapHeader) (fbGetScreenPixmap(pScreen),
  1155. pScreen->width,
  1156. pScreen->height,
  1157. screen->fb.depth,
  1158. screen->fb.bitsPerPixel,
  1159. screen->fb.byteStride,
  1160. screen->fb.frameBuffer);
  1161. }
  1162. return TRUE;
  1163. }
  1164. #ifndef TOSHIBA_SMM
  1165. # ifdef linux
  1166. # define TOSHIBA_SMM 1
  1167. # endif
  1168. # ifndef TOSHIBA_SMM
  1169. # define TOSHIBA_SMM 0
  1170. # endif
  1171. #endif
  1172. #if TOSHIBA_SMM
  1173. /*
  1174. * Toshiba laptops use a special interface to operate the backlight
  1175. */
  1176. #include <sys/ioctl.h>
  1177. #define TOSH_PROC "/proc/toshiba"
  1178. #define TOSH_DEVICE "/dev/toshiba"
  1179. #define TOSH_SMM _IOWR('t', 0x90, SMMRegisters)
  1180. typedef struct {
  1181. unsigned int eax;
  1182. unsigned int ebx __attribute__ ((packed));
  1183. unsigned int ecx __attribute__ ((packed));
  1184. unsigned int edx __attribute__ ((packed));
  1185. unsigned int esi __attribute__ ((packed));
  1186. unsigned int edi __attribute__ ((packed));
  1187. } SMMRegisters;
  1188. #define HCI_BACKLIGHT 0x0002
  1189. #define HCI_DISABLE 0x0000
  1190. #define HCI_ENABLE 0x0001
  1191. #define HCI_GET 0xfe00,
  1192. #define HCI_SET 0xff00
  1193. static Bool toshibaDPMS(ScreenPtr pScreen, int mode)
  1194. {
  1195. SMMRegisters regs;
  1196. static int fd;
  1197. if (!fd)
  1198. fd = open(TOSH_DEVICE, 2);
  1199. if (fd < 0)
  1200. return FALSE;
  1201. regs.eax = HCI_SET;
  1202. regs.ebx = HCI_BACKLIGHT;
  1203. regs.ecx = mode ? HCI_DISABLE : HCI_ENABLE;
  1204. if (ioctl(fd, TOSH_SMM, &regs) < 0)
  1205. return FALSE;
  1206. return TRUE;
  1207. }
  1208. #endif /* TOSHIBA_SMM */
  1209. Bool vesaDPMS(ScreenPtr pScreen, int mode)
  1210. {
  1211. KdScreenPriv(pScreen);
  1212. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1213. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1214. #if TOSHIBA_SMM
  1215. if (toshibaDPMS(pScreen, mode))
  1216. return TRUE;
  1217. #endif
  1218. if (pscr->mode.vbe)
  1219. return VbeDPMS(priv->vi, mode);
  1220. return FALSE;
  1221. }
  1222. void vesaDisable(ScreenPtr pScreen)
  1223. {
  1224. KdScreenPriv(pScreen);
  1225. KdScreenInfo *screen = pScreenPriv->screen;
  1226. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1227. VesaScreenPrivPtr pscr = screen->driver;
  1228. int i = 0;
  1229. CARD32 size;
  1230. char *p;
  1231. if (vesa_restore_font) {
  1232. switch (pscr->mapping) {
  1233. case VESA_LINEAR:
  1234. case VESA_MONO:
  1235. memcpy(pscr->fb, priv->text, VESA_TEXT_SAVE);
  1236. break;
  1237. case VESA_WINDOWED:
  1238. while (i < VESA_TEXT_SAVE) {
  1239. p = vesaSetWindowWindowed(pScreen, 0, i,
  1240. VBE_WINDOW_WRITE,
  1241. &size);
  1242. if (!p) {
  1243. ErrorF
  1244. ("Couldn't set window for restoring VGA font\n");
  1245. break;
  1246. }
  1247. if (i + size > VESA_TEXT_SAVE)
  1248. size = VESA_TEXT_SAVE - i;
  1249. memcpy(p, ((char *)priv->text) + i, size);
  1250. i += size;
  1251. }
  1252. break;
  1253. case VESA_PLANAR:
  1254. for (i = 0; i < 4; i++) {
  1255. p = vesaSetWindowPlanar(pScreen, 0, i,
  1256. VBE_WINDOW_WRITE,
  1257. &size);
  1258. memcpy(p,
  1259. ((char *)priv->text) +
  1260. i * (VESA_TEXT_SAVE / 4),
  1261. (VESA_TEXT_SAVE / 4));
  1262. }
  1263. break;
  1264. }
  1265. }
  1266. vesaUnmapFramebuffer(screen);
  1267. }
  1268. void vesaPreserve(KdCardInfo * card)
  1269. {
  1270. VesaCardPrivPtr priv = card->driver;
  1271. /* The framebuffer might not be valid at this point, so we cannot
  1272. save the VGA fonts now; we do it in vesaEnable. */
  1273. if (VbeGetMode(priv->vi, &priv->old_vbe_mode) < 0)
  1274. priv->old_vbe_mode = -1;
  1275. if (VgaGetMode(priv->vi, &priv->old_vga_mode) < 0)
  1276. priv->old_vga_mode = -1;
  1277. if (vesa_verbose)
  1278. ErrorF("Previous modes: VBE 0x%x BIOS 0x%x\n",
  1279. priv->old_vbe_mode, priv->old_vga_mode);
  1280. }
  1281. void vesaRestore(KdCardInfo * card)
  1282. {
  1283. VesaCardPrivPtr priv = card->driver;
  1284. int n;
  1285. if (vesa_force_text) {
  1286. if (vesa_verbose)
  1287. ErrorF("Forcing switch back to mode 3 text\n");
  1288. priv->old_vbe_mode = -1;
  1289. priv->old_vga_mode = 3;
  1290. }
  1291. for (n = 0; n < priv->nmode; n++)
  1292. if (priv->modes[n].vbe
  1293. && priv->modes[n].mode == (priv->old_vbe_mode & 0x3fff))
  1294. break;
  1295. if (n < priv->nmode) {
  1296. if (vesa_verbose)
  1297. ErrorF("Restore VBE mode 0x%x\n", priv->old_vbe_mode);
  1298. VbeSetMode(priv->vi, priv->vbeInfo, priv->old_vbe_mode, 0, 0);
  1299. } else {
  1300. if (vesa_verbose)
  1301. ErrorF("Restore BIOS mode 0x%x\n", priv->old_vga_mode);
  1302. VgaSetMode(priv->vi, priv->old_vga_mode);
  1303. }
  1304. }
  1305. void vesaCardFini(KdCardInfo * card)
  1306. {
  1307. VesaCardPrivPtr priv = card->driver;
  1308. if (priv->vbeInfo)
  1309. VbeCleanup(priv->vi, priv->vbeInfo);
  1310. if (priv->modes)
  1311. free(priv->modes);
  1312. Vm86Cleanup(priv->vi);
  1313. free(priv);
  1314. }
  1315. void vesaScreenFini(KdScreenInfo * screen)
  1316. {
  1317. VesaScreenPrivPtr pscr = screen->driver;
  1318. KdShadowFbFree(screen);
  1319. vesaUnmapFramebuffer(screen);
  1320. screen->fb.depth = pscr->origDepth;
  1321. free(pscr);
  1322. }
  1323. static int vesaSetPalette(VesaCardPrivPtr priv, int first, int number, U8 * entries)
  1324. {
  1325. if (priv->vga_palette)
  1326. return VgaSetPalette(priv->vi, first, number, entries);
  1327. else
  1328. return VbeSetPalette(priv->vi, priv->vbeInfo, first, number,
  1329. entries);
  1330. }
  1331. static int vesaGetPalette(VesaCardPrivPtr priv, int first, int number, U8 * entries)
  1332. {
  1333. int code;
  1334. if (priv->vga_palette)
  1335. code = VgaGetPalette(priv->vi, first, number, entries);
  1336. else {
  1337. code =
  1338. VbeGetPalette(priv->vi, priv->vbeInfo, first, number,
  1339. entries);
  1340. if (code < 0) {
  1341. priv->vga_palette = 1;
  1342. code = VgaGetPalette(priv->vi, first, number, entries);
  1343. }
  1344. }
  1345. return code;
  1346. }
  1347. void vesaPutColors(ScreenPtr pScreen, int n, xColorItem * pdefs)
  1348. {
  1349. KdScreenPriv(pScreen);
  1350. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1351. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1352. int p;
  1353. CARD8 *scratch;
  1354. int red, green, blue;
  1355. int min, max;
  1356. if (vesa_swap_rgb) {
  1357. red = 2;
  1358. green = 1;
  1359. blue = 0;
  1360. } else {
  1361. red = 0;
  1362. green = 1;
  1363. blue = 2;
  1364. }
  1365. min = 256;
  1366. max = 0;
  1367. while (n--) {
  1368. p = pdefs->pixel;
  1369. if (p < min)
  1370. min = p;
  1371. if (p > max)
  1372. max = p;
  1373. scratch = priv->cmap + (p * 4);
  1374. scratch[red] = pdefs->red >> 8;
  1375. scratch[green] = pdefs->green >> 8;
  1376. scratch[blue] = pdefs->blue >> 8;
  1377. scratch[3] = 0;
  1378. pdefs++;
  1379. if (pscr->mapping == VESA_PLANAR) {
  1380. /*
  1381. * VGA modes are strange; this code covers all
  1382. * possible modes by duplicating the color information
  1383. * however the attribute registers might be set
  1384. */
  1385. if (p < 16) {
  1386. vesaSetPalette(priv, p, 1, scratch);
  1387. if (p >= 8)
  1388. vesaSetPalette(priv, p + 0x30, 1,
  1389. scratch);
  1390. else if (p == 6)
  1391. vesaSetPalette(priv, 0x14, 1, scratch);
  1392. }
  1393. }
  1394. }
  1395. if (pscr->mapping != VESA_PLANAR)
  1396. vesaSetPalette(priv, min, max - min + 1, priv->cmap + min * 4);
  1397. }
  1398. void vesaGetColors(ScreenPtr pScreen, int n, xColorItem * pdefs)
  1399. {
  1400. KdScreenPriv(pScreen);
  1401. VesaScreenPrivPtr pscr = pScreenPriv->screen->driver;
  1402. VesaCardPrivPtr priv = pScreenPriv->card->driver;
  1403. int i;
  1404. int red, green, blue;
  1405. int min, max;
  1406. int p;
  1407. CARD8 *scratch;
  1408. if (vesa_swap_rgb) {
  1409. red = 2;
  1410. green = 1;
  1411. blue = 0;
  1412. } else {
  1413. red = 0;
  1414. green = 1;
  1415. blue = 2;
  1416. }
  1417. min = 256;
  1418. max = 0;
  1419. for (i = 0; i < n; i++) {
  1420. p = pdefs[i].pixel;
  1421. if (p < min)
  1422. min = p;
  1423. if (p > max)
  1424. max = p;
  1425. if (pscr->mapping == VESA_PLANAR)
  1426. vesaGetPalette(priv, p, 1, priv->cmap + p * 4);
  1427. }
  1428. if (pscr->mapping != VESA_PLANAR)
  1429. vesaGetPalette(priv, min, max - min + 1, priv->cmap + min * 4);
  1430. for (i = 0; i < n; i++) {
  1431. p = pdefs[i].pixel;
  1432. scratch = priv->cmap + p * 4;
  1433. pdefs[i].red = scratch[red] << 8;
  1434. pdefs[i].green = scratch[green] << 8;
  1435. pdefs[i].blue = scratch[blue] << 8;
  1436. }
  1437. }
  1438. void vesaUseMsg(void)
  1439. {
  1440. ErrorF("\nTinyX VESA Usage:\n");
  1441. ErrorF("-mode VESA video mode to use (Be careful!)\n");
  1442. ErrorF("-listmodes List supported video modes\n");
  1443. ErrorF("-force Attempt even unsupported modes\n");
  1444. ErrorF("-no-shadow Don't use a shadow framebuffer (default on)\n");
  1445. ErrorF("-nolinear Never use linear framebuffer (Not useful)\n");
  1446. ErrorF
  1447. ("-swaprgb Use if colors are wrong in PseudoColor and 16 color modes\n");
  1448. ErrorF
  1449. ("-map-holes Use contiguous memory map (For seg fault with rare BIOS)\n");
  1450. ErrorF
  1451. ("-verbose Emit diagnostic messages during BIOS initialization\n");
  1452. ErrorF
  1453. ("-force-text Always use standard 25x80 text mode on server exit or VT switch\n");
  1454. ErrorF("-boot Soft boot video card\n");
  1455. /* XXX: usage for -vesatest, -no-map-holes (don't need?),
  1456. * XXX: and -trash-font. Also in man page. */
  1457. }
  1458. int vesaProcessArgument(int argc, char **argv, int i)
  1459. {
  1460. if (!strcmp(argv[i], "-mode")) {
  1461. if (i + 1 < argc) {
  1462. vesa_video_mode = strtol(argv[i + 1], NULL, 0);
  1463. } else
  1464. UseMsg();
  1465. return 2;
  1466. } else if (!strcmp(argv[i], "-force")) {
  1467. vesa_force_mode = TRUE;
  1468. return 1;
  1469. } else if (!strcmp(argv[i], "-listmodes")) {
  1470. vesaListModes();
  1471. exit(0);
  1472. } else if (!strcmp(argv[i], "-vesatest")) {
  1473. vesaTestMode();
  1474. exit(0);
  1475. } else if (!strcmp(argv[i], "-swaprgb")) {
  1476. vesa_swap_rgb = TRUE;
  1477. return 1;
  1478. } else if (!strcmp(argv[i], "-shadow")) {
  1479. vesa_shadow = TRUE;
  1480. return 1;
  1481. } else if (!strcmp(argv[i], "-no-shadow")) {
  1482. vesa_shadow = FALSE;
  1483. return 1;
  1484. } else if (!strcmp(argv[i], "-nolinear")) {
  1485. vesa_linear_fb = FALSE;
  1486. return 1;
  1487. } else if (!strcmp(argv[i], "-verbose")) {
  1488. vesa_verbose = TRUE;
  1489. return 1;
  1490. } else if (!strcmp(argv[i], "-force-text")) {
  1491. vesa_force_text = TRUE;
  1492. return 1;
  1493. } else if (!strcmp(argv[i], "-map-holes")) {
  1494. vesa_map_holes = TRUE;
  1495. return 1;
  1496. } else if (!strcmp(argv[i], "-no-map-holes")) {
  1497. vesa_map_holes = FALSE;
  1498. return 1;
  1499. } else if (!strcmp(argv[i], "-trash-font")) {
  1500. vesa_restore_font = FALSE;
  1501. return 1;
  1502. } else if (!strcmp(argv[i], "-boot")) {
  1503. vesa_boot = TRUE;
  1504. return 1;
  1505. }
  1506. return 0;
  1507. }