fbsysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. 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. return 0;
  162. }
  163. static ssize_t show_modes(struct device *device, struct device_attribute *attr,
  164. char *buf)
  165. {
  166. struct fb_info *fb_info = dev_get_drvdata(device);
  167. unsigned int i;
  168. struct list_head *pos;
  169. struct fb_modelist *modelist;
  170. const struct fb_videomode *mode;
  171. i = 0;
  172. list_for_each(pos, &fb_info->modelist) {
  173. modelist = list_entry(pos, struct fb_modelist, list);
  174. mode = &modelist->mode;
  175. i += mode_string(buf, i, mode);
  176. }
  177. return i;
  178. }
  179. static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
  180. const char *buf, size_t count)
  181. {
  182. struct fb_info *fb_info = dev_get_drvdata(device);
  183. struct fb_var_screeninfo var;
  184. char ** last = NULL;
  185. int err;
  186. var = fb_info->var;
  187. var.bits_per_pixel = simple_strtoul(buf, last, 0);
  188. if ((err = activate(fb_info, &var)))
  189. return err;
  190. return count;
  191. }
  192. static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. struct fb_info *fb_info = dev_get_drvdata(device);
  196. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
  197. }
  198. static ssize_t store_rotate(struct device *device,
  199. struct device_attribute *attr,
  200. const char *buf, size_t count)
  201. {
  202. struct fb_info *fb_info = dev_get_drvdata(device);
  203. struct fb_var_screeninfo var;
  204. char **last = NULL;
  205. int err;
  206. var = fb_info->var;
  207. var.rotate = simple_strtoul(buf, last, 0);
  208. if ((err = activate(fb_info, &var)))
  209. return err;
  210. return count;
  211. }
  212. static ssize_t show_rotate(struct device *device,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. struct fb_info *fb_info = dev_get_drvdata(device);
  216. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
  217. }
  218. static ssize_t store_virtual(struct device *device,
  219. struct device_attribute *attr,
  220. const char *buf, size_t count)
  221. {
  222. struct fb_info *fb_info = dev_get_drvdata(device);
  223. struct fb_var_screeninfo var;
  224. char *last = NULL;
  225. int err;
  226. var = fb_info->var;
  227. var.xres_virtual = simple_strtoul(buf, &last, 0);
  228. last++;
  229. if (last - buf >= count)
  230. return -EINVAL;
  231. var.yres_virtual = simple_strtoul(last, &last, 0);
  232. if ((err = activate(fb_info, &var)))
  233. return err;
  234. return count;
  235. }
  236. static ssize_t show_virtual(struct device *device,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. struct fb_info *fb_info = dev_get_drvdata(device);
  240. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
  241. fb_info->var.yres_virtual);
  242. }
  243. static ssize_t show_stride(struct device *device,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. struct fb_info *fb_info = dev_get_drvdata(device);
  247. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
  248. }
  249. static ssize_t store_blank(struct device *device,
  250. struct device_attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. struct fb_info *fb_info = dev_get_drvdata(device);
  254. char *last = NULL;
  255. int err;
  256. console_lock();
  257. fb_info->flags |= FBINFO_MISC_USEREVENT;
  258. err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
  259. fb_info->flags &= ~FBINFO_MISC_USEREVENT;
  260. console_unlock();
  261. if (err < 0)
  262. return err;
  263. return count;
  264. }
  265. static ssize_t show_blank(struct device *device,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. // struct fb_info *fb_info = dev_get_drvdata(device);
  269. return 0;
  270. }
  271. static ssize_t store_console(struct device *device,
  272. struct device_attribute *attr,
  273. const char *buf, size_t count)
  274. {
  275. // struct fb_info *fb_info = dev_get_drvdata(device);
  276. return 0;
  277. }
  278. static ssize_t show_console(struct device *device,
  279. struct device_attribute *attr, char *buf)
  280. {
  281. // struct fb_info *fb_info = dev_get_drvdata(device);
  282. return 0;
  283. }
  284. static ssize_t store_cursor(struct device *device,
  285. struct device_attribute *attr,
  286. const char *buf, size_t count)
  287. {
  288. // struct fb_info *fb_info = dev_get_drvdata(device);
  289. return 0;
  290. }
  291. static ssize_t show_cursor(struct device *device,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. // struct fb_info *fb_info = dev_get_drvdata(device);
  295. return 0;
  296. }
  297. static ssize_t store_pan(struct device *device,
  298. struct device_attribute *attr,
  299. const char *buf, size_t count)
  300. {
  301. struct fb_info *fb_info = dev_get_drvdata(device);
  302. struct fb_var_screeninfo var;
  303. char *last = NULL;
  304. int err;
  305. var = fb_info->var;
  306. var.xoffset = simple_strtoul(buf, &last, 0);
  307. last++;
  308. if (last - buf >= count)
  309. return -EINVAL;
  310. var.yoffset = simple_strtoul(last, &last, 0);
  311. console_lock();
  312. err = fb_pan_display(fb_info, &var);
  313. console_unlock();
  314. if (err < 0)
  315. return err;
  316. return count;
  317. }
  318. static ssize_t show_pan(struct device *device,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. struct fb_info *fb_info = dev_get_drvdata(device);
  322. return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
  323. fb_info->var.yoffset);
  324. }
  325. static ssize_t show_name(struct device *device,
  326. struct device_attribute *attr, char *buf)
  327. {
  328. struct fb_info *fb_info = dev_get_drvdata(device);
  329. return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
  330. }
  331. static ssize_t store_fbstate(struct device *device,
  332. struct device_attribute *attr,
  333. const char *buf, size_t count)
  334. {
  335. struct fb_info *fb_info = dev_get_drvdata(device);
  336. u32 state;
  337. char *last = NULL;
  338. state = simple_strtoul(buf, &last, 0);
  339. if (!lock_fb_info(fb_info))
  340. return -ENODEV;
  341. console_lock();
  342. fb_set_suspend(fb_info, (int)state);
  343. console_unlock();
  344. unlock_fb_info(fb_info);
  345. return count;
  346. }
  347. static ssize_t show_fbstate(struct device *device,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. struct fb_info *fb_info = dev_get_drvdata(device);
  351. return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
  352. }
  353. #ifdef CONFIG_FB_BACKLIGHT
  354. static ssize_t store_bl_curve(struct device *device,
  355. struct device_attribute *attr,
  356. const char *buf, size_t count)
  357. {
  358. struct fb_info *fb_info = dev_get_drvdata(device);
  359. u8 tmp_curve[FB_BACKLIGHT_LEVELS];
  360. unsigned int i;
  361. /* Some drivers don't use framebuffer_alloc(), but those also
  362. * don't have backlights.
  363. */
  364. if (!fb_info || !fb_info->bl_dev)
  365. return -ENODEV;
  366. if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
  367. return -EINVAL;
  368. for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
  369. if (sscanf(&buf[i * 24],
  370. "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
  371. &tmp_curve[i * 8 + 0],
  372. &tmp_curve[i * 8 + 1],
  373. &tmp_curve[i * 8 + 2],
  374. &tmp_curve[i * 8 + 3],
  375. &tmp_curve[i * 8 + 4],
  376. &tmp_curve[i * 8 + 5],
  377. &tmp_curve[i * 8 + 6],
  378. &tmp_curve[i * 8 + 7]) != 8)
  379. return -EINVAL;
  380. /* If there has been an error in the input data, we won't
  381. * reach this loop.
  382. */
  383. mutex_lock(&fb_info->bl_curve_mutex);
  384. for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
  385. fb_info->bl_curve[i] = tmp_curve[i];
  386. mutex_unlock(&fb_info->bl_curve_mutex);
  387. return count;
  388. }
  389. static ssize_t show_bl_curve(struct device *device,
  390. struct device_attribute *attr, char *buf)
  391. {
  392. struct fb_info *fb_info = dev_get_drvdata(device);
  393. ssize_t len = 0;
  394. unsigned int i;
  395. /* Some drivers don't use framebuffer_alloc(), but those also
  396. * don't have backlights.
  397. */
  398. if (!fb_info || !fb_info->bl_dev)
  399. return -ENODEV;
  400. mutex_lock(&fb_info->bl_curve_mutex);
  401. for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
  402. len += snprintf(&buf[len], PAGE_SIZE,
  403. "%02x %02x %02x %02x %02x %02x %02x %02x\n",
  404. fb_info->bl_curve[i + 0],
  405. fb_info->bl_curve[i + 1],
  406. fb_info->bl_curve[i + 2],
  407. fb_info->bl_curve[i + 3],
  408. fb_info->bl_curve[i + 4],
  409. fb_info->bl_curve[i + 5],
  410. fb_info->bl_curve[i + 6],
  411. fb_info->bl_curve[i + 7]);
  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