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