1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * fbsysfs.c - framebuffer device class and attributes 4 * 5 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org> 6 */ 7 8 /* 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. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/slab.h> 16 #include <linux/fb.h> 17 #include <linux/fbcon.h> 18 #include <linux/console.h> 19 #include <linux/module.h> 20 21 #define FB_SYSFS_FLAG_ATTR 1 22 23 /** 24 * framebuffer_alloc - creates a new frame buffer info structure 25 * 26 * @size: size of driver private data, can be zero 27 * @dev: pointer to the device for this fb, this can be NULL 28 * 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). 32 * 33 * Returns the new structure, or NULL if an error occurred. 34 * 35 */ 36 struct fb_info *framebuffer_alloc(size_t size, struct device *dev) 37 { 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); 41 struct fb_info *info; 42 char *p; 43 44 if (size) 45 fb_info_size += PADDING; 46 47 p = kzalloc(fb_info_size + size, GFP_KERNEL); 48 49 if (!p) 50 return NULL; 51 52 info = (struct fb_info *) p; 53 54 if (size) 55 info->par = p + fb_info_size; 56 57 info->device = dev; 58 info->fbcon_rotate_hint = -1; 59 60 #if IS_ENABLED(CONFIG_FB_BACKLIGHT) 61 mutex_init(&info->bl_curve_mutex); 62 #endif 63 64 return info; 65 #undef PADDING 66 #undef BYTES_PER_LONG 67 } 68 EXPORT_SYMBOL(framebuffer_alloc); 69 70 /** 71 * framebuffer_release - marks the structure available for freeing 72 * 73 * @info: frame buffer info structure 74 * 75 * Drop the reference count of the device embedded in the 76 * framebuffer info structure. 77 * 78 */ 79 void framebuffer_release(struct fb_info *info) 80 { 81 if (!info) 82 return; 83 84 if (WARN_ON(refcount_read(&info->count))) 85 return; 86 87 kfree(info->apertures); 88 kfree(info); 89 } 90 EXPORT_SYMBOL(framebuffer_release); 91 92 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var) 93 { 94 int err; 95 96 var->activate |= FB_ACTIVATE_FORCE; 97 console_lock(); 98 err = fb_set_var(fb_info, var); 99 if (!err) 100 fbcon_update_vcs(fb_info, var->activate & FB_ACTIVATE_ALL); 101 console_unlock(); 102 if (err) 103 return err; 104 return 0; 105 } 106 107 static int mode_string(char *buf, unsigned int offset, 108 const struct fb_videomode *mode) 109 { 110 char m = 'U'; 111 char v = 'p'; 112 113 if (mode->flag & FB_MODE_IS_DETAILED) 114 m = 'D'; 115 if (mode->flag & FB_MODE_IS_VESA) 116 m = 'V'; 117 if (mode->flag & FB_MODE_IS_STANDARD) 118 m = 'S'; 119 120 if (mode->vmode & FB_VMODE_INTERLACED) 121 v = 'i'; 122 if (mode->vmode & FB_VMODE_DOUBLE) 123 v = 'd'; 124 125 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n", 126 m, mode->xres, mode->yres, v, mode->refresh); 127 } 128 129 static ssize_t store_mode(struct device *device, struct device_attribute *attr, 130 const char *buf, size_t count) 131 { 132 struct fb_info *fb_info = dev_get_drvdata(device); 133 char mstr[100]; 134 struct fb_var_screeninfo var; 135 struct fb_modelist *modelist; 136 struct fb_videomode *mode; 137 struct list_head *pos; 138 size_t i; 139 int err; 140 141 memset(&var, 0, sizeof(var)); 142 143 list_for_each(pos, &fb_info->modelist) { 144 modelist = list_entry(pos, struct fb_modelist, list); 145 mode = &modelist->mode; 146 i = mode_string(mstr, 0, mode); 147 if (strncmp(mstr, buf, max(count, i)) == 0) { 148 149 var = fb_info->var; 150 fb_videomode_to_var(&var, mode); 151 if ((err = activate(fb_info, &var))) 152 return err; 153 fb_info->mode = mode; 154 return count; 155 } 156 } 157 return -EINVAL; 158 } 159 160 static ssize_t show_mode(struct device *device, struct device_attribute *attr, 161 char *buf) 162 { 163 struct fb_info *fb_info = dev_get_drvdata(device); 164 165 if (!fb_info->mode) 166 return 0; 167 168 return mode_string(buf, 0, fb_info->mode); 169 } 170 171 static ssize_t store_modes(struct device *device, 172 struct device_attribute *attr, 173 const char *buf, size_t count) 174 { 175 struct fb_info *fb_info = dev_get_drvdata(device); 176 LIST_HEAD(old_list); 177 int i = count / sizeof(struct fb_videomode); 178 179 if (i * sizeof(struct fb_videomode) != count) 180 return -EINVAL; 181 182 console_lock(); 183 lock_fb_info(fb_info); 184 185 list_splice(&fb_info->modelist, &old_list); 186 fb_videomode_to_modelist((const struct fb_videomode *)buf, i, 187 &fb_info->modelist); 188 if (fb_new_modelist(fb_info)) { 189 fb_destroy_modelist(&fb_info->modelist); 190 list_splice(&old_list, &fb_info->modelist); 191 } else 192 fb_destroy_modelist(&old_list); 193 194 unlock_fb_info(fb_info); 195 console_unlock(); 196 197 return 0; 198 } 199 200 static ssize_t show_modes(struct device *device, struct device_attribute *attr, 201 char *buf) 202 { 203 struct fb_info *fb_info = dev_get_drvdata(device); 204 unsigned int i; 205 struct list_head *pos; 206 struct fb_modelist *modelist; 207 const struct fb_videomode *mode; 208 209 i = 0; 210 list_for_each(pos, &fb_info->modelist) { 211 modelist = list_entry(pos, struct fb_modelist, list); 212 mode = &modelist->mode; 213 i += mode_string(buf, i, mode); 214 } 215 return i; 216 } 217 218 static ssize_t store_bpp(struct device *device, struct device_attribute *attr, 219 const char *buf, size_t count) 220 { 221 struct fb_info *fb_info = dev_get_drvdata(device); 222 struct fb_var_screeninfo var; 223 char ** last = NULL; 224 int err; 225 226 var = fb_info->var; 227 var.bits_per_pixel = simple_strtoul(buf, last, 0); 228 if ((err = activate(fb_info, &var))) 229 return err; 230 return count; 231 } 232 233 static ssize_t show_bpp(struct device *device, struct device_attribute *attr, 234 char *buf) 235 { 236 struct fb_info *fb_info = dev_get_drvdata(device); 237 return sysfs_emit(buf, "%d\n", fb_info->var.bits_per_pixel); 238 } 239 240 static ssize_t store_rotate(struct device *device, 241 struct device_attribute *attr, 242 const char *buf, size_t count) 243 { 244 struct fb_info *fb_info = dev_get_drvdata(device); 245 struct fb_var_screeninfo var; 246 char **last = NULL; 247 int err; 248 249 var = fb_info->var; 250 var.rotate = simple_strtoul(buf, last, 0); 251 252 if ((err = activate(fb_info, &var))) 253 return err; 254 255 return count; 256 } 257 258 259 static ssize_t show_rotate(struct device *device, 260 struct device_attribute *attr, char *buf) 261 { 262 struct fb_info *fb_info = dev_get_drvdata(device); 263 264 return sysfs_emit(buf, "%d\n", fb_info->var.rotate); 265 } 266 267 static ssize_t store_virtual(struct device *device, 268 struct device_attribute *attr, 269 const char *buf, size_t count) 270 { 271 struct fb_info *fb_info = dev_get_drvdata(device); 272 struct fb_var_screeninfo var; 273 char *last = NULL; 274 int err; 275 276 var = fb_info->var; 277 var.xres_virtual = simple_strtoul(buf, &last, 0); 278 last++; 279 if (last - buf >= count) 280 return -EINVAL; 281 var.yres_virtual = simple_strtoul(last, &last, 0); 282 283 if ((err = activate(fb_info, &var))) 284 return err; 285 return count; 286 } 287 288 static ssize_t show_virtual(struct device *device, 289 struct device_attribute *attr, char *buf) 290 { 291 struct fb_info *fb_info = dev_get_drvdata(device); 292 return sysfs_emit(buf, "%d,%d\n", fb_info->var.xres_virtual, 293 fb_info->var.yres_virtual); 294 } 295 296 static ssize_t show_stride(struct device *device, 297 struct device_attribute *attr, char *buf) 298 { 299 struct fb_info *fb_info = dev_get_drvdata(device); 300 return sysfs_emit(buf, "%d\n", fb_info->fix.line_length); 301 } 302 303 static ssize_t store_blank(struct device *device, 304 struct device_attribute *attr, 305 const char *buf, size_t count) 306 { 307 struct fb_info *fb_info = dev_get_drvdata(device); 308 char *last = NULL; 309 int err, arg; 310 311 arg = simple_strtoul(buf, &last, 0); 312 console_lock(); 313 err = fb_blank(fb_info, arg); 314 /* might again call into fb_blank */ 315 fbcon_fb_blanked(fb_info, arg); 316 console_unlock(); 317 if (err < 0) 318 return err; 319 return count; 320 } 321 322 static ssize_t show_blank(struct device *device, 323 struct device_attribute *attr, char *buf) 324 { 325 // struct fb_info *fb_info = dev_get_drvdata(device); 326 return 0; 327 } 328 329 static ssize_t store_console(struct device *device, 330 struct device_attribute *attr, 331 const char *buf, size_t count) 332 { 333 // struct fb_info *fb_info = dev_get_drvdata(device); 334 return 0; 335 } 336 337 static ssize_t show_console(struct device *device, 338 struct device_attribute *attr, char *buf) 339 { 340 // struct fb_info *fb_info = dev_get_drvdata(device); 341 return 0; 342 } 343 344 static ssize_t store_cursor(struct device *device, 345 struct device_attribute *attr, 346 const char *buf, size_t count) 347 { 348 // struct fb_info *fb_info = dev_get_drvdata(device); 349 return 0; 350 } 351 352 static ssize_t show_cursor(struct device *device, 353 struct device_attribute *attr, char *buf) 354 { 355 // struct fb_info *fb_info = dev_get_drvdata(device); 356 return 0; 357 } 358 359 static ssize_t store_pan(struct device *device, 360 struct device_attribute *attr, 361 const char *buf, size_t count) 362 { 363 struct fb_info *fb_info = dev_get_drvdata(device); 364 struct fb_var_screeninfo var; 365 char *last = NULL; 366 int err; 367 368 var = fb_info->var; 369 var.xoffset = simple_strtoul(buf, &last, 0); 370 last++; 371 if (last - buf >= count) 372 return -EINVAL; 373 var.yoffset = simple_strtoul(last, &last, 0); 374 375 console_lock(); 376 err = fb_pan_display(fb_info, &var); 377 console_unlock(); 378 379 if (err < 0) 380 return err; 381 return count; 382 } 383 384 static ssize_t show_pan(struct device *device, 385 struct device_attribute *attr, char *buf) 386 { 387 struct fb_info *fb_info = dev_get_drvdata(device); 388 return sysfs_emit(buf, "%d,%d\n", fb_info->var.xoffset, 389 fb_info->var.yoffset); 390 } 391 392 static ssize_t show_name(struct device *device, 393 struct device_attribute *attr, char *buf) 394 { 395 struct fb_info *fb_info = dev_get_drvdata(device); 396 397 return sysfs_emit(buf, "%s\n", fb_info->fix.id); 398 } 399 400 static ssize_t store_fbstate(struct device *device, 401 struct device_attribute *attr, 402 const char *buf, size_t count) 403 { 404 struct fb_info *fb_info = dev_get_drvdata(device); 405 u32 state; 406 char *last = NULL; 407 408 state = simple_strtoul(buf, &last, 0); 409 410 console_lock(); 411 lock_fb_info(fb_info); 412 413 fb_set_suspend(fb_info, (int)state); 414 415 unlock_fb_info(fb_info); 416 console_unlock(); 417 418 return count; 419 } 420 421 static ssize_t show_fbstate(struct device *device, 422 struct device_attribute *attr, char *buf) 423 { 424 struct fb_info *fb_info = dev_get_drvdata(device); 425 return sysfs_emit(buf, "%d\n", fb_info->state); 426 } 427 428 #if IS_ENABLED(CONFIG_FB_BACKLIGHT) 429 static ssize_t store_bl_curve(struct device *device, 430 struct device_attribute *attr, 431 const char *buf, size_t count) 432 { 433 struct fb_info *fb_info = dev_get_drvdata(device); 434 u8 tmp_curve[FB_BACKLIGHT_LEVELS]; 435 unsigned int i; 436 437 /* Some drivers don't use framebuffer_alloc(), but those also 438 * don't have backlights. 439 */ 440 if (!fb_info || !fb_info->bl_dev) 441 return -ENODEV; 442 443 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24)) 444 return -EINVAL; 445 446 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i) 447 if (sscanf(&buf[i * 24], 448 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n", 449 &tmp_curve[i * 8 + 0], 450 &tmp_curve[i * 8 + 1], 451 &tmp_curve[i * 8 + 2], 452 &tmp_curve[i * 8 + 3], 453 &tmp_curve[i * 8 + 4], 454 &tmp_curve[i * 8 + 5], 455 &tmp_curve[i * 8 + 6], 456 &tmp_curve[i * 8 + 7]) != 8) 457 return -EINVAL; 458 459 /* If there has been an error in the input data, we won't 460 * reach this loop. 461 */ 462 mutex_lock(&fb_info->bl_curve_mutex); 463 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i) 464 fb_info->bl_curve[i] = tmp_curve[i]; 465 mutex_unlock(&fb_info->bl_curve_mutex); 466 467 return count; 468 } 469 470 static ssize_t show_bl_curve(struct device *device, 471 struct device_attribute *attr, char *buf) 472 { 473 struct fb_info *fb_info = dev_get_drvdata(device); 474 ssize_t len = 0; 475 unsigned int i; 476 477 /* Some drivers don't use framebuffer_alloc(), but those also 478 * don't have backlights. 479 */ 480 if (!fb_info || !fb_info->bl_dev) 481 return -ENODEV; 482 483 mutex_lock(&fb_info->bl_curve_mutex); 484 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8) 485 len += scnprintf(&buf[len], PAGE_SIZE - len, "%8ph\n", 486 fb_info->bl_curve + i); 487 mutex_unlock(&fb_info->bl_curve_mutex); 488 489 return len; 490 } 491 #endif 492 493 /* When cmap is added back in it should be a binary attribute 494 * not a text one. Consideration should also be given to converting 495 * fbdev to use configfs instead of sysfs */ 496 static struct device_attribute device_attrs[] = { 497 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp), 498 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank), 499 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console), 500 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor), 501 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode), 502 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes), 503 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan), 504 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual), 505 __ATTR(name, S_IRUGO, show_name, NULL), 506 __ATTR(stride, S_IRUGO, show_stride, NULL), 507 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate), 508 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate), 509 #if IS_ENABLED(CONFIG_FB_BACKLIGHT) 510 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve), 511 #endif 512 }; 513 514 int fb_init_device(struct fb_info *fb_info) 515 { 516 int i, error = 0; 517 518 dev_set_drvdata(fb_info->dev, fb_info); 519 520 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR; 521 522 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) { 523 error = device_create_file(fb_info->dev, &device_attrs[i]); 524 525 if (error) 526 break; 527 } 528 529 if (error) { 530 while (--i >= 0) 531 device_remove_file(fb_info->dev, &device_attrs[i]); 532 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR; 533 } 534 535 return 0; 536 } 537 538 void fb_cleanup_device(struct fb_info *fb_info) 539 { 540 unsigned int i; 541 542 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) { 543 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) 544 device_remove_file(fb_info->dev, &device_attrs[i]); 545 546 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR; 547 } 548 } 549 550 #if IS_ENABLED(CONFIG_FB_BACKLIGHT) 551 /* This function generates a linear backlight curve 552 * 553 * 0: off 554 * 1-7: min 555 * 8-127: linear from min to max 556 */ 557 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max) 558 { 559 unsigned int i, flat, count, range = (max - min); 560 561 mutex_lock(&fb_info->bl_curve_mutex); 562 563 fb_info->bl_curve[0] = off; 564 565 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat) 566 fb_info->bl_curve[flat] = min; 567 568 count = FB_BACKLIGHT_LEVELS * 15 / 16; 569 for (i = 0; i < count; ++i) 570 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count); 571 572 mutex_unlock(&fb_info->bl_curve_mutex); 573 } 574 EXPORT_SYMBOL_GPL(fb_bl_default_curve); 575 #endif 576