fbsysfs.c 14.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * fbsysfs.c - framebuffer device class and attributes
 *
 * Copyright (c) 2004 James Simmons <jsimmons@infradead.org>
 * 
 *	This program is free software you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

/*
 * Note:  currently there's only stubs for framebuffer_alloc and
 * framebuffer_release here.  The reson for that is that until all drivers
 * are converted to use it a sysfsification will open OOPSable races.
 */

#include <linux/kernel.h>
#include <linux/fb.h>
#include <linux/console.h>

/**
 * framebuffer_alloc - creates a new frame buffer info structure
 *
 * @size: size of driver private data, can be zero
 * @dev: pointer to the device for this fb, this can be NULL
 *
 * Creates a new frame buffer info structure. Also reserves @size bytes
 * for driver private data (info->par). info->par (if any) will be
 * aligned to sizeof(long).
 *
 * Returns the new structure, or NULL if an error occured.
 *
 */
struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
{
#define BYTES_PER_LONG (BITS_PER_LONG/8)
#define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
	int fb_info_size = sizeof(struct fb_info);
	struct fb_info *info;
	char *p;

	if (size)
		fb_info_size += PADDING;

46 47
	p = kzalloc(fb_info_size + size, GFP_KERNEL);

L
Linus Torvalds 已提交
48 49
	if (!p)
		return NULL;
50

L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	info = (struct fb_info *) p;

	if (size)
		info->par = p + fb_info_size;

	info->device = dev;

	return info;
#undef PADDING
#undef BYTES_PER_LONG
}
EXPORT_SYMBOL(framebuffer_alloc);

/**
 * framebuffer_release - marks the structure available for freeing
 *
 * @info: frame buffer info structure
 *
 * Drop the reference count of the class_device embedded in the
 * framebuffer info structure.
 *
 */
void framebuffer_release(struct fb_info *info)
{
	kfree(info);
}
EXPORT_SYMBOL(framebuffer_release);

static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
{
	int err;

	var->activate |= FB_ACTIVATE_FORCE;
	acquire_console_sem();
	fb_info->flags |= FBINFO_MISC_USEREVENT;
	err = fb_set_var(fb_info, var);
	fb_info->flags &= ~FBINFO_MISC_USEREVENT;
	release_console_sem();
	if (err)
		return err;
	return 0;
}

static int mode_string(char *buf, unsigned int offset,
		       const struct fb_videomode *mode)
{
	char m = 'U';
	if (mode->flag & FB_MODE_IS_DETAILED)
		m = 'D';
	if (mode->flag & FB_MODE_IS_VESA)
		m = 'V';
	if (mode->flag & FB_MODE_IS_STANDARD)
		m = 'S';
	return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d-%d\n", m, mode->xres, mode->yres, mode->refresh);
}

static ssize_t store_mode(struct class_device *class_device, const char * buf,
			  size_t count)
{
110
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	char mstr[100];
	struct fb_var_screeninfo var;
	struct fb_modelist *modelist;
	struct fb_videomode *mode;
	struct list_head *pos;
	size_t i;
	int err;

	memset(&var, 0, sizeof(var));

	list_for_each(pos, &fb_info->modelist) {
		modelist = list_entry(pos, struct fb_modelist, list);
		mode = &modelist->mode;
		i = mode_string(mstr, 0, mode);
		if (strncmp(mstr, buf, max(count, i)) == 0) {

			var = fb_info->var;
			fb_videomode_to_var(&var, mode);
			if ((err = activate(fb_info, &var)))
				return err;
			fb_info->mode = mode;
			return count;
		}
	}
	return -EINVAL;
}

static ssize_t show_mode(struct class_device *class_device, char *buf)
{
140
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150

	if (!fb_info->mode)
		return 0;

	return mode_string(buf, 0, fb_info->mode);
}

