blob: 4f78f234473d78ca36a25b0e34239ea052aedac0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Michael Hanselmann5474c122006-06-25 05:47:08 -070021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080047 p = kzalloc(fb_info_size + size, GFP_KERNEL);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!p)
50 return NULL;
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 info = (struct fb_info *) p;
53
54 if (size)
55 info->par = p + fb_info_size;
56
57 info->device = dev;
58
Michael Hanselmann5474c122006-06-25 05:47:08 -070059#ifdef CONFIG_FB_BACKLIGHT
60 mutex_init(&info->bl_mutex);
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700103 char v = 'p';
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700111
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static ssize_t store_mode(struct class_device *class_device, const char * buf,
122 size_t count)
123{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800124 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800154 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
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{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800165 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800189 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 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{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800207 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800221 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
223}
224
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800225static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static ssize_t store_virtual(struct class_device *class_device,
251 const char * buf, size_t count)
252{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800253 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct fb_var_screeninfo var;
255 char *last = NULL;
256 int err;
257
258 var = fb_info->var;
259 var.xres_virtual = simple_strtoul(buf, &last, 0);
260 last++;
261 if (last - buf >= count)
262 return -EINVAL;
263 var.yres_virtual = simple_strtoul(last, &last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if ((err = activate(fb_info, &var)))
266 return err;
267 return count;
268}
269
270static ssize_t show_virtual(struct class_device *class_device, char *buf)
271{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800272 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700274 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
James Simmonsc14e2cf2005-10-24 21:46:21 +0100277static ssize_t show_stride(struct class_device *class_device, char *buf)
278{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800279 struct fb_info *fb_info = class_get_devdata(class_device);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100280 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static ssize_t store_blank(struct class_device *class_device, const char * buf,
284 size_t count)
285{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800286 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 char *last = NULL;
288 int err;
289
290 acquire_console_sem();
291 fb_info->flags |= FBINFO_MISC_USEREVENT;
292 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
293 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
294 release_console_sem();
295 if (err < 0)
296 return err;
297 return count;
298}
299
300static ssize_t show_blank(struct class_device *class_device, char *buf)
301{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800302// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
306static ssize_t store_console(struct class_device *class_device,
307 const char * buf, size_t count)
308{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800309// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
313static ssize_t show_console(struct class_device *class_device, char *buf)
314{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800315// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static ssize_t store_cursor(struct class_device *class_device,
320 const char * buf, size_t count)
321{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800322// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
326static ssize_t show_cursor(struct class_device *class_device, char *buf)
327{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800328// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
332static ssize_t store_pan(struct class_device *class_device, const char * buf,
333 size_t count)
334{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800335 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct fb_var_screeninfo var;
337 char *last = NULL;
338 int err;
339
340 var = fb_info->var;
341 var.xoffset = simple_strtoul(buf, &last, 0);
342 last++;
343 if (last - buf >= count)
344 return -EINVAL;
345 var.yoffset = simple_strtoul(last, &last, 0);
346
347 acquire_console_sem();
348 err = fb_pan_display(fb_info, &var);
349 release_console_sem();
350
351 if (err < 0)
352 return err;
353 return count;
354}
355
356static ssize_t show_pan(struct class_device *class_device, char *buf)
357{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800358 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
360 fb_info->var.xoffset);
361}
362
James Simmons02459ea2005-07-30 23:49:54 +0100363static ssize_t show_name(struct class_device *class_device, char *buf)
364{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800365 struct fb_info *fb_info = class_get_devdata(class_device);
James Simmons02459ea2005-07-30 23:49:54 +0100366
367 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
368}
369
Matthew Garrett30420f82006-01-09 20:53:01 -0800370static ssize_t store_fbstate(struct class_device *class_device,
371 const char *buf, size_t count)
372{
373 struct fb_info *fb_info = class_get_devdata(class_device);
374 u32 state;
375 char *last = NULL;
376
377 state = simple_strtoul(buf, &last, 0);
378
379 acquire_console_sem();
380 fb_set_suspend(fb_info, (int)state);
381 release_console_sem();
382
383 return count;
384}
385
386static ssize_t show_fbstate(struct class_device *class_device, char *buf)
387{
388 struct fb_info *fb_info = class_get_devdata(class_device);
389 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
390}
391
Michael Hanselmann5474c122006-06-25 05:47:08 -0700392#ifdef CONFIG_FB_BACKLIGHT
393static ssize_t store_bl_curve(struct class_device *class_device,
394 const char *buf, size_t count)
395{
396 struct fb_info *fb_info = class_get_devdata(class_device);
397 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
398 unsigned int i;
399
400 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
401 return -EINVAL;
402
403 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
404 if (sscanf(&buf[i * 24],
405 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
406 &tmp_curve[i * 8 + 0],
407 &tmp_curve[i * 8 + 1],
408 &tmp_curve[i * 8 + 2],
409 &tmp_curve[i * 8 + 3],
410 &tmp_curve[i * 8 + 4],
411 &tmp_curve[i * 8 + 5],
412 &tmp_curve[i * 8 + 6],
413 &tmp_curve[i * 8 + 7]) != 8)
414 return -EINVAL;
415
416 /* If there has been an error in the input data, we won't
417 * reach this loop.
418 */
419 mutex_lock(&fb_info->bl_mutex);
420 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
421 fb_info->bl_curve[i] = tmp_curve[i];
422 mutex_unlock(&fb_info->bl_mutex);
423
424 return count;
425}
426
427static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
428{
429 struct fb_info *fb_info = class_get_devdata(class_device);
430 ssize_t len = 0;
431 unsigned int i;
432
433 mutex_lock(&fb_info->bl_mutex);
434 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
435 len += snprintf(&buf[len], PAGE_SIZE,
436 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
437 fb_info->bl_curve[i + 0],
438 fb_info->bl_curve[i + 1],
439 fb_info->bl_curve[i + 2],
440 fb_info->bl_curve[i + 3],
441 fb_info->bl_curve[i + 4],
442 fb_info->bl_curve[i + 5],
443 fb_info->bl_curve[i + 6],
444 fb_info->bl_curve[i + 7]);
445 mutex_unlock(&fb_info->bl_mutex);
446
447 return len;
448}
449#endif
450
Jon Smirl913e7ec2006-04-12 19:43:35 -0400451/* When cmap is added back in it should be a binary attribute
452 * not a text one. Consideration should also be given to converting
453 * fbdev to use configfs instead of sysfs */
Adrian Bunk75c96f82005-05-05 16:16:09 -0700454static struct class_device_attribute class_device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
456 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
458 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
459 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
460 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
461 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
462 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
James Simmons02459ea2005-07-30 23:49:54 +0100463 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100464 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800465 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
Matthew Garrett30420f82006-01-09 20:53:01 -0800466 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Michael Hanselmann5474c122006-06-25 05:47:08 -0700467#ifdef CONFIG_FB_BACKLIGHT
468 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470};
471
472int fb_init_class_device(struct fb_info *fb_info)
473{
474 unsigned int i;
475 class_set_devdata(fb_info->class_device, fb_info);
476
477 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
478 class_device_create_file(fb_info->class_device,
479 &class_device_attrs[i]);
480 return 0;
481}
482
483void fb_cleanup_class_device(struct fb_info *fb_info)
484{
485 unsigned int i;
486
487 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
488 class_device_remove_file(fb_info->class_device,
489 &class_device_attrs[i]);
490}
491
Michael Hanselmann5474c122006-06-25 05:47:08 -0700492#ifdef CONFIG_FB_BACKLIGHT
493/* This function generates a linear backlight curve
494 *
495 * 0: off
496 * 1-7: min
497 * 8-127: linear from min to max
498 */
499void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
500{
501 unsigned int i, flat, count, range = (max - min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Michael Hanselmann5474c122006-06-25 05:47:08 -0700503 fb_info->bl_curve[0] = off;
504
505 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
506 fb_info->bl_curve[flat] = min;
507
508 count = FB_BACKLIGHT_LEVELS * 15 / 16;
509 for (i = 0; i < count; ++i)
510 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
511}
512EXPORT_SYMBOL_GPL(fb_bl_default_curve);
513#endif