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