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