fbset.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. { "fb" , 1, CMD_FB },
  148. { "db" , 1, CMD_DB },
  149. { "a" , 0, CMD_ALL },
  150. { "i" , 0, CMD_INFO },
  151. { "g" , 5, CMD_GEOMETRY },
  152. { "t" , 7, CMD_TIMING },
  153. { "accel" , 1, CMD_ACCEL },
  154. { "hsync" , 1, CMD_HSYNC },
  155. { "vsync" , 1, CMD_VSYNC },
  156. { "laced" , 1, CMD_LACED },
  157. { "double" , 1, CMD_DOUBLE },
  158. { "show" , 0, CMD_SHOW },
  159. { "s" , 0, CMD_SHOW },
  160. #if ENABLE_FEATURE_FBSET_FANCY
  161. { "all" , 0, CMD_ALL },
  162. { "xres" , 1, CMD_XRES },
  163. { "yres" , 1, CMD_YRES },
  164. { "vxres" , 1, CMD_VXRES },
  165. { "vyres" , 1, CMD_VYRES },
  166. { "depth" , 1, CMD_DEPTH },
  167. { "match" , 0, CMD_MATCH },
  168. { "geometry", 5, CMD_GEOMETRY },
  169. { "pixclock", 1, CMD_PIXCLOCK },
  170. { "left" , 1, CMD_LEFT },
  171. { "right" , 1, CMD_RIGHT },
  172. { "upper" , 1, CMD_UPPER },
  173. { "lower" , 1, CMD_LOWER },
  174. { "hslen" , 1, CMD_HSLEN },
  175. { "vslen" , 1, CMD_VSLEN },
  176. { "timings" , 7, CMD_TIMING },
  177. { "csync" , 1, CMD_CSYNC },
  178. { "gsync" , 1, CMD_GSYNC },
  179. { "extsync" , 1, CMD_EXTSYNC },
  180. { "bcast" , 1, CMD_BCAST },
  181. { "rgba" , 1, CMD_RGBA },
  182. { "step" , 1, CMD_STEP },
  183. { "move" , 1, CMD_MOVE },
  184. #endif
  185. };
  186. /* taken from linux/fb.h */
  187. enum {
  188. FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
  189. FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
  190. #if ENABLE_FEATURE_FBSET_READMODE
  191. FB_VMODE_INTERLACED = 1, /* interlaced */
  192. FB_VMODE_DOUBLE = 2, /* double scan */
  193. FB_SYNC_EXT = 4, /* external sync */
  194. FB_SYNC_COMP_HIGH_ACT = 8, /* composite sync high active */
  195. #endif
  196. };
  197. #if ENABLE_FEATURE_FBSET_READMODE
  198. static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
  199. {
  200. if (strcmp(buf, what) == 0)
  201. *x &= ~flag;
  202. else
  203. *x |= flag;
  204. }
  205. /* Mode db file contains mode definitions like this:
  206. * mode "800x600-48-lace"
  207. * # D: 36.00 MHz, H: 33.835 kHz, V: 96.39 Hz
  208. * geometry 800 600 800 600 8
  209. * timings 27778 56 80 79 11 128 12
  210. * laced true
  211. * hsync high
  212. * vsync high
  213. * endmode
  214. */
  215. static int read_mode_db(struct fb_var_screeninfo *base, const char *fn,
  216. const char *mode)
  217. {
  218. char *token[2], *p, *s;
  219. parser_t *parser = config_open(fn);
  220. while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
  221. if (strcmp(token[0], "mode") != 0 || !token[1])
  222. continue;
  223. p = strstr(token[1], mode);
  224. if (!p)
  225. continue;
  226. s = p + strlen(mode);
  227. //bb_info_msg("CHECK[%s][%s][%d]", mode, p-1, *s);
  228. /* exact match? */
  229. if (((!*s || isspace(*s)) && '"' != s[-1]) /* end-of-token */
  230. || ('"' == *s && '"' == p[-1]) /* ends with " but starts with " too! */
  231. ) {
  232. //bb_info_msg("FOUND[%s][%s][%s][%d]", token[1], p, mode, isspace(*s));
  233. break;
  234. }
  235. }
  236. if (!token[0])
  237. return 0;
  238. while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
  239. int i;
  240. //bb_info_msg("???[%s][%s]", token[0], token[1]);
  241. if (strcmp(token[0], "endmode") == 0) {
  242. //bb_info_msg("OK[%s]", mode);
  243. return 1;
  244. }
  245. p = token[1];
  246. i = index_in_strings(
  247. "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0rgba\0",
  248. token[0]);
  249. switch (i) {
  250. case 0:
  251. if (sizeof(int) == sizeof(base->xres)) {
  252. sscanf(p, "%d %d %d %d %d",
  253. &base->xres, &base->yres,
  254. &base->xres_virtual, &base->yres_virtual,
  255. &base->bits_per_pixel);
  256. } else {
  257. int base_xres, base_yres;
  258. int base_xres_virtual, base_yres_virtual;
  259. int base_bits_per_pixel;
  260. sscanf(p, "%d %d %d %d %d",
  261. &base_xres, &base_yres,
  262. &base_xres_virtual, &base_yres_virtual,
  263. &base_bits_per_pixel);
  264. base->xres = base_xres;
  265. base->yres = base_yres;
  266. base->xres_virtual = base_xres_virtual;
  267. base->yres_virtual = base_yres_virtual;
  268. base->bits_per_pixel = base_bits_per_pixel;
  269. }
  270. //bb_info_msg("GEO[%s]", p);
  271. break;
  272. case 1:
  273. if (sizeof(int) == sizeof(base->xres)) {
  274. sscanf(p, "%d %d %d %d %d %d %d",
  275. &base->pixclock,
  276. &base->left_margin, &base->right_margin,
  277. &base->upper_margin, &base->lower_margin,
  278. &base->hsync_len, &base->vsync_len);
  279. } else {
  280. int base_pixclock;
  281. int base_left_margin, base_right_margin;
  282. int base_upper_margin, base_lower_margin;
  283. int base_hsync_len, base_vsync_len;
  284. sscanf(p, "%d %d %d %d %d %d %d",
  285. &base_pixclock,
  286. &base_left_margin, &base_right_margin,
  287. &base_upper_margin, &base_lower_margin,
  288. &base_hsync_len, &base_vsync_len);
  289. base->pixclock = base_pixclock;
  290. base->left_margin = base_left_margin;
  291. base->right_margin = base_right_margin;
  292. base->upper_margin = base_upper_margin;
  293. base->lower_margin = base_lower_margin;
  294. base->hsync_len = base_hsync_len;
  295. base->vsync_len = base_vsync_len;
  296. }
  297. //bb_info_msg("TIM[%s]", p);
  298. break;
  299. case 2:
  300. case 3: {
  301. static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
  302. ss(&base->vmode, syncs[i-2], p, "false");
  303. //bb_info_msg("VMODE[%s]", p);
  304. break;
  305. }
  306. case 4:
  307. case 5:
  308. case 6: {
  309. static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
  310. ss(&base->sync, syncs[i-4], p, "low");
  311. //bb_info_msg("SYNC[%s]", p);
  312. break;
  313. }
  314. case 7:
  315. ss(&base->sync, FB_SYNC_EXT, p, "false");
  316. //bb_info_msg("EXTSYNC[%s]", p);
  317. break;
  318. case 8: {
  319. int red_offset, red_length;
  320. int green_offset, green_length;
  321. int blue_offset, blue_length;
  322. int transp_offset, transp_length;
  323. sscanf(p, "%d/%d,%d/%d,%d/%d,%d/%d",
  324. &red_length, &red_offset,
  325. &green_length, &green_offset,
  326. &blue_length, &blue_offset,
  327. &transp_length, &transp_offset);
  328. base->red.offset = red_offset;
  329. base->red.length = red_length;
  330. base->red.msb_right = 0;
  331. base->green.offset = green_offset;
  332. base->green.length = green_length;
  333. base->green.msb_right = 0;
  334. base->blue.offset = blue_offset;
  335. base->blue.length = blue_length;
  336. base->blue.msb_right = 0;
  337. base->transp.offset = transp_offset;
  338. base->transp.length = transp_length;
  339. base->transp.msb_right = 0;
  340. }
  341. }
  342. }
  343. return 0;
  344. }
  345. #endif
  346. static NOINLINE void showmode(struct fb_var_screeninfo *v)
  347. {
  348. double drate = 0, hrate = 0, vrate = 0;
  349. if (v->pixclock) {
  350. drate = 1e12 / v->pixclock;
  351. hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
  352. vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
  353. }
  354. printf("\nmode \"%ux%u-%u\"\n"
  355. #if ENABLE_FEATURE_FBSET_FANCY
  356. "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
  357. #endif
  358. "\tgeometry %u %u %u %u %u\n"
  359. "\ttimings %u %u %u %u %u %u %u\n"
  360. "\taccel %s\n"
  361. "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
  362. "endmode\n\n",
  363. v->xres, v->yres, (int) (vrate + 0.5),
  364. #if ENABLE_FEATURE_FBSET_FANCY
  365. drate / 1e6, hrate / 1e3, vrate,
  366. #endif
  367. v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
  368. v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
  369. v->hsync_len, v->vsync_len,
  370. (v->accel_flags > 0 ? "true" : "false"),
  371. v->red.length, v->red.offset, v->green.length, v->green.offset,
  372. v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
  373. }
  374. int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  375. int fbset_main(int argc, char **argv)
  376. {
  377. enum {
  378. OPT_CHANGE = (1 << 0),
  379. OPT_SHOW = (1 << 1),
  380. OPT_READMODE = (1 << 2),
  381. OPT_ALL = (1 << 3),
  382. };
  383. struct fb_var_screeninfo var_old, var_set;
  384. int fh, i;
  385. unsigned options = 0;
  386. const char *fbdev = DEFAULTFBDEV;
  387. const char *modefile = DEFAULTFBMODE;
  388. char *thisarg;
  389. char *mode = mode; /* for compiler */
  390. memset(&var_set, 0xff, sizeof(var_set)); /* set all to -1 */
  391. /* parse cmd args.... why do they have to make things so difficult? */
  392. argv++;
  393. argc--;
  394. for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
  395. if (thisarg[0] != '-') {
  396. if (!ENABLE_FEATURE_FBSET_READMODE || argc != 1)
  397. bb_show_usage();
  398. mode = thisarg;
  399. options |= OPT_READMODE;
  400. goto contin;
  401. }
  402. for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
  403. if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
  404. continue;
  405. if (argc <= g_cmdoptions[i].param_count)
  406. bb_show_usage();
  407. switch (g_cmdoptions[i].code) {
  408. case CMD_FB:
  409. fbdev = argv[1];
  410. break;
  411. case CMD_DB:
  412. modefile = argv[1];
  413. break;
  414. case CMD_ALL:
  415. options |= OPT_ALL;
  416. break;
  417. case CMD_SHOW:
  418. options |= OPT_SHOW;
  419. break;
  420. case CMD_GEOMETRY:
  421. var_set.xres = xatou32(argv[1]);
  422. var_set.yres = xatou32(argv[2]);
  423. var_set.xres_virtual = xatou32(argv[3]);
  424. var_set.yres_virtual = xatou32(argv[4]);
  425. var_set.bits_per_pixel = xatou32(argv[5]);
  426. break;
  427. case CMD_TIMING:
  428. var_set.pixclock = xatou32(argv[1]);
  429. var_set.left_margin = xatou32(argv[2]);
  430. var_set.right_margin = xatou32(argv[3]);
  431. var_set.upper_margin = xatou32(argv[4]);
  432. var_set.lower_margin = xatou32(argv[5]);
  433. var_set.hsync_len = xatou32(argv[6]);
  434. var_set.vsync_len = xatou32(argv[7]);
  435. break;
  436. case CMD_ACCEL:
  437. break;
  438. case CMD_HSYNC:
  439. var_set.sync |= FB_SYNC_HOR_HIGH_ACT;
  440. break;
  441. case CMD_VSYNC:
  442. var_set.sync |= FB_SYNC_VERT_HIGH_ACT;
  443. break;
  444. #if ENABLE_FEATURE_FBSET_FANCY
  445. case CMD_XRES:
  446. var_set.xres = xatou32(argv[1]);
  447. break;
  448. case CMD_YRES:
  449. var_set.yres = xatou32(argv[1]);
  450. break;
  451. case CMD_DEPTH:
  452. var_set.bits_per_pixel = xatou32(argv[1]);
  453. break;
  454. #endif
  455. }
  456. switch (g_cmdoptions[i].code) {
  457. case CMD_FB:
  458. case CMD_DB:
  459. case CMD_ALL:
  460. case CMD_SHOW:
  461. break;
  462. default:
  463. /* other commands imply changes */
  464. options |= OPT_CHANGE;
  465. }
  466. argc -= g_cmdoptions[i].param_count;
  467. argv += g_cmdoptions[i].param_count;
  468. goto contin;
  469. }
  470. bb_show_usage();
  471. contin: ;
  472. }
  473. fh = xopen(fbdev, O_RDONLY);
  474. xioctl(fh, FBIOGET_VSCREENINFO, &var_old);
  475. if (options & OPT_READMODE) {
  476. #if ENABLE_FEATURE_FBSET_READMODE
  477. if (!read_mode_db(&var_old, modefile, mode)) {
  478. bb_error_msg_and_die("unknown video mode '%s'", mode);
  479. }
  480. options |= OPT_CHANGE;
  481. #endif
  482. }
  483. if (options & OPT_CHANGE) {
  484. copy_changed_values(&var_old, &var_set);
  485. if (options & OPT_ALL)
  486. var_old.activate = FB_ACTIVATE_ALL;
  487. xioctl(fh, FBIOPUT_VSCREENINFO, &var_old);
  488. }
  489. if (options == 0 || (options & OPT_SHOW))
  490. showmode(&var_old);
  491. if (ENABLE_FEATURE_CLEAN_UP)
  492. close(fh);
  493. return EXIT_SUCCESS;
  494. }