1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * fbsysfs.c - framebuffer device class and attributes
5 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
9 * Note: currently there's only stubs for framebuffer_alloc and
10 * framebuffer_release here. The reson for that is that until all drivers
11 * are converted to use it a sysfsification will open OOPSable races.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/fbcon.h>
18 #include <linux/console.h>
19 #include <linux/module.h>
21 #define FB_SYSFS_FLAG_ATTR 1
24 * framebuffer_alloc - creates a new frame buffer info structure
26 * @size: size of driver private data, can be zero
27 * @dev: pointer to the device for this fb, this can be NULL
29 * Creates a new frame buffer info structure. Also reserves @size bytes
30 * for driver private data (info->par). info->par (if any) will be
31 * aligned to sizeof(long).
33 * Returns the new structure, or NULL if an error occurred.
36 struct fb_info
*framebuffer_alloc(size_t size
, struct device
*dev
)
38 #define BYTES_PER_LONG (BITS_PER_LONG/8)
39 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
40 int fb_info_size
= sizeof(struct fb_info
);
45 fb_info_size
+= PADDING
;
47 p
= kzalloc(fb_info_size
+ size
, GFP_KERNEL
);
52 info
= (struct fb_info
*) p
;
55 info
->par
= p
+ fb_info_size
;
58 info
->fbcon_rotate_hint
= -1;
60 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
61 mutex_init(&info
->bl_curve_mutex
);
68 EXPORT_SYMBOL(framebuffer_alloc
);
71 * framebuffer_release - marks the structure available for freeing
73 * @info: frame buffer info structure
75 * Drop the reference count of the device embedded in the
76 * framebuffer info structure.
79 void framebuffer_release(struct fb_info
*info
)
83 kfree(info
->apertures
);
86 EXPORT_SYMBOL(framebuffer_release
);
88 static int activate(struct fb_info
*fb_info
, struct fb_var_screeninfo
*var
)
92 var
->activate
|= FB_ACTIVATE_FORCE
;
94 err
= fb_set_var(fb_info
, var
);
96 fbcon_update_vcs(fb_info
, var
->activate
& FB_ACTIVATE_ALL
);
103 static int mode_string(char *buf
, unsigned int offset
,
104 const struct fb_videomode
*mode
)
109 if (mode
->flag
& FB_MODE_IS_DETAILED
)
111 if (mode
->flag
& FB_MODE_IS_VESA
)
113 if (mode
->flag
& FB_MODE_IS_STANDARD
)
116 if (mode
->vmode
& FB_VMODE_INTERLACED
)
118 if (mode
->vmode
& FB_VMODE_DOUBLE
)
121 return snprintf(&buf
[offset
], PAGE_SIZE
- offset
, "%c:%dx%d%c-%d\n",
122 m
, mode
->xres
, mode
->yres
, v
, mode
->refresh
);
125 static ssize_t
store_mode(struct device
*device
, struct device_attribute
*attr
,
126 const char *buf
, size_t count
)
128 struct fb_info
*fb_info
= dev_get_drvdata(device
);
130 struct fb_var_screeninfo var
;
131 struct fb_modelist
*modelist
;
132 struct fb_videomode
*mode
;
133 struct list_head
*pos
;
137 memset(&var
, 0, sizeof(var
));
139 list_for_each(pos
, &fb_info
->modelist
) {
140 modelist
= list_entry(pos
, struct fb_modelist
, list
);
141 mode
= &modelist
->mode
;
142 i
= mode_string(mstr
, 0, mode
);
143 if (strncmp(mstr
, buf
, max(count
, i
)) == 0) {
146 fb_videomode_to_var(&var
, mode
);
147 if ((err
= activate(fb_info
, &var
)))
149 fb_info
->mode
= mode
;
156 static ssize_t
show_mode(struct device
*device
, struct device_attribute
*attr
,
159 struct fb_info
*fb_info
= dev_get_drvdata(device
);
164 return mode_string(buf
, 0, fb_info
->mode
);
167 static ssize_t
store_modes(struct device
*device
,
168 struct device_attribute
*attr
,
169 const char *buf
, size_t count
)
171 struct fb_info
*fb_info
= dev_get_drvdata(device
);
173 int i
= count
/ sizeof(struct fb_videomode
);
175 if (i
* sizeof(struct fb_videomode
) != count
)
179 lock_fb_info(fb_info
);
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
);
190 unlock_fb_info(fb_info
);
196 static ssize_t
show_modes(struct device
*device
, struct device_attribute
*attr
,
199 struct fb_info
*fb_info
= dev_get_drvdata(device
);
201 struct list_head
*pos
;
202 struct fb_modelist
*modelist
;
203 const struct fb_videomode
*mode
;
206 list_for_each(pos
, &fb_info
->modelist
) {
207 modelist
= list_entry(pos
, struct fb_modelist
, list
);
208 mode
= &modelist
->mode
;
209 i
+= mode_string(buf
, i
, mode
);
214 static ssize_t
store_bpp(struct device
*device
, struct device_attribute
*attr
,
215 const char *buf
, size_t count
)
217 struct fb_info
*fb_info
= dev_get_drvdata(device
);
218 struct fb_var_screeninfo var
;
223 var
.bits_per_pixel
= simple_strtoul(buf
, last
, 0);
224 if ((err
= activate(fb_info
, &var
)))
229 static ssize_t
show_bpp(struct device
*device
, struct device_attribute
*attr
,
232 struct fb_info
*fb_info
= dev_get_drvdata(device
);
233 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.bits_per_pixel
);
236 static ssize_t
store_rotate(struct device
*device
,
237 struct device_attribute
*attr
,
238 const char *buf
, size_t count
)
240 struct fb_info
*fb_info
= dev_get_drvdata(device
);
241 struct fb_var_screeninfo var
;
246 var
.rotate
= simple_strtoul(buf
, last
, 0);
248 if ((err
= activate(fb_info
, &var
)))
255 static ssize_t
show_rotate(struct device
*device
,
256 struct device_attribute
*attr
, char *buf
)
258 struct fb_info
*fb_info
= dev_get_drvdata(device
);
260 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->var
.rotate
);
263 static ssize_t
store_virtual(struct device
*device
,
264 struct device_attribute
*attr
,
265 const char *buf
, size_t count
)
267 struct fb_info
*fb_info
= dev_get_drvdata(device
);
268 struct fb_var_screeninfo var
;
273 var
.xres_virtual
= simple_strtoul(buf
, &last
, 0);
275 if (last
- buf
>= count
)
277 var
.yres_virtual
= simple_strtoul(last
, &last
, 0);
279 if ((err
= activate(fb_info
, &var
)))
284 static ssize_t
show_virtual(struct device
*device
,
285 struct device_attribute
*attr
, char *buf
)
287 struct fb_info
*fb_info
= dev_get_drvdata(device
);
288 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n", fb_info
->var
.xres_virtual
,
289 fb_info
->var
.yres_virtual
);
292 static ssize_t
show_stride(struct device
*device
,
293 struct device_attribute
*attr
, char *buf
)
295 struct fb_info
*fb_info
= dev_get_drvdata(device
);
296 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->fix
.line_length
);
299 static ssize_t
store_blank(struct device
*device
,
300 struct device_attribute
*attr
,
301 const char *buf
, size_t count
)
303 struct fb_info
*fb_info
= dev_get_drvdata(device
);
307 arg
= simple_strtoul(buf
, &last
, 0);
309 err
= fb_blank(fb_info
, arg
);
310 /* might again call into fb_blank */
311 fbcon_fb_blanked(fb_info
, arg
);
318 static ssize_t
show_blank(struct device
*device
,
319 struct device_attribute
*attr
, char *buf
)
321 // struct fb_info *fb_info = dev_get_drvdata(device);
325 static ssize_t
store_console(struct device
*device
,
326 struct device_attribute
*attr
,
327 const char *buf
, size_t count
)
329 // struct fb_info *fb_info = dev_get_drvdata(device);
333 static ssize_t
show_console(struct device
*device
,
334 struct device_attribute
*attr
, char *buf
)
336 // struct fb_info *fb_info = dev_get_drvdata(device);
340 static ssize_t
store_cursor(struct device
*device
,
341 struct device_attribute
*attr
,
342 const char *buf
, size_t count
)
344 // struct fb_info *fb_info = dev_get_drvdata(device);
348 static ssize_t
show_cursor(struct device
*device
,
349 struct device_attribute
*attr
, char *buf
)
351 // struct fb_info *fb_info = dev_get_drvdata(device);
355 static ssize_t
store_pan(struct device
*device
,
356 struct device_attribute
*attr
,
357 const char *buf
, size_t count
)
359 struct fb_info
*fb_info
= dev_get_drvdata(device
);
360 struct fb_var_screeninfo var
;
365 var
.xoffset
= simple_strtoul(buf
, &last
, 0);
367 if (last
- buf
>= count
)
369 var
.yoffset
= simple_strtoul(last
, &last
, 0);
372 err
= fb_pan_display(fb_info
, &var
);
380 static ssize_t
show_pan(struct device
*device
,
381 struct device_attribute
*attr
, char *buf
)
383 struct fb_info
*fb_info
= dev_get_drvdata(device
);
384 return snprintf(buf
, PAGE_SIZE
, "%d,%d\n", fb_info
->var
.xoffset
,
385 fb_info
->var
.yoffset
);
388 static ssize_t
show_name(struct device
*device
,
389 struct device_attribute
*attr
, char *buf
)
391 struct fb_info
*fb_info
= dev_get_drvdata(device
);
393 return snprintf(buf
, PAGE_SIZE
, "%s\n", fb_info
->fix
.id
);
396 static ssize_t
store_fbstate(struct device
*device
,
397 struct device_attribute
*attr
,
398 const char *buf
, size_t count
)
400 struct fb_info
*fb_info
= dev_get_drvdata(device
);
404 state
= simple_strtoul(buf
, &last
, 0);
407 lock_fb_info(fb_info
);
409 fb_set_suspend(fb_info
, (int)state
);
411 unlock_fb_info(fb_info
);
417 static ssize_t
show_fbstate(struct device
*device
,
418 struct device_attribute
*attr
, char *buf
)
420 struct fb_info
*fb_info
= dev_get_drvdata(device
);
421 return snprintf(buf
, PAGE_SIZE
, "%d\n", fb_info
->state
);
424 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
425 static ssize_t
store_bl_curve(struct device
*device
,
426 struct device_attribute
*attr
,
427 const char *buf
, size_t count
)
429 struct fb_info
*fb_info
= dev_get_drvdata(device
);
430 u8 tmp_curve
[FB_BACKLIGHT_LEVELS
];
433 /* Some drivers don't use framebuffer_alloc(), but those also
434 * don't have backlights.
436 if (!fb_info
|| !fb_info
->bl_dev
)
439 if (count
!= (FB_BACKLIGHT_LEVELS
/ 8 * 24))
442 for (i
= 0; i
< (FB_BACKLIGHT_LEVELS
/ 8); ++i
)
443 if (sscanf(&buf
[i
* 24],
444 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
445 &tmp_curve
[i
* 8 + 0],
446 &tmp_curve
[i
* 8 + 1],
447 &tmp_curve
[i
* 8 + 2],
448 &tmp_curve
[i
* 8 + 3],
449 &tmp_curve
[i
* 8 + 4],
450 &tmp_curve
[i
* 8 + 5],
451 &tmp_curve
[i
* 8 + 6],
452 &tmp_curve
[i
* 8 + 7]) != 8)
455 /* If there has been an error in the input data, we won't
458 mutex_lock(&fb_info
->bl_curve_mutex
);
459 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; ++i
)
460 fb_info
->bl_curve
[i
] = tmp_curve
[i
];
461 mutex_unlock(&fb_info
->bl_curve_mutex
);
466 static ssize_t
show_bl_curve(struct device
*device
,
467 struct device_attribute
*attr
, char *buf
)
469 struct fb_info
*fb_info
= dev_get_drvdata(device
);
473 /* Some drivers don't use framebuffer_alloc(), but those also
474 * don't have backlights.
476 if (!fb_info
|| !fb_info
->bl_dev
)
479 mutex_lock(&fb_info
->bl_curve_mutex
);
480 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; i
+= 8)
481 len
+= scnprintf(&buf
[len
], PAGE_SIZE
- len
, "%8ph\n",
482 fb_info
->bl_curve
+ i
);
483 mutex_unlock(&fb_info
->bl_curve_mutex
);
489 /* When cmap is added back in it should be a binary attribute
490 * not a text one. Consideration should also be given to converting
491 * fbdev to use configfs instead of sysfs */
492 static struct device_attribute device_attrs
[] = {
493 __ATTR(bits_per_pixel
, S_IRUGO
|S_IWUSR
, show_bpp
, store_bpp
),
494 __ATTR(blank
, S_IRUGO
|S_IWUSR
, show_blank
, store_blank
),
495 __ATTR(console
, S_IRUGO
|S_IWUSR
, show_console
, store_console
),
496 __ATTR(cursor
, S_IRUGO
|S_IWUSR
, show_cursor
, store_cursor
),
497 __ATTR(mode
, S_IRUGO
|S_IWUSR
, show_mode
, store_mode
),
498 __ATTR(modes
, S_IRUGO
|S_IWUSR
, show_modes
, store_modes
),
499 __ATTR(pan
, S_IRUGO
|S_IWUSR
, show_pan
, store_pan
),
500 __ATTR(virtual_size
, S_IRUGO
|S_IWUSR
, show_virtual
, store_virtual
),
501 __ATTR(name
, S_IRUGO
, show_name
, NULL
),
502 __ATTR(stride
, S_IRUGO
, show_stride
, NULL
),
503 __ATTR(rotate
, S_IRUGO
|S_IWUSR
, show_rotate
, store_rotate
),
504 __ATTR(state
, S_IRUGO
|S_IWUSR
, show_fbstate
, store_fbstate
),
505 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
506 __ATTR(bl_curve
, S_IRUGO
|S_IWUSR
, show_bl_curve
, store_bl_curve
),
510 int fb_init_device(struct fb_info
*fb_info
)
514 dev_set_drvdata(fb_info
->dev
, fb_info
);
516 fb_info
->class_flag
|= FB_SYSFS_FLAG_ATTR
;
518 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++) {
519 error
= device_create_file(fb_info
->dev
, &device_attrs
[i
]);
527 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
528 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
534 void fb_cleanup_device(struct fb_info
*fb_info
)
538 if (fb_info
->class_flag
& FB_SYSFS_FLAG_ATTR
) {
539 for (i
= 0; i
< ARRAY_SIZE(device_attrs
); i
++)
540 device_remove_file(fb_info
->dev
, &device_attrs
[i
]);
542 fb_info
->class_flag
&= ~FB_SYSFS_FLAG_ATTR
;
546 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
547 /* This function generates a linear backlight curve
551 * 8-127: linear from min to max
553 void fb_bl_default_curve(struct fb_info
*fb_info
, u8 off
, u8 min
, u8 max
)
555 unsigned int i
, flat
, count
, range
= (max
- min
);
557 mutex_lock(&fb_info
->bl_curve_mutex
);
559 fb_info
->bl_curve
[0] = off
;
561 for (flat
= 1; flat
< (FB_BACKLIGHT_LEVELS
/ 16); ++flat
)
562 fb_info
->bl_curve
[flat
] = min
;
564 count
= FB_BACKLIGHT_LEVELS
* 15 / 16;
565 for (i
= 0; i
< count
; ++i
)
566 fb_info
->bl_curve
[flat
+ i
] = min
+ (range
* (i
+ 1) / count
);
568 mutex_unlock(&fb_info
->bl_curve_mutex
);
570 EXPORT_SYMBOL_GPL(fb_bl_default_curve
);