blob: a55e3669d1352f387c29e1342d4d54db429c2b3f [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-Hartman78cde08872006-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{
Dan Carpentercc440112012-05-14 23:58:37 +030083 if (!info)
84 return;
Marcin Slusarz1471ca92010-05-16 17:27:03 +020085 kfree(info->apertures);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 kfree(info);
87}
88EXPORT_SYMBOL(framebuffer_release);
89
90static int activate(struct fb_info *fb_info, struct fb_var_screeninfo *var)
91{
92 int err;
93
94 var->activate |= FB_ACTIVATE_FORCE;
Torben Hohnac751ef2011-01-25 15:07:35 -080095 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 fb_info->flags |= FBINFO_MISC_USEREVENT;
97 err = fb_set_var(fb_info, var);
98 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
Torben Hohnac751ef2011-01-25 15:07:35 -080099 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (err)
101 return err;
102 return 0;
103}
104
105static int mode_string(char *buf, unsigned int offset,
106 const struct fb_videomode *mode)
107{
108 char m = 'U';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700109 char v = 'p';
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (mode->flag & FB_MODE_IS_DETAILED)
112 m = 'D';
113 if (mode->flag & FB_MODE_IS_VESA)
114 m = 'V';
115 if (mode->flag & FB_MODE_IS_STANDARD)
116 m = 'S';
Daniel R Thompson9dac73a2006-06-26 00:27:00 -0700117
118 if (mode->vmode & FB_VMODE_INTERLACED)
119 v = 'i';
120 if (mode->vmode & FB_VMODE_DOUBLE)
121 v = 'd';
122
123 return snprintf(&buf[offset], PAGE_SIZE - offset, "%c:%dx%d%c-%d\n",
124 m, mode->xres, mode->yres, v, mode->refresh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700127static ssize_t store_mode(struct device *device, struct device_attribute *attr,
128 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700130 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 char mstr[100];
132 struct fb_var_screeninfo var;
133 struct fb_modelist *modelist;
134 struct fb_videomode *mode;
135 struct list_head *pos;
136 size_t i;
137 int err;
138
139 memset(&var, 0, sizeof(var));
140
141 list_for_each(pos, &fb_info->modelist) {
142 modelist = list_entry(pos, struct fb_modelist, list);
143 mode = &modelist->mode;
144 i = mode_string(mstr, 0, mode);
145 if (strncmp(mstr, buf, max(count, i)) == 0) {
146
147 var = fb_info->var;
148 fb_videomode_to_var(&var, mode);
149 if ((err = activate(fb_info, &var)))
150 return err;
151 fb_info->mode = mode;
152 return count;
153 }
154 }
155 return -EINVAL;
156}
157
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700158static ssize_t show_mode(struct device *device, struct device_attribute *attr,
159 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700161 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!fb_info->mode)
164 return 0;
165
166 return mode_string(buf, 0, fb_info->mode);
167}
168
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700169static ssize_t store_modes(struct device *device,
170 struct device_attribute *attr,
171 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700173 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 LIST_HEAD(old_list);
175 int i = count / sizeof(struct fb_videomode);
176
177 if (i * sizeof(struct fb_videomode) != count)
178 return -EINVAL;
179
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();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return 0;
193}
194
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700195static ssize_t show_modes(struct device *device, struct device_attribute *attr,
196 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700198 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned int i;
200 struct list_head *pos;
201 struct fb_modelist *modelist;
202 const struct fb_videomode *mode;
203
204 i = 0;
205 list_for_each(pos, &fb_info->modelist) {
206 modelist = list_entry(pos, struct fb_modelist, list);
207 mode = &modelist->mode;
208 i += mode_string(buf, i, mode);
209 }
210 return i;
211}
212
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700213static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
214 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700216 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct fb_var_screeninfo var;
218 char ** last = NULL;
219 int err;
220
221 var = fb_info->var;
222 var.bits_per_pixel = simple_strtoul(buf, last, 0);
223 if ((err = activate(fb_info, &var)))
224 return err;
225 return count;
226}
227
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700228static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
229 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700231 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
233}
234
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700235static ssize_t store_rotate(struct device *device,
236 struct device_attribute *attr,
237 const char *buf, size_t count)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800238{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700239 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800240 struct fb_var_screeninfo var;
241 char **last = NULL;
242 int err;
243
244 var = fb_info->var;
245 var.rotate = simple_strtoul(buf, last, 0);
246
247 if ((err = activate(fb_info, &var)))
248 return err;
249
250 return count;
251}
252
253
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700254static ssize_t show_rotate(struct device *device,
255 struct device_attribute *attr, char *buf)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800256{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700257 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800258
259 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
260}
261
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700262static ssize_t store_virtual(struct device *device,
263 struct device_attribute *attr,
264 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700266 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct fb_var_screeninfo var;
268 char *last = NULL;
269 int err;
270
271 var = fb_info->var;
272 var.xres_virtual = simple_strtoul(buf, &last, 0);
273 last++;
274 if (last - buf >= count)
275 return -EINVAL;
276 var.yres_virtual = simple_strtoul(last, &last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if ((err = activate(fb_info, &var)))
279 return err;
280 return count;
281}
282
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700283static ssize_t show_virtual(struct device *device,
284 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700286 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700288 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700291static ssize_t show_stride(struct device *device,
292 struct device_attribute *attr, char *buf)
James Simmonsc14e2cf2005-10-24 21:46:21 +0100293{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700294 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100295 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
296}
297
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700298static ssize_t store_blank(struct device *device,
299 struct device_attribute *attr,
300 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700302 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 char *last = NULL;
304 int err;
305
Torben Hohnac751ef2011-01-25 15:07:35 -0800306 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 fb_info->flags |= FBINFO_MISC_USEREVENT;
308 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
309 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
Torben Hohnac751ef2011-01-25 15:07:35 -0800310 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (err < 0)
312 return err;
313 return count;
314}
315
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700316static ssize_t show_blank(struct device *device,
317 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700319// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321}
322
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700323static ssize_t store_console(struct device *device,
324 struct device_attribute *attr,
325 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700327// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700331static ssize_t show_console(struct device *device,
332 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700334// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
336}
337
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700338static ssize_t store_cursor(struct device *device,
339 struct device_attribute *attr,
340 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700342// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700346static ssize_t show_cursor(struct device *device,
347 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700349// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}
352
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700353static ssize_t store_pan(struct device *device,
354 struct device_attribute *attr,
355 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700357 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct fb_var_screeninfo var;
359 char *last = NULL;
360 int err;
361
362 var = fb_info->var;
363 var.xoffset = simple_strtoul(buf, &last, 0);
364 last++;
365 if (last - buf >= count)
366 return -EINVAL;
367 var.yoffset = simple_strtoul(last, &last, 0);
368
Torben Hohnac751ef2011-01-25 15:07:35 -0800369 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 err = fb_pan_display(fb_info, &var);
Torben Hohnac751ef2011-01-25 15:07:35 -0800371 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (err < 0)
374 return err;
375 return count;
376}
377
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700378static ssize_t show_pan(struct device *device,
379 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700381 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
Antonino A. Daplasc4270632007-05-08 00:37:30 -0700383 fb_info->var.yoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700386static ssize_t show_name(struct device *device,
387 struct device_attribute *attr, char *buf)
James Simmons02459ea2005-07-30 23:49:54 +0100388{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700389 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmons02459ea2005-07-30 23:49:54 +0100390
391 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
392}
393
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700394static ssize_t store_fbstate(struct device *device,
395 struct device_attribute *attr,
396 const char *buf, size_t count)
Matthew Garrett30420f82006-01-09 20:53:01 -0800397{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700398 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800399 u32 state;
400 char *last = NULL;
401
402 state = simple_strtoul(buf, &last, 0);
403
Herton Ronaldo Krzesinski9e769ff2011-06-17 19:02:39 +0000404 if (!lock_fb_info(fb_info))
405 return -ENODEV;
Torben Hohnac751ef2011-01-25 15:07:35 -0800406 console_lock();
Matthew Garrett30420f82006-01-09 20:53:01 -0800407 fb_set_suspend(fb_info, (int)state);
Torben Hohnac751ef2011-01-25 15:07:35 -0800408 console_unlock();
Herton Ronaldo Krzesinski9e769ff2011-06-17 19:02:39 +0000409 unlock_fb_info(fb_info);
Matthew Garrett30420f82006-01-09 20:53:01 -0800410
411 return count;
412}
413
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700414static ssize_t show_fbstate(struct device *device,
415 struct device_attribute *attr, char *buf)
Matthew Garrett30420f82006-01-09 20:53:01 -0800416{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700417 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800418 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
419}
420
Michael Hanselmann5474c122006-06-25 05:47:08 -0700421#ifdef CONFIG_FB_BACKLIGHT
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700422static ssize_t store_bl_curve(struct device *device,
423 struct device_attribute *attr,
424 const char *buf, size_t count)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700425{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700426 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700427 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
428 unsigned int i;
429
Michael Hanselmann25981de2006-09-25 16:25:07 -0700430 /* Some drivers don't use framebuffer_alloc(), but those also
431 * don't have backlights.
432 */
433 if (!fb_info || !fb_info->bl_dev)
434 return -ENODEV;
435
Michael Hanselmann5474c122006-06-25 05:47:08 -0700436 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
437 return -EINVAL;
438
439 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
440 if (sscanf(&buf[i * 24],
441 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
442 &tmp_curve[i * 8 + 0],
443 &tmp_curve[i * 8 + 1],
444 &tmp_curve[i * 8 + 2],
445 &tmp_curve[i * 8 + 3],
446 &tmp_curve[i * 8 + 4],
447 &tmp_curve[i * 8 + 5],
448 &tmp_curve[i * 8 + 6],
449 &tmp_curve[i * 8 + 7]) != 8)
450 return -EINVAL;
451
452 /* If there has been an error in the input data, we won't
453 * reach this loop.
454 */
Richard Purdie37ce69a2007-02-10 14:10:33 +0000455 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700456 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
457 fb_info->bl_curve[i] = tmp_curve[i];
Richard Purdie37ce69a2007-02-10 14:10:33 +0000458 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700459
460 return count;
461}
462
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700463static ssize_t show_bl_curve(struct device *device,
464 struct device_attribute *attr, char *buf)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700465{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700466 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700467 ssize_t len = 0;
468 unsigned int i;
469
Michael Hanselmann25981de2006-09-25 16:25:07 -0700470 /* Some drivers don't use framebuffer_alloc(), but those also
471 * don't have backlights.
472 */
473 if (!fb_info || !fb_info->bl_dev)
474 return -ENODEV;
475
Richard Purdie37ce69a2007-02-10 14:10:33 +0000476 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700477 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
478 len += snprintf(&buf[len], PAGE_SIZE,
479 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
480 fb_info->bl_curve[i + 0],
481 fb_info->bl_curve[i + 1],
482 fb_info->bl_curve[i + 2],
483 fb_info->bl_curve[i + 3],
484 fb_info->bl_curve[i + 4],
485 fb_info->bl_curve[i + 5],
486 fb_info->bl_curve[i + 6],
487 fb_info->bl_curve[i + 7]);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000488 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700489
490 return len;
491}
492#endif
493
Jon Smirl913e7ec2006-04-12 19:43:35 -0400494/* When cmap is added back in it should be a binary attribute
495 * not a text one. Consideration should also be given to converting
496 * fbdev to use configfs instead of sysfs */
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700497static struct device_attribute device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
499 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
501 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
502 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
503 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
504 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
505 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
James Simmons02459ea2005-07-30 23:49:54 +0100506 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100507 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800508 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
Matthew Garrett30420f82006-01-09 20:53:01 -0800509 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Michael Hanselmann5474c122006-06-25 05:47:08 -0700510#ifdef CONFIG_FB_BACKLIGHT
511 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
512#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513};
514
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700515int fb_init_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700517 int i, error = 0;
518
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700519 dev_set_drvdata(fb_info->dev, fb_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700521 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
522
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700523 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
524 error = device_create_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700525
526 if (error)
527 break;
528 }
529
530 if (error) {
531 while (--i >= 0)
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700532 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700533 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700539void fb_cleanup_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 unsigned int i;
542
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700543 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700544 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
545 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700546
547 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Michael Hanselmann5474c122006-06-25 05:47:08 -0700551#ifdef CONFIG_FB_BACKLIGHT
552/* This function generates a linear backlight curve
553 *
554 * 0: off
555 * 1-7: min
556 * 8-127: linear from min to max
557 */
558void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
559{
560 unsigned int i, flat, count, range = (max - min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Richard Purdie37ce69a2007-02-10 14:10:33 +0000562 mutex_lock(&fb_info->bl_curve_mutex);
563
Michael Hanselmann5474c122006-06-25 05:47:08 -0700564 fb_info->bl_curve[0] = off;
565
566 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
567 fb_info->bl_curve[flat] = min;
568
569 count = FB_BACKLIGHT_LEVELS * 15 / 16;
570 for (i = 0; i < count; ++i)
571 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000572
573 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700574}
575EXPORT_SYMBOL_GPL(fb_bl_default_curve);
576#endif