Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/fb.h> |
| 21 | #include <linux/console.h> |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 24 | #define FB_SYSFS_FLAG_ATTR 1 |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /** |
| 27 | * framebuffer_alloc - creates a new frame buffer info structure |
| 28 | * |
| 29 | * @size: size of driver private data, can be zero |
| 30 | * @dev: pointer to the device for this fb, this can be NULL |
| 31 | * |
| 32 | * Creates a new frame buffer info structure. Also reserves @size bytes |
| 33 | * for driver private data (info->par). info->par (if any) will be |
| 34 | * aligned to sizeof(long). |
| 35 | * |
| 36 | * Returns the new structure, or NULL if an error occured. |
| 37 | * |
| 38 | */ |
| 39 | struct fb_info *framebuffer_alloc(size_t size, struct device *dev) |
| 40 | { |
| 41 | #define BYTES_PER_LONG (BITS_PER_LONG/8) |
| 42 | #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG)) |
| 43 | int fb_info_size = sizeof(struct fb_info); |
| 44 | struct fb_info *info; |
| 45 | char *p; |
| 46 | |
| 47 | if (size) |
| 48 | fb_info_size += PADDING; |
| 49 | |
Antonino A. Daplas | def1ede | 2006-01-09 20:53:45 -0800 | [diff] [blame] | 50 | p = kzalloc(fb_info_size + size, GFP_KERNEL); |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (!p) |
| 53 | return NULL; |
Antonino A. Daplas | def1ede | 2006-01-09 20:53:45 -0800 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | info = (struct fb_info *) p; |
| 56 | |
| 57 | if (size) |
| 58 | info->par = p + fb_info_size; |
| 59 | |
| 60 | info->device = dev; |
| 61 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 62 | #ifdef CONFIG_FB_BACKLIGHT |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 63 | mutex_init(&info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 64 | #endif |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return info; |
| 67 | #undef PADDING |
| 68 | #undef BYTES_PER_LONG |
| 69 | } |
| 70 | EXPORT_SYMBOL(framebuffer_alloc); |
| 71 | |
| 72 | /** |
| 73 | * framebuffer_release - marks the structure available for freeing |
| 74 | * |
| 75 | * @info: frame buffer info structure |
| 76 | * |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 77 | * Drop the reference count of the device embedded in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * framebuffer info structure. |
| 79 | * |
| 80 | */ |
| 81 | void framebuffer_release(struct fb_info *info) |
| 82 | { |
| 83 | kfree(info); |
| 84 | } |
| 85 | EXPORT_SYMBOL(framebuffer_release); |
| 86 | |
| 87 | static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var) |
| 88 | { |
| 89 | int err; |
| 90 | |
| 91 | var->activate |= FB_ACTIVATE_FORCE; |
| 92 | acquire_console_sem(); |
| 93 | fb_info->flags |= FBINFO_MISC_USEREVENT; |
| 94 | err = fb_set_var(fb_info, var); |
| 95 | fb_info->flags &= ~FBINFO_MISC_USEREVENT; |
| 96 | release_console_sem(); |
| 97 | if (err) |
| 98 | return err; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int mode_string(char *buf, unsigned int offset, |
| 103 | const struct fb_videomode *mode) |
| 104 | { |
| 105 | char m = 'U'; |
Daniel R Thompson | 9dac73a | 2006-06-26 00:27:00 -0700 | [diff] [blame] | 106 | char v = 'p'; |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (mode->flag & FB_MODE_IS_DETAILED) |
| 109 | m = 'D'; |
| 110 | if (mode->flag & FB_MODE_IS_VESA) |
| 111 | m = 'V'; |
| 112 | if (mode->flag & FB_MODE_IS_STANDARD) |
| 113 | m = 'S'; |
Daniel R Thompson | 9dac73a | 2006-06-26 00:27:00 -0700 | [diff] [blame] | 114 | |
| 115 | if (mode->vmode & FB_VMODE_INTERLACED) |
| 116 | v = 'i'; |
| 117 | if (mode->vmode & FB_VMODE_DOUBLE) |
| 118 | v = 'd'; |
| 119 | |
| 120 | return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n", |
| 121 | m, mode->xres, mode->yres, v, mode->refresh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 124 | static ssize_t store_mode(struct device *device, struct device_attribute *attr, |
| 125 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 127 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | char mstr[100]; |
| 129 | struct fb_var_screeninfo var; |
| 130 | struct fb_modelist *modelist; |
| 131 | struct fb_videomode *mode; |
| 132 | struct list_head *pos; |
| 133 | size_t i; |
| 134 | int err; |
| 135 | |
| 136 | memset(&var, 0, sizeof(var)); |
| 137 | |
| 138 | list_for_each(pos, &fb_info->modelist) { |
| 139 | modelist = list_entry(pos, struct fb_modelist, list); |
| 140 | mode = &modelist->mode; |
| 141 | i = mode_string(mstr, 0, mode); |
| 142 | if (strncmp(mstr, buf, max(count, i)) == 0) { |
| 143 | |
| 144 | var = fb_info->var; |
| 145 | fb_videomode_to_var(&var, mode); |
| 146 | if ((err = activate(fb_info, &var))) |
| 147 | return err; |
| 148 | fb_info->mode = mode; |
| 149 | return count; |
| 150 | } |
| 151 | } |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 155 | static ssize_t show_mode(struct device *device, struct device_attribute *attr, |
| 156 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 158 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | if (!fb_info->mode) |
| 161 | return 0; |
| 162 | |
| 163 | return mode_string(buf, 0, fb_info->mode); |
| 164 | } |
| 165 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 166 | static ssize_t store_modes(struct device *device, |
| 167 | struct device_attribute *attr, |
| 168 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 170 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | LIST_HEAD(old_list); |
| 172 | int i = count / sizeof(struct fb_videomode); |
| 173 | |
| 174 | if (i * sizeof(struct fb_videomode) != count) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | acquire_console_sem(); |
| 178 | list_splice(&fb_info->modelist, &old_list); |
Geert Uytterhoeven | 9791d76 | 2007-02-12 00:55:19 -0800 | [diff] [blame] | 179 | fb_videomode_to_modelist((const struct fb_videomode *)buf, i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | &fb_info->modelist); |
| 181 | if (fb_new_modelist(fb_info)) { |
| 182 | fb_destroy_modelist(&fb_info->modelist); |
| 183 | list_splice(&old_list, &fb_info->modelist); |
| 184 | } else |
| 185 | fb_destroy_modelist(&old_list); |
| 186 | |
| 187 | release_console_sem(); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 192 | static ssize_t show_modes(struct device *device, struct device_attribute *attr, |
| 193 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 195 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | unsigned int i; |
| 197 | struct list_head *pos; |
| 198 | struct fb_modelist *modelist; |
| 199 | const struct fb_videomode *mode; |
| 200 | |
| 201 | i = 0; |
| 202 | list_for_each(pos, &fb_info->modelist) { |
| 203 | modelist = list_entry(pos, struct fb_modelist, list); |
| 204 | mode = &modelist->mode; |
| 205 | i += mode_string(buf, i, mode); |
| 206 | } |
| 207 | return i; |
| 208 | } |
| 209 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 210 | static ssize_t store_bpp(struct device *device, struct device_attribute *attr, |
| 211 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 213 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | struct fb_var_screeninfo var; |
| 215 | char ** last = NULL; |
| 216 | int err; |
| 217 | |
| 218 | var = fb_info->var; |
| 219 | var.bits_per_pixel = simple_strtoul(buf, last, 0); |
| 220 | if ((err = activate(fb_info, &var))) |
| 221 | return err; |
| 222 | return count; |
| 223 | } |
| 224 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 225 | static ssize_t show_bpp(struct device *device, struct device_attribute *attr, |
| 226 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 228 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel); |
| 230 | } |
| 231 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 232 | static ssize_t store_rotate(struct device *device, |
| 233 | struct device_attribute *attr, |
| 234 | const char *buf, size_t count) |
Antonino A. Daplas | a812c94 | 2005-11-08 21:39:15 -0800 | [diff] [blame] | 235 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 236 | struct fb_info *fb_info = dev_get_drvdata(device); |
Antonino A. Daplas | a812c94 | 2005-11-08 21:39:15 -0800 | [diff] [blame] | 237 | struct fb_var_screeninfo var; |
| 238 | char **last = NULL; |
| 239 | int err; |
| 240 | |
| 241 | var = fb_info->var; |
| 242 | var.rotate = simple_strtoul(buf, last, 0); |
| 243 | |
| 244 | if ((err = activate(fb_info, &var))) |
| 245 | return err; |
| 246 | |
| 247 | return count; |
| 248 | } |
| 249 | |
| 250 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 251 | static ssize_t show_rotate(struct device *device, |
| 252 | struct device_attribute *attr, char *buf) |
Antonino A. Daplas | a812c94 | 2005-11-08 21:39:15 -0800 | [diff] [blame] | 253 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 254 | struct fb_info *fb_info = dev_get_drvdata(device); |
Antonino A. Daplas | a812c94 | 2005-11-08 21:39:15 -0800 | [diff] [blame] | 255 | |
| 256 | return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate); |
| 257 | } |
| 258 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 259 | static ssize_t store_virtual(struct device *device, |
| 260 | struct device_attribute *attr, |
| 261 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 263 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | struct fb_var_screeninfo var; |
| 265 | char *last = NULL; |
| 266 | int err; |
| 267 | |
| 268 | var = fb_info->var; |
| 269 | var.xres_virtual = simple_strtoul(buf, &last, 0); |
| 270 | last++; |
| 271 | if (last - buf >= count) |
| 272 | return -EINVAL; |
| 273 | var.yres_virtual = simple_strtoul(last, &last, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if ((err = activate(fb_info, &var))) |
| 276 | return err; |
| 277 | return count; |
| 278 | } |
| 279 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 280 | static ssize_t show_virtual(struct device *device, |
| 281 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 283 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual, |
Jon Smirl | e2c1649 | 2005-06-13 15:52:36 -0700 | [diff] [blame] | 285 | fb_info->var.yres_virtual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 288 | static ssize_t show_stride(struct device *device, |
| 289 | struct device_attribute *attr, char *buf) |
James Simmons | c14e2cf | 2005-10-24 21:46:21 +0100 | [diff] [blame] | 290 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 291 | struct fb_info *fb_info = dev_get_drvdata(device); |
James Simmons | c14e2cf | 2005-10-24 21:46:21 +0100 | [diff] [blame] | 292 | return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length); |
| 293 | } |
| 294 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 295 | static ssize_t store_blank(struct device *device, |
| 296 | struct device_attribute *attr, |
| 297 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 299 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | char *last = NULL; |
| 301 | int err; |
| 302 | |
| 303 | acquire_console_sem(); |
| 304 | fb_info->flags |= FBINFO_MISC_USEREVENT; |
| 305 | err = fb_blank(fb_info, simple_strtoul(buf, &last, 0)); |
| 306 | fb_info->flags &= ~FBINFO_MISC_USEREVENT; |
| 307 | release_console_sem(); |
| 308 | if (err < 0) |
| 309 | return err; |
| 310 | return count; |
| 311 | } |
| 312 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 313 | static ssize_t show_blank(struct device *device, |
| 314 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 316 | // struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 320 | static ssize_t store_console(struct device *device, |
| 321 | struct device_attribute *attr, |
| 322 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 324 | // struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 328 | static ssize_t show_console(struct device *device, |
| 329 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 331 | // struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 335 | static ssize_t store_cursor(struct device *device, |
| 336 | struct device_attribute *attr, |
| 337 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 339 | // struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 343 | static ssize_t show_cursor(struct device *device, |
| 344 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 346 | // struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 350 | static ssize_t store_pan(struct device *device, |
| 351 | struct device_attribute *attr, |
| 352 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 354 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | struct fb_var_screeninfo var; |
| 356 | char *last = NULL; |
| 357 | int err; |
| 358 | |
| 359 | var = fb_info->var; |
| 360 | var.xoffset = simple_strtoul(buf, &last, 0); |
| 361 | last++; |
| 362 | if (last - buf >= count) |
| 363 | return -EINVAL; |
| 364 | var.yoffset = simple_strtoul(last, &last, 0); |
| 365 | |
| 366 | acquire_console_sem(); |
| 367 | err = fb_pan_display(fb_info, &var); |
| 368 | release_console_sem(); |
| 369 | |
| 370 | if (err < 0) |
| 371 | return err; |
| 372 | return count; |
| 373 | } |
| 374 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 375 | static ssize_t show_pan(struct device *device, |
| 376 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 378 | struct fb_info *fb_info = dev_get_drvdata(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset, |
Antonino A. Daplas | c427063 | 2007-05-08 00:37:30 -0700 | [diff] [blame] | 380 | fb_info->var.yoffset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 383 | static ssize_t show_name(struct device *device, |
| 384 | struct device_attribute *attr, char *buf) |
James Simmons | 02459ea | 2005-07-30 23:49:54 +0100 | [diff] [blame] | 385 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 386 | struct fb_info *fb_info = dev_get_drvdata(device); |
James Simmons | 02459ea | 2005-07-30 23:49:54 +0100 | [diff] [blame] | 387 | |
| 388 | return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id); |
| 389 | } |
| 390 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 391 | static ssize_t store_fbstate(struct device *device, |
| 392 | struct device_attribute *attr, |
| 393 | const char *buf, size_t count) |
Matthew Garrett | 30420f8 | 2006-01-09 20:53:01 -0800 | [diff] [blame] | 394 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 395 | struct fb_info *fb_info = dev_get_drvdata(device); |
Matthew Garrett | 30420f8 | 2006-01-09 20:53:01 -0800 | [diff] [blame] | 396 | u32 state; |
| 397 | char *last = NULL; |
| 398 | |
| 399 | state = simple_strtoul(buf, &last, 0); |
| 400 | |
| 401 | acquire_console_sem(); |
| 402 | fb_set_suspend(fb_info, (int)state); |
| 403 | release_console_sem(); |
| 404 | |
| 405 | return count; |
| 406 | } |
| 407 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 408 | static ssize_t show_fbstate(struct device *device, |
| 409 | struct device_attribute *attr, char *buf) |
Matthew Garrett | 30420f8 | 2006-01-09 20:53:01 -0800 | [diff] [blame] | 410 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 411 | struct fb_info *fb_info = dev_get_drvdata(device); |
Matthew Garrett | 30420f8 | 2006-01-09 20:53:01 -0800 | [diff] [blame] | 412 | return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state); |
| 413 | } |
| 414 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 415 | #ifdef CONFIG_FB_BACKLIGHT |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 416 | static ssize_t store_bl_curve(struct device *device, |
| 417 | struct device_attribute *attr, |
| 418 | const char *buf, size_t count) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 419 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 420 | struct fb_info *fb_info = dev_get_drvdata(device); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 421 | u8 tmp_curve[FB_BACKLIGHT_LEVELS]; |
| 422 | unsigned int i; |
| 423 | |
Michael Hanselmann | 25981de | 2006-09-25 16:25:07 -0700 | [diff] [blame] | 424 | /* Some drivers don't use framebuffer_alloc(), but those also |
| 425 | * don't have backlights. |
| 426 | */ |
| 427 | if (!fb_info || !fb_info->bl_dev) |
| 428 | return -ENODEV; |
| 429 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 430 | if (count != (FB_BACKLIGHT_LEVELS / 8 * 24)) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i) |
| 434 | if (sscanf(&buf[i * 24], |
| 435 | "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n", |
| 436 | &tmp_curve[i * 8 + 0], |
| 437 | &tmp_curve[i * 8 + 1], |
| 438 | &tmp_curve[i * 8 + 2], |
| 439 | &tmp_curve[i * 8 + 3], |
| 440 | &tmp_curve[i * 8 + 4], |
| 441 | &tmp_curve[i * 8 + 5], |
| 442 | &tmp_curve[i * 8 + 6], |
| 443 | &tmp_curve[i * 8 + 7]) != 8) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | /* If there has been an error in the input data, we won't |
| 447 | * reach this loop. |
| 448 | */ |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 449 | mutex_lock(&fb_info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 450 | for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i) |
| 451 | fb_info->bl_curve[i] = tmp_curve[i]; |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 452 | mutex_unlock(&fb_info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 453 | |
| 454 | return count; |
| 455 | } |
| 456 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 457 | static ssize_t show_bl_curve(struct device *device, |
| 458 | struct device_attribute *attr, char *buf) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 459 | { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 460 | struct fb_info *fb_info = dev_get_drvdata(device); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 461 | ssize_t len = 0; |
| 462 | unsigned int i; |
| 463 | |
Michael Hanselmann | 25981de | 2006-09-25 16:25:07 -0700 | [diff] [blame] | 464 | /* Some drivers don't use framebuffer_alloc(), but those also |
| 465 | * don't have backlights. |
| 466 | */ |
| 467 | if (!fb_info || !fb_info->bl_dev) |
| 468 | return -ENODEV; |
| 469 | |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 470 | mutex_lock(&fb_info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 471 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8) |
| 472 | len += snprintf(&buf[len], PAGE_SIZE, |
| 473 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 474 | fb_info->bl_curve[i + 0], |
| 475 | fb_info->bl_curve[i + 1], |
| 476 | fb_info->bl_curve[i + 2], |
| 477 | fb_info->bl_curve[i + 3], |
| 478 | fb_info->bl_curve[i + 4], |
| 479 | fb_info->bl_curve[i + 5], |
| 480 | fb_info->bl_curve[i + 6], |
| 481 | fb_info->bl_curve[i + 7]); |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 482 | mutex_unlock(&fb_info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 483 | |
| 484 | return len; |
| 485 | } |
| 486 | #endif |
| 487 | |
Jon Smirl | 913e7ec5 | 2006-04-12 19:43:35 -0400 | [diff] [blame] | 488 | /* When cmap is added back in it should be a binary attribute |
| 489 | * not a text one. Consideration should also be given to converting |
| 490 | * fbdev to use configfs instead of sysfs */ |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 491 | static struct device_attribute device_attrs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp), |
| 493 | __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console), |
| 495 | __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor), |
| 496 | __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode), |
| 497 | __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes), |
| 498 | __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan), |
| 499 | __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual), |
James Simmons | 02459ea | 2005-07-30 23:49:54 +0100 | [diff] [blame] | 500 | __ATTR(name, S_IRUGO, show_name, NULL), |
James Simmons | c14e2cf | 2005-10-24 21:46:21 +0100 | [diff] [blame] | 501 | __ATTR(stride, S_IRUGO, show_stride, NULL), |
Antonino A. Daplas | a812c94 | 2005-11-08 21:39:15 -0800 | [diff] [blame] | 502 | __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate), |
Matthew Garrett | 30420f8 | 2006-01-09 20:53:01 -0800 | [diff] [blame] | 503 | __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate), |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 504 | #ifdef CONFIG_FB_BACKLIGHT |
| 505 | __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve), |
| 506 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | }; |
| 508 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 509 | int fb_init_device(struct fb_info *fb_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 511 | int i, error = 0; |
| 512 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 513 | dev_set_drvdata(fb_info->dev, fb_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 515 | fb_info->class_flag |= FB_SYSFS_FLAG_ATTR; |
| 516 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 517 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) { |
| 518 | error = device_create_file(fb_info->dev, &device_attrs[i]); |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 519 | |
| 520 | if (error) |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | if (error) { |
| 525 | while (--i >= 0) |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 526 | device_remove_file(fb_info->dev, &device_attrs[i]); |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 527 | fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR; |
| 528 | } |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 533 | void fb_cleanup_device(struct fb_info *fb_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
| 535 | unsigned int i; |
| 536 | |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 537 | if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) { |
Greg Kroah-Hartman | 78cde0887 | 2006-09-14 07:30:59 -0700 | [diff] [blame] | 538 | for (i = 0; i < ARRAY_SIZE(device_attrs); i++) |
| 539 | device_remove_file(fb_info->dev, &device_attrs[i]); |
Antonino A. Daplas | 1a6600b | 2006-10-03 01:14:50 -0700 | [diff] [blame] | 540 | |
| 541 | fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR; |
| 542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 545 | #ifdef CONFIG_FB_BACKLIGHT |
| 546 | /* This function generates a linear backlight curve |
| 547 | * |
| 548 | * 0: off |
| 549 | * 1-7: min |
| 550 | * 8-127: linear from min to max |
| 551 | */ |
| 552 | void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max) |
| 553 | { |
| 554 | unsigned int i, flat, count, range = (max - min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 556 | mutex_lock(&fb_info->bl_curve_mutex); |
| 557 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 558 | fb_info->bl_curve[0] = off; |
| 559 | |
| 560 | for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat) |
| 561 | fb_info->bl_curve[flat] = min; |
| 562 | |
| 563 | count = FB_BACKLIGHT_LEVELS * 15 / 16; |
| 564 | for (i = 0; i < count; ++i) |
| 565 | fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count); |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 566 | |
| 567 | mutex_unlock(&fb_info->bl_curve_mutex); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 568 | } |
| 569 | EXPORT_SYMBOL_GPL(fb_bl_default_curve); |
| 570 | #endif |