3
0

fbset.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini fbset implementation for busybox
  4. *
  5. * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. *
  9. * This is a from-scratch implementation of fbset; but the de facto fbset
  10. * implementation was a good reference. fbset (original) is released under
  11. * the GPL, and is (c) 1995-1999 by:
  12. * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
  13. */
  14. //usage:#define fbset_trivial_usage
  15. //usage: "[OPTIONS] [MODE]"
  16. //usage:#define fbset_full_usage "\n\n"
  17. //usage: "Show and modify frame buffer settings"
  18. //usage:
  19. //usage:#define fbset_example_usage
  20. //usage: "$ fbset\n"
  21. //usage: "mode \"1024x768-76\"\n"
  22. //usage: " # D: 78.653 MHz, H: 59.949 kHz, V: 75.694 Hz\n"
  23. //usage: " geometry 1024 768 1024 768 16\n"
  24. //usage: " timings 12714 128 32 16 4 128 4\n"
  25. //usage: " accel false\n"
  26. //usage: " rgba 5/11,6/5,5/0,0/0\n"
  27. //usage: "endmode\n"
  28. #include "libbb.h"
  29. #define DEFAULTFBDEV FB_0
  30. #define DEFAULTFBMODE "/etc/fb.modes"
  31. /* Stuff stolen from the kernel's fb.h */
  32. #define FB_ACTIVATE_ALL 64
  33. enum {
  34. FBIOGET_VSCREENINFO = 0x4600,
  35. FBIOPUT_VSCREENINFO = 0x4601
  36. };
  37. struct fb_bitfield {
  38. uint32_t offset; /* beginning of bitfield */
  39. uint32_t length; /* length of bitfield */
  40. uint32_t msb_right; /* !=0: Most significant bit is right */
  41. };
  42. struct fb_var_screeninfo {
  43. uint32_t xres; /* visible resolution */
  44. uint32_t yres;
  45. uint32_t xres_virtual; /* virtual resolution */
  46. uint32_t yres_virtual;
  47. uint32_t xoffset; /* offset from virtual to visible */
  48. uint32_t yoffset; /* resolution */
  49. uint32_t bits_per_pixel;
  50. uint32_t grayscale; /* !=0 Graylevels instead of colors */
  51. struct fb_bitfield red; /* bitfield in fb mem if true color, */
  52. struct fb_bitfield green; /* else only length is significant */
  53. struct fb_bitfield blue;
  54. struct fb_bitfield transp; /* transparency */
  55. uint32_t nonstd; /* !=0 Non standard pixel format */
  56. uint32_t activate; /* see FB_ACTIVATE_x */
  57. uint32_t height; /* height of picture in mm */
  58. uint32_t width; /* width of picture in mm */
  59. uint32_t accel_flags; /* acceleration flags (hints) */
  60. /* Timing: All values in pixclocks, except pixclock (of course) */
  61. uint32_t pixclock; /* pixel clock in ps (pico seconds) */
  62. uint32_t left_margin; /* time from sync to picture */
  63. uint32_t right_margin; /* time from picture to sync */
  64. uint32_t upper_margin; /* time from sync to picture */
  65. uint32_t lower_margin;
  66. uint32_t hsync_len; /* length of horizontal sync */
  67. uint32_t vsync_len; /* length of vertical sync */
  68. uint32_t sync; /* see FB_SYNC_x */
  69. uint32_t vmode; /* see FB_VMODE_x */
  70. uint32_t reserved[6]; /* Reserved for future compatibility */
  71. };
  72. static void copy_if_gt0(uint32_t *src, uint32_t *dst, unsigned cnt)
  73. {
  74. do {
  75. if ((int32_t) *src > 0)
  76. *dst = *src;
  77. src++;
  78. dst++;
  79. } while (--cnt);
  80. }
  81. static NOINLINE void copy_changed_values(
  82. struct fb_var_screeninfo *base,
  83. struct fb_var_screeninfo *set)
  84. {
  85. //if ((int32_t) set->xres > 0) base->xres = set->xres;
  86. //if ((int32_t) set->yres > 0) base->yres = set->yres;
  87. //if ((int32_t) set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
  88. //if ((int32_t) set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
  89. copy_if_gt0(&set->xres, &base->xres, 4);
  90. if ((int32_t) set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
  91. //copy_if_gt0(&set->bits_per_pixel, &base->bits_per_pixel, 1);
  92. //if ((int32_t) set->pixclock > 0) base->pixclock = set->pixclock;
  93. //if ((int32_t) set->left_margin > 0) base->left_margin = set->left_margin;
  94. //if ((int32_t) set->right_margin > 0) base->right_margin = set->right_margin;
  95. //if ((int32_t) set->upper_margin > 0) base->upper_margin = set->upper_margin;
  96. //if ((int32_t) set->lower_margin > 0) base->lower_margin = set->lower_margin;
  97. //if ((int32_t) set->hsync_len > 0) base->hsync_len = set->hsync_len;
  98. //if ((int32_t) set->vsync_len > 0) base->vsync_len = set->vsync_len;
  99. //if ((int32_t) set->sync > 0) base->sync = set->sync;
  100. //if ((int32_t) set->vmode > 0) base->vmode = set->vmode;
  101. copy_if_gt0(&set->pixclock, &base->pixclock, 9);
  102. }
  103. enum {
  104. CMD_FB = 1,
  105. CMD_DB = 2,
  106. CMD_GEOMETRY = 3,
  107. CMD_TIMING = 4,
  108. CMD_ACCEL = 5,
  109. CMD_HSYNC = 6,
  110. CMD_VSYNC = 7,
  111. CMD_LACED = 8,
  112. CMD_DOUBLE = 9,
  113. /* CMD_XCOMPAT = 10, */
  114. CMD_ALL = 11,
  115. CMD_INFO = 12,
  116. CMD_SHOW = 13,
  117. CMD_CHANGE = 14,
  118. #if ENABLE_FEATURE_FBSET_FANCY
  119. CMD_XRES = 100,
  120. CMD_YRES = 101,
  121. CMD_VXRES = 102,
  122. CMD_VYRES = 103,
  123. CMD_DEPTH = 104,
  124. CMD_MATCH = 105,
  125. CMD_PIXCLOCK = 106,
  126. CMD_LEFT = 107,
  127. CMD_RIGHT = 108,
  128. CMD_UPPER = 109,
  129. CMD_LOWER = 110,
  130. CMD_HSLEN = 111,
  131. CMD_VSLEN = 112,
  132. CMD_CSYNC = 113,
  133. CMD_GSYNC = 114,
  134. CMD_EXTSYNC = 115,
  135. CMD_BCAST = 116,
  136. CMD_RGBA = 117,
  137. CMD_STEP = 118,
  138. CMD_MOVE = 119,
  139. #endif
  140. };
  141. static const struct cmdoptions_t {
  142. const char name[9];
  143. const unsigned char param_count;
  144. const unsigned char code;
  145. } g_cmdoptions[] = {
  146. /*"12345678" + NUL */
  147. //TODO: convert to index_in_strings()
  148. { "fb" , 1, CMD_FB },
  149. { "db" , 1, CMD_DB },
  150. { "a" , 0, CMD_ALL },
  151. { "i" , 0, CMD_INFO },
  152. { "g" , 5, CMD_GEOMETRY },
  153. { "t" , 7, CMD_TIMING },
  154. { "accel" , 1, CMD_ACCEL },
  155. { "hsync" , 1, CMD_HSYNC },
  156. { "vsync" , 1, CMD_VSYNC },
  157. { "laced" , 1, CMD_LACED },
  158. { "double" , 1, CMD_DOUBLE },
  159. { "show" , 0, CMD_SHOW },
  160. { "s" , 0, CMD_SHOW },
  161. #if ENABLE_FEATURE_FBSET_FANCY
  162. { "all" , 0, CMD_ALL },
  163. { "xres" , 1, CMD_XRES },
  164. { "yres" , 1, CMD_YRES },
  165. { "vxres" , 1, CMD_VXRES },
  166. { "vyres" , 1, CMD_VYRES },
  167. { "depth" , 1, CMD_DEPTH },
  168. { "match" , 0, CMD_MATCH },
  169. { "geometry", 5, CMD_GEOMETRY },
  170. { "pixclock", 1, CMD_PIXCLOCK },
  171. { "left" , 1, CMD_LEFT },
  172. { "right" , 1, CMD_RIGHT },
  173. { "upper" , 1, CMD_UPPER },
  174. { "lower" , 1, CMD_LOWER },
  175. { "hslen" , 1, CMD_HSLEN },
  176. { "vslen" , 1, CMD_VSLEN },
  177. { "timings" , 7, CMD_TIMING },
  178. { "csync" , 1, CMD_CSYNC },
  179. { "gsync" , 1, CMD_GSYNC },
  180. { "extsync" , 1, CMD_EXTSYNC },
  181. { "bcast" , 1, CMD_BCAST },
  182. { "rgba" , 1, CMD_RGBA },
  183. { "step" , 1, CMD_STEP },
  184. { "move" , 1, CMD_MOVE },
  185. #endif
  186. };
  187. /* taken from linux/fb.h */
  188. enum {
  189. FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
  190. FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
  191. #if ENABLE_FEATURE_FBSET_READMODE
  192. FB_VMODE_INTERLACED = 1, /* interlaced */
  193. FB_VMODE_DOUBLE = 2, /* double scan */
  194. FB_SYNC_EXT = 4, /* external sync */
  195. FB_SYNC_COMP_HIGH_ACT = 8, /* composite sync high active */
  196. #endif
  197. };
  198. #if ENABLE_FEATURE_FBSET_READMODE
  199. static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
  200. {
  201. if (strcmp(buf, what) == 0)
  202. *x &= ~flag;
  203. else
  204. *x |= flag;
  205. }
  206. /* Mode db file contains mode definitions like this:
  207. * mode "800x600-48-lace"
  208. * # D: 36.00 MHz, H: 33.835 kHz, V: 96.39 Hz
  209. * geometry 800 600 800 600 8
  210. * timings 27778 56 80 79 11 128 12
  211. * laced true
  212. * hsync high
  213. * vsync high
  214. * endmode
  215. */
  216. static int read_mode_db(struct fb_var_screeninfo *base, const char *fn,
  217. const char *mode)
  218. {
  219. char *token[2], *p, *s;
  220. parser_t *parser = config_open(fn);
  221. while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
  222. if (strcmp(token[0], "mode") != 0 || !token[1])
  223. continue;
  224. p = strstr(token[1], mode);
  225. if (!p)
  226. continue;
  227. s = p + strlen(mode);
  228. //bb_error_msg("CHECK[%s][%s][%d]", mode, p-1, *s);
  229. /* exact match? */
  230. if (((!*s || isspace(*s)) && '"' != s[-1]) /* end-of-token */
  231. || ('"' == *s && '"' == p[-1]) /* ends with " but starts with " too! */
  232. ) {
  233. //bb_error_msg("FOUND[%s][%s][%s][%d]", token[1], p, mode, isspace(*s));
  234. break;
  235. }
  236. }
  237. if (!token[0])
  238. return 0;
  239. while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
  240. int i;
  241. //bb_error_msg("???[%s][%s]", token[0], token[1]);
  242. if (strcmp(token[0], "endmode") == 0) {
  243. //bb_error_msg("OK[%s]", mode);
  244. return 1;
  245. }
  246. p = token[1];
  247. i = index_in_strings(
  248. "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0rgba\0",
  249. token[0]);
  250. switch (i) {
  251. case 0:
  252. if (sizeof(int) == sizeof(base->xres)) {
  253. sscanf(p, "%d %d %d %d %d",
  254. &base->xres, &base->yres,
  255. &base->xres_virtual, &base->yres_virtual,
  256. &base->bits_per_pixel);
  257. } else {
  258. int base_xres, base_yres;
  259. int base_xres_virtual, base_yres_virtual;
  260. int base_bits_per_pixel;
  261. sscanf(p, "%d %d %d %d %d",
  262. &base_xres, &base_yres,
  263. &base_xres_virtual, &base_yres_virtual,
  264. &base_bits_per_pixel);
  265. base->xres = base_xres;
  266. base->yres = base_yres;
  267. base->xres_virtual = base_xres_virtual;
  268. base->yres_virtual = base_yres_virtual;
  269. base->bits_per_pixel = base_bits_per_pixel;
  270. }
  271. //bb_error_msg("GEO[%s]", p);
  272. break;
  273. case 1:
  274. if (sizeof(int) == sizeof(base->xres)) {
  275. sscanf(p, "%d %d %d %d %d %d %d",
  276. &base->pixclock,
  277. &base->left_margin, &base->right_margin,
  278. &base->upper_margin, &base->lower_margin,
  279. &base->hsync_len, &base->vsync_len);
  280. } else {
  281. int base_pixclock;
  282. int base_left_margin, base_right_margin;
  283. int base_upper_margin, base_lower_margin;
  284. int base_hsync_len, base_vsync_len;
  285. sscanf(p, "%d %d %d %d %d %d %d",
  286. &base_pixclock,
  287. &base_left_margin, &base_right_margin,
  288. &base_upper_margin, &base_lower_margin,
  289. &base_hsync_len, &base_vsync_len);
  290. base->pixclock = base_pixclock;
  291. base->left_margin = base_left_margin;
  292. base->right_margin = base_right_margin;
  293. base->upper_margin = base_upper_margin;
  294. base->lower_margin = base_lower_margin;
  295. base->hsync_len = base_hsync_len;
  296. base->vsync_len = base_vsync_len;
  297. }
  298. //bb_error_msg("TIM[%s]", p);
  299. break;
  300. case 2:
  301. case 3: {
  302. static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
  303. ss(&base->vmode, syncs[i-2], p, "false");
  304. //bb_error_msg("VMODE[%s]", p);
  305. break;
  306. }
  307. case 4:
  308. case 5:
  309. case 6: {
  310. static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
  311. ss(&base->sync, syncs[i-4], p, "low");
  312. //bb_error_msg("SYNC[%s]", p);
  313. break;
  314. }
  315. case 7:
  316. ss(&base->sync, FB_SYNC_EXT, p, "false");
  317. //bb_error_msg("EXTSYNC[%s]", p);
  318. break;
  319. case 8: {
  320. int red_offset, red_length;
  321. int green_offset, green_length;
  322. int blue_offset, blue_length;
  323. int transp_offset, transp_length;
  324. sscanf(p, "%d/%d,%d/%d,%d/%d,%d/%d",
  325. &red_length, &red_offset,
  326. &green_length, &green_offset,
  327. &blue_length, &blue_offset,
  328. &transp_length, &transp_offset);
  329. base->red.offset = red_offset;
  330. base->red.length = red_length;
  331. base->red.msb_right = 0;
  332. base->green.offset = green_offset;
  333. base->green.length = green_length;
  334. base->green.msb_right = 0;
  335. base->blue.offset = blue_offset;
  336. base->blue.length = blue_length;
  337. base->blue.msb_right = 0;
  338. base->transp.offset = transp_offset;
  339. base->transp.length = transp_length;
  340. base->transp.msb_right = 0;
  341. }
  342. }
  343. }
  344. return 0;
  345. }
  346. #endif
  347. static NOINLINE void showmode(struct fb_var_screeninfo *v)
  348. {
  349. double drate = 0, hrate = 0, vrate = 0;
  350. if (v->pixclock) {
  351. drate = 1e12 / v->pixclock;
  352. hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
  353. vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
  354. }
  355. printf("\nmode \"%ux%u-%u\"\n"
  356. #if ENABLE_FEATURE_FBSET_FANCY
  357. "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
  358. #endif
  359. "\tgeometry %u %u %u %u %u\n"
  360. "\ttimings %u %u %u %u %u %u %u\n"
  361. "\taccel %s\n"
  362. "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
  363. "endmode\n\n",
  364. v->xres, v->yres, (int) (vrate + 0.5),
  365. #if ENABLE_FEATURE_FBSET_FANCY
  366. drate / 1e6, hrate / 1e3, vrate,
  367. #endif
  368. v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
  369. v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
  370. v->hsync_len, v->vsync_len,
  371. (v->accel_flags > 0 ? "true" : "false"),
  372. v->red.length, v->red.offset, v->green.length, v->green.offset,
  373. v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
  374. }
  375. int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  376. int fbset_main(int argc, char **argv)
  377. {
  378. enum {
  379. OPT_CHANGE = (1 << 0),
  380. OPT_SHOW = (1 << 1),
  381. OPT_READMODE = (1 << 2),
  382. OPT_ALL = (1 << 3),
  383. };
  384. struct fb_var_screeninfo var_old, var_set;
  385. int fh, i;
  386. unsigned options = 0;
  387. const char *fbdev = DEFAULTFBDEV;
  388. IF_FEATURE_FBSET_READMODE(const char *modefile = DEFAULTFBMODE;)
  389. char *thisarg;
  390. char *mode = mode; /* for compiler */
  391. memset(&var_set, 0xff, sizeof(var_set)); /* set all to -1 */
  392. /* parse cmd args.... why do they have to make things so difficult? */
  393. argv++;
  394. argc--;
  395. for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
  396. if (thisarg[0] != '-') {
  397. if (!ENABLE_FEATURE_FBSET_READMODE || argc != 1)
  398. bb_show_usage();
  399. mode = thisarg;
  400. options |= OPT_READMODE;
  401. goto contin;
  402. }
  403. for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
  404. if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
  405. continue;
  406. if (argc <= g_cmdoptions[i].param_count)
  407. bb_show_usage();
  408. switch (g_cmdoptions[i].code) {
  409. case CMD_FB:
  410. fbdev = argv[1];
  411. break;
  412. case CMD_DB:
  413. IF_FEATURE_FBSET_READMODE(modefile = argv[1];)
  414. break;
  415. case CMD_ALL:
  416. options |= OPT_ALL;
  417. break;
  418. case CMD_SHOW:
  419. options |= OPT_SHOW;
  420. break;
  421. case CMD_GEOMETRY:
  422. var_set.xres = xatou32(argv[1]);
  423. var_set.yres = xatou32(argv[2]);
  424. var_set.xres_virtual = xatou32(argv[3]);
  425. var_set.yres_virtual = xatou32(argv[4]);
  426. var_set.bits_per_pixel = xatou32(argv[5]);
  427. break;
  428. case CMD_TIMING:
  429. var_set.pixclock = xatou32(argv[1]);
  430. var_set.left_margin = xatou32(argv[2]);
  431. var_set.right_margin = xatou32(argv[3]);
  432. var_set.upper_margin = xatou32(argv[4]);
  433. var_set.lower_margin = xatou32(argv[5]);
  434. var_set.hsync_len = xatou32(argv[6]);
  435. var_set.vsync_len = xatou32(argv[7]);
  436. break;
  437. case CMD_ACCEL:
  438. break;
  439. case CMD_HSYNC:
  440. var_set.sync |= FB_SYNC_HOR_HIGH_ACT;
  441. break;
  442. case CMD_VSYNC:
  443. var_set.sync |= FB_SYNC_VERT_HIGH_ACT;
  444. break;
  445. #if ENABLE_FEATURE_FBSET_FANCY
  446. case CMD_XRES:
  447. var_set.xres = xatou32(argv[1]);
  448. break;
  449. case CMD_YRES:
  450. var_set.yres = xatou32(argv[1]);
  451. break;
  452. case CMD_DEPTH:
  453. var_set.bits_per_pixel = xatou32(argv[1]);
  454. break;
  455. #endif
  456. }
  457. switch (g_cmdoptions[i].code) {
  458. case CMD_FB:
  459. case CMD_DB:
  460. case CMD_ALL:
  461. case CMD_SHOW:
  462. break;
  463. default:
  464. /* other commands imply changes */
  465. options |= OPT_CHANGE;
  466. }
  467. argc -= g_cmdoptions[i].param_count;
  468. argv += g_cmdoptions[i].param_count;
  469. goto contin;
  470. }
  471. bb_show_usage();
  472. contin: ;
  473. }
  474. fh = xopen(fbdev, O_RDONLY);
  475. xioctl(fh, FBIOGET_VSCREENINFO, &var_old);
  476. if (options & OPT_READMODE) {
  477. #if ENABLE_FEATURE_FBSET_READMODE
  478. if (!read_mode_db(&var_old, modefile, mode)) {
  479. bb_error_msg_and_die("unknown video mode '%s'", mode);
  480. }
  481. options |= OPT_CHANGE;
  482. #endif
  483. }
  484. if (options & OPT_CHANGE) {
  485. copy_changed_values(&var_old, &var_set);
  486. if (options & OPT_ALL)
  487. var_old.activate = FB_ACTIVATE_ALL;
  488. xioctl(fh, FBIOPUT_VSCREENINFO, &var_old);
  489. }
  490. if (options == 0 || (options & OPT_SHOW))
  491. showmode(&var_old);
  492. if (ENABLE_FEATURE_CLEAN_UP)
  493. close(fh);
  494. return EXIT_SUCCESS;
  495. }