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)
84 if (WARN_ON(refcount_read(&info->count)))
87 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
88 mutex_destroy(&info->bl_curve_mutex);
93 EXPORT_SYMBOL(framebuffer_release);
95 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
99 var->activate |= FB_ACTIVATE_FORCE;
101 lock_fb_info(fb_info);
102 err = fb_set_var(fb_info, var);
104 fbcon_update_vcs(fb_info, var->activate & FB_ACTIVATE_ALL);
105 unlock_fb_info(fb_info);
112 static int mode_string(char *buf, unsigned int offset,
113 const struct fb_videomode *mode)
118 if (mode->flag & FB_MODE_IS_DETAILED)
120 if (mode->flag & FB_MODE_IS_VESA)
122 if (mode->flag & FB_MODE_IS_STANDARD)
125 if (mode->vmode & FB_VMODE_INTERLACED)
127 if (mode->vmode & FB_VMODE_DOUBLE)
130 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
131 m, mode->xres, mode->yres, v, mode->refresh);
134 static ssize_t store_mode(struct device *device, struct device_attribute *attr,
135 const char *buf, size_t count)
137 struct fb_info *fb_info = dev_get_drvdata(device);
139 struct fb_var_screeninfo var;
140 struct fb_modelist *modelist;
141 struct fb_videomode *mode;
142 struct list_head *pos;
146 memset(&var, 0, sizeof(var));
148 list_for_each(pos, &fb_info->modelist) {
149 modelist = list_entry(pos, struct fb_modelist, list);
150 mode = &modelist->mode;
151 i = mode_string(mstr, 0, mode);
152 if (strncmp(mstr, buf, max(count, i)) == 0) {
155 fb_videomode_to_var(&var, mode);
156 if ((err = activate(fb_info, &var)))
158 fb_info->mode = mode;
165 static ssize_t show_mode(struct device *device, struct device_attribute *attr,
168 struct fb_info *fb_info = dev_get_drvdata(device);
173 return mode_string(buf, 0, fb_info->mode);
176 static ssize_t store_modes(struct device *device,
177 struct device_attribute *attr,
178 const char *buf, size_t count)
180 struct fb_info *fb_info = dev_get_drvdata(device);
182 int i = count / sizeof(struct fb_videomode);
184 if (i * sizeof(struct fb_videomode) != count)
188 lock_fb_info(fb_info);
190 list_splice(&fb_info->modelist, &old_list);
191 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
193 if (fb_new_modelist(fb_info)) {
194 fb_destroy_modelist(&fb_info->modelist);
195 list_splice(&old_list, &fb_info->modelist);
197 fb_destroy_modelist(&old_list);
199 unlock_fb_info(fb_info);
205 static ssize_t show_modes(struct device *device, struct device_attribute *attr,
208 struct fb_info *fb_info = dev_get_drvdata(device);
210 struct list_head *pos;
211 struct fb_modelist *modelist;
212 const struct fb_videomode *mode;
215 list_for_each(pos, &fb_info->modelist) {
216 modelist = list_entry(pos, struct fb_modelist, list);
217 mode = &modelist->mode;
218 i += mode_string(buf, i, mode);
223 static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
224 const char *buf, size_t count)
226 struct fb_info *fb_info = dev_get_drvdata(device);
227 struct fb_var_screeninfo var;
232 var.bits_per_pixel = simple_strtoul(buf, last, 0);
233 if ((err = activate(fb_info, &var)))
238 static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
241 struct fb_info *fb_info = dev_get_drvdata(device);
242 return sysfs_emit(buf, "%d\n", fb_info->var.bits_per_pixel);
245 static ssize_t store_rotate(struct device *device,
246 struct device_attribute *attr,
247 const char *buf, size_t count)
249 struct fb_info *fb_info = dev_get_drvdata(device);
250 struct fb_var_screeninfo var;
255 var.rotate = simple_strtoul(buf, last, 0);
257 if ((err = activate(fb_info, &var)))
264 static ssize_t show_rotate(struct device *device,
265 struct device_attribute *attr, char *buf)
267 struct fb_info *fb_info = dev_get_drvdata(device);
269 return sysfs_emit(buf, "%d\n", fb_info->var.rotate);
272 static ssize_t store_virtual(struct device *device,
273 struct device_attribute *attr,
274 const char *buf, size_t count)
276 struct fb_info *fb_info = dev_get_drvdata(device);
277 struct fb_var_screeninfo var;
282 var.xres_virtual = simple_strtoul(buf, &last, 0);
284 if (last - buf >= count)
286 var.yres_virtual = simple_strtoul(last, &last, 0);
288 if ((err = activate(fb_info, &var)))
293 static ssize_t show_virtual(struct device *device,
294 struct device_attribute *attr, char *buf)
296 struct fb_info *fb_info = dev_get_drvdata(device);
297 return sysfs_emit(buf, "%d,%d\n", fb_info->var.xres_virtual,
298 fb_info->var.yres_virtual);
301 static ssize_t show_stride(struct device *device,
302 struct device_attribute *attr, char *buf)
304 struct fb_info *fb_info = dev_get_drvdata(device);
305 return sysfs_emit(buf, "%d\n", fb_info->fix.line_length);
308 static ssize_t store_blank(struct device *device,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
312 struct fb_info *fb_info = dev_get_drvdata(device);
316 arg = simple_strtoul(buf, &last, 0);
318 err = fb_blank(fb_info, arg);
319 /* might again call into fb_blank */
320 fbcon_fb_blanked(fb_info, arg);
327 static ssize_t show_blank(struct device *device,
328 struct device_attribute *attr, char *buf)
330 // struct fb_info *fb_info = dev_get_drvdata(device);
334 static ssize_t store_console(struct device *device,
335 struct device_attribute *attr,
336 const char *buf, size_t count)
338 // struct fb_info *fb_info = dev_get_drvdata(device);
342 static ssize_t show_console(struct device *device,
343 struct device_attribute *attr, char *buf)
345 // struct fb_info *fb_info = dev_get_drvdata(device);
349 static ssize_t store_cursor(struct device *device,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
353 // struct fb_info *fb_info = dev_get_drvdata(device);
357 static ssize_t show_cursor(struct device *device,
358 struct device_attribute *attr, char *buf)
360 // struct fb_info *fb_info = dev_get_drvdata(device);
364 static ssize_t store_pan(struct device *device,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
368 struct fb_info *fb_info = dev_get_drvdata(device);
369 struct fb_var_screeninfo var;
374 var.xoffset = simple_strtoul(buf, &last, 0);
376 if (last - buf >= count)
378 var.yoffset = simple_strtoul(last, &last, 0);
381 err = fb_pan_display(fb_info, &var);
389 static ssize_t show_pan(struct device *device,
390 struct device_attribute *attr, char *buf)
392 struct fb_info *fb_info = dev_get_drvdata(device);
393 return sysfs_emit(buf, "%d,%d\n", fb_info->var.xoffset,
394 fb_info->var.yoffset);
397 static ssize_t show_name(struct device *device,
398 struct device_attribute *attr, char *buf)
400 struct fb_info *fb_info = dev_get_drvdata(device);
402 return sysfs_emit(buf, "%s\n", fb_info->fix.id);
405 static ssize_t store_fbstate(struct device *device,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
409 struct fb_info *fb_info = dev_get_drvdata(device);
413 state = simple_strtoul(buf, &last, 0);
416 lock_fb_info(fb_info);
418 fb_set_suspend(fb_info, (int)state);
420 unlock_fb_info(fb_info);
426 static ssize_t show_fbstate(struct device *device,
427 struct device_attribute *attr, char *buf)
429 struct fb_info *fb_info = dev_get_drvdata(device);
430 return sysfs_emit(buf, "%d\n", fb_info->state);
433 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
434 static ssize_t store_bl_curve(struct device *device,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
438 struct fb_info *fb_info = dev_get_drvdata(device);
439 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
442 /* Some drivers don't use framebuffer_alloc(), but those also
443 * don't have backlights.
445 if (!fb_info || !fb_info->bl_dev)
448 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
451 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
452 if (sscanf(&buf[i * 24],
453 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
454 &tmp_curve[i * 8 + 0],
455 &tmp_curve[i * 8 + 1],
456 &tmp_curve[i * 8 + 2],
457 &tmp_curve[i * 8 + 3],
458 &tmp_curve[i * 8 + 4],
459 &tmp_curve[i * 8 + 5],
460 &tmp_curve[i * 8 + 6],
461 &tmp_curve[i * 8 + 7]) != 8)
464 /* If there has been an error in the input data, we won't
467 mutex_lock(&fb_info->bl_curve_mutex);
468 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
469 fb_info->bl_curve[i] = tmp_curve[i];
470 mutex_unlock(&fb_info->bl_curve_mutex);
475 static ssize_t show_bl_curve(struct device *device,
476 struct device_attribute *attr, char *buf)
478 struct fb_info *fb_info = dev_get_drvdata(device);
482 /* Some drivers don't use framebuffer_alloc(), but those also
483 * don't have backlights.
485 if (!fb_info || !fb_info->bl_dev)
488 mutex_lock(&fb_info->bl_curve_mutex);
489 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
490 len += scnprintf(&buf[len], PAGE_SIZE - len, "%8ph\n",
491 fb_info->bl_curve + i);
492 mutex_unlock(&fb_info->bl_curve_mutex);
498 /* When cmap is added back in it should be a binary attribute
499 * not a text one. Consideration should also be given to converting
500 * fbdev to use configfs instead of sysfs */
501 static struct device_attribute device_attrs[] = {
502 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
503 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
504 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
505 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
506 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
507 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
508 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
509 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
510 __ATTR(name, S_IRUGO, show_name, NULL),
511 __ATTR(stride, S_IRUGO, show_stride, NULL),
512 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
513 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
514 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
515 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
519 int fb_init_device(struct fb_info *fb_info)
523 dev_set_drvdata(fb_info->dev, fb_info);
525 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
527 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
528 error = device_create_file(fb_info->dev, &device_attrs[i]);
536 device_remove_file(fb_info->dev, &device_attrs[i]);
537 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
543 void fb_cleanup_device(struct fb_info *fb_info)
547 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
548 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
549 device_remove_file(fb_info->dev, &device_attrs[i]);
551 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
555 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
556 /* This function generates a linear backlight curve
560 * 8-127: linear from min to max
562 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
564 unsigned int i, flat, count, range = (max - min);
566 mutex_lock(&fb_info->bl_curve_mutex);
568 fb_info->bl_curve[0] = off;
570 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
571 fb_info->bl_curve[flat] = min;
573 count = FB_BACKLIGHT_LEVELS * 15 / 16;
574 for (i = 0; i < count; ++i)
575 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
577 mutex_unlock(&fb_info->bl_curve_mutex);
579 EXPORT_SYMBOL_GPL(fb_bl_default_curve);