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> |
| 21 | |
| 22 | /** |
| 23 | * framebuffer_alloc - creates a new frame buffer info structure |
| 24 | * |
| 25 | * @size: size of driver private data, can be zero |
| 26 | * @dev: pointer to the device for this fb, this can be NULL |
| 27 | * |
| 28 | * Creates a new frame buffer info structure. Also reserves @size bytes |
| 29 | * for driver private data (info->par). info->par (if any) will be |
| 30 | * aligned to sizeof(long). |
| 31 | * |
| 32 | * Returns the new structure, or NULL if an error occured. |
| 33 | * |
| 34 | */ |
| 35 | struct fb_info *framebuffer_alloc(size_t size, struct device *dev) |
| 36 | { |
| 37 | #define BYTES_PER_LONG (BITS_PER_LONG/8) |
| 38 | #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG)) |
| 39 | int fb_info_size = sizeof(struct fb_info); |
| 40 | struct fb_info *info; |
| 41 | char *p; |
| 42 | |
| 43 | if (size) |
| 44 | fb_info_size += PADDING; |
| 45 | |
| 46 | p = kmalloc(fb_info_size + size, GFP_KERNEL); |
| 47 | if (!p) |
| 48 | return NULL; |
| 49 | memset(p, 0, fb_info_size + size); |
| 50 | info = (struct fb_info *) p; |
| 51 | |
| 52 | if (size) |
| 53 | info->par = p + fb_info_size; |
| 54 | |
| 55 | info->device = dev; |
| 56 | |
| 57 | return info; |
| 58 | #undef PADDING |
| 59 | #undef BYTES_PER_LONG |
| 60 | } |
| 61 | EXPORT_SYMBOL(framebuffer_alloc); |
| 62 | |
| 63 | /** |
| 64 | * framebuffer_release - marks the structure available for freeing |
| 65 | * |
| 66 | * @info: frame buffer info structure |
| 67 | * |
| 68 | * Drop the reference count of the class_device embedded in the |
| 69 | * framebuffer info structure. |
| 70 | * |
| 71 | */ |
| 72 | void framebuffer_release(struct fb_info *info) |
| 73 | { |
| 74 | kfree(info); |
| 75 | } |
| 76 | EXPORT_SYMBOL(framebuffer_release); |
| 77 | |
| 78 | static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var) |
| 79 | { |
| 80 | int err; |
| 81 | |
| 82 | var->activate |= FB_ACTIVATE_FORCE; |
| 83 | acquire_console_sem(); |
| 84 | fb_info->flags |= FBINFO_MISC_USEREVENT; |
| 85 | err = fb_set_var(fb_info, var); |
| 86 | fb_info->flags &= ~FBINFO_MISC_USEREVENT; |
| 87 | release_console_sem(); |
| 88 | if (err) |
| 89 | return err; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int mode_string(char *buf, unsigned int offset, |
| 94 | const struct fb_videomode *mode) |
| 95 | { |
| 96 | char m = 'U'; |
| 97 | if (mode->flag & FB_MODE_IS_DETAILED) |
| 98 | m = 'D'; |
| 99 | if (mode->flag & FB_MODE_IS_VESA) |
| 100 | m = 'V'; |
| 101 | if (mode->flag & FB_MODE_IS_STANDARD) |
| 102 | m = 'S'; |
| 103 | return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh); |
| 104 | } |
| 105 | |
| 106 | static ssize_t store_mode(struct class_device *class_device, const char * buf, |
| 107 | size_t count) |
| 108 | { |
| 109 | struct fb_info *fb_info = |
| 110 | (struct fb_info *)class_get_devdata(class_device); |
| 111 | char mstr[100]; |
| 112 | struct fb_var_screeninfo var; |
| 113 | struct fb_modelist *modelist; |
| 114 | struct fb_videomode *mode; |
| 115 | struct list_head *pos; |
| 116 | size_t i; |
| 117 | int err; |
| 118 | |
| 119 | memset(&var, 0, sizeof(var)); |
| 120 | |
| 121 | list_for_each(pos, &fb_info->modelist) { |
| 122 | modelist = list_entry(pos, struct fb_modelist, list); |
| 123 | mode = &modelist->mode; |
| 124 | i = mode_string(mstr, 0, mode); |
| 125 | if (strncmp(mstr, buf, max(count, i)) == 0) { |
| 126 | |
| 127 | var = fb_info->var; |
| 128 | fb_videomode_to_var(&var, mode); |
| 129 | if ((err = activate(fb_info, &var))) |
| 130 | return err; |
| 131 | fb_info->mode = mode; |
| 132 | return count; |
| 133 | } |
| 134 | } |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | static ssize_t show_mode(struct class_device *class_device, char *buf) |
| 139 | { |
| 140 | struct fb_info *fb_info = |
| 141 | (struct fb_info *)class_get_devdata(class_device); |
| 142 | |
| 143 | if (!fb_info->mode) |
| 144 | return 0; |
| 145 | |
| 146 | return mode_string(buf, 0, fb_info->mode); |
| 147 | } |
| 148 | |
| 149 | static ssize_t store_modes(struct class_device *class_device, const char * buf, |
| 150 | size_t count) |
| 151 | { |
| 152 | struct fb_info *fb_info = |
| 153 | (struct fb_info *)class_get_devdata(class_device); |
| 154 | LIST_HEAD(old_list); |
| 155 | int i = count / sizeof(struct fb_videomode); |
| 156 | |
| 157 | if (i * sizeof(struct fb_videomode) != count) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | acquire_console_sem(); |
| 161 | list_splice(&fb_info->modelist, &old_list); |
| 162 | fb_videomode_to_modelist((struct fb_videomode *)buf, i, |
| 163 | &fb_info->modelist); |
| 164 | if (fb_new_modelist(fb_info)) { |
| 165 | fb_destroy_modelist(&fb_info->modelist); |
| 166 | list_splice(&old_list, &fb_info->modelist); |
| 167 | } else |
| 168 | fb_destroy_modelist(&old_list); |
| 169 | |
| 170 | release_console_sem(); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static ssize_t show_modes(struct class_device *class_device, char *buf) |
| 176 | { |
| 177 | struct fb_info *fb_info = |
| 178 | (struct fb_info *)class_get_devdata(class_device); |
| 179 | unsigned int i; |
| 180 | struct list_head *pos; |
| 181 | struct fb_modelist *modelist; |
| 182 | const struct fb_videomode *mode; |
| 183 | |
| 184 | i = 0; |
| 185 | list_for_each(pos, &fb_info->modelist) { |
| 186 | modelist = list_entry(pos, struct fb_modelist, list); |
| 187 | mode = &modelist->mode; |
| 188 | i += mode_string(buf, i, mode); |
| 189 | } |
| 190 | return i; |
| 191 | } |
| 192 | |
| 193 | static ssize_t store_bpp(struct class_device *class_device, const char * buf, |
| 194 | size_t count) |
| 195 | { |
| 196 | struct fb_info *fb_info = |
| 197 | (struct fb_info *)class_get_devdata(class_device); |
| 198 | struct fb_var_screeninfo var; |
| 199 | char ** last = NULL; |
| 200 | int err; |
| 201 | |
| 202 | var = fb_info->var; |
| 203 | var.bits_per_pixel = simple_strtoul(buf, last, 0); |
| 204 | if ((err = activate(fb_info, &var))) |
| 205 | return err; |
| 206 | return count; |
| 207 | } |
| 208 | |
| 209 | static ssize_t show_bpp(struct class_device *class_device, char *buf) |
| 210 | { |
| 211 | struct fb_info *fb_info = |
| 212 | (struct fb_info *)class_get_devdata(class_device); |
| 213 | return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel); |
| 214 | } |
| 215 | |
| 216 | static ssize_t store_virtual(struct class_device *class_device, |
| 217 | const char * buf, size_t count) |
| 218 | { |
| 219 | struct fb_info *fb_info = |
| 220 | (struct fb_info *)class_get_devdata(class_device); |
| 221 | struct fb_var_screeninfo var; |
| 222 | char *last = NULL; |
| 223 | int err; |
| 224 | |
| 225 | var = fb_info->var; |
| 226 | var.xres_virtual = simple_strtoul(buf, &last, 0); |
| 227 | last++; |
| 228 | if (last - buf >= count) |
| 229 | return -EINVAL; |
| 230 | var.yres_virtual = simple_strtoul(last, &last, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | if ((err = activate(fb_info, &var))) |
| 233 | return err; |
| 234 | return count; |
| 235 | } |
| 236 | |
| 237 | static ssize_t show_virtual(struct class_device *class_device, char *buf) |
| 238 | { |
| 239 | struct fb_info *fb_info = |
| 240 | (struct fb_info *)class_get_devdata(class_device); |
| 241 | 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] | 242 | fb_info->var.yres_virtual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 245 | /* Format for cmap is "%02x%c%4x%4x%4x\n" */ |
| 246 | /* %02x entry %c transp %4x red %4x blue %4x green \n */ |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 247 | /* 256 rows at 16 chars equals 4096, the normal page size */ |
| 248 | /* the code will automatically adjust for different page sizes */ |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 249 | static ssize_t store_cmap(struct class_device *class_device, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | size_t count) |
| 251 | { |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 252 | struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 253 | int rc, i, start, length, transp = 0; |
| 254 | |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 255 | if ((count > PAGE_SIZE) || ((count % 16) != 0)) |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | |
| 258 | if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | sscanf(buf, "%02x", &start); |
| 262 | length = count / 16; |
| 263 | |
| 264 | for (i = 0; i < length; i++) |
| 265 | if (buf[i * 16 + 2] != ' ') |
| 266 | transp = 1; |
| 267 | |
| 268 | /* If we can batch, do it */ |
| 269 | if (fb_info->fbops->fb_setcmap && length > 1) { |
| 270 | struct fb_cmap umap; |
| 271 | |
| 272 | memset(&umap, 0, sizeof(umap)); |
| 273 | if ((rc = fb_alloc_cmap(&umap, length, transp))) |
| 274 | return rc; |
| 275 | |
| 276 | umap.start = start; |
| 277 | for (i = 0; i < length; i++) { |
| 278 | sscanf(&buf[i * 16 + 3], "%4hx", &umap.red[i]); |
| 279 | sscanf(&buf[i * 16 + 7], "%4hx", &umap.blue[i]); |
| 280 | sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]); |
| 281 | if (transp) |
| 282 | umap.transp[i] = (buf[i * 16 + 2] != ' '); |
| 283 | } |
| 284 | rc = fb_info->fbops->fb_setcmap(&umap, fb_info); |
| 285 | fb_copy_cmap(&umap, &fb_info->cmap); |
| 286 | fb_dealloc_cmap(&umap); |
| 287 | |
| 288 | return rc; |
| 289 | } |
| 290 | for (i = 0; i < length; i++) { |
| 291 | u16 red, blue, green, tsp; |
| 292 | |
| 293 | sscanf(&buf[i * 16 + 3], "%4hx", &red); |
| 294 | sscanf(&buf[i * 16 + 7], "%4hx", &blue); |
| 295 | sscanf(&buf[i * 16 + 11], "%4hx", &green); |
| 296 | tsp = (buf[i * 16 + 2] != ' '); |
| 297 | if ((rc = fb_info->fbops->fb_setcolreg(start++, |
| 298 | red, green, blue, tsp, fb_info))) |
| 299 | return rc; |
| 300 | |
| 301 | fb_info->cmap.red[i] = red; |
| 302 | fb_info->cmap.blue[i] = blue; |
| 303 | fb_info->cmap.green[i] = green; |
| 304 | if (transp) |
| 305 | fb_info->cmap.transp[i] = tsp; |
| 306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static ssize_t show_cmap(struct class_device *class_device, char *buf) |
| 311 | { |
| 312 | struct fb_info *fb_info = |
| 313 | (struct fb_info *)class_get_devdata(class_device); |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 314 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | if (!fb_info->cmap.red || !fb_info->cmap.blue || |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 317 | !fb_info->cmap.green) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return -EINVAL; |
| 319 | |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 320 | if (fb_info->cmap.len > PAGE_SIZE / 16) |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | |
| 323 | /* don't mess with the format, the buffer is PAGE_SIZE */ |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 324 | /* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | for (i = 0; i < fb_info->cmap.len; i++) { |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 326 | snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start, |
Jon Smirl | d210224 | 2005-07-27 11:46:05 -0700 | [diff] [blame] | 327 | ((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '), |
| 328 | fb_info->cmap.red[i], fb_info->cmap.blue[i], |
| 329 | fb_info->cmap.green[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Jon Smirl | f0b9d79 | 2005-07-28 21:16:19 -0700 | [diff] [blame] | 331 | return 16 * fb_info->cmap.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static ssize_t store_blank(struct class_device *class_device, const char * buf, |
| 335 | size_t count) |
| 336 | { |
| 337 | struct fb_info *fb_info = |
| 338 | (struct fb_info *)class_get_devdata(class_device); |
| 339 | char *last = NULL; |
| 340 | int err; |
| 341 | |
| 342 | acquire_console_sem(); |
| 343 | fb_info->flags |= FBINFO_MISC_USEREVENT; |
| 344 | err = fb_blank(fb_info, simple_strtoul(buf, &last, 0)); |
| 345 | fb_info->flags &= ~FBINFO_MISC_USEREVENT; |
| 346 | release_console_sem(); |
| 347 | if (err < 0) |
| 348 | return err; |
| 349 | return count; |
| 350 | } |
| 351 | |
| 352 | static ssize_t show_blank(struct class_device *class_device, char *buf) |
| 353 | { |
| 354 | // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static ssize_t store_console(struct class_device *class_device, |
| 359 | const char * buf, size_t count) |
| 360 | { |
| 361 | // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static ssize_t show_console(struct class_device *class_device, char *buf) |
| 366 | { |
| 367 | // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static ssize_t store_cursor(struct class_device *class_device, |
| 372 | const char * buf, size_t count) |
| 373 | { |
| 374 | // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static ssize_t show_cursor(struct class_device *class_device, char *buf) |
| 379 | { |
| 380 | // struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static ssize_t store_pan(struct class_device *class_device, const char * buf, |
| 385 | size_t count) |
| 386 | { |
| 387 | struct fb_info *fb_info = |
| 388 | (struct fb_info *)class_get_devdata(class_device); |
| 389 | struct fb_var_screeninfo var; |
| 390 | char *last = NULL; |
| 391 | int err; |
| 392 | |
| 393 | var = fb_info->var; |
| 394 | var.xoffset = simple_strtoul(buf, &last, 0); |
| 395 | last++; |
| 396 | if (last - buf >= count) |
| 397 | return -EINVAL; |
| 398 | var.yoffset = simple_strtoul(last, &last, 0); |
| 399 | |
| 400 | acquire_console_sem(); |
| 401 | err = fb_pan_display(fb_info, &var); |
| 402 | release_console_sem(); |
| 403 | |
| 404 | if (err < 0) |
| 405 | return err; |
| 406 | return count; |
| 407 | } |
| 408 | |
| 409 | static ssize_t show_pan(struct class_device *class_device, char *buf) |
| 410 | { |
| 411 | struct fb_info *fb_info = |
| 412 | (struct fb_info *)class_get_devdata(class_device); |
| 413 | return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset, |
| 414 | fb_info->var.xoffset); |
| 415 | } |
| 416 | |
James Simmons | 02459ea | 2005-07-30 23:49:54 +0100 | [diff] [blame] | 417 | static ssize_t show_name(struct class_device *class_device, char *buf) |
| 418 | { |
| 419 | struct fb_info *fb_info = (struct fb_info *)class_get_devdata(class_device); |
| 420 | |
| 421 | return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id); |
| 422 | } |
| 423 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 424 | static struct class_device_attribute class_device_attrs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp), |
| 426 | __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank), |
| 427 | __ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap), |
| 428 | __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console), |
| 429 | __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor), |
| 430 | __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode), |
| 431 | __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes), |
| 432 | __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan), |
| 433 | __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual), |
James Simmons | 02459ea | 2005-07-30 23:49:54 +0100 | [diff] [blame] | 434 | __ATTR(name, S_IRUGO, show_name, NULL), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | int fb_init_class_device(struct fb_info *fb_info) |
| 438 | { |
| 439 | unsigned int i; |
| 440 | class_set_devdata(fb_info->class_device, fb_info); |
| 441 | |
| 442 | for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++) |
| 443 | class_device_create_file(fb_info->class_device, |
| 444 | &class_device_attrs[i]); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | void fb_cleanup_class_device(struct fb_info *fb_info) |
| 449 | { |
| 450 | unsigned int i; |
| 451 | |
| 452 | for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++) |
| 453 | class_device_remove_file(fb_info->class_device, |
| 454 | &class_device_attrs[i]); |
| 455 | } |
| 456 | |
| 457 | |