Merge branch 'akpm'
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / fbsysfs.c
blob0a08f134122761df00476ffcc6243b566195db79
1 /*
2 * fbsysfs.c - framebuffer device class and attributes
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.
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.
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>
24 #define FB_SYSFS_FLAG_ATTR 1
26 /**
27 * framebuffer_alloc - creates a new frame buffer info structure
29 * @size: size of driver private data, can be zero
30 * @dev: pointer to the device for this fb, this can be NULL
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).
36 * Returns the new structure, or NULL if an error occured.
39 struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
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;
47 if (size)
48 fb_info_size += PADDING;
50 p = kzalloc(fb_info_size + size, GFP_KERNEL);
52 if (!p)
53 return NULL;
55 info = (struct fb_info *) p;
57 if (size)
58 info->par = p + fb_info_size;
60 info->device = dev;
62 #ifdef CONFIG_FB_BACKLIGHT
63 mutex_init(&info->bl_curve_mutex);
64 #endif
66 return info;
67 #undef PADDING
68 #undef BYTES_PER_LONG
70 EXPORT_SYMBOL(framebuffer_alloc);
72 /**
73 * framebuffer_release - marks the structure available for freeing
75 * @info: frame buffer info structure
77 * Drop the reference count of the device embedded in the
78 * framebuffer info structure.
81 void framebuffer_release(struct fb_info *info)
83 kfree(info->apertures);
84 kfree(info);
86 EXPORT_SYMBOL(framebuffer_release);
88 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
90 int err;
92 var->activate |= FB_ACTIVATE_FORCE;
93 acquire_console_sem();
94 fb_info->flags |= FBINFO_MISC_USEREVENT;
95 err = fb_set_var(fb_info, var);
96 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
97 release_console_sem();
98 if (err)
99 return err;
100 return 0;
103 static int mode_string(char *buf, unsigned int offset,
104 const struct fb_videomode *mode)
106 char m = 'U';
107 char v = 'p';
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';
116 if (mode->vmode & FB_VMODE_INTERLACED)
117 v = 'i';
118 if (mode->vmode & FB_VMODE_DOUBLE)
119 v = 'd';
121 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
122 m, mode->xres, mode->yres, v, mode->refresh);
125 static ssize_t store_mode(struct device *device, struct device_attribute *attr,
126 const char *buf, size_t count)
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;
137 memset(&var, 0, sizeof(var));
139 list_for_each(pos, &fb_info->modelist) {
140 modelist = list_entry(pos, struct fb_modelist, list);
141 mode = &modelist->mode;
142 i = mode_string(mstr, 0, mode);
143 if (strncmp(mstr, buf, max(count, i)) == 0) {
145 var = fb_info->var;
146 fb_videomode_to_var(&var, mode);
147 if ((err = activate(fb_info, &var)))
148 return err;
149 fb_info->mode = mode;
150 return count;
153 return -EINVAL;
156 static ssize_t show_mode(struct device *device, struct device_attribute *attr,
157 char *buf)
159 struct fb_info *fb_info = dev_get_drvdata(device);
161 if (!fb_info->mode)
162 return 0;
164 return mode_string(buf, 0, fb_info->mode);
167 static ssize_t store_modes(struct device *device,
168 struct device_attribute *attr,
169 const char *buf, size_t count)
171 struct fb_info *fb_info = dev_get_drvdata(device);
172 LIST_HEAD(old_list);
173 int i = count / sizeof(struct fb_videomode);
175 if (i * sizeof(struct fb_videomode) != count)
176 return -EINVAL;
178 acquire_console_sem();
179 list_splice(&fb_info->modelist, &old_list);
180 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
181 &fb_info->modelist);
182 if (fb_new_modelist(fb_info)) {
183 fb_destroy_modelist(&fb_info->modelist);
184 list_splice(&old_list, &fb_info->modelist);
185 } else
186 fb_destroy_modelist(&old_list);
188 release_console_sem();
190 return 0;
193 static ssize_t show_modes(struct device *device, struct device_attribute *attr,
194 char *buf)
196 struct fb_info *fb_info = dev_get_drvdata(device);
197 unsigned int i;
198 struct list_head *pos;
199 struct fb_modelist *modelist;
200 const struct fb_videomode *mode;
202 i = 0;
203 list_for_each(pos, &fb_info->modelist) {
204 modelist = list_entry(pos, struct fb_modelist, list);
205 mode = &modelist->mode;
206 i += mode_string(buf, i, mode);
208 return i;
211 static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
212 const char *buf, size_t count)
214 struct fb_info *fb_info = dev_get_drvdata(device);
215 struct fb_var_screeninfo var;
216 char ** last = NULL;
217 int err;
219 var = fb_info->var;
220 var.bits_per_pixel = simple_strtoul(buf, last, 0);
221 if ((err = activate(fb_info, &var)))
222 return err;
223 return count;
226 static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
227 char *buf)
229 struct fb_info *fb_info = dev_get_drvdata(device);
230 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
233 static ssize_t store_rotate(struct device *device,
234 struct device_attribute *attr,
235 const char *buf, size_t count)
237 struct fb_info *fb_info = dev_get_drvdata(device);
238 struct fb_var_screeninfo var;
239 char **last = NULL;
240 int err;
242 var = fb_info->var;
243 var.rotate = simple_strtoul(buf, last, 0);
245 if ((err = activate(fb_info, &var)))
246 return err;
248 return count;
252 static ssize_t show_rotate(struct device *device,
253 struct device_attribute *attr, char *buf)
255 struct fb_info *fb_info = dev_get_drvdata(device);
257 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
260 static ssize_t store_virtual(struct device *device,
261 struct device_attribute *attr,
262 const char *buf, size_t count)
264 struct fb_info *fb_info = dev_get_drvdata(device);
265 struct fb_var_screeninfo var;
266 char *last = NULL;
267 int err;
269 var = fb_info->var;
270 var.xres_virtual = simple_strtoul(buf, &last, 0);
271 last++;
272 if (last - buf >= count)
273 return -EINVAL;
274 var.yres_virtual = simple_strtoul(last, &last, 0);
276 if ((err = activate(fb_info, &var)))
277 return err;
278 return count;
281 static ssize_t show_virtual(struct device *device,
282 struct device_attribute *attr, char *buf)
284 struct fb_info *fb_info = dev_get_drvdata(device);
285 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
286 fb_info->var.yres_virtual);
289 static ssize_t show_stride(struct device *device,
290 struct device_attribute *attr, char *buf)
292 struct fb_info *fb_info = dev_get_drvdata(device);
293 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
296 static ssize_t store_blank(struct device *device,
297 struct device_attribute *attr,
298 const char *buf, size_t count)
300 struct fb_info *fb_info = dev_get_drvdata(device);
301 char *last = NULL;
302 int err;
304 acquire_console_sem();
305 fb_info->flags |= FBINFO_MISC_USEREVENT;
306 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
307 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
308 release_console_sem();
309 if (err < 0)
310 return err;
311 return count;
314 static ssize_t show_blank(struct device *device,
315 struct device_attribute *attr, char *buf)
317 // struct fb_info *fb_info = dev_get_drvdata(device);
318 return 0;
321 static ssize_t store_console(struct device *device,
322 struct device_attribute *attr,
323 const char *buf, size_t count)
325 // struct fb_info *fb_info = dev_get_drvdata(device);
326 return 0;
329 static ssize_t show_console(struct device *device,
330 struct device_attribute *attr, char *buf)
332 // struct fb_info *fb_info = dev_get_drvdata(device);
333 return 0;
336 static ssize_t store_cursor(struct device *device,
337 struct device_attribute *attr,
338 const char *buf, size_t count)
340 // struct fb_info *fb_info = dev_get_drvdata(device);
341 return 0;
344 static ssize_t show_cursor(struct device *device,
345 struct device_attribute *attr, char *buf)
347 // struct fb_info *fb_info = dev_get_drvdata(device);
348 return 0;
351 static ssize_t store_pan(struct device *device,
352 struct device_attribute *attr,
353 const char *buf, size_t count)
355 struct fb_info *fb_info = dev_get_drvdata(device);
356 struct fb_var_screeninfo var;
357 char *last = NULL;
358 int err;
360 var = fb_info->var;
361 var.xoffset = simple_strtoul(buf, &last, 0);
362 last++;
363 if (last - buf >= count)
364 return -EINVAL;
365 var.yoffset = simple_strtoul(last, &last, 0);
367 acquire_console_sem();
368 err = fb_pan_display(fb_info, &var);
369 release_console_sem();
371 if (err < 0)
372 return err;
373 return count;
376 static ssize_t show_pan(struct device *device,
377 struct device_attribute *attr, char *buf)
379 struct fb_info *fb_info = dev_get_drvdata(device);
380 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
381 fb_info->var.yoffset);
384 static ssize_t show_name(struct device *device,
385 struct device_attribute *attr, char *buf)
387 struct fb_info *fb_info = dev_get_drvdata(device);
389 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
392 static ssize_t store_fbstate(struct device *device,
393 struct device_attribute *attr,
394 const char *buf, size_t count)
396 struct fb_info *fb_info = dev_get_drvdata(device);
397 u32 state;
398 char *last = NULL;
400 state = simple_strtoul(buf, &last, 0);
402 acquire_console_sem();
403 fb_set_suspend(fb_info, (int)state);
404 release_console_sem();
406 return count;
409 static ssize_t show_fbstate(struct device *device,
410 struct device_attribute *attr, char *buf)
412 struct fb_info *fb_info = dev_get_drvdata(device);
413 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
416 #ifdef CONFIG_FB_BACKLIGHT
417 static ssize_t store_bl_curve(struct device *device,
418 struct device_attribute *attr,
419 const char *buf, size_t count)
421 struct fb_info *fb_info = dev_get_drvdata(device);
422 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
423 unsigned int i;
425 /* Some drivers don't use framebuffer_alloc(), but those also
426 * don't have backlights.
428 if (!fb_info || !fb_info->bl_dev)
429 return -ENODEV;
431 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
432 return -EINVAL;
434 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
435 if (sscanf(&buf[i * 24],
436 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
437 &tmp_curve[i * 8 + 0],
438 &tmp_curve[i * 8 + 1],
439 &tmp_curve[i * 8 + 2],
440 &tmp_curve[i * 8 + 3],
441 &tmp_curve[i * 8 + 4],
442 &tmp_curve[i * 8 + 5],
443 &tmp_curve[i * 8 + 6],
444 &tmp_curve[i * 8 + 7]) != 8)
445 return -EINVAL;
447 /* If there has been an error in the input data, we won't
448 * reach this loop.
450 mutex_lock(&fb_info->bl_curve_mutex);
451 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
452 fb_info->bl_curve[i] = tmp_curve[i];
453 mutex_unlock(&fb_info->bl_curve_mutex);
455 return count;
458 static ssize_t show_bl_curve(struct device *device,
459 struct device_attribute *attr, char *buf)
461 struct fb_info *fb_info = dev_get_drvdata(device);
462 ssize_t len = 0;
463 unsigned int i;
465 /* Some drivers don't use framebuffer_alloc(), but those also
466 * don't have backlights.
468 if (!fb_info || !fb_info->bl_dev)
469 return -ENODEV;
471 mutex_lock(&fb_info->bl_curve_mutex);
472 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
473 len += snprintf(&buf[len], PAGE_SIZE,
474 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
475 fb_info->bl_curve[i + 0],
476 fb_info->bl_curve[i + 1],
477 fb_info->bl_curve[i + 2],
478 fb_info->bl_curve[i + 3],
479 fb_info->bl_curve[i + 4],
480 fb_info->bl_curve[i + 5],
481 fb_info->bl_curve[i + 6],
482 fb_info->bl_curve[i + 7]);
483 mutex_unlock(&fb_info->bl_curve_mutex);
485 return len;
487 #endif
489 /* When cmap is added back in it should be a binary attribute
490 * not a text one. Consideration should also be given to converting
491 * fbdev to use configfs instead of sysfs */
492 static struct device_attribute device_attrs[] = {
493 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
494 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
495 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
496 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
497 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
498 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
499 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
500 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
501 __ATTR(name, S_IRUGO, show_name, NULL),
502 __ATTR(stride, S_IRUGO, show_stride, NULL),
503 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
504 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
505 #ifdef CONFIG_FB_BACKLIGHT
506 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
507 #endif
510 int fb_init_device(struct fb_info *fb_info)
512 int i, error = 0;
514 dev_set_drvdata(fb_info->dev, fb_info);
516 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
518 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
519 error = device_create_file(fb_info->dev, &device_attrs[i]);
521 if (error)
522 break;
525 if (error) {
526 while (--i >= 0)
527 device_remove_file(fb_info->dev, &device_attrs[i]);
528 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
531 return 0;
534 void fb_cleanup_device(struct fb_info *fb_info)
536 unsigned int i;
538 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
539 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
540 device_remove_file(fb_info->dev, &device_attrs[i]);
542 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
546 #ifdef CONFIG_FB_BACKLIGHT
547 /* This function generates a linear backlight curve
549 * 0: off
550 * 1-7: min
551 * 8-127: linear from min to max
553 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
555 unsigned int i, flat, count, range = (max - min);
557 mutex_lock(&fb_info->bl_curve_mutex);
559 fb_info->bl_curve[0] = off;
561 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
562 fb_info->bl_curve[flat] = min;
564 count = FB_BACKLIGHT_LEVELS * 15 / 16;
565 for (i = 0; i < count; ++i)
566 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
568 mutex_unlock(&fb_info->bl_curve_mutex);
570 EXPORT_SYMBOL_GPL(fb_bl_default_curve);
571 #endif