Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

Linux/drivers/acpi/video.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us