fbset.c 13 KB

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