fbsysfs.c 14 KB

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