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