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