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_curve_mutex);
65 mutex_init(&info->lock);
66 mutex_init(&info->mm_lock);
72 EXPORT_SYMBOL(framebuffer_alloc);
75 * framebuffer_release - marks the structure available for freeing
77 * @info: frame buffer info structure
79 * Drop the reference count of the device embedded in the
80 * framebuffer info structure.
83 void framebuffer_release(struct fb_info *info)
87 EXPORT_SYMBOL(framebuffer_release);
89 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
93 var->activate |= FB_ACTIVATE_FORCE;
94 acquire_console_sem();
95 fb_info->flags |= FBINFO_MISC_USEREVENT;
96 err = fb_set_var(fb_info, var);
97 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
98 release_console_sem();
104 static int mode_string(char *buf, unsigned int offset,
105 const struct fb_videomode *mode)
110 if (mode->flag & FB_MODE_IS_DETAILED)
112 if (mode->flag & FB_MODE_IS_VESA)
114 if (mode->flag & FB_MODE_IS_STANDARD)
117 if (mode->vmode & FB_VMODE_INTERLACED)
119 if (mode->vmode & FB_VMODE_DOUBLE)
122 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
123 m, mode->xres, mode->yres, v, mode->refresh);
126 static ssize_t store_mode(struct device *device, struct device_attribute *attr,
127 const char *buf, size_t count)
129 struct fb_info *fb_info = dev_get_drvdata(device);
131 struct fb_var_screeninfo var;
132 struct fb_modelist *modelist;
133 struct fb_videomode *mode;
134 struct list_head *pos;
138 memset(&var, 0, sizeof(var));
140 list_for_each(pos, &fb_info->modelist) {
141 modelist = list_entry(pos, struct fb_modelist, list);
142 mode = &modelist->mode;
143 i = mode_string(mstr, 0, mode);
144 if (strncmp(mstr, buf, max(count, i)) == 0) {
147 fb_videomode_to_var(&var, mode);
148 if ((err = activate(fb_info, &var)))
150 fb_info->mode = mode;
157 static ssize_t show_mode(struct device *device, struct device_attribute *attr,
160 struct fb_info *fb_info = dev_get_drvdata(device);
165 return mode_string(buf, 0, fb_info->mode);
168 static ssize_t store_modes(struct device *device,
169 struct device_attribute *attr,
170 const char *buf, size_t count)
172 struct fb_info *fb_info = dev_get_drvdata(device);
174 int i = count / sizeof(struct fb_videomode);
176 if (i * sizeof(struct fb_videomode) != count)
179 acquire_console_sem();
180 list_splice(&fb_info->modelist, &old_list);
181 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
183 if (fb_new_modelist(fb_info)) {
184 fb_destroy_modelist(&fb_info->modelist);
185 list_splice(&old_list, &fb_info->modelist);
187 fb_destroy_modelist(&old_list);
189 release_console_sem();
194 static ssize_t show_modes(struct device *device, struct device_attribute *attr,
197 struct fb_info *fb_info = dev_get_drvdata(device);
199 struct list_head *pos;
200 struct fb_modelist *modelist;
201 const struct fb_videomode *mode;
204 list_for_each(pos, &fb_info->modelist) {
205 modelist = list_entry(pos, struct fb_modelist, list);
206 mode = &modelist->mode;
207 i += mode_string(buf, i, mode);
212 static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
213 const char *buf, size_t count)
215 struct fb_info *fb_info = dev_get_drvdata(device);
216 struct fb_var_screeninfo var;
221 var.bits_per_pixel = simple_strtoul(buf, last, 0);
222 if ((err = activate(fb_info, &var)))
227 static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
230 struct fb_info *fb_info = dev_get_drvdata(device);
231 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
234 static ssize_t store_rotate(struct device *device,
235 struct device_attribute *attr,
236 const char *buf, size_t count)
238 struct fb_info *fb_info = dev_get_drvdata(device);
239 struct fb_var_screeninfo var;
244 var.rotate = simple_strtoul(buf, last, 0);
246 if ((err = activate(fb_info, &var)))
253 static ssize_t show_rotate(struct device *device,
254 struct device_attribute *attr, char *buf)
256 struct fb_info *fb_info = dev_get_drvdata(device);
258 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
261 static ssize_t store_virtual(struct device *device,
262 struct device_attribute *attr,
263 const char *buf, size_t count)
265 struct fb_info *fb_info = dev_get_drvdata(device);
266 struct fb_var_screeninfo var;
271 var.xres_virtual = simple_strtoul(buf, &last, 0);
273 if (last - buf >= count)
275 var.yres_virtual = simple_strtoul(last, &last, 0);
277 if ((err = activate(fb_info, &var)))
282 static ssize_t show_virtual(struct device *device,
283 struct device_attribute *attr, char *buf)
285 struct fb_info *fb_info = dev_get_drvdata(device);
286 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
287 fb_info->var.yres_virtual);
290 static ssize_t show_stride(struct device *device,
291 struct device_attribute *attr, char *buf)
293 struct fb_info *fb_info = dev_get_drvdata(device);
294 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
297 static ssize_t store_blank(struct device *device,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct fb_info *fb_info = dev_get_drvdata(device);
305 acquire_console_sem();
306 fb_info->flags |= FBINFO_MISC_USEREVENT;
307 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
308 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
309 release_console_sem();
315 static ssize_t show_blank(struct device *device,
316 struct device_attribute *attr, char *buf)
318 // struct fb_info *fb_info = dev_get_drvdata(device);
322 static ssize_t store_console(struct device *device,
323 struct device_attribute *attr,
324 const char *buf, size_t count)
326 // struct fb_info *fb_info = dev_get_drvdata(device);
330 static ssize_t show_console(struct device *device,
331 struct device_attribute *attr, char *buf)
333 // struct fb_info *fb_info = dev_get_drvdata(device);
337 static ssize_t store_cursor(struct device *device,
338 struct device_attribute *attr,
339 const char *buf, size_t count)
341 // struct fb_info *fb_info = dev_get_drvdata(device);
345 static ssize_t show_cursor(struct device *device,
346 struct device_attribute *attr, char *buf)
348 // struct fb_info *fb_info = dev_get_drvdata(device);
352 static ssize_t store_pan(struct device *device,
353 struct device_attribute *attr,
354 const char *buf, size_t count)
356 struct fb_info *fb_info = dev_get_drvdata(device);
357 struct fb_var_screeninfo var;
362 var.xoffset = simple_strtoul(buf, &last, 0);
364 if (last - buf >= count)
366 var.yoffset = simple_strtoul(last, &last, 0);
368 acquire_console_sem();
369 err = fb_pan_display(fb_info, &var);
370 release_console_sem();
377 static ssize_t show_pan(struct device *device,
378 struct device_attribute *attr, char *buf)
380 struct fb_info *fb_info = dev_get_drvdata(device);
381 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
382 fb_info->var.yoffset);
385 static ssize_t show_name(struct device *device,
386 struct device_attribute *attr, char *buf)
388 struct fb_info *fb_info = dev_get_drvdata(device);
390 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
393 static ssize_t store_fbstate(struct device *device,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
397 struct fb_info *fb_info = dev_get_drvdata(device);
401 state = simple_strtoul(buf, &last, 0);
403 acquire_console_sem();
404 fb_set_suspend(fb_info, (int)state);
405 release_console_sem();
410 static ssize_t show_fbstate(struct device *device,
411 struct device_attribute *attr, char *buf)
413 struct fb_info *fb_info = dev_get_drvdata(device);
414 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
417 #ifdef CONFIG_FB_BACKLIGHT
418 static ssize_t store_bl_curve(struct device *device,
419 struct device_attribute *attr,
420 const char *buf, size_t count)
422 struct fb_info *fb_info = dev_get_drvdata(device);
423 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
426 /* Some drivers don't use framebuffer_alloc(), but those also
427 * don't have backlights.
429 if (!fb_info || !fb_info->bl_dev)
432 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
435 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
436 if (sscanf(&buf[i * 24],
437 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
438 &tmp_curve[i * 8 + 0],
439 &tmp_curve[i * 8 + 1],
440 &tmp_curve[i * 8 + 2],
441 &tmp_curve[i * 8 + 3],
442 &tmp_curve[i * 8 + 4],
443 &tmp_curve[i * 8 + 5],
444 &tmp_curve[i * 8 + 6],
445 &tmp_curve[i * 8 + 7]) != 8)
448 /* If there has been an error in the input data, we won't
451 mutex_lock(&fb_info->bl_curve_mutex);
452 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
453 fb_info->bl_curve[i] = tmp_curve[i];
454 mutex_unlock(&fb_info->bl_curve_mutex);
459 static ssize_t show_bl_curve(struct device *device,
460 struct device_attribute *attr, char *buf)
462 struct fb_info *fb_info = dev_get_drvdata(device);
466 /* Some drivers don't use framebuffer_alloc(), but those also
467 * don't have backlights.
469 if (!fb_info || !fb_info->bl_dev)
472 mutex_lock(&fb_info->bl_curve_mutex);
473 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
474 len += snprintf(&buf[len], PAGE_SIZE,
475 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
476 fb_info->bl_curve[i + 0],
477 fb_info->bl_curve[i + 1],
478 fb_info->bl_curve[i + 2],
479 fb_info->bl_curve[i + 3],
480 fb_info->bl_curve[i + 4],
481 fb_info->bl_curve[i + 5],
482 fb_info->bl_curve[i + 6],
483 fb_info->bl_curve[i + 7]);
484 mutex_unlock(&fb_info->bl_curve_mutex);
490 /* When cmap is added back in it should be a binary attribute
491 * not a text one. Consideration should also be given to converting
492 * fbdev to use configfs instead of sysfs */
493 static struct device_attribute device_attrs[] = {
494 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
495 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
496 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
497 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
498 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
499 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
500 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
501 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
502 __ATTR(name, S_IRUGO, show_name, NULL),
503 __ATTR(stride, S_IRUGO, show_stride, NULL),
504 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
505 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
506 #ifdef CONFIG_FB_BACKLIGHT
507 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
511 int fb_init_device(struct fb_info *fb_info)
515 dev_set_drvdata(fb_info->dev, fb_info);
517 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
519 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
520 error = device_create_file(fb_info->dev, &device_attrs[i]);
528 device_remove_file(fb_info->dev, &device_attrs[i]);
529 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
535 void fb_cleanup_device(struct fb_info *fb_info)
539 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
540 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
541 device_remove_file(fb_info->dev, &device_attrs[i]);
543 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
547 #ifdef CONFIG_FB_BACKLIGHT
548 /* This function generates a linear backlight curve
552 * 8-127: linear from min to max
554 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
556 unsigned int i, flat, count, range = (max - min);
558 mutex_lock(&fb_info->bl_curve_mutex);
560 fb_info->bl_curve[0] = off;
562 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
563 fb_info->bl_curve[flat] = min;
565 count = FB_BACKLIGHT_LEVELS * 15 / 16;
566 for (i = 0; i < count; ++i)
567 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
569 mutex_unlock(&fb_info->bl_curve_mutex);
571 EXPORT_SYMBOL_GPL(fb_bl_default_curve);