blob: 53444ac19fe05a320852b89d05aa6b58a9da64ae [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();
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800181 if (!lock_fb_info(fb_info)) {
182 console_unlock();
183 return -ENODEV;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 list_splice(&fb_info->modelist, &old_list);
Geert Uytterhoeven9791d762007-02-12 00:55:19 -0800187 fb_videomode_to_modelist((const struct fb_videomode *)buf, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 &fb_info->modelist);
189 if (fb_new_modelist(fb_info)) {
190 fb_destroy_modelist(&fb_info->modelist);
191 list_splice(&old_list, &fb_info->modelist);
192 } else
193 fb_destroy_modelist(&old_list);
194
Alan Cox50e244c2013-01-25 10:28:15 +1000195 unlock_fb_info(fb_info);
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800196 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 return 0;
199}
200
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700201static ssize_t show_modes(struct device *device, struct device_attribute *attr,
202 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700204 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned int i;
206 struct list_head *pos;
207 struct fb_modelist *modelist;
208 const struct fb_videomode *mode;
209
210 i = 0;
211 list_for_each(pos, &fb_info->modelist) {
212 modelist = list_entry(pos, struct fb_modelist, list);
213 mode = &modelist->mode;
214 i += mode_string(buf, i, mode);
215 }
216 return i;
217}
218
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700219static ssize_t store_bpp(struct device *device, struct device_attribute *attr,
220 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700222 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct fb_var_screeninfo var;
224 char ** last = NULL;
225 int err;
226
227 var = fb_info->var;
228 var.bits_per_pixel = simple_strtoul(buf, last, 0);
229 if ((err = activate(fb_info, &var)))
230 return err;
231 return count;
232}
233
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700234static ssize_t show_bpp(struct device *device, struct device_attribute *attr,
235 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700237 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.bits_per_pixel);
239}
240
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700241static ssize_t store_rotate(struct device *device,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800244{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700245 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800246 struct fb_var_screeninfo var;
247 char **last = NULL;
248 int err;
249
250 var = fb_info->var;
251 var.rotate = simple_strtoul(buf, last, 0);
252
253 if ((err = activate(fb_info, &var)))
254 return err;
255
256 return count;
257}
258
259
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700260static ssize_t show_rotate(struct device *device,
261 struct device_attribute *attr, char *buf)
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800262{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700263 struct fb_info *fb_info = dev_get_drvdata(device);
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800264
265 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->var.rotate);
266}
267
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700268static ssize_t store_virtual(struct device *device,
269 struct device_attribute *attr,
270 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700272 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct fb_var_screeninfo var;
274 char *last = NULL;
275 int err;
276
277 var = fb_info->var;
278 var.xres_virtual = simple_strtoul(buf, &last, 0);
279 last++;
280 if (last - buf >= count)
281 return -EINVAL;
282 var.yres_virtual = simple_strtoul(last, &last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if ((err = activate(fb_info, &var)))
285 return err;
286 return count;
287}
288
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700289static ssize_t show_virtual(struct device *device,
290 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700292 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xres_virtual,
Jon Smirle2c16492005-06-13 15:52:36 -0700294 fb_info->var.yres_virtual);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700297static ssize_t show_stride(struct device *device,
298 struct device_attribute *attr, char *buf)
James Simmonsc14e2cf2005-10-24 21:46:21 +0100299{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700300 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmonsc14e2cf2005-10-24 21:46:21 +0100301 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->fix.line_length);
302}
303
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700304static ssize_t store_blank(struct device *device,
305 struct device_attribute *attr,
306 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700308 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 char *last = NULL;
310 int err;
311
Torben Hohnac751ef2011-01-25 15:07:35 -0800312 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 fb_info->flags |= FBINFO_MISC_USEREVENT;
314 err = fb_blank(fb_info, simple_strtoul(buf, &last, 0));
315 fb_info->flags &= ~FBINFO_MISC_USEREVENT;
Torben Hohnac751ef2011-01-25 15:07:35 -0800316 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (err < 0)
318 return err;
319 return count;
320}
321
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700322static ssize_t show_blank(struct device *device,
323 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700325// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327}
328
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700329static ssize_t store_console(struct device *device,
330 struct device_attribute *attr,
331 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700333// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700337static ssize_t show_console(struct device *device,
338 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700340// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342}
343
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700344static ssize_t store_cursor(struct device *device,
345 struct device_attribute *attr,
346 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700348// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700352static ssize_t show_cursor(struct device *device,
353 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700355// struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700359static ssize_t store_pan(struct device *device,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700363 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct fb_var_screeninfo var;
365 char *last = NULL;
366 int err;
367
368 var = fb_info->var;
369 var.xoffset = simple_strtoul(buf, &last, 0);
370 last++;
371 if (last - buf >= count)
372 return -EINVAL;
373 var.yoffset = simple_strtoul(last, &last, 0);
374
Torben Hohnac751ef2011-01-25 15:07:35 -0800375 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 err = fb_pan_display(fb_info, &var);
Torben Hohnac751ef2011-01-25 15:07:35 -0800377 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (err < 0)
380 return err;
381 return count;
382}
383
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700384static ssize_t show_pan(struct device *device,
385 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700387 struct fb_info *fb_info = dev_get_drvdata(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return snprintf(buf, PAGE_SIZE, "%d,%d\n", fb_info->var.xoffset,
Antonino A. Daplasc4270632007-05-08 00:37:30 -0700389 fb_info->var.yoffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700392static ssize_t show_name(struct device *device,
393 struct device_attribute *attr, char *buf)
James Simmons02459ea2005-07-30 23:49:54 +0100394{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700395 struct fb_info *fb_info = dev_get_drvdata(device);
James Simmons02459ea2005-07-30 23:49:54 +0100396
397 return snprintf(buf, PAGE_SIZE, "%s\n", fb_info->fix.id);
398}
399
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700400static ssize_t store_fbstate(struct device *device,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
Matthew Garrett30420f82006-01-09 20:53:01 -0800403{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700404 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800405 u32 state;
406 char *last = NULL;
407
408 state = simple_strtoul(buf, &last, 0);
409
Torben Hohnac751ef2011-01-25 15:07:35 -0800410 console_lock();
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800411 if (!lock_fb_info(fb_info)) {
412 console_unlock();
413 return -ENODEV;
414 }
415
Matthew Garrett30420f82006-01-09 20:53:01 -0800416 fb_set_suspend(fb_info, (int)state);
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800417
Herton Ronaldo Krzesinski9e769ff2011-06-17 19:02:39 +0000418 unlock_fb_info(fb_info);
Gu Zheng3a41c5d2013-11-05 18:00:57 +0800419 console_unlock();
Matthew Garrett30420f82006-01-09 20:53:01 -0800420
421 return count;
422}
423
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700424static ssize_t show_fbstate(struct device *device,
425 struct device_attribute *attr, char *buf)
Matthew Garrett30420f82006-01-09 20:53:01 -0800426{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700427 struct fb_info *fb_info = dev_get_drvdata(device);
Matthew Garrett30420f82006-01-09 20:53:01 -0800428 return snprintf(buf, PAGE_SIZE, "%d\n", fb_info->state);
429}
430
Michael Hanselmann5474c122006-06-25 05:47:08 -0700431#ifdef CONFIG_FB_BACKLIGHT
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700432static ssize_t store_bl_curve(struct device *device,
433 struct device_attribute *attr,
434 const char *buf, size_t count)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700435{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700436 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700437 u8 tmp_curve[FB_BACKLIGHT_LEVELS];
438 unsigned int i;
439
Michael Hanselmann25981de2006-09-25 16:25:07 -0700440 /* Some drivers don't use framebuffer_alloc(), but those also
441 * don't have backlights.
442 */
443 if (!fb_info || !fb_info->bl_dev)
444 return -ENODEV;
445
Michael Hanselmann5474c122006-06-25 05:47:08 -0700446 if (count != (FB_BACKLIGHT_LEVELS / 8 * 24))
447 return -EINVAL;
448
449 for (i = 0; i < (FB_BACKLIGHT_LEVELS / 8); ++i)
450 if (sscanf(&buf[i * 24],
451 "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx\n",
452 &tmp_curve[i * 8 + 0],
453 &tmp_curve[i * 8 + 1],
454 &tmp_curve[i * 8 + 2],
455 &tmp_curve[i * 8 + 3],
456 &tmp_curve[i * 8 + 4],
457 &tmp_curve[i * 8 + 5],
458 &tmp_curve[i * 8 + 6],
459 &tmp_curve[i * 8 + 7]) != 8)
460 return -EINVAL;
461
462 /* If there has been an error in the input data, we won't
463 * reach this loop.
464 */
Richard Purdie37ce69a2007-02-10 14:10:33 +0000465 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700466 for (i = 0; i < FB_BACKLIGHT_LEVELS; ++i)
467 fb_info->bl_curve[i] = tmp_curve[i];
Richard Purdie37ce69a2007-02-10 14:10:33 +0000468 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700469
470 return count;
471}
472
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700473static ssize_t show_bl_curve(struct device *device,
474 struct device_attribute *attr, char *buf)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700475{
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700476 struct fb_info *fb_info = dev_get_drvdata(device);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700477 ssize_t len = 0;
478 unsigned int i;
479
Michael Hanselmann25981de2006-09-25 16:25:07 -0700480 /* Some drivers don't use framebuffer_alloc(), but those also
481 * don't have backlights.
482 */
483 if (!fb_info || !fb_info->bl_dev)
484 return -ENODEV;
485
Richard Purdie37ce69a2007-02-10 14:10:33 +0000486 mutex_lock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700487 for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
488 len += snprintf(&buf[len], PAGE_SIZE,
489 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
490 fb_info->bl_curve[i + 0],
491 fb_info->bl_curve[i + 1],
492 fb_info->bl_curve[i + 2],
493 fb_info->bl_curve[i + 3],
494 fb_info->bl_curve[i + 4],
495 fb_info->bl_curve[i + 5],
496 fb_info->bl_curve[i + 6],
497 fb_info->bl_curve[i + 7]);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000498 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700499
500 return len;
501}
502#endif
503
Jon Smirl913e7ec2006-04-12 19:43:35 -0400504/* When cmap is added back in it should be a binary attribute
505 * not a text one. Consideration should also be given to converting
506 * fbdev to use configfs instead of sysfs */
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700507static struct device_attribute device_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 __ATTR(bits_per_pixel, S_IRUGO|S_IWUSR, show_bpp, store_bpp),
509 __ATTR(blank, S_IRUGO|S_IWUSR, show_blank, store_blank),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 __ATTR(console, S_IRUGO|S_IWUSR, show_console, store_console),
511 __ATTR(cursor, S_IRUGO|S_IWUSR, show_cursor, store_cursor),
512 __ATTR(mode, S_IRUGO|S_IWUSR, show_mode, store_mode),
513 __ATTR(modes, S_IRUGO|S_IWUSR, show_modes, store_modes),
514 __ATTR(pan, S_IRUGO|S_IWUSR, show_pan, store_pan),
515 __ATTR(virtual_size, S_IRUGO|S_IWUSR, show_virtual, store_virtual),
James Simmons02459ea2005-07-30 23:49:54 +0100516 __ATTR(name, S_IRUGO, show_name, NULL),
James Simmonsc14e2cf2005-10-24 21:46:21 +0100517 __ATTR(stride, S_IRUGO, show_stride, NULL),
Antonino A. Daplasa812c942005-11-08 21:39:15 -0800518 __ATTR(rotate, S_IRUGO|S_IWUSR, show_rotate, store_rotate),
Matthew Garrett30420f82006-01-09 20:53:01 -0800519 __ATTR(state, S_IRUGO|S_IWUSR, show_fbstate, store_fbstate),
Michael Hanselmann5474c122006-06-25 05:47:08 -0700520#ifdef CONFIG_FB_BACKLIGHT
521 __ATTR(bl_curve, S_IRUGO|S_IWUSR, show_bl_curve, store_bl_curve),
522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523};
524
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700525int fb_init_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700527 int i, error = 0;
528
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700529 dev_set_drvdata(fb_info->dev, fb_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700531 fb_info->class_flag |= FB_SYSFS_FLAG_ATTR;
532
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700533 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
534 error = device_create_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700535
536 if (error)
537 break;
538 }
539
540 if (error) {
541 while (--i >= 0)
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700542 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700543 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
544 }
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547}
548
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700549void fb_cleanup_device(struct fb_info *fb_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned int i;
552
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700553 if (fb_info->class_flag & FB_SYSFS_FLAG_ATTR) {
Greg Kroah-Hartman78cde08872006-09-14 07:30:59 -0700554 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
555 device_remove_file(fb_info->dev, &device_attrs[i]);
Antonino A. Daplas1a6600b2006-10-03 01:14:50 -0700556
557 fb_info->class_flag &= ~FB_SYSFS_FLAG_ATTR;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Michael Hanselmann5474c122006-06-25 05:47:08 -0700561#ifdef CONFIG_FB_BACKLIGHT
562/* This function generates a linear backlight curve
563 *
564 * 0: off
565 * 1-7: min
566 * 8-127: linear from min to max
567 */
568void fb_bl_default_curve(struct fb_info *fb_info, u8 off, u8 min, u8 max)
569{
570 unsigned int i, flat, count, range = (max - min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Richard Purdie37ce69a2007-02-10 14:10:33 +0000572 mutex_lock(&fb_info->bl_curve_mutex);
573
Michael Hanselmann5474c122006-06-25 05:47:08 -0700574 fb_info->bl_curve[0] = off;
575
576 for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat)
577 fb_info->bl_curve[flat] = min;
578
579 count = FB_BACKLIGHT_LEVELS * 15 / 16;
580 for (i = 0; i < count; ++i)
581 fb_info->bl_curve[flat + i] = min + (range * (i + 1) / count);
Richard Purdie37ce69a2007-02-10 14:10:33 +0000582
583 mutex_unlock(&fb_info->bl_curve_mutex);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700584}
585EXPORT_SYMBOL_GPL(fb_bl_default_curve);
586#endif