2 * fbsysfs.c - framebuffer device class and attributes
4 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
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>
20 #include <linux/console.h>
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);
44 fb_info_size += PADDING;
46 p = kmalloc(fb_info_size + size, GFP_KERNEL);
49 memset(p, 0, fb_info_size + size);
50 info = (struct fb_info *) p;
53 info->par = p + fb_info_size;
61 EXPORT_SYMBOL(framebuffer_alloc);
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)
76 EXPORT_SYMBOL(framebuffer_release);
78 static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
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();
93 static int mode_string(char *buf, unsigned int offset,
94 const struct fb_videomode *mode)
97 if (mode->flag & FB_MODE_IS_DETAILED)
99 if (mode->flag & FB_MODE_IS_VESA)
101 if (mode->flag & FB_MODE_IS_STANDARD)
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,
109 struct fb_info *fb_info =
110 (struct fb_info *)class_get_devdata(class_device);
112 struct fb_var_screeninfo var;
113 struct fb_modelist *modelist;
114 struct fb_videomode *mode;
115 struct list_head *pos;
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) {
128 fb_videomode_to_var(&var, mode);
129 if ((err = activate(fb_info, &var)))
131 fb_info->mode = mode;
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);
146 return mode_string(buf, 0, fb_info->mode);
149 static ssize_t store_modes(struct class_device *class_device, const char * buf,
152 struct fb_info *fb_info =
153 (struct fb_info *)class_get_devdata(class_device);
155 int i = count / sizeof(struct fb_videomode);
157 if (i * sizeof(struct fb_videomode) != count)
160 acquire_console_sem();
161 list_splice(&fb_info->modelist, &old_list);
162 fb_videomode_to_modelist((struct fb_videomode *)buf, i,
164 if (fb_new_modelist(fb_info)) {
165 fb_destroy_modelist(&fb_info->modelist);
166 list_splice(&old_list, &fb_info->modelist);
168 fb_destroy_modelist(&old_list);
170 release_console_sem();
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);
180 struct list_head *pos;
181 struct fb_modelist *modelist;
182 const struct fb_videomode *mode;
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);
193 static ssize_t store_bpp(struct class_device *class_device, const char * buf,
196 struct fb_info *fb_info =
197 (struct fb_info *)class_get_devdata(class_device);
198 struct fb_var_screeninfo var;
203 var.bits_per_pixel = simple_strtoul(buf, last, 0);
204 if ((err = activate(fb_info, &var)))
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_rotate(struct class_device *class_device, const char *buf,
219 struct fb_info *fb_info = class_get_devdata(class_device);
220 struct fb_var_screeninfo var;
225 var.rotate = simple_strtoul(buf, last, 0);
227 if ((err = activate(fb_info, &var)))
234 static ssize_t show_rotate(struct class_device *class_device, char *buf)
236 struct fb_info *fb_info = class_get_devdata(class_device);
238 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
241 static ssize_t store_con_rotate(struct class_device *class_device,
242 const char *buf, size_t count)
244 struct fb_info *fb_info = class_get_devdata(class_device);
248 acquire_console_sem();
249 rotate = simple_strtoul(buf, last, 0);
250 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
251 release_console_sem();
255 static ssize_t store_con_rotate_all(struct class_device *class_device,
256 const char *buf, size_t count)
258 struct fb_info *fb_info = class_get_devdata(class_device);
262 acquire_console_sem();
263 rotate = simple_strtoul(buf, last, 0);
264 fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
265 release_console_sem();
269 static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
271 struct fb_info *fb_info = class_get_devdata(class_device);
274 acquire_console_sem();
275 rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
276 release_console_sem();
277 return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
280 static ssize_t store_virtual(struct class_device *class_device,
281 const char * buf, size_t count)
283 struct fb_info *fb_info =
284 (struct fb_info *)class_get_devdata(class_device);
285 struct fb_var_screeninfo var;
290 var.xres_virtual = simple_strtoul(buf, &last, 0);
292 if (last - buf >= count)
294 var.yres_virtual = simple_strtoul(last, &last, 0);
296 if ((err = activate(fb_info, &var)))
301 static ssize_t show_virtual(struct class_device *class_device, char *buf)
303 struct fb_info *fb_info =
304 (struct fb_info *)class_get_devdata(class_device);
305 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
306 fb_info->var.yres_virtual);
309 static ssize_t show_stride(struct class_device *class_device, char *buf)
311 struct fb_info *fb_info =
312 (struct fb_info *)class_get_devdata(class_device);
313 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
316 /* Format for cmap is "%02x%c%4x%4x%4x\n" */
317 /* %02x entry %c transp %4x red %4x blue %4x green \n */
318 /* 256 rows at 16 chars equals 4096, the normal page size */
319 /* the code will automatically adjust for different page sizes */
320 static ssize_t store_cmap(struct class_device *class_device, const char *buf,
323 struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
324 int rc, i, start, length, transp = 0;
326 if ((count > PAGE_SIZE) || ((count % 16) != 0))
329 if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap)
332 sscanf(buf, "%02x", &start);
335 for (i = 0; i < length; i++)
336 if (buf[i * 16 + 2] != ' ')
339 /* If we can batch, do it */
340 if (fb_info->fbops->fb_setcmap && length > 1) {
343 memset(&umap, 0, sizeof(umap));
344 if ((rc = fb_alloc_cmap(&umap, length, transp)))
348 for (i = 0; i < length; i++) {
349 sscanf(&buf[i * 16 + 3], "%4hx", &umap.red[i]);
350 sscanf(&buf[i * 16 + 7], "%4hx", &umap.blue[i]);
351 sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]);
353 umap.transp[i] = (buf[i * 16 + 2] != ' ');
355 rc = fb_info->fbops->fb_setcmap(&umap, fb_info);
356 fb_copy_cmap(&umap, &fb_info->cmap);
357 fb_dealloc_cmap(&umap);
361 for (i = 0; i < length; i++) {
362 u16 red, blue, green, tsp;
364 sscanf(&buf[i * 16 + 3], "%4hx", &red);
365 sscanf(&buf[i * 16 + 7], "%4hx", &blue);
366 sscanf(&buf[i * 16 + 11], "%4hx", &green);
367 tsp = (buf[i * 16 + 2] != ' ');
368 if ((rc = fb_info->fbops->fb_setcolreg(start++,
369 red, green, blue, tsp, fb_info)))
372 fb_info->cmap.red[i] = red;
373 fb_info->cmap.blue[i] = blue;
374 fb_info->cmap.green[i] = green;
376 fb_info->cmap.transp[i] = tsp;
381 static ssize_t show_cmap(struct class_device *class_device, char *buf)
383 struct fb_info *fb_info =
384 (struct fb_info *)class_get_devdata(class_device);
387 if (!fb_info->cmap.red || !fb_info->cmap.blue ||
388 !fb_info->cmap.green)
391 if (fb_info->cmap.len > PAGE_SIZE / 16)
394 /* don't mess with the format, the buffer is PAGE_SIZE */
395 /* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */
396 for (i = 0; i < fb_info->cmap.len; i++) {
397 snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start,
398 ((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '),
399 fb_info->cmap.red[i], fb_info->cmap.blue[i],
400 fb_info->cmap.green[i]);
402 return 16 * fb_info->cmap.len;
405 static ssize_t store_blank(struct class_device *class_device, const char * buf,
408 struct fb_info *fb_info =
409 (struct fb_info *)class_get_devdata(class_device);
413 acquire_console_sem();
414 fb_info->flags |= FBINFO_MISC_USEREVENT;
415 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
416 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
417 release_console_sem();
423 static ssize_t show_blank(struct class_device *class_device, char *buf)
425 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
429 static ssize_t store_console(struct class_device *class_device,
430 const char * buf, size_t count)
432 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
436 static ssize_t show_console(struct class_device *class_device, char *buf)
438 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
442 static ssize_t store_cursor(struct class_device *class_device,
443 const char * buf, size_t count)
445 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
449 static ssize_t show_cursor(struct class_device *class_device, char *buf)
451 // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
455 static ssize_t store_pan(struct class_device *class_device, const char * buf,
458 struct fb_info *fb_info =
459 (struct fb_info *)class_get_devdata(class_device);
460 struct fb_var_screeninfo var;
465 var.xoffset = simple_strtoul(buf, &last, 0);
467 if (last - buf >= count)
469 var.yoffset = simple_strtoul(last, &last, 0);
471 acquire_console_sem();
472 err = fb_pan_display(fb_info, &var);
473 release_console_sem();
480 static ssize_t show_pan(struct class_device *class_device, char *buf)
482 struct fb_info *fb_info =
483 (struct fb_info *)class_get_devdata(class_device);
484 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
485 fb_info->var.xoffset);
488 static ssize_t show_name(struct class_device *class_device, char *buf)
490 struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device);
492 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
495 static struct class_device_attribute class_device_attrs[] = {
496 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
497 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
498 __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
499 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
500 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
501 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
502 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
503 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
504 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
505 __ATTR(name, S_IRUGO, show_name, NULL),
506 __ATTR(stride, S_IRUGO, show_stride, NULL),
507 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
508 __ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
509 __ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
512 int fb_init_class_device(struct fb_info *fb_info)
515 class_set_devdata(fb_info->class_device, fb_info);
517 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
518 class_device_create_file(fb_info->class_device,
519 &class_device_attrs[i]);
523 void fb_cleanup_class_device(struct fb_info *fb_info)
527 for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
528 class_device_remove_file(fb_info->class_device,
529 &class_device_attrs[i]);