fbsysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * fbsysfs.c - framebuffer device class and attributes
  3. *
  4. * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
  5. *
  6. * This program is free software you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Note: currently there's only stubs for framebuffer_alloc and
  13. * framebuffer_release here. The reson for that is that until all drivers
  14. * are converted to use it a sysfsification will open OOPSable races.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/fb.h>
  18. #include <linux/console.h>
  19. /**
  20. * framebuffer_alloc - creates a new frame buffer info structure
  21. *
  22. * @size: size of driver private data, can be zero
  23. * @dev: pointer to the device for this fb, this can be NULL
  24. *
  25. * Creates a new frame buffer info structure. Also reserves @size bytes
  26. * for driver private data (info->par). info->par (if any) will be
  27. * aligned to sizeof(long).
  28. *
  29. * Returns the new structure, or NULL if an error occured.
  30. *
  31. */
  32. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
  33. {
  34. #define BYTES_PER_LONG (BITS_PER_LONG/8)
  35. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  36. int fb_info_size = sizeof(struct fb_info);
  37. struct fb_info *info;
  38. char *p;
  39. if (size)
  40. fb_info_size += PADDING;
  41. p = kmalloc(fb_info_size + size, GFP_KERNEL);
  42. if (!p)
  43. return NULL;
  44. memset(p, 0, fb_info_size + size);
  45. info = (struct fb_info *) p;
  46. if (size)
  47. info->par = p + fb_info_size;
  48. info->device = dev;
  49. return info;
  50. #undef PADDING
  51. #undef BYTES_PER_LONG
  52. }
  53. EXPORT_SYMBOL(framebuffer_alloc);
  54. /**
  55. * framebuffer_release - marks the structure available for freeing
  56. *
  57. * @info: frame buffer info structure
  58. *
  59. * Drop the reference count of the class_device embedded in the
  60. * framebuffer info structure.
  61. *
  62. */
  63. void framebuffer_release(struct fb_info *info)
  64. {
  65. kfree(info);
  66. }
  67. EXPORT_SYMBOL(framebuffer_release);
  68. static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
  69. {
  70. int err;
  71. var->activate |= FB_ACTIVATE_FORCE;
  72. acquire_console_sem();
  73. fb_info->flags |= FBINFO_MISC_USEREVENT;
  74. err = fb_set_var(fb_info, var);
  75. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  76. release_console_sem();
  77. if (err)
  78. return err;
  79. return 0;
  80. }
  81. static int mode_string(char *buf, unsigned int offset,
  82. const struct fb_videomode *mode)
  83. {
  84. char m = 'U';
  85. if (mode->flag & FB_MODE_IS_DETAILED)
  86. m = 'D';
  87. if (mode->flag & FB_MODE_IS_VESA)
  88. m = 'V';
  89. if (mode->flag & FB_MODE_IS_STANDARD)
  90. m = 'S';
  91. return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh);
  92. }
  93. static ssize_t store_mode(struct class_device *class_device, const char * buf,
  94. size_t count)
  95. {
  96. struct fb_info *fb_info = class_get_devdata(class_device);
  97. char mstr[100];
  98. struct fb_var_screeninfo var;
  99. struct fb_modelist *modelist;
  100. struct fb_videomode *mode;
  101. struct list_head *pos;
  102. size_t i;
  103. int err;
  104. memset(&var, 0, sizeof(var));
  105. list_for_each(pos, &fb_info->modelist) {
  106. modelist = list_entry(pos, struct fb_modelist, list);
  107. mode = &modelist->mode;
  108. i = mode_string(mstr, 0, mode);
  109. if (strncmp(mstr, buf, max(count, i)) == 0) {
  110. var = fb_info->var;
  111. fb_videomode_to_var(&var, mode);
  112. if ((err = activate(fb_info, &var)))
  113. return err;
  114. fb_info->mode = mode;
  115. return count;
  116. }
  117. }
  118. return -EINVAL;
  119. }
  120. static ssize_t show_mode(struct class_device *class_device, char *buf)
  121. {
  122. struct fb_info *fb_info = class_get_devdata(class_device);
  123. if (!fb_info->mode)
  124. return 0;
  125. return mode_string(buf, 0, fb_info->mode);
  126. }
  127. static ssize_t store_modes(struct class_device *class_device, const char * buf,
  128. size_t count)
  129. {
  130. struct fb_info *fb_info = class_get_devdata(class_device);
  131. LIST_HEAD(old_list);
  132. int i = count / sizeof(struct fb_videomode);
  133. if (i * sizeof(struct fb_videomode) != count)
  134. return -EINVAL;
  135. acquire_console_sem();
  136. list_splice(&fb_info->modelist, &old_list);
  137. fb_videomode_to_modelist((struct fb_videomode *)buf, i,
  138. &fb_info->modelist);
  139. if (fb_new_modelist(fb_info)) {
  140. fb_destroy_modelist(&fb_info->modelist);
  141. list_splice(&old_list, &fb_info->modelist);
  142. } else
  143. fb_destroy_modelist(&old_list);
  144. release_console_sem();
  145. return 0;
  146. }
  147. static ssize_t show_modes(struct class_device *class_device, char *buf)
  148. {
  149. struct fb_info *fb_info = class_get_devdata(class_device);
  150. unsigned int i;
  151. struct list_head *pos;
  152. struct fb_modelist *modelist;
  153. const struct fb_videomode *mode;
  154. i = 0;
  155. list_for_each(pos, &fb_info->modelist) {
  156. modelist = list_entry(pos, struct fb_modelist, list);
  157. mode = &modelist->mode;
  158. i += mode_string(buf, i, mode);
  159. }
  160. return i;
  161. }
  162. static ssize_t store_bpp(struct class_device *class_device, const char * buf,
  163. size_t count)
  164. {
  165. struct fb_info *fb_info = class_get_devdata(class_device);
  166. struct fb_var_screeninfo var;
  167. char ** last = NULL;
  168. int err;
  169. var = fb_info->var;
  170. var.bits_per_pixel = simple_strtoul(buf, last, 0);
  171. if ((err = activate(fb_info, &var)))
  172. return err;
  173. return count;
  174. }
  175. static ssize_t show_bpp(struct class_device *class_device, char *buf)
  176. {
  177. struct fb_info *fb_info = class_get_devdata(class_device);
  178. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
  179. }
  180. static ssize_t store_rotate(struct class_device *class_device, const char *buf,
  181. size_t count)
  182. {
  183. struct fb_info *fb_info = class_get_devdata(class_device);
  184. struct fb_var_screeninfo var;
  185. char **last = NULL;
  186. int err;
  187. var = fb_info->var;
  188. var.rotate = simple_strtoul(buf, last, 0);
  189. if ((err = activate(fb_info, &var)))
  190. return err;
  191. return count;
  192. }
  193. static ssize_t show_rotate(struct class_device *class_device, char *buf)
  194. {
  195. struct fb_info *fb_info = class_get_devdata(class_device);
  196. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
  197. }
  198. static ssize_t store_con_rotate(struct class_device *class_device,
  199. const char *buf, size_t count)
  200. {
  201. struct fb_info *fb_info = class_get_devdata(class_device);
  202. int rotate;
  203. char **last = NULL;
  204. acquire_console_sem();
  205. rotate = simple_strtoul(buf, last, 0);
  206. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
  207. release_console_sem();
  208. return count;
  209. }
  210. static ssize_t store_con_rotate_all(struct class_device *class_device,
  211. const char *buf, size_t count)
  212. {
  213. struct fb_info *fb_info = class_get_devdata(class_device);
  214. int rotate;
  215. char **last = NULL;
  216. acquire_console_sem();
  217. rotate = simple_strtoul(buf, last, 0);
  218. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
  219. release_console_sem();
  220. return count;
  221. }
  222. static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
  223. {
  224. struct fb_info *fb_info = class_get_devdata(class_device);
  225. int rotate;
  226. acquire_console_sem();
  227. rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
  228. release_console_sem();
  229. return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
  230. }
  231. static ssize_t store_virtual(struct class_device *class_device,
  232. const char * buf, size_t count)
  233. {
  234. struct fb_info *fb_info = class_get_devdata(class_device);
  235. struct fb_var_screeninfo var;
  236. char *last = NULL;
  237. int err;
  238. var = fb_info->var;
  239. var.xres_virtual = simple_strtoul(buf, &last, 0);
  240. last++;
  241. if (last - buf >= count)
  242. return -EINVAL;
  243. var.yres_virtual = simple_strtoul(last, &last, 0);
  244. if ((err = activate(fb_info, &var)))
  245. return err;
  246. return count;
  247. }
  248. static ssize_t show_virtual(struct class_device *class_device, char *buf)
  249. {
  250. struct fb_info *fb_info = class_get_devdata(class_device);
  251. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
  252. fb_info->var.yres_virtual);
  253. }
  254. static ssize_t show_stride(struct class_device *class_device, char *buf)
  255. {
  256. struct fb_info *fb_info = class_get_devdata(class_device);
  257. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
  258. }
  259. /* Format for cmap is "%02x%c%4x%4x%4x\n" */
  260. /* %02x entry %c transp %4x red %4x blue %4x green \n */
  261. /* 256 rows at 16 chars equals 4096, the normal page size */
  262. /* the code will automatically adjust for different page sizes */
  263. static ssize_t store_cmap(struct class_device *class_device, const char *buf,
  264. size_t count)
  265. {
  266. struct fb_info *fb_info = class_get_devdata(class_device);
  267. int rc, i, start, length, transp = 0;
  268. if ((count > PAGE_SIZE) || ((count % 16) != 0))
  269. return -EINVAL;
  270. if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap)
  271. return -EINVAL;
  272. sscanf(buf, "%02x", &start);
  273. length = count / 16;
  274. for (i = 0; i < length; i++)
  275. if (buf[i * 16 + 2] != ' ')
  276. transp = 1;
  277. /* If we can batch, do it */
  278. if (fb_info->fbops->fb_setcmap && length > 1) {
  279. struct fb_cmap umap;
  280. memset(&umap, 0, sizeof(umap));
  281. if ((rc = fb_alloc_cmap(&umap, length, transp)))
  282. return rc;
  283. umap.start = start;
  284. for (i = 0; i < length; i++) {
  285. sscanf(&buf[i * 16 + 3], "%4hx", &umap.red[i]);
  286. sscanf(&buf[i * 16 + 7], "%4hx", &umap.blue[i]);
  287. sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]);
  288. if (transp)
  289. umap.transp[i] = (buf[i * 16 + 2] != ' ');
  290. }
  291. rc = fb_info->fbops->fb_setcmap(&umap, fb_info);
  292. fb_copy_cmap(&umap, &fb_info->cmap);
  293. fb_dealloc_cmap(&umap);
  294. return rc;
  295. }
  296. for (i = 0; i < length; i++) {
  297. u16 red, blue, green, tsp;
  298. sscanf(&buf[i * 16 + 3], "%4hx", &red);
  299. sscanf(&buf[i * 16 + 7], "%4hx", &blue);
  300. sscanf(&buf[i * 16 + 11], "%4hx", &green);
  301. tsp = (buf[i * 16 + 2] != ' ');
  302. if ((rc = fb_info->fbops->fb_setcolreg(start++,
  303. red, green, blue, tsp, fb_info)))
  304. return rc;
  305. fb_info->cmap.red[i] = red;
  306. fb_info->cmap.blue[i] = blue;
  307. fb_info->cmap.green[i] = green;
  308. if (transp)
  309. fb_info->cmap.transp[i] = tsp;
  310. }
  311. return 0;
  312. }
  313. static ssize_t show_cmap(struct class_device *class_device, char *buf)
  314. {
  315. struct fb_info *fb_info = class_get_devdata(class_device);
  316. unsigned int i;
  317. if (!fb_info->cmap.red || !fb_info->cmap.blue ||
  318. !fb_info->cmap.green)
  319. return -EINVAL;
  320. if (fb_info->cmap.len > PAGE_SIZE / 16)
  321. return -EINVAL;
  322. /* don't mess with the format, the buffer is PAGE_SIZE */
  323. /* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */
  324. for (i = 0; i < fb_info->cmap.len; i++) {
  325. snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start,
  326. ((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '),
  327. fb_info->cmap.red[i], fb_info->cmap.blue[i],
  328. fb_info->cmap.green[i]);
  329. }
  330. return 16 * fb_info->cmap.len;
  331. }
  332. static ssize_t store_blank(struct class_device *class_device, const char * buf,
  333. size_t count)
  334. {
  335. struct fb_info *fb_info = class_get_devdata(class_device);
  336. char *last = NULL;
  337. int err;
  338. acquire_console_sem();
  339. fb_info->flags |= FBINFO_MISC_USEREVENT;
  340. err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
  341. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  342. release_console_sem();
  343. if (err < 0)
  344. return err;
  345. return count;
  346. }
  347. static ssize_t show_blank(struct class_device *class_device, char *buf)
  348. {
  349. // struct fb_info *fb_info = class_get_devdata(class_device);
  350. return 0;
  351. }
  352. static ssize_t store_console(struct class_device *class_device,
  353. const char * buf, size_t count)
  354. {
  355. // struct fb_info *fb_info = class_get_devdata(class_device);
  356. return 0;
  357. }
  358. static ssize_t show_console(struct class_device *class_device, char *buf)
  359. {
  360. // struct fb_info *fb_info = class_get_devdata(class_device);
  361. return 0;
  362. }
  363. static ssize_t store_cursor(struct class_device *class_device,
  364. const char * buf, size_t count)
  365. {
  366. // struct fb_info *fb_info = class_get_devdata(class_device);
  367. return 0;
  368. }
  369. static ssize_t show_cursor(struct class_device *class_device, char *buf)
  370. {
  371. // struct fb_info *fb_info = class_get_devdata(class_device);
  372. return 0;
  373. }
  374. static ssize_t store_pan(struct class_device *class_device, const char * buf,
  375. size_t count)
  376. {
  377. struct fb_info *fb_info = class_get_devdata(class_device);
  378. struct fb_var_screeninfo var;
  379. char *last = NULL;
  380. int err;
  381. var = fb_info->var;
  382. var.xoffset = simple_strtoul(buf, &last, 0);
  383. last++;
  384. if (last - buf >= count)
  385. return -EINVAL;
  386. var.yoffset = simple_strtoul(last, &last, 0);
  387. acquire_console_sem();
  388. err = fb_pan_display(fb_info, &var);
  389. release_console_sem();
  390. if (err < 0)
  391. return err;
  392. return count;
  393. }
  394. static ssize_t show_pan(struct class_device *class_device, char *buf)
  395. {
  396. struct fb_info *fb_info = class_get_devdata(class_device);
  397. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
  398. fb_info->var.xoffset);
  399. }
  400. static ssize_t show_name(struct class_device *class_device, char *buf)
  401. {
  402. struct fb_info *fb_info = class_get_devdata(class_device);
  403. return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
  404. }
  405. static ssize_t store_fbstate(struct class_device *class_device,
  406. const char *buf, size_t count)
  407. {
  408. struct fb_info *fb_info = class_get_devdata(class_device);
  409. u32 state;
  410. char *last = NULL;
  411. state = simple_strtoul(buf, &last, 0);
  412. acquire_console_sem();
  413. fb_set_suspend(fb_info, (int)state);
  414. release_console_sem();
  415. return count;
  416. }
  417. static ssize_t show_fbstate(struct class_device *class_device, char *buf)
  418. {
  419. struct fb_info *fb_info = class_get_devdata(class_device);
  420. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
  421. }
  422. static struct class_device_attribute class_device_attrs[] = {
  423. __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
  424. __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
  425. __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
  426. __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
  427. __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
  428. __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
  429. __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
  430. __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
  431. __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
  432. __ATTR(name, S_IRUGO, show_name, NULL),
  433. __ATTR(stride, S_IRUGO, show_stride, NULL),
  434. __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
  435. __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
  436. __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
  437. __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
  438. };
  439. int fb_init_class_device(struct fb_info *fb_info)
  440. {
  441. unsigned int i;
  442. class_set_devdata(fb_info->class_device, fb_info);
  443. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  444. class_device_create_file(fb_info->class_device,
  445. &class_device_attrs[i]);
  446. return 0;
  447. }
  448. void fb_cleanup_class_device(struct fb_info *fb_info)
  449. {
  450. unsigned int i;
  451. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  452. class_device_remove_file(fb_info->class_device,
  453. &class_device_attrs[i]);
  454. }