Driver core: convert firmware code to use struct device
[linux-block.git] / drivers / video / fbsysfs.c
CommitLineData
1da177e4
LT
1/*
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>
5474c120 21#include <linux/module.h>
1da177e4 22
1a6600be
AD
23#define FB_SYSFS_FLAG_ATTR 1
24
1da177e4
LT
25/**
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
def1eded
AD
49 p = kzalloc(fb_info_size + size, GFP_KERNEL);
50
1da177e4
LT
51 if (!p)
52 return NULL;
def1eded 53
1da177e4
LT
54 info = (struct fb_info *) p;
55
56 if (size)
57 info->par = p + fb_info_size;
58
59 info->device = dev;
60
5474c120
MH
61#ifdef CONFIG_FB_BACKLIGHT
62 mutex_init(&info->bl_mutex);
63#endif
64
1da177e4
LT
65 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';
9dac73a4
DT
105 char v = 'p';
106
1da177e4
LT
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';
9dac73a4
DT
113
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);
1da177e4
LT
121}
122
123static ssize_t store_mode(struct class_device *class_device, const char * buf,
124 size_t count)
125{
313e58ab 126 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
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;
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{
313e58ab 156 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
157
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{
313e58ab 167 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
168 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{
313e58ab 191 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
192 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{
313e58ab 209 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
210 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{
313e58ab 223 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
224 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
225}
226
a812c94b
AD
227static 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
1da177e4
LT
252static ssize_t store_virtual(struct class_device *class_device,
253 const char * buf, size_t count)
254{
313e58ab 255 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
256 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);
1da177e4
LT
266
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{
313e58ab 274 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4 275 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
e2c16499 276 fb_info->var.yres_virtual);
1da177e4
LT
277}
278
c14e2cfc
JS
279static ssize_t show_stride(struct class_device *class_device, char *buf)
280{
313e58ab 281 struct fb_info *fb_info = class_get_devdata(class_device);
c14e2cfc
JS
282 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
283}
284
1da177e4
LT
285static ssize_t store_blank(struct class_device *class_device, const char * buf,
286 size_t count)
287{
313e58ab 288 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
289 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{
313e58ab 304// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
305 return 0;
306}
307
308static ssize_t store_console(struct class_device *class_device,
309 const char * buf, size_t count)
310{
313e58ab 311// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
312 return 0;
313}
314
315static ssize_t show_console(struct class_device *class_device, char *buf)
316{
313e58ab 317// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
318 return 0;
319}
320
321static ssize_t store_cursor(struct class_device *class_device,
322 const char * buf, size_t count)
323{
313e58ab 324// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
325 return 0;
326}
327
328static ssize_t show_cursor(struct class_device *class_device, char *buf)
329{
313e58ab 330// struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
331 return 0;
332}
333
334static ssize_t store_pan(struct class_device *class_device, const char * buf,
335 size_t count)
336{
313e58ab 337 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
338 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{
313e58ab 360 struct fb_info *fb_info = class_get_devdata(class_device);
1da177e4
LT
361 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
362 fb_info->var.xoffset);
363}
364
02459eaa
JS
365static ssize_t show_name(struct class_device *class_device, char *buf)
366{
313e58ab 367 struct fb_info *fb_info = class_get_devdata(class_device);
02459eaa
JS
368
369 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
370}
371
30420f8f
MG
372static 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
5474c120
MH
394#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
25981de5
MH
402 /* 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
5474c120
MH
408 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
25981de5
MH
441 /* 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
5474c120
MH
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);
460
461 return len;
462}
463#endif
464
913e7ec5
JS
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 */
75c96f85 468static struct class_device_attribute class_device_attrs[] = {
1da177e4
LT
469 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
470 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
1da177e4
LT
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),
02459eaa 477 __ATTR(name, S_IRUGO, show_name, NULL),
c14e2cfc 478 __ATTR(stride, S_IRUGO, show_stride, NULL),
a812c94b 479 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
30420f8f 480 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
5474c120
MH
481#ifdef CONFIG_FB_BACKLIGHT
482 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
483#endif
1da177e4
LT
484};
485
486int fb_init_class_device(struct fb_info *fb_info)
487{
1a6600be
AD
488 int i, error = 0;
489
1da177e4
LT
490 class_set_devdata(fb_info->class_device, fb_info);
491
1a6600be
AD
492 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
1da177e4
LT
509 return 0;
510}
511
512void fb_cleanup_class_device(struct fb_info *fb_info)
513{
514 unsigned int i;
515
1a6600be
AD
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]);
520
521 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
522 }
1da177e4
LT
523}
524
5474c120
MH
525#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);
535
536 fb_info->bl_curve[0] = off;
1da177e4 537
5474c120
MH
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