3
0

fbset.c 14 KB

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