V4L/DVB (8895): pvrusb2: Fail gracefully if an alien USB ID is used
[linux-2.6/kvm.git] / drivers / video / backlight / lcd.c
blob8e1731d3b2283e311bcad1d6f10475901467cda2
1 /*
2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
6 */
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/device.h>
11 #include <linux/lcd.h>
12 #include <linux/notifier.h>
13 #include <linux/ctype.h>
14 #include <linux/err.h>
15 #include <linux/fb.h>
17 #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
18 defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
19 /* This callback gets called when something important happens inside a
20 * framebuffer driver. We're looking if that important event is blanking,
21 * and if it is, we're switching lcd power as well ...
23 static int fb_notifier_callback(struct notifier_block *self,
24 unsigned long event, void *data)
26 struct lcd_device *ld;
27 struct fb_event *evdata = data;
29 /* If we aren't interested in this event, skip it immediately ... */
30 switch (event) {
31 case FB_EVENT_BLANK:
32 case FB_EVENT_MODE_CHANGE:
33 case FB_EVENT_MODE_CHANGE_ALL:
34 break;
35 default:
36 return 0;
39 ld = container_of(self, struct lcd_device, fb_notif);
40 if (!ld->ops)
41 return 0;
43 mutex_lock(&ld->ops_lock);
44 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
45 if (event == FB_EVENT_BLANK)
46 ld->ops->set_power(ld, *(int *)evdata->data);
47 else
48 ld->ops->set_mode(ld, evdata->data);
50 mutex_unlock(&ld->ops_lock);
51 return 0;
54 static int lcd_register_fb(struct lcd_device *ld)
56 memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
57 ld->fb_notif.notifier_call = fb_notifier_callback;
58 return fb_register_client(&ld->fb_notif);
61 static void lcd_unregister_fb(struct lcd_device *ld)
63 fb_unregister_client(&ld->fb_notif);
65 #else
66 static int lcd_register_fb(struct lcd_device *ld)
68 return 0;
71 static inline void lcd_unregister_fb(struct lcd_device *ld)
74 #endif /* CONFIG_FB */
76 static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
77 char *buf)
79 int rc;
80 struct lcd_device *ld = to_lcd_device(dev);
82 mutex_lock(&ld->ops_lock);
83 if (ld->ops && ld->ops->get_power)
84 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
85 else
86 rc = -ENXIO;
87 mutex_unlock(&ld->ops_lock);
89 return rc;
92 static ssize_t lcd_store_power(struct device *dev,
93 struct device_attribute *attr, const char *buf, size_t count)
95 int rc = -ENXIO;
96 char *endp;
97 struct lcd_device *ld = to_lcd_device(dev);
98 int power = simple_strtoul(buf, &endp, 0);
99 size_t size = endp - buf;
101 if (*endp && isspace(*endp))
102 size++;
103 if (size != count)
104 return -EINVAL;
106 mutex_lock(&ld->ops_lock);
107 if (ld->ops && ld->ops->set_power) {
108 pr_debug("lcd: set power to %d\n", power);
109 ld->ops->set_power(ld, power);
110 rc = count;
112 mutex_unlock(&ld->ops_lock);
114 return rc;
117 static ssize_t lcd_show_contrast(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 int rc = -ENXIO;
121 struct lcd_device *ld = to_lcd_device(dev);
123 mutex_lock(&ld->ops_lock);
124 if (ld->ops && ld->ops->get_contrast)
125 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
126 mutex_unlock(&ld->ops_lock);
128 return rc;
131 static ssize_t lcd_store_contrast(struct device *dev,
132 struct device_attribute *attr, const char *buf, size_t count)
134 int rc = -ENXIO;
135 char *endp;
136 struct lcd_device *ld = to_lcd_device(dev);
137 int contrast = simple_strtoul(buf, &endp, 0);
138 size_t size = endp - buf;
140 if (*endp && isspace(*endp))
141 size++;
142 if (size != count)
143 return -EINVAL;
145 mutex_lock(&ld->ops_lock);
146 if (ld->ops && ld->ops->set_contrast) {
147 pr_debug("lcd: set contrast to %d\n", contrast);
148 ld->ops->set_contrast(ld, contrast);
149 rc = count;
151 mutex_unlock(&ld->ops_lock);
153 return rc;
156 static ssize_t lcd_show_max_contrast(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct lcd_device *ld = to_lcd_device(dev);
161 return sprintf(buf, "%d\n", ld->props.max_contrast);
164 static struct class *lcd_class;
166 static void lcd_device_release(struct device *dev)
168 struct lcd_device *ld = to_lcd_device(dev);
169 kfree(ld);
172 static struct device_attribute lcd_device_attributes[] = {
173 __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
174 __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
175 __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
176 __ATTR_NULL,
180 * lcd_device_register - register a new object of lcd_device class.
181 * @name: the name of the new object(must be the same as the name of the
182 * respective framebuffer device).
183 * @devdata: an optional pointer to be stored in the device. The
184 * methods may retrieve it by using lcd_get_data(ld).
185 * @ops: the lcd operations structure.
187 * Creates and registers a new lcd device. Returns either an ERR_PTR()
188 * or a pointer to the newly allocated device.
190 struct lcd_device *lcd_device_register(const char *name, struct device *parent,
191 void *devdata, struct lcd_ops *ops)
193 struct lcd_device *new_ld;
194 int rc;
196 pr_debug("lcd_device_register: name=%s\n", name);
198 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
199 if (!new_ld)
200 return ERR_PTR(-ENOMEM);
202 mutex_init(&new_ld->ops_lock);
203 mutex_init(&new_ld->update_lock);
205 new_ld->dev.class = lcd_class;
206 new_ld->dev.parent = parent;
207 new_ld->dev.release = lcd_device_release;
208 strlcpy(new_ld->dev.bus_id, name, BUS_ID_SIZE);
209 dev_set_drvdata(&new_ld->dev, devdata);
211 rc = device_register(&new_ld->dev);
212 if (rc) {
213 kfree(new_ld);
214 return ERR_PTR(rc);
217 rc = lcd_register_fb(new_ld);
218 if (rc) {
219 device_unregister(&new_ld->dev);
220 return ERR_PTR(rc);
223 new_ld->ops = ops;
225 return new_ld;
227 EXPORT_SYMBOL(lcd_device_register);
230 * lcd_device_unregister - unregisters a object of lcd_device class.
231 * @ld: the lcd device object to be unregistered and freed.
233 * Unregisters a previously registered via lcd_device_register object.
235 void lcd_device_unregister(struct lcd_device *ld)
237 if (!ld)
238 return;
240 mutex_lock(&ld->ops_lock);
241 ld->ops = NULL;
242 mutex_unlock(&ld->ops_lock);
243 lcd_unregister_fb(ld);
245 device_unregister(&ld->dev);
247 EXPORT_SYMBOL(lcd_device_unregister);
249 static void __exit lcd_class_exit(void)
251 class_destroy(lcd_class);
254 static int __init lcd_class_init(void)
256 lcd_class = class_create(THIS_MODULE, "lcd");
257 if (IS_ERR(lcd_class)) {
258 printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n",
259 PTR_ERR(lcd_class));
260 return PTR_ERR(lcd_class);
263 lcd_class->dev_attrs = lcd_device_attributes;
264 return 0;
268 * if this is compiled into the kernel, we need to ensure that the
269 * class is registered before users of the class try to register lcd's
271 postcore_initcall(lcd_class_init);
272 module_exit(lcd_class_exit);
274 MODULE_LICENSE("GPL");
275 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
276 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");