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