2 * video.c - ACPI Video Driver
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
48 #define PREFIX "ACPI: "
50 #define ACPI_VIDEO_BUS_NAME "Video Bus"
51 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
52 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
53 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
54 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
55 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
56 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
58 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
59 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
60 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
61 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
62 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
64 #define MAX_NAME_LEN 20
66 #define _COMPONENT ACPI_VIDEO_COMPONENT
67 ACPI_MODULE_NAME("video");
69 MODULE_AUTHOR("Bruno Ducrot");
70 MODULE_DESCRIPTION("ACPI Video Driver");
71 MODULE_LICENSE("GPL");
73 static bool brightness_switch_enabled = 1;
74 module_param(brightness_switch_enabled, bool, 0644);
77 * By default, we don't allow duplicate ACPI video bus devices
78 * under the same VGA controller
80 static bool allow_duplicates;
81 module_param(allow_duplicates, bool, 0644);
84 * For Windows 8 systems: used to decide if video module
85 * should skip registering backlight interface of its own.
87 static int use_native_backlight_param = -1;
88 module_param_named(use_native_backlight, use_native_backlight_param, int, 0444);
89 static bool use_native_backlight_dmi = false;
91 static int register_count;
92 static struct mutex video_list_lock;
93 static struct list_head video_bus_head;
94 static int acpi_video_bus_add(struct acpi_device *device);
95 static int acpi_video_bus_remove(struct acpi_device *device);
96 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
98 static const struct acpi_device_id video_device_ids[] = {
102 MODULE_DEVICE_TABLE(acpi, video_device_ids);
104 static struct acpi_driver acpi_video_bus = {
106 .class = ACPI_VIDEO_CLASS,
107 .ids = video_device_ids,
109 .add = acpi_video_bus_add,
110 .remove = acpi_video_bus_remove,
111 .notify = acpi_video_bus_notify,
115 struct acpi_video_bus_flags {
116 u8 multihead:1; /* can switch video heads */
117 u8 rom:1; /* can retrieve a video rom */
118 u8 post:1; /* can configure the head to */
122 struct acpi_video_bus_cap {
123 u8 _DOS:1; /* Enable/Disable output switching */
124 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
125 u8 _ROM:1; /* Get ROM Data */
126 u8 _GPD:1; /* Get POST Device */
127 u8 _SPD:1; /* Set POST Device */
128 u8 _VPO:1; /* Video POST Options */
132 struct acpi_video_device_attrib {
133 u32 display_index:4; /* A zero-based instance of the Display */
134 u32 display_port_attachment:4; /* This field differentiates the display type */
135 u32 display_type:4; /* Describe the specific type in use */
136 u32 vendor_specific:4; /* Chipset Vendor Specific */
137 u32 bios_can_detect:1; /* BIOS can detect the device */
138 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
140 u32 pipe_id:3; /* For VGA multiple-head devices. */
141 u32 reserved:10; /* Must be 0 */
142 u32 device_id_scheme:1; /* Device ID Scheme */
145 struct acpi_video_enumerated_device {
148 struct acpi_video_device_attrib attrib;
150 struct acpi_video_device *bind_info;
153 struct acpi_video_bus {
154 struct acpi_device *device;
156 struct acpi_video_enumerated_device *attached_array;
158 struct acpi_video_bus_cap cap;
159 struct acpi_video_bus_flags flags;
160 struct list_head video_device_list;
161 struct mutex device_list_lock; /* protects video_device_list */
162 struct list_head entry;
163 struct input_dev *input;
164 char phys[32]; /* for input device */
165 struct notifier_block pm_nb;
168 struct acpi_video_device_flags {
179 struct acpi_video_device_cap {
180 u8 _ADR:1; /* Return the unique ID */
181 u8 _BCL:1; /* Query list of brightness control levels supported */
182 u8 _BCM:1; /* Set the brightness level */
183 u8 _BQC:1; /* Get current brightness level */
184 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
185 u8 _DDC:1; /* Return the EDID for this device */
188 struct acpi_video_brightness_flags {
189 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
190 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
191 u8 _BQC_use_index:1; /* _BQC returns an index value */
194 struct acpi_video_device_brightness {
198 struct acpi_video_brightness_flags flags;
201 struct acpi_video_device {
202 unsigned long device_id;
203 struct acpi_video_device_flags flags;
204 struct acpi_video_device_cap cap;
205 struct list_head entry;
206 struct acpi_video_bus *video;
207 struct acpi_device *dev;
208 struct acpi_video_device_brightness *brightness;
209 struct backlight_device *backlight;
210 struct thermal_cooling_device *cooling_dev;
213 static const char device_decode[][30] = {
214 "motherboard VGA device",
220 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
221 static void acpi_video_device_rebind(struct acpi_video_bus *video);
222 static void acpi_video_device_bind(struct acpi_video_bus *video,
223 struct acpi_video_device *device);
224 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
225 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
227 static int acpi_video_device_lcd_get_level_current(
228 struct acpi_video_device *device,
229 unsigned long long *level, bool raw);
230 static int acpi_video_get_next_level(struct acpi_video_device *device,
231 u32 level_current, u32 event);
232 static int acpi_video_switch_brightness(struct acpi_video_device *device,
235 static bool acpi_video_use_native_backlight(void)
237 if (use_native_backlight_param != -1)
238 return use_native_backlight_param;
240 return use_native_backlight_dmi;
243 static bool acpi_video_verify_backlight_support(void)
245 if (acpi_osi_is_win8() && acpi_video_use_native_backlight() &&
246 backlight_device_registered(BACKLIGHT_RAW))
248 return acpi_video_backlight_support();
251 /* backlight device sysfs support */
252 static int acpi_video_get_brightness(struct backlight_device *bd)
254 unsigned long long cur_level;
256 struct acpi_video_device *vd = bl_get_data(bd);
258 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
260 for (i = 2; i < vd->brightness->count; i++) {
261 if (vd->brightness->levels[i] == cur_level)
263 * The first two entries are special - see page 575
264 * of the ACPI spec 3.0
271 static int acpi_video_set_brightness(struct backlight_device *bd)
273 int request_level = bd->props.brightness + 2;
274 struct acpi_video_device *vd = bl_get_data(bd);
276 return acpi_video_device_lcd_set_level(vd,
277 vd->brightness->levels[request_level]);
280 static const struct backlight_ops acpi_backlight_ops = {
281 .get_brightness = acpi_video_get_brightness,
282 .update_status = acpi_video_set_brightness,
285 /* thermal cooling device callbacks */
286 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
289 struct acpi_device *device = cooling_dev->devdata;
290 struct acpi_video_device *video = acpi_driver_data(device);
292 *state = video->brightness->count - 3;
296 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
299 struct acpi_device *device = cooling_dev->devdata;
300 struct acpi_video_device *video = acpi_driver_data(device);
301 unsigned long long level;
304 if (acpi_video_device_lcd_get_level_current(video, &level, false))
306 for (offset = 2; offset < video->brightness->count; offset++)
307 if (level == video->brightness->levels[offset]) {
308 *state = video->brightness->count - offset - 1;
316 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
318 struct acpi_device *device = cooling_dev->devdata;
319 struct acpi_video_device *video = acpi_driver_data(device);
322 if (state >= video->brightness->count - 2)
325 state = video->brightness->count - state;
326 level = video->brightness->levels[state - 1];
327 return acpi_video_device_lcd_set_level(video, level);
330 static const struct thermal_cooling_device_ops video_cooling_ops = {
331 .get_max_state = video_get_max_state,
332 .get_cur_state = video_get_cur_state,
333 .set_cur_state = video_set_cur_state,
337 * --------------------------------------------------------------------------
339 * --------------------------------------------------------------------------
343 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
344 union acpi_object **levels)
347 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
348 union acpi_object *obj;
353 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
354 if (!ACPI_SUCCESS(status))
356 obj = (union acpi_object *)buffer.pointer;
357 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
358 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
368 kfree(buffer.pointer);
374 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
379 status = acpi_execute_simple_method(device->dev->handle,
381 if (ACPI_FAILURE(status)) {
382 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
386 device->brightness->curr = level;
387 for (state = 2; state < device->brightness->count; state++)
388 if (level == device->brightness->levels[state]) {
389 if (device->backlight)
390 device->backlight->props.brightness = state - 2;
394 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
399 * For some buggy _BQC methods, we need to add a constant value to
400 * the _BQC return value to get the actual current brightness level
403 static int bqc_offset_aml_bug_workaround;
404 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
406 bqc_offset_aml_bug_workaround = 9;
410 static int __init video_set_use_native_backlight(const struct dmi_system_id *d)
412 use_native_backlight_dmi = true;
416 static struct dmi_system_id video_dmi_table[] __initdata = {
418 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
421 .callback = video_set_bqc_offset,
422 .ident = "Acer Aspire 5720",
424 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
425 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
429 .callback = video_set_bqc_offset,
430 .ident = "Acer Aspire 5710Z",
432 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
433 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
437 .callback = video_set_bqc_offset,
438 .ident = "eMachines E510",
440 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
441 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
445 .callback = video_set_bqc_offset,
446 .ident = "Acer Aspire 5315",
448 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
449 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
453 .callback = video_set_bqc_offset,
454 .ident = "Acer Aspire 7720",
456 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
457 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
461 .callback = video_set_use_native_backlight,
462 .ident = "ThinkPad T430 and T430s",
464 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
465 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T430"),
469 .callback = video_set_use_native_backlight,
470 .ident = "ThinkPad X230",
472 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
473 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X230"),
477 .callback = video_set_use_native_backlight,
478 .ident = "ThinkPad X1 Carbon",
480 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
481 DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X1 Carbon"),
485 .callback = video_set_use_native_backlight,
486 .ident = "Lenovo Yoga 13",
488 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
489 DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo IdeaPad Yoga 13"),
493 .callback = video_set_use_native_backlight,
494 .ident = "Dell Inspiron 7520",
496 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
497 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7520"),
501 .callback = video_set_use_native_backlight,
502 .ident = "Acer Aspire 5733Z",
504 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
505 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5733Z"),
509 .callback = video_set_use_native_backlight,
510 .ident = "Acer Aspire 5742G",
512 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
513 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5742G"),
517 .callback = video_set_use_native_backlight,
518 .ident = "Acer Aspire V5-431",
520 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
521 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-431"),
525 .callback = video_set_use_native_backlight,
526 .ident = "HP ProBook 4340s",
528 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
529 DMI_MATCH(DMI_PRODUCT_VERSION, "HP ProBook 4340s"),
533 .callback = video_set_use_native_backlight,
534 .ident = "HP ProBook 2013 models",
536 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
537 DMI_MATCH(DMI_PRODUCT_NAME, "HP ProBook "),
538 DMI_MATCH(DMI_PRODUCT_NAME, " G1"),
542 .callback = video_set_use_native_backlight,
543 .ident = "HP EliteBook 2013 models",
545 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
546 DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook "),
547 DMI_MATCH(DMI_PRODUCT_NAME, " G1"),
551 .callback = video_set_use_native_backlight,
552 .ident = "HP ZBook 14",
554 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
555 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 14"),
559 .callback = video_set_use_native_backlight,
560 .ident = "HP ZBook 15",
562 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
563 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 15"),
567 .callback = video_set_use_native_backlight,
568 .ident = "HP ZBook 17",
570 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
571 DMI_MATCH(DMI_PRODUCT_NAME, "HP ZBook 17"),
575 .callback = video_set_use_native_backlight,
576 .ident = "HP EliteBook 8780w",
578 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
579 DMI_MATCH(DMI_PRODUCT_NAME, "HP EliteBook 8780w"),
585 static unsigned long long
586 acpi_video_bqc_value_to_level(struct acpi_video_device *device,
587 unsigned long long bqc_value)
589 unsigned long long level;
591 if (device->brightness->flags._BQC_use_index) {
593 * _BQC returns an index that doesn't account for
594 * the first 2 items with special meaning, so we need
595 * to compensate for that by offsetting ourselves
597 if (device->brightness->flags._BCL_reversed)
598 bqc_value = device->brightness->count - 3 - bqc_value;
600 level = device->brightness->levels[bqc_value + 2];
605 level += bqc_offset_aml_bug_workaround;
611 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
612 unsigned long long *level, bool raw)
614 acpi_status status = AE_OK;
617 if (device->cap._BQC || device->cap._BCQ) {
618 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
620 status = acpi_evaluate_integer(device->dev->handle, buf,
622 if (ACPI_SUCCESS(status)) {
625 * Caller has indicated he wants the raw
626 * value returned by _BQC, so don't furtherly
627 * mess with the value.
632 *level = acpi_video_bqc_value_to_level(device, *level);
634 for (i = 2; i < device->brightness->count; i++)
635 if (device->brightness->levels[i] == *level) {
636 device->brightness->curr = *level;
640 * BQC returned an invalid level.
643 ACPI_WARNING((AE_INFO,
644 "%s returned an invalid level",
646 device->cap._BQC = device->cap._BCQ = 0;
650 * should we return an error or ignore this failure?
651 * dev->brightness->curr is a cached value which stores
652 * the correct current backlight level in most cases.
653 * ACPI video backlight still works w/ buggy _BQC.
654 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
656 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
657 device->cap._BQC = device->cap._BCQ = 0;
661 *level = device->brightness->curr;
666 acpi_video_device_EDID(struct acpi_video_device *device,
667 union acpi_object **edid, ssize_t length)
670 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
671 union acpi_object *obj;
672 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
673 struct acpi_object_list args = { 1, &arg0 };
681 arg0.integer.value = 1;
682 else if (length == 256)
683 arg0.integer.value = 2;
687 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
688 if (ACPI_FAILURE(status))
691 obj = buffer.pointer;
693 if (obj && obj->type == ACPI_TYPE_BUFFER)
696 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
708 * video : video bus device pointer
710 * 0. The system BIOS should NOT automatically switch(toggle)
711 * the active display output.
712 * 1. The system BIOS should automatically switch (toggle) the
713 * active display output. No switch event.
714 * 2. The _DGS value should be locked.
715 * 3. The system BIOS should not automatically switch (toggle) the
716 * active display output, but instead generate the display switch
719 * 0. The system BIOS should automatically control the brightness level
720 * of the LCD when the power changes from AC to DC
721 * 1. The system BIOS should NOT automatically control the brightness
722 * level of the LCD when the power changes from AC to DC.
728 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
732 if (!video->cap._DOS)
735 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
737 video->dos_setting = (lcd_flag << 2) | bios_flag;
738 status = acpi_execute_simple_method(video->device->handle, "_DOS",
739 (lcd_flag << 2) | bios_flag);
740 if (ACPI_FAILURE(status))
747 * Simple comparison function used to sort backlight levels.
751 acpi_video_cmp_level(const void *a, const void *b)
753 return *(int *)a - *(int *)b;
757 * Decides if _BQC/_BCQ for this system is usable
759 * We do this by changing the level first and then read out the current
760 * brightness level, if the value does not match, find out if it is using
761 * index. If not, clear the _BQC/_BCQ capability.
763 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
764 int max_level, int current_level)
766 struct acpi_video_device_brightness *br = device->brightness;
768 unsigned long long level;
771 /* don't mess with existing known broken systems */
772 if (bqc_offset_aml_bug_workaround)
776 * Some systems always report current brightness level as maximum
777 * through _BQC, we need to test another value for them.
779 test_level = current_level == max_level ? br->levels[3] : max_level;
781 result = acpi_video_device_lcd_set_level(device, test_level);
785 result = acpi_video_device_lcd_get_level_current(device, &level, true);
789 if (level != test_level) {
790 /* buggy _BQC found, need to find out if it uses index */
791 if (level < br->count) {
792 if (br->flags._BCL_reversed)
793 level = br->count - 3 - level;
794 if (br->levels[level + 2] == test_level)
795 br->flags._BQC_use_index = 1;
798 if (!br->flags._BQC_use_index)
799 device->cap._BQC = device->cap._BCQ = 0;
808 * device : video output device (LCD, CRT, ..)
811 * Maximum brightness level
813 * Allocate and initialize device->brightness.
817 acpi_video_init_brightness(struct acpi_video_device *device)
819 union acpi_object *obj = NULL;
820 int i, max_level = 0, count = 0, level_ac_battery = 0;
821 unsigned long long level, level_old;
822 union acpi_object *o;
823 struct acpi_video_device_brightness *br = NULL;
824 int result = -EINVAL;
827 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
828 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
829 "LCD brightness level\n"));
833 if (obj->package.count < 2)
836 br = kzalloc(sizeof(*br), GFP_KERNEL);
838 printk(KERN_ERR "can't allocate memory\n");
843 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
850 for (i = 0; i < obj->package.count; i++) {
851 o = (union acpi_object *)&obj->package.elements[i];
852 if (o->type != ACPI_TYPE_INTEGER) {
853 printk(KERN_ERR PREFIX "Invalid data\n");
856 value = (u32) o->integer.value;
857 /* Skip duplicate entries */
858 if (count > 2 && br->levels[count - 1] == value)
861 br->levels[count] = value;
863 if (br->levels[count] > max_level)
864 max_level = br->levels[count];
869 * some buggy BIOS don't export the levels
870 * when machine is on AC/Battery in _BCL package.
871 * In this case, the first two elements in _BCL packages
872 * are also supported brightness levels that OS should take care of.
874 for (i = 2; i < count; i++) {
875 if (br->levels[i] == br->levels[0])
877 if (br->levels[i] == br->levels[1])
881 if (level_ac_battery < 2) {
882 level_ac_battery = 2 - level_ac_battery;
883 br->flags._BCL_no_ac_battery_levels = 1;
884 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
885 br->levels[i] = br->levels[i - level_ac_battery];
886 count += level_ac_battery;
887 } else if (level_ac_battery > 2)
888 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
890 /* Check if the _BCL package is in a reversed order */
891 if (max_level == br->levels[2]) {
892 br->flags._BCL_reversed = 1;
893 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
894 acpi_video_cmp_level, NULL);
895 } else if (max_level != br->levels[count - 1])
897 "Found unordered _BCL package"));
900 device->brightness = br;
902 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
903 br->curr = level = max_level;
905 if (!device->cap._BQC)
908 result = acpi_video_device_lcd_get_level_current(device,
911 goto out_free_levels;
913 result = acpi_video_bqc_quirk(device, max_level, level_old);
915 goto out_free_levels;
917 * cap._BQC may get cleared due to _BQC is found to be broken
918 * in acpi_video_bqc_quirk, so check again here.
920 if (!device->cap._BQC)
923 level = acpi_video_bqc_value_to_level(device, level_old);
925 * On some buggy laptops, _BQC returns an uninitialized
926 * value when invoked for the first time, i.e.
927 * level_old is invalid (no matter whether it's a level
928 * or an index). Set the backlight to max_level in this case.
930 for (i = 2; i < br->count; i++)
931 if (level == br->levels[i])
933 if (i == br->count || !level)
937 result = acpi_video_device_lcd_set_level(device, level);
939 goto out_free_levels;
941 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
942 "found %d brightness levels\n", count - 2));
951 device->brightness = NULL;
958 * device : video output device (LCD, CRT, ..)
963 * Find out all required AML methods defined under the output
967 static void acpi_video_device_find_cap(struct acpi_video_device *device)
969 if (acpi_has_method(device->dev->handle, "_ADR"))
970 device->cap._ADR = 1;
971 if (acpi_has_method(device->dev->handle, "_BCL"))
972 device->cap._BCL = 1;
973 if (acpi_has_method(device->dev->handle, "_BCM"))
974 device->cap._BCM = 1;
975 if (acpi_has_method(device->dev->handle, "_BQC")) {
976 device->cap._BQC = 1;
977 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
978 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
979 device->cap._BCQ = 1;
982 if (acpi_has_method(device->dev->handle, "_DDC"))
983 device->cap._DDC = 1;
988 * device : video output device (VGA)
993 * Find out all required AML methods defined under the video bus device.
996 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
998 if (acpi_has_method(video->device->handle, "_DOS"))
1000 if (acpi_has_method(video->device->handle, "_DOD"))
1001 video->cap._DOD = 1;
1002 if (acpi_has_method(video->device->handle, "_ROM"))
1003 video->cap._ROM = 1;
1004 if (acpi_has_method(video->device->handle, "_GPD"))
1005 video->cap._GPD = 1;
1006 if (acpi_has_method(video->device->handle, "_SPD"))
1007 video->cap._SPD = 1;
1008 if (acpi_has_method(video->device->handle, "_VPO"))
1009 video->cap._VPO = 1;
1013 * Check whether the video bus device has required AML method to
1014 * support the desired features
1017 static int acpi_video_bus_check(struct acpi_video_bus *video)
1019 acpi_status status = -ENOENT;
1020 struct pci_dev *dev;
1025 dev = acpi_get_pci_dev(video->device->handle);
1031 * Since there is no HID, CID and so on for VGA driver, we have
1032 * to check well known required nodes.
1035 /* Does this device support video switching? */
1036 if (video->cap._DOS || video->cap._DOD) {
1037 if (!video->cap._DOS) {
1038 printk(KERN_WARNING FW_BUG
1039 "ACPI(%s) defines _DOD but not _DOS\n",
1040 acpi_device_bid(video->device));
1042 video->flags.multihead = 1;
1046 /* Does this device support retrieving a video ROM? */
1047 if (video->cap._ROM) {
1048 video->flags.rom = 1;
1052 /* Does this device support configuring which video device to POST? */
1053 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1054 video->flags.post = 1;
1062 * --------------------------------------------------------------------------
1064 * --------------------------------------------------------------------------
1067 /* device interface */
1068 static struct acpi_video_device_attrib *
1069 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1071 struct acpi_video_enumerated_device *ids;
1074 for (i = 0; i < video->attached_count; i++) {
1075 ids = &video->attached_array[i];
1076 if ((ids->value.int_val & 0xffff) == device_id)
1077 return &ids->value.attrib;
1084 acpi_video_get_device_type(struct acpi_video_bus *video,
1085 unsigned long device_id)
1087 struct acpi_video_enumerated_device *ids;
1090 for (i = 0; i < video->attached_count; i++) {
1091 ids = &video->attached_array[i];
1092 if ((ids->value.int_val & 0xffff) == device_id)
1093 return ids->value.int_val;
1100 acpi_video_bus_get_one_device(struct acpi_device *device,
1101 struct acpi_video_bus *video)
1103 unsigned long long device_id;
1104 int status, device_type;
1105 struct acpi_video_device *data;
1106 struct acpi_video_device_attrib *attribute;
1109 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1110 /* Some device omits _ADR, we skip them instead of fail */
1111 if (ACPI_FAILURE(status))
1114 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1118 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1119 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1120 device->driver_data = data;
1122 data->device_id = device_id;
1123 data->video = video;
1126 attribute = acpi_video_get_device_attr(video, device_id);
1128 if (attribute && attribute->device_id_scheme) {
1129 switch (attribute->display_type) {
1130 case ACPI_VIDEO_DISPLAY_CRT:
1131 data->flags.crt = 1;
1133 case ACPI_VIDEO_DISPLAY_TV:
1134 data->flags.tvout = 1;
1136 case ACPI_VIDEO_DISPLAY_DVI:
1137 data->flags.dvi = 1;
1139 case ACPI_VIDEO_DISPLAY_LCD:
1140 data->flags.lcd = 1;
1143 data->flags.unknown = 1;
1146 if (attribute->bios_can_detect)
1147 data->flags.bios = 1;
1149 /* Check for legacy IDs */
1150 device_type = acpi_video_get_device_type(video, device_id);
1151 /* Ignore bits 16 and 18-20 */
1152 switch (device_type & 0xffe2ffff) {
1153 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1154 data->flags.crt = 1;
1156 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1157 data->flags.lcd = 1;
1159 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1160 data->flags.tvout = 1;
1163 data->flags.unknown = 1;
1167 acpi_video_device_bind(video, data);
1168 acpi_video_device_find_cap(data);
1170 mutex_lock(&video->device_list_lock);
1171 list_add_tail(&data->entry, &video->video_device_list);
1172 mutex_unlock(&video->device_list_lock);
1179 * video : video bus device
1184 * Enumerate the video device list of the video bus,
1185 * bind the ids with the corresponding video devices
1186 * under the video bus.
1189 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1191 struct acpi_video_device *dev;
1193 mutex_lock(&video->device_list_lock);
1195 list_for_each_entry(dev, &video->video_device_list, entry)
1196 acpi_video_device_bind(video, dev);
1198 mutex_unlock(&video->device_list_lock);
1203 * video : video bus device
1204 * device : video output device under the video
1210 * Bind the ids with the corresponding video devices
1211 * under the video bus.
1215 acpi_video_device_bind(struct acpi_video_bus *video,
1216 struct acpi_video_device *device)
1218 struct acpi_video_enumerated_device *ids;
1221 for (i = 0; i < video->attached_count; i++) {
1222 ids = &video->attached_array[i];
1223 if (device->device_id == (ids->value.int_val & 0xffff)) {
1224 ids->bind_info = device;
1225 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1232 * video : video bus device
1237 * Call _DOD to enumerate all devices attached to display adapter
1241 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1246 struct acpi_video_enumerated_device *active_list;
1247 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1248 union acpi_object *dod = NULL;
1249 union acpi_object *obj;
1251 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1252 if (!ACPI_SUCCESS(status)) {
1253 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1257 dod = buffer.pointer;
1258 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1259 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1265 dod->package.count));
1267 active_list = kcalloc(1 + dod->package.count,
1268 sizeof(struct acpi_video_enumerated_device),
1276 for (i = 0; i < dod->package.count; i++) {
1277 obj = &dod->package.elements[i];
1279 if (obj->type != ACPI_TYPE_INTEGER) {
1280 printk(KERN_ERR PREFIX
1281 "Invalid _DOD data in element %d\n", i);
1285 active_list[count].value.int_val = obj->integer.value;
1286 active_list[count].bind_info = NULL;
1287 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1288 (int)obj->integer.value));
1292 kfree(video->attached_array);
1294 video->attached_array = active_list;
1295 video->attached_count = count;
1298 kfree(buffer.pointer);
1303 acpi_video_get_next_level(struct acpi_video_device *device,
1304 u32 level_current, u32 event)
1306 int min, max, min_above, max_below, i, l, delta = 255;
1307 max = max_below = 0;
1308 min = min_above = 255;
1309 /* Find closest level to level_current */
1310 for (i = 2; i < device->brightness->count; i++) {
1311 l = device->brightness->levels[i];
1312 if (abs(l - level_current) < abs(delta)) {
1313 delta = l - level_current;
1318 /* Ajust level_current to closest available level */
1319 level_current += delta;
1320 for (i = 2; i < device->brightness->count; i++) {
1321 l = device->brightness->levels[i];
1326 if (l < min_above && l > level_current)
1328 if (l > max_below && l < level_current)
1333 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1334 return (level_current < max) ? min_above : min;
1335 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1336 return (level_current < max) ? min_above : max;
1337 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1338 return (level_current > min) ? max_below : min;
1339 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1340 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1343 return level_current;
1348 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1350 unsigned long long level_current, level_next;
1351 int result = -EINVAL;
1353 /* no warning message if acpi_backlight=vendor or a quirk is used */
1354 if (!acpi_video_verify_backlight_support())
1357 if (!device->brightness)
1360 result = acpi_video_device_lcd_get_level_current(device,
1366 level_next = acpi_video_get_next_level(device, level_current, event);
1368 result = acpi_video_device_lcd_set_level(device, level_next);
1371 backlight_force_update(device->backlight,
1372 BACKLIGHT_UPDATE_HOTKEY);
1376 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1381 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1384 struct acpi_video_bus *video;
1385 struct acpi_video_device *video_device;
1386 union acpi_object *buffer = NULL;
1390 if (!device || !acpi_driver_data(device))
1393 video = acpi_driver_data(device);
1395 for (i = 0; i < video->attached_count; i++) {
1396 video_device = video->attached_array[i].bind_info;
1402 if (!video_device->cap._DDC)
1407 case ACPI_VIDEO_DISPLAY_CRT:
1408 if (!video_device->flags.crt)
1411 case ACPI_VIDEO_DISPLAY_TV:
1412 if (!video_device->flags.tvout)
1415 case ACPI_VIDEO_DISPLAY_DVI:
1416 if (!video_device->flags.dvi)
1419 case ACPI_VIDEO_DISPLAY_LCD:
1420 if (!video_device->flags.lcd)
1424 } else if (video_device->device_id != device_id) {
1428 status = acpi_video_device_EDID(video_device, &buffer, length);
1430 if (ACPI_FAILURE(status) || !buffer ||
1431 buffer->type != ACPI_TYPE_BUFFER) {
1433 status = acpi_video_device_EDID(video_device, &buffer,
1435 if (ACPI_FAILURE(status) || !buffer ||
1436 buffer->type != ACPI_TYPE_BUFFER) {
1441 *edid = buffer->buffer.pointer;
1447 EXPORT_SYMBOL(acpi_video_get_edid);
1450 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1451 struct acpi_device *device)
1454 struct acpi_device *dev;
1457 * There are systems where video module known to work fine regardless
1458 * of broken _DOD and ignoring returned value here doesn't cause
1461 acpi_video_device_enumerate(video);
1463 list_for_each_entry(dev, &device->children, node) {
1465 status = acpi_video_bus_get_one_device(dev, video);
1467 dev_err(&dev->dev, "Can't attach device\n");
1474 /* acpi_video interface */
1477 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1478 * preform any automatic brightness change on receiving a notification.
1480 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1482 return acpi_video_bus_DOS(video, 0,
1483 acpi_osi_is_win8() ? 1 : 0);
1486 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1488 return acpi_video_bus_DOS(video, 0,
1489 acpi_osi_is_win8() ? 0 : 1);
1492 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1494 struct acpi_video_bus *video = acpi_driver_data(device);
1495 struct input_dev *input;
1498 if (!video || !video->input)
1501 input = video->input;
1504 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1505 * most likely via hotkey. */
1506 keycode = KEY_SWITCHVIDEOMODE;
1509 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1511 acpi_video_device_enumerate(video);
1512 acpi_video_device_rebind(video);
1513 keycode = KEY_SWITCHVIDEOMODE;
1516 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1517 keycode = KEY_SWITCHVIDEOMODE;
1519 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1520 keycode = KEY_VIDEO_NEXT;
1522 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1523 keycode = KEY_VIDEO_PREV;
1527 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1528 "Unsupported event [0x%x]\n", event));
1532 if (acpi_notifier_call_chain(device, event, 0))
1533 /* Something vetoed the keypress. */
1537 input_report_key(input, keycode, 1);
1539 input_report_key(input, keycode, 0);
1546 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1548 struct acpi_video_device *video_device = data;
1549 struct acpi_device *device = NULL;
1550 struct acpi_video_bus *bus;
1551 struct input_dev *input;
1557 device = video_device->dev;
1558 bus = video_device->video;
1562 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1563 if (brightness_switch_enabled)
1564 acpi_video_switch_brightness(video_device, event);
1565 keycode = KEY_BRIGHTNESS_CYCLE;
1567 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1568 if (brightness_switch_enabled)
1569 acpi_video_switch_brightness(video_device, event);
1570 keycode = KEY_BRIGHTNESSUP;
1572 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1573 if (brightness_switch_enabled)
1574 acpi_video_switch_brightness(video_device, event);
1575 keycode = KEY_BRIGHTNESSDOWN;
1577 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
1578 if (brightness_switch_enabled)
1579 acpi_video_switch_brightness(video_device, event);
1580 keycode = KEY_BRIGHTNESS_ZERO;
1582 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1583 if (brightness_switch_enabled)
1584 acpi_video_switch_brightness(video_device, event);
1585 keycode = KEY_DISPLAY_OFF;
1588 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1589 "Unsupported event [0x%x]\n", event));
1593 acpi_notifier_call_chain(device, event, 0);
1596 input_report_key(input, keycode, 1);
1598 input_report_key(input, keycode, 0);
1605 static int acpi_video_resume(struct notifier_block *nb,
1606 unsigned long val, void *ign)
1608 struct acpi_video_bus *video;
1609 struct acpi_video_device *video_device;
1613 case PM_HIBERNATION_PREPARE:
1614 case PM_SUSPEND_PREPARE:
1615 case PM_RESTORE_PREPARE:
1619 video = container_of(nb, struct acpi_video_bus, pm_nb);
1621 dev_info(&video->device->dev, "Restoring backlight state\n");
1623 for (i = 0; i < video->attached_count; i++) {
1624 video_device = video->attached_array[i].bind_info;
1625 if (video_device && video_device->backlight)
1626 acpi_video_set_brightness(video_device->backlight);
1633 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1634 void **return_value)
1636 struct acpi_device *device = context;
1637 struct acpi_device *sibling;
1640 if (handle == device->handle)
1641 return AE_CTRL_TERMINATE;
1643 result = acpi_bus_get_device(handle, &sibling);
1647 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1648 return AE_ALREADY_EXISTS;
1653 static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1655 if (acpi_video_verify_backlight_support()) {
1656 struct backlight_properties props;
1657 struct pci_dev *pdev;
1658 acpi_handle acpi_parent;
1659 struct device *parent = NULL;
1664 result = acpi_video_init_brightness(device);
1667 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1672 acpi_get_parent(device->dev->handle, &acpi_parent);
1674 pdev = acpi_get_pci_dev(acpi_parent);
1676 parent = &pdev->dev;
1680 memset(&props, 0, sizeof(struct backlight_properties));
1681 props.type = BACKLIGHT_FIRMWARE;
1682 props.max_brightness = device->brightness->count - 3;
1683 device->backlight = backlight_device_register(name,
1686 &acpi_backlight_ops,
1689 if (IS_ERR(device->backlight))
1693 * Save current brightness level in case we have to restore it
1694 * before acpi_video_device_lcd_set_level() is called next time.
1696 device->backlight->props.brightness =
1697 acpi_video_get_brightness(device->backlight);
1699 device->cooling_dev = thermal_cooling_device_register("LCD",
1700 device->dev, &video_cooling_ops);
1701 if (IS_ERR(device->cooling_dev)) {
1703 * Set cooling_dev to NULL so we don't crash trying to
1705 * Also, why the hell we are returning early and
1706 * not attempt to register video output if cooling
1707 * device registration failed?
1710 device->cooling_dev = NULL;
1714 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1715 device->cooling_dev->id);
1716 result = sysfs_create_link(&device->dev->dev.kobj,
1717 &device->cooling_dev->device.kobj,
1720 printk(KERN_ERR PREFIX "Create sysfs link\n");
1721 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1722 &device->dev->dev.kobj, "device");
1724 printk(KERN_ERR PREFIX "Create sysfs link\n");
1728 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1730 struct acpi_video_device *dev;
1732 mutex_lock(&video->device_list_lock);
1733 list_for_each_entry(dev, &video->video_device_list, entry)
1734 acpi_video_dev_register_backlight(dev);
1735 mutex_unlock(&video->device_list_lock);
1737 video->pm_nb.notifier_call = acpi_video_resume;
1738 video->pm_nb.priority = 0;
1739 return register_pm_notifier(&video->pm_nb);
1742 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1744 if (device->backlight) {
1745 backlight_device_unregister(device->backlight);
1746 device->backlight = NULL;
1748 if (device->brightness) {
1749 kfree(device->brightness->levels);
1750 kfree(device->brightness);
1751 device->brightness = NULL;
1753 if (device->cooling_dev) {
1754 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1755 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1756 thermal_cooling_device_unregister(device->cooling_dev);
1757 device->cooling_dev = NULL;
1761 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1763 struct acpi_video_device *dev;
1764 int error = unregister_pm_notifier(&video->pm_nb);
1766 mutex_lock(&video->device_list_lock);
1767 list_for_each_entry(dev, &video->video_device_list, entry)
1768 acpi_video_dev_unregister_backlight(dev);
1769 mutex_unlock(&video->device_list_lock);
1774 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1777 struct acpi_device *adev = device->dev;
1779 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1780 acpi_video_device_notify, device);
1781 if (ACPI_FAILURE(status))
1782 dev_err(&adev->dev, "Error installing notify handler\n");
1784 device->flags.notify = 1;
1787 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1789 struct input_dev *input;
1790 struct acpi_video_device *dev;
1793 video->input = input = input_allocate_device();
1799 error = acpi_video_bus_start_devices(video);
1801 goto err_free_input;
1803 snprintf(video->phys, sizeof(video->phys),
1804 "%s/video/input0", acpi_device_hid(video->device));
1806 input->name = acpi_device_name(video->device);
1807 input->phys = video->phys;
1808 input->id.bustype = BUS_HOST;
1809 input->id.product = 0x06;
1810 input->dev.parent = &video->device->dev;
1811 input->evbit[0] = BIT(EV_KEY);
1812 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1813 set_bit(KEY_VIDEO_NEXT, input->keybit);
1814 set_bit(KEY_VIDEO_PREV, input->keybit);
1815 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1816 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1817 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1818 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1819 set_bit(KEY_DISPLAY_OFF, input->keybit);
1821 error = input_register_device(input);
1825 mutex_lock(&video->device_list_lock);
1826 list_for_each_entry(dev, &video->video_device_list, entry)
1827 acpi_video_dev_add_notify_handler(dev);
1828 mutex_unlock(&video->device_list_lock);
1833 acpi_video_bus_stop_devices(video);
1835 input_free_device(input);
1836 video->input = NULL;
1841 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1843 if (dev->flags.notify) {
1844 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1845 acpi_video_device_notify);
1846 dev->flags.notify = 0;
1850 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1852 struct acpi_video_device *dev;
1854 mutex_lock(&video->device_list_lock);
1855 list_for_each_entry(dev, &video->video_device_list, entry)
1856 acpi_video_dev_remove_notify_handler(dev);
1857 mutex_unlock(&video->device_list_lock);
1859 acpi_video_bus_stop_devices(video);
1860 input_unregister_device(video->input);
1861 video->input = NULL;
1864 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1866 struct acpi_video_device *dev, *next;
1868 mutex_lock(&video->device_list_lock);
1869 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1870 list_del(&dev->entry);
1873 mutex_unlock(&video->device_list_lock);
1878 static int instance;
1880 static int acpi_video_bus_add(struct acpi_device *device)
1882 struct acpi_video_bus *video;
1886 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1887 device->parent->handle, 1,
1888 acpi_video_bus_match, NULL,
1890 if (status == AE_ALREADY_EXISTS) {
1891 printk(KERN_WARNING FW_BUG
1892 "Duplicate ACPI video bus devices for the"
1893 " same VGA controller, please try module "
1894 "parameter \"video.allow_duplicates=1\""
1895 "if the current driver doesn't work.\n");
1896 if (!allow_duplicates)
1900 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1904 /* a hack to fix the duplicate name "VID" problem on T61 */
1905 if (!strcmp(device->pnp.bus_id, "VID")) {
1907 device->pnp.bus_id[3] = '0' + instance;
1910 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1911 if (!strcmp(device->pnp.bus_id, "VGA")) {
1913 device->pnp.bus_id[3] = '0' + instance;
1917 video->device = device;
1918 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1919 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1920 device->driver_data = video;
1922 acpi_video_bus_find_cap(video);
1923 error = acpi_video_bus_check(video);
1925 goto err_free_video;
1927 mutex_init(&video->device_list_lock);
1928 INIT_LIST_HEAD(&video->video_device_list);
1930 error = acpi_video_bus_get_devices(video, device);
1934 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
1935 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1936 video->flags.multihead ? "yes" : "no",
1937 video->flags.rom ? "yes" : "no",
1938 video->flags.post ? "yes" : "no");
1939 mutex_lock(&video_list_lock);
1940 list_add_tail(&video->entry, &video_bus_head);
1941 mutex_unlock(&video_list_lock);
1943 acpi_video_bus_register_backlight(video);
1944 acpi_video_bus_add_notify_handler(video);
1949 acpi_video_bus_put_devices(video);
1950 kfree(video->attached_array);
1953 device->driver_data = NULL;
1958 static int acpi_video_bus_remove(struct acpi_device *device)
1960 struct acpi_video_bus *video = NULL;
1963 if (!device || !acpi_driver_data(device))
1966 video = acpi_driver_data(device);
1968 acpi_video_bus_remove_notify_handler(video);
1969 acpi_video_bus_unregister_backlight(video);
1970 acpi_video_bus_put_devices(video);
1972 mutex_lock(&video_list_lock);
1973 list_del(&video->entry);
1974 mutex_unlock(&video_list_lock);
1976 kfree(video->attached_array);
1982 static int __init is_i740(struct pci_dev *dev)
1984 if (dev->device == 0x00D1)
1986 if (dev->device == 0x7000)
1991 static int __init intel_opregion_present(void)
1994 struct pci_dev *dev = NULL;
1997 for_each_pci_dev(dev) {
1998 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2000 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2002 /* We don't want to poke around undefined i740 registers */
2005 pci_read_config_dword(dev, 0xfc, &address);
2013 int acpi_video_register(void)
2016 if (register_count) {
2018 * if the function of acpi_video_register is already called,
2019 * don't register the acpi_vide_bus again and return no error.
2024 mutex_init(&video_list_lock);
2025 INIT_LIST_HEAD(&video_bus_head);
2027 result = acpi_bus_register_driver(&acpi_video_bus);
2032 * When the acpi_video_bus is loaded successfully, increase
2033 * the counter reference.
2039 EXPORT_SYMBOL(acpi_video_register);
2041 void acpi_video_unregister(void)
2043 if (!register_count) {
2045 * If the acpi video bus is already unloaded, don't
2046 * unload it again and return directly.
2050 acpi_bus_unregister_driver(&acpi_video_bus);
2056 EXPORT_SYMBOL(acpi_video_unregister);
2059 * This is kind of nasty. Hardware using Intel chipsets may require
2060 * the video opregion code to be run first in order to initialise
2061 * state before any ACPI video calls are made. To handle this we defer
2062 * registration of the video class until the opregion code has run.
2065 static int __init acpi_video_init(void)
2067 dmi_check_system(video_dmi_table);
2069 if (intel_opregion_present())
2072 return acpi_video_register();
2075 static void __exit acpi_video_exit(void)
2077 acpi_video_unregister();
2082 module_init(acpi_video_init);
2083 module_exit(acpi_video_exit);