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
;
62 #ifdef CONFIG_FB_BACKLIGHT
63 mutex_init(&info
->bl_curve_mutex
);
70 EXPORT_SYMBOL(framebuffer_alloc
);
73 * framebuffer_release - marks the structure available for freeing
75 * @info: frame buffer info structure
77 * Drop the reference count of the device embedded in the
78 * framebuffer info structure.
81 void framebuffer_release(struct fb_info
*info
)
85 kfree(info
->apertures
);
88 EXPORT_SYMBOL(framebuffer_release
);
90 static int activate(struct fb_info
*fb_info
, struct fb_var_screeninfo
*var
)
94 var
->activate
|= FB_ACTIVATE_FORCE
;
96 fb_info
->flags
|= FBINFO_MISC_USEREVENT
;
97 err
= fb_set_var(fb_info
, var
);
98 fb_info
->flags
&= ~FBINFO_MISC_USEREVENT
;
105 static int mode_string(char *buf
, unsigned int offset
,
106 const struct fb_videomode
*mode
)
111 if (mode
->flag
& FB_MODE_IS_DETAILED
)
113 if (mode
->flag
& FB_MODE_IS_VESA
)
115 if (mode
->flag
& FB_MODE_IS_STANDARD
)
118 if (mode
->vmode
& FB_VMODE_INTERLACED
)
120 if (mode
->vmode
& FB_VMODE_DOUBLE
)
123 return snprintf(&buf
[offset
], PAGE_SIZE
- offset
, "%c:%dx%d%c-%d\n",
124 m
, mode
->xres
, mode
->yres
, v
, mode
->refresh
);
127 static ssize_t
store_mode(struct device
*device
, struct device_attribute
*attr
,
128 const char *buf
, size_t count
)
130 struct fb_info
*fb_info
= dev_get_drvdata(device
);
132 struct fb_var_screeninfo var
;
133 struct fb_modelist
*modelist
;
134 struct fb_videomode
*mode
;
135 struct list_head
*pos
;
139 memset(&var
, 0, sizeof(var
));
141 list_for_each(pos
, &fb_info
->modelist
) {
142 modelist
= list_entry(pos
, struct fb_modelist
, list
);
143 mode
= &modelist
->mode
;
144 i
= mode_string(mstr
, 0, mode
);
145 if (strncmp(mstr
, buf
, max(count
, i
)) == 0) {
148 fb_videomode_to_var(&var
, mode
);
149 if ((err
= activate(fb_info
, &var
)))
151 fb_info
->mode
= mode
;
158 static ssize_t
show_mode(struct device
*device
, struct device_attribute
*attr
,
161 struct fb_info
*fb_info
= dev_get_drvdata(device
);
166 return mode_string(buf
, 0, fb_info
->mode
);
169 static ssize_t
store_modes(struct device
*device
,
170 struct device_attribute
*attr
,
171 const char *buf
, size_t count
)
173 struct fb_info
*fb_info
= dev_get_drvdata(device
);
175 int i
= count
/ sizeof(struct fb_videomode
);
177 if (i
* sizeof(struct fb_videomode
) != count
)
181 list_splice(&fb_info
->modelist
, &old_list
);
182 fb_videomode_to_modelist((const struct fb_videomode
*)buf
, i
,
184 if (fb_new_modelist(fb_info
)) {
185 fb_destroy_modelist(&fb_info
->modelist
);
186 list_splice(&old_list
, &fb_info
->modelist
);
188 fb_destroy_modelist(&old_list
);
195 static ssize_t
show_modes(struct device
*device
, struct device_attribute
*attr
,
198 struct fb_info
*fb_info
= dev_get_drvdata(device
);
200 struct list_head
*pos
;
201 struct fb_modelist
*modelist
;
202 const struct fb_videomode
*mode
;
205 list_for_each(pos
, &fb_info
->modelist
) {
206 modelist
= list_entry(pos
, struct fb_modelist
, list
);
207 mode
= &modelist
->mode
;
208 i
+= mode_string(buf
, i
, mode
);
213 static ssize_t
store_bpp(struct device
*device
, struct device_attribute
*attr
,
214 const char *buf
, size_t count
)
216 struct fb_info
*fb_info
= dev_get_drvdata(device
);
217 struct fb_var_screeninfo var
;
222 var
.bits_per_pixel
= simple_strtoul(buf
, last
, 0);
223 if ((err
= activate(fb_info
, &var
)))
228 static ssize_t
show_bpp(struct device
*device
, struct device_attribute
*attr
,
231 struct fb_info
*fb_info
= dev_get_drvdata(device
);
232 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.bits_per_pixel
);
235 static ssize_t
store_rotate(struct device
*device
,
236 struct device_attribute
*attr
,
237 const char *buf
, size_t count
)
239 struct fb_info
*fb_info
= dev_get_drvdata(device
);
240 struct fb_var_screeninfo var
;
245 var
.rotate
= simple_strtoul(buf
, last
, 0);
247 if ((err
= activate(fb_info
, &var
)))
254 static ssize_t
show_rotate(struct device
*device
,
255 struct device_attribute
*attr
, char *buf
)
257 struct fb_info
*fb_info
= dev_get_drvdata(device
);
259 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.rotate
);
262 static ssize_t
store_virtual(struct device
*device
,
263 struct device_attribute
*attr
,
264 const char *buf
, size_t count
)
266 struct fb_info
*fb_info
= dev_get_drvdata(device
);
267 struct fb_var_screeninfo var
;
272 var
.xres_virtual
= simple_strtoul(buf
, &last
, 0);
274 if (last
- buf
>= count
)
276 var
.yres_virtual
= simple_strtoul(last
, &last
, 0);
278 if ((err
= activate(fb_info
, &var
)))
283 static ssize_t
show_virtual(struct device
*device
,
284 struct device_attribute
*attr
, char *buf
)
286 struct fb_info
*fb_info
= dev_get_drvdata(device
);
287 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n", fb_info
->var
.xres_virtual
,
288 fb_info
->var
.yres_virtual
);
291 static ssize_t
show_stride(struct device
*device
,
292 struct device_attribute
*attr
, char *buf
)
294 struct fb_info
*fb_info
= dev_get_drvdata(device
);
295 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->fix
.line_length
);
298 static ssize_t
store_blank(struct device
*device
,
299 struct device_attribute
*attr
,
300 const char *buf
, size_t count
)
302 struct fb_info
*fb_info
= dev_get_drvdata(device
);
307 fb_info
->flags
|= FBINFO_MISC_USEREVENT
;
308 err
= fb_blank(fb_info
, simple_strtoul(buf
, &last
, 0));
309 fb_info
->flags
&= ~FBINFO_MISC_USEREVENT
;
316 static ssize_t
show_blank(struct device
*device
,
317 struct device_attribute
*attr
, char *buf
)
319 // struct fb_info *fb_info = dev_get_drvdata(device);
323 static ssize_t
store_console(struct device
*device
,
324 struct device_attribute
*attr
,
325 const char *buf
, size_t count
)
327 // struct fb_info *fb_info = dev_get_drvdata(device);
331 static ssize_t
show_console(struct device
*device
,
332 struct device_attribute
*attr
, char *buf
)
334 // struct fb_info *fb_info = dev_get_drvdata(device);
338 static ssize_t
store_cursor(struct device
*device
,
339 struct device_attribute
*attr
,
340 const char *buf
, size_t count
)
342 // struct fb_info *fb_info = dev_get_drvdata(device);
346 static ssize_t
show_cursor(struct device
*device
,
347 struct device_attribute
*attr
, char *buf
)
349 // struct fb_info *fb_info = dev_get_drvdata(device);
353 static ssize_t
store_pan(struct device
*device
,
354 struct device_attribute
*attr
,
355 const char *buf
, size_t count
)
357 struct fb_info
*fb_info
= dev_get_drvdata(device
);
358 struct fb_var_screeninfo var
;
363 var
.xoffset
= simple_strtoul(buf
, &last
, 0);
365 if (last
- buf
>= count
)
367 var
.yoffset
= simple_strtoul(last
, &last
, 0);
370 err
= fb_pan_display(fb_info
, &var
);
378 static ssize_t
show_pan(struct device
*device
,
379 struct device_attribute
*attr
, char *buf
)
381 struct fb_info
*fb_info
= dev_get_drvdata(device
);
382 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n", fb_info
->var
.xoffset
,
383 fb_info
->var
.yoffset
);
386 static ssize_t
show_name(struct device
*device
,
387 struct device_attribute
*attr
, char *buf
)
389 struct fb_info
*fb_info
= dev_get_drvdata(device
);
391 return snprintf(buf
, PAGE_SIZE
, "%s\n", fb_info
->fix
.id
);
394 static ssize_t
store_fbstate(struct device
*device
,
395 struct device_attribute
*attr
,
396 const char *buf
, size_t count
)
398 struct fb_info
*fb_info
= dev_get_drvdata(device
);
402 state
= simple_strtoul(buf
, &last
, 0);
404 if (!lock_fb_info(fb_info
))
407 fb_set_suspend(fb_info
, (int)state
);
409 unlock_fb_info(fb_info
);
414 static ssize_t
show_fbstate(struct device
*device
,
415 struct device_attribute
*attr
, char *buf
)
417 struct fb_info
*fb_info
= dev_get_drvdata(device
);
418 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->state
);
421 #ifdef CONFIG_FB_BACKLIGHT
422 static ssize_t
store_bl_curve(struct device
*device
,
423 struct device_attribute
*attr
,
424 const char *buf
, size_t count
)
426 struct fb_info
*fb_info
= dev_get_drvdata(device
);
427 u8 tmp_curve
[FB_BACKLIGHT_LEVELS
];
430 /* Some drivers don't use framebuffer_alloc(), but those also
431 * don't have backlights.
433 if (!fb_info
|| !fb_info
->bl_dev
)
436 if (count
!= (FB_BACKLIGHT_LEVELS
/ 8 * 24))
439 for (i
= 0; i
< (FB_BACKLIGHT_LEVELS
/ 8); ++i
)
440 if (sscanf(&buf
[i
* 24],
441 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
442 &tmp_curve
[i
* 8 + 0],
443 &tmp_curve
[i
* 8 + 1],
444 &tmp_curve
[i
* 8 + 2],
445 &tmp_curve
[i
* 8 + 3],
446 &tmp_curve
[i
* 8 + 4],
447 &tmp_curve
[i
* 8 + 5],
448 &tmp_curve
[i
* 8 + 6],
449 &tmp_curve
[i
* 8 + 7]) != 8)
452 /* If there has been an error in the input data, we won't
455 mutex_lock(&fb_info
->bl_curve_mutex
);
456 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; ++i
)
457 fb_info
->bl_curve
[i
] = tmp_curve
[i
];
458 mutex_unlock(&fb_info
->bl_curve_mutex
);
463 static ssize_t
show_bl_curve(struct device
*device
,
464 struct device_attribute
*attr
, char *buf
)
466 struct fb_info
*fb_info
= dev_get_drvdata(device
);
470 /* Some drivers don't use framebuffer_alloc(), but those also
471 * don't have backlights.
473 if (!fb_info
|| !fb_info
->bl_dev
)
476 mutex_lock(&fb_info
->bl_curve_mutex
);
477 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; i
+= 8)
478 len
+= snprintf(&buf
[len
], PAGE_SIZE
,
479 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
480 fb_info
->bl_curve
[i
+ 0],
481 fb_info
->bl_curve
[i
+ 1],
482 fb_info
->bl_curve
[i
+ 2],
483 fb_info
->bl_curve
[i
+ 3],
484 fb_info
->bl_curve
[i
+ 4],
485 fb_info
->bl_curve
[i
+ 5],
486 fb_info
->bl_curve
[i
+ 6],
487 fb_info
->bl_curve
[i
+ 7]);
488 mutex_unlock(&fb_info
->bl_curve_mutex
);
494 /* When cmap is added back in it should be a binary attribute
495 * not a text one. Consideration should also be given to converting
496 * fbdev to use configfs instead of sysfs */
497 static struct device_attribute device_attrs
[] = {
498 __ATTR(bits_per_pixel
, S_IRUGO
|S_IWUSR
, show_bpp
, store_bpp
),
499 __ATTR(blank
, S_IRUGO
|S_IWUSR
, show_blank
, store_blank
),
500 __ATTR(console
, S_IRUGO
|S_IWUSR
, show_console
, store_console
),
501 __ATTR(cursor
, S_IRUGO
|S_IWUSR
, show_cursor
, store_cursor
),
502 __ATTR(mode
, S_IRUGO
|S_IWUSR
, show_mode
, store_mode
),
503 __ATTR(modes
, S_IRUGO
|S_IWUSR
, show_modes
, store_modes
),
504 __ATTR(pan
, S_IRUGO
|S_IWUSR
, show_pan
, store_pan
),
505 __ATTR(virtual_size
, S_IRUGO
|S_IWUSR
, show_virtual
, store_virtual
),
506 __ATTR(name
, S_IRUGO
, show_name
, NULL
),
507 __ATTR(stride
, S_IRUGO
, show_stride
, NULL
),
508 __ATTR(rotate
, S_IRUGO
|S_IWUSR
, show_rotate
, store_rotate
),
509 __ATTR(state
, S_IRUGO
|S_IWUSR
, show_fbstate
, store_fbstate
),
510 #ifdef CONFIG_FB_BACKLIGHT
511 __ATTR(bl_curve
, S_IRUGO
|S_IWUSR
, show_bl_curve
, store_bl_curve
),
515 int fb_init_device(struct fb_info
*fb_info
)
519 dev_set_drvdata(fb_info
->dev
, fb_info
);
521 fb_info
->class_flag
|= FB_SYSFS_FLAG_ATTR
;
523 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++) {
524 error
= device_create_file(fb_info
->dev
, &device_attrs
[i
]);
532 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
533 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
539 void fb_cleanup_device(struct fb_info
*fb_info
)
543 if (fb_info
->class_flag
& FB_SYSFS_FLAG_ATTR
) {
544 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++)
545 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
547 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
551 #ifdef CONFIG_FB_BACKLIGHT
552 /* This function generates a linear backlight curve
556 * 8-127: linear from min to max
558 void fb_bl_default_curve(struct fb_info
*fb_info
, u8 off
, u8 min
, u8 max
)
560 unsigned int i
, flat
, count
, range
= (max
- min
);
562 mutex_lock(&fb_info
->bl_curve_mutex
);
564 fb_info
->bl_curve
[0] = off
;
566 for (flat
= 1; flat
< (FB_BACKLIGHT_LEVELS
/ 16); ++flat
)
567 fb_info
->bl_curve
[flat
] = min
;
569 count
= FB_BACKLIGHT_LEVELS
* 15 / 16;
570 for (i
= 0; i
< count
; ++i
)
571 fb_info
->bl_curve
[flat
+ i
] = min
+ (range
* (i
+ 1) / count
);
573 mutex_unlock(&fb_info
->bl_curve_mutex
);
575 EXPORT_SYMBOL_GPL(fb_bl_default_curve
);