fbsysfs.c 14 KB

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