fbsysfs.c 14.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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>
19
#include <linux/slab.h>
L
Linus Torvalds 已提交
20 21
#include <linux/fb.h>
#include <linux/console.h>
22
#include <linux/module.h>
L
Linus Torvalds 已提交
23

24 25
#define FB_SYSFS_FLAG_ATTR 1

L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35
/**
 * 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).
 *
L
Lucas De Marchi 已提交
36
 * Returns the new structure, or NULL if an error occurred.
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 */
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;

50 51
	p = kzalloc(fb_info_size + size, GFP_KERNEL);

L
Linus Torvalds 已提交
52 53
	if (!p)
		return NULL;
54

L
Linus Torvalds 已提交
55 56 57 58 59 60
	info = (struct fb_info *) p;

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

	info->device = dev;
61
	info->fbcon_rotate_hint = -1;
L
Linus Torvalds 已提交
62

R
Rob Clark 已提交
63
#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
64
	mutex_init(&info->bl_curve_mutex);
65 66
#endif

L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77
	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
 *
78
 * Drop the reference count of the device embedded in the
L
Linus Torvalds 已提交
79 80 81 82 83
 * framebuffer info structure.
 *
 */
void framebuffer_release(struct fb_info *info)
{
84 85
	if (!info)
		return;
86
	kfree(info->apertures);
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95
	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;
96
	console_lock();
L
Linus Torvalds 已提交
97 98 99
	fb_info->flags |= FBINFO_MISC_USEREVENT;
	err = fb_set_var(fb_info, var);
	fb_info->flags &= ~FBINFO_MISC_USEREVENT;
100
	console_unlock();
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109
	if (err)
		return err;
	return 0;
}

static int mode_string(char *buf, unsigned int offset,
		       const struct fb_videomode *mode)
{
	char m = 'U';
110 111
	char v = 'p';

L
Linus Torvalds 已提交
112 113 114 115 116 117
	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';
118 119 120 121 122 123 124 125

	if (mode->vmode & FB_VMODE_INTERLACED)
		v = 'i';
	if (mode->vmode & FB_VMODE_DOUBLE)
		v = 'd';

	return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
	                m, mode->xres, mode->yres, v, mode->refresh);
L
Linus Torvalds 已提交
126 127
}

128 129
static ssize_t store_mode(struct device *device, struct device_attribute *attr,
			  const char *buf, size_t count)
L
Linus Torvalds 已提交
130
{
131
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	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;
}

159 160
static ssize_t show_mode(struct device *device, struct device_attribute *attr,
			 char *buf)
L
Linus Torvalds 已提交
161
{
162
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169

	if (!fb_info->mode)
		return 0;

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

170 171 172
static ssize_t store_modes(struct device *device,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
173
{
174
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
175 176 177 178 179 180
	LIST_HEAD(old_list);
	int i = count / sizeof(struct fb_videomode);

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

181
	console_lock();
182 183 184 185 186
	if (!lock_fb_info(fb_info)) {
		console_unlock();
		return -ENODEV;
	}

L
Linus Torvalds 已提交
187
	list_splice(&fb_info->modelist, &old_list);
188
	fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
L
Linus Torvalds 已提交
189 190 191 192 193 194 195
				 &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);

196
	unlock_fb_info(fb_info);
197
	console_unlock();
L
Linus Torvalds 已提交
198 199 200 201

	return 0;
}

202 203
static ssize_t show_modes(struct device *device, struct device_attribute *attr,
			  char *buf)
L
Linus Torvalds 已提交
204
{
205
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219
	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;
}

220 221
static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
			 const char *buf, size_t count)
L
Linus Torvalds 已提交
222
{
223
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233 234
	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;
}

235 236
static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
			char *buf)
L
Linus Torvalds 已提交
237
{
238
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
239 240 241
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
}

