fbsysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. #include <linux/module.h>
  20. /**
  21. * framebuffer_alloc - creates a new frame buffer info structure
  22. *
  23. * @size: size of driver private data, can be zero
  24. * @dev: pointer to the device for this fb, this can be NULL
  25. *
  26. * Creates a new frame buffer info structure. Also reserves @size bytes
  27. * for driver private data (info->par). info->par (if any) will be
  28. * aligned to sizeof(long).
  29. *
  30. * Returns the new structure, or NULL if an error occured.
  31. *
  32. */
  33. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
  34. {
  35. #define BYTES_PER_LONG (BITS_PER_LONG/8)
  36. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  37. int fb_info_size = sizeof(struct fb_info);
  38. struct fb_info *info;
  39. char *p;
  40. if (size)
  41. fb_info_size += PADDING;
  42. p = kzalloc(fb_info_size + size, GFP_KERNEL);
  43. if (!p)
  44. return NULL;
  45. info = (struct fb_info *) p;
  46. if (size)
  47. info->par = p + fb_info_size;
  48. info->device = dev;
  49. #ifdef CONFIG_FB_BACKLIGHT
  50. mutex_init(&info->bl_mutex);
  51. #endif
  52. return info;
  53. #undef PADDING
  54. #undef BYTES_PER_LONG
  55. }
  56. EXPORT_SYMBOL(framebuffer_alloc);
  57. /**
  58. * framebuffer_release - marks the structure available for freeing
  59. *
  60. * @info: frame buffer info structure
  61. *
  62. * Drop the reference count of the class_device embedded in the
  63. * framebuffer info structure.
  64. *
  65. */
  66. void framebuffer_release(struct fb_info *info)
  67. {
  68. kfree(info);
  69. }
  70. EXPORT_SYMBOL(framebuffer_release);
  71. static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
  72. {
  73. int err;
  74. var->activate |= FB_ACTIVATE_FORCE;
  75. acquire_console_sem();
  76. fb_info->flags |= FBINFO_MISC_USEREVENT;
  77. err = fb_set_var(fb_info, var);
  78. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  79. release_console_sem();
  80. if (err)
  81. return err;
  82. return 0;
  83. }
  84. static int mode_string(char *buf, unsigned int offset,
  85. const struct fb_videomode *mode)
  86. {
  87. char m = 'U';
  88. char v = 'p';
  89. if (mode->flag & FB_MODE_IS_DETAILED)
  90. m = 'D';
  91. if (mode->flag & FB_MODE_IS_VESA)
  92. m = 'V';
  93. if (mode->flag & FB_MODE_IS_STANDARD)
  94. m = 'S';
  95. if (mode->vmode & FB_VMODE_INTERLACED)
  96. v = 'i';
  97. if (mode->vmode & FB_VMODE_DOUBLE)
  98. v = 'd';
  99. return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
  100. m, mode->xres, mode->yres, v, mode->refresh);
  101. }
  102. static ssize_t store_mode(struct class_device *class_device, const char * buf,
  103. size_t count)
  104. {
  105. struct fb_info *fb_info = class_get_devdata(class_device);
  106. char mstr[100];
  107. struct fb_var_screeninfo var;
  108. struct fb_modelist *modelist;
  109. struct fb_videomode *mode;
  110. struct list_head *pos;
  111. size_t i;
  112. int err;
  113. memset(&var, 0, sizeof(var));
  114. list_for_each(pos, &fb_info->modelist) {
  115. modelist = list_entry(pos, struct fb_modelist, list);
  116. mode = &modelist->mode;
  117. i = mode_string(mstr, 0, mode);
  118. if (strncmp(mstr, buf, max(count, i)) == 0) {
  119. var = fb_info->var;
  120. fb_videomode_to_var(&var, mode);
  121. if ((err = activate(fb_info, &var)))
  122. return err;
  123. fb_info->mode = mode;
  124. return count;
  125. }
  126. }
  127. return -EINVAL;
  128. }
  129. static ssize_t show_mode(struct class_device *class_device, char *buf)
  130. {
  131. struct fb_info *fb_info = class_get_devdata(class_device);
  132. if (!fb_info->mode)
  133. return 0;
  134. return mode_string(buf, 0, fb_info->mode);
  135. }
  136. static ssize_t store_modes(struct class_device *class_device, const char * buf,
  137. size_t count)
  138. {
  139. struct fb_info *fb_info = class_get_devdata(class_device);
  140. LIST_HEAD(old_list);
  141. int i = count / sizeof(struct fb_videomode);
  142. if (i * sizeof(struct fb_videomode) != count)
  143. return -EINVAL;
  144. acquire_console_sem();
  145. list_splice(&fb_info->modelist, &old_list);
  146. fb_videomode_to_modelist((struct fb_videomode *)buf, i,
  147. &fb_info->modelist);
  148. if (fb_new_modelist(fb_info)) {
  149. fb_destroy_modelist(&fb_info->modelist);
  150. list_splice(&old_list, &fb_info->modelist);
  151. } else
  152. fb_destroy_modelist(&old_list);
  153. release_console_sem();
  154. return 0;
  155. }
  156. static ssize_t show_modes(struct class_device *class_device, char *buf)
  157. {
  158. struct fb_info *fb_info = class_get_devdata(class_device);
  159. unsigned int i;
  160. struct list_head *pos;
  161. struct fb_modelist *modelist;
  162. const struct fb_videomode *mode;
  163. i = 0;
  164. list_for_each(pos, &fb_info->modelist) {
  165. modelist = list_entry(pos, struct fb_modelist, list);
  166. mode = &modelist->mode;
  167. i += mode_string(buf, i, mode);
  168. }
  169. return i;
  170. }
  171. static ssize_t store_bpp(struct class_device *class_device, const char * buf,
  172. size_t count)
  173. {
  174. struct fb_info *fb_info = class_get_devdata(class_device);
  175. struct fb_var_screeninfo var;
  176. char ** last = NULL;
  177. int err;
  178. var = fb_info->var;
  179. var.bits_per_pixel = simple_strtoul(buf, last, 0);
  180. if ((err = activate(fb_info, &var)))
  181. return err;
  182. return count;
  183. }
  184. static ssize_t show_bpp(struct class_device *class_device, char *buf)
  185. {
  186. struct fb_info *fb_info = class_get_devdata(class_device);
  187. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
  188. }
  189. static ssize_t store_rotate(struct class_device *class_device, const char *buf,
  190. size_t count)
  191. {
  192. struct fb_info *fb_info = class_get_devdata(class_device);
  193. struct fb_var_screeninfo var;
  194. char **last = NULL;
  195. int err;
  196. var = fb_info->var;
  197. var.rotate = simple_strtoul(buf, last, 0);
  198. if ((err = activate(fb_info, &var)))
  199. return err;
  200. return count;
  201. }
  202. static ssize_t show_rotate(struct class_device *class_device, char *buf)
  203. {
  204. struct fb_info *fb_info = class_get_devdata(class_device);
  205. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
  206. }
  207. static ssize_t store_virtual(struct class_device *class_device,
  208. const char * buf, size_t count)
  209. {
  210. struct fb_info *fb_info = class_get_devdata(class_device);
  211. struct fb_var_screeninfo var;
  212. char *last = NULL;
  213. int err;
  214. var = fb_info->var;
  215. var.xres_virtual = simple_strtoul(buf, &last, 0);
  216. last++;
  217. if (last - buf >= count)
  218. return -EINVAL;
  219. var.yres_virtual = simple_strtoul(last, &last, 0);
  220. if ((err = activate(fb_info, &var)))
  221. return err;
  222. return count;
  223. }
  224. static ssize_t show_virtual(struct class_device *class_device, char *buf)
  225. {
  226. struct fb_info *fb_info = class_get_devdata(class_device);
  227. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
  228. fb_info->var.yres_virtual);
  229. }
  230. static ssize_t show_stride(struct class_device *class_device, char *buf)
  231. {
  232. struct fb_info *fb_info = class_get_devdata(class_device);
  233. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
  234. }
  235. static ssize_t store_blank(struct class_device *class_device, const char * buf,
  236. size_t count)
  237. {
  238. struct fb_info *fb_info = class_get_devdata(class_device);
  239. char *last = NULL;
  240. int err;
  241. acquire_console_sem();
  242. fb_info->flags |= FBINFO_MISC_USEREVENT;
  243. err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
  244. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  245. release_console_sem();
  246. if (err < 0)
  247. return err;
  248. return count;
  249. }
  250. static ssize_t show_blank(struct class_device *class_device, char *buf)
  251. {
  252. // struct fb_info *fb_info = class_get_devdata(class_device);
  253. return 0;
  254. }
  255. static ssize_t store_console(struct class_device *class_device,
  256. const char * buf, size_t count)
  257. {
  258. // struct fb_info *fb_info = class_get_devdata(class_device);
  259. return 0;
  260. }
  261. static ssize_t show_console(struct class_device *class_device, char *buf)
  262. {
  263. // struct fb_info *fb_info = class_get_devdata(class_device);
  264. return 0;
  265. }
  266. static ssize_t store_cursor(struct class_device *class_device,
  267. const char * buf, size_t count)
  268. {
  269. // struct fb_info *fb_info = class_get_devdata(class_device);
  270. return 0;
  271. }
  272. static ssize_t show_cursor(struct class_device *class_device, char *buf)
  273. {
  274. // struct fb_info *fb_info = class_get_devdata(class_device);
  275. return 0;
  276. }
  277. static ssize_t store_pan(struct class_device *class_device, const char * buf,
  278. size_t count)
  279. {
  280. struct fb_info *fb_info = class_get_devdata(class_device);
  281. struct fb_var_screeninfo var;
  282. char *last = NULL;
  283. int err;
  284. var = fb_info->var;
  285. var.xoffset = simple_strtoul(buf, &last, 0);
  286. last++;
  287. if (last - buf >= count)
  288. return -EINVAL;
  289. var.yoffset = simple_strtoul(last, &last, 0);
  290. acquire_console_sem();
  291. err = fb_pan_display(fb_info, &var);
  292. release_console_sem();
  293. if (err < 0)
  294. return err;
  295. return count;
  296. }
  297. static ssize_t show_pan(struct class_device *class_device, char *buf)
  298. {
  299. struct fb_info *fb_info = class_get_devdata(class_device);
  300. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
  301. fb_info->var.xoffset);
  302. }
  303. static ssize_t show_name(struct class_device *class_device, char *buf)
  304. {
  305. struct fb_info *fb_info = class_get_devdata(class_device);
  306. return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
  307. }
  308. static ssize_t store_fbstate(struct class_device *class_device,
  309. const char *buf, size_t count)
  310. {
  311. struct fb_info *fb_info = class_get_devdata(class_device);
  312. u32 state;
  313. char *last = NULL;
  314. state = simple_strtoul(buf, &last, 0);
  315. acquire_console_sem();
  316. fb_set_suspend(fb_info, (int)state);
  317. release_console_sem();
  318. return count;
  319. }
  320. static ssize_t show_fbstate(struct class_device *class_device, char *buf)
  321. {
  322. struct fb_info *fb_info = class_get_devdata(class_device);
  323. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
  324. }
  325. #ifdef CONFIG_FB_BACKLIGHT
  326. static ssize_t store_bl_curve(struct class_device *class_device,
  327. const char *buf, size_t count)
  328. {
  329. struct fb_info *fb_info = class_get_devdata(class_device);
  330. u8 tmp_curve[FB_BACKLIGHT_LEVELS];
  331. unsigned int i;
  332. /* Some drivers don't use framebuffer_alloc(), but those also
  333. * don't have backlights.
  334. */
  335. if (!fb_info || !fb_info->bl_dev)
  336. return -ENODEV;
  337. if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
  338. return -EINVAL;
  339. for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
  340. if (sscanf(&buf[i * 24],
  341. "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
  342. &tmp_curve[i * 8 + 0],
  343. &tmp_curve[i * 8 + 1],
  344. &tmp_curve[i * 8 + 2],
  345. &tmp_curve[i * 8 + 3],
  346. &tmp_curve[i * 8 + 4],
  347. &tmp_curve[i * 8 + 5],
  348. &tmp_curve[i * 8 + 6],
  349. &tmp_curve[i * 8 + 7]) != 8)
  350. return -EINVAL;
  351. /* If there has been an error in the input data, we won't
  352. * reach this loop.
  353. */
  354. mutex_lock(&fb_info->bl_mutex);
  355. for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
  356. fb_info->bl_curve[i] = tmp_curve[i];
  357. mutex_unlock(&fb_info->bl_mutex);
  358. return count;
  359. }
  360. static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
  361. {
  362. struct fb_info *fb_info = class_get_devdata(class_device);
  363. ssize_t len = 0;
  364. unsigned int i;
  365. /* Some drivers don't use framebuffer_alloc(), but those also
  366. * don't have backlights.
  367. */
  368. if (!fb_info || !fb_info->bl_dev)
  369. return -ENODEV;
  370. mutex_lock(&fb_info->bl_mutex);
  371. for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
  372. len += snprintf(&buf[len], PAGE_SIZE,
  373. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  374. fb_info->bl_curve[i + 0],
  375. fb_info->bl_curve[i + 1],
  376. fb_info->bl_curve[i + 2],
  377. fb_info->bl_curve[i + 3],
  378. fb_info->bl_curve[i + 4],
  379. fb_info->bl_curve[i + 5],
  380. fb_info->bl_curve[i + 6],
  381. fb_info->bl_curve[i + 7]);
  382. mutex_unlock(&fb_info->bl_mutex);
  383. return len;
  384. }
  385. #endif
  386. /* When cmap is added back in it should be a binary attribute
  387. * not a text one. Consideration should also be given to converting
  388. * fbdev to use configfs instead of sysfs */
  389. static struct class_device_attribute class_device_attrs[] = {
  390. __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
  391. __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
  392. __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
  393. __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
  394. __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
  395. __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
  396. __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
  397. __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
  398. __ATTR(name, S_IRUGO, show_name, NULL),
  399. __ATTR(stride, S_IRUGO, show_stride, NULL),
  400. __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
  401. __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
  402. #ifdef CONFIG_FB_BACKLIGHT
  403. __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
  404. #endif
  405. };
  406. int fb_init_class_device(struct fb_info *fb_info)
  407. {
  408. unsigned int i;
  409. class_set_devdata(fb_info->class_device, fb_info);
  410. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  411. class_device_create_file(fb_info->class_device,
  412. &class_device_attrs[i]);
  413. return 0;
  414. }
  415. void fb_cleanup_class_device(struct fb_info *fb_info)
  416. {
  417. unsigned int i;
  418. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  419. class_device_remove_file(fb_info->class_device,
  420. &class_device_attrs[i]);
  421. }
  422. #ifdef CONFIG_FB_BACKLIGHT
  423. /* This function generates a linear backlight curve
  424. *
  425. * 0: off
  426. * 1-7: min
  427. * 8-127: linear from min to max
  428. */
  429. void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
  430. {
  431. unsigned int i, flat, count, range = (max - min);
  432. fb_info->bl_curve[0] = off;
  433. for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
  434. fb_info->bl_curve[flat] = min;
  435. count = FB_BACKLIGHT_LEVELS * 15 / 16;
  436. for (i = 0; i < count; ++i)
  437. fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
  438. }
  439. EXPORT_SYMBOL_GPL(fb_bl_default_curve);
  440. #endif