2 * fbsysfs.c - framebuffer device class and attributes
4 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
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.
13 * Note: currently there's only stubs for framebuffer_alloc and
14 * framebuffer_release here. The reson for that is that until all drivers
15 * are converted to use it a sysfsification will open OOPSable races.
18 #include <linux/kernel.h>
20 #include <linux/console.h>
21 #include <linux/module.h>
23 #define FB_SYSFS_FLAG_ATTR 1
26 * framebuffer_alloc - creates a new frame buffer info structure
28 * @size: size of driver private data, can be zero
29 * @dev: pointer to the device for this fb, this can be NULL
31 * Creates a new frame buffer info structure. Also reserves @size bytes
32 * for driver private data (info->par). info->par (if any) will be
33 * aligned to sizeof(long).
35 * Returns the new structure, or NULL if an error occured.
38 struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
40 #define BYTES_PER_LONG (BITS_PER_LONG/8)
41 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
42 int fb_info_size = sizeof(struct fb_info);
47 fb_info_size += PADDING;
49 p = kzalloc(fb_info_size + size, GFP_KERNEL);
54 info = (struct fb_info *) p;
57 info->par = p + fb_info_size;
61 #ifdef CONFIG_FB_BACKLIGHT
62 mutex_init(&info->bl_mutex);
69 EXPORT_SYMBOL(framebuffer_alloc);
72 * framebuffer_release - marks the structure available for freeing
74 * @info: frame buffer info structure
76 * Drop the reference count of the class_device embedded in the
77 * framebuffer info structure.
80 void framebuffer_release(struct fb_info *info)
84 EXPORT_SYMBOL(framebuffer_release);
86 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
90 var->activate |= FB_ACTIVATE_FORCE;
91 acquire_console_sem();
92 fb_info->flags |= FBINFO_MISC_USEREVENT;
93 err = fb_set_var(fb_info, var);
94 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
95 release_console_sem();
101 static int mode_string(char *buf, unsigned int offset,
102 const struct fb_videomode *mode)
107 if (mode->flag & FB_MODE_IS_DETAILED)
109 if (mode->flag & FB_MODE_IS_VESA)
111 if (mode->flag & FB_MODE_IS_STANDARD)
114 if (mode->vmode & FB_VMODE_INTERLACED)
116 if (mode->vmode & FB_VMODE_DOUBLE)
119 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
120 m, mode->xres, mode->yres, v, mode->refresh);
123 static ssize_t store_mode(struct class_device *class_device, const char * buf,
126 struct fb_info *fb_info = class_get_devdata(class_device);
128 struct fb_var_screeninfo var;
129 struct fb_modelist *modelist;
130 struct fb_videomode *mode;
131 struct list_head *pos;
135 memset(&var, 0, sizeof(var));
137 list_for_each(pos, &fb_info->modelist) {
138 modelist = list_entry(pos, struct fb_modelist, list);
139 mode = &modelist->mode;
140 i = mode_string(mstr, 0, mode);
141 if (strncmp(mstr, buf, max(count, i)) == 0) {
144 fb_videomode_to_var(&var, mode);
145 if ((err = activate(fb_info, &var)))
147 fb_info->mode = mode;
154 static ssize_t show_mode(struct class_device *class_device, char *buf)
156 struct fb_info *fb_info = class_get_devdata(class_device);
161 return mode_string(buf, 0, fb_info->mode);
164 static ssize_t store_modes(struct class_device *class_device, const char * buf,
167 struct fb_info *fb_info = class_get_devdata(class_device);
169 int i = count / sizeof(struct fb_videomode);
171 if (i * sizeof(struct fb_videomode) != count)
174 acquire_console_sem();
175 list_splice(&fb_info->modelist, &old_list);
176 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
178 if (fb_new_modelist(fb_info)) {
179 fb_destroy_modelist(&fb_info->modelist);
180 list_splice(&old_list, &fb_info->modelist);
182 fb_destroy_modelist(&old_list);
184 release_console_sem();
189 static ssize_t show_modes(struct class_device *class_device, char *buf)
191 struct fb_info *fb_info = class_get_devdata(class_device);
193 struct list_head *pos;
194 struct fb_modelist *modelist;
195 const struct fb_videomode *mode;
198 list_for_each(pos, &fb_info->modelist) {
199 modelist = list_entry(pos, struct fb_modelist, list);
200 mode = &modelist->mode;
201 i += mode_string(buf, i, mode);
206 static ssize_t store_bpp(struct class_device *class_device, const char * buf,
209 struct fb_info *fb_info = class_get_devdata(class_device);
210 struct fb_var_screeninfo var;
215 var.bits_per_pixel = simple_strtoul(buf, last, 0);
216 if ((err = activate(fb_info, &var)))
221 static ssize_t show_bpp(struct class_device *class_device, char *buf)
223 struct fb_info *fb_info = class_get_devdata(class_device);
224 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
227 static ssize_t store_rotate(struct class_device *class_device, const char *buf,
230 struct fb_info *fb_info = class_get_devdata(class_device);
231 struct fb_var_screeninfo var;
236 var.rotate = simple_strtoul(buf, last, 0);
238 if ((err = activate(fb_info, &var)))
245 static ssize_t show_rotate(struct class_device *class_device, char *buf)
247 struct fb_info *fb_info = class_get_devdata(class_device);
249 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
252 static ssize_t store_virtual(struct class_device *class_device,
253 const char * buf, size_t count)
255 struct fb_info *fb_info = class_get_devdata(class_device);
256 struct fb_var_screeninfo var;
261 var.xres_virtual = simple_strtoul(buf, &last, 0);
263 if (last - buf >= count)
265 var.yres_virtual = simple_strtoul(last, &last, 0);
267 if ((err = activate(fb_info, &var)))
272 static ssize_t show_virtual(struct class_device *class_device, char *buf)
274 struct fb_info *fb_info = class_get_devdata(class_device);
275 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
276 fb_info->var.yres_virtual);
279 static ssize_t show_stride(struct class_device *class_device, char *buf)
281 struct fb_info *fb_info = class_get_devdata(class_device);
282 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
285 static ssize_t store_blank(struct class_device *class_device, const char * buf,
288 struct fb_info *fb_info = class_get_devdata(class_device);
292 acquire_console_sem();
293 fb_info->flags |= FBINFO_MISC_USEREVENT;
294 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
295 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
296 release_console_sem();
302 static ssize_t show_blank(struct class_device *class_device, char *buf)
304 // struct fb_info *fb_info = class_get_devdata(class_device);
308 static ssize_t store_console(struct class_device *class_device,
309 const char * buf, size_t count)
311 // struct fb_info *fb_info = class_get_devdata(class_device);
315 static ssize_t show_console(struct class_device *class_device, char *buf)
317 // struct fb_info *fb_info = class_get_devdata(class_device);
321 static ssize_t store_cursor(struct class_device *class_device,
322 const char * buf, size_t count)
324 // struct fb_info *fb_info = class_get_devdata(class_device);
328 static ssize_t show_cursor(struct class_device *class_device, char *buf)
330 // struct fb_info *fb_info = class_get_devdata(class_device);
334 static ssize_t store_pan(struct class_device *class_device, const char * buf,
337 struct fb_info *fb_info = class_get_devdata(class_device);
338 struct fb_var_screeninfo var;
343 var.xoffset = simple_strtoul(buf, &last, 0);
345 if (last - buf >= count)
347 var.yoffset = simple_strtoul(last, &last, 0);
349 acquire_console_sem();
350 err = fb_pan_display(fb_info, &var);
351 release_console_sem();
358 static ssize_t show_pan(struct class_device *class_device, char *buf)
360 struct fb_info *fb_info = class_get_devdata(class_device);
361 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
362 fb_info->var.xoffset);
365 static ssize_t show_name(struct class_device *class_device, char *buf)
367 struct fb_info *fb_info = class_get_devdata(class_device);
369 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
372 static ssize_t store_fbstate(struct class_device *class_device,
373 const char *buf, size_t count)
375 struct fb_info *fb_info = class_get_devdata(class_device);
379 state = simple_strtoul(buf, &last, 0);
381 acquire_console_sem();
382 fb_set_suspend(fb_info, (int)state);
383 release_console_sem();
388 static ssize_t show_fbstate(struct class_device *class_device, char *buf)
390 struct fb_info *fb_info = class_get_devdata(class_device);
391 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
394 #ifdef CONFIG_FB_BACKLIGHT
395 static ssize_t store_bl_curve(struct class_device *class_device,
396 const char *buf, size_t count)
398 struct fb_info *fb_info = class_get_devdata(class_device);
399 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
402 /* Some drivers don't use framebuffer_alloc(), but those also
403 * don't have backlights.
405 if (!fb_info || !fb_info->bl_dev)
408 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
411 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
412 if (sscanf(&buf[i * 24],
413 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
414 &tmp_curve[i * 8 + 0],
415 &tmp_curve[i * 8 + 1],
416 &tmp_curve[i * 8 + 2],
417 &tmp_curve[i * 8 + 3],
418 &tmp_curve[i * 8 + 4],
419 &tmp_curve[i * 8 + 5],
420 &tmp_curve[i * 8 + 6],
421 &tmp_curve[i * 8 + 7]) != 8)
424 /* If there has been an error in the input data, we won't
427 mutex_lock(&fb_info->bl_mutex);
428 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
429 fb_info->bl_curve[i] = tmp_curve[i];
430 mutex_unlock(&fb_info->bl_mutex);
435 static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
437 struct fb_info *fb_info = class_get_devdata(class_device);
441 /* Some drivers don't use framebuffer_alloc(), but those also
442 * don't have backlights.
444 if (!fb_info || !fb_info->bl_dev)
447 mutex_lock(&fb_info->bl_mutex);
448 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
449 len += snprintf(&buf[len], PAGE_SIZE,
450 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
451 fb_info->bl_curve[i + 0],
452 fb_info->bl_curve[i + 1],
453 fb_info->bl_curve[i + 2],
454 fb_info->bl_curve[i + 3],
455 fb_info->bl_curve[i + 4],
456 fb_info->bl_curve[i + 5],
457 fb_info->bl_curve[i + 6],
458 fb_info->bl_curve[i + 7]);
459 mutex_unlock(&fb_info->bl_mutex);
465 /* When cmap is added back in it should be a binary attribute
466 * not a text one. Consideration should also be given to converting
467 * fbdev to use configfs instead of sysfs */
468 static struct class_device_attribute class_device_attrs[] = {
469 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
470 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
471 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
472 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
473 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
474 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
475 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
476 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
477 __ATTR(name, S_IRUGO, show_name, NULL),
478 __ATTR(stride, S_IRUGO, show_stride, NULL),
479 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
480 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
481 #ifdef CONFIG_FB_BACKLIGHT
482 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
486 int fb_init_class_device(struct fb_info *fb_info)
490 class_set_devdata(fb_info->class_device, fb_info);
492 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
494 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++) {
495 error = class_device_create_file(fb_info->class_device,
496 &class_device_attrs[i]);
504 class_device_remove_file(fb_info->class_device,
505 &class_device_attrs[i]);
506 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
512 void fb_cleanup_class_device(struct fb_info *fb_info)
516 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
517 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
518 class_device_remove_file(fb_info->class_device,
519 &class_device_attrs[i]);
521 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
525 #ifdef CONFIG_FB_BACKLIGHT
526 /* This function generates a linear backlight curve
530 * 8-127: linear from min to max
532 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
534 unsigned int i, flat, count, range = (max - min);
536 fb_info->bl_curve[0] = off;
538 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
539 fb_info->bl_curve[flat] = min;
541 count = FB_BACKLIGHT_LEVELS * 15 / 16;
542 for (i = 0; i < count; ++i)
543 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
545 EXPORT_SYMBOL_GPL(fb_bl_default_curve);