ACPI: ibm-acpi: implement fan watchdog command
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / fbsysfs.c
blobd3a50417ed9a6ab09d16841e435597bf8f6da312
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/fb.h>
20 #include <linux/console.h>
21 #include <linux/module.h>
23 #define FB_SYSFS_FLAG_ATTR 1
25 /**
26 * framebuffer_alloc - creates a new frame buffer info structure
28 * @size: size of driver private data, can be zero
29 * @dev: pointer to the device for this fb, this can be NULL
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).
35 * Returns the new structure, or NULL if an error occured.
38 struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
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;
46 if (size)
47 fb_info_size += PADDING;
49 p = kzalloc(fb_info_size + size, GFP_KERNEL);
51 if (!p)
52 return NULL;
54 info = (struct fb_info *) p;
56 if (size)
57 info->par = p + fb_info_size;
59 info->device = dev;
61 #ifdef CONFIG_FB_BACKLIGHT
62 mutex_init(&info->bl_mutex);
63 #endif
65 return info;
66 #undef PADDING
67 #undef BYTES_PER_LONG
69 EXPORT_SYMBOL(framebuffer_alloc);
71 /**
72 * framebuffer_release - marks the structure available for freeing
74 * @info: frame buffer info structure
76 * Drop the reference count of the class_device embedded in the
77 * framebuffer info structure.
80 void framebuffer_release(struct fb_info *info)
82 kfree(info);
84 EXPORT_SYMBOL(framebuffer_release);
86 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
88 int err;
90 var->activate |= FB_ACTIVATE_FORCE;
91 acquire_console_sem();
92 fb_info->flags |= FBINFO_MISC_USEREVENT;
93 err = fb_set_var(fb_info, var);
94 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
95 release_console_sem();
96 if (err)
97 return err;
98 return 0;
101 static int mode_string(char *buf, unsigned int offset,
102 const struct fb_videomode *mode)
104 char m = 'U';
105 char v = 'p';
107 if (mode->flag & FB_MODE_IS_DETAILED)
108 m = 'D';
109 if (mode->flag & FB_MODE_IS_VESA)
110 m = 'V';
111 if (mode->flag & FB_MODE_IS_STANDARD)
112 m = 'S';
114 if (mode->vmode & FB_VMODE_INTERLACED)
115 v = 'i';
116 if (mode->vmode & FB_VMODE_DOUBLE)
117 v = 'd';
119 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
120 m, mode->xres, mode->yres, v, mode->refresh);
123 static ssize_t store_mode(struct class_device *class_device, const char * buf,
124 size_t count)
126 struct fb_info *fb_info = class_get_devdata(class_device);
127 char mstr[100];
128 struct fb_var_screeninfo var;
129 struct fb_modelist *modelist;
130 struct fb_videomode *mode;
131 struct list_head *pos;
132 size_t i;
133 int err;
135 memset(&var, 0, sizeof(var));
137 list_for_each(pos, &fb_info->modelist) {
138 modelist = list_entry(pos, struct fb_modelist, list);
139 mode = &modelist->mode;
140 i = mode_string(mstr, 0, mode);
141 if (strncmp(mstr, buf, max(count, i)) == 0) {
143 var = fb_info->var;
144 fb_videomode_to_var(&var, mode);
145 if ((err = activate(fb_info, &var)))
146 return err;
147 fb_info->mode = mode;
148 return count;
151 return -EINVAL;
154 static ssize_t show_mode(struct class_device *class_device, char *buf)
156 struct fb_info *fb_info = class_get_devdata(class_device);
158 if (!fb_info->mode)
159 return 0;
161 return mode_string(buf, 0, fb_info->mode);
164 static ssize_t store_modes(struct class_device *class_device, const char * buf,
165 size_t count)
167 struct fb_info *fb_info = class_get_devdata(class_device);
168 LIST_HEAD(old_list);
169 int i = count / sizeof(struct fb_videomode);
171 if (i * sizeof(struct fb_videomode) != count)
172 return -EINVAL;
174 acquire_console_sem();
175 list_splice(&fb_info->modelist, &old_list);
176 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
177 &fb_info->modelist);
178 if (fb_new_modelist(fb_info)) {
179 fb_destroy_modelist(&fb_info->modelist);
180 list_splice(&old_list, &fb_info->modelist);
181 } else
182 fb_destroy_modelist(&old_list);
184 release_console_sem();
186 return 0;
189 static ssize_t show_modes(struct class_device *class_device, char *buf)
191 struct fb_info *fb_info = class_get_devdata(class_device);
192 unsigned int i;
193 struct list_head *pos;
194 struct fb_modelist *modelist;
195 const struct fb_videomode *mode;
197 i = 0;
198 list_for_each(pos, &fb_info->modelist) {
199 modelist = list_entry(pos, struct fb_modelist, list);
200 mode = &modelist->mode;
201 i += mode_string(buf, i, mode);
203 return i;
206 static ssize_t store_bpp(struct class_device *class_device, const char * buf,
207 size_t count)
209 struct fb_info *fb_info = class_get_devdata(class_device);
210 struct fb_var_screeninfo var;
211 char ** last = NULL;
212 int err;
214 var = fb_info->var;
215 var.bits_per_pixel = simple_strtoul(buf, last, 0);
216 if ((err = activate(fb_info, &var)))
217 return err;
218 return count;
221 static ssize_t show_bpp(struct class_device *class_device, char *buf)
223 struct fb_info *fb_info = class_get_devdata(class_device);
224 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
227 static ssize_t store_rotate(struct class_device *class_device, const char *buf,
228 size_t count)
230 struct fb_info *fb_info = class_get_devdata(class_device);
231 struct fb_var_screeninfo var;
232 char **last = NULL;
233 int err;
235 var = fb_info->var;
236 var.rotate = simple_strtoul(buf, last, 0);
238 if ((err = activate(fb_info, &var)))
239 return err;
241 return count;
245 static ssize_t show_rotate(struct class_device *class_device, char *buf)
247 struct fb_info *fb_info = class_get_devdata(class_device);
249 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
252 static ssize_t store_virtual(struct class_device *class_device,
253 const char * buf, size_t count)
255 struct fb_info *fb_info = class_get_devdata(class_device);
256 struct fb_var_screeninfo var;
257 char *last = NULL;
258 int err;
260 var = fb_info->var;
261 var.xres_virtual = simple_strtoul(buf, &last, 0);
262 last++;
263 if (last - buf >= count)
264 return -EINVAL;
265 var.yres_virtual = simple_strtoul(last, &last, 0);
267 if ((err = activate(fb_info, &var)))
268 return err;
269 return count;
272 static ssize_t show_virtual(struct class_device *class_device, char *buf)
274 struct fb_info *fb_info = class_get_devdata(class_device);
275 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
276 fb_info->var.yres_virtual);
279 static ssize_t show_stride(struct class_device *class_device, char *buf)
281 struct fb_info *fb_info = class_get_devdata(class_device);
282 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
285 static ssize_t store_blank(struct class_device *class_device, const char * buf,
286 size_t count)
288 struct fb_info *fb_info = class_get_devdata(class_device);
289 char *last = NULL;
290 int err;
292 acquire_console_sem();
293 fb_info->flags |= FBINFO_MISC_USEREVENT;
294 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
295 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
296 release_console_sem();
297 if (err < 0)
298 return err;
299 return count;
302 static ssize_t show_blank(struct class_device *class_device, char *buf)
304 // struct fb_info *fb_info = class_get_devdata(class_device);
305 return 0;
308 static ssize_t store_console(struct class_device *class_device,
309 const char * buf, size_t count)
311 // struct fb_info *fb_info = class_get_devdata(class_device);
312 return 0;
315 static ssize_t show_console(struct class_device *class_device, char *buf)
317 // struct fb_info *fb_info = class_get_devdata(class_device);
318 return 0;
321 static ssize_t store_cursor(struct class_device *class_device,
322 const char * buf, size_t count)
324 // struct fb_info *fb_info = class_get_devdata(class_device);
325 return 0;
328 static ssize_t show_cursor(struct class_device *class_device, char *buf)
330 // struct fb_info *fb_info = class_get_devdata(class_device);
331 return 0;
334 static ssize_t store_pan(struct class_device *class_device, const char * buf,
335 size_t count)
337 struct fb_info *fb_info = class_get_devdata(class_device);
338 struct fb_var_screeninfo var;
339 char *last = NULL;
340 int err;
342 var = fb_info->var;
343 var.xoffset = simple_strtoul(buf, &last, 0);
344 last++;
345 if (last - buf >= count)
346 return -EINVAL;
347 var.yoffset = simple_strtoul(last, &last, 0);
349 acquire_console_sem();
350 err = fb_pan_display(fb_info, &var);
351 release_console_sem();
353 if (err < 0)
354 return err;
355 return count;
358 static ssize_t show_pan(struct class_device *class_device, char *buf)
360 struct fb_info *fb_info = class_get_devdata(class_device);
361 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
362 fb_info->var.xoffset);
365 static ssize_t show_name(struct class_device *class_device, char *buf)
367 struct fb_info *fb_info = class_get_devdata(class_device);
369 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
372 static ssize_t store_fbstate(struct class_device *class_device,
373 const char *buf, size_t count)
375 struct fb_info *fb_info = class_get_devdata(class_device);
376 u32 state;
377 char *last = NULL;
379 state = simple_strtoul(buf, &last, 0);
381 acquire_console_sem();
382 fb_set_suspend(fb_info, (int)state);
383 release_console_sem();
385 return count;
388 static ssize_t show_fbstate(struct class_device *class_device, char *buf)
390 struct fb_info *fb_info = class_get_devdata(class_device);
391 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
394 #ifdef CONFIG_FB_BACKLIGHT
395 static ssize_t store_bl_curve(struct class_device *class_device,
396 const char *buf, size_t count)
398 struct fb_info *fb_info = class_get_devdata(class_device);
399 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
400 unsigned int i;
402 /* Some drivers don't use framebuffer_alloc(), but those also
403 * don't have backlights.
405 if (!fb_info || !fb_info->bl_dev)
406 return -ENODEV;
408 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
409 return -EINVAL;
411 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
412 if (sscanf(&buf[i * 24],
413 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
414 &tmp_curve[i * 8 + 0],
415 &tmp_curve[i * 8 + 1],
416 &tmp_curve[i * 8 + 2],
417 &tmp_curve[i * 8 + 3],
418 &tmp_curve[i * 8 + 4],
419 &tmp_curve[i * 8 + 5],
420 &tmp_curve[i * 8 + 6],
421 &tmp_curve[i * 8 + 7]) != 8)
422 return -EINVAL;
424 /* If there has been an error in the input data, we won't
425 * reach this loop.
427 mutex_lock(&fb_info->bl_mutex);
428 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
429 fb_info->bl_curve[i] = tmp_curve[i];
430 mutex_unlock(&fb_info->bl_mutex);
432 return count;
435 static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
437 struct fb_info *fb_info = class_get_devdata(class_device);
438 ssize_t len = 0;
439 unsigned int i;
441 /* Some drivers don't use framebuffer_alloc(), but those also
442 * don't have backlights.
444 if (!fb_info || !fb_info->bl_dev)
445 return -ENODEV;
447 mutex_lock(&fb_info->bl_mutex);
448 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
449 len += snprintf(&buf[len], PAGE_SIZE,
450 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
451 fb_info->bl_curve[i + 0],
452 fb_info->bl_curve[i + 1],
453 fb_info->bl_curve[i + 2],
454 fb_info->bl_curve[i + 3],
455 fb_info->bl_curve[i + 4],
456 fb_info->bl_curve[i + 5],
457 fb_info->bl_curve[i + 6],
458 fb_info->bl_curve[i + 7]);
459 mutex_unlock(&fb_info->bl_mutex);
461 return len;
463 #endif
465 /* When cmap is added back in it should be a binary attribute
466 * not a text one. Consideration should also be given to converting
467 * fbdev to use configfs instead of sysfs */
468 static struct class_device_attribute class_device_attrs[] = {
469 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
470 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
471 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
472 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
473 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
474 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
475 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
476 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
477 __ATTR(name, S_IRUGO, show_name, NULL),
478 __ATTR(stride, S_IRUGO, show_stride, NULL),
479 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
480 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
481 #ifdef CONFIG_FB_BACKLIGHT
482 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
483 #endif
486 int fb_init_class_device(struct fb_info *fb_info)
488 int i, error = 0;
490 class_set_devdata(fb_info->class_device, fb_info);
492 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
494 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++) {
495 error = class_device_create_file(fb_info->class_device,
496 &class_device_attrs[i]);
498 if (error)
499 break;
502 if (error) {
503 while (--i >= 0)
504 class_device_remove_file(fb_info->class_device,
505 &class_device_attrs[i]);
506 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
509 return 0;
512 void fb_cleanup_class_device(struct fb_info *fb_info)
514 unsigned int i;
516 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
517 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
518 class_device_remove_file(fb_info->class_device,
519 &class_device_attrs[i]);
521 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
525 #ifdef CONFIG_FB_BACKLIGHT
526 /* This function generates a linear backlight curve
528 * 0: off
529 * 1-7: min
530 * 8-127: linear from min to max
532 void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
534 unsigned int i, flat, count, range = (max - min);
536 fb_info->bl_curve[0] = off;
538 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
539 fb_info->bl_curve[flat] = min;
541 count = FB_BACKLIGHT_LEVELS * 15 / 16;
542 for (i = 0; i < count; ++i)
543 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
545 EXPORT_SYMBOL_GPL(fb_bl_default_curve);
546 #endif