blob: 303fb9f35b2a9a147c3141d0784caef4b09fee13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/fb.h>
21#include <linux/console.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -070024#define FB_SYSFS_FLAG_ATTR 1
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/**
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 Marchi25985ed2011-03-30 22:57:33 -030036 * Returns the new structure, or NULL if an error occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 */
39struct 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. Daplasdef1ede2006-01-09 20:53:45 -080050 p = kzalloc(fb_info_size + size, GFP_KERNEL);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (!p)
53 return NULL;
Antonino A. Daplasdef1ede2006-01-09 20:53:45 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 info = (struct fb_info *) p;
56
57 if (size)
58 info->par = p + fb_info_size;
59
60 info->device = dev;
61
Michael Hanselmann5474c122006-06-25 05:47:08 -070062#ifdef CONFIG_FB_BACKLIGHT
Richard Purdie37ce69a2007-02-10 14:10:33 +000063 mutex_init(&info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -070064#endif
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return info;
67#undef PADDING
68#undef BYTES_PER_LONG
69}
70EXPORT_SYMBOL(framebuffer_alloc);
71
72/**
73 * framebuffer_release - marks the structure available for freeing
74 *
75 * @info: frame buffer info structure
76 *
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -070077 * Drop the reference count of the device embedded in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * framebuffer info structure.
79 *
80 */
81void framebuffer_release(struct fb_info *info)
82{
Marcin Slusarz1471ca92010-05-16 17:27:03 +020083 kfree(info->apertures);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 kfree(info);
85}
86EXPORT_SYMBOL(framebuffer_release);
87
88static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
89{
90 int err;
91
92 var->activate |= FB_ACTIVATE_FORCE;
Torben Hohnac751ef2011-01-25 15:07:35 -080093 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 fb_info->flags |= FBINFO_MISC_USEREVENT;
95 err = fb_set_var(fb_info, var);
96 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
Torben Hohnac751ef2011-01-25 15:07:35 -080097 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (err)
99 return err;
100 return 0;
101}
102
103static int mode_string(char *buf, unsigned int offset,
104 const struct fb_videomode *mode)
105{
106 char m = 'U';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700107 char v = 'p';
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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 Thompson9dac73a2006-06-26 00:27:00 -0700115
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 Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700125static ssize_t store_mode(struct device *device, struct device_attribute *attr,
126 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700128 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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-Hartman78cde082006-09-14 07:30:59 -0700156static ssize_t show_mode(struct device *device, struct device_attribute *attr,
157 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700159 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (!fb_info->mode)
162 return 0;
163
164 return mode_string(buf, 0, fb_info->mode);
165}
166
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700167static ssize_t store_modes(struct device *device,
168 struct device_attribute *attr,
169 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700171 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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
Alan Cox50a60452013-01-25 10:28:15 +1000178 if (!lock_fb_info(fb_info))
179 return -ENODEV;
Torben Hohnac751ef2011-01-25 15:07:35 -0800180 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 list_splice(&fb_info->modelist, &old_list);
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800182 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 &fb_info->modelist);
184 if (fb_new_modelist(fb_info)) {
185 fb_destroy_modelist(&fb_info->modelist);
186 list_splice(&old_list, &fb_info->modelist);
187 } else
188 fb_destroy_modelist(&old_list);
189
Torben Hohnac751ef2011-01-25 15:07:35 -0800190 console_unlock();
Alan Cox50a60452013-01-25 10:28:15 +1000191 unlock_fb_info(fb_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return 0;
194}
195
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700196static ssize_t show_modes(struct device *device, struct device_attribute *attr,
197 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700199 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned int i;
201 struct list_head *pos;
202 struct fb_modelist *modelist;
203 const struct fb_videomode *mode;
204
205 i = 0;
206 list_for_each(pos, &fb_info->modelist) {
207 modelist = list_entry(pos, struct fb_modelist, list);
208 mode = &modelist->mode;
209 i += mode_string(buf, i, mode);
210 }
211 return i;
212}
213
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700214static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
215 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700217 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct fb_var_screeninfo var;
219 char ** last = NULL;
220 int err;
221
222 var = fb_info->var;
223 var.bits_per_pixel = simple_strtoul(buf, last, 0);
224 if ((err = activate(fb_info, &var)))
225 return err;
226 return count;
227}
228
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700229static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
230 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700232 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
234}
235
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700236static ssize_t store_rotate(struct device *device,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800239{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700240 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800241 struct fb_var_screeninfo var;
242 char **last = NULL;
243 int err;
244
245 var = fb_info->var;
246 var.rotate = simple_strtoul(buf, last, 0);
247
248 if ((err = activate(fb_info, &var)))
249 return err;
250
251 return count;
252}
253
254
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700255static ssize_t show_rotate(struct device *device,
256 struct device_attribute *attr, char *buf)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800257{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700258 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800259
260 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
261}
262
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700263static ssize_t store_virtual(struct device *device,
264 struct device_attribute *attr,
265 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700267 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct fb_var_screeninfo var;
269 char *last = NULL;
270 int err;
271
272 var = fb_info->var;
273 var.xres_virtual = simple_strtoul(buf, &last, 0);
274 last++;
275 if (last - buf >= count)
276 return -EINVAL;
277 var.yres_virtual = simple_strtoul(last, &last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if ((err = activate(fb_info, &var)))
280 return err;
281 return count;
282}
283
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700284static ssize_t show_virtual(struct device *device,
285 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700287 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700289 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700292static ssize_t show_stride(struct device *device,
293 struct device_attribute *attr, char *buf)
James Simmonsc14e2cf2005-10-24 21:46:21 +0100294{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700295 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100296 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
297}
298
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700299static ssize_t store_blank(struct device *device,
300 struct device_attribute *attr,
301 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700303 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 char *last = NULL;
305 int err;
306
Torben Hohnac751ef2011-01-25 15:07:35 -0800307 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 fb_info->flags |= FBINFO_MISC_USEREVENT;
309 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
310 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
Torben Hohnac751ef2011-01-25 15:07:35 -0800311 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (err < 0)
313 return err;
314 return count;
315}
316
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700317static ssize_t show_blank(struct device *device,
318 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700320// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322}
323
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700324static ssize_t store_console(struct device *device,
325 struct device_attribute *attr,
326 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700328// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700332static ssize_t show_console(struct device *device,
333 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700335// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700339static ssize_t store_cursor(struct device *device,
340 struct device_attribute *attr,
341 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700343// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700347static ssize_t show_cursor(struct device *device,
348 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700350// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352}
353
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700354static ssize_t store_pan(struct device *device,
355 struct device_attribute *attr,
356 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700358 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct fb_var_screeninfo var;
360 char *last = NULL;
361 int err;
362
363 var = fb_info->var;
364 var.xoffset = simple_strtoul(buf, &last, 0);
365 last++;
366 if (last - buf >= count)
367 return -EINVAL;
368 var.yoffset = simple_strtoul(last, &last, 0);
369
Torben Hohnac751ef2011-01-25 15:07:35 -0800370 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 err = fb_pan_display(fb_info, &var);
Torben Hohnac751ef2011-01-25 15:07:35 -0800372 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (err < 0)
375 return err;
376 return count;
377}
378
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700379static ssize_t show_pan(struct device *device,
380 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700382 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
Antonino A. Daplasc4270632007-05-08 00:37:30 -0700384 fb_info->var.yoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700387static ssize_t show_name(struct device *device,
388 struct device_attribute *attr, char *buf)
James Simmons02459ea2005-07-30 23:49:54 +0100389{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700390 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmons02459ea2005-07-30 23:49:54 +0100391
392 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
393}
394
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700395static ssize_t store_fbstate(struct device *device,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
Matthew Garrett30420f82006-01-09 20:53:01 -0800398{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700399 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800400 u32 state;
401 char *last = NULL;
402
403 state = simple_strtoul(buf, &last, 0);
404
Herton Ronaldo Krzesinski9e769ff2011-06-17 19:02:39 +0000405 if (!lock_fb_info(fb_info))
406 return -ENODEV;
Torben Hohnac751ef2011-01-25 15:07:35 -0800407 console_lock();
Matthew Garrett30420f82006-01-09 20:53:01 -0800408 fb_set_suspend(fb_info, (int)state);
Torben Hohnac751ef2011-01-25 15:07:35 -0800409 console_unlock();
Herton Ronaldo Krzesinski9e769ff2011-06-17 19:02:39 +0000410 unlock_fb_info(fb_info);
Matthew Garrett30420f82006-01-09 20:53:01 -0800411
412 return count;
413}
414
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700415static ssize_t show_fbstate(struct device *device,
416 struct device_attribute *attr, char *buf)
Matthew Garrett30420f82006-01-09 20:53:01 -0800417{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700418 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800419 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
420}
421
Michael Hanselmann5474c122006-06-25 05:47:08 -0700422#ifdef CONFIG_FB_BACKLIGHT
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700423static ssize_t store_bl_curve(struct device *device,
424 struct device_attribute *attr,
425 const char *buf, size_t count)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700426{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700427 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700428 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
429 unsigned int i;
430
Michael Hanselmann25981de2006-09-25 16:25:07 -0700431 /* Some drivers don't use framebuffer_alloc(), but those also
432 * don't have backlights.
433 */
434 if (!fb_info || !fb_info->bl_dev)
435 return -ENODEV;
436
Michael Hanselmann5474c122006-06-25 05:47:08 -0700437 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
438 return -EINVAL;
439
440 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
441 if (sscanf(&buf[i * 24],
442 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
443 &tmp_curve[i * 8 + 0],
444 &tmp_curve[i * 8 + 1],
445 &tmp_curve[i * 8 + 2],
446 &tmp_curve[i * 8 + 3],
447 &tmp_curve[i * 8 + 4],
448 &tmp_curve[i * 8 + 5],
449 &tmp_curve[i * 8 + 6],
450 &tmp_curve[i * 8 + 7]) != 8)
451 return -EINVAL;
452
453 /* If there has been an error in the input data, we won't
454 * reach this loop.
455 */
Richard Purdie37ce69a2007-02-10 14:10:33 +0000456 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700457 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
458 fb_info->bl_curve[i] = tmp_curve[i];
Richard Purdie37ce69a2007-02-10 14:10:33 +0000459 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700460
461 return count;
462}
463
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700464static ssize_t show_bl_curve(struct device *device,
465 struct device_attribute *attr, char *buf)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700466{
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700467 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700468 ssize_t len = 0;
469 unsigned int i;
470
Michael Hanselmann25981de2006-09-25 16:25:07 -0700471 /* Some drivers don't use framebuffer_alloc(), but those also
472 * don't have backlights.
473 */
474 if (!fb_info || !fb_info->bl_dev)
475 return -ENODEV;
476
Richard Purdie37ce69a2007-02-10 14:10:33 +0000477 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700478 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
479 len += snprintf(&buf[len], PAGE_SIZE,
480 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
481 fb_info->bl_curve[i + 0],
482 fb_info->bl_curve[i + 1],
483 fb_info->bl_curve[i + 2],
484 fb_info->bl_curve[i + 3],
485 fb_info->bl_curve[i + 4],
486 fb_info->bl_curve[i + 5],
487 fb_info->bl_curve[i + 6],
488 fb_info->bl_curve[i + 7]);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000489 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700490
491 return len;
492}
493#endif
494
Jon Smirl913e7ec2006-04-12 19:43:35 -0400495/* When cmap is added back in it should be a binary attribute
496 * not a text one. Consideration should also be given to converting
497 * fbdev to use configfs instead of sysfs */
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700498static struct device_attribute device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
500 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
502 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
503 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
504 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
505 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
506 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
James Simmons02459ea2005-07-30 23:49:54 +0100507 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100508 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800509 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
Matthew Garrett30420f82006-01-09 20:53:01 -0800510 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Michael Hanselmann5474c122006-06-25 05:47:08 -0700511#ifdef CONFIG_FB_BACKLIGHT
512 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514};
515
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700516int fb_init_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700518 int i, error = 0;
519
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700520 dev_set_drvdata(fb_info->dev, fb_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700522 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
523
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700524 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
525 error = device_create_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700526
527 if (error)
528 break;
529 }
530
531 if (error) {
532 while (--i >= 0)
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700533 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700534 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538}
539
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700540void fb_cleanup_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned int i;
543
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700544 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
Greg Kroah-Hartman78cde082006-09-14 07:30:59 -0700545 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
546 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700547
548 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Michael Hanselmann5474c122006-06-25 05:47:08 -0700552#ifdef CONFIG_FB_BACKLIGHT
553/* This function generates a linear backlight curve
554 *
555 * 0: off
556 * 1-7: min
557 * 8-127: linear from min to max
558 */
559void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
560{
561 unsigned int i, flat, count, range = (max - min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Richard Purdie37ce69a2007-02-10 14:10:33 +0000563 mutex_lock(&fb_info->bl_curve_mutex);
564
Michael Hanselmann5474c122006-06-25 05:47:08 -0700565 fb_info->bl_curve[0] = off;
566
567 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
568 fb_info->bl_curve[flat] = min;
569
570 count = FB_BACKLIGHT_LEVELS * 15 / 16;
571 for (i = 0; i < count; ++i)
572 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000573
574 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700575}
576EXPORT_SYMBOL_GPL(fb_bl_default_curve);
577#endif