static ssize_t store_modes(struct class_device *class_device, const char * buf,
			   size_t count)
{
151
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	LIST_HEAD(old_list);
	int i = count / sizeof(struct fb_videomode);

	if (i * sizeof(struct fb_videomode) != count)
		return -EINVAL;

	acquire_console_sem();
	list_splice(&fb_info->modelist, &old_list);
	fb_videomode_to_modelist((struct fb_videomode *)buf, i,
				 &fb_info->modelist);
	if (fb_new_modelist(fb_info)) {
		fb_destroy_modelist(&fb_info->modelist);
		list_splice(&old_list, &fb_info->modelist);
	} else
		fb_destroy_modelist(&old_list);

	release_console_sem();

	return 0;
}

static ssize_t show_modes(struct class_device *class_device, char *buf)
{
175
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	unsigned int i;
	struct list_head *pos;
	struct fb_modelist *modelist;
	const struct fb_videomode *mode;

	i = 0;
	list_for_each(pos, &fb_info->modelist) {
		modelist = list_entry(pos, struct fb_modelist, list);
		mode = &modelist->mode;
		i += mode_string(buf, i, mode);
	}
	return i;
}

static ssize_t store_bpp(struct class_device *class_device, const char * buf,
			 size_t count)
{
193
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
	struct fb_var_screeninfo var;
	char ** last = NULL;
	int err;

	var = fb_info->var;
	var.bits_per_pixel = simple_strtoul(buf, last, 0);
	if ((err = activate(fb_info, &var)))
		return err;
	return count;
}

static ssize_t show_bpp(struct class_device *class_device, char *buf)
{
207
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
208 209 210
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
static ssize_t store_rotate(struct class_device *class_device, const char *buf,
			    size_t count)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	struct fb_var_screeninfo var;
	char **last = NULL;
	int err;

	var = fb_info->var;
	var.rotate = simple_strtoul(buf, last, 0);

	if ((err = activate(fb_info, &var)))
		return err;

	return count;
}


static ssize_t show_rotate(struct class_device *class_device, char *buf)
{
	struct fb_info *fb_info = class_get_devdata(class_device);

	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
}

static ssize_t store_con_rotate(struct class_device *class_device,
				const char *buf, size_t count)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	int rotate;
	char **last = NULL;

	acquire_console_sem();
	rotate = simple_strtoul(buf, last, 0);
	fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE, &rotate);
	release_console_sem();
	return count;
}

static ssize_t store_con_rotate_all(struct class_device *class_device,
				const char *buf, size_t count)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	int rotate;
	char **last = NULL;

	acquire_console_sem();
	rotate = simple_strtoul(buf, last, 0);
	fb_con_duit(fb_info, FB_EVENT_SET_CON_ROTATE_ALL, &rotate);
	release_console_sem();
	return count;
}

static ssize_t show_con_rotate(struct class_device *class_device, char *buf)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	int rotate;

	acquire_console_sem();
	rotate = fb_con_duit(fb_info, FB_EVENT_GET_CON_ROTATE, NULL);
	release_console_sem();
	return snprintf(buf, PAGE_SIZE, "%d\n", rotate);
}

L
Linus Torvalds 已提交
275 276 277
static ssize_t store_virtual(struct class_device *class_device,
			     const char * buf, size_t count)
{
278
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	struct fb_var_screeninfo var;
	char *last = NULL;
	int err;

	var = fb_info->var;
	var.xres_virtual = simple_strtoul(buf, &last, 0);
	last++;
	if (last - buf >= count)
		return -EINVAL;
	var.yres_virtual = simple_strtoul(last, &last, 0);

	if ((err = activate(fb_info, &var)))
		return err;
	return count;
}

static ssize_t show_virtual(struct class_device *class_device, char *buf)
{
297
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
298
	return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
299
			fb_info->var.yres_virtual);
L
Linus Torvalds 已提交
300 301
}

302 303
static ssize_t show_stride(struct class_device *class_device, char *buf)
{
304
	struct fb_info *fb_info = class_get_devdata(class_device);
305 306 307
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
}

