fbsysfs.c 14 KB

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