blob: 6d26057337e262e1c762ae2de279a02a9054273d [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>
21
22/**
23 * framebuffer_alloc - creates a new frame buffer info structure
24 *
25 * @size: size of driver private data, can be zero
26 * @dev: pointer to the device for this fb, this can be NULL
27 *
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).
31 *
32 * Returns the new structure, or NULL if an error occured.
33 *
34 */
35struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
36{
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;
42
43 if (size)
44 fb_info_size += PADDING;
45
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080046 p = kzalloc(fb_info_size + size, GFP_KERNEL);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (!p)
49 return NULL;
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 info = (struct fb_info *) p;
52
53 if (size)
54 info->par = p + fb_info_size;
55
56 info->device = dev;
57
58 return info;
59#undef PADDING
60#undef BYTES_PER_LONG
61}
62EXPORT_SYMBOL(framebuffer_alloc);
63
64/**
65 * framebuffer_release - marks the structure available for freeing
66 *
67 * @info: frame buffer info structure
68 *
69 * Drop the reference count of the class_device embedded in the
70 * framebuffer info structure.
71 *
72 */
73void framebuffer_release(struct fb_info *info)
74{
75 kfree(info);
76}
77EXPORT_SYMBOL(framebuffer_release);
78
79static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
80{
81 int err;
82
83 var->activate |= FB_ACTIVATE_FORCE;
84 acquire_console_sem();
85 fb_info->flags |= FBINFO_MISC_USEREVENT;
86 err = fb_set_var(fb_info, var);
87 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
88 release_console_sem();
89 if (err)
90 return err;
91 return 0;
92}
93
94static int mode_string(char *buf, unsigned int offset,
95 const struct fb_videomode *mode)
96{
97 char m = 'U';
98 if (mode->flag & FB_MODE_IS_DETAILED)
99 m = 'D';
100 if (mode->flag & FB_MODE_IS_VESA)
101 m = 'V';
102 if (mode->flag & FB_MODE_IS_STANDARD)
103 m = 'S';
104 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh);
105}
106
107static ssize_t store_mode(struct class_device *class_device, const char * buf,
108 size_t count)
109{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800110 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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;
118
119 memset(&var, 0, sizeof(var));
120
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) {
126
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;
133 }
134 }
135 return -EINVAL;
136}
137
138static ssize_t show_mode(struct class_device *class_device, char *buf)
139{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800140 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (!fb_info->mode)
143 return 0;
144
145 return mode_string(buf, 0, fb_info->mode);
146}
147
148static ssize_t store_modes(struct class_device *class_device, const char * buf,
149 size_t count)
150{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800151 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 LIST_HEAD(old_list);
153 int i = count / sizeof(struct fb_videomode);
154
155 if (i * sizeof(struct fb_videomode) != count)
156 return -EINVAL;
157
158 acquire_console_sem();
159 list_splice(&fb_info->modelist, &old_list);
160 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
161 &fb_info->modelist);
162 if (fb_new_modelist(fb_info)) {
163 fb_destroy_modelist(&fb_info->modelist);
164 list_splice(&old_list, &fb_info->modelist);
165 } else
166 fb_destroy_modelist(&old_list);
167
168 release_console_sem();
169
170 return 0;
171}
172
173static ssize_t show_modes(struct class_device *class_device, char *buf)
174{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800175 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned int i;
177 struct list_head *pos;
178 struct fb_modelist *modelist;
179 const struct fb_videomode *mode;
180
181 i = 0;
182 list_for_each(pos, &fb_info->modelist) {
183 modelist = list_entry(pos, struct fb_modelist, list);
184 mode = &modelist->mode;
185 i += mode_string(buf, i, mode);
186 }
187 return i;
188}
189
190static ssize_t store_bpp(struct class_device *class_device, const char * buf,
191 size_t count)
192{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800193 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct fb_var_screeninfo var;
195 char ** last = NULL;
196 int err;
197
198 var = fb_info->var;
199 var.bits_per_pixel = simple_strtoul(buf, last, 0);
200 if ((err = activate(fb_info, &var)))
201 return err;
202 return count;
203}
204
205static ssize_t show_bpp(struct class_device *class_device, char *buf)
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 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
209}
210
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800211static ssize_t store_rotate(struct class_device *class_device, const char *buf,
212 size_t count)
213{
214 struct fb_info *fb_info = class_get_devdata(class_device);
215 struct fb_var_screeninfo var;
216 char **last = NULL;
217 int err;
218
219 var = fb_info->var;
220 var.rotate = simple_strtoul(buf, last, 0);
221
222 if ((err = activate(fb_info, &var)))
223 return err;
224
225 return count;
226}
227
228
229static ssize_t show_rotate(struct class_device *class_device, char *buf)
230{
231 struct fb_info *fb_info = class_get_devdata(class_device);
232
233 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
234}
235
236static ssize_t store_con_rotate(struct class_device *class_device,
237 const char *buf, size_t count)
238{
239 struct fb_info *fb_info = class_get_devdata(class_device);
240 int rotate;
241 char **last = NULL;
242
243 acquire_console_sem();
244 rotate = simple_strtoul(buf, last, 0);
245 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
246 release_console_sem();
247 return count;
248}
249
250static ssize_t store_con_rotate_all(struct class_device *class_device,
251 const char *buf, size_t count)
252{
253 struct fb_info *fb_info = class_get_devdata(class_device);
254 int rotate;
255 char **last = NULL;
256
257 acquire_console_sem();
258 rotate = simple_strtoul(buf, last, 0);
259 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
260 release_console_sem();
261 return count;
262}
263
264static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
265{
266 struct fb_info *fb_info = class_get_devdata(class_device);
267 int rotate;
268
269 acquire_console_sem();
270 rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
271 release_console_sem();
272 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static ssize_t store_virtual(struct class_device *class_device,
276 const char * buf, size_t count)
277{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800278 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct fb_var_screeninfo var;
280 char *last = NULL;
281 int err;
282
283 var = fb_info->var;
284 var.xres_virtual = simple_strtoul(buf, &last, 0);
285 last++;
286 if (last - buf >= count)
287 return -EINVAL;
288 var.yres_virtual = simple_strtoul(last, &last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if ((err = activate(fb_info, &var)))
291 return err;
292 return count;
293}
294
295static ssize_t show_virtual(struct class_device *class_device, char *buf)
296{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800297 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700299 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
James Simmonsc14e2cf2005-10-24 21:46:21 +0100302static ssize_t show_stride(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);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100305 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
306}
307
Jon Smirld2102242005-07-27 11:46:05 -0700308/* Format for cmap is "%02x%c%4x%4x%4x\n" */
309/* %02x entry %c transp %4x red %4x blue %4x green \n */
Jon Smirlf0b9d792005-07-28 21:16:19 -0700310/* 256 rows at 16 chars equals 4096, the normal page size */
311/* the code will automatically adjust for different page sizes */
Jon Smirld2102242005-07-27 11:46:05 -0700312static ssize_t store_cmap(struct class_device *class_device, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 size_t count)
314{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800315 struct fb_info *fb_info = class_get_devdata(class_device);
Jon Smirld2102242005-07-27 11:46:05 -0700316 int rc, i, start, length, transp = 0;
317
Jon Smirlf0b9d792005-07-28 21:16:19 -0700318 if ((count > PAGE_SIZE) || ((count % 16) != 0))
Jon Smirld2102242005-07-27 11:46:05 -0700319 return -EINVAL;
320
321 if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap)
322 return -EINVAL;
323
324 sscanf(buf, "%02x", &start);
325 length = count / 16;
326
327 for (i = 0; i < length; i++)
328 if (buf[i * 16 + 2] != ' ')
329 transp = 1;
330
331 /* If we can batch, do it */
332 if (fb_info->fbops->fb_setcmap && length > 1) {
333 struct fb_cmap umap;
334
335 memset(&umap, 0, sizeof(umap));
336 if ((rc = fb_alloc_cmap(&umap, length, transp)))
337 return rc;
338
339 umap.start = start;
340 for (i = 0; i < length; i++) {
341 sscanf(&buf[i * 16 + 3], "%4hx", &umap.red[i]);
342 sscanf(&buf[i * 16 + 7], "%4hx", &umap.blue[i]);
343 sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]);
344 if (transp)
345 umap.transp[i] = (buf[i * 16 + 2] != ' ');
346 }
347 rc = fb_info->fbops->fb_setcmap(&umap, fb_info);
348 fb_copy_cmap(&umap, &fb_info->cmap);
349 fb_dealloc_cmap(&umap);
350
351 return rc;
352 }
353 for (i = 0; i < length; i++) {
354 u16 red, blue, green, tsp;
355
356 sscanf(&buf[i * 16 + 3], "%4hx", &red);
357 sscanf(&buf[i * 16 + 7], "%4hx", &blue);
358 sscanf(&buf[i * 16 + 11], "%4hx", &green);
359 tsp = (buf[i * 16 + 2] != ' ');
360 if ((rc = fb_info->fbops->fb_setcolreg(start++,
361 red, green, blue, tsp, fb_info)))
362 return rc;
363
364 fb_info->cmap.red[i] = red;
365 fb_info->cmap.blue[i] = blue;
366 fb_info->cmap.green[i] = green;
367 if (transp)
368 fb_info->cmap.transp[i] = tsp;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
373static ssize_t show_cmap(struct class_device *class_device, char *buf)
374{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800375 struct fb_info *fb_info = class_get_devdata(class_device);
Jon Smirld2102242005-07-27 11:46:05 -0700376 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (!fb_info->cmap.red || !fb_info->cmap.blue ||
Jon Smirld2102242005-07-27 11:46:05 -0700379 !fb_info->cmap.green)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -EINVAL;
381
Jon Smirlf0b9d792005-07-28 21:16:19 -0700382 if (fb_info->cmap.len > PAGE_SIZE / 16)
Jon Smirld2102242005-07-27 11:46:05 -0700383 return -EINVAL;
384
385 /* don't mess with the format, the buffer is PAGE_SIZE */
Jon Smirlf0b9d792005-07-28 21:16:19 -0700386 /* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 for (i = 0; i < fb_info->cmap.len; i++) {
Jon Smirlf0b9d792005-07-28 21:16:19 -0700388 snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start,
Jon Smirld2102242005-07-27 11:46:05 -0700389 ((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '),
390 fb_info->cmap.red[i], fb_info->cmap.blue[i],
391 fb_info->cmap.green[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Jon Smirlf0b9d792005-07-28 21:16:19 -0700393 return 16 * fb_info->cmap.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396static ssize_t store_blank(struct class_device *class_device, const char * buf,
397 size_t count)
398{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800399 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 char *last = NULL;
401 int err;
402
403 acquire_console_sem();
404 fb_info->flags |= FBINFO_MISC_USEREVENT;
405 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
406 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
407 release_console_sem();
408 if (err < 0)
409 return err;
410 return count;
411}
412
413static ssize_t show_blank(struct class_device *class_device, char *buf)
414{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800415// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417}
418
419static ssize_t store_console(struct class_device *class_device,
420 const char * buf, size_t count)
421{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800422// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
426static ssize_t show_console(struct class_device *class_device, char *buf)
427{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800428// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432static ssize_t store_cursor(struct class_device *class_device,
433 const char * buf, size_t count)
434{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800435// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439static ssize_t show_cursor(struct class_device *class_device, char *buf)
440{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800441// struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
443}
444
445static ssize_t store_pan(struct class_device *class_device, const char * buf,
446 size_t count)
447{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800448 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct fb_var_screeninfo var;
450 char *last = NULL;
451 int err;
452
453 var = fb_info->var;
454 var.xoffset = simple_strtoul(buf, &last, 0);
455 last++;
456 if (last - buf >= count)
457 return -EINVAL;
458 var.yoffset = simple_strtoul(last, &last, 0);
459
460 acquire_console_sem();
461 err = fb_pan_display(fb_info, &var);
462 release_console_sem();
463
464 if (err < 0)
465 return err;
466 return count;
467}
468
469static ssize_t show_pan(struct class_device *class_device, char *buf)
470{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800471 struct fb_info *fb_info = class_get_devdata(class_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
473 fb_info->var.xoffset);
474}
475
James Simmons02459ea2005-07-30 23:49:54 +0100476static ssize_t show_name(struct class_device *class_device, char *buf)
477{
Antonino A. Daplas313e58a2006-01-09 20:53:12 -0800478 struct fb_info *fb_info = class_get_devdata(class_device);
James Simmons02459ea2005-07-30 23:49:54 +0100479
480 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
481}
482
Matthew Garrett30420f82006-01-09 20:53:01 -0800483static ssize_t store_fbstate(struct class_device *class_device,
484 const char *buf, size_t count)
485{
486 struct fb_info *fb_info = class_get_devdata(class_device);
487 u32 state;
488 char *last = NULL;
489
490 state = simple_strtoul(buf, &last, 0);
491
492 acquire_console_sem();
493 fb_set_suspend(fb_info, (int)state);
494 release_console_sem();
495
496 return count;
497}
498
499static ssize_t show_fbstate(struct class_device *class_device, char *buf)
500{
501 struct fb_info *fb_info = class_get_devdata(class_device);
502 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
503}
504
Adrian Bunk75c96f82005-05-05 16:16:09 -0700505static struct class_device_attribute class_device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
507 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
508 __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
509 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
510 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
511 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
512 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
513 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
514 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
James Simmons02459ea2005-07-30 23:49:54 +0100515 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100516 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800517 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
518 __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
519 __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
Matthew Garrett30420f82006-01-09 20:53:01 -0800520 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
523int fb_init_class_device(struct fb_info *fb_info)
524{
525 unsigned int i;
526 class_set_devdata(fb_info->class_device, fb_info);
527
528 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
529 class_device_create_file(fb_info->class_device,
530 &class_device_attrs[i]);
531 return 0;
532}
533
534void fb_cleanup_class_device(struct fb_info *fb_info)
535{
536 unsigned int i;
537
538 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
539 class_device_remove_file(fb_info->class_device,
540 &class_device_attrs[i]);
541}
542
543