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