ACPI: video: DMI workaround broken Acer 7720 BIOS enabling display brightness
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / video.c
1 /*
2  *  video.c - ACPI Video Driver ($Revision:$)
3  *
4  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
6  *  Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/mutex.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/input.h>
36 #include <linux/backlight.h>
37 #include <linux/thermal.h>
38 #include <linux/video_output.h>
39 #include <linux/sort.h>
40 #include <linux/pci.h>
41 #include <linux/pci_ids.h>
42 #include <asm/uaccess.h>
43
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
46
47 #define ACPI_VIDEO_CLASS                "video"
48 #define ACPI_VIDEO_BUS_NAME             "Video Bus"
49 #define ACPI_VIDEO_DEVICE_NAME          "Video Device"
50 #define ACPI_VIDEO_NOTIFY_SWITCH        0x80
51 #define ACPI_VIDEO_NOTIFY_PROBE         0x81
52 #define ACPI_VIDEO_NOTIFY_CYCLE         0x82
53 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT   0x83
54 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT   0x84
55
56 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS      0x85
57 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x86
58 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS        0x87
59 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS       0x88
60 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF           0x89
61
62 #define MAX_NAME_LEN    20
63
64 #define ACPI_VIDEO_DISPLAY_CRT  1
65 #define ACPI_VIDEO_DISPLAY_TV   2
66 #define ACPI_VIDEO_DISPLAY_DVI  3
67 #define ACPI_VIDEO_DISPLAY_LCD  4
68
69 #define _COMPONENT              ACPI_VIDEO_COMPONENT
70 ACPI_MODULE_NAME("video");
71
72 MODULE_AUTHOR("Bruno Ducrot");
73 MODULE_DESCRIPTION("ACPI Video Driver");
74 MODULE_LICENSE("GPL");
75
76 static int brightness_switch_enabled = 1;
77 module_param(brightness_switch_enabled, bool, 0644);
78
79 static int acpi_video_bus_add(struct acpi_device *device);
80 static int acpi_video_bus_remove(struct acpi_device *device, int type);
81 static int acpi_video_resume(struct acpi_device *device);
82 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
83
84 static const struct acpi_device_id video_device_ids[] = {
85         {ACPI_VIDEO_HID, 0},
86         {"", 0},
87 };
88 MODULE_DEVICE_TABLE(acpi, video_device_ids);
89
90 static struct acpi_driver acpi_video_bus = {
91         .name = "video",
92         .class = ACPI_VIDEO_CLASS,
93         .ids = video_device_ids,
94         .ops = {
95                 .add = acpi_video_bus_add,
96                 .remove = acpi_video_bus_remove,
97                 .resume = acpi_video_resume,
98                 .notify = acpi_video_bus_notify,
99                 },
100 };
101
102 struct acpi_video_bus_flags {
103         u8 multihead:1;         /* can switch video heads */
104         u8 rom:1;               /* can retrieve a video rom */
105         u8 post:1;              /* can configure the head to */
106         u8 reserved:5;
107 };
108
109 struct acpi_video_bus_cap {
110         u8 _DOS:1;              /*Enable/Disable output switching */
111         u8 _DOD:1;              /*Enumerate all devices attached to display adapter */
112         u8 _ROM:1;              /*Get ROM Data */
113         u8 _GPD:1;              /*Get POST Device */
114         u8 _SPD:1;              /*Set POST Device */
115         u8 _VPO:1;              /*Video POST Options */
116         u8 reserved:2;
117 };
118
119 struct acpi_video_device_attrib {
120         u32 display_index:4;    /* A zero-based instance of the Display */
121         u32 display_port_attachment:4;  /*This field differentiates the display type */
122         u32 display_type:4;     /*Describe the specific type in use */
123         u32 vendor_specific:4;  /*Chipset Vendor Specific */
124         u32 bios_can_detect:1;  /*BIOS can detect the device */
125         u32 depend_on_vga:1;    /*Non-VGA output device whose power is related to 
126                                    the VGA device. */
127         u32 pipe_id:3;          /*For VGA multiple-head devices. */
128         u32 reserved:10;        /*Must be 0 */
129         u32 device_id_scheme:1; /*Device ID Scheme */
130 };
131
132 struct acpi_video_enumerated_device {
133         union {
134                 u32 int_val;
135                 struct acpi_video_device_attrib attrib;
136         } value;
137         struct acpi_video_device *bind_info;
138 };
139
140 struct acpi_video_bus {
141         struct acpi_device *device;
142         u8 dos_setting;
143         struct acpi_video_enumerated_device *attached_array;
144         u8 attached_count;
145         struct acpi_video_bus_cap cap;
146         struct acpi_video_bus_flags flags;
147         struct list_head video_device_list;
148         struct mutex device_list_lock;  /* protects video_device_list */
149         struct proc_dir_entry *dir;
150         struct input_dev *input;
151         char phys[32];  /* for input device */
152 };
153
154 struct acpi_video_device_flags {
155         u8 crt:1;
156         u8 lcd:1;
157         u8 tvout:1;
158         u8 dvi:1;
159         u8 bios:1;
160         u8 unknown:1;
161         u8 reserved:2;
162 };
163
164 struct acpi_video_device_cap {
165         u8 _ADR:1;              /*Return the unique ID */
166         u8 _BCL:1;              /*Query list of brightness control levels supported */
167         u8 _BCM:1;              /*Set the brightness level */
168         u8 _BQC:1;              /* Get current brightness level */
169         u8 _BCQ:1;              /* Some buggy BIOS uses _BCQ instead of _BQC */
170         u8 _DDC:1;              /*Return the EDID for this device */
171         u8 _DCS:1;              /*Return status of output device */
172         u8 _DGS:1;              /*Query graphics state */
173         u8 _DSS:1;              /*Device state set */
174 };
175
176 struct acpi_video_brightness_flags {
177         u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
178         u8 _BCL_reversed:1;             /* _BCL package is in a reversed order*/
179         u8 _BCL_use_index:1;            /* levels in _BCL are index values */
180         u8 _BCM_use_index:1;            /* input of _BCM is an index value */
181         u8 _BQC_use_index:1;            /* _BQC returns an index value */
182 };
183
184 struct acpi_video_device_brightness {
185         int curr;
186         int count;
187         int *levels;
188         struct acpi_video_brightness_flags flags;
189 };
190
191 struct acpi_video_device {
192         unsigned long device_id;
193         struct acpi_video_device_flags flags;
194         struct acpi_video_device_cap cap;
195         struct list_head entry;
196         struct acpi_video_bus *video;
197         struct acpi_device *dev;
198         struct acpi_video_device_brightness *brightness;
199         struct backlight_device *backlight;
200         struct thermal_cooling_device *cdev;
201         struct output_device *output_dev;
202 };
203
204 /* bus */
205 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
206 static const struct file_operations acpi_video_bus_info_fops = {
207         .owner = THIS_MODULE,
208         .open = acpi_video_bus_info_open_fs,
209         .read = seq_read,
210         .llseek = seq_lseek,
211         .release = single_release,
212 };
213
214 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
215 static const struct file_operations acpi_video_bus_ROM_fops = {
216         .owner = THIS_MODULE,
217         .open = acpi_video_bus_ROM_open_fs,
218         .read = seq_read,
219         .llseek = seq_lseek,
220         .release = single_release,
221 };
222
223 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
224                                             struct file *file);
225 static const struct file_operations acpi_video_bus_POST_info_fops = {
226         .owner = THIS_MODULE,
227         .open = acpi_video_bus_POST_info_open_fs,
228         .read = seq_read,
229         .llseek = seq_lseek,
230         .release = single_release,
231 };
232
233 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
234 static ssize_t acpi_video_bus_write_POST(struct file *file,
235         const char __user *buffer, size_t count, loff_t *data);
236 static const struct file_operations acpi_video_bus_POST_fops = {
237         .owner = THIS_MODULE,
238         .open = acpi_video_bus_POST_open_fs,
239         .read = seq_read,
240         .write = acpi_video_bus_write_POST,
241         .llseek = seq_lseek,
242         .release = single_release,
243 };
244
245 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
246 static ssize_t acpi_video_bus_write_DOS(struct file *file,
247         const char __user *buffer, size_t count, loff_t *data);
248 static const struct file_operations acpi_video_bus_DOS_fops = {
249         .owner = THIS_MODULE,
250         .open = acpi_video_bus_DOS_open_fs,
251         .read = seq_read,
252         .write = acpi_video_bus_write_DOS,
253         .llseek = seq_lseek,
254         .release = single_release,
255 };
256
257 /* device */
258 static int acpi_video_device_info_open_fs(struct inode *inode,
259                                           struct file *file);
260 static const struct file_operations acpi_video_device_info_fops = {
261         .owner = THIS_MODULE,
262         .open = acpi_video_device_info_open_fs,
263         .read = seq_read,
264         .llseek = seq_lseek,
265         .release = single_release,
266 };
267
268 static int acpi_video_device_state_open_fs(struct inode *inode,
269                                            struct file *file);
270 static ssize_t acpi_video_device_write_state(struct file *file,
271         const char __user *buffer, size_t count, loff_t *data);
272 static const struct file_operations acpi_video_device_state_fops = {
273         .owner = THIS_MODULE,
274         .open = acpi_video_device_state_open_fs,
275         .read = seq_read,
276         .write = acpi_video_device_write_state,
277         .llseek = seq_lseek,
278         .release = single_release,
279 };
280
281 static int acpi_video_device_brightness_open_fs(struct inode *inode,
282                                                 struct file *file);
283 static ssize_t acpi_video_device_write_brightness(struct file *file,
284         const char __user *buffer, size_t count, loff_t *data);
285 static struct file_operations acpi_video_device_brightness_fops = {
286         .owner = THIS_MODULE,
287         .open = acpi_video_device_brightness_open_fs,
288         .read = seq_read,
289         .write = acpi_video_device_write_brightness,
290         .llseek = seq_lseek,
291         .release = single_release,
292 };
293
294 static int acpi_video_device_EDID_open_fs(struct inode *inode,
295                                           struct file *file);
296 static const struct file_operations acpi_video_device_EDID_fops = {
297         .owner = THIS_MODULE,
298         .open = acpi_video_device_EDID_open_fs,
299         .read = seq_read,
300         .llseek = seq_lseek,
301         .release = single_release,
302 };
303
304 static const char device_decode[][30] = {
305         "motherboard VGA device",
306         "PCI VGA device",
307         "AGP VGA device",
308         "UNKNOWN",
309 };
310
311 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
312 static void acpi_video_device_rebind(struct acpi_video_bus *video);
313 static void acpi_video_device_bind(struct acpi_video_bus *video,
314                                    struct acpi_video_device *device);
315 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
316 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
317                         int level);
318 static int acpi_video_device_lcd_get_level_current(
319                         struct acpi_video_device *device,
320                         unsigned long long *level);
321 static int acpi_video_get_next_level(struct acpi_video_device *device,
322                                      u32 level_current, u32 event);
323 static int acpi_video_switch_brightness(struct acpi_video_device *device,
324                                          int event);
325 static int acpi_video_device_get_state(struct acpi_video_device *device,
326                             unsigned long long *state);
327 static int acpi_video_output_get(struct output_device *od);
328 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
329
330 /*backlight device sysfs support*/
331 static int acpi_video_get_brightness(struct backlight_device *bd)
332 {
333         unsigned long long cur_level;
334         int i;
335         struct acpi_video_device *vd =
336                 (struct acpi_video_device *)bl_get_data(bd);
337
338         if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
339                 return -EINVAL;
340         for (i = 2; i < vd->brightness->count; i++) {
341                 if (vd->brightness->levels[i] == cur_level)
342                         /* The first two entries are special - see page 575
343                            of the ACPI spec 3.0 */
344                         return i-2;
345         }
346         return 0;
347 }
348
349 static int acpi_video_set_brightness(struct backlight_device *bd)
350 {
351         int request_level = bd->props.brightness + 2;
352         struct acpi_video_device *vd =
353                 (struct acpi_video_device *)bl_get_data(bd);
354
355         return acpi_video_device_lcd_set_level(vd,
356                                 vd->brightness->levels[request_level]);
357 }
358
359 static struct backlight_ops acpi_backlight_ops = {
360         .get_brightness = acpi_video_get_brightness,
361         .update_status  = acpi_video_set_brightness,
362 };
363
364 /*video output device sysfs support*/
365 static int acpi_video_output_get(struct output_device *od)
366 {
367         unsigned long long state;
368         struct acpi_video_device *vd =
369                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
370         acpi_video_device_get_state(vd, &state);
371         return (int)state;
372 }
373
374 static int acpi_video_output_set(struct output_device *od)
375 {
376         unsigned long state = od->request_state;
377         struct acpi_video_device *vd=
378                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
379         return acpi_video_device_set_state(vd, state);
380 }
381
382 static struct output_properties acpi_output_properties = {
383         .set_state = acpi_video_output_set,
384         .get_status = acpi_video_output_get,
385 };
386
387
388 /* thermal cooling device callbacks */
389 static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
390                                long *state)
391 {
392         struct acpi_device *device = cdev->devdata;
393         struct acpi_video_device *video = acpi_driver_data(device);
394
395         *state = video->brightness->count - 3;
396         return 0;
397 }
398
399 static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
400                                long *state)
401 {
402         struct acpi_device *device = cdev->devdata;
403         struct acpi_video_device *video = acpi_driver_data(device);
404         unsigned long long level;
405         int offset;
406
407         if (acpi_video_device_lcd_get_level_current(video, &level))
408                 return -EINVAL;
409         for (offset = 2; offset < video->brightness->count; offset++)
410                 if (level == video->brightness->levels[offset]) {
411                         *state = video->brightness->count - offset - 1;
412                         return 0;
413                 }
414
415         return -EINVAL;
416 }
417
418 static int
419 video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
420 {
421         struct acpi_device *device = cdev->devdata;
422         struct acpi_video_device *video = acpi_driver_data(device);
423         int level;
424
425         if ( state >= video->brightness->count - 2)
426                 return -EINVAL;
427
428         state = video->brightness->count - state;
429         level = video->brightness->levels[state -1];
430         return acpi_video_device_lcd_set_level(video, level);
431 }
432
433 static struct thermal_cooling_device_ops video_cooling_ops = {
434         .get_max_state = video_get_max_state,
435         .get_cur_state = video_get_cur_state,
436         .set_cur_state = video_set_cur_state,
437 };
438
439 /* --------------------------------------------------------------------------
440                                Video Management
441    -------------------------------------------------------------------------- */
442
443 /* device */
444
445 static int
446 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
447 {
448         int status;
449
450         status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
451
452         return status;
453 }
454
455 static int
456 acpi_video_device_get_state(struct acpi_video_device *device,
457                             unsigned long long *state)
458 {
459         int status;
460
461         status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
462
463         return status;
464 }
465
466 static int
467 acpi_video_device_set_state(struct acpi_video_device *device, int state)
468 {
469         int status;
470         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
471         struct acpi_object_list args = { 1, &arg0 };
472         unsigned long long ret;
473
474
475         arg0.integer.value = state;
476         status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
477
478         return status;
479 }
480
481 static int
482 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
483                                    union acpi_object **levels)
484 {
485         int status;
486         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
487         union acpi_object *obj;
488
489
490         *levels = NULL;
491
492         status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
493         if (!ACPI_SUCCESS(status))
494                 return status;
495         obj = (union acpi_object *)buffer.pointer;
496         if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
497                 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
498                 status = -EFAULT;
499                 goto err;
500         }
501
502         *levels = obj;
503
504         return 0;
505
506       err:
507         kfree(buffer.pointer);
508
509         return status;
510 }
511
512 static int
513 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
514 {
515         int status;
516         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
517         struct acpi_object_list args = { 1, &arg0 };
518         int state;
519
520         arg0.integer.value = level;
521
522         status = acpi_evaluate_object(device->dev->handle, "_BCM",
523                                       &args, NULL);
524         if (ACPI_FAILURE(status)) {
525                 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
526                 return -EIO;
527         }
528
529         device->brightness->curr = level;
530         for (state = 2; state < device->brightness->count; state++)
531                 if (level == device->brightness->levels[state]) {
532                         if (device->backlight)
533                                 device->backlight->props.brightness = state - 2;
534                         return 0;
535                 }
536
537         ACPI_ERROR((AE_INFO, "Current brightness invalid"));
538         return -EINVAL;
539 }
540
541 /*
542  * For some buggy _BQC methods, we need to add a constant value to
543  * the _BQC return value to get the actual current brightness level
544  */
545
546 static int bqc_offset_aml_bug_workaround;
547 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
548 {
549         bqc_offset_aml_bug_workaround = 9;
550         return 0;
551 }
552
553 static struct dmi_system_id video_dmi_table[] __initdata = {
554         /*
555          * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
556          */
557         {
558          .callback = video_set_bqc_offset,
559          .ident = "Acer Aspire 5720",
560          .matches = {
561                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
562                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
563                 },
564         },
565         {
566          .callback = video_set_bqc_offset,
567          .ident = "Acer Aspire 5710Z",
568          .matches = {
569                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
570                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
571                 },
572         },
573         {
574          .callback = video_set_bqc_offset,
575          .ident = "eMachines E510",
576          .matches = {
577                 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
578                 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
579                 },
580         },
581         {
582          .callback = video_set_bqc_offset,
583          .ident = "Acer Aspire 5315",
584          .matches = {
585                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
586                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
587                 },
588         },
589         {
590          .callback = video_set_bqc_offset,
591          .ident = "Acer Aspire 7720",
592          .matches = {
593                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
594                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
595                 },
596         },
597         {}
598 };
599
600 static int
601 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
602                                         unsigned long long *level)
603 {
604         acpi_status status = AE_OK;
605
606         if (device->cap._BQC || device->cap._BCQ) {
607                 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
608
609                 status = acpi_evaluate_integer(device->dev->handle, buf,
610                                                 NULL, level);
611                 if (ACPI_SUCCESS(status)) {
612                         if (device->brightness->flags._BQC_use_index) {
613                                 if (device->brightness->flags._BCL_reversed)
614                                         *level = device->brightness->count
615                                                                  - 3 - (*level);
616                                 *level = device->brightness->levels[*level + 2];
617
618                         }
619                         *level += bqc_offset_aml_bug_workaround;
620                         device->brightness->curr = *level;
621                         return 0;
622                 } else {
623                         /* Fixme:
624                          * should we return an error or ignore this failure?
625                          * dev->brightness->curr is a cached value which stores
626                          * the correct current backlight level in most cases.
627                          * ACPI video backlight still works w/ buggy _BQC.
628                          * http://bugzilla.kernel.org/show_bug.cgi?id=12233
629                          */
630                         ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
631                         device->cap._BQC = device->cap._BCQ = 0;
632                 }
633         }
634
635         *level = device->brightness->curr;
636         return 0;
637 }
638
639 static int
640 acpi_video_device_EDID(struct acpi_video_device *device,
641                        union acpi_object **edid, ssize_t length)
642 {
643         int status;
644         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
645         union acpi_object *obj;
646         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
647         struct acpi_object_list args = { 1, &arg0 };
648
649
650         *edid = NULL;
651
652         if (!device)
653                 return -ENODEV;
654         if (length == 128)
655                 arg0.integer.value = 1;
656         else if (length == 256)
657                 arg0.integer.value = 2;
658         else
659                 return -EINVAL;
660
661         status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
662         if (ACPI_FAILURE(status))
663                 return -ENODEV;
664
665         obj = buffer.pointer;
666
667         if (obj && obj->type == ACPI_TYPE_BUFFER)
668                 *edid = obj;
669         else {
670                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
671                 status = -EFAULT;
672                 kfree(obj);
673         }
674
675         return status;
676 }
677
678 /* bus */
679
680 static int
681 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
682 {
683         int status;
684         unsigned long long tmp;
685         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
686         struct acpi_object_list args = { 1, &arg0 };
687
688
689         arg0.integer.value = option;
690
691         status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
692         if (ACPI_SUCCESS(status))
693                 status = tmp ? (-EINVAL) : (AE_OK);
694
695         return status;
696 }
697
698 static int
699 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
700 {
701         int status;
702
703         status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
704
705         return status;
706 }
707
708 static int
709 acpi_video_bus_POST_options(struct acpi_video_bus *video,
710                             unsigned long long *options)
711 {
712         int status;
713
714         status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
715         *options &= 3;
716
717         return status;
718 }
719
720 /*
721  *  Arg:
722  *      video           : video bus device pointer
723  *      bios_flag       : 
724  *              0.      The system BIOS should NOT automatically switch(toggle)
725  *                      the active display output.
726  *              1.      The system BIOS should automatically switch (toggle) the
727  *                      active display output. No switch event.
728  *              2.      The _DGS value should be locked.
729  *              3.      The system BIOS should not automatically switch (toggle) the
730  *                      active display output, but instead generate the display switch
731  *                      event notify code.
732  *      lcd_flag        :
733  *              0.      The system BIOS should automatically control the brightness level
734  *                      of the LCD when the power changes from AC to DC
735  *              1.      The system BIOS should NOT automatically control the brightness 
736  *                      level of the LCD when the power changes from AC to DC.
737  * Return Value:
738  *              -1      wrong arg.
739  */
740
741 static int
742 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
743 {
744         acpi_integer status = 0;
745         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
746         struct acpi_object_list args = { 1, &arg0 };
747
748
749         if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
750                 status = -1;
751                 goto Failed;
752         }
753         arg0.integer.value = (lcd_flag << 2) | bios_flag;
754         video->dos_setting = arg0.integer.value;
755         acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
756
757       Failed:
758         return status;
759 }
760
761 /*
762  * Simple comparison function used to sort backlight levels.
763  */
764
765 static int
766 acpi_video_cmp_level(const void *a, const void *b)
767 {
768         return *(int *)a - *(int *)b;
769 }
770
771 /*
772  *  Arg:        
773  *      device  : video output device (LCD, CRT, ..)
774  *
775  *  Return Value:
776  *      Maximum brightness level
777  *
778  *  Allocate and initialize device->brightness.
779  */
780
781 static int
782 acpi_video_init_brightness(struct acpi_video_device *device)
783 {
784         union acpi_object *obj = NULL;
785         int i, max_level = 0, count = 0, level_ac_battery = 0;
786         unsigned long long level, level_old;
787         union acpi_object *o;
788         struct acpi_video_device_brightness *br = NULL;
789         int result = -EINVAL;
790
791         if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
792                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
793                                                 "LCD brightness level\n"));
794                 goto out;
795         }
796
797         if (obj->package.count < 2)
798                 goto out;
799
800         br = kzalloc(sizeof(*br), GFP_KERNEL);
801         if (!br) {
802                 printk(KERN_ERR "can't allocate memory\n");
803                 result = -ENOMEM;
804                 goto out;
805         }
806
807         br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
808                                 GFP_KERNEL);
809         if (!br->levels) {
810                 result = -ENOMEM;
811                 goto out_free;
812         }
813
814         for (i = 0; i < obj->package.count; i++) {
815                 o = (union acpi_object *)&obj->package.elements[i];
816                 if (o->type != ACPI_TYPE_INTEGER) {
817                         printk(KERN_ERR PREFIX "Invalid data\n");
818                         continue;
819                 }
820                 br->levels[count] = (u32) o->integer.value;
821
822                 if (br->levels[count] > max_level)
823                         max_level = br->levels[count];
824                 count++;
825         }
826
827         /*
828          * some buggy BIOS don't export the levels
829          * when machine is on AC/Battery in _BCL package.
830          * In this case, the first two elements in _BCL packages
831          * are also supported brightness levels that OS should take care of.
832          */
833         for (i = 2; i < count; i++) {
834                 if (br->levels[i] == br->levels[0])
835                         level_ac_battery++;
836                 if (br->levels[i] == br->levels[1])
837                         level_ac_battery++;
838         }
839
840         if (level_ac_battery < 2) {
841                 level_ac_battery = 2 - level_ac_battery;
842                 br->flags._BCL_no_ac_battery_levels = 1;
843                 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
844                         br->levels[i] = br->levels[i - level_ac_battery];
845                 count += level_ac_battery;
846         } else if (level_ac_battery > 2)
847                 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
848
849         /* Check if the _BCL package is in a reversed order */
850         if (max_level == br->levels[2]) {
851                 br->flags._BCL_reversed = 1;
852                 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
853                         acpi_video_cmp_level, NULL);
854         } else if (max_level != br->levels[count - 1])
855                 ACPI_ERROR((AE_INFO,
856                             "Found unordered _BCL package\n"));
857
858         br->count = count;
859         device->brightness = br;
860
861         /* Check the input/output of _BQC/_BCL/_BCM */
862         if ((max_level < 100) && (max_level <= (count - 2)))
863                 br->flags._BCL_use_index = 1;
864
865         /*
866          * _BCM is always consistent with _BCL,
867          * at least for all the laptops we have ever seen.
868          */
869         br->flags._BCM_use_index = br->flags._BCL_use_index;
870
871         /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
872         br->curr = level_old = max_level;
873
874         if (!device->cap._BQC)
875                 goto set_level;
876
877         result = acpi_video_device_lcd_get_level_current(device, &level_old);
878         if (result)
879                 goto out_free_levels;
880
881         /*
882          * Set the level to maximum and check if _BQC uses indexed value
883          */
884         result = acpi_video_device_lcd_set_level(device, max_level);
885         if (result)
886                 goto out_free_levels;
887
888         result = acpi_video_device_lcd_get_level_current(device, &level);
889         if (result)
890                 goto out_free_levels;
891
892         br->flags._BQC_use_index = (level == max_level ? 0 : 1);
893
894         if (!br->flags._BQC_use_index)
895                 goto set_level;
896
897         if (br->flags._BCL_reversed)
898                 level_old = (br->count - 1) - level_old;
899         level_old = br->levels[level_old];
900
901 set_level:
902         result = acpi_video_device_lcd_set_level(device, level_old);
903         if (result)
904                 goto out_free_levels;
905
906         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
907                           "found %d brightness levels\n", count - 2));
908         kfree(obj);
909         return result;
910
911 out_free_levels:
912         kfree(br->levels);
913 out_free:
914         kfree(br);
915 out:
916         device->brightness = NULL;
917         kfree(obj);
918         return result;
919 }
920
921 /*
922  *  Arg:
923  *      device  : video output device (LCD, CRT, ..)
924  *
925  *  Return Value:
926  *      None
927  *
928  *  Find out all required AML methods defined under the output
929  *  device.
930  */
931
932 static void acpi_video_device_find_cap(struct acpi_video_device *device)
933 {
934         acpi_handle h_dummy1;
935
936
937         memset(&device->cap, 0, sizeof(device->cap));
938
939         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
940                 device->cap._ADR = 1;
941         }
942         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
943                 device->cap._BCL = 1;
944         }
945         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
946                 device->cap._BCM = 1;
947         }
948         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
949                 device->cap._BQC = 1;
950         else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
951                                 &h_dummy1))) {
952                 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
953                 device->cap._BCQ = 1;
954         }
955
956         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
957                 device->cap._DDC = 1;
958         }
959         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
960                 device->cap._DCS = 1;
961         }
962         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
963                 device->cap._DGS = 1;
964         }
965         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
966                 device->cap._DSS = 1;
967         }
968
969         if (acpi_video_backlight_support()) {
970                 int result;
971                 static int count = 0;
972                 char *name;
973
974                 result = acpi_video_init_brightness(device);
975                 if (result)
976                         return;
977                 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
978                 if (!name)
979                         return;
980
981                 sprintf(name, "acpi_video%d", count++);
982                 device->backlight = backlight_device_register(name,
983                         NULL, device, &acpi_backlight_ops);
984                 device->backlight->props.max_brightness = device->brightness->count-3;
985                 kfree(name);
986
987                 device->cdev = thermal_cooling_device_register("LCD",
988                                         device->dev, &video_cooling_ops);
989                 if (IS_ERR(device->cdev))
990                         return;
991
992                 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
993                          device->cdev->id);
994                 result = sysfs_create_link(&device->dev->dev.kobj,
995                                 &device->cdev->device.kobj,
996                                 "thermal_cooling");
997                 if (result)
998                         printk(KERN_ERR PREFIX "Create sysfs link\n");
999                 result = sysfs_create_link(&device->cdev->device.kobj,
1000                                 &device->dev->dev.kobj, "device");
1001                 if (result)
1002                         printk(KERN_ERR PREFIX "Create sysfs link\n");
1003
1004         }
1005
1006         if (acpi_video_display_switch_support()) {
1007
1008                 if (device->cap._DCS && device->cap._DSS) {
1009                         static int count;
1010                         char *name;
1011                         name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1012                         if (!name)
1013                                 return;
1014                         sprintf(name, "acpi_video%d", count++);
1015                         device->output_dev = video_output_register(name,
1016                                         NULL, device, &acpi_output_properties);
1017                         kfree(name);
1018                 }
1019         }
1020 }
1021
1022 /*
1023  *  Arg:        
1024  *      device  : video output device (VGA)
1025  *
1026  *  Return Value:
1027  *      None
1028  *
1029  *  Find out all required AML methods defined under the video bus device.
1030  */
1031
1032 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1033 {
1034         acpi_handle h_dummy1;
1035
1036         memset(&video->cap, 0, sizeof(video->cap));
1037         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1038                 video->cap._DOS = 1;
1039         }
1040         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1041                 video->cap._DOD = 1;
1042         }
1043         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1044                 video->cap._ROM = 1;
1045         }
1046         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1047                 video->cap._GPD = 1;
1048         }
1049         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1050                 video->cap._SPD = 1;
1051         }
1052         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1053                 video->cap._VPO = 1;
1054         }
1055 }
1056
1057 /*
1058  * Check whether the video bus device has required AML method to
1059  * support the desired features
1060  */
1061
1062 static int acpi_video_bus_check(struct acpi_video_bus *video)
1063 {
1064         acpi_status status = -ENOENT;
1065         struct device *dev;
1066
1067         if (!video)
1068                 return -EINVAL;
1069
1070         dev = acpi_get_physical_pci_device(video->device->handle);
1071         if (!dev)
1072                 return -ENODEV;
1073         put_device(dev);
1074
1075         /* Since there is no HID, CID and so on for VGA driver, we have
1076          * to check well known required nodes.
1077          */
1078
1079         /* Does this device support video switching? */
1080         if (video->cap._DOS) {
1081                 video->flags.multihead = 1;
1082                 status = 0;
1083         }
1084
1085         /* Does this device support retrieving a video ROM? */
1086         if (video->cap._ROM) {
1087                 video->flags.rom = 1;
1088                 status = 0;
1089         }
1090
1091         /* Does this device support configuring which video device to POST? */
1092         if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1093                 video->flags.post = 1;
1094                 status = 0;
1095         }
1096
1097         return status;
1098 }
1099
1100 /* --------------------------------------------------------------------------
1101                               FS Interface (/proc)
1102    -------------------------------------------------------------------------- */
1103
1104 static struct proc_dir_entry *acpi_video_dir;
1105
1106 /* video devices */
1107
1108 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1109 {
1110         struct acpi_video_device *dev = seq->private;
1111
1112
1113         if (!dev)
1114                 goto end;
1115
1116         seq_printf(seq, "device_id:    0x%04x\n", (u32) dev->device_id);
1117         seq_printf(seq, "type:         ");
1118         if (dev->flags.crt)
1119                 seq_printf(seq, "CRT\n");
1120         else if (dev->flags.lcd)
1121                 seq_printf(seq, "LCD\n");
1122         else if (dev->flags.tvout)
1123                 seq_printf(seq, "TVOUT\n");
1124         else if (dev->flags.dvi)
1125                 seq_printf(seq, "DVI\n");
1126         else
1127                 seq_printf(seq, "UNKNOWN\n");
1128
1129         seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1130
1131       end:
1132         return 0;
1133 }
1134
1135 static int
1136 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1137 {
1138         return single_open(file, acpi_video_device_info_seq_show,
1139                            PDE(inode)->data);
1140 }
1141
1142 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1143 {
1144         int status;
1145         struct acpi_video_device *dev = seq->private;
1146         unsigned long long state;
1147
1148
1149         if (!dev)
1150                 goto end;
1151
1152         status = acpi_video_device_get_state(dev, &state);
1153         seq_printf(seq, "state:     ");
1154         if (ACPI_SUCCESS(status))
1155                 seq_printf(seq, "0x%02llx\n", state);
1156         else
1157                 seq_printf(seq, "<not supported>\n");
1158
1159         status = acpi_video_device_query(dev, &state);
1160         seq_printf(seq, "query:     ");
1161         if (ACPI_SUCCESS(status))
1162                 seq_printf(seq, "0x%02llx\n", state);
1163         else
1164                 seq_printf(seq, "<not supported>\n");
1165
1166       end:
1167         return 0;
1168 }
1169
1170 static int
1171 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1172 {
1173         return single_open(file, acpi_video_device_state_seq_show,
1174                            PDE(inode)->data);
1175 }
1176
1177 static ssize_t
1178 acpi_video_device_write_state(struct file *file,
1179                               const char __user * buffer,
1180                               size_t count, loff_t * data)
1181 {
1182         int status;
1183         struct seq_file *m = file->private_data;
1184         struct acpi_video_device *dev = m->private;
1185         char str[12] = { 0 };
1186         u32 state = 0;
1187
1188
1189         if (!dev || count + 1 > sizeof str)
1190                 return -EINVAL;
1191
1192         if (copy_from_user(str, buffer, count))
1193                 return -EFAULT;
1194
1195         str[count] = 0;
1196         state = simple_strtoul(str, NULL, 0);
1197         state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1198
1199         status = acpi_video_device_set_state(dev, state);
1200
1201         if (status)
1202                 return -EFAULT;
1203
1204         return count;
1205 }
1206
1207 static int
1208 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1209 {
1210         struct acpi_video_device *dev = seq->private;
1211         int i;
1212
1213
1214         if (!dev || !dev->brightness) {
1215                 seq_printf(seq, "<not supported>\n");
1216                 return 0;
1217         }
1218
1219         seq_printf(seq, "levels: ");
1220         for (i = 2; i < dev->brightness->count; i++)
1221                 seq_printf(seq, " %d", dev->brightness->levels[i]);
1222         seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1223
1224         return 0;
1225 }
1226
1227 static int
1228 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1229 {
1230         return single_open(file, acpi_video_device_brightness_seq_show,
1231                            PDE(inode)->data);
1232 }
1233
1234 static ssize_t
1235 acpi_video_device_write_brightness(struct file *file,
1236                                    const char __user * buffer,
1237                                    size_t count, loff_t * data)
1238 {
1239         struct seq_file *m = file->private_data;
1240         struct acpi_video_device *dev = m->private;
1241         char str[5] = { 0 };
1242         unsigned int level = 0;
1243         int i;
1244
1245
1246         if (!dev || !dev->brightness || count + 1 > sizeof str)
1247                 return -EINVAL;
1248
1249         if (copy_from_user(str, buffer, count))
1250                 return -EFAULT;
1251
1252         str[count] = 0;
1253         level = simple_strtoul(str, NULL, 0);
1254
1255         if (level > 100)
1256                 return -EFAULT;
1257
1258         /* validate through the list of available levels */
1259         for (i = 2; i < dev->brightness->count; i++)
1260                 if (level == dev->brightness->levels[i]) {
1261                         if (!acpi_video_device_lcd_set_level(dev, level))
1262                                 return count;
1263                         break;
1264                 }
1265
1266         return -EINVAL;
1267 }
1268
1269 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1270 {
1271         struct acpi_video_device *dev = seq->private;
1272         int status;
1273         int i;
1274         union acpi_object *edid = NULL;
1275
1276
1277         if (!dev)
1278                 goto out;
1279
1280         status = acpi_video_device_EDID(dev, &edid, 128);
1281         if (ACPI_FAILURE(status)) {
1282                 status = acpi_video_device_EDID(dev, &edid, 256);
1283         }
1284
1285         if (ACPI_FAILURE(status)) {
1286                 goto out;
1287         }
1288
1289         if (edid && edid->type == ACPI_TYPE_BUFFER) {
1290                 for (i = 0; i < edid->buffer.length; i++)
1291                         seq_putc(seq, edid->buffer.pointer[i]);
1292         }
1293
1294       out:
1295         if (!edid)
1296                 seq_printf(seq, "<not supported>\n");
1297         else
1298                 kfree(edid);
1299
1300         return 0;
1301 }
1302
1303 static int
1304 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1305 {
1306         return single_open(file, acpi_video_device_EDID_seq_show,
1307                            PDE(inode)->data);
1308 }
1309
1310 static int acpi_video_device_add_fs(struct acpi_device *device)
1311 {
1312         struct proc_dir_entry *entry, *device_dir;
1313         struct acpi_video_device *vid_dev;
1314
1315         vid_dev = acpi_driver_data(device);
1316         if (!vid_dev)
1317                 return -ENODEV;
1318
1319         device_dir = proc_mkdir(acpi_device_bid(device),
1320                                 vid_dev->video->dir);
1321         if (!device_dir)
1322                 return -ENOMEM;
1323
1324         /* 'info' [R] */
1325         entry = proc_create_data("info", S_IRUGO, device_dir,
1326                         &acpi_video_device_info_fops, acpi_driver_data(device));
1327         if (!entry)
1328                 goto err_remove_dir;
1329
1330         /* 'state' [R/W] */
1331         entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1332                                  device_dir,
1333                                  &acpi_video_device_state_fops,
1334                                  acpi_driver_data(device));
1335         if (!entry)
1336                 goto err_remove_info;
1337
1338         /* 'brightness' [R/W] */
1339         entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1340                                  device_dir,
1341                                  &acpi_video_device_brightness_fops,
1342                                  acpi_driver_data(device));
1343         if (!entry)
1344                 goto err_remove_state;
1345
1346         /* 'EDID' [R] */
1347         entry = proc_create_data("EDID", S_IRUGO, device_dir,
1348                                  &acpi_video_device_EDID_fops,
1349                                  acpi_driver_data(device));
1350         if (!entry)
1351                 goto err_remove_brightness;
1352
1353         acpi_device_dir(device) = device_dir;
1354
1355         return 0;
1356
1357  err_remove_brightness:
1358         remove_proc_entry("brightness", device_dir);
1359  err_remove_state:
1360         remove_proc_entry("state", device_dir);
1361  err_remove_info:
1362         remove_proc_entry("info", device_dir);
1363  err_remove_dir:
1364         remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1365         return -ENOMEM;
1366 }
1367
1368 static int acpi_video_device_remove_fs(struct acpi_device *device)
1369 {
1370         struct acpi_video_device *vid_dev;
1371         struct proc_dir_entry *device_dir;
1372
1373         vid_dev = acpi_driver_data(device);
1374         if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1375                 return -ENODEV;
1376
1377         device_dir = acpi_device_dir(device);
1378         if (device_dir) {
1379                 remove_proc_entry("info", device_dir);
1380                 remove_proc_entry("state", device_dir);
1381                 remove_proc_entry("brightness", device_dir);
1382                 remove_proc_entry("EDID", device_dir);
1383                 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1384                 acpi_device_dir(device) = NULL;
1385         }
1386
1387         return 0;
1388 }
1389
1390 /* video bus */
1391 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1392 {
1393         struct acpi_video_bus *video = seq->private;
1394
1395
1396         if (!video)
1397                 goto end;
1398
1399         seq_printf(seq, "Switching heads:              %s\n",
1400                    video->flags.multihead ? "yes" : "no");
1401         seq_printf(seq, "Video ROM:                    %s\n",
1402                    video->flags.rom ? "yes" : "no");
1403         seq_printf(seq, "Device to be POSTed on boot:  %s\n",
1404                    video->flags.post ? "yes" : "no");
1405
1406       end:
1407         return 0;
1408 }
1409
1410 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1411 {
1412         return single_open(file, acpi_video_bus_info_seq_show,
1413                            PDE(inode)->data);
1414 }
1415
1416 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1417 {
1418         struct acpi_video_bus *video = seq->private;
1419
1420
1421         if (!video)
1422                 goto end;
1423
1424         printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1425         seq_printf(seq, "<TODO>\n");
1426
1427       end:
1428         return 0;
1429 }
1430
1431 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1432 {
1433         return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1434 }
1435
1436 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1437 {
1438         struct acpi_video_bus *video = seq->private;
1439         unsigned long long options;
1440         int status;
1441
1442
1443         if (!video)
1444                 goto end;
1445
1446         status = acpi_video_bus_POST_options(video, &options);
1447         if (ACPI_SUCCESS(status)) {
1448                 if (!(options & 1)) {
1449                         printk(KERN_WARNING PREFIX
1450                                "The motherboard VGA device is not listed as a possible POST device.\n");
1451                         printk(KERN_WARNING PREFIX
1452                                "This indicates a BIOS bug. Please contact the manufacturer.\n");
1453                 }
1454                 printk(KERN_WARNING "%llx\n", options);
1455                 seq_printf(seq, "can POST: <integrated video>");
1456                 if (options & 2)
1457                         seq_printf(seq, " <PCI video>");
1458                 if (options & 4)
1459                         seq_printf(seq, " <AGP video>");
1460                 seq_putc(seq, '\n');
1461         } else
1462                 seq_printf(seq, "<not supported>\n");
1463       end:
1464         return 0;
1465 }
1466
1467 static int
1468 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1469 {
1470         return single_open(file, acpi_video_bus_POST_info_seq_show,
1471                            PDE(inode)->data);
1472 }
1473
1474 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1475 {
1476         struct acpi_video_bus *video = seq->private;
1477         int status;
1478         unsigned long long id;
1479
1480
1481         if (!video)
1482                 goto end;
1483
1484         status = acpi_video_bus_get_POST(video, &id);
1485         if (!ACPI_SUCCESS(status)) {
1486                 seq_printf(seq, "<not supported>\n");
1487                 goto end;
1488         }
1489         seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1490
1491       end:
1492         return 0;
1493 }
1494
1495 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1496 {
1497         struct acpi_video_bus *video = seq->private;
1498
1499
1500         seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1501
1502         return 0;
1503 }
1504
1505 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1506 {
1507         return single_open(file, acpi_video_bus_POST_seq_show,
1508                            PDE(inode)->data);
1509 }
1510
1511 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1512 {
1513         return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1514 }
1515
1516 static ssize_t
1517 acpi_video_bus_write_POST(struct file *file,
1518                           const char __user * buffer,
1519                           size_t count, loff_t * data)
1520 {
1521         int status;
1522         struct seq_file *m = file->private_data;
1523         struct acpi_video_bus *video = m->private;
1524         char str[12] = { 0 };
1525         unsigned long long opt, options;
1526
1527
1528         if (!video || count + 1 > sizeof str)
1529                 return -EINVAL;
1530
1531         status = acpi_video_bus_POST_options(video, &options);
1532         if (!ACPI_SUCCESS(status))
1533                 return -EINVAL;
1534
1535         if (copy_from_user(str, buffer, count))
1536                 return -EFAULT;
1537
1538         str[count] = 0;
1539         opt = strtoul(str, NULL, 0);
1540         if (opt > 3)
1541                 return -EFAULT;
1542
1543         /* just in case an OEM 'forgot' the motherboard... */
1544         options |= 1;
1545
1546         if (options & (1ul << opt)) {
1547                 status = acpi_video_bus_set_POST(video, opt);
1548                 if (!ACPI_SUCCESS(status))
1549                         return -EFAULT;
1550
1551         }
1552
1553         return count;
1554 }
1555
1556 static ssize_t
1557 acpi_video_bus_write_DOS(struct file *file,
1558                          const char __user * buffer,
1559                          size_t count, loff_t * data)
1560 {
1561         int status;
1562         struct seq_file *m = file->private_data;
1563         struct acpi_video_bus *video = m->private;
1564         char str[12] = { 0 };
1565         unsigned long opt;
1566
1567
1568         if (!video || count + 1 > sizeof str)
1569                 return -EINVAL;
1570
1571         if (copy_from_user(str, buffer, count))
1572                 return -EFAULT;
1573
1574         str[count] = 0;
1575         opt = strtoul(str, NULL, 0);
1576         if (opt > 7)
1577                 return -EFAULT;
1578
1579         status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1580
1581         if (!ACPI_SUCCESS(status))
1582                 return -EFAULT;
1583
1584         return count;
1585 }
1586
1587 static int acpi_video_bus_add_fs(struct acpi_device *device)
1588 {
1589         struct acpi_video_bus *video = acpi_driver_data(device);
1590         struct proc_dir_entry *device_dir;
1591         struct proc_dir_entry *entry;
1592
1593         device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1594         if (!device_dir)
1595                 return -ENOMEM;
1596
1597         /* 'info' [R] */
1598         entry = proc_create_data("info", S_IRUGO, device_dir,
1599                                  &acpi_video_bus_info_fops,
1600                                  acpi_driver_data(device));
1601         if (!entry)
1602                 goto err_remove_dir;
1603
1604         /* 'ROM' [R] */
1605         entry = proc_create_data("ROM", S_IRUGO, device_dir,
1606                                  &acpi_video_bus_ROM_fops,
1607                                  acpi_driver_data(device));
1608         if (!entry)
1609                 goto err_remove_info;
1610
1611         /* 'POST_info' [R] */
1612         entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1613                                  &acpi_video_bus_POST_info_fops,
1614                                  acpi_driver_data(device));
1615         if (!entry)
1616                 goto err_remove_rom;
1617
1618         /* 'POST' [R/W] */
1619         entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1620                                  device_dir,
1621                                  &acpi_video_bus_POST_fops,
1622                                  acpi_driver_data(device));
1623         if (!entry)
1624                 goto err_remove_post_info;
1625
1626         /* 'DOS' [R/W] */
1627         entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1628                                  device_dir,
1629                                  &acpi_video_bus_DOS_fops,
1630                                  acpi_driver_data(device));
1631         if (!entry)
1632                 goto err_remove_post;
1633
1634         video->dir = acpi_device_dir(device) = device_dir;
1635         return 0;
1636
1637  err_remove_post:
1638         remove_proc_entry("POST", device_dir);
1639  err_remove_post_info:
1640         remove_proc_entry("POST_info", device_dir);
1641  err_remove_rom:
1642         remove_proc_entry("ROM", device_dir);
1643  err_remove_info:
1644         remove_proc_entry("info", device_dir);
1645  err_remove_dir:
1646         remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1647         return -ENOMEM;
1648 }
1649
1650 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1651 {
1652         struct proc_dir_entry *device_dir = acpi_device_dir(device);
1653
1654         if (device_dir) {
1655                 remove_proc_entry("info", device_dir);
1656                 remove_proc_entry("ROM", device_dir);
1657                 remove_proc_entry("POST_info", device_dir);
1658                 remove_proc_entry("POST", device_dir);
1659                 remove_proc_entry("DOS", device_dir);
1660                 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1661                 acpi_device_dir(device) = NULL;
1662         }
1663
1664         return 0;
1665 }
1666
1667 /* --------------------------------------------------------------------------
1668                                  Driver Interface
1669    -------------------------------------------------------------------------- */
1670
1671 /* device interface */
1672 static struct acpi_video_device_attrib*
1673 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1674 {
1675         struct acpi_video_enumerated_device *ids;
1676         int i;
1677
1678         for (i = 0; i < video->attached_count; i++) {
1679                 ids = &video->attached_array[i];
1680                 if ((ids->value.int_val & 0xffff) == device_id)
1681                         return &ids->value.attrib;
1682         }
1683
1684         return NULL;
1685 }
1686
1687 static int
1688 acpi_video_bus_get_one_device(struct acpi_device *device,
1689                               struct acpi_video_bus *video)
1690 {
1691         unsigned long long device_id;
1692         int status;
1693         struct acpi_video_device *data;
1694         struct acpi_video_device_attrib* attribute;
1695
1696         if (!device || !video)
1697                 return -EINVAL;
1698
1699         status =
1700             acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1701         if (ACPI_SUCCESS(status)) {
1702
1703                 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1704                 if (!data)
1705                         return -ENOMEM;
1706
1707                 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1708                 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1709                 device->driver_data = data;
1710
1711                 data->device_id = device_id;
1712                 data->video = video;
1713                 data->dev = device;
1714
1715                 attribute = acpi_video_get_device_attr(video, device_id);
1716
1717                 if((attribute != NULL) && attribute->device_id_scheme) {
1718                         switch (attribute->display_type) {
1719                         case ACPI_VIDEO_DISPLAY_CRT:
1720                                 data->flags.crt = 1;
1721                                 break;
1722                         case ACPI_VIDEO_DISPLAY_TV:
1723                                 data->flags.tvout = 1;
1724                                 break;
1725                         case ACPI_VIDEO_DISPLAY_DVI:
1726                                 data->flags.dvi = 1;
1727                                 break;
1728                         case ACPI_VIDEO_DISPLAY_LCD:
1729                                 data->flags.lcd = 1;
1730                                 break;
1731                         default:
1732                                 data->flags.unknown = 1;
1733                                 break;
1734                         }
1735                         if(attribute->bios_can_detect)
1736                                 data->flags.bios = 1;
1737                 } else
1738                         data->flags.unknown = 1;
1739
1740                 acpi_video_device_bind(video, data);
1741                 acpi_video_device_find_cap(data);
1742
1743                 status = acpi_install_notify_handler(device->handle,
1744                                                      ACPI_DEVICE_NOTIFY,
1745                                                      acpi_video_device_notify,
1746                                                      data);
1747                 if (ACPI_FAILURE(status)) {
1748                         printk(KERN_ERR PREFIX
1749                                           "Error installing notify handler\n");
1750                         if(data->brightness)
1751                                 kfree(data->brightness->levels);
1752                         kfree(data->brightness);
1753                         kfree(data);
1754                         return -ENODEV;
1755                 }
1756
1757                 mutex_lock(&video->device_list_lock);
1758                 list_add_tail(&data->entry, &video->video_device_list);
1759                 mutex_unlock(&video->device_list_lock);
1760
1761                 acpi_video_device_add_fs(device);
1762
1763                 return 0;
1764         }
1765
1766         return -ENOENT;
1767 }
1768
1769 /*
1770  *  Arg:
1771  *      video   : video bus device 
1772  *
1773  *  Return:
1774  *      none
1775  *  
1776  *  Enumerate the video device list of the video bus, 
1777  *  bind the ids with the corresponding video devices
1778  *  under the video bus.
1779  */
1780
1781 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1782 {
1783         struct acpi_video_device *dev;
1784
1785         mutex_lock(&video->device_list_lock);
1786
1787         list_for_each_entry(dev, &video->video_device_list, entry)
1788                 acpi_video_device_bind(video, dev);
1789
1790         mutex_unlock(&video->device_list_lock);
1791 }
1792
1793 /*
1794  *  Arg:
1795  *      video   : video bus device 
1796  *      device  : video output device under the video 
1797  *              bus
1798  *
1799  *  Return:
1800  *      none
1801  *  
1802  *  Bind the ids with the corresponding video devices
1803  *  under the video bus.
1804  */
1805
1806 static void
1807 acpi_video_device_bind(struct acpi_video_bus *video,
1808                        struct acpi_video_device *device)
1809 {
1810         struct acpi_video_enumerated_device *ids;
1811         int i;
1812
1813         for (i = 0; i < video->attached_count; i++) {
1814                 ids = &video->attached_array[i];
1815                 if (device->device_id == (ids->value.int_val & 0xffff)) {
1816                         ids->bind_info = device;
1817                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1818                 }
1819         }
1820 }
1821
1822 /*
1823  *  Arg:
1824  *      video   : video bus device 
1825  *
1826  *  Return:
1827  *      < 0     : error
1828  *  
1829  *  Call _DOD to enumerate all devices attached to display adapter
1830  *
1831  */
1832
1833 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1834 {
1835         int status;
1836         int count;
1837         int i;
1838         struct acpi_video_enumerated_device *active_list;
1839         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1840         union acpi_object *dod = NULL;
1841         union acpi_object *obj;
1842
1843         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1844         if (!ACPI_SUCCESS(status)) {
1845                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1846                 return status;
1847         }
1848
1849         dod = buffer.pointer;
1850         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1851                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1852                 status = -EFAULT;
1853                 goto out;
1854         }
1855
1856         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1857                           dod->package.count));
1858
1859         active_list = kcalloc(1 + dod->package.count,
1860                               sizeof(struct acpi_video_enumerated_device),
1861                               GFP_KERNEL);
1862         if (!active_list) {
1863                 status = -ENOMEM;
1864                 goto out;
1865         }
1866
1867         count = 0;
1868         for (i = 0; i < dod->package.count; i++) {
1869                 obj = &dod->package.elements[i];
1870
1871                 if (obj->type != ACPI_TYPE_INTEGER) {
1872                         printk(KERN_ERR PREFIX
1873                                 "Invalid _DOD data in element %d\n", i);
1874                         continue;
1875                 }
1876
1877                 active_list[count].value.int_val = obj->integer.value;
1878                 active_list[count].bind_info = NULL;
1879                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1880                                   (int)obj->integer.value));
1881                 count++;
1882         }
1883
1884         kfree(video->attached_array);
1885
1886         video->attached_array = active_list;
1887         video->attached_count = count;
1888
1889  out:
1890         kfree(buffer.pointer);
1891         return status;
1892 }
1893
1894 static int
1895 acpi_video_get_next_level(struct acpi_video_device *device,
1896                           u32 level_current, u32 event)
1897 {
1898         int min, max, min_above, max_below, i, l, delta = 255;
1899         max = max_below = 0;
1900         min = min_above = 255;
1901         /* Find closest level to level_current */
1902         for (i = 2; i < device->brightness->count; i++) {
1903                 l = device->brightness->levels[i];
1904                 if (abs(l - level_current) < abs(delta)) {
1905                         delta = l - level_current;
1906                         if (!delta)
1907                                 break;
1908                 }
1909         }
1910         /* Ajust level_current to closest available level */
1911         level_current += delta;
1912         for (i = 2; i < device->brightness->count; i++) {
1913                 l = device->brightness->levels[i];
1914                 if (l < min)
1915                         min = l;
1916                 if (l > max)
1917                         max = l;
1918                 if (l < min_above && l > level_current)
1919                         min_above = l;
1920                 if (l > max_below && l < level_current)
1921                         max_below = l;
1922         }
1923
1924         switch (event) {
1925         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1926                 return (level_current < max) ? min_above : min;
1927         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1928                 return (level_current < max) ? min_above : max;
1929         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1930                 return (level_current > min) ? max_below : min;
1931         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1932         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1933                 return 0;
1934         default:
1935                 return level_current;
1936         }
1937 }
1938
1939 static int
1940 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1941 {
1942         unsigned long long level_current, level_next;
1943         int result = -EINVAL;
1944
1945         if (!device->brightness)
1946                 goto out;
1947
1948         result = acpi_video_device_lcd_get_level_current(device,
1949                                                          &level_current);
1950         if (result)
1951                 goto out;
1952
1953         level_next = acpi_video_get_next_level(device, level_current, event);
1954
1955         result = acpi_video_device_lcd_set_level(device, level_next);
1956
1957 out:
1958         if (result)
1959                 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1960
1961         return result;
1962 }
1963
1964 static int
1965 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1966                            struct acpi_device *device)
1967 {
1968         int status = 0;
1969         struct acpi_device *dev;
1970
1971         acpi_video_device_enumerate(video);
1972
1973         list_for_each_entry(dev, &device->children, node) {
1974
1975                 status = acpi_video_bus_get_one_device(dev, video);
1976                 if (ACPI_FAILURE(status)) {
1977                         printk(KERN_WARNING PREFIX
1978                                         "Cant attach device");
1979                         continue;
1980                 }
1981         }
1982         return status;
1983 }
1984
1985 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1986 {
1987         acpi_status status;
1988         struct acpi_video_bus *video;
1989
1990
1991         if (!device || !device->video)
1992                 return -ENOENT;
1993
1994         video = device->video;
1995
1996         acpi_video_device_remove_fs(device->dev);
1997
1998         status = acpi_remove_notify_handler(device->dev->handle,
1999                                             ACPI_DEVICE_NOTIFY,
2000                                             acpi_video_device_notify);
2001         backlight_device_unregister(device->backlight);
2002         if (device->cdev) {
2003                 sysfs_remove_link(&device->dev->dev.kobj,
2004                                   "thermal_cooling");
2005                 sysfs_remove_link(&device->cdev->device.kobj,
2006                                   "device");
2007                 thermal_cooling_device_unregister(device->cdev);
2008                 device->cdev = NULL;
2009         }
2010         video_output_unregister(device->output_dev);
2011
2012         return 0;
2013 }
2014
2015 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
2016 {
2017         int status;
2018         struct acpi_video_device *dev, *next;
2019
2020         mutex_lock(&video->device_list_lock);
2021
2022         list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
2023
2024                 status = acpi_video_bus_put_one_device(dev);
2025                 if (ACPI_FAILURE(status))
2026                         printk(KERN_WARNING PREFIX
2027                                "hhuuhhuu bug in acpi video driver.\n");
2028
2029                 if (dev->brightness) {
2030                         kfree(dev->brightness->levels);
2031                         kfree(dev->brightness);
2032                 }
2033                 list_del(&dev->entry);
2034                 kfree(dev);
2035         }
2036
2037         mutex_unlock(&video->device_list_lock);
2038
2039         return 0;
2040 }
2041
2042 /* acpi_video interface */
2043
2044 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
2045 {
2046         return acpi_video_bus_DOS(video, 0, 0);
2047 }
2048
2049 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
2050 {
2051         return acpi_video_bus_DOS(video, 0, 1);
2052 }
2053
2054 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
2055 {
2056         struct acpi_video_bus *video = acpi_driver_data(device);
2057         struct input_dev *input;
2058         int keycode;
2059
2060         if (!video)
2061                 return;
2062
2063         input = video->input;
2064
2065         switch (event) {
2066         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User requested a switch,
2067                                          * most likely via hotkey. */
2068                 acpi_bus_generate_proc_event(device, event, 0);
2069                 keycode = KEY_SWITCHVIDEOMODE;
2070                 break;
2071
2072         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plugged in or removed a video
2073                                          * connector. */
2074                 acpi_video_device_enumerate(video);
2075                 acpi_video_device_rebind(video);
2076                 acpi_bus_generate_proc_event(device, event, 0);
2077                 keycode = KEY_SWITCHVIDEOMODE;
2078                 break;
2079
2080         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
2081                 acpi_bus_generate_proc_event(device, event, 0);
2082                 keycode = KEY_SWITCHVIDEOMODE;
2083                 break;
2084         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
2085                 acpi_bus_generate_proc_event(device, event, 0);
2086                 keycode = KEY_VIDEO_NEXT;
2087                 break;
2088         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
2089                 acpi_bus_generate_proc_event(device, event, 0);
2090                 keycode = KEY_VIDEO_PREV;
2091                 break;
2092
2093         default:
2094                 keycode = KEY_UNKNOWN;
2095                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2096                                   "Unsupported event [0x%x]\n", event));
2097                 break;
2098         }
2099
2100         acpi_notifier_call_chain(device, event, 0);
2101         input_report_key(input, keycode, 1);
2102         input_sync(input);
2103         input_report_key(input, keycode, 0);
2104         input_sync(input);
2105
2106         return;
2107 }
2108
2109 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
2110 {
2111         struct acpi_video_device *video_device = data;
2112         struct acpi_device *device = NULL;
2113         struct acpi_video_bus *bus;
2114         struct input_dev *input;
2115         int keycode;
2116
2117         if (!video_device)
2118                 return;
2119
2120         device = video_device->dev;
2121         bus = video_device->video;
2122         input = bus->input;
2123
2124         switch (event) {
2125         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
2126                 if (brightness_switch_enabled)
2127                         acpi_video_switch_brightness(video_device, event);
2128                 acpi_bus_generate_proc_event(device, event, 0);
2129                 keycode = KEY_BRIGHTNESS_CYCLE;
2130                 break;
2131         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
2132                 if (brightness_switch_enabled)
2133                         acpi_video_switch_brightness(video_device, event);
2134                 acpi_bus_generate_proc_event(device, event, 0);
2135                 keycode = KEY_BRIGHTNESSUP;
2136                 break;
2137         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
2138                 if (brightness_switch_enabled)
2139                         acpi_video_switch_brightness(video_device, event);
2140                 acpi_bus_generate_proc_event(device, event, 0);
2141                 keycode = KEY_BRIGHTNESSDOWN;
2142                 break;
2143         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
2144                 if (brightness_switch_enabled)
2145                         acpi_video_switch_brightness(video_device, event);
2146                 acpi_bus_generate_proc_event(device, event, 0);
2147                 keycode = KEY_BRIGHTNESS_ZERO;
2148                 break;
2149         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
2150                 if (brightness_switch_enabled)
2151                         acpi_video_switch_brightness(video_device, event);
2152                 acpi_bus_generate_proc_event(device, event, 0);
2153                 keycode = KEY_DISPLAY_OFF;
2154                 break;
2155         default:
2156                 keycode = KEY_UNKNOWN;
2157                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2158                                   "Unsupported event [0x%x]\n", event));
2159                 break;
2160         }
2161
2162         acpi_notifier_call_chain(device, event, 0);
2163         input_report_key(input, keycode, 1);
2164         input_sync(input);
2165         input_report_key(input, keycode, 0);
2166         input_sync(input);
2167
2168         return;
2169 }
2170
2171 static int instance;
2172 static int acpi_video_resume(struct acpi_device *device)
2173 {
2174         struct acpi_video_bus *video;
2175         struct acpi_video_device *video_device;
2176         int i;
2177
2178         if (!device || !acpi_driver_data(device))
2179                 return -EINVAL;
2180
2181         video = acpi_driver_data(device);
2182
2183         for (i = 0; i < video->attached_count; i++) {
2184                 video_device = video->attached_array[i].bind_info;
2185                 if (video_device && video_device->backlight)
2186                         acpi_video_set_brightness(video_device->backlight);
2187         }
2188         return AE_OK;
2189 }
2190
2191 static int acpi_video_bus_add(struct acpi_device *device)
2192 {
2193         struct acpi_video_bus *video;
2194         struct input_dev *input;
2195         int error;
2196
2197         video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2198         if (!video)
2199                 return -ENOMEM;
2200
2201         /* a hack to fix the duplicate name "VID" problem on T61 */
2202         if (!strcmp(device->pnp.bus_id, "VID")) {
2203                 if (instance)
2204                         device->pnp.bus_id[3] = '0' + instance;
2205                 instance ++;
2206         }
2207         /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2208         if (!strcmp(device->pnp.bus_id, "VGA")) {
2209                 if (instance)
2210                         device->pnp.bus_id[3] = '0' + instance;
2211                 instance++;
2212         }
2213
2214         video->device = device;
2215         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2216         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2217         device->driver_data = video;
2218
2219         acpi_video_bus_find_cap(video);
2220         error = acpi_video_bus_check(video);
2221         if (error)
2222                 goto err_free_video;
2223
2224         error = acpi_video_bus_add_fs(device);
2225         if (error)
2226                 goto err_free_video;
2227
2228         mutex_init(&video->device_list_lock);
2229         INIT_LIST_HEAD(&video->video_device_list);
2230
2231         acpi_video_bus_get_devices(video, device);
2232         acpi_video_bus_start_devices(video);
2233
2234         video->input = input = input_allocate_device();
2235         if (!input) {
2236                 error = -ENOMEM;
2237                 goto err_stop_video;
2238         }
2239
2240         snprintf(video->phys, sizeof(video->phys),
2241                 "%s/video/input0", acpi_device_hid(video->device));
2242
2243         input->name = acpi_device_name(video->device);
2244         input->phys = video->phys;
2245         input->id.bustype = BUS_HOST;
2246         input->id.product = 0x06;
2247         input->dev.parent = &device->dev;
2248         input->evbit[0] = BIT(EV_KEY);
2249         set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2250         set_bit(KEY_VIDEO_NEXT, input->keybit);
2251         set_bit(KEY_VIDEO_PREV, input->keybit);
2252         set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2253         set_bit(KEY_BRIGHTNESSUP, input->keybit);
2254         set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2255         set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2256         set_bit(KEY_DISPLAY_OFF, input->keybit);
2257         set_bit(KEY_UNKNOWN, input->keybit);
2258
2259         error = input_register_device(input);
2260         if (error)
2261                 goto err_free_input_dev;
2262
2263         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
2264                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2265                video->flags.multihead ? "yes" : "no",
2266                video->flags.rom ? "yes" : "no",
2267                video->flags.post ? "yes" : "no");
2268
2269         return 0;
2270
2271  err_free_input_dev:
2272         input_free_device(input);
2273  err_stop_video:
2274         acpi_video_bus_stop_devices(video);
2275         acpi_video_bus_put_devices(video);
2276         kfree(video->attached_array);
2277         acpi_video_bus_remove_fs(device);
2278  err_free_video:
2279         kfree(video);
2280         device->driver_data = NULL;
2281
2282         return error;
2283 }
2284
2285 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2286 {
2287         struct acpi_video_bus *video = NULL;
2288
2289
2290         if (!device || !acpi_driver_data(device))
2291                 return -EINVAL;
2292
2293         video = acpi_driver_data(device);
2294
2295         acpi_video_bus_stop_devices(video);
2296         acpi_video_bus_put_devices(video);
2297         acpi_video_bus_remove_fs(device);
2298
2299         input_unregister_device(video->input);
2300         kfree(video->attached_array);
2301         kfree(video);
2302
2303         return 0;
2304 }
2305
2306 static int __init intel_opregion_present(void)
2307 {
2308 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2309         struct pci_dev *dev = NULL;
2310         u32 address;
2311
2312         for_each_pci_dev(dev) {
2313                 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2314                         continue;
2315                 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2316                         continue;
2317                 pci_read_config_dword(dev, 0xfc, &address);
2318                 if (!address)
2319                         continue;
2320                 return 1;
2321         }
2322 #endif
2323         return 0;
2324 }
2325
2326 int acpi_video_register(void)
2327 {
2328         int result = 0;
2329
2330         acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2331         if (!acpi_video_dir)
2332                 return -ENODEV;
2333
2334         result = acpi_bus_register_driver(&acpi_video_bus);
2335         if (result < 0) {
2336                 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2337                 return -ENODEV;
2338         }
2339
2340         return 0;
2341 }
2342 EXPORT_SYMBOL(acpi_video_register);
2343
2344 /*
2345  * This is kind of nasty. Hardware using Intel chipsets may require
2346  * the video opregion code to be run first in order to initialise
2347  * state before any ACPI video calls are made. To handle this we defer
2348  * registration of the video class until the opregion code has run.
2349  */
2350
2351 static int __init acpi_video_init(void)
2352 {
2353         dmi_check_system(video_dmi_table);
2354
2355         if (intel_opregion_present())
2356                 return 0;
2357
2358         return acpi_video_register();
2359 }
2360
2361 void acpi_video_exit(void)
2362 {
2363
2364         acpi_bus_unregister_driver(&acpi_video_bus);
2365
2366         remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2367
2368         return;
2369 }
2370 EXPORT_SYMBOL(acpi_video_exit);
2371
2372 module_init(acpi_video_init);
2373 module_exit(acpi_video_exit);