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