Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[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/input.h>
34 #include <linux/backlight.h>
35 #include <linux/thermal.h>
36 #include <linux/video_output.h>
37 #include <linux/sort.h>
38 #include <linux/pci.h>
39 #include <linux/pci_ids.h>
40 #include <linux/slab.h>
41 #include <asm/uaccess.h>
42 #include <linux/dmi.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <linux/suspend.h>
46 #include <acpi/video.h>
47
48 #define PREFIX "ACPI: "
49
50 #define ACPI_VIDEO_CLASS                "video"
51 #define ACPI_VIDEO_BUS_NAME             "Video Bus"
52 #define ACPI_VIDEO_DEVICE_NAME          "Video Device"
53 #define ACPI_VIDEO_NOTIFY_SWITCH        0x80
54 #define ACPI_VIDEO_NOTIFY_PROBE         0x81
55 #define ACPI_VIDEO_NOTIFY_CYCLE         0x82
56 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT   0x83
57 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT   0x84
58
59 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS      0x85
60 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x86
61 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS        0x87
62 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS       0x88
63 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF           0x89
64
65 #define MAX_NAME_LEN    20
66
67 #define _COMPONENT              ACPI_VIDEO_COMPONENT
68 ACPI_MODULE_NAME("video");
69
70 MODULE_AUTHOR("Bruno Ducrot");
71 MODULE_DESCRIPTION("ACPI Video Driver");
72 MODULE_LICENSE("GPL");
73
74 static int brightness_switch_enabled = 1;
75 module_param(brightness_switch_enabled, bool, 0644);
76
77 /*
78  * By default, we don't allow duplicate ACPI video bus devices
79  * under the same VGA controller
80  */
81 static int allow_duplicates;
82 module_param(allow_duplicates, bool, 0644);
83
84 static int register_count = 0;
85 static int acpi_video_bus_add(struct acpi_device *device);
86 static int acpi_video_bus_remove(struct acpi_device *device, int type);
87 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
88
89 static const struct acpi_device_id video_device_ids[] = {
90         {ACPI_VIDEO_HID, 0},
91         {"", 0},
92 };
93 MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
95 static struct acpi_driver acpi_video_bus = {
96         .name = "video",
97         .class = ACPI_VIDEO_CLASS,
98         .ids = video_device_ids,
99         .ops = {
100                 .add = acpi_video_bus_add,
101                 .remove = acpi_video_bus_remove,
102                 .notify = acpi_video_bus_notify,
103                 },
104 };
105
106 struct acpi_video_bus_flags {
107         u8 multihead:1;         /* can switch video heads */
108         u8 rom:1;               /* can retrieve a video rom */
109         u8 post:1;              /* can configure the head to */
110         u8 reserved:5;
111 };
112
113 struct acpi_video_bus_cap {
114         u8 _DOS:1;              /*Enable/Disable output switching */
115         u8 _DOD:1;              /*Enumerate all devices attached to display adapter */
116         u8 _ROM:1;              /*Get ROM Data */
117         u8 _GPD:1;              /*Get POST Device */
118         u8 _SPD:1;              /*Set POST Device */
119         u8 _VPO:1;              /*Video POST Options */
120         u8 reserved:2;
121 };
122
123 struct acpi_video_device_attrib {
124         u32 display_index:4;    /* A zero-based instance of the Display */
125         u32 display_port_attachment:4;  /*This field differentiates the display type */
126         u32 display_type:4;     /*Describe the specific type in use */
127         u32 vendor_specific:4;  /*Chipset Vendor Specific */
128         u32 bios_can_detect:1;  /*BIOS can detect the device */
129         u32 depend_on_vga:1;    /*Non-VGA output device whose power is related to 
130                                    the VGA device. */
131         u32 pipe_id:3;          /*For VGA multiple-head devices. */
132         u32 reserved:10;        /*Must be 0 */
133         u32 device_id_scheme:1; /*Device ID Scheme */
134 };
135
136 struct acpi_video_enumerated_device {
137         union {
138                 u32 int_val;
139                 struct acpi_video_device_attrib attrib;
140         } value;
141         struct acpi_video_device *bind_info;
142 };
143
144 struct acpi_video_bus {
145         struct acpi_device *device;
146         u8 dos_setting;
147         struct acpi_video_enumerated_device *attached_array;
148         u8 attached_count;
149         struct acpi_video_bus_cap cap;
150         struct acpi_video_bus_flags flags;
151         struct list_head video_device_list;
152         struct mutex device_list_lock;  /* protects video_device_list */
153         struct input_dev *input;
154         char phys[32];  /* for input device */
155         struct notifier_block pm_nb;
156 };
157
158 struct acpi_video_device_flags {
159         u8 crt:1;
160         u8 lcd:1;
161         u8 tvout:1;
162         u8 dvi:1;
163         u8 bios:1;
164         u8 unknown:1;
165         u8 reserved:2;
166 };
167
168 struct acpi_video_device_cap {
169         u8 _ADR:1;              /*Return the unique ID */
170         u8 _BCL:1;              /*Query list of brightness control levels supported */
171         u8 _BCM:1;              /*Set the brightness level */
172         u8 _BQC:1;              /* Get current brightness level */
173         u8 _BCQ:1;              /* Some buggy BIOS uses _BCQ instead of _BQC */
174         u8 _DDC:1;              /*Return the EDID for this device */
175         u8 _DCS:1;              /*Return status of output device */
176         u8 _DGS:1;              /*Query graphics state */
177         u8 _DSS:1;              /*Device state set */
178 };
179
180 struct acpi_video_brightness_flags {
181         u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
182         u8 _BCL_reversed:1;             /* _BCL package is in a reversed order*/
183         u8 _BCL_use_index:1;            /* levels in _BCL are index values */
184         u8 _BCM_use_index:1;            /* input of _BCM is an index value */
185         u8 _BQC_use_index:1;            /* _BQC returns an index value */
186 };
187
188 struct acpi_video_device_brightness {
189         int curr;
190         int count;
191         int *levels;
192         struct acpi_video_brightness_flags flags;
193 };
194
195 struct acpi_video_device {
196         unsigned long device_id;
197         struct acpi_video_device_flags flags;
198         struct acpi_video_device_cap cap;
199         struct list_head entry;
200         struct acpi_video_bus *video;
201         struct acpi_device *dev;
202         struct acpi_video_device_brightness *brightness;
203         struct backlight_device *backlight;
204         struct thermal_cooling_device *cooling_dev;
205         struct output_device *output_dev;
206 };
207
208 static const char device_decode[][30] = {
209         "motherboard VGA device",
210         "PCI VGA device",
211         "AGP VGA device",
212         "UNKNOWN",
213 };
214
215 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216 static void acpi_video_device_rebind(struct acpi_video_bus *video);
217 static void acpi_video_device_bind(struct acpi_video_bus *video,
218                                    struct acpi_video_device *device);
219 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
220 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221                         int level);
222 static int acpi_video_device_lcd_get_level_current(
223                         struct acpi_video_device *device,
224                         unsigned long long *level, int init);
225 static int acpi_video_get_next_level(struct acpi_video_device *device,
226                                      u32 level_current, u32 event);
227 static int acpi_video_switch_brightness(struct acpi_video_device *device,
228                                          int event);
229 static int acpi_video_device_get_state(struct acpi_video_device *device,
230                             unsigned long long *state);
231 static int acpi_video_output_get(struct output_device *od);
232 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
233
234 /*backlight device sysfs support*/
235 static int acpi_video_get_brightness(struct backlight_device *bd)
236 {
237         unsigned long long cur_level;
238         int i;
239         struct acpi_video_device *vd =
240                 (struct acpi_video_device *)bl_get_data(bd);
241
242         if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
243                 return -EINVAL;
244         for (i = 2; i < vd->brightness->count; i++) {
245                 if (vd->brightness->levels[i] == cur_level)
246                         /* The first two entries are special - see page 575
247                            of the ACPI spec 3.0 */
248                         return i-2;
249         }
250         return 0;
251 }
252
253 static int acpi_video_set_brightness(struct backlight_device *bd)
254 {
255         int request_level = bd->props.brightness + 2;
256         struct acpi_video_device *vd =
257                 (struct acpi_video_device *)bl_get_data(bd);
258
259         return acpi_video_device_lcd_set_level(vd,
260                                 vd->brightness->levels[request_level]);
261 }
262
263 static struct backlight_ops acpi_backlight_ops = {
264         .get_brightness = acpi_video_get_brightness,
265         .update_status  = acpi_video_set_brightness,
266 };
267
268 /*video output device sysfs support*/
269 static int acpi_video_output_get(struct output_device *od)
270 {
271         unsigned long long state;
272         struct acpi_video_device *vd =
273                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
274         acpi_video_device_get_state(vd, &state);
275         return (int)state;
276 }
277
278 static int acpi_video_output_set(struct output_device *od)
279 {
280         unsigned long state = od->request_state;
281         struct acpi_video_device *vd=
282                 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
283         return acpi_video_device_set_state(vd, state);
284 }
285
286 static struct output_properties acpi_output_properties = {
287         .set_state = acpi_video_output_set,
288         .get_status = acpi_video_output_get,
289 };
290
291
292 /* thermal cooling device callbacks */
293 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
294                                long *state)
295 {
296         struct acpi_device *device = cooling_dev->devdata;
297         struct acpi_video_device *video = acpi_driver_data(device);
298
299         *state = video->brightness->count - 3;
300         return 0;
301 }
302
303 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
304                                long *state)
305 {
306         struct acpi_device *device = cooling_dev->devdata;
307         struct acpi_video_device *video = acpi_driver_data(device);
308         unsigned long long level;
309         int offset;
310
311         if (acpi_video_device_lcd_get_level_current(video, &level, 0))
312                 return -EINVAL;
313         for (offset = 2; offset < video->brightness->count; offset++)
314                 if (level == video->brightness->levels[offset]) {
315                         *state = video->brightness->count - offset - 1;
316                         return 0;
317                 }
318
319         return -EINVAL;
320 }
321
322 static int
323 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
324 {
325         struct acpi_device *device = cooling_dev->devdata;
326         struct acpi_video_device *video = acpi_driver_data(device);
327         int level;
328
329         if ( state >= video->brightness->count - 2)
330                 return -EINVAL;
331
332         state = video->brightness->count - state;
333         level = video->brightness->levels[state -1];
334         return acpi_video_device_lcd_set_level(video, level);
335 }
336
337 static struct thermal_cooling_device_ops video_cooling_ops = {
338         .get_max_state = video_get_max_state,
339         .get_cur_state = video_get_cur_state,
340         .set_cur_state = video_set_cur_state,
341 };
342
343 /* --------------------------------------------------------------------------
344                                Video Management
345    -------------------------------------------------------------------------- */
346
347 /* device */
348
349 static int
350 acpi_video_device_get_state(struct acpi_video_device *device,
351                             unsigned long long *state)
352 {
353         int status;
354
355         status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
356
357         return status;
358 }
359
360 static int
361 acpi_video_device_set_state(struct acpi_video_device *device, int state)
362 {
363         int status;
364         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
365         struct acpi_object_list args = { 1, &arg0 };
366         unsigned long long ret;
367
368
369         arg0.integer.value = state;
370         status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
371
372         return status;
373 }
374
375 static int
376 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
377                                    union acpi_object **levels)
378 {
379         int status;
380         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
381         union acpi_object *obj;
382
383
384         *levels = NULL;
385
386         status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
387         if (!ACPI_SUCCESS(status))
388                 return status;
389         obj = (union acpi_object *)buffer.pointer;
390         if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
391                 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
392                 status = -EFAULT;
393                 goto err;
394         }
395
396         *levels = obj;
397
398         return 0;
399
400       err:
401         kfree(buffer.pointer);
402
403         return status;
404 }
405
406 static int
407 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
408 {
409         int status;
410         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
411         struct acpi_object_list args = { 1, &arg0 };
412         int state;
413
414         arg0.integer.value = level;
415
416         status = acpi_evaluate_object(device->dev->handle, "_BCM",
417                                       &args, NULL);
418         if (ACPI_FAILURE(status)) {
419                 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
420                 return -EIO;
421         }
422
423         device->brightness->curr = level;
424         for (state = 2; state < device->brightness->count; state++)
425                 if (level == device->brightness->levels[state]) {
426                         if (device->backlight)
427                                 device->backlight->props.brightness = state - 2;
428                         return 0;
429                 }
430
431         ACPI_ERROR((AE_INFO, "Current brightness invalid"));
432         return -EINVAL;
433 }
434
435 /*
436  * For some buggy _BQC methods, we need to add a constant value to
437  * the _BQC return value to get the actual current brightness level
438  */
439
440 static int bqc_offset_aml_bug_workaround;
441 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
442 {
443         bqc_offset_aml_bug_workaround = 9;
444         return 0;
445 }
446
447 static struct dmi_system_id video_dmi_table[] __initdata = {
448         /*
449          * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
450          */
451         {
452          .callback = video_set_bqc_offset,
453          .ident = "Acer Aspire 5720",
454          .matches = {
455                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
456                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
457                 },
458         },
459         {
460          .callback = video_set_bqc_offset,
461          .ident = "Acer Aspire 5710Z",
462          .matches = {
463                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
464                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
465                 },
466         },
467         {
468          .callback = video_set_bqc_offset,
469          .ident = "eMachines E510",
470          .matches = {
471                 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
472                 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
473                 },
474         },
475         {
476          .callback = video_set_bqc_offset,
477          .ident = "Acer Aspire 5315",
478          .matches = {
479                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
480                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
481                 },
482         },
483         {
484          .callback = video_set_bqc_offset,
485          .ident = "Acer Aspire 7720",
486          .matches = {
487                 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
488                 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
489                 },
490         },
491         {}
492 };
493
494 static int
495 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
496                                         unsigned long long *level, int init)
497 {
498         acpi_status status = AE_OK;
499         int i;
500
501         if (device->cap._BQC || device->cap._BCQ) {
502                 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
503
504                 status = acpi_evaluate_integer(device->dev->handle, buf,
505                                                 NULL, level);
506                 if (ACPI_SUCCESS(status)) {
507                         if (device->brightness->flags._BQC_use_index) {
508                                 if (device->brightness->flags._BCL_reversed)
509                                         *level = device->brightness->count
510                                                                  - 3 - (*level);
511                                 *level = device->brightness->levels[*level + 2];
512
513                         }
514                         *level += bqc_offset_aml_bug_workaround;
515                         for (i = 2; i < device->brightness->count; i++)
516                                 if (device->brightness->levels[i] == *level) {
517                                         device->brightness->curr = *level;
518                                         return 0;
519                         }
520                         if (!init) {
521                                 /*
522                                  * BQC returned an invalid level.
523                                  * Stop using it.
524                                  */
525                                 ACPI_WARNING((AE_INFO,
526                                               "%s returned an invalid level",
527                                               buf));
528                                 device->cap._BQC = device->cap._BCQ = 0;
529                         }
530                 } else {
531                         /* Fixme:
532                          * should we return an error or ignore this failure?
533                          * dev->brightness->curr is a cached value which stores
534                          * the correct current backlight level in most cases.
535                          * ACPI video backlight still works w/ buggy _BQC.
536                          * http://bugzilla.kernel.org/show_bug.cgi?id=12233
537                          */
538                         ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
539                         device->cap._BQC = device->cap._BCQ = 0;
540                 }
541         }
542
543         *level = device->brightness->curr;
544         return 0;
545 }
546
547 static int
548 acpi_video_device_EDID(struct acpi_video_device *device,
549                        union acpi_object **edid, ssize_t length)
550 {
551         int status;
552         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
553         union acpi_object *obj;
554         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
555         struct acpi_object_list args = { 1, &arg0 };
556
557
558         *edid = NULL;
559
560         if (!device)
561                 return -ENODEV;
562         if (length == 128)
563                 arg0.integer.value = 1;
564         else if (length == 256)
565                 arg0.integer.value = 2;
566         else
567                 return -EINVAL;
568
569         status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
570         if (ACPI_FAILURE(status))
571                 return -ENODEV;
572
573         obj = buffer.pointer;
574
575         if (obj && obj->type == ACPI_TYPE_BUFFER)
576                 *edid = obj;
577         else {
578                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
579                 status = -EFAULT;
580                 kfree(obj);
581         }
582
583         return status;
584 }
585
586 /* bus */
587
588 /*
589  *  Arg:
590  *      video           : video bus device pointer
591  *      bios_flag       : 
592  *              0.      The system BIOS should NOT automatically switch(toggle)
593  *                      the active display output.
594  *              1.      The system BIOS should automatically switch (toggle) the
595  *                      active display output. No switch event.
596  *              2.      The _DGS value should be locked.
597  *              3.      The system BIOS should not automatically switch (toggle) the
598  *                      active display output, but instead generate the display switch
599  *                      event notify code.
600  *      lcd_flag        :
601  *              0.      The system BIOS should automatically control the brightness level
602  *                      of the LCD when the power changes from AC to DC
603  *              1.      The system BIOS should NOT automatically control the brightness 
604  *                      level of the LCD when the power changes from AC to DC.
605  * Return Value:
606  *              -1      wrong arg.
607  */
608
609 static int
610 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
611 {
612         u64 status = 0;
613         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
614         struct acpi_object_list args = { 1, &arg0 };
615
616
617         if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
618                 status = -1;
619                 goto Failed;
620         }
621         arg0.integer.value = (lcd_flag << 2) | bios_flag;
622         video->dos_setting = arg0.integer.value;
623         acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
624
625       Failed:
626         return status;
627 }
628
629 /*
630  * Simple comparison function used to sort backlight levels.
631  */
632
633 static int
634 acpi_video_cmp_level(const void *a, const void *b)
635 {
636         return *(int *)a - *(int *)b;
637 }
638
639 /*
640  *  Arg:        
641  *      device  : video output device (LCD, CRT, ..)
642  *
643  *  Return Value:
644  *      Maximum brightness level
645  *
646  *  Allocate and initialize device->brightness.
647  */
648
649 static int
650 acpi_video_init_brightness(struct acpi_video_device *device)
651 {
652         union acpi_object *obj = NULL;
653         int i, max_level = 0, count = 0, level_ac_battery = 0;
654         unsigned long long level, level_old;
655         union acpi_object *o;
656         struct acpi_video_device_brightness *br = NULL;
657         int result = -EINVAL;
658
659         if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
660                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
661                                                 "LCD brightness level\n"));
662                 goto out;
663         }
664
665         if (obj->package.count < 2)
666                 goto out;
667
668         br = kzalloc(sizeof(*br), GFP_KERNEL);
669         if (!br) {
670                 printk(KERN_ERR "can't allocate memory\n");
671                 result = -ENOMEM;
672                 goto out;
673         }
674
675         br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
676                                 GFP_KERNEL);
677         if (!br->levels) {
678                 result = -ENOMEM;
679                 goto out_free;
680         }
681
682         for (i = 0; i < obj->package.count; i++) {
683                 o = (union acpi_object *)&obj->package.elements[i];
684                 if (o->type != ACPI_TYPE_INTEGER) {
685                         printk(KERN_ERR PREFIX "Invalid data\n");
686                         continue;
687                 }
688                 br->levels[count] = (u32) o->integer.value;
689
690                 if (br->levels[count] > max_level)
691                         max_level = br->levels[count];
692                 count++;
693         }
694
695         /*
696          * some buggy BIOS don't export the levels
697          * when machine is on AC/Battery in _BCL package.
698          * In this case, the first two elements in _BCL packages
699          * are also supported brightness levels that OS should take care of.
700          */
701         for (i = 2; i < count; i++) {
702                 if (br->levels[i] == br->levels[0])
703                         level_ac_battery++;
704                 if (br->levels[i] == br->levels[1])
705                         level_ac_battery++;
706         }
707
708         if (level_ac_battery < 2) {
709                 level_ac_battery = 2 - level_ac_battery;
710                 br->flags._BCL_no_ac_battery_levels = 1;
711                 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
712                         br->levels[i] = br->levels[i - level_ac_battery];
713                 count += level_ac_battery;
714         } else if (level_ac_battery > 2)
715                 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
716
717         /* Check if the _BCL package is in a reversed order */
718         if (max_level == br->levels[2]) {
719                 br->flags._BCL_reversed = 1;
720                 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
721                         acpi_video_cmp_level, NULL);
722         } else if (max_level != br->levels[count - 1])
723                 ACPI_ERROR((AE_INFO,
724                             "Found unordered _BCL package\n"));
725
726         br->count = count;
727         device->brightness = br;
728
729         /* Check the input/output of _BQC/_BCL/_BCM */
730         if ((max_level < 100) && (max_level <= (count - 2)))
731                 br->flags._BCL_use_index = 1;
732
733         /*
734          * _BCM is always consistent with _BCL,
735          * at least for all the laptops we have ever seen.
736          */
737         br->flags._BCM_use_index = br->flags._BCL_use_index;
738
739         /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
740         br->curr = level = max_level;
741
742         if (!device->cap._BQC)
743                 goto set_level;
744
745         result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
746         if (result)
747                 goto out_free_levels;
748
749         /*
750          * Set the level to maximum and check if _BQC uses indexed value
751          */
752         result = acpi_video_device_lcd_set_level(device, max_level);
753         if (result)
754                 goto out_free_levels;
755
756         result = acpi_video_device_lcd_get_level_current(device, &level, 0);
757         if (result)
758                 goto out_free_levels;
759
760         br->flags._BQC_use_index = (level == max_level ? 0 : 1);
761
762         if (!br->flags._BQC_use_index) {
763                 /*
764                  * Set the backlight to the initial state.
765                  * On some buggy laptops, _BQC returns an uninitialized value
766                  * when invoked for the first time, i.e. level_old is invalid.
767                  * set the backlight to max_level in this case
768                  */
769                 for (i = 2; i < br->count; i++)
770                         if (level_old == br->levels[i])
771                                 level = level_old;
772                 goto set_level;
773         }
774
775         if (br->flags._BCL_reversed)
776                 level_old = (br->count - 1) - level_old;
777         level = br->levels[level_old];
778
779 set_level:
780         result = acpi_video_device_lcd_set_level(device, level);
781         if (result)
782                 goto out_free_levels;
783
784         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
785                           "found %d brightness levels\n", count - 2));
786         kfree(obj);
787         return result;
788
789 out_free_levels:
790         kfree(br->levels);
791 out_free:
792         kfree(br);
793 out:
794         device->brightness = NULL;
795         kfree(obj);
796         return result;
797 }
798
799 /*
800  *  Arg:
801  *      device  : video output device (LCD, CRT, ..)
802  *
803  *  Return Value:
804  *      None
805  *
806  *  Find out all required AML methods defined under the output
807  *  device.
808  */
809
810 static void acpi_video_device_find_cap(struct acpi_video_device *device)
811 {
812         acpi_handle h_dummy1;
813
814         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
815                 device->cap._ADR = 1;
816         }
817         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
818                 device->cap._BCL = 1;
819         }
820         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
821                 device->cap._BCM = 1;
822         }
823         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
824                 device->cap._BQC = 1;
825         else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
826                                 &h_dummy1))) {
827                 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
828                 device->cap._BCQ = 1;
829         }
830
831         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
832                 device->cap._DDC = 1;
833         }
834         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
835                 device->cap._DCS = 1;
836         }
837         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
838                 device->cap._DGS = 1;
839         }
840         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
841                 device->cap._DSS = 1;
842         }
843
844         if (acpi_video_backlight_support()) {
845                 struct backlight_properties props;
846                 int result;
847                 static int count = 0;
848                 char *name;
849
850                 result = acpi_video_init_brightness(device);
851                 if (result)
852                         return;
853                 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
854                 if (!name)
855                         return;
856                 count++;
857
858                 memset(&props, 0, sizeof(struct backlight_properties));
859                 props.max_brightness = device->brightness->count - 3;
860                 device->backlight = backlight_device_register(name, NULL, device,
861                                                               &acpi_backlight_ops,
862                                                               &props);
863                 kfree(name);
864                 if (IS_ERR(device->backlight))
865                         return;
866
867                 /*
868                  * Save current brightness level in case we have to restore it
869                  * before acpi_video_device_lcd_set_level() is called next time.
870                  */
871                 device->backlight->props.brightness =
872                                 acpi_video_get_brightness(device->backlight);
873
874                 result = sysfs_create_link(&device->backlight->dev.kobj,
875                                            &device->dev->dev.kobj, "device");
876                 if (result)
877                         printk(KERN_ERR PREFIX "Create sysfs link\n");
878
879                 device->cooling_dev = thermal_cooling_device_register("LCD",
880                                         device->dev, &video_cooling_ops);
881                 if (IS_ERR(device->cooling_dev)) {
882                         /*
883                          * Set cooling_dev to NULL so we don't crash trying to
884                          * free it.
885                          * Also, why the hell we are returning early and
886                          * not attempt to register video output if cooling
887                          * device registration failed?
888                          * -- dtor
889                          */
890                         device->cooling_dev = NULL;
891                         return;
892                 }
893
894                 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
895                          device->cooling_dev->id);
896                 result = sysfs_create_link(&device->dev->dev.kobj,
897                                 &device->cooling_dev->device.kobj,
898                                 "thermal_cooling");
899                 if (result)
900                         printk(KERN_ERR PREFIX "Create sysfs link\n");
901                 result = sysfs_create_link(&device->cooling_dev->device.kobj,
902                                 &device->dev->dev.kobj, "device");
903                 if (result)
904                         printk(KERN_ERR PREFIX "Create sysfs link\n");
905
906         }
907
908         if (acpi_video_display_switch_support()) {
909
910                 if (device->cap._DCS && device->cap._DSS) {
911                         static int count;
912                         char *name;
913                         name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
914                         if (!name)
915                                 return;
916                         count++;
917                         device->output_dev = video_output_register(name,
918                                         NULL, device, &acpi_output_properties);
919                         kfree(name);
920                 }
921         }
922 }
923
924 /*
925  *  Arg:        
926  *      device  : video output device (VGA)
927  *
928  *  Return Value:
929  *      None
930  *
931  *  Find out all required AML methods defined under the video bus device.
932  */
933
934 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
935 {
936         acpi_handle h_dummy1;
937
938         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
939                 video->cap._DOS = 1;
940         }
941         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
942                 video->cap._DOD = 1;
943         }
944         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
945                 video->cap._ROM = 1;
946         }
947         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
948                 video->cap._GPD = 1;
949         }
950         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
951                 video->cap._SPD = 1;
952         }
953         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
954                 video->cap._VPO = 1;
955         }
956 }
957
958 /*
959  * Check whether the video bus device has required AML method to
960  * support the desired features
961  */
962
963 static int acpi_video_bus_check(struct acpi_video_bus *video)
964 {
965         acpi_status status = -ENOENT;
966         struct pci_dev *dev;
967
968         if (!video)
969                 return -EINVAL;
970
971         dev = acpi_get_pci_dev(video->device->handle);
972         if (!dev)
973                 return -ENODEV;
974         pci_dev_put(dev);
975
976         /* Since there is no HID, CID and so on for VGA driver, we have
977          * to check well known required nodes.
978          */
979
980         /* Does this device support video switching? */
981         if (video->cap._DOS || video->cap._DOD) {
982                 if (!video->cap._DOS) {
983                         printk(KERN_WARNING FW_BUG
984                                 "ACPI(%s) defines _DOD but not _DOS\n",
985                                 acpi_device_bid(video->device));
986                 }
987                 video->flags.multihead = 1;
988                 status = 0;
989         }
990
991         /* Does this device support retrieving a video ROM? */
992         if (video->cap._ROM) {
993                 video->flags.rom = 1;
994                 status = 0;
995         }
996
997         /* Does this device support configuring which video device to POST? */
998         if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
999                 video->flags.post = 1;
1000                 status = 0;
1001         }
1002
1003         return status;
1004 }
1005
1006 /* --------------------------------------------------------------------------
1007                                  Driver Interface
1008    -------------------------------------------------------------------------- */
1009
1010 /* device interface */
1011 static struct acpi_video_device_attrib*
1012 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1013 {
1014         struct acpi_video_enumerated_device *ids;
1015         int i;
1016
1017         for (i = 0; i < video->attached_count; i++) {
1018                 ids = &video->attached_array[i];
1019                 if ((ids->value.int_val & 0xffff) == device_id)
1020                         return &ids->value.attrib;
1021         }
1022
1023         return NULL;
1024 }
1025
1026 static int
1027 acpi_video_get_device_type(struct acpi_video_bus *video,
1028                            unsigned long device_id)
1029 {
1030         struct acpi_video_enumerated_device *ids;
1031         int i;
1032
1033         for (i = 0; i < video->attached_count; i++) {
1034                 ids = &video->attached_array[i];
1035                 if ((ids->value.int_val & 0xffff) == device_id)
1036                         return ids->value.int_val;
1037         }
1038
1039         return 0;
1040 }
1041
1042 static int
1043 acpi_video_bus_get_one_device(struct acpi_device *device,
1044                               struct acpi_video_bus *video)
1045 {
1046         unsigned long long device_id;
1047         int status, device_type;
1048         struct acpi_video_device *data;
1049         struct acpi_video_device_attrib* attribute;
1050
1051         if (!device || !video)
1052                 return -EINVAL;
1053
1054         status =
1055             acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1056         if (ACPI_SUCCESS(status)) {
1057
1058                 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1059                 if (!data)
1060                         return -ENOMEM;
1061
1062                 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1063                 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1064                 device->driver_data = data;
1065
1066                 data->device_id = device_id;
1067                 data->video = video;
1068                 data->dev = device;
1069
1070                 attribute = acpi_video_get_device_attr(video, device_id);
1071
1072                 if((attribute != NULL) && attribute->device_id_scheme) {
1073                         switch (attribute->display_type) {
1074                         case ACPI_VIDEO_DISPLAY_CRT:
1075                                 data->flags.crt = 1;
1076                                 break;
1077                         case ACPI_VIDEO_DISPLAY_TV:
1078                                 data->flags.tvout = 1;
1079                                 break;
1080                         case ACPI_VIDEO_DISPLAY_DVI:
1081                                 data->flags.dvi = 1;
1082                                 break;
1083                         case ACPI_VIDEO_DISPLAY_LCD:
1084                                 data->flags.lcd = 1;
1085                                 break;
1086                         default:
1087                                 data->flags.unknown = 1;
1088                                 break;
1089                         }
1090                         if(attribute->bios_can_detect)
1091                                 data->flags.bios = 1;
1092                 } else {
1093                         /* Check for legacy IDs */
1094                         device_type = acpi_video_get_device_type(video,
1095                                                                  device_id);
1096                         /* Ignore bits 16 and 18-20 */
1097                         switch (device_type & 0xffe2ffff) {
1098                         case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1099                                 data->flags.crt = 1;
1100                                 break;
1101                         case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1102                                 data->flags.lcd = 1;
1103                                 break;
1104                         case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1105                                 data->flags.tvout = 1;
1106                                 break;
1107                         default:
1108                                 data->flags.unknown = 1;
1109                         }
1110                 }
1111
1112                 acpi_video_device_bind(video, data);
1113                 acpi_video_device_find_cap(data);
1114
1115                 status = acpi_install_notify_handler(device->handle,
1116                                                      ACPI_DEVICE_NOTIFY,
1117                                                      acpi_video_device_notify,
1118                                                      data);
1119                 if (ACPI_FAILURE(status)) {
1120                         printk(KERN_ERR PREFIX
1121                                           "Error installing notify handler\n");
1122                         if(data->brightness)
1123                                 kfree(data->brightness->levels);
1124                         kfree(data->brightness);
1125                         kfree(data);
1126                         return -ENODEV;
1127                 }
1128
1129                 mutex_lock(&video->device_list_lock);
1130                 list_add_tail(&data->entry, &video->video_device_list);
1131                 mutex_unlock(&video->device_list_lock);
1132
1133                 return 0;
1134         }
1135
1136         return -ENOENT;
1137 }
1138
1139 /*
1140  *  Arg:
1141  *      video   : video bus device 
1142  *
1143  *  Return:
1144  *      none
1145  *  
1146  *  Enumerate the video device list of the video bus, 
1147  *  bind the ids with the corresponding video devices
1148  *  under the video bus.
1149  */
1150
1151 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1152 {
1153         struct acpi_video_device *dev;
1154
1155         mutex_lock(&video->device_list_lock);
1156
1157         list_for_each_entry(dev, &video->video_device_list, entry)
1158                 acpi_video_device_bind(video, dev);
1159
1160         mutex_unlock(&video->device_list_lock);
1161 }
1162
1163 /*
1164  *  Arg:
1165  *      video   : video bus device 
1166  *      device  : video output device under the video 
1167  *              bus
1168  *
1169  *  Return:
1170  *      none
1171  *  
1172  *  Bind the ids with the corresponding video devices
1173  *  under the video bus.
1174  */
1175
1176 static void
1177 acpi_video_device_bind(struct acpi_video_bus *video,
1178                        struct acpi_video_device *device)
1179 {
1180         struct acpi_video_enumerated_device *ids;
1181         int i;
1182
1183         for (i = 0; i < video->attached_count; i++) {
1184                 ids = &video->attached_array[i];
1185                 if (device->device_id == (ids->value.int_val & 0xffff)) {
1186                         ids->bind_info = device;
1187                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1188                 }
1189         }
1190 }
1191
1192 /*
1193  *  Arg:
1194  *      video   : video bus device 
1195  *
1196  *  Return:
1197  *      < 0     : error
1198  *  
1199  *  Call _DOD to enumerate all devices attached to display adapter
1200  *
1201  */
1202
1203 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1204 {
1205         int status;
1206         int count;
1207         int i;
1208         struct acpi_video_enumerated_device *active_list;
1209         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1210         union acpi_object *dod = NULL;
1211         union acpi_object *obj;
1212
1213         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1214         if (!ACPI_SUCCESS(status)) {
1215                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1216                 return status;
1217         }
1218
1219         dod = buffer.pointer;
1220         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1221                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1222                 status = -EFAULT;
1223                 goto out;
1224         }
1225
1226         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1227                           dod->package.count));
1228
1229         active_list = kcalloc(1 + dod->package.count,
1230                               sizeof(struct acpi_video_enumerated_device),
1231                               GFP_KERNEL);
1232         if (!active_list) {
1233                 status = -ENOMEM;
1234                 goto out;
1235         }
1236
1237         count = 0;
1238         for (i = 0; i < dod->package.count; i++) {
1239                 obj = &dod->package.elements[i];
1240
1241                 if (obj->type != ACPI_TYPE_INTEGER) {
1242                         printk(KERN_ERR PREFIX
1243                                 "Invalid _DOD data in element %d\n", i);
1244                         continue;
1245                 }
1246
1247                 active_list[count].value.int_val = obj->integer.value;
1248                 active_list[count].bind_info = NULL;
1249                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1250                                   (int)obj->integer.value));
1251                 count++;
1252         }
1253
1254         kfree(video->attached_array);
1255
1256         video->attached_array = active_list;
1257         video->attached_count = count;
1258
1259  out:
1260         kfree(buffer.pointer);
1261         return status;
1262 }
1263
1264 static int
1265 acpi_video_get_next_level(struct acpi_video_device *device,
1266                           u32 level_current, u32 event)
1267 {
1268         int min, max, min_above, max_below, i, l, delta = 255;
1269         max = max_below = 0;
1270         min = min_above = 255;
1271         /* Find closest level to level_current */
1272         for (i = 2; i < device->brightness->count; i++) {
1273                 l = device->brightness->levels[i];
1274                 if (abs(l - level_current) < abs(delta)) {
1275                         delta = l - level_current;
1276                         if (!delta)
1277                                 break;
1278                 }
1279         }
1280         /* Ajust level_current to closest available level */
1281         level_current += delta;
1282         for (i = 2; i < device->brightness->count; i++) {
1283                 l = device->brightness->levels[i];
1284                 if (l < min)
1285                         min = l;
1286                 if (l > max)
1287                         max = l;
1288                 if (l < min_above && l > level_current)
1289                         min_above = l;
1290                 if (l > max_below && l < level_current)
1291                         max_below = l;
1292         }
1293
1294         switch (event) {
1295         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1296                 return (level_current < max) ? min_above : min;
1297         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1298                 return (level_current < max) ? min_above : max;
1299         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1300                 return (level_current > min) ? max_below : min;
1301         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1302         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1303                 return 0;
1304         default:
1305                 return level_current;
1306         }
1307 }
1308
1309 static int
1310 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1311 {
1312         unsigned long long level_current, level_next;
1313         int result = -EINVAL;
1314
1315         /* no warning message if acpi_backlight=vendor is used */
1316         if (!acpi_video_backlight_support())
1317                 return 0;
1318
1319         if (!device->brightness)
1320                 goto out;
1321
1322         result = acpi_video_device_lcd_get_level_current(device,
1323                                                          &level_current, 0);
1324         if (result)
1325                 goto out;
1326
1327         level_next = acpi_video_get_next_level(device, level_current, event);
1328
1329         result = acpi_video_device_lcd_set_level(device, level_next);
1330
1331         if (!result)
1332                 backlight_force_update(device->backlight,
1333                                        BACKLIGHT_UPDATE_HOTKEY);
1334
1335 out:
1336         if (result)
1337                 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1338
1339         return result;
1340 }
1341
1342 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1343                         void **edid)
1344 {
1345         struct acpi_video_bus *video;
1346         struct acpi_video_device *video_device;
1347         union acpi_object *buffer = NULL;
1348         acpi_status status;
1349         int i, length;
1350
1351         if (!device || !acpi_driver_data(device))
1352                 return -EINVAL;
1353
1354         video = acpi_driver_data(device);
1355
1356         for (i = 0; i < video->attached_count; i++) {
1357                 video_device = video->attached_array[i].bind_info;
1358                 length = 256;
1359
1360                 if (!video_device)
1361                         continue;
1362
1363                 if (type) {
1364                         switch (type) {
1365                         case ACPI_VIDEO_DISPLAY_CRT:
1366                                 if (!video_device->flags.crt)
1367                                         continue;
1368                                 break;
1369                         case ACPI_VIDEO_DISPLAY_TV:
1370                                 if (!video_device->flags.tvout)
1371                                         continue;
1372                                 break;
1373                         case ACPI_VIDEO_DISPLAY_DVI:
1374                                 if (!video_device->flags.dvi)
1375                                         continue;
1376                                 break;
1377                         case ACPI_VIDEO_DISPLAY_LCD:
1378                                 if (!video_device->flags.lcd)
1379                                         continue;
1380                                 break;
1381                         }
1382                 } else if (video_device->device_id != device_id) {
1383                         continue;
1384                 }
1385
1386                 status = acpi_video_device_EDID(video_device, &buffer, length);
1387
1388                 if (ACPI_FAILURE(status) || !buffer ||
1389                     buffer->type != ACPI_TYPE_BUFFER) {
1390                         length = 128;
1391                         status = acpi_video_device_EDID(video_device, &buffer,
1392                                                         length);
1393                         if (ACPI_FAILURE(status) || !buffer ||
1394                             buffer->type != ACPI_TYPE_BUFFER) {
1395                                 continue;
1396                         }
1397                 }
1398
1399                 *edid = buffer->buffer.pointer;
1400                 return length;
1401         }
1402
1403         return -ENODEV;
1404 }
1405 EXPORT_SYMBOL(acpi_video_get_edid);
1406
1407 static int
1408 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1409                            struct acpi_device *device)
1410 {
1411         int status = 0;
1412         struct acpi_device *dev;
1413
1414         acpi_video_device_enumerate(video);
1415
1416         list_for_each_entry(dev, &device->children, node) {
1417
1418                 status = acpi_video_bus_get_one_device(dev, video);
1419                 if (ACPI_FAILURE(status)) {
1420                         printk(KERN_WARNING PREFIX
1421                                         "Cant attach device\n");
1422                         continue;
1423                 }
1424         }
1425         return status;
1426 }
1427
1428 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1429 {
1430         acpi_status status;
1431
1432         if (!device || !device->video)
1433                 return -ENOENT;
1434
1435         status = acpi_remove_notify_handler(device->dev->handle,
1436                                             ACPI_DEVICE_NOTIFY,
1437                                             acpi_video_device_notify);
1438         if (ACPI_FAILURE(status)) {
1439                 printk(KERN_WARNING PREFIX
1440                        "Cant remove video notify handler\n");
1441         }
1442         if (device->backlight) {
1443                 sysfs_remove_link(&device->backlight->dev.kobj, "device");
1444                 backlight_device_unregister(device->backlight);
1445                 device->backlight = NULL;
1446         }
1447         if (device->cooling_dev) {
1448                 sysfs_remove_link(&device->dev->dev.kobj,
1449                                   "thermal_cooling");
1450                 sysfs_remove_link(&device->cooling_dev->device.kobj,
1451                                   "device");
1452                 thermal_cooling_device_unregister(device->cooling_dev);
1453                 device->cooling_dev = NULL;
1454         }
1455         video_output_unregister(device->output_dev);
1456
1457         return 0;
1458 }
1459
1460 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1461 {
1462         int status;
1463         struct acpi_video_device *dev, *next;
1464
1465         mutex_lock(&video->device_list_lock);
1466
1467         list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1468
1469                 status = acpi_video_bus_put_one_device(dev);
1470                 if (ACPI_FAILURE(status))
1471                         printk(KERN_WARNING PREFIX
1472                                "hhuuhhuu bug in acpi video driver.\n");
1473
1474                 if (dev->brightness) {
1475                         kfree(dev->brightness->levels);
1476                         kfree(dev->brightness);
1477                 }
1478                 list_del(&dev->entry);
1479                 kfree(dev);
1480         }
1481
1482         mutex_unlock(&video->device_list_lock);
1483
1484         return 0;
1485 }
1486
1487 /* acpi_video interface */
1488
1489 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1490 {
1491         return acpi_video_bus_DOS(video, 0, 0);
1492 }
1493
1494 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1495 {
1496         return acpi_video_bus_DOS(video, 0, 1);
1497 }
1498
1499 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1500 {
1501         struct acpi_video_bus *video = acpi_driver_data(device);
1502         struct input_dev *input;
1503         int keycode = 0;
1504
1505         if (!video)
1506                 return;
1507
1508         input = video->input;
1509
1510         switch (event) {
1511         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User requested a switch,
1512                                          * most likely via hotkey. */
1513                 acpi_bus_generate_proc_event(device, event, 0);
1514                 keycode = KEY_SWITCHVIDEOMODE;
1515                 break;
1516
1517         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plugged in or removed a video
1518                                          * connector. */
1519                 acpi_video_device_enumerate(video);
1520                 acpi_video_device_rebind(video);
1521                 acpi_bus_generate_proc_event(device, event, 0);
1522                 keycode = KEY_SWITCHVIDEOMODE;
1523                 break;
1524
1525         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
1526                 acpi_bus_generate_proc_event(device, event, 0);
1527                 keycode = KEY_SWITCHVIDEOMODE;
1528                 break;
1529         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
1530                 acpi_bus_generate_proc_event(device, event, 0);
1531                 keycode = KEY_VIDEO_NEXT;
1532                 break;
1533         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
1534                 acpi_bus_generate_proc_event(device, event, 0);
1535                 keycode = KEY_VIDEO_PREV;
1536                 break;
1537
1538         default:
1539                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1540                                   "Unsupported event [0x%x]\n", event));
1541                 break;
1542         }
1543
1544         acpi_notifier_call_chain(device, event, 0);
1545
1546         if (keycode) {
1547                 input_report_key(input, keycode, 1);
1548                 input_sync(input);
1549                 input_report_key(input, keycode, 0);
1550                 input_sync(input);
1551         }
1552
1553         return;
1554 }
1555
1556 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1557 {
1558         struct acpi_video_device *video_device = data;
1559         struct acpi_device *device = NULL;
1560         struct acpi_video_bus *bus;
1561         struct input_dev *input;
1562         int keycode = 0;
1563
1564         if (!video_device)
1565                 return;
1566
1567         device = video_device->dev;
1568         bus = video_device->video;
1569         input = bus->input;
1570
1571         switch (event) {
1572         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
1573                 if (brightness_switch_enabled)
1574                         acpi_video_switch_brightness(video_device, event);
1575                 acpi_bus_generate_proc_event(device, event, 0);
1576                 keycode = KEY_BRIGHTNESS_CYCLE;
1577                 break;
1578         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
1579                 if (brightness_switch_enabled)
1580                         acpi_video_switch_brightness(video_device, event);
1581                 acpi_bus_generate_proc_event(device, event, 0);
1582                 keycode = KEY_BRIGHTNESSUP;
1583                 break;
1584         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
1585                 if (brightness_switch_enabled)
1586                         acpi_video_switch_brightness(video_device, event);
1587                 acpi_bus_generate_proc_event(device, event, 0);
1588                 keycode = KEY_BRIGHTNESSDOWN;
1589                 break;
1590         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1591                 if (brightness_switch_enabled)
1592                         acpi_video_switch_brightness(video_device, event);
1593                 acpi_bus_generate_proc_event(device, event, 0);
1594                 keycode = KEY_BRIGHTNESS_ZERO;
1595                 break;
1596         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
1597                 if (brightness_switch_enabled)
1598                         acpi_video_switch_brightness(video_device, event);
1599                 acpi_bus_generate_proc_event(device, event, 0);
1600                 keycode = KEY_DISPLAY_OFF;
1601                 break;
1602         default:
1603                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1604                                   "Unsupported event [0x%x]\n", event));
1605                 break;
1606         }
1607
1608         acpi_notifier_call_chain(device, event, 0);
1609
1610         if (keycode) {
1611                 input_report_key(input, keycode, 1);
1612                 input_sync(input);
1613                 input_report_key(input, keycode, 0);
1614                 input_sync(input);
1615         }
1616
1617         return;
1618 }
1619
1620 static int acpi_video_resume(struct notifier_block *nb,
1621                                 unsigned long val, void *ign)
1622 {
1623         struct acpi_video_bus *video;
1624         struct acpi_video_device *video_device;
1625         int i;
1626
1627         switch (val) {
1628         case PM_HIBERNATION_PREPARE:
1629         case PM_SUSPEND_PREPARE:
1630         case PM_RESTORE_PREPARE:
1631                 return NOTIFY_DONE;
1632         }
1633
1634         video = container_of(nb, struct acpi_video_bus, pm_nb);
1635
1636         dev_info(&video->device->dev, "Restoring backlight state\n");
1637
1638         for (i = 0; i < video->attached_count; i++) {
1639                 video_device = video->attached_array[i].bind_info;
1640                 if (video_device && video_device->backlight)
1641                         acpi_video_set_brightness(video_device->backlight);
1642         }
1643
1644         return NOTIFY_OK;
1645 }
1646
1647 static acpi_status
1648 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1649                         void **return_value)
1650 {
1651         struct acpi_device *device = context;
1652         struct acpi_device *sibling;
1653         int result;
1654
1655         if (handle == device->handle)
1656                 return AE_CTRL_TERMINATE;
1657
1658         result = acpi_bus_get_device(handle, &sibling);
1659         if (result)
1660                 return AE_OK;
1661
1662         if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1663                         return AE_ALREADY_EXISTS;
1664
1665         return AE_OK;
1666 }
1667
1668 static int instance;
1669
1670 static int acpi_video_bus_add(struct acpi_device *device)
1671 {
1672         struct acpi_video_bus *video;
1673         struct input_dev *input;
1674         int error;
1675         acpi_status status;
1676
1677         status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1678                                 device->parent->handle, 1,
1679                                 acpi_video_bus_match, NULL,
1680                                 device, NULL);
1681         if (status == AE_ALREADY_EXISTS) {
1682                 printk(KERN_WARNING FW_BUG
1683                         "Duplicate ACPI video bus devices for the"
1684                         " same VGA controller, please try module "
1685                         "parameter \"video.allow_duplicates=1\""
1686                         "if the current driver doesn't work.\n");
1687                 if (!allow_duplicates)
1688                         return -ENODEV;
1689         }
1690
1691         video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1692         if (!video)
1693                 return -ENOMEM;
1694
1695         /* a hack to fix the duplicate name "VID" problem on T61 */
1696         if (!strcmp(device->pnp.bus_id, "VID")) {
1697                 if (instance)
1698                         device->pnp.bus_id[3] = '0' + instance;
1699                 instance ++;
1700         }
1701         /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1702         if (!strcmp(device->pnp.bus_id, "VGA")) {
1703                 if (instance)
1704                         device->pnp.bus_id[3] = '0' + instance;
1705                 instance++;
1706         }
1707
1708         video->device = device;
1709         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1710         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1711         device->driver_data = video;
1712
1713         acpi_video_bus_find_cap(video);
1714         error = acpi_video_bus_check(video);
1715         if (error)
1716                 goto err_free_video;
1717
1718         mutex_init(&video->device_list_lock);
1719         INIT_LIST_HEAD(&video->video_device_list);
1720
1721         acpi_video_bus_get_devices(video, device);
1722         acpi_video_bus_start_devices(video);
1723
1724         video->input = input = input_allocate_device();
1725         if (!input) {
1726                 error = -ENOMEM;
1727                 goto err_stop_video;
1728         }
1729
1730         snprintf(video->phys, sizeof(video->phys),
1731                 "%s/video/input0", acpi_device_hid(video->device));
1732
1733         input->name = acpi_device_name(video->device);
1734         input->phys = video->phys;
1735         input->id.bustype = BUS_HOST;
1736         input->id.product = 0x06;
1737         input->dev.parent = &device->dev;
1738         input->evbit[0] = BIT(EV_KEY);
1739         set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1740         set_bit(KEY_VIDEO_NEXT, input->keybit);
1741         set_bit(KEY_VIDEO_PREV, input->keybit);
1742         set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1743         set_bit(KEY_BRIGHTNESSUP, input->keybit);
1744         set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1745         set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1746         set_bit(KEY_DISPLAY_OFF, input->keybit);
1747
1748         error = input_register_device(input);
1749         if (error)
1750                 goto err_free_input_dev;
1751
1752         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
1753                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1754                video->flags.multihead ? "yes" : "no",
1755                video->flags.rom ? "yes" : "no",
1756                video->flags.post ? "yes" : "no");
1757
1758         video->pm_nb.notifier_call = acpi_video_resume;
1759         video->pm_nb.priority = 0;
1760         register_pm_notifier(&video->pm_nb);
1761
1762         return 0;
1763
1764  err_free_input_dev:
1765         input_free_device(input);
1766  err_stop_video:
1767         acpi_video_bus_stop_devices(video);
1768         acpi_video_bus_put_devices(video);
1769         kfree(video->attached_array);
1770  err_free_video:
1771         kfree(video);
1772         device->driver_data = NULL;
1773
1774         return error;
1775 }
1776
1777 static int acpi_video_bus_remove(struct acpi_device *device, int type)
1778 {
1779         struct acpi_video_bus *video = NULL;
1780
1781
1782         if (!device || !acpi_driver_data(device))
1783                 return -EINVAL;
1784
1785         video = acpi_driver_data(device);
1786
1787         unregister_pm_notifier(&video->pm_nb);
1788
1789         acpi_video_bus_stop_devices(video);
1790         acpi_video_bus_put_devices(video);
1791
1792         input_unregister_device(video->input);
1793         kfree(video->attached_array);
1794         kfree(video);
1795
1796         return 0;
1797 }
1798
1799 static int __init intel_opregion_present(void)
1800 {
1801 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1802         struct pci_dev *dev = NULL;
1803         u32 address;
1804
1805         for_each_pci_dev(dev) {
1806                 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1807                         continue;
1808                 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1809                         continue;
1810                 pci_read_config_dword(dev, 0xfc, &address);
1811                 if (!address)
1812                         continue;
1813                 return 1;
1814         }
1815 #endif
1816         return 0;
1817 }
1818
1819 int acpi_video_register(void)
1820 {
1821         int result = 0;
1822         if (register_count) {
1823                 /*
1824                  * if the function of acpi_video_register is already called,
1825                  * don't register the acpi_vide_bus again and return no error.
1826                  */
1827                 return 0;
1828         }
1829
1830         result = acpi_bus_register_driver(&acpi_video_bus);
1831         if (result < 0)
1832                 return -ENODEV;
1833
1834         /*
1835          * When the acpi_video_bus is loaded successfully, increase
1836          * the counter reference.
1837          */
1838         register_count = 1;
1839
1840         return 0;
1841 }
1842 EXPORT_SYMBOL(acpi_video_register);
1843
1844 void acpi_video_unregister(void)
1845 {
1846         if (!register_count) {
1847                 /*
1848                  * If the acpi video bus is already unloaded, don't
1849                  * unload it again and return directly.
1850                  */
1851                 return;
1852         }
1853         acpi_bus_unregister_driver(&acpi_video_bus);
1854
1855         register_count = 0;
1856
1857         return;
1858 }
1859 EXPORT_SYMBOL(acpi_video_unregister);
1860
1861 /*
1862  * This is kind of nasty. Hardware using Intel chipsets may require
1863  * the video opregion code to be run first in order to initialise
1864  * state before any ACPI video calls are made. To handle this we defer
1865  * registration of the video class until the opregion code has run.
1866  */
1867
1868 static int __init acpi_video_init(void)
1869 {
1870         dmi_check_system(video_dmi_table);
1871
1872         if (intel_opregion_present())
1873                 return 0;
1874
1875         return acpi_video_register();
1876 }
1877
1878 static void __exit acpi_video_exit(void)
1879 {
1880         acpi_video_unregister();
1881
1882         return;
1883 }
1884
1885 module_init(acpi_video_init);
1886 module_exit(acpi_video_exit);