[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / video / fbsysfs.c
blobc78a2c5961d3bada42cd7362085fe414f9fcd40e
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>
22 /**
23 * framebuffer_alloc - creates a new frame buffer info structure
25 * @size: size of driver private data, can be zero
26 * @dev: pointer to the device for this fb, this can be NULL
28 * Creates a new frame buffer info structure. Also reserves @size bytes
29 * for driver private data (info->par). info->par (if any) will be
30 * aligned to sizeof(long).
32 * Returns the new structure, or NULL if an error occured.
35 struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
37 #define BYTES_PER_LONG (BITS_PER_LONG/8)
38 #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
39 int fb_info_size = sizeof(struct fb_info);
40 struct fb_info *info;
41 char *p;
43 if (size)
44 fb_info_size += PADDING;
46 p = kmalloc(fb_info_size + size, GFP_KERNEL);
47 if (!p)
48 return NULL;
49 memset(p, 0, fb_info_size + size);
50 info = (struct fb_info *) p;
52 if (size)
53 info->par = p + fb_info_size;
55 info->device = dev;
57 return info;
58 #undef PADDING
59 #undef BYTES_PER_LONG
61 EXPORT_SYMBOL(framebuffer_alloc);
63 /**
64 * framebuffer_release - marks the structure available for freeing
66 * @info: frame buffer info structure
68 * Drop the reference count of the class_device embedded in the
69 * framebuffer info structure.
72 void framebuffer_release(struct fb_info *info)
74 kfree(info);
76 EXPORT_SYMBOL(framebuffer_release);
78 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
80 int err;
82 var->activate |= FB_ACTIVATE_FORCE;
83 acquire_console_sem();
84 fb_info->flags |= FBINFO_MISC_USEREVENT;
85 err = fb_set_var(fb_info, var);
86 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
87 release_console_sem();
88 if (err)
89 return err;
90 return 0;
93 static int mode_string(char *buf, unsigned int offset,
94 const struct fb_videomode *mode)
96 char m = 'U';
97 if (mode->flag & FB_MODE_IS_DETAILED)
98 m = 'D';
99 if (mode->flag & FB_MODE_IS_VESA)
100 m = 'V';
101 if (mode->flag & FB_MODE_IS_STANDARD)
102 m = 'S';
103 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh);
106 static ssize_t store_mode(struct class_device *class_device, const char * buf,
107 size_t count)
109 struct fb_info *fb_info =
110 (struct fb_info *)class_get_devdata(class_device);
111 char mstr[100];
112 struct fb_var_screeninfo var;
113 struct fb_modelist *modelist;
114 struct fb_videomode *mode;
115 struct list_head *pos;
116 size_t i;
117 int err;
119 memset(&var, 0, sizeof(var));
121 list_for_each(pos, &fb_info->modelist) {
122 modelist = list_entry(pos, struct fb_modelist, list);
123 mode = &modelist->mode;
124 i = mode_string(mstr, 0, mode);
125 if (strncmp(mstr, buf, max(count, i)) == 0) {
127 var = fb_info->var;
128 fb_videomode_to_var(&var, mode);
129 if ((err = activate(fb_info, &var)))
130 return err;
131 fb_info->mode = mode;
132 return count;
135 return -EINVAL;
138 static ssize_t show_mode(struct class_device *class_device, char *buf)
140 struct fb_info *fb_info =
141 (struct fb_info *)class_get_devdata(class_device);
143 if (!fb_info->mode)
144 return 0;
146 return mode_string(buf, 0, fb_info->mode);
149 static ssize_t store_modes(struct class_device *class_device, const char * buf,
150 size_t count)
152 struct fb_info *fb_info =
153 (struct fb_info *)class_get_devdata(class_device);
154 LIST_HEAD(old_list);
155 int i = count / sizeof(struct fb_videomode);
157 if (i * sizeof(struct fb_videomode) != count)
158 return -EINVAL;
160 acquire_console_sem();
161 list_splice(&fb_info->modelist, &old_list);
162 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
163 &fb_info->modelist);
164 if (fb_new_modelist(fb_info)) {
165 fb_destroy_modelist(&fb_info->modelist);
166 list_splice(&old_list, &fb_info->modelist);
167 } else
168 fb_destroy_modelist(&old_list);
170 release_console_sem();
172 return 0;
175 static ssize_t show_modes(struct class_device *class_device, char *buf)
177 struct fb_info *fb_info =
178 (struct fb_info *)class_get_devdata(class_device);
179 unsigned int i;
180 struct list_head *pos;
181 struct fb_modelist *modelist;
182 const struct fb_videomode *mode;
184 i = 0;
185 list_for_each(pos, &fb_info->modelist) {
186 modelist = list_entry(pos, struct fb_modelist, list);
187 mode = &modelist->mode;
188 i += mode_string(buf, i, mode);
190 return i;
193 static ssize_t store_bpp(struct class_device *class_device, const char * buf,
194 size_t count)
196 struct fb_info *fb_info =
197 (struct fb_info *)class_get_devdata(class_device);
198 struct fb_var_screeninfo var;
199 char ** last = NULL;
200 int err;
202 var = fb_info->var;
203 var.bits_per_pixel = simple_strtoul(buf, last, 0);
204 if ((err = activate(fb_info, &var)))
205 return err;
206 return count;
209 static ssize_t show_bpp(struct class_device *class_device, char *buf)
211 struct fb_info *fb_info =
212 (struct fb_info *)class_get_devdata(class_device);
213 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
216 static ssize_t store_virtual(struct class_device *class_device,
217 const char * buf, size_t count)
219 struct fb_info *fb_info =
220 (struct fb_info *)class_get_devdata(class_device);
221 struct fb_var_screeninfo var;
222 char *last = NULL;
223 int err;
225 var = fb_info->var;
226 var.xres_virtual = simple_strtoul(buf, &last, 0);
227 last++;
228 if (last - buf >= count)
229 return -EINVAL;
230 var.yres_virtual = simple_strtoul(last, &last, 0);
231 printk(KERN_ERR "fb: xres %d yres %d\n", var.xres_virtual,
232 var.yres_virtual);
234 if ((err = activate(fb_info, &var)))
235 return err;
236 return count;
239 static ssize_t show_virtual(struct class_device *class_device, char *buf)
241 struct fb_info *fb_info =
242 (struct fb_info *)class_get_devdata(class_device);
243 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
244 fb_info->var.xres_virtual);
247 static ssize_t store_cmap(struct class_device *class_device, const char * buf,
248 size_t count)
250 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
251 return 0;
254 static ssize_t show_cmap(struct class_device *class_device, char *buf)
256 struct fb_info *fb_info =
257 (struct fb_info *)class_get_devdata(class_device);
258 unsigned int offset = 0, i;
260 if (!fb_info->cmap.red || !fb_info->cmap.blue ||
261 fb_info->cmap.green || fb_info->cmap.transp)
262 return -EINVAL;
264 for (i = 0; i < fb_info->cmap.len; i++) {
265 offset += snprintf(buf, PAGE_SIZE - offset,
266 "%d,%d,%d,%d,%d\n", i + fb_info->cmap.start,
267 fb_info->cmap.red[i], fb_info->cmap.blue[i],
268 fb_info->cmap.green[i],
269 fb_info->cmap.transp[i]);
271 return offset;
274 static ssize_t store_blank(struct class_device *class_device, const char * buf,
275 size_t count)
277 struct fb_info *fb_info =
278 (struct fb_info *)class_get_devdata(class_device);
279 char *last = NULL;
280 int err;
282 acquire_console_sem();
283 fb_info->flags |= FBINFO_MISC_USEREVENT;
284 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
285 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
286 release_console_sem();
287 if (err < 0)
288 return err;
289 return count;
292 static ssize_t show_blank(struct class_device *class_device, char *buf)
294 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
295 return 0;
298 static ssize_t store_console(struct class_device *class_device,
299 const char * buf, size_t count)
301 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
302 return 0;
305 static ssize_t show_console(struct class_device *class_device, char *buf)
307 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
308 return 0;
311 static ssize_t store_cursor(struct class_device *class_device,
312 const char * buf, size_t count)
314 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
315 return 0;
318 static ssize_t show_cursor(struct class_device *class_device, char *buf)
320 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
321 return 0;
324 static ssize_t store_pan(struct class_device *class_device, const char * buf,
325 size_t count)
327 struct fb_info *fb_info =
328 (struct fb_info *)class_get_devdata(class_device);
329 struct fb_var_screeninfo var;
330 char *last = NULL;
331 int err;
333 var = fb_info->var;
334 var.xoffset = simple_strtoul(buf, &last, 0);
335 last++;
336 if (last - buf >= count)
337 return -EINVAL;
338 var.yoffset = simple_strtoul(last, &last, 0);
340 acquire_console_sem();
341 err = fb_pan_display(fb_info, &var);
342 release_console_sem();
344 if (err < 0)
345 return err;
346 return count;
349 static ssize_t show_pan(struct class_device *class_device, char *buf)
351 struct fb_info *fb_info =
352 (struct fb_info *)class_get_devdata(class_device);
353 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
354 fb_info->var.xoffset);
357 static struct class_device_attribute class_device_attrs[] = {
358 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
359 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
360 __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
361 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
362 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
363 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
364 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
365 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
366 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
369 int fb_init_class_device(struct fb_info *fb_info)
371 unsigned int i;
372 class_set_devdata(fb_info->class_device, fb_info);
374 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
375 class_device_create_file(fb_info->class_device,
376 &class_device_attrs[i]);
377 return 0;
380 void fb_cleanup_class_device(struct fb_info *fb_info)
382 unsigned int i;
384 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
385 class_device_remove_file(fb_info->class_device,
386 &class_device_attrs[i]);