[PATCH] Detaching fbcon: fix vgacon to allow retaking of the console
[linux-block.git] / drivers / video / 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>
19#include <linux/fb.h>
20#include <linux/console.h>
5474c120 21#include <linux/module.h>
1da177e4
LT
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 occured.
34 *
35 */
36struct 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
def1eded
AD
47 p = kzalloc(fb_info_size + size, GFP_KERNEL);
48
1da177e4
LT
49 if (!p)
50 return NULL;
def1eded 51
1da177e4
LT
52 info = (struct fb_info *) p;
53
54 if (size)
55 info->par = p + fb_info_size;
56
57 info->device = dev;
58
5474c120
MH
59#ifdef CONFIG_FB_BACKLIGHT
60 mutex_init(&info->bl_mutex);
61#endif
62
1da177e4
LT
63 return info;
64#undef PADDING
65#undef BYTES_PER_LONG
66}
67EXPORT_SYMBOL(framebuffer_alloc);
68
69/**
70 * framebuffer_release - marks the structure available for freeing
71 *
72 * @info: frame buffer info structure
73 *
74 * Drop the reference count of the class_device embedded in the
75 * framebuffer info structure.
76 *
77 */
78void framebuffer_release(struct fb_info *info)
79{
80 kfree(info);
81}
82EXPORT_SYMBOL(framebuffer_release);
83
84static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
85{
86 int err;
87
88 var->activate |= FB_ACTIVATE_FORCE;
89 acquire_console_sem();
90 fb_info->flags |= FBINFO_MISC_USEREVENT;
91 err = fb_set_var(fb_info, var);
92 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
93 release_console_sem();
94 if (err)
95 return err;
96 return 0;
97}
98
99static int mode_string(char *buf, unsigned int offset,
100 const struct fb_videomode *mode)
101{
102 char m = 'U';
9dac73a4
DT
103 char v = 'p';
104
1da177e4
LT
105 if (mode->flag & FB_MODE_IS_DETAILED)
106 m = 'D';
107 if (mode->flag & FB_MODE_IS_VESA)
108 m = 'V';
109 if (mode->flag & FB_MODE_IS_STANDARD)
110 m = 'S';
9dac73a4
DT
111
112 if (mode->vmode & FB_VMODE_INTERLACED)
113 v = 'i';
114 if (mode->vmode & FB_VMODE_DOUBLE)
115 v = 'd';
116
117 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
118 m, mode->xres, mode->yres, v, mode->refresh);
1da177e4
LT
119}
120
121static ssize_t store_mode(struct class_device *class_device, const char * buf,
122 size_t count)
123{
313e58ab 124 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
125 char mstr[100];
126 struct fb_var_screeninfo var;
127 struct fb_modelist *modelist;
128 struct fb_videomode *mode;
129 struct list_head *pos;
130 size_t i;
131 int err;
132
133 memset(&var, 0, sizeof(var));
134
135 list_for_each(pos, &fb_info->modelist) {
136 modelist = list_entry(pos, struct fb_modelist, list);
137 mode = &modelist->mode;
138 i = mode_string(mstr, 0, mode);
139 if (strncmp(mstr, buf, max(count, i)) == 0) {
140
141 var = fb_info->var;
142 fb_videomode_to_var(&var, mode);
143 if ((err = activate(fb_info, &var)))
144 return err;
145 fb_info->mode = mode;
146 return count;
147 }
148 }
149 return -EINVAL;
150}
151
152static ssize_t show_mode(struct class_device *class_device, char *buf)
153{
313e58ab 154 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
155
156 if (!fb_info->mode)
157 return 0;
158
159 return mode_string(buf, 0, fb_info->mode);
160}
161
162static ssize_t store_modes(struct class_device *class_device, const char * buf,
163 size_t count)
164{
313e58ab 165 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
166 LIST_HEAD(old_list);
167 int i = count / sizeof(struct fb_videomode);
168
169 if (i * sizeof(struct fb_videomode) != count)
170 return -EINVAL;
171
172 acquire_console_sem();
173 list_splice(&fb_info->modelist, &old_list);
174 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
175 &fb_info->modelist);
176 if (fb_new_modelist(fb_info)) {
177 fb_destroy_modelist(&fb_info->modelist);
178 list_splice(&old_list, &fb_info->modelist);
179 } else
180 fb_destroy_modelist(&old_list);
181
182 release_console_sem();
183
184 return 0;
185}
186
187static ssize_t show_modes(struct class_device *class_device, char *buf)
188{
313e58ab 189 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
190 unsigned int i;
191 struct list_head *pos;
192 struct fb_modelist *modelist;
193 const struct fb_videomode *mode;
194
195 i = 0;
196 list_for_each(pos, &fb_info->modelist) {
197 modelist = list_entry(pos, struct fb_modelist, list);
198 mode = &modelist->mode;
199 i += mode_string(buf, i, mode);
200 }
201 return i;
202}
203
204static ssize_t store_bpp(struct class_device *class_device, const char * buf,
205 size_t count)
206{
313e58ab 207 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
208 struct fb_var_screeninfo var;
209 char ** last = NULL;
210 int err;
211
212 var = fb_info->var;
213 var.bits_per_pixel = simple_strtoul(buf, last, 0);
214 if ((err = activate(fb_info, &var)))
215 return err;
216 return count;
217}
218
219static ssize_t show_bpp(struct class_device *class_device, char *buf)
220{
313e58ab 221 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
222 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
223}
224
a812c94b
AD
225static ssize_t store_rotate(struct class_device *class_device, const char *buf,
226 size_t count)
227{
228 struct fb_info *fb_info = class_get_devdata(class_device);
229 struct fb_var_screeninfo var;
230 char **last = NULL;
231 int err;
232
233 var = fb_info->var;
234 var.rotate = simple_strtoul(buf, last, 0);
235
236 if ((err = activate(fb_info, &var)))
237 return err;
238
239 return count;
240}
241
242
243static ssize_t show_rotate(struct class_device *class_device, char *buf)
244{
245 struct fb_info *fb_info = class_get_devdata(class_device);
246
247 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
248}
249
250static ssize_t store_con_rotate(struct class_device *class_device,
251 const char *buf, size_t count)
252{
253 struct fb_info *fb_info = class_get_devdata(class_device);
254 int rotate;
255 char **last = NULL;
256
257 acquire_console_sem();
258 rotate = simple_strtoul(buf, last, 0);
259 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
260 release_console_sem();
261 return count;
262}
263
264static ssize_t store_con_rotate_all(struct class_device *class_device,
265 const char *buf, size_t count)
266{
267 struct fb_info *fb_info = class_get_devdata(class_device);
268 int rotate;
269 char **last = NULL;
270
271 acquire_console_sem();
272 rotate = simple_strtoul(buf, last, 0);
273 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
274 release_console_sem();
275 return count;
276}
277
278static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
279{
280 struct fb_info *fb_info = class_get_devdata(class_device);
281 int rotate;
282
283 acquire_console_sem();
284 rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
285 release_console_sem();
286 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
287}
288
1da177e4
LT
289static ssize_t store_virtual(struct class_device *class_device,
290 const char * buf, size_t count)
291{
313e58ab 292 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
293 struct fb_var_screeninfo var;
294 char *last = NULL;
295 int err;
296
297 var = fb_info->var;
298 var.xres_virtual = simple_strtoul(buf, &last, 0);
299 last++;
300 if (last - buf >= count)
301 return -EINVAL;
302 var.yres_virtual = simple_strtoul(last, &last, 0);
1da177e4
LT
303
304 if ((err = activate(fb_info, &var)))
305 return err;
306 return count;
307}
308
309static ssize_t show_virtual(struct class_device *class_device, char *buf)
310{
313e58ab 311 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4 312 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
e2c16499 313 fb_info->var.yres_virtual);
1da177e4
LT
314}
315
c14e2cfc
JS
316static ssize_t show_stride(struct class_device *class_device, char *buf)
317{
313e58ab 318 struct fb_info *fb_info = class_get_devdata(class_device);
c14e2cfc
JS
319 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
320}
321
1da177e4
LT
322static ssize_t store_blank(struct class_device *class_device, const char * buf,
323 size_t count)
324{
313e58ab 325 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
326 char *last = NULL;
327 int err;
328
329 acquire_console_sem();
330 fb_info->flags |= FBINFO_MISC_USEREVENT;
331 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
332 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
333 release_console_sem();
334 if (err < 0)
335 return err;
336 return count;
337}
338
339static ssize_t show_blank(struct class_device *class_device, char *buf)
340{
313e58ab 341// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
342 return 0;
343}
344
345static ssize_t store_console(struct class_device *class_device,
346 const char * buf, size_t count)
347{
313e58ab 348// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
349 return 0;
350}
351
352static ssize_t show_console(struct class_device *class_device, char *buf)
353{
313e58ab 354// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
355 return 0;
356}
357
358static ssize_t store_cursor(struct class_device *class_device,
359 const char * buf, size_t count)
360{
313e58ab 361// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
362 return 0;
363}
364
365static ssize_t show_cursor(struct class_device *class_device, char *buf)
366{
313e58ab 367// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
368 return 0;
369}
370
371static ssize_t store_pan(struct class_device *class_device, const char * buf,
372 size_t count)
373{
313e58ab 374 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
375 struct fb_var_screeninfo var;
376 char *last = NULL;
377 int err;
378
379 var = fb_info->var;
380 var.xoffset = simple_strtoul(buf, &last, 0);
381 last++;
382 if (last - buf >= count)
383 return -EINVAL;
384 var.yoffset = simple_strtoul(last, &last, 0);
385
386 acquire_console_sem();
387 err = fb_pan_display(fb_info, &var);
388 release_console_sem();
389
390 if (err < 0)
391 return err;
392 return count;
393}
394
395static ssize_t show_pan(struct class_device *class_device, char *buf)
396{
313e58ab 397 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
398 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
399 fb_info->var.xoffset);
400}
401
02459eaa
JS
402static ssize_t show_name(struct class_device *class_device, char *buf)
403{
313e58ab 404 struct fb_info *fb_info = class_get_devdata(class_device);
02459eaa
JS
405
406 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
407}
408
30420f8f
MG
409static ssize_t store_fbstate(struct class_device *class_device,
410 const char *buf, size_t count)
411{
412 struct fb_info *fb_info = class_get_devdata(class_device);
413 u32 state;
414 char *last = NULL;
415
416 state = simple_strtoul(buf, &last, 0);
417
418 acquire_console_sem();
419 fb_set_suspend(fb_info, (int)state);
420 release_console_sem();
421
422 return count;
423}
424
425static ssize_t show_fbstate(struct class_device *class_device, char *buf)
426{
427 struct fb_info *fb_info = class_get_devdata(class_device);
428 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
429}
430
5474c120
MH
431#ifdef CONFIG_FB_BACKLIGHT
432static ssize_t store_bl_curve(struct class_device *class_device,
433 const char *buf, size_t count)
434{
435 struct fb_info *fb_info = class_get_devdata(class_device);
436 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
437 unsigned int i;
438
439 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
440 return -EINVAL;
441
442 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
443 if (sscanf(&buf[i * 24],
444 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
445 &tmp_curve[i * 8 + 0],
446 &tmp_curve[i * 8 + 1],
447 &tmp_curve[i * 8 + 2],
448 &tmp_curve[i * 8 + 3],
449 &tmp_curve[i * 8 + 4],
450 &tmp_curve[i * 8 + 5],
451 &tmp_curve[i * 8 + 6],
452 &tmp_curve[i * 8 + 7]) != 8)
453 return -EINVAL;
454
455 /* If there has been an error in the input data, we won't
456 * reach this loop.
457 */
458 mutex_lock(&fb_info->bl_mutex);
459 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
460 fb_info->bl_curve[i] = tmp_curve[i];
461 mutex_unlock(&fb_info->bl_mutex);
462
463 return count;
464}
465
466static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
467{
468 struct fb_info *fb_info = class_get_devdata(class_device);
469 ssize_t len = 0;
470 unsigned int i;
471
472 mutex_lock(&fb_info->bl_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_mutex);
485
486 return len;
487}
488#endif
489
913e7ec5
JS
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 */
75c96f85 493static struct class_device_attribute class_device_attrs[] = {
1da177e4
LT
494 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
495 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
1da177e4
LT
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),
02459eaa 502 __ATTR(name, S_IRUGO, show_name, NULL),
c14e2cfc 503 __ATTR(stride, S_IRUGO, show_stride, NULL),
a812c94b
AD
504 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
505 __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
506 __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
30420f8f 507 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
5474c120
MH
508#ifdef CONFIG_FB_BACKLIGHT
509 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
510#endif
1da177e4
LT
511};
512
513int fb_init_class_device(struct fb_info *fb_info)
514{
515 unsigned int i;
516 class_set_devdata(fb_info->class_device, fb_info);
517
518 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
519 class_device_create_file(fb_info->class_device,
520 &class_device_attrs[i]);
521 return 0;
522}
523
524void fb_cleanup_class_device(struct fb_info *fb_info)
525{
526 unsigned int i;
527
528 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
529 class_device_remove_file(fb_info->class_device,
530 &class_device_attrs[i]);
531}
532
5474c120
MH
533#ifdef CONFIG_FB_BACKLIGHT
534/* This function generates a linear backlight curve
535 *
536 * 0: off
537 * 1-7: min
538 * 8-127: linear from min to max
539 */
540void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
541{
542 unsigned int i, flat, count, range = (max - min);
543
544 fb_info->bl_curve[0] = off;
1da177e4 545
5474c120
MH
546 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
547 fb_info->bl_curve[flat] = min;
548
549 count = FB_BACKLIGHT_LEVELS * 15 / 16;
550 for (i = 0; i < count; ++i)
551 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
552}
553EXPORT_SYMBOL_GPL(fb_bl_default_curve);
554#endif