fbset.c 11 KB

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