]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - drivers/video/fbdev/core/fbsysfs.c
Merge tag 'for-linus-20190118' of git://git.kernel.dk/linux-block
[mirror_ubuntu-disco-kernel.git] / drivers / video / fbdev / core / fbsysfs.c
CommitLineData
1da177e4
LT
1/*
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>
5a0e3ad6 19#include <linux/slab.h>
1da177e4
LT
20#include <linux/fb.h>
21#include <linux/console.h>
5474c120 22#include <linux/module.h>
1da177e4 23
1a6600be
AD
24#define FB_SYSFS_FLAG_ATTR 1
25
1da177e4
LT
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 *
25985edc 36 * Returns the new structure, or NULL if an error occurred.
1da177e4
LT
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
def1eded
AD
50 p = kzalloc(fb_info_size + size, GFP_KERNEL);
51
1da177e4
LT
52 if (!p)
53 return NULL;
def1eded 54
1da177e4
LT
55 info = (struct fb_info *) p;
56
57 if (size)
58 info->par = p + fb_info_size;
59
60 info->device = dev;
c9e6a364 61 info->fbcon_rotate_hint = -1;
1da177e4 62
b4a1ed0c 63#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
37ce69a5 64 mutex_init(&info->bl_curve_mutex);
5474c120
MH
65#endif
66
1da177e4
LT
67 return info;
68#undef PADDING
69#undef BYTES_PER_LONG
70}
71EXPORT_SYMBOL(framebuffer_alloc);
72
73/**
74 * framebuffer_release - marks the structure available for freeing
75 *
76 * @info: frame buffer info structure
77 *
78cde088 78 * Drop the reference count of the device embedded in the
1da177e4
LT
79 * framebuffer info structure.
80 *
81 */
82void framebuffer_release(struct fb_info *info)
83{
cc440114
DC
84 if (!info)
85 return;
1471ca9a 86 kfree(info->apertures);
1da177e4
LT
87 kfree(info);
88}
89EXPORT_SYMBOL(framebuffer_release);
90
91static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
92{
93 int err;
94
95 var->activate |= FB_ACTIVATE_FORCE;
ac751efa 96 console_lock();
1da177e4
LT
97 fb_info->flags |= FBINFO_MISC_USEREVENT;
98 err = fb_set_var(fb_info, var);
99 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
ac751efa 100 console_unlock();
1da177e4
LT
101 if (err)
102 return err;
103 return 0;
104}
105
106static int mode_string(char *buf, unsigned int offset,
107 const struct fb_videomode *mode)
108{
109 char m = 'U';
9dac73a4
DT
110 char v = 'p';
111
1da177e4
LT
112 if (mode->flag & FB_MODE_IS_DETAILED)
113 m = 'D';
114 if (mode->flag & FB_MODE_IS_VESA)
115 m = 'V';
116 if (mode->flag & FB_MODE_IS_STANDARD)
117 m = 'S';
9dac73a4
DT
118
119 if (mode->vmode & FB_VMODE_INTERLACED)
120 v = 'i';
121 if (mode->vmode & FB_VMODE_DOUBLE)
122 v = 'd';
123
124 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
125 m, mode->xres, mode->yres, v, mode->refresh);
1da177e4
LT
126}
127
78cde088
GKH
128static ssize_t store_mode(struct device *device, struct device_attribute *attr,
129 const char *buf, size_t count)
1da177e4 130{
78cde088 131 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
132 char mstr[100];
133 struct fb_var_screeninfo var;
134 struct fb_modelist *modelist;
135 struct fb_videomode *mode;
136 struct list_head *pos;
137 size_t i;
138 int err;
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
78cde088
GKH
159static ssize_t show_mode(struct device *device, struct device_attribute *attr,
160 char *buf)
1da177e4 161{
78cde088 162 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
163
164 if (!fb_info->mode)
165 return 0;
166
167 return mode_string(buf, 0, fb_info->mode);
168}
169
78cde088
GKH
170static ssize_t store_modes(struct device *device,
171 struct device_attribute *attr,
172 const char *buf, size_t count)
1da177e4 173{
78cde088 174 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
175 LIST_HEAD(old_list);
176 int i = count / sizeof(struct fb_videomode);
177
178 if (i * sizeof(struct fb_videomode) != count)
179 return -EINVAL;
180
ac751efa 181 console_lock();
3a41c5db
GZ
182 if (!lock_fb_info(fb_info)) {
183 console_unlock();
184 return -ENODEV;
185 }
186
1da177e4 187 list_splice(&fb_info->modelist, &old_list);
9791d763 188 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
1da177e4
LT
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
50e244cc 196 unlock_fb_info(fb_info);
3a41c5db 197 console_unlock();
1da177e4
LT
198
199 return 0;
200}
201
78cde088
GKH
202static ssize_t show_modes(struct device *device, struct device_attribute *attr,
203 char *buf)
1da177e4 204{
78cde088 205 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
206 unsigned int i;
207 struct list_head *pos;
208 struct fb_modelist *modelist;
209 const struct fb_videomode *mode;
210
211 i = 0;
212 list_for_each(pos, &fb_info->modelist) {
213 modelist = list_entry(pos, struct fb_modelist, list);
214 mode = &modelist->mode;
215 i += mode_string(buf, i, mode);
216 }
217 return i;
218}
219
78cde088
GKH
220static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
221 const char *buf, size_t count)
1da177e4 222{
78cde088 223 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
224 struct fb_var_screeninfo var;
225 char ** last = NULL;
226 int err;
227
228 var = fb_info->var;
229 var.bits_per_pixel = simple_strtoul(buf, last, 0);
230 if ((err = activate(fb_info, &var)))
231 return err;
232 return count;
233}
234
78cde088
GKH
235static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
236 char *buf)
1da177e4 237{
78cde088 238 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
239 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
240}
241
78cde088
GKH
242static ssize_t store_rotate(struct device *device,
243 struct device_attribute *attr,
244 const char *buf, size_t count)
a812c94b 245{
78cde088 246 struct fb_info *fb_info = dev_get_drvdata(device);
a812c94b
AD
247 struct fb_var_screeninfo var;
248 char **last = NULL;
249 int err;
250
251 var = fb_info->var;
252 var.rotate = simple_strtoul(buf, last, 0);
253
254 if ((err = activate(fb_info, &var)))
255 return err;
256
257 return count;
258}
259
260
78cde088
GKH
261static ssize_t show_rotate(struct device *device,
262 struct device_attribute *attr, char *buf)
a812c94b 263{
78cde088 264 struct fb_info *fb_info = dev_get_drvdata(device);
a812c94b
AD
265
266 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
267}
268
78cde088
GKH
269static ssize_t store_virtual(struct device *device,
270 struct device_attribute *attr,
271 const char *buf, size_t count)
1da177e4 272{
78cde088 273 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
274 struct fb_var_screeninfo var;
275 char *last = NULL;
276 int err;
277
278 var = fb_info->var;
279 var.xres_virtual = simple_strtoul(buf, &last, 0);
280 last++;
281 if (last - buf >= count)
282 return -EINVAL;
283 var.yres_virtual = simple_strtoul(last, &last, 0);
1da177e4
LT
284
285 if ((err = activate(fb_info, &var)))
286 return err;
287 return count;
288}
289
78cde088
GKH
290static ssize_t show_virtual(struct device *device,
291 struct device_attribute *attr, char *buf)
1da177e4 292{
78cde088 293 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4 294 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
e2c16499 295 fb_info->var.yres_virtual);
1da177e4
LT
296}
297
78cde088
GKH
298static ssize_t show_stride(struct device *device,
299 struct device_attribute *attr, char *buf)
c14e2cfc 300{
78cde088 301 struct fb_info *fb_info = dev_get_drvdata(device);
c14e2cfc
JS
302 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
303}
304
78cde088
GKH
305static ssize_t store_blank(struct device *device,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
1da177e4 308{
78cde088 309 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
310 char *last = NULL;
311 int err;
312
ac751efa 313 console_lock();
1da177e4
LT
314 fb_info->flags |= FBINFO_MISC_USEREVENT;
315 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
316 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
ac751efa 317 console_unlock();
1da177e4
LT
318 if (err < 0)
319 return err;
320 return count;
321}
322
78cde088
GKH
323static ssize_t show_blank(struct device *device,
324 struct device_attribute *attr, char *buf)
1da177e4 325{
78cde088 326// struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
327 return 0;
328}
329
78cde088
GKH
330static ssize_t store_console(struct device *device,
331 struct device_attribute *attr,
332 const char *buf, size_t count)
1da177e4 333{
78cde088 334// struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
335 return 0;
336}
337
78cde088
GKH
338static ssize_t show_console(struct device *device,
339 struct device_attribute *attr, char *buf)
1da177e4 340{
78cde088 341// struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
342 return 0;
343}
344
78cde088
GKH
345static ssize_t store_cursor(struct device *device,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
1da177e4 348{
78cde088 349// struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
350 return 0;
351}
352
78cde088
GKH
353static ssize_t show_cursor(struct device *device,
354 struct device_attribute *attr, char *buf)
1da177e4 355{
78cde088 356// struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
357 return 0;
358}
359
78cde088
GKH
360static ssize_t store_pan(struct device *device,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
1da177e4 363{
78cde088 364 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4
LT
365 struct fb_var_screeninfo var;
366 char *last = NULL;
367 int err;
368
369 var = fb_info->var;
370 var.xoffset = simple_strtoul(buf, &last, 0);
371 last++;
372 if (last - buf >= count)
373 return -EINVAL;
374 var.yoffset = simple_strtoul(last, &last, 0);
375
ac751efa 376 console_lock();
1da177e4 377 err = fb_pan_display(fb_info, &var);
ac751efa 378 console_unlock();
1da177e4
LT
379
380 if (err < 0)
381 return err;
382 return count;
383}
384
78cde088
GKH
385static ssize_t show_pan(struct device *device,
386 struct device_attribute *attr, char *buf)
1da177e4 387{
78cde088 388 struct fb_info *fb_info = dev_get_drvdata(device);
1da177e4 389 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
c4270637 390 fb_info->var.yoffset);
1da177e4
LT
391}
392
78cde088
GKH
393static ssize_t show_name(struct device *device,
394 struct device_attribute *attr, char *buf)
02459eaa 395{
78cde088 396 struct fb_info *fb_info = dev_get_drvdata(device);
02459eaa
JS
397
398 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
399}
400
78cde088
GKH
401static ssize_t store_fbstate(struct device *device,
402 struct device_attribute *attr,
403 const char *buf, size_t count)
30420f8f 404{
78cde088 405 struct fb_info *fb_info = dev_get_drvdata(device);
30420f8f
MG
406 u32 state;
407 char *last = NULL;
408
409 state = simple_strtoul(buf, &last, 0);
410
ac751efa 411 console_lock();
3a41c5db
GZ
412 if (!lock_fb_info(fb_info)) {
413 console_unlock();
414 return -ENODEV;
415 }
416
30420f8f 417 fb_set_suspend(fb_info, (int)state);
3a41c5db 418
9e769ff3 419 unlock_fb_info(fb_info);
3a41c5db 420 console_unlock();
30420f8f
MG
421
422 return count;
423}
424
78cde088
GKH
425static ssize_t show_fbstate(struct device *device,
426 struct device_attribute *attr, char *buf)
30420f8f 427{
78cde088 428 struct fb_info *fb_info = dev_get_drvdata(device);
30420f8f
MG
429 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
430}
431
b4a1ed0c 432#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
78cde088
GKH
433static ssize_t store_bl_curve(struct device *device,
434 struct device_attribute *attr,
435 const char *buf, size_t count)
5474c120 436{
78cde088 437 struct fb_info *fb_info = dev_get_drvdata(device);
5474c120
MH
438 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
439 unsigned int i;
440
25981de5
MH
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
5474c120
MH
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 */
37ce69a5 466 mutex_lock(&fb_info->bl_curve_mutex);
5474c120
MH
467 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
468 fb_info->bl_curve[i] = tmp_curve[i];
37ce69a5 469 mutex_unlock(&fb_info->bl_curve_mutex);
5474c120
MH
470
471 return count;
472}
473
78cde088
GKH
474static ssize_t show_bl_curve(struct device *device,
475 struct device_attribute *attr, char *buf)
5474c120 476{
78cde088 477 struct fb_info *fb_info = dev_get_drvdata(device);
5474c120
MH
478 ssize_t len = 0;
479 unsigned int i;
480
25981de5
MH
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
37ce69a5 487 mutex_lock(&fb_info->bl_curve_mutex);
5474c120 488 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
b4df2047 489 len += scnprintf(&buf[len], PAGE_SIZE - len, "%8ph\n",
30296f61 490 fb_info->bl_curve + i);
37ce69a5 491 mutex_unlock(&fb_info->bl_curve_mutex);
5474c120
MH
492
493 return len;
494}
495#endif
496
913e7ec5
JS
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 */
78cde088 500static struct device_attribute device_attrs[] = {
1da177e4
LT
501 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
502 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
1da177e4
LT
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),
02459eaa 509 __ATTR(name, S_IRUGO, show_name, NULL),
c14e2cfc 510 __ATTR(stride, S_IRUGO, show_stride, NULL),
a812c94b 511 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
30420f8f 512 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
b4a1ed0c 513#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
5474c120
MH
514 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
515#endif
1da177e4
LT
516};
517
78cde088 518int fb_init_device(struct fb_info *fb_info)
1da177e4 519{
1a6600be
AD
520 int i, error = 0;
521
78cde088 522 dev_set_drvdata(fb_info->dev, fb_info);
1da177e4 523
1a6600be
AD
524 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
525
78cde088
GKH
526 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
527 error = device_create_file(fb_info->dev, &device_attrs[i]);
1a6600be
AD
528
529 if (error)
530 break;
531 }
532
533 if (error) {
534 while (--i >= 0)
78cde088 535 device_remove_file(fb_info->dev, &device_attrs[i]);
1a6600be
AD
536 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
537 }
538
1da177e4
LT
539 return 0;
540}
541
78cde088 542void fb_cleanup_device(struct fb_info *fb_info)
1da177e4
LT
543{
544 unsigned int i;
545
1a6600be 546 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
78cde088
GKH
547 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
548 device_remove_file(fb_info->dev, &device_attrs[i]);
1a6600be
AD
549
550 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
551 }
1da177e4
LT
552}
553
b4a1ed0c 554#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
5474c120
MH
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 */
561void 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
37ce69a5
RP
565 mutex_lock(&fb_info->bl_curve_mutex);
566
5474c120 567 fb_info->bl_curve[0] = off;
1da177e4 568
5474c120
MH
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);
37ce69a5
RP
575
576 mutex_unlock(&fb_info->bl_curve_mutex);
5474c120
MH
577}
578EXPORT_SYMBOL_GPL(fb_bl_default_curve);
579#endif