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