fbset.c 11 KB

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