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