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