J
Jon Smirl 已提交
308 309
/* Format for cmap is "%02x%c%4x%4x%4x\n" */
/* %02x entry %c transp %4x red %4x blue %4x green \n */
J
Jon Smirl 已提交
310 311
/* 256 rows at 16 chars equals 4096, the normal page size */
/* the code will automatically adjust for different page sizes */
J
Jon Smirl 已提交
312
static ssize_t store_cmap(struct class_device *class_device, const char *buf,
L
Linus Torvalds 已提交
313 314
			  size_t count)
{
315
	struct fb_info *fb_info = class_get_devdata(class_device);
J
Jon Smirl 已提交
316 317
	int rc, i, start, length, transp = 0;

J
Jon Smirl 已提交
318
	if ((count > PAGE_SIZE) || ((count % 16) != 0))
J
Jon Smirl 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		return -EINVAL;

	if (!fb_info->fbops->fb_setcolreg && !fb_info->fbops->fb_setcmap)
		return -EINVAL;

	sscanf(buf, "%02x", &start);
	length = count / 16;

	for (i = 0; i < length; i++)
		if (buf[i * 16 + 2] != ' ')
			transp = 1;

	/* If we can batch, do it */
	if (fb_info->fbops->fb_setcmap && length > 1) {
		struct fb_cmap umap;

		memset(&umap, 0, sizeof(umap));
		if ((rc = fb_alloc_cmap(&umap, length, transp)))
			return rc;

		umap.start = start;
		for (i = 0; i < length; i++) {
			sscanf(&buf[i * 16 +  3], "%4hx", &umap.red[i]);
			sscanf(&buf[i * 16 +  7], "%4hx", &umap.blue[i]);
			sscanf(&buf[i * 16 + 11], "%4hx", &umap.green[i]);
			if (transp)
				umap.transp[i] = (buf[i * 16 +  2] != ' ');
		}
		rc = fb_info->fbops->fb_setcmap(&umap, fb_info);
		fb_copy_cmap(&umap, &fb_info->cmap);
		fb_dealloc_cmap(&umap);

351
		return rc ?: count;
J
Jon Smirl 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	}
	for (i = 0; i < length; i++) {
		u16 red, blue, green, tsp;

		sscanf(&buf[i * 16 +  3], "%4hx", &red);
		sscanf(&buf[i * 16 +  7], "%4hx", &blue);
		sscanf(&buf[i * 16 + 11], "%4hx", &green);
		tsp = (buf[i * 16 +  2] != ' ');
		if ((rc = fb_info->fbops->fb_setcolreg(start++,
				      red, green, blue, tsp, fb_info)))
			return rc;

		fb_info->cmap.red[i] = red;
		fb_info->cmap.blue[i] = blue;
		fb_info->cmap.green[i] = green;
		if (transp)
			fb_info->cmap.transp[i] = tsp;
	}
370
	return count;
L
Linus Torvalds 已提交
371 372 373 374
}

static ssize_t show_cmap(struct class_device *class_device, char *buf)
{
375
	struct fb_info *fb_info = class_get_devdata(class_device);
J
Jon Smirl 已提交
376
	unsigned int i;
L
Linus Torvalds 已提交
377 378

	if (!fb_info->cmap.red || !fb_info->cmap.blue ||
J
Jon Smirl 已提交
379 380 381
	   !fb_info->cmap.green)
		return -EINVAL;

J
Jon Smirl 已提交
382
	if (fb_info->cmap.len > PAGE_SIZE / 16)
L
Linus Torvalds 已提交
383 384
		return -EINVAL;

J
Jon Smirl 已提交
385
	/* don't mess with the format, the buffer is PAGE_SIZE */
J
Jon Smirl 已提交
386
	/* 256 entries at 16 chars per line equals 4096 = PAGE_SIZE */
L
Linus Torvalds 已提交
387
	for (i = 0; i < fb_info->cmap.len; i++) {
J
Jon Smirl 已提交
388
		snprintf(&buf[ i * 16], PAGE_SIZE - i * 16, "%02x%c%4x%4x%4x\n", i + fb_info->cmap.start,
J
Jon Smirl 已提交
389 390 391
			((fb_info->cmap.transp && fb_info->cmap.transp[i]) ? '*' : ' '),
			fb_info->cmap.red[i], fb_info->cmap.blue[i],
			fb_info->cmap.green[i]);
L
Linus Torvalds 已提交
392
	}
J
Jon Smirl 已提交
393
	return 16 * fb_info->cmap.len;
L
Linus Torvalds 已提交
394 395 396 397 398
}

