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>
19 #include <linux/slab.h>
21 #include <linux/console.h>
22 #include <linux/module.h>
24 #define FB_SYSFS_FLAG_ATTR 1
27 * framebuffer_alloc - creates a new frame buffer info structure
29 * @size: size of driver private data, can be zero
30 * @dev: pointer to the device for this fb, this can be NULL
32 * Creates a new frame buffer info structure. Also reserves @size bytes
33 * for driver private data (info->par). info->par (if any) will be
34 * aligned to sizeof(long).
36 * Returns the new structure, or NULL if an error occurred.
39 struct fb_info
*framebuffer_alloc(size_t size
, struct device
*dev
)
41 #define BYTES_PER_LONG (BITS_PER_LONG/8)
42 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
43 int fb_info_size
= sizeof(struct fb_info
);
48 fb_info_size
+= PADDING
;
50 p
= kzalloc(fb_info_size
+ size
, GFP_KERNEL
);
55 info
= (struct fb_info
*) p
;
58 info
->par
= p
+ fb_info_size
;
61 info
->fbcon_rotate_hint
= -1;
63 #ifdef CONFIG_FB_BACKLIGHT
64 mutex_init(&info
->bl_curve_mutex
);
71 EXPORT_SYMBOL(framebuffer_alloc
);
74 * framebuffer_release - marks the structure available for freeing
76 * @info: frame buffer info structure
78 * Drop the reference count of the device embedded in the
79 * framebuffer info structure.
82 void framebuffer_release(struct fb_info
*info
)
86 kfree(info
->apertures
);
89 EXPORT_SYMBOL(framebuffer_release
);
91 static int activate(struct fb_info
*fb_info
, struct fb_var_screeninfo
*var
)
95 var
->activate
|= FB_ACTIVATE_FORCE
;
97 fb_info
->flags
|= FBINFO_MISC_USEREVENT
;
98 err
= fb_set_var(fb_info
, var
);
99 fb_info
->flags
&= ~FBINFO_MISC_USEREVENT
;
106 static int mode_string(char *buf
, unsigned int offset
,
107 const struct fb_videomode
*mode
)
112 if (mode
->flag
& FB_MODE_IS_DETAILED
)
114 if (mode
->flag
& FB_MODE_IS_VESA
)
116 if (mode
->flag
& FB_MODE_IS_STANDARD
)
119 if (mode
->vmode
& FB_VMODE_INTERLACED
)
121 if (mode
->vmode
& FB_VMODE_DOUBLE
)
124 return snprintf(&buf
[offset
], PAGE_SIZE
- offset
, "%c:%dx%d%c-%d\n",
125 m
, mode
->xres
, mode
->yres
, v
, mode
->refresh
);
128 static ssize_t
store_mode(struct device
*device
, struct device_attribute
*attr
,
129 const char *buf
, size_t count
)
131 struct fb_info
*fb_info
= dev_get_drvdata(device
);
133 struct fb_var_screeninfo var
;
134 struct fb_modelist
*modelist
;
135 struct fb_videomode
*mode
;
136 struct list_head
*pos
;
140 memset(&var
, 0, sizeof(var
));
142 list_for_each(pos
, &fb_info
->modelist
) {
143 modelist
= list_entry(pos
, struct fb_modelist
, list
);
144 mode
= &modelist
->mode
;
145 i
= mode_string(mstr
, 0, mode
);
146 if (strncmp(mstr
, buf
, max(count
, i
)) == 0) {
149 fb_videomode_to_var(&var
, mode
);
150 if ((err
= activate(fb_info
, &var
)))
152 fb_info
->mode
= mode
;
159 static ssize_t
show_mode(struct device
*device
, struct device_attribute
*attr
,
162 struct fb_info
*fb_info
= dev_get_drvdata(device
);
167 return mode_string(buf
, 0, fb_info
->mode
);
170 static ssize_t
store_modes(struct device
*device
,
171 struct device_attribute
*attr
,
172 const char *buf
, size_t count
)
174 struct fb_info
*fb_info
= dev_get_drvdata(device
);
176 int i
= count
/ sizeof(struct fb_videomode
);
178 if (i
* sizeof(struct fb_videomode
) != count
)
182 if (!lock_fb_info(fb_info
)) {
187 list_splice(&fb_info
->modelist
, &old_list
);
188 fb_videomode_to_modelist((const struct fb_videomode
*)buf
, i
,
190 if (fb_new_modelist(fb_info
)) {
191 fb_destroy_modelist(&fb_info
->modelist
);
192 list_splice(&old_list
, &fb_info
->modelist
);
194 fb_destroy_modelist(&old_list
);
196 unlock_fb_info(fb_info
);
202 static ssize_t
show_modes(struct device
*device
, struct device_attribute
*attr
,
205 struct fb_info
*fb_info
= dev_get_drvdata(device
);
207 struct list_head
*pos
;
208 struct fb_modelist
*modelist
;
209 const struct fb_videomode
*mode
;
212 list_for_each(pos
, &fb_info
->modelist
) {
213 modelist
= list_entry(pos
, struct fb_modelist
, list
);
214 mode
= &modelist
->mode
;
215 i
+= mode_string(buf
, i
, mode
);
220 static ssize_t
store_bpp(struct device
*device
, struct device_attribute
*attr
,
221 const char *buf
, size_t count
)
223 struct fb_info
*fb_info
= dev_get_drvdata(device
);
224 struct fb_var_screeninfo var
;
229 var
.bits_per_pixel
= simple_strtoul(buf
, last
, 0);
230 if ((err
= activate(fb_info
, &var
)))
235 static ssize_t
show_bpp(struct device
*device
, struct device_attribute
*attr
,
238 struct fb_info
*fb_info
= dev_get_drvdata(device
);
239 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.bits_per_pixel
);
242 static ssize_t
store_rotate(struct device
*device
,
243 struct device_attribute
*attr
,
244 const char *buf
, size_t count
)
246 struct fb_info
*fb_info
= dev_get_drvdata(device
);
247 struct fb_var_screeninfo var
;
252 var
.rotate
= simple_strtoul(buf
, last
, 0);
254 if ((err
= activate(fb_info
, &var
)))
261 static ssize_t
show_rotate(struct device
*device
,
262 struct device_attribute
*attr
, char *buf
)
264 struct fb_info
*fb_info
= dev_get_drvdata(device
);
266 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.rotate
);
269 static ssize_t
store_virtual(struct device
*device
,
270 struct device_attribute
*attr
,
271 const char *buf
, size_t count
)
273 struct fb_info
*fb_info
= dev_get_drvdata(device
);
274 struct fb_var_screeninfo var
;
279 var
.xres_virtual
= simple_strtoul(buf
, &last
, 0);
281 if (last
- buf
>= count
)
283 var
.yres_virtual
= simple_strtoul(last
, &last
, 0);
285 if ((err
= activate(fb_info
, &var
)))
290 static ssize_t
show_virtual(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,%d\n", fb_info
->var
.xres_virtual
,
295 fb_info
->var
.yres_virtual
);
298 static ssize_t
show_stride(struct device
*device
,
299 struct device_attribute
*attr
, char *buf
)
301 struct fb_info
*fb_info
= dev_get_drvdata(device
);
302 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->fix
.line_length
);
305 static ssize_t
store_blank(struct device
*device
,
306 struct device_attribute
*attr
,
307 const char *buf
, size_t count
)
309 struct fb_info
*fb_info
= dev_get_drvdata(device
);
314 fb_info
->flags
|= FBINFO_MISC_USEREVENT
;
315 err
= fb_blank(fb_info
, simple_strtoul(buf
, &last
, 0));
316 fb_info
->flags
&= ~FBINFO_MISC_USEREVENT
;
323 static ssize_t
show_blank(struct device
*device
,
324 struct device_attribute
*attr
, char *buf
)
326 // struct fb_info *fb_info = dev_get_drvdata(device);
330 static ssize_t
store_console(struct device
*device
,
331 struct device_attribute
*attr
,
332 const char *buf
, size_t count
)
334 // struct fb_info *fb_info = dev_get_drvdata(device);
338 static ssize_t
show_console(struct device
*device
,
339 struct device_attribute
*attr
, char *buf
)
341 // struct fb_info *fb_info = dev_get_drvdata(device);
345 static ssize_t
store_cursor(struct device
*device
,
346 struct device_attribute
*attr
,
347 const char *buf
, size_t count
)
349 // struct fb_info *fb_info = dev_get_drvdata(device);
353 static ssize_t
show_cursor(struct device
*device
,
354 struct device_attribute
*attr
, char *buf
)
356 // struct fb_info *fb_info = dev_get_drvdata(device);
360 static ssize_t
store_pan(struct device
*device
,
361 struct device_attribute
*attr
,
362 const char *buf
, size_t count
)
364 struct fb_info
*fb_info
= dev_get_drvdata(device
);
365 struct fb_var_screeninfo var
;
370 var
.xoffset
= simple_strtoul(buf
, &last
, 0);
372 if (last
- buf
>= count
)
374 var
.yoffset
= simple_strtoul(last
, &last
, 0);
377 err
= fb_pan_display(fb_info
, &var
);
385 static ssize_t
show_pan(struct device
*device
,
386 struct device_attribute
*attr
, char *buf
)
388 struct fb_info
*fb_info
= dev_get_drvdata(device
);
389 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n", fb_info
->var
.xoffset
,
390 fb_info
->var
.yoffset
);
393 static ssize_t
show_name(struct device
*device
,
394 struct device_attribute
*attr
, char *buf
)
396 struct fb_info
*fb_info
= dev_get_drvdata(device
);
398 return snprintf(buf
, PAGE_SIZE
, "%s\n", fb_info
->fix
.id
);
401 static ssize_t
store_fbstate(struct device
*device
,
402 struct device_attribute
*attr
,
403 const char *buf
, size_t count
)
405 struct fb_info
*fb_info
= dev_get_drvdata(device
);
409 state
= simple_strtoul(buf
, &last
, 0);
412 if (!lock_fb_info(fb_info
)) {
417 fb_set_suspend(fb_info
, (int)state
);
419 unlock_fb_info(fb_info
);
425 static ssize_t
show_fbstate(struct device
*device
,
426 struct device_attribute
*attr
, char *buf
)
428 struct fb_info
*fb_info
= dev_get_drvdata(device
);
429 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->state
);
432 #ifdef CONFIG_FB_BACKLIGHT
433 static ssize_t
store_bl_curve(struct device
*device
,
434 struct device_attribute
*attr
,
435 const char *buf
, size_t count
)
437 struct fb_info
*fb_info
= dev_get_drvdata(device
);
438 u8 tmp_curve
[FB_BACKLIGHT_LEVELS
];
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 if (count
!= (FB_BACKLIGHT_LEVELS
/ 8 * 24))
450 for (i
= 0; i
< (FB_BACKLIGHT_LEVELS
/ 8); ++i
)
451 if (sscanf(&buf
[i
* 24],
452 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
453 &tmp_curve
[i
* 8 + 0],
454 &tmp_curve
[i
* 8 + 1],
455 &tmp_curve
[i
* 8 + 2],
456 &tmp_curve
[i
* 8 + 3],
457 &tmp_curve
[i
* 8 + 4],
458 &tmp_curve
[i
* 8 + 5],
459 &tmp_curve
[i
* 8 + 6],
460 &tmp_curve
[i
* 8 + 7]) != 8)
463 /* If there has been an error in the input data, we won't
466 mutex_lock(&fb_info
->bl_curve_mutex
);
467 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; ++i
)
468 fb_info
->bl_curve
[i
] = tmp_curve
[i
];
469 mutex_unlock(&fb_info
->bl_curve_mutex
);
474 static ssize_t
show_bl_curve(struct device
*device
,
475 struct device_attribute
*attr
, char *buf
)
477 struct fb_info
*fb_info
= dev_get_drvdata(device
);
481 /* Some drivers don't use framebuffer_alloc(), but those also
482 * don't have backlights.
484 if (!fb_info
|| !fb_info
->bl_dev
)
487 mutex_lock(&fb_info
->bl_curve_mutex
);
488 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; i
+= 8)
489 len
+= scnprintf(&buf
[len
], PAGE_SIZE
- len
, "%8ph\n",
490 fb_info
->bl_curve
+ i
);
491 mutex_unlock(&fb_info
->bl_curve_mutex
);
497 /* When cmap is added back in it should be a binary attribute
498 * not a text one. Consideration should also be given to converting
499 * fbdev to use configfs instead of sysfs */
500 static struct device_attribute device_attrs
[] = {
501 __ATTR(bits_per_pixel
, S_IRUGO
|S_IWUSR
, show_bpp
, store_bpp
),
502 __ATTR(blank
, S_IRUGO
|S_IWUSR
, show_blank
, store_blank
),
503 __ATTR(console
, S_IRUGO
|S_IWUSR
, show_console
, store_console
),
504 __ATTR(cursor
, S_IRUGO
|S_IWUSR
, show_cursor
, store_cursor
),
505 __ATTR(mode
, S_IRUGO
|S_IWUSR
, show_mode
, store_mode
),
506 __ATTR(modes
, S_IRUGO
|S_IWUSR
, show_modes
, store_modes
),
507 __ATTR(pan
, S_IRUGO
|S_IWUSR
, show_pan
, store_pan
),
508 __ATTR(virtual_size
, S_IRUGO
|S_IWUSR
, show_virtual
, store_virtual
),
509 __ATTR(name
, S_IRUGO
, show_name
, NULL
),
510 __ATTR(stride
, S_IRUGO
, show_stride
, NULL
),
511 __ATTR(rotate
, S_IRUGO
|S_IWUSR
, show_rotate
, store_rotate
),
512 __ATTR(state
, S_IRUGO
|S_IWUSR
, show_fbstate
, store_fbstate
),
513 #ifdef CONFIG_FB_BACKLIGHT
514 __ATTR(bl_curve
, S_IRUGO
|S_IWUSR
, show_bl_curve
, store_bl_curve
),
518 int fb_init_device(struct fb_info
*fb_info
)
522 dev_set_drvdata(fb_info
->dev
, fb_info
);
524 fb_info
->class_flag
|= FB_SYSFS_FLAG_ATTR
;
526 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++) {
527 error
= device_create_file(fb_info
->dev
, &device_attrs
[i
]);
535 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
536 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
542 void fb_cleanup_device(struct fb_info
*fb_info
)
546 if (fb_info
->class_flag
& FB_SYSFS_FLAG_ATTR
) {
547 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++)
548 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
550 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
554 #ifdef CONFIG_FB_BACKLIGHT
555 /* This function generates a linear backlight curve
559 * 8-127: linear from min to max
561 void fb_bl_default_curve(struct fb_info
*fb_info
, u8 off
, u8 min
, u8 max
)
563 unsigned int i
, flat
, count
, range
= (max
- min
);
565 mutex_lock(&fb_info
->bl_curve_mutex
);
567 fb_info
->bl_curve
[0] = off
;
569 for (flat
= 1; flat
< (FB_BACKLIGHT_LEVELS
/ 16); ++flat
)
570 fb_info
->bl_curve
[flat
] = min
;
572 count
= FB_BACKLIGHT_LEVELS
* 15 / 16;
573 for (i
= 0; i
< count
; ++i
)
574 fb_info
->bl_curve
[flat
+ i
] = min
+ (range
* (i
+ 1) / count
);
576 mutex_unlock(&fb_info
->bl_curve_mutex
);
578 EXPORT_SYMBOL_GPL(fb_bl_default_curve
);