fbsysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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_con_rotate(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. int rotate;
  212. char **last = NULL;
  213. acquire_console_sem();
  214. rotate = simple_strtoul(buf, last, 0);
  215. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
  216. release_console_sem();
  217. return count;
  218. }
  219. static ssize_t store_con_rotate_all(struct class_device *class_device,
  220. const char *buf, size_t count)
  221. {
  222. struct fb_info *fb_info = class_get_devdata(class_device);
  223. int rotate;
  224. char **last = NULL;
  225. acquire_console_sem();
  226. rotate = simple_strtoul(buf, last, 0);
  227. fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
  228. release_console_sem();
  229. return count;
  230. }
  231. static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
  232. {
  233. struct fb_info *fb_info = class_get_devdata(class_device);
  234. int rotate;
  235. acquire_console_sem();
  236. rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
  237. release_console_sem();
  238. return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
  239. }
  240. static ssize_t store_virtual(struct class_device *class_device,
  241. const char * buf, size_t count)
  242. {
  243. struct fb_info *fb_info = class_get_devdata(class_device);
  244. struct fb_var_screeninfo var;
  245. char *last = NULL;
  246. int err;
  247. var = fb_info->var;
  248. var.xres_virtual = simple_strtoul(buf, &last, 0);
  249. last++;
  250. if (last - buf >= count)
  251. return -EINVAL;
  252. var.yres_virtual = simple_strtoul(last, &last, 0);
  253. if ((err = activate(fb_info, &var)))
  254. return err;
  255. return count;
  256. }
  257. static ssize_t show_virtual(struct class_device *class_device, char *buf)
  258. {
  259. struct fb_info *fb_info = class_get_devdata(class_device);
  260. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
  261. fb_info->var.yres_virtual);
  262. }
  263. static ssize_t show_stride(struct class_device *class_device, char *buf)
  264. {
  265. struct fb_info *fb_info = class_get_devdata(class_device);
  266. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
  267. }
  268. static ssize_t store_blank(struct class_device *class_device, const char * buf,
  269. size_t count)
  270. {
  271. struct fb_info *fb_info = class_get_devdata(class_device);
  272. char *last = NULL;
  273. int err;
  274. acquire_console_sem();
  275. fb_info->flags |= FBINFO_MISC_USEREVENT;
  276. err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
  277. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  278. release_console_sem();
  279. if (err < 0)
  280. return err;
  281. return count;
  282. }
  283. static ssize_t show_blank(struct class_device *class_device, char *buf)
  284. {
  285. // struct fb_info *fb_info = class_get_devdata(class_device);
  286. return 0;
  287. }
  288. static ssize_t store_console(struct class_device *class_device,
  289. const char * buf, size_t count)
  290. {
  291. // struct fb_info *fb_info = class_get_devdata(class_device);
  292. return 0;
  293. }
  294. static ssize_t show_console(struct class_device *class_device, char *buf)
  295. {
  296. // struct fb_info *fb_info = class_get_devdata(class_device);
  297. return 0;
  298. }
  299. static ssize_t store_cursor(struct class_device *class_device,
  300. const char * buf, size_t count)
  301. {
  302. // struct fb_info *fb_info = class_get_devdata(class_device);
  303. return 0;
  304. }
  305. static ssize_t show_cursor(struct class_device *class_device, char *buf)
  306. {
  307. // struct fb_info *fb_info = class_get_devdata(class_device);
  308. return 0;
  309. }
  310. static ssize_t store_pan(struct class_device *class_device, const char * buf,
  311. size_t count)
  312. {
  313. struct fb_info *fb_info = class_get_devdata(class_device);
  314. struct fb_var_screeninfo var;
  315. char *last = NULL;
  316. int err;
  317. var = fb_info->var;
  318. var.xoffset = simple_strtoul(buf, &last, 0);
  319. last++;
  320. if (last - buf >= count)
  321. return -EINVAL;
  322. var.yoffset = simple_strtoul(last, &last, 0);
  323. acquire_console_sem();
  324. err = fb_pan_display(fb_info, &var);
  325. release_console_sem();
  326. if (err < 0)
  327. return err;
  328. return count;
  329. }
  330. static ssize_t show_pan(struct class_device *class_device, char *buf)
  331. {
  332. struct fb_info *fb_info = class_get_devdata(class_device);
  333. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
  334. fb_info->var.xoffset);
  335. }
  336. static ssize_t show_name(struct class_device *class_device, char *buf)
  337. {
  338. struct fb_info *fb_info = class_get_devdata(class_device);
  339. return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
  340. }
  341. static ssize_t store_fbstate(struct class_device *class_device,
  342. const char *buf, size_t count)
  343. {
  344. struct fb_info *fb_info = class_get_devdata(class_device);
  345. u32 state;
  346. char *last = NULL;
  347. state = simple_strtoul(buf, &last, 0);
  348. acquire_console_sem();
  349. fb_set_suspend(fb_info, (int)state);
  350. release_console_sem();
  351. return count;
  352. }
  353. static ssize_t show_fbstate(struct class_device *class_device, char *buf)
  354. {
  355. struct fb_info *fb_info = class_get_devdata(class_device);
  356. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
  357. }
  358. #ifdef CONFIG_FB_BACKLIGHT
  359. static ssize_t store_bl_curve(struct class_device *class_device,
  360. const char *buf, size_t count)
  361. {
  362. struct fb_info *fb_info = class_get_devdata(class_device);
  363. u8 tmp_curve[FB_BACKLIGHT_LEVELS];
  364. unsigned int i;
  365. if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
  366. return -EINVAL;
  367. for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
  368. if (sscanf(&buf[i * 24],
  369. "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
  370. &tmp_curve[i * 8 + 0],
  371. &tmp_curve[i * 8 + 1],
  372. &tmp_curve[i * 8 + 2],
  373. &tmp_curve[i * 8 + 3],
  374. &tmp_curve[i * 8 + 4],
  375. &tmp_curve[i * 8 + 5],
  376. &tmp_curve[i * 8 + 6],
  377. &tmp_curve[i * 8 + 7]) != 8)
  378. return -EINVAL;
  379. /* If there has been an error in the input data, we won't
  380. * reach this loop.
  381. */
  382. mutex_lock(&fb_info->bl_mutex);
  383. for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
  384. fb_info->bl_curve[i] = tmp_curve[i];
  385. mutex_unlock(&fb_info->bl_mutex);
  386. return count;
  387. }
  388. static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
  389. {
  390. struct fb_info *fb_info = class_get_devdata(class_device);
  391. ssize_t len = 0;
  392. unsigned int i;
  393. mutex_lock(&fb_info->bl_mutex);
  394. for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
  395. len += snprintf(&buf[len], PAGE_SIZE,
  396. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  397. fb_info->bl_curve[i + 0],
  398. fb_info->bl_curve[i + 1],
  399. fb_info->bl_curve[i + 2],
  400. fb_info->bl_curve[i + 3],
  401. fb_info->bl_curve[i + 4],
  402. fb_info->bl_curve[i + 5],
  403. fb_info->bl_curve[i + 6],
  404. fb_info->bl_curve[i + 7]);
  405. mutex_unlock(&fb_info->bl_mutex);
  406. return len;
  407. }
  408. #endif
  409. /* When cmap is added back in it should be a binary attribute
  410. * not a text one. Consideration should also be given to converting
  411. * fbdev to use configfs instead of sysfs */
  412. static struct class_device_attribute class_device_attrs[] = {
  413. __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
  414. __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
  415. __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
  416. __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
  417. __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
  418. __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
  419. __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
  420. __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
  421. __ATTR(name, S_IRUGO, show_name, NULL),
  422. __ATTR(stride, S_IRUGO, show_stride, NULL),
  423. __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
  424. __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
  425. __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
  426. __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
  427. #ifdef CONFIG_FB_BACKLIGHT
  428. __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
  429. #endif
  430. };
  431. int fb_init_class_device(struct fb_info *fb_info)
  432. {
  433. unsigned int i;
  434. class_set_devdata(fb_info->class_device, fb_info);
  435. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  436. class_device_create_file(fb_info->class_device,
  437. &class_device_attrs[i]);
  438. return 0;
  439. }
  440. void fb_cleanup_class_device(struct fb_info *fb_info)
  441. {
  442. unsigned int i;
  443. for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
  444. class_device_remove_file(fb_info->class_device,
  445. &class_device_attrs[i]);
  446. }
  447. #ifdef CONFIG_FB_BACKLIGHT
  448. /* This function generates a linear backlight curve
  449. *
  450. * 0: off
  451. * 1-7: min
  452. * 8-127: linear from min to max
  453. */
  454. void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
  455. {
  456. unsigned int i, flat, count, range = (max - min);
  457. fb_info->bl_curve[0] = off;
  458. for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
  459. fb_info->bl_curve[flat] = min;
  460. count = FB_BACKLIGHT_LEVELS * 15 / 16;
  461. for (i = 0; i < count; ++i)
  462. fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
  463. }
  464. EXPORT_SYMBOL_GPL(fb_bl_default_curve);
  465. #endif