static ssize_t store_blank(struct class_device *class_device, const char * buf,
			   size_t count)
{
399
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	char *last = NULL;
	int err;

	acquire_console_sem();
	fb_info->flags |= FBINFO_MISC_USEREVENT;
	err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
	fb_info->flags &= ~FBINFO_MISC_USEREVENT;
	release_console_sem();
	if (err < 0)
		return err;
	return count;
}

static ssize_t show_blank(struct class_device *class_device, char *buf)
{
415
//	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
416 417 418 419 420 421
	return 0;
}

static ssize_t store_console(struct class_device *class_device,
			     const char * buf, size_t count)
{
422
//	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
423 424 425 426 427
	return 0;
}

static ssize_t show_console(struct class_device *class_device, char *buf)
{
428
//	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
429 430 431 432 433 434
	return 0;
}

static ssize_t store_cursor(struct class_device *class_device,
			    const char * buf, size_t count)
{
435
//	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
436 437 438 439 440
	return 0;
}

static ssize_t show_cursor(struct class_device *class_device, char *buf)
{
441
//	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
442 443 444 445 446 447
	return 0;
}

static ssize_t store_pan(struct class_device *class_device, const char * buf,
			 size_t count)
{
448
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
	struct fb_var_screeninfo var;
	char *last = NULL;
	int err;

	var = fb_info->var;
	var.xoffset = simple_strtoul(buf, &last, 0);
	last++;
	if (last - buf >= count)
		return -EINVAL;
	var.yoffset = simple_strtoul(last, &last, 0);

	acquire_console_sem();
	err = fb_pan_display(fb_info, &var);
	release_console_sem();

	if (err < 0)
		return err;
	return count;
}

static ssize_t show_pan(struct class_device *class_device, char *buf)
{
471
	struct fb_info *fb_info = class_get_devdata(class_device);
L
Linus Torvalds 已提交
472 473 474 475
	return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
			fb_info->var.xoffset);
}

476 477
static ssize_t show_name(struct class_device *class_device, char *buf)
{
478
	struct fb_info *fb_info = class_get_devdata(class_device);
479 480 481 482

	return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
static ssize_t store_fbstate(struct class_device *class_device,
			const char *buf, size_t count)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	u32 state;
	char *last = NULL;

	state = simple_strtoul(buf, &last, 0);

	acquire_console_sem();
	fb_set_suspend(fb_info, (int)state);
	release_console_sem();

	return count;
}

static ssize_t show_fbstate(struct class_device *class_device, char *buf)
{
	struct fb_info *fb_info = class_get_devdata(class_device);
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
}

A
Adrian Bunk 已提交
505
static struct class_device_attribute class_device_attrs[] = {
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514
	__ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
	__ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
	__ATTR(color_map, S_IRUGO|S_IWUSR, show_cmap, store_cmap),
	__ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
	__ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
	__ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
	__ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
	__ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
	__ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
515
	__ATTR(name, S_IRUGO, show_name, NULL),
516
	__ATTR(stride, S_IRUGO, show_stride, NULL),
517 518 519
	__ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
	__ATTR(con_rotate, S_IRUGO|S_IWUSR, show_con_rotate, store_con_rotate),
	__ATTR(con_rotate_all, S_IWUSR, NULL, store_con_rotate_all),
520
	__ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
};

int fb_init_class_device(struct fb_info *fb_info)
{
	unsigned int i;
	class_set_devdata(fb_info->class_device, fb_info);

	for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
		class_device_create_file(fb_info->class_device,
					 &class_device_attrs[i]);
	return 0;
}

void fb_cleanup_class_device(struct fb_info *fb_info)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(class_device_attrs); i++)
		class_device_remove_file(fb_info->class_device,
					 &class_device_attrs[i]);
}