fbdev.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. *
  3. * Copyright © 1999 Keith Packard
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include <kdrive-config.h>
  25. #endif
  26. #include "fbdev.h"
  27. #include <sys/ioctl.h>
  28. #include <errno.h>
  29. extern int KdTsPhyScreen;
  30. const char *fbdevDevicePath = NULL;
  31. static Bool fbdevMapFramebuffer(KdScreenInfo * screen);
  32. static Bool fbdevInitialize(KdCardInfo * card, FbdevPriv * priv)
  33. {
  34. unsigned long off;
  35. if (fbdevDevicePath == NULL)
  36. fbdevDevicePath = "/dev/fb0";
  37. if ((priv->fd = open(fbdevDevicePath, O_RDWR)) < 0) {
  38. ErrorF("Error opening framebuffer %s: %s\n",
  39. fbdevDevicePath, strerror(errno));
  40. return FALSE;
  41. }
  42. /* quiet valgrind */
  43. memset(&priv->fix, '\0', sizeof(priv->fix));
  44. if (ioctl(priv->fd, FBIOGET_FSCREENINFO, &priv->fix) < 0) {
  45. perror("Error with /dev/fb ioctl FIOGET_FSCREENINFO");
  46. close(priv->fd);
  47. return FALSE;
  48. }
  49. /* quiet valgrind */
  50. memset(&priv->var, '\0', sizeof(priv->var));
  51. if (ioctl(priv->fd, FBIOGET_VSCREENINFO, &priv->var) < 0) {
  52. perror("Error with /dev/fb ioctl FIOGET_VSCREENINFO");
  53. close(priv->fd);
  54. return FALSE;
  55. }
  56. priv->fb_base = (char *) mmap((caddr_t) NULL,
  57. priv->fix.smem_len,
  58. PROT_READ | PROT_WRITE,
  59. MAP_SHARED, priv->fd, 0);
  60. if (priv->fb_base == (char *) -1) {
  61. perror("ERROR: mmap framebuffer fails!");
  62. close(priv->fd);
  63. return FALSE;
  64. }
  65. off = (unsigned long) priv->fix.smem_start % (unsigned long) getpagesize();
  66. priv->fb = priv->fb_base + off;
  67. return TRUE;
  68. }
  69. Bool fbdevCardInit(KdCardInfo * card)
  70. {
  71. FbdevPriv *priv;
  72. priv = malloc(sizeof(FbdevPriv));
  73. if (!priv)
  74. return FALSE;
  75. if (!fbdevInitialize(card, priv)) {
  76. free(priv);
  77. return FALSE;
  78. }
  79. card->driver = priv;
  80. return TRUE;
  81. }
  82. static Pixel fbdevMakeContig(Pixel orig, Pixel others)
  83. {
  84. Pixel low;
  85. low = lowbit(orig) >> 1;
  86. while (low && (others & low) == 0) {
  87. orig |= low;
  88. low >>= 1;
  89. }
  90. return orig;
  91. }
  92. static Bool fbdevModeSupported(KdScreenInfo * screen, const KdMonitorTiming * t)
  93. {
  94. return TRUE;
  95. }
  96. static void
  97. fbdevConvertMonitorTiming(const KdMonitorTiming * t,
  98. struct fb_var_screeninfo *var)
  99. {
  100. memset(var, 0, sizeof(struct fb_var_screeninfo));
  101. var->xres = t->horizontal;
  102. var->yres = t->vertical;
  103. var->xres_virtual = t->horizontal;
  104. var->yres_virtual = t->vertical;
  105. var->xoffset = 0;
  106. var->yoffset = 0;
  107. var->pixclock = t->clock ? 1000000000 / t->clock : 0;
  108. var->left_margin = t->hbp;
  109. var->right_margin = t->hfp;
  110. var->upper_margin = t->vbp;
  111. var->lower_margin = t->vfp;
  112. var->hsync_len = t->hblank - t->hfp - t->hbp;
  113. var->vsync_len = t->vblank - t->vfp - t->vbp;
  114. var->sync = 0;
  115. var->vmode = 0;
  116. if (t->hpol == KdSyncPositive)
  117. var->sync |= FB_SYNC_HOR_HIGH_ACT;
  118. if (t->vpol == KdSyncPositive)
  119. var->sync |= FB_SYNC_VERT_HIGH_ACT;
  120. }
  121. static Bool fbdevScreenInitialize(KdScreenInfo * screen, FbdevScrPriv * scrpriv)
  122. {
  123. FbdevPriv *priv = screen->card->driver;
  124. Pixel allbits;
  125. int depth;
  126. Bool gray;
  127. struct fb_var_screeninfo var;
  128. const KdMonitorTiming *t;
  129. int k;
  130. k = ioctl(priv->fd, FBIOGET_VSCREENINFO, &var);
  131. if (!screen->width || !screen->height) {
  132. if (k >= 0) {
  133. screen->width = var.xres;
  134. screen->height = var.yres;
  135. } else {
  136. screen->width = 1024;
  137. screen->height = 768;
  138. }
  139. screen->rate = 103; /* FIXME: should get proper value from fb driver */
  140. }
  141. if (!screen->fb.depth) {
  142. if (k >= 0)
  143. screen->fb.depth = var.bits_per_pixel;
  144. else
  145. screen->fb.depth = 16;
  146. }
  147. if ((screen->width != var.xres) || (screen->height != var.yres)) {
  148. t = KdFindMode(screen, fbdevModeSupported);
  149. screen->rate = t->rate;
  150. screen->width = t->horizontal;
  151. screen->height = t->vertical;
  152. /* Now try setting the mode */
  153. if (k < 0 || (t->horizontal != var.xres || t->vertical != var.yres))
  154. fbdevConvertMonitorTiming(t, &var);
  155. }
  156. var.activate = FB_ACTIVATE_NOW;
  157. var.bits_per_pixel = screen->fb.depth;
  158. var.nonstd = 0;
  159. var.grayscale = 0;
  160. k = ioctl(priv->fd, FBIOPUT_VSCREENINFO, &var);
  161. if (k < 0) {
  162. fprintf(stderr, "error: %s\n", strerror(errno));
  163. return FALSE;
  164. }
  165. /* Re-get the "fixed" parameters since they might have changed */
  166. k = ioctl(priv->fd, FBIOGET_FSCREENINFO, &priv->fix);
  167. if (k < 0)
  168. perror("FBIOGET_FSCREENINFO");
  169. /* Now get the new screeninfo */
  170. ioctl(priv->fd, FBIOGET_VSCREENINFO, &priv->var);
  171. depth = priv->var.bits_per_pixel;
  172. gray = priv->var.grayscale;
  173. /* Calculate fix.line_length if it's zero */
  174. if (!priv->fix.line_length)
  175. priv->fix.line_length = (priv->var.xres_virtual * depth + 7) / 8;
  176. switch (priv->fix.visual) {
  177. case FB_VISUAL_PSEUDOCOLOR:
  178. if (gray) {
  179. screen->fb.visuals = (1 << StaticGray);
  180. /* could also support GrayScale, but what's the point? */
  181. } else {
  182. screen->fb.visuals = ((1 << StaticGray) |
  183. (1 << GrayScale) |
  184. (1 << StaticColor) |
  185. (1 << PseudoColor) |
  186. (1 << TrueColor) |
  187. (1 << DirectColor));
  188. }
  189. screen->fb.blueMask = 0x00;
  190. screen->fb.greenMask = 0x00;
  191. screen->fb.redMask = 0x00;
  192. break;
  193. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  194. if (gray) {
  195. screen->fb.visuals = (1 << StaticGray);
  196. } else {
  197. screen->fb.visuals = (1 << StaticColor);
  198. }
  199. screen->fb.blueMask = 0x00;
  200. screen->fb.greenMask = 0x00;
  201. screen->fb.redMask = 0x00;
  202. break;
  203. case FB_VISUAL_TRUECOLOR:
  204. case FB_VISUAL_DIRECTCOLOR:
  205. screen->fb.visuals = (1 << TrueColor);
  206. #define Mask(o,l) (((1 << l) - 1) << o)
  207. screen->fb.redMask =
  208. Mask(priv->var.red.offset, priv->var.red.length);
  209. screen->fb.greenMask =
  210. Mask(priv->var.green.offset, priv->var.green.length);
  211. screen->fb.blueMask =
  212. Mask(priv->var.blue.offset, priv->var.blue.length);
  213. /*
  214. * This is a kludge so that Render will work -- fill in the gaps
  215. * in the pixel
  216. */
  217. screen->fb.redMask = fbdevMakeContig(screen->fb.redMask,
  218. screen->fb.
  219. greenMask | screen->
  220. fb.blueMask);
  221. screen->fb.greenMask =
  222. fbdevMakeContig(screen->fb.greenMask,
  223. screen->fb.redMask | screen->fb.
  224. blueMask);
  225. screen->fb.blueMask = fbdevMakeContig(screen->fb.blueMask,
  226. screen->fb.redMask |
  227. screen->fb.
  228. greenMask);
  229. allbits =
  230. screen->fb.redMask | screen->fb.greenMask | screen->
  231. fb.blueMask;
  232. depth = 32;
  233. while (depth && !(allbits & (1 << (depth - 1))))
  234. depth--;
  235. break;
  236. default:
  237. return FALSE;
  238. break;
  239. }
  240. screen->fb.depth = depth;
  241. screen->fb.bitsPerPixel = priv->var.bits_per_pixel;
  242. scrpriv->randr = screen->randr;
  243. return fbdevMapFramebuffer(screen);
  244. }
  245. Bool fbdevScreenInit(KdScreenInfo * screen)
  246. {
  247. FbdevScrPriv *scrpriv;
  248. scrpriv = calloc(1, sizeof(FbdevScrPriv));
  249. if (!scrpriv)
  250. return FALSE;
  251. screen->driver = scrpriv;
  252. if (!fbdevScreenInitialize(screen, scrpriv)) {
  253. screen->driver = 0;
  254. free(scrpriv);
  255. return FALSE;
  256. }
  257. return TRUE;
  258. }
  259. static void *fbdevWindowLinear(ScreenPtr pScreen,
  260. CARD32 row,
  261. CARD32 offset, int mode, CARD32 * size, void *closure)
  262. {
  263. KdScreenPriv(pScreen);
  264. FbdevPriv *priv = pScreenPriv->card->driver;
  265. if (!pScreenPriv->enabled)
  266. return 0;
  267. *size = priv->fix.line_length;
  268. return (CARD8 *) priv->fb + row * priv->fix.line_length + offset;
  269. }
  270. static Bool fbdevMapFramebuffer(KdScreenInfo * screen)
  271. {
  272. FbdevScrPriv *scrpriv = screen->driver;
  273. KdMouseMatrix m;
  274. FbdevPriv *priv = screen->card->driver;
  275. if (scrpriv->randr != RR_Rotate_0 ||
  276. priv->fix.type != FB_TYPE_PACKED_PIXELS)
  277. scrpriv->shadow = TRUE;
  278. else
  279. scrpriv->shadow = FALSE;
  280. KdComputeMouseMatrix(&m, scrpriv->randr, screen->width, screen->height);
  281. KdSetMouseMatrix(&m);
  282. screen->width = priv->var.xres;
  283. screen->height = priv->var.yres;
  284. screen->memory_base = (CARD8 *) (priv->fb);
  285. screen->memory_size = priv->fix.smem_len;
  286. if (scrpriv->shadow) {
  287. if (!KdShadowFbAlloc(screen,
  288. scrpriv->
  289. randr & (RR_Rotate_90 | RR_Rotate_270)))
  290. return FALSE;
  291. screen->off_screen_base = screen->memory_size;
  292. } else {
  293. screen->fb.byteStride = priv->fix.line_length;
  294. screen->fb.pixelStride = (priv->fix.line_length * 8 /
  295. priv->var.bits_per_pixel);
  296. screen->fb.frameBuffer = (CARD8 *) (priv->fb);
  297. screen->off_screen_base =
  298. screen->fb.byteStride * screen->height;
  299. }
  300. return TRUE;
  301. }
  302. static void fbdevSetScreenSizes(ScreenPtr pScreen)
  303. {
  304. KdScreenPriv(pScreen);
  305. KdScreenInfo *screen = pScreenPriv->screen;
  306. FbdevScrPriv *scrpriv = screen->driver;
  307. FbdevPriv *priv = screen->card->driver;
  308. if (scrpriv->randr & (RR_Rotate_0 | RR_Rotate_180)) {
  309. pScreen->width = priv->var.xres;
  310. pScreen->height = priv->var.yres;
  311. pScreen->mmWidth = screen->width_mm;
  312. pScreen->mmHeight = screen->height_mm;
  313. } else {
  314. pScreen->width = priv->var.yres;
  315. pScreen->height = priv->var.xres;
  316. pScreen->mmWidth = screen->height_mm;
  317. pScreen->mmHeight = screen->width_mm;
  318. }
  319. }
  320. static Bool fbdevUnmapFramebuffer(KdScreenInfo * screen)
  321. {
  322. KdShadowFbFree(screen);
  323. return TRUE;
  324. }
  325. static Bool fbdevSetShadow(ScreenPtr pScreen)
  326. {
  327. KdScreenPriv(pScreen);
  328. KdScreenInfo *screen = pScreenPriv->screen;
  329. FbdevScrPriv *scrpriv = screen->driver;
  330. FbdevPriv *priv = screen->card->driver;
  331. ShadowUpdateProc update;
  332. ShadowWindowProc window;
  333. int useYX = 0;
  334. #ifdef __arm__
  335. /* Use variant copy routines that always read left to right in the
  336. shadow framebuffer. Reading vertical strips is exceptionally
  337. slow on XScale due to cache effects. */
  338. useYX = 1;
  339. #endif
  340. window = fbdevWindowLinear;
  341. update = 0;
  342. if (priv->fix.type != FB_TYPE_PACKED_PIXELS)
  343. FatalError("Unsupported frame buffer type %u\n", priv->fix.type);
  344. if (scrpriv->randr)
  345. if (priv->var.bits_per_pixel == 16) {
  346. switch (scrpriv->randr) {
  347. case RR_Rotate_90:
  348. if (useYX)
  349. update = shadowUpdateRotate16_90YX;
  350. else
  351. update = shadowUpdateRotate16_90;
  352. break;
  353. case RR_Rotate_180:
  354. update = shadowUpdateRotate16_180;
  355. break;
  356. case RR_Rotate_270:
  357. if (useYX)
  358. update = shadowUpdateRotate16_270YX;
  359. else
  360. update = shadowUpdateRotate16_270;
  361. break;
  362. default:
  363. update = shadowUpdateRotate16;
  364. break;
  365. }
  366. } else
  367. update = shadowUpdateRotatePacked;
  368. else
  369. update = shadowUpdatePacked;
  370. return KdShadowSet(pScreen, scrpriv->randr, update, window);
  371. }
  372. static Bool fbdevRandRGetInfo(ScreenPtr pScreen, Rotation * rotations)
  373. {
  374. KdScreenPriv(pScreen);
  375. KdScreenInfo *screen = pScreenPriv->screen;
  376. FbdevScrPriv *scrpriv = screen->driver;
  377. RRScreenSizePtr pSize;
  378. Rotation randr;
  379. int n;
  380. *rotations = RR_Rotate_All | RR_Reflect_All;
  381. for (n = 0; n < pScreen->numDepths; n++)
  382. if (pScreen->allowedDepths[n].numVids)
  383. break;
  384. if (n == pScreen->numDepths)
  385. return FALSE;
  386. pSize = RRRegisterSize(pScreen,
  387. screen->width,
  388. screen->height,
  389. screen->width_mm, screen->height_mm);
  390. randr = KdSubRotation(scrpriv->randr, screen->randr);
  391. RRSetCurrentConfig(pScreen, randr, 0, pSize);
  392. return TRUE;
  393. }
  394. static Bool
  395. fbdevRandRSetConfig(ScreenPtr pScreen,
  396. Rotation randr, int rate, RRScreenSizePtr pSize)
  397. {
  398. KdScreenPriv(pScreen);
  399. KdScreenInfo *screen = pScreenPriv->screen;
  400. FbdevScrPriv *scrpriv = screen->driver;
  401. Bool wasEnabled = pScreenPriv->enabled;
  402. FbdevScrPriv oldscr;
  403. int oldwidth;
  404. int oldheight;
  405. int oldmmwidth;
  406. int oldmmheight;
  407. int newwidth, newheight, newmmwidth, newmmheight;
  408. if (screen->randr & (RR_Rotate_0 | RR_Rotate_180)) {
  409. newwidth = pSize->width;
  410. newheight = pSize->height;
  411. newmmwidth = pSize->mmWidth;
  412. newmmheight = pSize->mmHeight;
  413. } else {
  414. newwidth = pSize->height;
  415. newheight = pSize->width;
  416. newmmwidth = pSize->mmHeight;
  417. newmmheight = pSize->mmWidth;
  418. }
  419. if (wasEnabled)
  420. KdDisableScreen(pScreen);
  421. oldscr = *scrpriv;
  422. oldwidth = screen->width;
  423. oldheight = screen->height;
  424. oldmmwidth = pScreen->mmWidth;
  425. oldmmheight = pScreen->mmHeight;
  426. /*
  427. * Set new configuration
  428. */
  429. scrpriv->randr = KdAddRotation(screen->randr, randr);
  430. pScreen->width = newwidth;
  431. pScreen->height = newheight;
  432. pScreen->mmWidth = newmmwidth;
  433. pScreen->mmHeight = newmmheight;
  434. fbdevUnmapFramebuffer(screen);
  435. if (!fbdevMapFramebuffer(screen))
  436. goto bail4;
  437. KdShadowUnset(screen->pScreen);
  438. if (!fbdevSetShadow(screen->pScreen))
  439. goto bail4;
  440. fbdevSetScreenSizes(screen->pScreen);
  441. /*
  442. * Set frame buffer mapping
  443. */
  444. (*pScreen->ModifyPixmapHeader) (fbGetScreenPixmap(pScreen),
  445. pScreen->width,
  446. pScreen->height,
  447. screen->fb.depth,
  448. screen->fb.bitsPerPixel,
  449. screen->fb.byteStride,
  450. screen->fb.frameBuffer);
  451. /* set the subpixel order */
  452. KdSetSubpixelOrder(pScreen, scrpriv->randr);
  453. if (wasEnabled)
  454. KdEnableScreen(pScreen);
  455. return TRUE;
  456. bail4:
  457. fbdevUnmapFramebuffer(screen);
  458. *scrpriv = oldscr;
  459. fbdevMapFramebuffer(screen);
  460. pScreen->width = oldwidth;
  461. pScreen->height = oldheight;
  462. pScreen->mmWidth = oldmmwidth;
  463. pScreen->mmHeight = oldmmheight;
  464. if (wasEnabled)
  465. KdEnableScreen(pScreen);
  466. return FALSE;
  467. }
  468. static Bool fbdevRandRInit(ScreenPtr pScreen)
  469. {
  470. rrScrPrivPtr pScrPriv;
  471. if (!RRScreenInit(pScreen))
  472. return FALSE;
  473. pScrPriv = rrGetScrPriv(pScreen);
  474. pScrPriv->rrGetInfo = fbdevRandRGetInfo;
  475. pScrPriv->rrSetConfig = fbdevRandRSetConfig;
  476. return TRUE;
  477. }
  478. static Bool fbdevCreateColormap(ColormapPtr pmap)
  479. {
  480. ScreenPtr pScreen = pmap->pScreen;
  481. KdScreenPriv(pScreen);
  482. FbdevPriv *priv = pScreenPriv->card->driver;
  483. VisualPtr pVisual;
  484. int i;
  485. int nent;
  486. xColorItem *pdefs;
  487. switch (priv->fix.visual) {
  488. case FB_VISUAL_STATIC_PSEUDOCOLOR:
  489. pVisual = pmap->pVisual;
  490. nent = pVisual->ColormapEntries;
  491. pdefs = malloc(nent * sizeof(xColorItem));
  492. if (!pdefs)
  493. return FALSE;
  494. for (i = 0; i < nent; i++)
  495. pdefs[i].pixel = i;
  496. fbdevGetColors(pScreen, nent, pdefs);
  497. for (i = 0; i < nent; i++) {
  498. pmap->red[i].co.local.red = pdefs[i].red;
  499. pmap->red[i].co.local.green = pdefs[i].green;
  500. pmap->red[i].co.local.blue = pdefs[i].blue;
  501. }
  502. free(pdefs);
  503. return TRUE;
  504. default:
  505. return fbInitializeColormap(pmap);
  506. }
  507. }
  508. Bool fbdevInitScreen(ScreenPtr pScreen)
  509. {
  510. pScreen->CreateColormap = fbdevCreateColormap;
  511. return TRUE;
  512. }
  513. Bool fbdevFinishInitScreen(ScreenPtr pScreen)
  514. {
  515. if (!shadowSetup(pScreen))
  516. return FALSE;
  517. if (!fbdevRandRInit(pScreen))
  518. return FALSE;
  519. return TRUE;
  520. }
  521. Bool fbdevCreateResources(ScreenPtr pScreen)
  522. {
  523. return fbdevSetShadow(pScreen);
  524. }
  525. void fbdevPreserve(KdCardInfo * card)
  526. {
  527. }
  528. static int fbdevUpdateFbColormap(FbdevPriv *priv, int minidx, int maxidx)
  529. {
  530. struct fb_cmap cmap;
  531. cmap.start = minidx;
  532. cmap.len = maxidx - minidx + 1;
  533. cmap.red = &priv->red[minidx];
  534. cmap.green = &priv->green[minidx];
  535. cmap.blue = &priv->blue[minidx];
  536. cmap.transp = 0;
  537. return ioctl(priv->fd, FBIOPUTCMAP, &cmap);
  538. }
  539. Bool fbdevEnable(ScreenPtr pScreen)
  540. {
  541. KdScreenPriv(pScreen);
  542. FbdevPriv *priv = pScreenPriv->card->driver;
  543. int k;
  544. priv->var.activate = FB_ACTIVATE_NOW | FB_CHANGE_CMAP_VBL;
  545. /* display it on the LCD */
  546. k = ioctl(priv->fd, FBIOPUT_VSCREENINFO, &priv->var);
  547. if (k < 0) {
  548. perror("FBIOPUT_VSCREENINFO");
  549. return FALSE;
  550. }
  551. if (priv->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  552. int i;
  553. for (i = 0;
  554. i < (1 << priv->var.red.length) ||
  555. i < (1 << priv->var.green.length) ||
  556. i < (1 << priv->var.blue.length); i++) {
  557. priv->red[i] =
  558. i * 65535 / ((1 << priv->var.red.length) - 1);
  559. priv->green[i] =
  560. i * 65535 / ((1 << priv->var.green.length) - 1);
  561. priv->blue[i] =
  562. i * 65535 / ((1 << priv->var.blue.length) - 1);
  563. }
  564. i--;
  565. fbdevUpdateFbColormap(priv, 0, i);
  566. }
  567. return TRUE;
  568. }
  569. Bool fbdevDPMS(ScreenPtr pScreen, int mode)
  570. {
  571. KdScreenPriv(pScreen);
  572. FbdevPriv *priv = pScreenPriv->card->driver;
  573. static int oldmode = -1;
  574. if (mode == oldmode)
  575. return TRUE;
  576. #ifdef FBIOPUT_POWERMODE
  577. if (ioctl(priv->fd, FBIOPUT_POWERMODE, &mode) >= 0) {
  578. oldmode = mode;
  579. return TRUE;
  580. }
  581. #endif
  582. #ifdef FBIOBLANK
  583. if (ioctl(priv->fd, FBIOBLANK, mode ? mode + 1 : 0) >= 0) {
  584. oldmode = mode;
  585. return TRUE;
  586. }
  587. #endif
  588. return FALSE;
  589. }
  590. void fbdevDisable(ScreenPtr pScreen)
  591. {
  592. }
  593. void fbdevRestore(KdCardInfo * card)
  594. {
  595. }
  596. void fbdevScreenFini(KdScreenInfo * screen)
  597. {
  598. }
  599. void fbdevCardFini(KdCardInfo * card)
  600. {
  601. FbdevPriv *priv = card->driver;
  602. munmap(priv->fb_base, priv->fix.smem_len);
  603. close(priv->fd);
  604. free(priv);
  605. }
  606. /*
  607. * Retrieve actual colormap and return selected n entries in pdefs.
  608. */
  609. void fbdevGetColors(ScreenPtr pScreen, int n, xColorItem * pdefs)
  610. {
  611. KdScreenPriv(pScreen);
  612. FbdevPriv *priv = pScreenPriv->card->driver;
  613. struct fb_cmap cmap;
  614. int p;
  615. int k;
  616. int min, max;
  617. min = 256;
  618. max = 0;
  619. for (k = 0; k < n; k++) {
  620. if (pdefs[k].pixel < min)
  621. min = pdefs[k].pixel;
  622. if (pdefs[k].pixel > max)
  623. max = pdefs[k].pixel;
  624. }
  625. cmap.start = min;
  626. cmap.len = max - min + 1;
  627. cmap.red = &priv->red[min];
  628. cmap.green = &priv->green[min];
  629. cmap.blue = &priv->blue[min];
  630. cmap.transp = 0;
  631. k = ioctl(priv->fd, FBIOGETCMAP, &cmap);
  632. if (k < 0) {
  633. perror("can't get colormap");
  634. return;
  635. }
  636. while (n--) {
  637. p = pdefs->pixel;
  638. pdefs->red = priv->red[p];
  639. pdefs->green = priv->green[p];
  640. pdefs->blue = priv->blue[p];
  641. pdefs++;
  642. }
  643. }
  644. /*
  645. * Change colormap by updating n entries described in pdefs.
  646. */
  647. void fbdevPutColors(ScreenPtr pScreen, int n, xColorItem * pdefs)
  648. {
  649. KdScreenPriv(pScreen);
  650. FbdevPriv *priv = pScreenPriv->card->driver;
  651. int p;
  652. int min, max;
  653. min = 256;
  654. max = 0;
  655. while (n--) {
  656. p = pdefs->pixel;
  657. priv->red[p] = pdefs->red;
  658. priv->green[p] = pdefs->green;
  659. priv->blue[p] = pdefs->blue;
  660. if (p < min)
  661. min = p;
  662. if (p > max)
  663. max = p;
  664. pdefs++;
  665. }
  666. fbdevUpdateFbColormap(priv, min, max);
  667. }