fbsysfs.c 14 KB

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