blob: d32fc74ff0981cf3fb7465ca1714cbc5d6ca5f8f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * fbsysfs.c - framebuffer device class and attributes
3 *
4 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
5 *
6 * This program is free software you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12/*
13 * Note: currently there's only stubs for framebuffer_alloc and
14 * framebuffer_release here. The reson for that is that until all drivers
15 * are converted to use it a sysfsification will open OOPSable races.
16 */
17
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/fb.h>
21#include <linux/console.h>
22#include <linux/module.h>
23
24#define FB_SYSFS_FLAG_ATTR 1
25
26/**
27 * framebuffer_alloc - creates a new frame buffer info structure
28 *
29 * @size: size of driver private data, can be zero
30 * @dev: pointer to the device for this fb, this can be NULL
31 *
32 * Creates a new frame buffer info structure. Also reserves @size bytes
33 * for driver private data (info->par). info->par (if any) will be
34 * aligned to sizeof(long).
35 *
36 * Returns the new structure, or NULL if an error occurred.
37 *
38 */
39struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
40{
41#define BYTES_PER_LONG (BITS_PER_LONG/8)
42#define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
43 int fb_info_size = sizeof(struct fb_info);
44 struct fb_info *info;
45 char *p;
46
47 if (size)
48 fb_info_size += PADDING;
49
50 p = kzalloc(fb_info_size + size, GFP_KERNEL);
51
52 if (!p)
53 return NULL;
54
55 info = (struct fb_info *) p;
56
57 if (size)
58 info->par = p + fb_info_size;
59
60 info->device = dev;
61
62#ifdef CONFIG_FB_BACKLIGHT
63 mutex_init(&info->bl_curve_mutex);
64#endif
65
66 return info;
67#undef PADDING
68#undef BYTES_PER_LONG
69}
70EXPORT_SYMBOL(framebuffer_alloc);
71
72/**
73 * framebuffer_release - marks the structure available for freeing
74 *
75 * @info: frame buffer info structure
76 *
77 * Drop the reference count of the device embedded in the
78 * framebuffer info structure.
79 *
80 */
81void framebuffer_release(struct fb_info *info)
82{
83 kfree(info->apertures);
84 kfree(info);
85}
86EXPORT_SYMBOL(framebuffer_release);
87
88static 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 fb_info->flags |= FBINFO_MISC_USEREVENT;
95 err = fb_set_var(fb_info, var);
96 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
97 console_unlock();
98 if (err)
99 return err;
100 return 0;
101}
102
103static int mode_string(char *buf, unsigned int offset,
104 const struct fb_videomode *mode)
105{
106 char m = 'U';
107 char v = 'p';
108
109 if (mode->flag & FB_MODE_IS_DETAILED)
110 m = 'D';
111 if (mode->flag & FB_MODE_IS_VESA)
112 m = 'V';
113 if (mode->flag & FB_MODE_IS_STANDARD)
114 m = 'S';
115
116 if (mode->vmode & FB_VMODE_INTERLACED)
117 v = 'i';
118 if (mode->vmode & FB_VMODE_DOUBLE)
119 v = 'd';
120
121 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
122 m, mode->xres, mode->yres, v, mode->refresh);
123}
124
125static ssize_t store_mode(struct device *device, struct device_attribute *attr,
126 const char *buf, size_t count)
127{
128 struct fb_info *fb_info = dev_get_drvdata(device);
129 char mstr[100];
130 struct fb_var_screeninfo var;
131 struct fb_modelist *modelist;
132 struct fb_videomode *mode;
133 struct list_head *pos;
134 size_t i;
135 int err;
136
137 if(!fb_info)
138 return -EINVAL;
139
140 memset(&var, 0, sizeof(var));
141
142 list_for_each(pos, &fb_info->modelist) {
143 modelist = list_entry(pos, struct fb_modelist, list);
144 mode = &modelist->mode;
145 i = mode_string(mstr, 0, mode);
146 if (strncmp(mstr, buf, max(count, i)) == 0) {
147
148 var = fb_info->var;
149 fb_videomode_to_var(&var, mode);
150 if ((err = activate(fb_info, &var)))
151 return err;
152 fb_info->mode = mode;
153 return count;
154 }
155 }
156 return -EINVAL;
157}
158
159static ssize_t show_mode(struct device *device, struct device_attribute *attr,
160 char *buf)
161{
162 struct fb_info *fb_info = dev_get_drvdata(device);
163
164 if(!fb_info)
165 return -EINVAL;
166
167 if (!fb_info->mode)
168 return 0;
169
170 return mode_string(buf, 0, fb_info->mode);
171}
172
173static ssize_t store_modes(struct device *device,
174 struct device_attribute *attr,
175 const char *buf, size_t count)
176{
177 struct fb_info *fb_info = dev_get_drvdata(device);
178 LIST_HEAD(old_list);
179 int i = count / sizeof(struct fb_videomode);
180
181 if (!fb_info || (i * sizeof(struct fb_videomode) != count))
182 return -EINVAL;
183
184 if (!lock_fb_info(fb_info))
185 return -ENODEV;
186 console_lock();
187 list_splice(&fb_info->modelist, &old_list);
188 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
189 &fb_info->modelist);
190 if (fb_new_modelist(fb_info)) {
191 fb_destroy_modelist(&fb_info->modelist);
192 list_splice(&old_list, &fb_info->modelist);
193 } else
194 fb_destroy_modelist(&old_list);
195
196 console_unlock();
197 unlock_fb_info(fb_info);
198
199 return 0;
200}
201
202static ssize_t show_modes(struct device *device, struct device_attribute *attr,
203 char *buf)
204{
205 struct fb_info *fb_info = dev_get_drvdata(device);
206 unsigned int i;
207 struct list_head *pos;
208 struct fb_modelist *modelist;
209 const struct fb_videomode *mode;
210
211 if(!fb_info)
212 return -EINVAL;
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
223static 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 if(!fb_info)
232 return -EINVAL;
233
234 var = fb_info->var;
235 var.bits_per_pixel = simple_strtoul(buf, last, 0);
236 if ((err = activate(fb_info, &var)))
237 return err;
238 return count;
239}
240
241static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
242 char *buf)
243{
244 struct fb_info *fb_info = dev_get_drvdata(device);
245
246 if(!fb_info)
247 return -EINVAL;
248 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
249}
250
251static ssize_t store_rotate(struct device *device,
252 struct device_attribute *attr,
253 const char *buf, size_t count)
254{
255 struct fb_info *fb_info = dev_get_drvdata(device);
256 struct fb_var_screeninfo var;
257 char **last = NULL;
258 int err;
259
260 if(!fb_info)
261 return -EINVAL;
262 var = fb_info->var;
263 var.rotate = simple_strtoul(buf, last, 0);
264
265 if ((err = activate(fb_info, &var)))
266 return err;
267
268 return count;
269}
270
271
272static ssize_t show_rotate(struct device *device,
273 struct device_attribute *attr, char *buf)
274{
275 struct fb_info *fb_info = dev_get_drvdata(device);
276
277 if(!fb_info)
278 return -EINVAL;
279 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
280}
281
282static ssize_t store_virtual(struct device *device,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
285{
286 struct fb_info *fb_info = dev_get_drvdata(device);
287 struct fb_var_screeninfo var;
288 char *last = NULL;
289 int err;
290
291 if(!fb_info)
292 return -EINVAL;
293
294 var = fb_info->var;
295 var.xres_virtual = simple_strtoul(buf, &last, 0);
296 last++;
297 if (last - buf >= count)
298 return -EINVAL;
299 var.yres_virtual = simple_strtoul(last, &last, 0);
300
301 if ((err = activate(fb_info, &var)))
302 return err;
303 return count;
304}
305
306static ssize_t show_virtual(struct device *device,
307 struct device_attribute *attr, char *buf)
308{
309 struct fb_info *fb_info = dev_get_drvdata(device);
310
311 if(!fb_info)
312 return -EINVAL;
313 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
314 fb_info->var.yres_virtual);
315}
316
317static ssize_t show_stride(struct device *device,
318 struct device_attribute *attr, char *buf)
319{
320 struct fb_info *fb_info = dev_get_drvdata(device);
321 if(!fb_info)
322 return -EINVAL;
323
324 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
325}
326
327static ssize_t store_blank(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 char *last = NULL;
333 int err;
334
335 if(!fb_info)
336 return -EINVAL;
337
338 console_lock();
339 fb_info->flags |= FBINFO_MISC_USEREVENT;
340 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
341 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
342 console_unlock();
343 if (err < 0)
344 return err;
345 return count;
346}
347
348static ssize_t show_blank(struct device *device,
349 struct device_attribute *attr, char *buf)
350{
351// struct fb_info *fb_info = dev_get_drvdata(device);
352 return 0;
353}
354
355static ssize_t store_console(struct device *device,
356 struct device_attribute *attr,
357 const char *buf, size_t count)
358{
359// struct fb_info *fb_info = dev_get_drvdata(device);
360 return 0;
361}
362
363static ssize_t show_console(struct device *device,
364 struct device_attribute *attr, char *buf)
365{
366// struct fb_info *fb_info = dev_get_drvdata(device);
367 return 0;
368}
369
370static ssize_t store_cursor(struct device *device,
371 struct device_attribute *attr,
372 const char *buf, size_t count)
373{
374// struct fb_info *fb_info = dev_get_drvdata(device);
375 return 0;
376}
377
378static ssize_t show_cursor(struct device *device,
379 struct device_attribute *attr, char *buf)
380{
381// struct fb_info *fb_info = dev_get_drvdata(device);
382 return 0;
383}
384
385static ssize_t store_pan(struct device *device,
386 struct device_attribute *attr,
387 const char *buf, size_t count)
388{
389 struct fb_info *fb_info = dev_get_drvdata(device);
390 struct fb_var_screeninfo var;
391 char *last = NULL;
392 int err;
393
394 if(!fb_info)
395 return -EINVAL;
396
397 var = fb_info->var;
398 var.xoffset = simple_strtoul(buf, &last, 0);
399 last++;
400 if (last - buf >= count)
401 return -EINVAL;
402 var.yoffset = simple_strtoul(last, &last, 0);
403
404 console_lock();
405 err = fb_pan_display(fb_info, &var);
406 console_unlock();
407
408 if (err < 0)
409 return err;
410 return count;
411}
412
413static ssize_t show_pan(struct device *device,
414 struct device_attribute *attr, char *buf)
415{
416 struct fb_info *fb_info = dev_get_drvdata(device);
417
418 if(!fb_info)
419 return -EINVAL;
420 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
421 fb_info->var.yoffset);
422}
423
424static ssize_t show_name(struct device *device,
425 struct device_attribute *attr, char *buf)
426{
427 struct fb_info *fb_info = dev_get_drvdata(device);
428
429 if(!fb_info)
430 return -EINVAL;
431
432 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
433}
434
435static ssize_t store_fbstate(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 u32 state;
441 char *last = NULL;
442
443 state = simple_strtoul(buf, &last, 0);
444
445 if (!fb_info ||(!lock_fb_info(fb_info)))
446 return -ENODEV;
447 console_lock();
448 fb_set_suspend(fb_info, (int)state);
449 console_unlock();
450 unlock_fb_info(fb_info);
451
452 return count;
453}
454
455static ssize_t show_fbstate(struct device *device,
456 struct device_attribute *attr, char *buf)
457{
458 struct fb_info *fb_info = dev_get_drvdata(device);
459
460 if(!fb_info)
461 return -EINVAL;
462 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
463}
464
465#ifdef CONFIG_FB_BACKLIGHT
466static ssize_t store_bl_curve(struct device *device,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
469{
470 struct fb_info *fb_info = dev_get_drvdata(device);
471 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
472 unsigned int i;
473
474 /* Some drivers don't use framebuffer_alloc(), but those also
475 * don't have backlights.
476 */
477 if (!fb_info || !fb_info->bl_dev)
478 return -ENODEV;
479
480 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
481 return -EINVAL;
482
483 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
484 if (sscanf(&buf[i * 24],
485 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
486 &tmp_curve[i * 8 + 0],
487 &tmp_curve[i * 8 + 1],
488 &tmp_curve[i * 8 + 2],
489 &tmp_curve[i * 8 + 3],
490 &tmp_curve[i * 8 + 4],
491 &tmp_curve[i * 8 + 5],
492 &tmp_curve[i * 8 + 6],
493 &tmp_curve[i * 8 + 7]) != 8)
494 return -EINVAL;
495
496 /* If there has been an error in the input data, we won't
497 * reach this loop.
498 */
499 mutex_lock(&fb_info->bl_curve_mutex);
500 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
501 fb_info->bl_curve[i] = tmp_curve[i];
502 mutex_unlock(&fb_info->bl_curve_mutex);
503
504 return count;
505}
506
507static ssize_t show_bl_curve(struct device *device,
508 struct device_attribute *attr, char *buf)
509{
510 struct fb_info *fb_info = dev_get_drvdata(device);
511 ssize_t len = 0;
512 unsigned int i;
513
514 /* Some drivers don't use framebuffer_alloc(), but those also
515 * don't have backlights.
516 */
517 if (!fb_info || !fb_info->bl_dev)
518 return -ENODEV;
519
520 mutex_lock(&fb_info->bl_curve_mutex);
521 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
522 len += snprintf(&buf[len], PAGE_SIZE,
523 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
524 fb_info->bl_curve[i + 0],
525 fb_info->bl_curve[i + 1],
526 fb_info->bl_curve[i + 2],
527 fb_info->bl_curve[i + 3],
528 fb_info->bl_curve[i + 4],
529 fb_info->bl_curve[i + 5],
530 fb_info->bl_curve[i + 6],
531 fb_info->bl_curve[i + 7]);
532 mutex_unlock(&fb_info->bl_curve_mutex);
533
534 return len;
535}
536#endif
537
538/* When cmap is added back in it should be a binary attribute
539 * not a text one. Consideration should also be given to converting
540 * fbdev to use configfs instead of sysfs */
541static struct device_attribute device_attrs[] = {
542 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
543 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
544 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
545 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
546 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
547 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
548 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
549 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
550 __ATTR(name, S_IRUGO, show_name, NULL),
551 __ATTR(stride, S_IRUGO, show_stride, NULL),
552 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
553 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
554#ifdef CONFIG_FB_BACKLIGHT
555 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
556#endif
557};
558
559int fb_init_device(struct fb_info *fb_info)
560{
561 int i, error = 0;
562
563 dev_set_drvdata(fb_info->dev, fb_info);
564
565 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
566
567 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
568 error = device_create_file(fb_info->dev, &device_attrs[i]);
569
570 if (error)
571 break;
572 }
573
574 if (error) {
575 while (--i >= 0)
576 device_remove_file(fb_info->dev, &device_attrs[i]);
577 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
578 }
579
580 return 0;
581}
582
583void fb_cleanup_device(struct fb_info *fb_info)
584{
585 unsigned int i;
586
587 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
588 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
589 device_remove_file(fb_info->dev, &device_attrs[i]);
590
591 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
592 }
593}
594
595#ifdef CONFIG_FB_BACKLIGHT
596/* This function generates a linear backlight curve
597 *
598 * 0: off
599 * 1-7: min
600 * 8-127: linear from min to max
601 */
602void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
603{
604 unsigned int i, flat, count, range = (max - min);
605
606 mutex_lock(&fb_info->bl_curve_mutex);
607
608 fb_info->bl_curve[0] = off;
609
610 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
611 fb_info->bl_curve[flat] = min;
612
613 count = FB_BACKLIGHT_LEVELS * 15 / 16;
614 for (i = 0; i < count; ++i)
615 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
616
617 mutex_unlock(&fb_info->bl_curve_mutex);
618}
619EXPORT_SYMBOL_GPL(fb_bl_default_curve);
620#endif