xref: /openbmc/linux/drivers/video/fbdev/core/fb_info.c (revision 050bb587)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/export.h>
4 #include <linux/fb.h>
5 #include <linux/mutex.h>
6 #include <linux/slab.h>
7 
8 /**
9  * framebuffer_alloc - creates a new frame buffer info structure
10  *
11  * @size: size of driver private data, can be zero
12  * @dev: pointer to the device for this fb, this can be NULL
13  *
14  * Creates a new frame buffer info structure. Also reserves @size bytes
15  * for driver private data (info->par). info->par (if any) will be
16  * aligned to sizeof(long).
17  *
18  * Returns the new structure, or NULL if an error occurred.
19  *
20  */
21 struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
22 {
23 #define BYTES_PER_LONG (BITS_PER_LONG/8)
24 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
25 	int fb_info_size = sizeof(struct fb_info);
26 	struct fb_info *info;
27 	char *p;
28 
29 	if (size)
30 		fb_info_size += PADDING;
31 
32 	p = kzalloc(fb_info_size + size, GFP_KERNEL);
33 
34 	if (!p)
35 		return NULL;
36 
37 	info = (struct fb_info *) p;
38 
39 	if (size)
40 		info->par = p + fb_info_size;
41 
42 	info->device = dev;
43 	info->fbcon_rotate_hint = -1;
44 
45 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
46 	mutex_init(&info->bl_curve_mutex);
47 #endif
48 
49 	return info;
50 #undef PADDING
51 #undef BYTES_PER_LONG
52 }
53 EXPORT_SYMBOL(framebuffer_alloc);
54 
55 /**
56  * framebuffer_release - marks the structure available for freeing
57  *
58  * @info: frame buffer info structure
59  *
60  * Drop the reference count of the device embedded in the
61  * framebuffer info structure.
62  *
63  */
64 void framebuffer_release(struct fb_info *info)
65 {
66 	if (!info)
67 		return;
68 
69 	if (WARN_ON(refcount_read(&info->count)))
70 		return;
71 
72 #if IS_ENABLED(CONFIG_FB_BACKLIGHT)
73 	mutex_destroy(&info->bl_curve_mutex);
74 #endif
75 
76 	kfree(info);
77 }
78 EXPORT_SYMBOL(framebuffer_release);
79