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