fbsysfs.c 14 KB

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