xref: /openbmc/linux/drivers/video/fbdev/core/fbsysfs.c (revision f4356947)
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 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
88 	mutex_destroy(&info->bl_curve_mutex);
89 #endif
90 
91 	kfree(info);
92 }
93 EXPORT_SYMBOL(framebuffer_release);
94 
95 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
96 {
97 	int err;
98 
99 	var->activate |= FB_ACTIVATE_FORCE;
100 	console_lock();
101 	lock_fb_info(fb_info);
102 	err = fb_set_var(fb_info, var);
103 	if (!err)
104 		fbcon_update_vcs(fb_info, var->activate & FB_ACTIVATE_ALL);
105 	unlock_fb_info(fb_info);
106 	console_unlock();
107 	if (err)
108 		return err;
109 	return 0;
110 }
111 
112 static int mode_string(char *buf, unsigned int offset,
113 		       const struct fb_videomode *mode)
114 {
115 	char m = 'U';
116 	char v = 'p';
117 
118 	if (mode->flag & FB_MODE_IS_DETAILED)
119 		m = 'D';
120 	if (mode->flag & FB_MODE_IS_VESA)
121 		m = 'V';
122 	if (mode->flag & FB_MODE_IS_STANDARD)
123 		m = 'S';
124 
125 	if (mode->vmode & FB_VMODE_INTERLACED)
126 		v = 'i';
127 	if (mode->vmode & FB_VMODE_DOUBLE)
128 		v = 'd';
129 
130 	return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
131 	                m, mode->xres, mode->yres, v, mode->refresh);
132 }
133 
134 static ssize_t store_mode(struct device *device, struct device_attribute *attr,
135 			  const char *buf, size_t count)
136 {
137 	struct fb_info *fb_info = dev_get_drvdata(device);
138 	char mstr[100];
139 	struct fb_var_screeninfo var;
140 	struct fb_modelist *modelist;
141 	struct fb_videomode *mode;
142 	struct list_head *pos;
143 	size_t i;
144 	int err;
145 
146 	memset(&var, 0, sizeof(var));
147 
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) {
153 
154 			var = fb_info->var;
155 			fb_videomode_to_var(&var, mode);
156 			if ((err = activate(fb_info, &var)))
157 				return err;
158 			fb_info->mode = mode;
159 			return count;
160 		}
161 	}
162 	return -EINVAL;
163 }
164 
165 static ssize_t show_mode(struct device *device, struct device_attribute *attr,
166 			 char *buf)
167 {
168 	struct fb_info *fb_info = dev_get_drvdata(device);
169 
170 	if (!fb_info->mode)
171 		return 0;
172 
173 	return mode_string(buf, 0, fb_info->mode);
174 }
175 
176 static ssize_t store_modes(struct device *device,
177 			   struct device_attribute *attr,
178 			   const char *buf, size_t count)
179 {
180 	struct fb_info *fb_info = dev_get_drvdata(device);
181 	LIST_HEAD(old_list);
182 	int i = count / sizeof(struct fb_videomode);
183 
184 	if (i * sizeof(struct fb_videomode) != count)
185 		return -EINVAL;
186 
187 	console_lock();
188 	lock_fb_info(fb_info);
189 
190 	list_splice(&fb_info->modelist, &old_list);
191 	fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
192 				 &fb_info->modelist);
193 	if (fb_new_modelist(fb_info)) {
194 		fb_destroy_modelist(&fb_info->modelist);
195 		list_splice(&old_list, &fb_info->modelist);
196 	} else
197 		fb_destroy_modelist(&old_list);
198 
199 	unlock_fb_info(fb_info);
200 	console_unlock();
201 
202 	return 0;
203 }
204 
205 static ssize_t show_modes(struct device *device, struct device_attribute *attr,
206 			  char *buf)
207 {
208 	struct fb_info *fb_info = dev_get_drvdata(device);
209 	unsigned int i;
210 	struct list_head *pos;
211 	struct fb_modelist *modelist;
212 	const struct fb_videomode *mode;
213 
214 	i = 0;
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);
219 	}
220 	return i;
221 }
222 
223 static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
224 			 const char *buf, size_t count)
225 {
226 	struct fb_info *fb_info = dev_get_drvdata(device);
227 	struct fb_var_screeninfo var;
228 	char ** last = NULL;
229 	int err;
230 
231 	var = fb_info->var;
232 	var.bits_per_pixel = simple_strtoul(buf, last, 0);
233 	if ((err = activate(fb_info, &var)))
234 		return err;
235 	return count;
236 }
237 
238 static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
239 			char *buf)
240 {
241 	struct fb_info *fb_info = dev_get_drvdata(device);
242 	return sysfs_emit(buf, "%d\n", fb_info->var.bits_per_pixel);
243 }
244 
245 static ssize_t store_rotate(struct device *device,
246 			    struct device_attribute *attr,
247 			    const char *buf, size_t count)
248 {
249 	struct fb_info *fb_info = dev_get_drvdata(device);
250 	struct fb_var_screeninfo var;
251 	char **last = NULL;
252 	int err;
253 
254 	var = fb_info->var;
255 	var.rotate = simple_strtoul(buf, last, 0);
256 
257 	if ((err = activate(fb_info, &var)))
258 		return err;
259 
260 	return count;
261 }
262 
263 
264 static ssize_t show_rotate(struct device *device,
265 			   struct device_attribute *attr, char *buf)
266 {
267 	struct fb_info *fb_info = dev_get_drvdata(device);
268 
269 	return sysfs_emit(buf, "%d\n", fb_info->var.rotate);
270 }
271 
272 static ssize_t store_virtual(struct device *device,
273 			     struct device_attribute *attr,
274 			     const char *buf, size_t count)
275 {
276 	struct fb_info *fb_info = dev_get_drvdata(device);
277 	struct fb_var_screeninfo var;
278 	char *last = NULL;
279 	int err;
280 
281 	var = fb_info->var;
282 	var.xres_virtual = simple_strtoul(buf, &last, 0);
283 	last++;
284 	if (last - buf >= count)
285 		return -EINVAL;
286 	var.yres_virtual = simple_strtoul(last, &last, 0);
287 
288 	if ((err = activate(fb_info, &var)))
289 		return err;
290 	return count;
291 }
292 
293 static ssize_t show_virtual(struct device *device,
294 			    struct device_attribute *attr, char *buf)
295 {
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);
299 }
300 
301 static ssize_t show_stride(struct device *device,
302 			   struct device_attribute *attr, char *buf)
303 {
304 	struct fb_info *fb_info = dev_get_drvdata(device);
305 	return sysfs_emit(buf, "%d\n", fb_info->fix.line_length);
306 }
307 
308 static ssize_t store_blank(struct device *device,
309 			   struct device_attribute *attr,
310 			   const char *buf, size_t count)
311 {
312 	struct fb_info *fb_info = dev_get_drvdata(device);
313 	char *last = NULL;
314 	int err, arg;
315 
316 	arg = simple_strtoul(buf, &last, 0);
317 	console_lock();
318 	err = fb_blank(fb_info, arg);
319 	/* might again call into fb_blank */
320 	fbcon_fb_blanked(fb_info, arg);
321 	console_unlock();
322 	if (err < 0)
323 		return err;
324 	return count;
325 }
326 
327 static ssize_t show_blank(struct device *device,
328 			  struct device_attribute *attr, char *buf)
329 {
330 //	struct fb_info *fb_info = dev_get_drvdata(device);
331 	return 0;
332 }
333 
334 static ssize_t store_console(struct device *device,
335 			     struct device_attribute *attr,
336 			     const char *buf, size_t count)
337 {
338 //	struct fb_info *fb_info = dev_get_drvdata(device);
339 	return 0;
340 }
341 
342 static ssize_t show_console(struct device *device,
343 			    struct device_attribute *attr, char *buf)
344 {
345 //	struct fb_info *fb_info = dev_get_drvdata(device);
346 	return 0;
347 }
348 
349 static ssize_t store_cursor(struct device *device,
350 			    struct device_attribute *attr,
351 			    const char *buf, size_t count)
352 {
353 //	struct fb_info *fb_info = dev_get_drvdata(device);
354 	return 0;
355 }
356 
357 static ssize_t show_cursor(struct device *device,
358 			   struct device_attribute *attr, char *buf)
359 {
360 //	struct fb_info *fb_info = dev_get_drvdata(device);
361 	return 0;
362 }
363 
364 static ssize_t store_pan(struct device *device,
365 			 struct device_attribute *attr,
366 			 const char *buf, size_t count)
367 {
368 	struct fb_info *fb_info = dev_get_drvdata(device);
369 	struct fb_var_screeninfo var;
370 	char *last = NULL;
371 	int err;
372 
373 	var = fb_info->var;
374 	var.xoffset = simple_strtoul(buf, &last, 0);
375 	last++;
376 	if (last - buf >= count)
377 		return -EINVAL;
378 	var.yoffset = simple_strtoul(last, &last, 0);
379 
380 	console_lock();
381 	err = fb_pan_display(fb_info, &var);
382 	console_unlock();
383 
384 	if (err < 0)
385 		return err;
386 	return count;
387 }
388 
389 static ssize_t show_pan(struct device *device,
390 			struct device_attribute *attr, char *buf)
391 {
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);
395 }
396 
397 static ssize_t show_name(struct device *device,
398 			 struct device_attribute *attr, char *buf)
399 {
400 	struct fb_info *fb_info = dev_get_drvdata(device);
401 
402 	return sysfs_emit(buf, "%s\n", fb_info->fix.id);
403 }
404 
405 static ssize_t store_fbstate(struct device *device,
406 			     struct device_attribute *attr,
407 			     const char *buf, size_t count)
408 {
409 	struct fb_info *fb_info = dev_get_drvdata(device);
410 	u32 state;
411 	char *last = NULL;
412 
413 	state = simple_strtoul(buf, &last, 0);
414 
415 	console_lock();
416 	lock_fb_info(fb_info);
417 
418 	fb_set_suspend(fb_info, (int)state);
419 
420 	unlock_fb_info(fb_info);
421 	console_unlock();
422 
423 	return count;
424 }
425 
426 static ssize_t show_fbstate(struct device *device,
427 			    struct device_attribute *attr, char *buf)
428 {
429 	struct fb_info *fb_info = dev_get_drvdata(device);
430 	return sysfs_emit(buf, "%d\n", fb_info->state);
431 }
432 
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)
437 {
438 	struct fb_info *fb_info = dev_get_drvdata(device);
439 	u8 tmp_curve[FB_BACKLIGHT_LEVELS];
440 	unsigned int i;
441 
442 	/* Some drivers don't use framebuffer_alloc(), but those also
443 	 * don't have backlights.
444 	 */
445 	if (!fb_info || !fb_info->bl_dev)
446 		return -ENODEV;
447 
448 	if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
449 		return -EINVAL;
450 
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)
462 			return -EINVAL;
463 
464 	/* If there has been an error in the input data, we won't
465 	 * reach this loop.
466 	 */
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);
471 
472 	return count;
473 }
474 
475 static ssize_t show_bl_curve(struct device *device,
476 			     struct device_attribute *attr, char *buf)
477 {
478 	struct fb_info *fb_info = dev_get_drvdata(device);
479 	ssize_t len = 0;
480 	unsigned int i;
481 
482 	/* Some drivers don't use framebuffer_alloc(), but those also
483 	 * don't have backlights.
484 	 */
485 	if (!fb_info || !fb_info->bl_dev)
486 		return -ENODEV;
487 
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);
493 
494 	return len;
495 }
496 #endif
497 
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),
516 #endif
517 };
518 
519 int fb_init_device(struct fb_info *fb_info)
520 {
521 	int i, error = 0;
522 
523 	dev_set_drvdata(fb_info->dev, fb_info);
524 
525 	fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
526 
527 	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
528 		error = device_create_file(fb_info->dev, &device_attrs[i]);
529 
530 		if (error)
531 			break;
532 	}
533 
534 	if (error) {
535 		while (--i >= 0)
536 			device_remove_file(fb_info->dev, &device_attrs[i]);
537 		fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
538 	}
539 
540 	return 0;
541 }
542 
543 void fb_cleanup_device(struct fb_info *fb_info)
544 {
545 	unsigned int i;
546 
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]);
550 
551 		fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
552 	}
553 }
554 
555 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
556 /* This function generates a linear backlight curve
557  *
558  *     0: off
559  *   1-7: min
560  * 8-127: linear from min to max
561  */
562 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
563 {
564 	unsigned int i, flat, count, range = (max - min);
565 
566 	mutex_lock(&fb_info->bl_curve_mutex);
567 
568 	fb_info->bl_curve[0] = off;
569 
570 	for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
571 		fb_info->bl_curve[flat] = min;
572 
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);
576 
577 	mutex_unlock(&fb_info->bl_curve_mutex);
578 }
579 EXPORT_SYMBOL_GPL(fb_bl_default_curve);
580 #endif
581