242 243 244
static ssize_t store_rotate(struct device *device,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
245
{
246
	struct fb_info *fb_info = dev_get_drvdata(device);
247 248 249 250 251 252 253 254 255 256 257 258 259 260
	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;
}


261 262
static ssize_t show_rotate(struct device *device,
			   struct device_attribute *attr, char *buf)
263
{
264
	struct fb_info *fb_info = dev_get_drvdata(device);
265 266 267 268

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

269 270 271
static ssize_t store_virtual(struct device *device,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
L
Linus Torvalds 已提交
272
{
273
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	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;
}

290 291
static ssize_t show_virtual(struct device *device,
			    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
292
{
293
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
294
	return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
295
			fb_info->var.yres_virtual);
L
Linus Torvalds 已提交
296 297
}

298 299
static ssize_t show_stride(struct device *device,
			   struct device_attribute *attr, char *buf)
300
{
301
	struct fb_info *fb_info = dev_get_drvdata(device);
302 303 304
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
}

305 306 307
static ssize_t store_blank(struct device *device,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
L
Linus Torvalds 已提交
308
{
309
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
310 311 312
	char *last = NULL;
	int err;

313
	console_lock();
L
Linus Torvalds 已提交
314 315 316
	fb_info->flags |= FBINFO_MISC_USEREVENT;
	err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
	fb_info->flags &= ~FBINFO_MISC_USEREVENT;
317
	console_unlock();
L
Linus Torvalds 已提交
318 319 320 321 322
	if (err < 0)
		return err;
	return count;
}

323 324
static ssize_t show_blank(struct device *device,
			  struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
325
{
326
//	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
327 328 329
	return 0;
}

330 331 332
static ssize_t store_console(struct device *device,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
L
Linus Torvalds 已提交
333
{
334
//	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
335 336 337
	return 0;
}

338 339
static ssize_t show_console(struct device *device,
			    struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
340
{
341
//	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
342 343 344
	return 0;
}

345 346 347
static ssize_t store_cursor(struct device *device,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
348
{
349
//	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
350 351 352
	return 0;
}

353 354
static ssize_t show_cursor(struct device *device,
			   struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
355
{
356
//	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
357 358 359
	return 0;
}

360 361 362
static ssize_t store_pan(struct device *device,
			 struct device_attribute *attr,
			 const char *buf, size_t count)
L
Linus Torvalds 已提交
363
{
364
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372 373 374 375
	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);

376
	console_lock();
L
Linus Torvalds 已提交
377
	err = fb_pan_display(fb_info, &var);
378
	console_unlock();
L
Linus Torvalds 已提交
379 380 381 382 383 384

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

385 386
static ssize_t show_pan(struct device *device,
			struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
387
{
388
	struct fb_info *fb_info = dev_get_drvdata(device);
L
Linus Torvalds 已提交
389
	return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
390
			fb_info->var.yoffset);
L
Linus Torvalds 已提交
391 392
}

393 394
static ssize_t show_name(struct device *device,
			 struct device_attribute *attr, char *buf)
395
{
396
	struct fb_info *fb_info = dev_get_drvdata(device);
397 398 399 400

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

401 402 403
static ssize_t store_fbstate(struct device *device,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
404
{
405
	struct fb_info *fb_info = dev_get_drvdata(device);
406 407 408 409 410
	u32 state;
	char *last = NULL;

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

411
	console_lock();
412 413 414 415 416
	if (!lock_fb_info(fb_info)) {
		console_unlock();
		return -ENODEV;
	}

417
	fb_set_suspend(fb_info, (int)state);
418

419
	unlock_fb_info(fb_info);
420
	console_unlock();
421 422 423 424

	return count;
}

425 426
static ssize_t show_fbstate(struct device *device,
			    struct device_attribute *attr, char *buf)
427
{
428
	struct fb_info *fb_info = dev_get_drvdata(device);
429 430 431
	return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
}

R
Rob Clark 已提交
432
#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
433 434 435
static ssize_t store_bl_curve(struct device *device,
			      struct device_attribute *attr,
			      const char *buf, size_t count)
436
{
437
	struct fb_info *fb_info = dev_get_drvdata(device);
438 439 440
	u8 tmp_curve[FB_BACKLIGHT_LEVELS];
	unsigned int i;

441 442 443 444 445 446
	/* Some drivers don't use framebuffer_alloc(), but those also
	 * don't have backlights.
	 */
	if (!fb_info || !fb_info->bl_dev)
		return -ENODEV;

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
		return -EINVAL;

	for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
		if (sscanf(&buf[i * 24],
			"%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
			&tmp_curve[i * 8 + 0],
			&tmp_curve[i * 8 + 1],
			&tmp_curve[i * 8 + 2],
			&tmp_curve[i * 8 + 3],
			&tmp_curve[i * 8 + 4],
			&tmp_curve[i * 8 + 5],
			&tmp_curve[i * 8 + 6],
			&tmp_curve[i * 8 + 7]) != 8)
			return -EINVAL;

	/* If there has been an error in the input data, we won't
	 * reach this loop.
	 */
466
	mutex_lock(&fb_info->bl_curve_mutex);
467 468
	for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
		fb_info->bl_curve[i] = tmp_curve[i];
469
	mutex_unlock(&fb_info->bl_curve_mutex);
470 471 472 473

	return count;
}

474 475
static ssize_t show_bl_curve(struct device *device,
			     struct device_attribute *attr, char *buf)
476
{
477
	struct fb_info *fb_info = dev_get_drvdata(device);
478 479 480
	ssize_t len = 0;
	unsigned int i;

481 482 483 484 485 486
	/* Some drivers don't use framebuffer_alloc(), but those also
	 * don't have backlights.
	 */
	if (!fb_info || !fb_info->bl_dev)
		return -ENODEV;

487
	mutex_lock(&fb_info->bl_curve_mutex);
488
	for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
489
		len += scnprintf(&buf[len], PAGE_SIZE - len, "%8ph\n",
490
				fb_info->bl_curve + i);
491
	mutex_unlock(&fb_info->bl_curve_mutex);
492 493 494 495 496

	return len;
}
#endif

497 498 499
/* When cmap is added back in it should be a binary attribute
 * not a text one. Consideration should also be given to converting
 * fbdev to use configfs instead of sysfs */
500
static struct device_attribute device_attrs[] = {
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508
	__ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
	__ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
	__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),
509
	__ATTR(name, S_IRUGO, show_name, NULL),
510
	__ATTR(stride, S_IRUGO, show_stride, NULL),
511
	__ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
512
	__ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
R
Rob Clark 已提交
513
#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
514 515
	__ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
#endif
L
Linus Torvalds 已提交
516 517
};

518
int fb_init_device(struct fb_info *fb_info)
L
Linus Torvalds 已提交
519
{
520 521
	int i, error = 0;

522
	dev_set_drvdata(fb_info->dev, fb_info);
L
Linus Torvalds 已提交
523

524 525
	fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;

526 527
	for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
		error = device_create_file(fb_info->dev, &device_attrs[i]);
528 529 530 531 532 533 534

		if (error)
			break;
	}

	if (error) {
		while (--i >= 0)
535
			device_remove_file(fb_info->dev, &device_attrs[i]);
536 537 538
		fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
	}

L
Linus Torvalds 已提交
539 540 541
	return 0;
}

542
void fb_cleanup_device(struct fb_info *fb_info)
L
Linus Torvalds 已提交
543 544 545
{
	unsigned int i;

546
	if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
547 548
		for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
			device_remove_file(fb_info->dev, &device_attrs[i]);
549 550 551

		fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
	}
L
Linus Torvalds 已提交
552 553
}

R
Rob Clark 已提交
554
#if IS_ENABLED(CONFIG_FB_BACKLIGHT)
555 556 557 558 559 560 561 562 563 564
/* This function generates a linear backlight curve
 *
 *     0: off
 *   1-7: min
 * 8-127: linear from min to max
 */
void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
{
	unsigned int i, flat, count, range = (max - min);

565 566
	mutex_lock(&fb_info->bl_curve_mutex);

567
	fb_info->bl_curve[0] = off;
L
Linus Torvalds 已提交
568

569 570 571 572 573 574
	for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
		fb_info->bl_curve[flat] = min;

	count = FB_BACKLIGHT_LEVELS * 15 / 16;
	for (i = 0; i < count; ++i)
		fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
575 576

	mutex_unlock(&fb_info->bl_curve_mutex);
577 578 579
}
EXPORT_SYMBOL_GPL(fb_bl_default_curve);
#endif