fbsysfs.c 14 KB

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