fbset.c 12 KB

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