blob: d3a50417ed9a6ab09d16841e435597bf8f6da312 [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
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -070023#define FB_SYSFS_FLAG_ATTR 1
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/**
26 * framebuffer_alloc - creates a new frame buffer info structure
27 *
28 * @size: size of driver private data, can be zero
29 * @dev: pointer to the device for this fb, this can be NULL
30 *
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).
34 *
35 * Returns the new structure, or NULL if an error occured.
36 *
37 */
38struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
39{
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;
45
46 if (size)
47 fb_info_size += PADDING;
48
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080049 p = kzalloc(fb_info_size + size, GFP_KERNEL);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (!p)
52 return NULL;
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 info = (struct fb_info *) p;
55
56 if (size)
57 info->par = p + fb_info_size;
58
59 info->device = dev;
60
Michael Hanselmann5474c122006-06-25 05:47:08 -070061#ifdef CONFIG_FB_BACKLIGHT
62 mutex_init(&info->bl_mutex);
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return info;
66#undef PADDING
67#undef BYTES_PER_LONG
68}
69EXPORT_SYMBOL(framebuffer_alloc);
70
71/**
72 * framebuffer_release - marks the structure available for freeing
73 *
74 * @info: frame buffer info structure
75 *
76 * Drop the reference count of the class_device embedded in the
77 * framebuffer info structure.
78 *
79 */
80void framebuffer_release(struct fb_info *info)
81{
82 kfree(info);
83}
84EXPORT_SYMBOL(framebuffer_release);
85
86static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
87{
88 int err;
89
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;
99}
100
101static int mode_string(char *buf, unsigned int offset,
102 const struct fb_videomode *mode)
103{
104 char m = 'U';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700105 char v = 'p';
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700113
114 if (mode->vmode & FB_VMODE_INTERLACED)
115 v = 'i';
116 if (mode->vmode & FB_VMODE_DOUBLE)
117 v = 'd';
118
119 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
120 m, mode->xres, mode->yres, v, mode->refresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static ssize_t store_mode(struct class_device *class_device, const char * buf,
124 size_t count)
125{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800126 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 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;
134
135 memset(&var, 0, sizeof(var));
136
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) {
142
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;
149 }
150 }
151 return -EINVAL;
152}
153
154static ssize_t show_mode(struct class_device *class_device, char *buf)
155{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800156 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (!fb_info->mode)
159 return 0;
160
161 return mode_string(buf, 0, fb_info->mode);
162}
163
164static ssize_t store_modes(struct class_device *class_device, const char * buf,
165 size_t count)
166{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800167 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 LIST_HEAD(old_list);
169 int i = count / sizeof(struct fb_videomode);
170
171 if (i * sizeof(struct fb_videomode) != count)
172 return -EINVAL;
173
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);
183
184 release_console_sem();
185
186 return 0;
187}
188
189static ssize_t show_modes(struct class_device *class_device, char *buf)
190{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800191 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int i;
193 struct list_head *pos;
194 struct fb_modelist *modelist;
195 const struct fb_videomode *mode;
196
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);
202 }
203 return i;
204}
205
206static ssize_t store_bpp(struct class_device *class_device, const char * buf,
207 size_t count)
208{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800209 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct fb_var_screeninfo var;
211 char ** last = NULL;
212 int err;
213
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;
219}
220
221static ssize_t show_bpp(struct class_device *class_device, char *buf)
222{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800223 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
225}
226
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800227static ssize_t store_rotate(struct class_device *class_device, const char *buf,
228 size_t count)
229{
230 struct fb_info *fb_info = class_get_devdata(class_device);
231 struct fb_var_screeninfo var;
232 char **last = NULL;
233 int err;
234
235 var = fb_info->var;
236 var.rotate = simple_strtoul(buf, last, 0);
237
238 if ((err = activate(fb_info, &var)))
239 return err;
240
241 return count;
242}
243
244
245static ssize_t show_rotate(struct class_device *class_device, char *buf)
246{
247 struct fb_info *fb_info = class_get_devdata(class_device);
248
249 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static ssize_t store_virtual(struct class_device *class_device,
253 const char * buf, size_t count)
254{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800255 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct fb_var_screeninfo var;
257 char *last = NULL;
258 int err;
259
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if ((err = activate(fb_info, &var)))
268 return err;
269 return count;
270}
271
272static ssize_t show_virtual(struct class_device *class_device, char *buf)
273{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800274 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700276 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
James Simmonsc14e2cf2005-10-24 21:46:21 +0100279static ssize_t show_stride(struct class_device *class_device, char *buf)
280{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800281 struct fb_info *fb_info = class_get_devdata(class_device);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100282 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static ssize_t store_blank(struct class_device *class_device, const char * buf,
286 size_t count)
287{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800288 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 char *last = NULL;
290 int err;
291
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;
300}
301
302static ssize_t show_blank(struct class_device *class_device, char *buf)
303{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800304// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306}
307
308static ssize_t store_console(struct class_device *class_device,
309 const char * buf, size_t count)
310{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800311// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313}
314
315static ssize_t show_console(struct class_device *class_device, char *buf)
316{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800317// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321static ssize_t store_cursor(struct class_device *class_device,
322 const char * buf, size_t count)
323{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800324// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
328static ssize_t show_cursor(struct class_device *class_device, char *buf)
329{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800330// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332}
333
334static ssize_t store_pan(struct class_device *class_device, const char * buf,
335 size_t count)
336{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800337 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct fb_var_screeninfo var;
339 char *last = NULL;
340 int err;
341
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);
348
349 acquire_console_sem();
350 err = fb_pan_display(fb_info, &var);
351 release_console_sem();
352
353 if (err < 0)
354 return err;
355 return count;
356}
357
358static ssize_t show_pan(struct class_device *class_device, char *buf)
359{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800360 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
362 fb_info->var.xoffset);
363}
364
James Simmons02459ea2005-07-30 23:49:54 +0100365static ssize_t show_name(struct class_device *class_device, char *buf)
366{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800367 struct fb_info *fb_info = class_get_devdata(class_device);
James Simmons02459ea2005-07-30 23:49:54 +0100368
369 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
370}
371
Matthew Garrett30420f82006-01-09 20:53:01 -0800372static ssize_t store_fbstate(struct class_device *class_device,
373 const char *buf, size_t count)
374{
375 struct fb_info *fb_info = class_get_devdata(class_device);
376 u32 state;
377 char *last = NULL;
378
379 state = simple_strtoul(buf, &last, 0);
380
381 acquire_console_sem();
382 fb_set_suspend(fb_info, (int)state);
383 release_console_sem();
384
385 return count;
386}
387
388static ssize_t show_fbstate(struct class_device *class_device, char *buf)
389{
390 struct fb_info *fb_info = class_get_devdata(class_device);
391 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
392}
393
Michael Hanselmann5474c122006-06-25 05:47:08 -0700394#ifdef CONFIG_FB_BACKLIGHT
395static ssize_t store_bl_curve(struct class_device *class_device,
396 const char *buf, size_t count)
397{
398 struct fb_info *fb_info = class_get_devdata(class_device);
399 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
400 unsigned int i;
401
Michael Hanselmann25981de2006-09-25 16:25:07 -0700402 /* Some drivers don't use framebuffer_alloc(), but those also
403 * don't have backlights.
404 */
405 if (!fb_info || !fb_info->bl_dev)
406 return -ENODEV;
407
Michael Hanselmann5474c122006-06-25 05:47:08 -0700408 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
409 return -EINVAL;
410
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;
423
424 /* If there has been an error in the input data, we won't
425 * reach this loop.
426 */
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);
431
432 return count;
433}
434
435static ssize_t show_bl_curve(struct class_device *class_device, char *buf)
436{
437 struct fb_info *fb_info = class_get_devdata(class_device);
438 ssize_t len = 0;
439 unsigned int i;
440
Michael Hanselmann25981de2006-09-25 16:25:07 -0700441 /* Some drivers don't use framebuffer_alloc(), but those also
442 * don't have backlights.
443 */
444 if (!fb_info || !fb_info->bl_dev)
445 return -ENODEV;
446
Michael Hanselmann5474c122006-06-25 05:47:08 -0700447 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);
460
461 return len;
462}
463#endif
464
Jon Smirl913e7ec2006-04-12 19:43:35 -0400465/* 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 */
Adrian Bunk75c96f82005-05-05 16:16:09 -0700468static struct class_device_attribute class_device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
470 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 __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),
James Simmons02459ea2005-07-30 23:49:54 +0100477 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100478 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800479 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
Matthew Garrett30420f82006-01-09 20:53:01 -0800480 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Michael Hanselmann5474c122006-06-25 05:47:08 -0700481#ifdef CONFIG_FB_BACKLIGHT
482 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484};
485
486int fb_init_class_device(struct fb_info *fb_info)
487{
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700488 int i, error = 0;
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 class_set_devdata(fb_info->class_device, fb_info);
491
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700492 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
493
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]);
497
498 if (error)
499 break;
500 }
501
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;
507 }
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510}
511
512void fb_cleanup_class_device(struct fb_info *fb_info)
513{
514 unsigned int i;
515
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700516 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]);
520
521 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Michael Hanselmann5474c122006-06-25 05:47:08 -0700525#ifdef CONFIG_FB_BACKLIGHT
526/* This function generates a linear backlight curve
527 *
528 * 0: off
529 * 1-7: min
530 * 8-127: linear from min to max
531 */
532void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
533{
534 unsigned int i, flat, count, range = (max - min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Michael Hanselmann5474c122006-06-25 05:47:08 -0700536 fb_info->bl_curve[0] = off;
537
538 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
539 fb_info->bl_curve[flat] = min;
540
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);
544}
545EXPORT_SYMBOL_GPL(fb_bl_default_curve);
546#endif