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 kfree(info->apertures);
90 EXPORT_SYMBOL(framebuffer_release);
92 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
96 var->activate |= FB_ACTIVATE_FORCE;
98 lock_fb_info(fb_info);
99 err = fb_set_var(fb_info, var);
101 fbcon_update_vcs(fb_info, var->activate & FB_ACTIVATE_ALL);
102 unlock_fb_info(fb_info);
109 static int mode_string(char *buf, unsigned int offset,
110 const struct fb_videomode *mode)
115 if (mode->flag & FB_MODE_IS_DETAILED)
117 if (mode->flag & FB_MODE_IS_VESA)
119 if (mode->flag & FB_MODE_IS_STANDARD)
122 if (mode->vmode & FB_VMODE_INTERLACED)
124 if (mode->vmode & FB_VMODE_DOUBLE)
127 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
128 m, mode->xres, mode->yres, v, mode->refresh);
131 static ssize_t store_mode(struct device *device, struct device_attribute *attr,
132 const char *buf, size_t count)
134 struct fb_info *fb_info = dev_get_drvdata(device);
136 struct fb_var_screeninfo var;
137 struct fb_modelist *modelist;
138 struct fb_videomode *mode;
139 struct list_head *pos;
143 memset(&var, 0, sizeof(var));
145 list_for_each(pos, &fb_info->modelist) {
146 modelist = list_entry(pos, struct fb_modelist, list);
147 mode = &modelist->mode;
148 i = mode_string(mstr, 0, mode);
149 if (strncmp(mstr, buf, max(count, i)) == 0) {
152 fb_videomode_to_var(&var, mode);
153 if ((err = activate(fb_info, &var)))
155 fb_info->mode = mode;
162 static ssize_t show_mode(struct device *device, struct device_attribute *attr,
165 struct fb_info *fb_info = dev_get_drvdata(device);
170 return mode_string(buf, 0, fb_info->mode);
173 static ssize_t store_modes(struct device *device,
174 struct device_attribute *attr,
175 const char *buf, size_t count)
177 struct fb_info *fb_info = dev_get_drvdata(device);
179 int i = count / sizeof(struct fb_videomode);
181 if (i * sizeof(struct fb_videomode) != count)
185 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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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);
313 arg = simple_strtoul(buf, &last, 0);
315 err = fb_blank(fb_info, arg);
316 /* might again call into fb_blank */
317 fbcon_fb_blanked(fb_info, arg);
324 static ssize_t show_blank(struct device *device,
325 struct device_attribute *attr, char *buf)
327 // struct fb_info *fb_info = dev_get_drvdata(device);
331 static ssize_t store_console(struct device *device,
332 struct device_attribute *attr,
333 const char *buf, size_t count)
335 // struct fb_info *fb_info = dev_get_drvdata(device);
339 static ssize_t show_console(struct device *device,
340 struct device_attribute *attr, char *buf)
342 // struct fb_info *fb_info = dev_get_drvdata(device);
346 static ssize_t store_cursor(struct device *device,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
350 // struct fb_info *fb_info = dev_get_drvdata(device);
354 static ssize_t show_cursor(struct device *device,
355 struct device_attribute *attr, char *buf)
357 // struct fb_info *fb_info = dev_get_drvdata(device);
361 static ssize_t store_pan(struct device *device,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
365 struct fb_info *fb_info = dev_get_drvdata(device);
366 struct fb_var_screeninfo var;
371 var.xoffset = simple_strtoul(buf, &last, 0);
373 if (last - buf >= count)
375 var.yoffset = simple_strtoul(last, &last, 0);
378 err = fb_pan_display(fb_info, &var);
386 static ssize_t show_pan(struct device *device,
387 struct device_attribute *attr, char *buf)
389 struct fb_info *fb_info = dev_get_drvdata(device);
390 return sysfs_emit(buf, "%d,%d\n", fb_info->var.xoffset,
391 fb_info->var.yoffset);
394 static ssize_t show_name(struct device *device,
395 struct device_attribute *attr, char *buf)
397 struct fb_info *fb_info = dev_get_drvdata(device);
399 return sysfs_emit(buf, "%s\n", fb_info->fix.id);
402 static ssize_t store_fbstate(struct device *device,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
406 struct fb_info *fb_info = dev_get_drvdata(device);
410 state = simple_strtoul(buf, &last, 0);
413 lock_fb_info(fb_info);
415 fb_set_suspend(fb_info, (int)state);
417 unlock_fb_info(fb_info);
423 static ssize_t show_fbstate(struct device *device,
424 struct device_attribute *attr, char *buf)
426 struct fb_info *fb_info = dev_get_drvdata(device);
427 return sysfs_emit(buf, "%d\n", fb_info->state);
430 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
431 static ssize_t store_bl_curve(struct device *device,
432 struct device_attribute *attr,
433 const char *buf, size_t count)
435 struct fb_info *fb_info = dev_get_drvdata(device);
436 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
439 /* Some drivers don't use framebuffer_alloc(), but those also
440 * don't have backlights.
442 if (!fb_info || !fb_info->bl_dev)
445 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
448 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
449 if (sscanf(&buf[i * 24],
450 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
451 &tmp_curve[i * 8 + 0],
452 &tmp_curve[i * 8 + 1],
453 &tmp_curve[i * 8 + 2],
454 &tmp_curve[i * 8 + 3],
455 &tmp_curve[i * 8 + 4],
456 &tmp_curve[i * 8 + 5],
457 &tmp_curve[i * 8 + 6],
458 &tmp_curve[i * 8 + 7]) != 8)
461 /* If there has been an error in the input data, we won't
464 mutex_lock(&fb_info->bl_curve_mutex);
465 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
466 fb_info->bl_curve[i] = tmp_curve[i];
467 mutex_unlock(&fb_info->bl_curve_mutex);
472 static ssize_t show_bl_curve(struct device *device,
473 struct device_attribute *attr, char *buf)
475 struct fb_info *fb_info = dev_get_drvdata(device);
479 /* Some drivers don't use framebuffer_alloc(), but those also
480 * don't have backlights.
482 if (!fb_info || !fb_info->bl_dev)
485 mutex_lock(&fb_info->bl_curve_mutex);
486 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
487 len += scnprintf(&buf[len], PAGE_SIZE - len, "%8ph\n",
488 fb_info->bl_curve + i);
489 mutex_unlock(&fb_info->bl_curve_mutex);
495 /* When cmap is added back in it should be a binary attribute
496 * not a text one. Consideration should also be given to converting
497 * fbdev to use configfs instead of sysfs */
498 static struct device_attribute device_attrs[] = {
499 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
500 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
501 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
502 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
503 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
504 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
505 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
506 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
507 __ATTR(name, S_IRUGO, show_name, NULL),
508 __ATTR(stride, S_IRUGO, show_stride, NULL),
509 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
510 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
511 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
512 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
516 int fb_init_device(struct fb_info *fb_info)
520 dev_set_drvdata(fb_info->dev, fb_info);
522 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
524 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
525 error = device_create_file(fb_info->dev, &device_attrs[i]);
533 device_remove_file(fb_info->dev, &device_attrs[i]);
534 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
540 void fb_cleanup_device(struct fb_info *fb_info)
544 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
545 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
546 device_remove_file(fb_info->dev, &device_attrs[i]);
548 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
552 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
553 /* This function generates a linear backlight curve
557 * 8-127: linear from min to max
559 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
561 unsigned int i, flat, count, range = (max - min);
563 mutex_lock(&fb_info->bl_curve_mutex);
565 fb_info->bl_curve[0] = off;
567 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
568 fb_info->bl_curve[flat] = min;
570 count = FB_BACKLIGHT_LEVELS * 15 / 16;
571 for (i = 0; i < count; ++i)
572 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
574 mutex_unlock(&fb_info->bl_curve_mutex);
576 EXPORT_SYMBOL_GPL(fb_bl_default_curve);