fbsysfs.c 14 KB

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