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 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
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
62 #define MAX_NAME_LEN 20
64 #define _COMPONENT ACPI_VIDEO_COMPONENT
65 ACPI_MODULE_NAME("video");
67 MODULE_AUTHOR("Bruno Ducrot");
68 MODULE_DESCRIPTION("ACPI Video Driver");
69 MODULE_LICENSE("GPL");
71 static bool brightness_switch_enabled
;
72 module_param(brightness_switch_enabled
, bool, 0644);
75 * By default, we don't allow duplicate ACPI video bus devices
76 * under the same VGA controller
78 static bool allow_duplicates
;
79 module_param(allow_duplicates
, bool, 0644);
82 * For Windows 8 systems: used to decide if video module
83 * should skip registering backlight interface of its own.
85 static int use_native_backlight_param
= -1;
86 module_param_named(use_native_backlight
, use_native_backlight_param
, int, 0444);
87 static bool use_native_backlight_dmi
= false;
89 static int register_count
;
90 static struct mutex video_list_lock
;
91 static struct list_head video_bus_head
;
92 static int acpi_video_bus_add(struct acpi_device
*device
);
93 static int acpi_video_bus_remove(struct acpi_device
*device
);
94 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
);
96 static const struct acpi_device_id video_device_ids
[] = {
100 MODULE_DEVICE_TABLE(acpi
, video_device_ids
);
102 static struct acpi_driver acpi_video_bus
= {
104 .class = ACPI_VIDEO_CLASS
,
105 .ids
= video_device_ids
,
107 .add
= acpi_video_bus_add
,
108 .remove
= acpi_video_bus_remove
,
109 .notify
= acpi_video_bus_notify
,
113 struct acpi_video_bus_flags
{
114 u8 multihead
:1; /* can switch video heads */
115 u8 rom
:1; /* can retrieve a video rom */
116 u8 post
:1; /* can configure the head to */
120 struct acpi_video_bus_cap
{
121 u8 _DOS
:1; /* Enable/Disable output switching */
122 u8 _DOD
:1; /* Enumerate all devices attached to display adapter */
123 u8 _ROM
:1; /* Get ROM Data */
124 u8 _GPD
:1; /* Get POST Device */
125 u8 _SPD
:1; /* Set POST Device */
126 u8 _VPO
:1; /* Video POST Options */
130 struct acpi_video_device_attrib
{
131 u32 display_index
:4; /* A zero-based instance of the Display */
132 u32 display_port_attachment
:4; /* This field differentiates the display type */
133 u32 display_type
:4; /* Describe the specific type in use */
134 u32 vendor_specific
:4; /* Chipset Vendor Specific */
135 u32 bios_can_detect
:1; /* BIOS can detect the device */
136 u32 depend_on_vga
:1; /* Non-VGA output device whose power is related to
138 u32 pipe_id
:3; /* For VGA multiple-head devices. */
139 u32 reserved
:10; /* Must be 0 */
140 u32 device_id_scheme
:1; /* Device ID Scheme */
143 struct acpi_video_enumerated_device
{
146 struct acpi_video_device_attrib attrib
;
148 struct acpi_video_device
*bind_info
;
151 struct acpi_video_bus
{
152 struct acpi_device
*device
;
153 bool backlight_registered
;
155 struct acpi_video_enumerated_device
*attached_array
;
157 struct acpi_video_bus_cap cap
;
158 struct acpi_video_bus_flags flags
;
159 struct list_head video_device_list
;
160 struct mutex device_list_lock
; /* protects video_device_list */
161 struct list_head entry
;
162 struct input_dev
*input
;
163 char phys
[32]; /* for input device */
164 struct notifier_block pm_nb
;
167 struct acpi_video_device_flags
{
178 struct acpi_video_device_cap
{
179 u8 _ADR
:1; /* Return the unique ID */
180 u8 _BCL
:1; /* Query list of brightness control levels supported */
181 u8 _BCM
:1; /* Set the brightness level */
182 u8 _BQC
:1; /* Get current brightness level */
183 u8 _BCQ
:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
184 u8 _DDC
:1; /* Return the EDID for this device */
187 struct acpi_video_brightness_flags
{
188 u8 _BCL_no_ac_battery_levels
:1; /* no AC/Battery levels in _BCL */
189 u8 _BCL_reversed
:1; /* _BCL package is in a reversed order */
190 u8 _BQC_use_index
:1; /* _BQC returns an index value */
193 struct acpi_video_device_brightness
{
197 struct acpi_video_brightness_flags flags
;
200 struct acpi_video_device
{
201 unsigned long device_id
;
202 struct acpi_video_device_flags flags
;
203 struct acpi_video_device_cap cap
;
204 struct list_head entry
;
205 struct acpi_video_bus
*video
;
206 struct acpi_device
*dev
;
207 struct acpi_video_device_brightness
*brightness
;
208 struct backlight_device
*backlight
;
209 struct thermal_cooling_device
*cooling_dev
;
212 static const char device_decode
[][30] = {
213 "motherboard VGA device",
219 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
);
220 static void acpi_video_device_rebind(struct acpi_video_bus
*video
);
221 static void acpi_video_device_bind(struct acpi_video_bus
*video
,
222 struct acpi_video_device
*device
);
223 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
);
224 static int acpi_video_device_lcd_set_level(struct acpi_video_device
*device
,
226 static int acpi_video_device_lcd_get_level_current(
227 struct acpi_video_device
*device
,
228 unsigned long long *level
, bool raw
);
229 static int acpi_video_get_next_level(struct acpi_video_device
*device
,
230 u32 level_current
, u32 event
);
231 static int acpi_video_switch_brightness(struct acpi_video_device
*device
,
234 static bool acpi_video_use_native_backlight(void)
236 if (use_native_backlight_param
!= -1)
237 return use_native_backlight_param
;
239 return use_native_backlight_dmi
;
242 static bool acpi_video_verify_backlight_support(void)
244 if (acpi_osi_is_win8() && acpi_video_use_native_backlight() &&
245 backlight_device_registered(BACKLIGHT_RAW
))
247 return acpi_video_backlight_support();
250 /* backlight device sysfs support */
251 static int acpi_video_get_brightness(struct backlight_device
*bd
)
253 unsigned long long cur_level
;
255 struct acpi_video_device
*vd
= bl_get_data(bd
);
257 if (acpi_video_device_lcd_get_level_current(vd
, &cur_level
, false))
259 for (i
= 2; i
< vd
->brightness
->count
; i
++) {
260 if (vd
->brightness
->levels
[i
] == cur_level
)
262 * The first two entries are special - see page 575
263 * of the ACPI spec 3.0
270 static int acpi_video_set_brightness(struct backlight_device
*bd
)
272 int request_level
= bd
->props
.brightness
+ 2;
273 struct acpi_video_device
*vd
= bl_get_data(bd
);
275 return acpi_video_device_lcd_set_level(vd
,
276 vd
->brightness
->levels
[request_level
]);
279 static const struct backlight_ops acpi_backlight_ops
= {
280 .get_brightness
= acpi_video_get_brightness
,
281 .update_status
= acpi_video_set_brightness
,
284 /* thermal cooling device callbacks */
285 static int video_get_max_state(struct thermal_cooling_device
*cooling_dev
, unsigned
288 struct acpi_device
*device
= cooling_dev
->devdata
;
289 struct acpi_video_device
*video
= acpi_driver_data(device
);
291 *state
= video
->brightness
->count
- 3;
295 static int video_get_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned
298 struct acpi_device
*device
= cooling_dev
->devdata
;
299 struct acpi_video_device
*video
= acpi_driver_data(device
);
300 unsigned long long level
;
303 if (acpi_video_device_lcd_get_level_current(video
, &level
, false))
305 for (offset
= 2; offset
< video
->brightness
->count
; offset
++)
306 if (level
== video
->brightness
->levels
[offset
]) {
307 *state
= video
->brightness
->count
- offset
- 1;
315 video_set_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned long state
)
317 struct acpi_device
*device
= cooling_dev
->devdata
;
318 struct acpi_video_device
*video
= acpi_driver_data(device
);
321 if (state
>= video
->brightness
->count
- 2)
324 state
= video
->brightness
->count
- state
;
325 level
= video
->brightness
->levels
[state
- 1];
326 return acpi_video_device_lcd_set_level(video
, level
);
329 static const struct thermal_cooling_device_ops video_cooling_ops
= {
330 .get_max_state
= video_get_max_state
,
331 .get_cur_state
= video_get_cur_state
,
332 .set_cur_state
= video_set_cur_state
,
336 * --------------------------------------------------------------------------
338 * --------------------------------------------------------------------------
342 acpi_video_device_lcd_query_levels(struct acpi_video_device
*device
,
343 union acpi_object
**levels
)
346 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
347 union acpi_object
*obj
;
352 status
= acpi_evaluate_object(device
->dev
->handle
, "_BCL", NULL
, &buffer
);
353 if (!ACPI_SUCCESS(status
))
355 obj
= (union acpi_object
*)buffer
.pointer
;
356 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
357 printk(KERN_ERR PREFIX
"Invalid _BCL data\n");
367 kfree(buffer
.pointer
);
373 acpi_video_device_lcd_set_level(struct acpi_video_device
*device
, int level
)
378 status
= acpi_execute_simple_method(device
->dev
->handle
,
380 if (ACPI_FAILURE(status
)) {
381 ACPI_ERROR((AE_INFO
, "Evaluating _BCM failed"));
385 device
->brightness
->curr
= level
;
386 for (state
= 2; state
< device
->brightness
->count
; state
++)
387 if (level
== device
->brightness
->levels
[state
]) {
388 if (device
->backlight
)
389 device
->backlight
->props
.brightness
= state
- 2;
393 ACPI_ERROR((AE_INFO
, "Current brightness invalid"));
398 * For some buggy _BQC methods, we need to add a constant value to
399 * the _BQC return value to get the actual current brightness level
402 static int bqc_offset_aml_bug_workaround
;
403 static int __init
video_set_bqc_offset(const struct dmi_system_id
*d
)
405 bqc_offset_aml_bug_workaround
= 9;
409 static int __init
video_set_use_native_backlight(const struct dmi_system_id
*d
)
411 use_native_backlight_dmi
= true;
415 static struct dmi_system_id video_dmi_table
[] __initdata
= {
417 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
420 .callback
= video_set_bqc_offset
,
421 .ident
= "Acer Aspire 5720",
423 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
424 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5720"),
428 .callback
= video_set_bqc_offset
,
429 .ident
= "Acer Aspire 5710Z",
431 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
432 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5710Z"),
436 .callback
= video_set_bqc_offset
,
437 .ident
= "eMachines E510",
439 DMI_MATCH(DMI_BOARD_VENDOR
, "EMACHINES"),
440 DMI_MATCH(DMI_PRODUCT_NAME
, "eMachines E510"),
444 .callback
= video_set_bqc_offset
,
445 .ident
= "Acer Aspire 5315",
447 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
448 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5315"),
452 .callback
= video_set_bqc_offset
,
453 .ident
= "Acer Aspire 7720",
455 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
456 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 7720"),
460 .callback
= video_set_use_native_backlight
,
461 .ident
= "ThinkPad T430 and T430s",
463 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
464 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad T430"),
468 .callback
= video_set_use_native_backlight
,
469 .ident
= "ThinkPad X230",
471 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
472 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad X230"),
476 .callback
= video_set_use_native_backlight
,
477 .ident
= "ThinkPad T530",
479 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
480 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad T530"),
484 .callback
= video_set_use_native_backlight
,
485 .ident
= "ThinkPad X1 Carbon",
487 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
488 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad X1 Carbon"),
492 .callback
= video_set_use_native_backlight
,
493 .ident
= "Lenovo Yoga 13",
495 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
496 DMI_MATCH(DMI_PRODUCT_VERSION
, "Lenovo IdeaPad Yoga 13"),
500 .callback
= video_set_use_native_backlight
,
501 .ident
= "Thinkpad Helix",
503 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
504 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad Helix"),
508 .callback
= video_set_use_native_backlight
,
509 .ident
= "Dell Inspiron 7520",
511 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
512 DMI_MATCH(DMI_PRODUCT_VERSION
, "Inspiron 7520"),
516 .callback
= video_set_use_native_backlight
,
517 .ident
= "Acer Aspire 5733Z",
519 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
520 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5733Z"),
524 .callback
= video_set_use_native_backlight
,
525 .ident
= "Acer Aspire 5742G",
527 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
528 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5742G"),
532 .callback
= video_set_use_native_backlight
,
533 .ident
= "Acer Aspire V5-431",
535 DMI_MATCH(DMI_SYS_VENDOR
, "Acer"),
536 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire V5-431"),
540 .callback
= video_set_use_native_backlight
,
541 .ident
= "HP ProBook 4340s",
543 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
544 DMI_MATCH(DMI_PRODUCT_VERSION
, "HP ProBook 4340s"),
548 .callback
= video_set_use_native_backlight
,
549 .ident
= "HP ProBook 2013 models",
551 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
552 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ProBook "),
553 DMI_MATCH(DMI_PRODUCT_NAME
, " G1"),
557 .callback
= video_set_use_native_backlight
,
558 .ident
= "HP EliteBook 2013 models",
560 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
561 DMI_MATCH(DMI_PRODUCT_NAME
, "HP EliteBook "),
562 DMI_MATCH(DMI_PRODUCT_NAME
, " G1"),
566 .callback
= video_set_use_native_backlight
,
567 .ident
= "HP ZBook 14",
569 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
570 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ZBook 14"),
574 .callback
= video_set_use_native_backlight
,
575 .ident
= "HP ZBook 15",
577 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
578 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ZBook 15"),
582 .callback
= video_set_use_native_backlight
,
583 .ident
= "HP ZBook 17",
585 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
586 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ZBook 17"),
590 .callback
= video_set_use_native_backlight
,
591 .ident
= "HP EliteBook 8780w",
593 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
594 DMI_MATCH(DMI_PRODUCT_NAME
, "HP EliteBook 8780w"),
600 static unsigned long long
601 acpi_video_bqc_value_to_level(struct acpi_video_device
*device
,
602 unsigned long long bqc_value
)
604 unsigned long long level
;
606 if (device
->brightness
->flags
._BQC_use_index
) {
608 * _BQC returns an index that doesn't account for
609 * the first 2 items with special meaning, so we need
610 * to compensate for that by offsetting ourselves
612 if (device
->brightness
->flags
._BCL_reversed
)
613 bqc_value
= device
->brightness
->count
- 3 - bqc_value
;
615 level
= device
->brightness
->levels
[bqc_value
+ 2];
620 level
+= bqc_offset_aml_bug_workaround
;
626 acpi_video_device_lcd_get_level_current(struct acpi_video_device
*device
,
627 unsigned long long *level
, bool raw
)
629 acpi_status status
= AE_OK
;
632 if (device
->cap
._BQC
|| device
->cap
._BCQ
) {
633 char *buf
= device
->cap
._BQC
? "_BQC" : "_BCQ";
635 status
= acpi_evaluate_integer(device
->dev
->handle
, buf
,
637 if (ACPI_SUCCESS(status
)) {
640 * Caller has indicated he wants the raw
641 * value returned by _BQC, so don't furtherly
642 * mess with the value.
647 *level
= acpi_video_bqc_value_to_level(device
, *level
);
649 for (i
= 2; i
< device
->brightness
->count
; i
++)
650 if (device
->brightness
->levels
[i
] == *level
) {
651 device
->brightness
->curr
= *level
;
655 * BQC returned an invalid level.
658 ACPI_WARNING((AE_INFO
,
659 "%s returned an invalid level",
661 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
665 * should we return an error or ignore this failure?
666 * dev->brightness->curr is a cached value which stores
667 * the correct current backlight level in most cases.
668 * ACPI video backlight still works w/ buggy _BQC.
669 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
671 ACPI_WARNING((AE_INFO
, "Evaluating %s failed", buf
));
672 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
676 *level
= device
->brightness
->curr
;
681 acpi_video_device_EDID(struct acpi_video_device
*device
,
682 union acpi_object
**edid
, ssize_t length
)
685 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
686 union acpi_object
*obj
;
687 union acpi_object arg0
= { ACPI_TYPE_INTEGER
};
688 struct acpi_object_list args
= { 1, &arg0
};
696 arg0
.integer
.value
= 1;
697 else if (length
== 256)
698 arg0
.integer
.value
= 2;
702 status
= acpi_evaluate_object(device
->dev
->handle
, "_DDC", &args
, &buffer
);
703 if (ACPI_FAILURE(status
))
706 obj
= buffer
.pointer
;
708 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
)
711 printk(KERN_ERR PREFIX
"Invalid _DDC data\n");
723 * video : video bus device pointer
725 * 0. The system BIOS should NOT automatically switch(toggle)
726 * the active display output.
727 * 1. The system BIOS should automatically switch (toggle) the
728 * active display output. No switch event.
729 * 2. The _DGS value should be locked.
730 * 3. The system BIOS should not automatically switch (toggle) the
731 * active display output, but instead generate the display switch
734 * 0. The system BIOS should automatically control the brightness level
735 * of the LCD when the power changes from AC to DC
736 * 1. The system BIOS should NOT automatically control the brightness
737 * level of the LCD when the power changes from AC to DC.
743 acpi_video_bus_DOS(struct acpi_video_bus
*video
, int bios_flag
, int lcd_flag
)
747 if (!video
->cap
._DOS
)
750 if (bios_flag
< 0 || bios_flag
> 3 || lcd_flag
< 0 || lcd_flag
> 1)
752 video
->dos_setting
= (lcd_flag
<< 2) | bios_flag
;
753 status
= acpi_execute_simple_method(video
->device
->handle
, "_DOS",
754 (lcd_flag
<< 2) | bios_flag
);
755 if (ACPI_FAILURE(status
))
762 * Simple comparison function used to sort backlight levels.
766 acpi_video_cmp_level(const void *a
, const void *b
)
768 return *(int *)a
- *(int *)b
;
772 * Decides if _BQC/_BCQ for this system is usable
774 * We do this by changing the level first and then read out the current
775 * brightness level, if the value does not match, find out if it is using
776 * index. If not, clear the _BQC/_BCQ capability.
778 static int acpi_video_bqc_quirk(struct acpi_video_device
*device
,
779 int max_level
, int current_level
)
781 struct acpi_video_device_brightness
*br
= device
->brightness
;
783 unsigned long long level
;
786 /* don't mess with existing known broken systems */
787 if (bqc_offset_aml_bug_workaround
)
791 * Some systems always report current brightness level as maximum
792 * through _BQC, we need to test another value for them.
794 test_level
= current_level
== max_level
? br
->levels
[3] : max_level
;
796 result
= acpi_video_device_lcd_set_level(device
, test_level
);
800 result
= acpi_video_device_lcd_get_level_current(device
, &level
, true);
804 if (level
!= test_level
) {
805 /* buggy _BQC found, need to find out if it uses index */
806 if (level
< br
->count
) {
807 if (br
->flags
._BCL_reversed
)
808 level
= br
->count
- 3 - level
;
809 if (br
->levels
[level
+ 2] == test_level
)
810 br
->flags
._BQC_use_index
= 1;
813 if (!br
->flags
._BQC_use_index
)
814 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
823 * device : video output device (LCD, CRT, ..)
826 * Maximum brightness level
828 * Allocate and initialize device->brightness.
832 acpi_video_init_brightness(struct acpi_video_device
*device
)
834 union acpi_object
*obj
= NULL
;
835 int i
, max_level
= 0, count
= 0, level_ac_battery
= 0;
836 unsigned long long level
, level_old
;
837 union acpi_object
*o
;
838 struct acpi_video_device_brightness
*br
= NULL
;
839 int result
= -EINVAL
;
842 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device
, &obj
))) {
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Could not query available "
844 "LCD brightness level\n"));
848 if (obj
->package
.count
< 2)
851 br
= kzalloc(sizeof(*br
), GFP_KERNEL
);
853 printk(KERN_ERR
"can't allocate memory\n");
858 br
->levels
= kmalloc((obj
->package
.count
+ 2) * sizeof *(br
->levels
),
865 for (i
= 0; i
< obj
->package
.count
; i
++) {
866 o
= (union acpi_object
*)&obj
->package
.elements
[i
];
867 if (o
->type
!= ACPI_TYPE_INTEGER
) {
868 printk(KERN_ERR PREFIX
"Invalid data\n");
871 value
= (u32
) o
->integer
.value
;
872 /* Skip duplicate entries */
873 if (count
> 2 && br
->levels
[count
- 1] == value
)
876 br
->levels
[count
] = value
;
878 if (br
->levels
[count
] > max_level
)
879 max_level
= br
->levels
[count
];
884 * some buggy BIOS don't export the levels
885 * when machine is on AC/Battery in _BCL package.
886 * In this case, the first two elements in _BCL packages
887 * are also supported brightness levels that OS should take care of.
889 for (i
= 2; i
< count
; i
++) {
890 if (br
->levels
[i
] == br
->levels
[0])
892 if (br
->levels
[i
] == br
->levels
[1])
896 if (level_ac_battery
< 2) {
897 level_ac_battery
= 2 - level_ac_battery
;
898 br
->flags
._BCL_no_ac_battery_levels
= 1;
899 for (i
= (count
- 1 + level_ac_battery
); i
>= 2; i
--)
900 br
->levels
[i
] = br
->levels
[i
- level_ac_battery
];
901 count
+= level_ac_battery
;
902 } else if (level_ac_battery
> 2)
903 ACPI_ERROR((AE_INFO
, "Too many duplicates in _BCL package"));
905 /* Check if the _BCL package is in a reversed order */
906 if (max_level
== br
->levels
[2]) {
907 br
->flags
._BCL_reversed
= 1;
908 sort(&br
->levels
[2], count
- 2, sizeof(br
->levels
[2]),
909 acpi_video_cmp_level
, NULL
);
910 } else if (max_level
!= br
->levels
[count
- 1])
912 "Found unordered _BCL package"));
915 device
->brightness
= br
;
917 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
918 br
->curr
= level
= max_level
;
920 if (!device
->cap
._BQC
)
923 result
= acpi_video_device_lcd_get_level_current(device
,
926 goto out_free_levels
;
928 result
= acpi_video_bqc_quirk(device
, max_level
, level_old
);
930 goto out_free_levels
;
932 * cap._BQC may get cleared due to _BQC is found to be broken
933 * in acpi_video_bqc_quirk, so check again here.
935 if (!device
->cap
._BQC
)
938 level
= acpi_video_bqc_value_to_level(device
, level_old
);
940 * On some buggy laptops, _BQC returns an uninitialized
941 * value when invoked for the first time, i.e.
942 * level_old is invalid (no matter whether it's a level
943 * or an index). Set the backlight to max_level in this case.
945 for (i
= 2; i
< br
->count
; i
++)
946 if (level
== br
->levels
[i
])
948 if (i
== br
->count
|| !level
)
952 result
= acpi_video_device_lcd_set_level(device
, level
);
954 goto out_free_levels
;
956 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
957 "found %d brightness levels\n", count
- 2));
966 device
->brightness
= NULL
;
973 * device : video output device (LCD, CRT, ..)
978 * Find out all required AML methods defined under the output
982 static void acpi_video_device_find_cap(struct acpi_video_device
*device
)
984 if (acpi_has_method(device
->dev
->handle
, "_ADR"))
985 device
->cap
._ADR
= 1;
986 if (acpi_has_method(device
->dev
->handle
, "_BCL"))
987 device
->cap
._BCL
= 1;
988 if (acpi_has_method(device
->dev
->handle
, "_BCM"))
989 device
->cap
._BCM
= 1;
990 if (acpi_has_method(device
->dev
->handle
, "_BQC")) {
991 device
->cap
._BQC
= 1;
992 } else if (acpi_has_method(device
->dev
->handle
, "_BCQ")) {
993 printk(KERN_WARNING FW_BUG
"_BCQ is used instead of _BQC\n");
994 device
->cap
._BCQ
= 1;
997 if (acpi_has_method(device
->dev
->handle
, "_DDC"))
998 device
->cap
._DDC
= 1;
1003 * device : video output device (VGA)
1008 * Find out all required AML methods defined under the video bus device.
1011 static void acpi_video_bus_find_cap(struct acpi_video_bus
*video
)
1013 if (acpi_has_method(video
->device
->handle
, "_DOS"))
1014 video
->cap
._DOS
= 1;
1015 if (acpi_has_method(video
->device
->handle
, "_DOD"))
1016 video
->cap
._DOD
= 1;
1017 if (acpi_has_method(video
->device
->handle
, "_ROM"))
1018 video
->cap
._ROM
= 1;
1019 if (acpi_has_method(video
->device
->handle
, "_GPD"))
1020 video
->cap
._GPD
= 1;
1021 if (acpi_has_method(video
->device
->handle
, "_SPD"))
1022 video
->cap
._SPD
= 1;
1023 if (acpi_has_method(video
->device
->handle
, "_VPO"))
1024 video
->cap
._VPO
= 1;
1028 * Check whether the video bus device has required AML method to
1029 * support the desired features
1032 static int acpi_video_bus_check(struct acpi_video_bus
*video
)
1034 acpi_status status
= -ENOENT
;
1035 struct pci_dev
*dev
;
1040 dev
= acpi_get_pci_dev(video
->device
->handle
);
1046 * Since there is no HID, CID and so on for VGA driver, we have
1047 * to check well known required nodes.
1050 /* Does this device support video switching? */
1051 if (video
->cap
._DOS
|| video
->cap
._DOD
) {
1052 if (!video
->cap
._DOS
) {
1053 printk(KERN_WARNING FW_BUG
1054 "ACPI(%s) defines _DOD but not _DOS\n",
1055 acpi_device_bid(video
->device
));
1057 video
->flags
.multihead
= 1;
1061 /* Does this device support retrieving a video ROM? */
1062 if (video
->cap
._ROM
) {
1063 video
->flags
.rom
= 1;
1067 /* Does this device support configuring which video device to POST? */
1068 if (video
->cap
._GPD
&& video
->cap
._SPD
&& video
->cap
._VPO
) {
1069 video
->flags
.post
= 1;
1077 * --------------------------------------------------------------------------
1079 * --------------------------------------------------------------------------
1082 /* device interface */
1083 static struct acpi_video_device_attrib
*
1084 acpi_video_get_device_attr(struct acpi_video_bus
*video
, unsigned long device_id
)
1086 struct acpi_video_enumerated_device
*ids
;
1089 for (i
= 0; i
< video
->attached_count
; i
++) {
1090 ids
= &video
->attached_array
[i
];
1091 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1092 return &ids
->value
.attrib
;
1099 acpi_video_get_device_type(struct acpi_video_bus
*video
,
1100 unsigned long device_id
)
1102 struct acpi_video_enumerated_device
*ids
;
1105 for (i
= 0; i
< video
->attached_count
; i
++) {
1106 ids
= &video
->attached_array
[i
];
1107 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1108 return ids
->value
.int_val
;
1115 acpi_video_bus_get_one_device(struct acpi_device
*device
,
1116 struct acpi_video_bus
*video
)
1118 unsigned long long device_id
;
1119 int status
, device_type
;
1120 struct acpi_video_device
*data
;
1121 struct acpi_video_device_attrib
*attribute
;
1124 acpi_evaluate_integer(device
->handle
, "_ADR", NULL
, &device_id
);
1125 /* Some device omits _ADR, we skip them instead of fail */
1126 if (ACPI_FAILURE(status
))
1129 data
= kzalloc(sizeof(struct acpi_video_device
), GFP_KERNEL
);
1133 strcpy(acpi_device_name(device
), ACPI_VIDEO_DEVICE_NAME
);
1134 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
1135 device
->driver_data
= data
;
1137 data
->device_id
= device_id
;
1138 data
->video
= video
;
1141 attribute
= acpi_video_get_device_attr(video
, device_id
);
1143 if (attribute
&& attribute
->device_id_scheme
) {
1144 switch (attribute
->display_type
) {
1145 case ACPI_VIDEO_DISPLAY_CRT
:
1146 data
->flags
.crt
= 1;
1148 case ACPI_VIDEO_DISPLAY_TV
:
1149 data
->flags
.tvout
= 1;
1151 case ACPI_VIDEO_DISPLAY_DVI
:
1152 data
->flags
.dvi
= 1;
1154 case ACPI_VIDEO_DISPLAY_LCD
:
1155 data
->flags
.lcd
= 1;
1158 data
->flags
.unknown
= 1;
1161 if (attribute
->bios_can_detect
)
1162 data
->flags
.bios
= 1;
1164 /* Check for legacy IDs */
1165 device_type
= acpi_video_get_device_type(video
, device_id
);
1166 /* Ignore bits 16 and 18-20 */
1167 switch (device_type
& 0xffe2ffff) {
1168 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR
:
1169 data
->flags
.crt
= 1;
1171 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL
:
1172 data
->flags
.lcd
= 1;
1174 case ACPI_VIDEO_DISPLAY_LEGACY_TV
:
1175 data
->flags
.tvout
= 1;
1178 data
->flags
.unknown
= 1;
1182 acpi_video_device_bind(video
, data
);
1183 acpi_video_device_find_cap(data
);
1185 mutex_lock(&video
->device_list_lock
);
1186 list_add_tail(&data
->entry
, &video
->video_device_list
);
1187 mutex_unlock(&video
->device_list_lock
);
1194 * video : video bus device
1199 * Enumerate the video device list of the video bus,
1200 * bind the ids with the corresponding video devices
1201 * under the video bus.
1204 static void acpi_video_device_rebind(struct acpi_video_bus
*video
)
1206 struct acpi_video_device
*dev
;
1208 mutex_lock(&video
->device_list_lock
);
1210 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1211 acpi_video_device_bind(video
, dev
);
1213 mutex_unlock(&video
->device_list_lock
);
1218 * video : video bus device
1219 * device : video output device under the video
1225 * Bind the ids with the corresponding video devices
1226 * under the video bus.
1230 acpi_video_device_bind(struct acpi_video_bus
*video
,
1231 struct acpi_video_device
*device
)
1233 struct acpi_video_enumerated_device
*ids
;
1236 for (i
= 0; i
< video
->attached_count
; i
++) {
1237 ids
= &video
->attached_array
[i
];
1238 if (device
->device_id
== (ids
->value
.int_val
& 0xffff)) {
1239 ids
->bind_info
= device
;
1240 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "device_bind %d\n", i
));
1247 * video : video bus device
1252 * Call _DOD to enumerate all devices attached to display adapter
1256 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
)
1261 struct acpi_video_enumerated_device
*active_list
;
1262 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1263 union acpi_object
*dod
= NULL
;
1264 union acpi_object
*obj
;
1266 status
= acpi_evaluate_object(video
->device
->handle
, "_DOD", NULL
, &buffer
);
1267 if (!ACPI_SUCCESS(status
)) {
1268 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _DOD"));
1272 dod
= buffer
.pointer
;
1273 if (!dod
|| (dod
->type
!= ACPI_TYPE_PACKAGE
)) {
1274 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _DOD data"));
1279 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d video heads in _DOD\n",
1280 dod
->package
.count
));
1282 active_list
= kcalloc(1 + dod
->package
.count
,
1283 sizeof(struct acpi_video_enumerated_device
),
1291 for (i
= 0; i
< dod
->package
.count
; i
++) {
1292 obj
= &dod
->package
.elements
[i
];
1294 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
1295 printk(KERN_ERR PREFIX
1296 "Invalid _DOD data in element %d\n", i
);
1300 active_list
[count
].value
.int_val
= obj
->integer
.value
;
1301 active_list
[count
].bind_info
= NULL
;
1302 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "dod element[%d] = %d\n", i
,
1303 (int)obj
->integer
.value
));
1307 kfree(video
->attached_array
);
1309 video
->attached_array
= active_list
;
1310 video
->attached_count
= count
;
1313 kfree(buffer
.pointer
);
1318 acpi_video_get_next_level(struct acpi_video_device
*device
,
1319 u32 level_current
, u32 event
)
1321 int min
, max
, min_above
, max_below
, i
, l
, delta
= 255;
1322 max
= max_below
= 0;
1323 min
= min_above
= 255;
1324 /* Find closest level to level_current */
1325 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1326 l
= device
->brightness
->levels
[i
];
1327 if (abs(l
- level_current
) < abs(delta
)) {
1328 delta
= l
- level_current
;
1333 /* Ajust level_current to closest available level */
1334 level_current
+= delta
;
1335 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1336 l
= device
->brightness
->levels
[i
];
1341 if (l
< min_above
&& l
> level_current
)
1343 if (l
> max_below
&& l
< level_current
)
1348 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
:
1349 return (level_current
< max
) ? min_above
: min
;
1350 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
:
1351 return (level_current
< max
) ? min_above
: max
;
1352 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
:
1353 return (level_current
> min
) ? max_below
: min
;
1354 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
:
1355 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
:
1358 return level_current
;
1363 acpi_video_switch_brightness(struct acpi_video_device
*device
, int event
)
1365 unsigned long long level_current
, level_next
;
1366 int result
= -EINVAL
;
1368 /* no warning message if acpi_backlight=vendor or a quirk is used */
1369 if (!acpi_video_verify_backlight_support())
1372 if (!device
->brightness
)
1375 result
= acpi_video_device_lcd_get_level_current(device
,
1381 level_next
= acpi_video_get_next_level(device
, level_current
, event
);
1383 result
= acpi_video_device_lcd_set_level(device
, level_next
);
1386 backlight_force_update(device
->backlight
,
1387 BACKLIGHT_UPDATE_HOTKEY
);
1391 printk(KERN_ERR PREFIX
"Failed to switch the brightness\n");
1396 int acpi_video_get_edid(struct acpi_device
*device
, int type
, int device_id
,
1399 struct acpi_video_bus
*video
;
1400 struct acpi_video_device
*video_device
;
1401 union acpi_object
*buffer
= NULL
;
1405 if (!device
|| !acpi_driver_data(device
))
1408 video
= acpi_driver_data(device
);
1410 for (i
= 0; i
< video
->attached_count
; i
++) {
1411 video_device
= video
->attached_array
[i
].bind_info
;
1417 if (!video_device
->cap
._DDC
)
1422 case ACPI_VIDEO_DISPLAY_CRT
:
1423 if (!video_device
->flags
.crt
)
1426 case ACPI_VIDEO_DISPLAY_TV
:
1427 if (!video_device
->flags
.tvout
)
1430 case ACPI_VIDEO_DISPLAY_DVI
:
1431 if (!video_device
->flags
.dvi
)
1434 case ACPI_VIDEO_DISPLAY_LCD
:
1435 if (!video_device
->flags
.lcd
)
1439 } else if (video_device
->device_id
!= device_id
) {
1443 status
= acpi_video_device_EDID(video_device
, &buffer
, length
);
1445 if (ACPI_FAILURE(status
) || !buffer
||
1446 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1448 status
= acpi_video_device_EDID(video_device
, &buffer
,
1450 if (ACPI_FAILURE(status
) || !buffer
||
1451 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1456 *edid
= buffer
->buffer
.pointer
;
1462 EXPORT_SYMBOL(acpi_video_get_edid
);
1465 acpi_video_bus_get_devices(struct acpi_video_bus
*video
,
1466 struct acpi_device
*device
)
1469 struct acpi_device
*dev
;
1472 * There are systems where video module known to work fine regardless
1473 * of broken _DOD and ignoring returned value here doesn't cause
1476 acpi_video_device_enumerate(video
);
1478 list_for_each_entry(dev
, &device
->children
, node
) {
1480 status
= acpi_video_bus_get_one_device(dev
, video
);
1482 dev_err(&dev
->dev
, "Can't attach device\n");
1489 /* acpi_video interface */
1492 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1493 * preform any automatic brightness change on receiving a notification.
1495 static int acpi_video_bus_start_devices(struct acpi_video_bus
*video
)
1497 return acpi_video_bus_DOS(video
, 0,
1498 acpi_osi_is_win8() ? 1 : 0);
1501 static int acpi_video_bus_stop_devices(struct acpi_video_bus
*video
)
1503 return acpi_video_bus_DOS(video
, 0,
1504 acpi_osi_is_win8() ? 0 : 1);
1507 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
)
1509 struct acpi_video_bus
*video
= acpi_driver_data(device
);
1510 struct input_dev
*input
;
1513 if (!video
|| !video
->input
)
1516 input
= video
->input
;
1519 case ACPI_VIDEO_NOTIFY_SWITCH
: /* User requested a switch,
1520 * most likely via hotkey. */
1521 keycode
= KEY_SWITCHVIDEOMODE
;
1524 case ACPI_VIDEO_NOTIFY_PROBE
: /* User plugged in or removed a video
1526 acpi_video_device_enumerate(video
);
1527 acpi_video_device_rebind(video
);
1528 keycode
= KEY_SWITCHVIDEOMODE
;
1531 case ACPI_VIDEO_NOTIFY_CYCLE
: /* Cycle Display output hotkey pressed. */
1532 keycode
= KEY_SWITCHVIDEOMODE
;
1534 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT
: /* Next Display output hotkey pressed. */
1535 keycode
= KEY_VIDEO_NEXT
;
1537 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT
: /* previous Display output hotkey pressed. */
1538 keycode
= KEY_VIDEO_PREV
;
1542 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1543 "Unsupported event [0x%x]\n", event
));
1547 if (acpi_notifier_call_chain(device
, event
, 0))
1548 /* Something vetoed the keypress. */
1552 input_report_key(input
, keycode
, 1);
1554 input_report_key(input
, keycode
, 0);
1561 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
)
1563 struct acpi_video_device
*video_device
= data
;
1564 struct acpi_device
*device
= NULL
;
1565 struct acpi_video_bus
*bus
;
1566 struct input_dev
*input
;
1572 device
= video_device
->dev
;
1573 bus
= video_device
->video
;
1577 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
: /* Cycle brightness */
1578 if (brightness_switch_enabled
)
1579 acpi_video_switch_brightness(video_device
, event
);
1580 keycode
= KEY_BRIGHTNESS_CYCLE
;
1582 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
: /* Increase brightness */
1583 if (brightness_switch_enabled
)
1584 acpi_video_switch_brightness(video_device
, event
);
1585 keycode
= KEY_BRIGHTNESSUP
;
1587 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
: /* Decrease brightness */
1588 if (brightness_switch_enabled
)
1589 acpi_video_switch_brightness(video_device
, event
);
1590 keycode
= KEY_BRIGHTNESSDOWN
;
1592 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
: /* zero brightness */
1593 if (brightness_switch_enabled
)
1594 acpi_video_switch_brightness(video_device
, event
);
1595 keycode
= KEY_BRIGHTNESS_ZERO
;
1597 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
: /* display device off */
1598 if (brightness_switch_enabled
)
1599 acpi_video_switch_brightness(video_device
, event
);
1600 keycode
= KEY_DISPLAY_OFF
;
1603 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1604 "Unsupported event [0x%x]\n", event
));
1608 acpi_notifier_call_chain(device
, event
, 0);
1611 input_report_key(input
, keycode
, 1);
1613 input_report_key(input
, keycode
, 0);
1620 static int acpi_video_resume(struct notifier_block
*nb
,
1621 unsigned long val
, void *ign
)
1623 struct acpi_video_bus
*video
;
1624 struct acpi_video_device
*video_device
;
1628 case PM_HIBERNATION_PREPARE
:
1629 case PM_SUSPEND_PREPARE
:
1630 case PM_RESTORE_PREPARE
:
1634 video
= container_of(nb
, struct acpi_video_bus
, pm_nb
);
1636 dev_info(&video
->device
->dev
, "Restoring backlight state\n");
1638 for (i
= 0; i
< video
->attached_count
; i
++) {
1639 video_device
= video
->attached_array
[i
].bind_info
;
1640 if (video_device
&& video_device
->backlight
)
1641 acpi_video_set_brightness(video_device
->backlight
);
1648 acpi_video_bus_match(acpi_handle handle
, u32 level
, void *context
,
1649 void **return_value
)
1651 struct acpi_device
*device
= context
;
1652 struct acpi_device
*sibling
;
1655 if (handle
== device
->handle
)
1656 return AE_CTRL_TERMINATE
;
1658 result
= acpi_bus_get_device(handle
, &sibling
);
1662 if (!strcmp(acpi_device_name(sibling
), ACPI_VIDEO_BUS_NAME
))
1663 return AE_ALREADY_EXISTS
;
1668 static void acpi_video_dev_register_backlight(struct acpi_video_device
*device
)
1670 struct backlight_properties props
;
1671 struct pci_dev
*pdev
;
1672 acpi_handle acpi_parent
;
1673 struct device
*parent
= NULL
;
1678 result
= acpi_video_init_brightness(device
);
1681 name
= kasprintf(GFP_KERNEL
, "acpi_video%d", count
);
1686 acpi_get_parent(device
->dev
->handle
, &acpi_parent
);
1688 pdev
= acpi_get_pci_dev(acpi_parent
);
1690 parent
= &pdev
->dev
;
1694 memset(&props
, 0, sizeof(struct backlight_properties
));
1695 props
.type
= BACKLIGHT_FIRMWARE
;
1696 props
.max_brightness
= device
->brightness
->count
- 3;
1697 device
->backlight
= backlight_device_register(name
,
1700 &acpi_backlight_ops
,
1703 if (IS_ERR(device
->backlight
))
1707 * Save current brightness level in case we have to restore it
1708 * before acpi_video_device_lcd_set_level() is called next time.
1710 device
->backlight
->props
.brightness
=
1711 acpi_video_get_brightness(device
->backlight
);
1713 device
->cooling_dev
= thermal_cooling_device_register("LCD",
1714 device
->dev
, &video_cooling_ops
);
1715 if (IS_ERR(device
->cooling_dev
)) {
1717 * Set cooling_dev to NULL so we don't crash trying to free it.
1718 * Also, why the hell we are returning early and not attempt to
1719 * register video output if cooling device registration failed?
1722 device
->cooling_dev
= NULL
;
1726 dev_info(&device
->dev
->dev
, "registered as cooling_device%d\n",
1727 device
->cooling_dev
->id
);
1728 result
= sysfs_create_link(&device
->dev
->dev
.kobj
,
1729 &device
->cooling_dev
->device
.kobj
,
1732 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1733 result
= sysfs_create_link(&device
->cooling_dev
->device
.kobj
,
1734 &device
->dev
->dev
.kobj
, "device");
1736 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1739 static int acpi_video_bus_register_backlight(struct acpi_video_bus
*video
)
1741 struct acpi_video_device
*dev
;
1743 if (!acpi_video_verify_backlight_support())
1746 mutex_lock(&video
->device_list_lock
);
1747 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1748 acpi_video_dev_register_backlight(dev
);
1749 mutex_unlock(&video
->device_list_lock
);
1751 video
->backlight_registered
= true;
1753 video
->pm_nb
.notifier_call
= acpi_video_resume
;
1754 video
->pm_nb
.priority
= 0;
1755 return register_pm_notifier(&video
->pm_nb
);
1758 static void acpi_video_dev_unregister_backlight(struct acpi_video_device
*device
)
1760 if (device
->backlight
) {
1761 backlight_device_unregister(device
->backlight
);
1762 device
->backlight
= NULL
;
1764 if (device
->brightness
) {
1765 kfree(device
->brightness
->levels
);
1766 kfree(device
->brightness
);
1767 device
->brightness
= NULL
;
1769 if (device
->cooling_dev
) {
1770 sysfs_remove_link(&device
->dev
->dev
.kobj
, "thermal_cooling");
1771 sysfs_remove_link(&device
->cooling_dev
->device
.kobj
, "device");
1772 thermal_cooling_device_unregister(device
->cooling_dev
);
1773 device
->cooling_dev
= NULL
;
1777 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus
*video
)
1779 struct acpi_video_device
*dev
;
1782 if (!video
->backlight_registered
)
1785 error
= unregister_pm_notifier(&video
->pm_nb
);
1787 mutex_lock(&video
->device_list_lock
);
1788 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1789 acpi_video_dev_unregister_backlight(dev
);
1790 mutex_unlock(&video
->device_list_lock
);
1792 video
->backlight_registered
= false;
1797 static void acpi_video_dev_add_notify_handler(struct acpi_video_device
*device
)
1800 struct acpi_device
*adev
= device
->dev
;
1802 status
= acpi_install_notify_handler(adev
->handle
, ACPI_DEVICE_NOTIFY
,
1803 acpi_video_device_notify
, device
);
1804 if (ACPI_FAILURE(status
))
1805 dev_err(&adev
->dev
, "Error installing notify handler\n");
1807 device
->flags
.notify
= 1;
1810 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus
*video
)
1812 struct input_dev
*input
;
1813 struct acpi_video_device
*dev
;
1816 video
->input
= input
= input_allocate_device();
1822 error
= acpi_video_bus_start_devices(video
);
1824 goto err_free_input
;
1826 snprintf(video
->phys
, sizeof(video
->phys
),
1827 "%s/video/input0", acpi_device_hid(video
->device
));
1829 input
->name
= acpi_device_name(video
->device
);
1830 input
->phys
= video
->phys
;
1831 input
->id
.bustype
= BUS_HOST
;
1832 input
->id
.product
= 0x06;
1833 input
->dev
.parent
= &video
->device
->dev
;
1834 input
->evbit
[0] = BIT(EV_KEY
);
1835 set_bit(KEY_SWITCHVIDEOMODE
, input
->keybit
);
1836 set_bit(KEY_VIDEO_NEXT
, input
->keybit
);
1837 set_bit(KEY_VIDEO_PREV
, input
->keybit
);
1838 set_bit(KEY_BRIGHTNESS_CYCLE
, input
->keybit
);
1839 set_bit(KEY_BRIGHTNESSUP
, input
->keybit
);
1840 set_bit(KEY_BRIGHTNESSDOWN
, input
->keybit
);
1841 set_bit(KEY_BRIGHTNESS_ZERO
, input
->keybit
);
1842 set_bit(KEY_DISPLAY_OFF
, input
->keybit
);
1844 error
= input_register_device(input
);
1848 mutex_lock(&video
->device_list_lock
);
1849 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1850 acpi_video_dev_add_notify_handler(dev
);
1851 mutex_unlock(&video
->device_list_lock
);
1856 acpi_video_bus_stop_devices(video
);
1858 input_free_device(input
);
1859 video
->input
= NULL
;
1864 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device
*dev
)
1866 if (dev
->flags
.notify
) {
1867 acpi_remove_notify_handler(dev
->dev
->handle
, ACPI_DEVICE_NOTIFY
,
1868 acpi_video_device_notify
);
1869 dev
->flags
.notify
= 0;
1873 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus
*video
)
1875 struct acpi_video_device
*dev
;
1877 mutex_lock(&video
->device_list_lock
);
1878 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1879 acpi_video_dev_remove_notify_handler(dev
);
1880 mutex_unlock(&video
->device_list_lock
);
1882 acpi_video_bus_stop_devices(video
);
1883 input_unregister_device(video
->input
);
1884 video
->input
= NULL
;
1887 static int acpi_video_bus_put_devices(struct acpi_video_bus
*video
)
1889 struct acpi_video_device
*dev
, *next
;
1891 mutex_lock(&video
->device_list_lock
);
1892 list_for_each_entry_safe(dev
, next
, &video
->video_device_list
, entry
) {
1893 list_del(&dev
->entry
);
1896 mutex_unlock(&video
->device_list_lock
);
1901 static int instance
;
1903 static int acpi_video_bus_add(struct acpi_device
*device
)
1905 struct acpi_video_bus
*video
;
1909 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
,
1910 device
->parent
->handle
, 1,
1911 acpi_video_bus_match
, NULL
,
1913 if (status
== AE_ALREADY_EXISTS
) {
1914 printk(KERN_WARNING FW_BUG
1915 "Duplicate ACPI video bus devices for the"
1916 " same VGA controller, please try module "
1917 "parameter \"video.allow_duplicates=1\""
1918 "if the current driver doesn't work.\n");
1919 if (!allow_duplicates
)
1923 video
= kzalloc(sizeof(struct acpi_video_bus
), GFP_KERNEL
);
1927 /* a hack to fix the duplicate name "VID" problem on T61 */
1928 if (!strcmp(device
->pnp
.bus_id
, "VID")) {
1930 device
->pnp
.bus_id
[3] = '0' + instance
;
1933 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1934 if (!strcmp(device
->pnp
.bus_id
, "VGA")) {
1936 device
->pnp
.bus_id
[3] = '0' + instance
;
1940 video
->device
= device
;
1941 strcpy(acpi_device_name(device
), ACPI_VIDEO_BUS_NAME
);
1942 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
1943 device
->driver_data
= video
;
1945 acpi_video_bus_find_cap(video
);
1946 error
= acpi_video_bus_check(video
);
1948 goto err_free_video
;
1950 mutex_init(&video
->device_list_lock
);
1951 INIT_LIST_HEAD(&video
->video_device_list
);
1953 error
= acpi_video_bus_get_devices(video
, device
);
1957 printk(KERN_INFO PREFIX
"%s [%s] (multi-head: %s rom: %s post: %s)\n",
1958 ACPI_VIDEO_DEVICE_NAME
, acpi_device_bid(device
),
1959 video
->flags
.multihead
? "yes" : "no",
1960 video
->flags
.rom
? "yes" : "no",
1961 video
->flags
.post
? "yes" : "no");
1962 mutex_lock(&video_list_lock
);
1963 list_add_tail(&video
->entry
, &video_bus_head
);
1964 mutex_unlock(&video_list_lock
);
1966 acpi_video_bus_register_backlight(video
);
1967 acpi_video_bus_add_notify_handler(video
);
1972 acpi_video_bus_put_devices(video
);
1973 kfree(video
->attached_array
);
1976 device
->driver_data
= NULL
;
1981 static int acpi_video_bus_remove(struct acpi_device
*device
)
1983 struct acpi_video_bus
*video
= NULL
;
1986 if (!device
|| !acpi_driver_data(device
))
1989 video
= acpi_driver_data(device
);
1991 acpi_video_bus_remove_notify_handler(video
);
1992 acpi_video_bus_unregister_backlight(video
);
1993 acpi_video_bus_put_devices(video
);
1995 mutex_lock(&video_list_lock
);
1996 list_del(&video
->entry
);
1997 mutex_unlock(&video_list_lock
);
1999 kfree(video
->attached_array
);
2005 static int __init
is_i740(struct pci_dev
*dev
)
2007 if (dev
->device
== 0x00D1)
2009 if (dev
->device
== 0x7000)
2014 static int __init
intel_opregion_present(void)
2017 struct pci_dev
*dev
= NULL
;
2020 for_each_pci_dev(dev
) {
2021 if ((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
2023 if (dev
->vendor
!= PCI_VENDOR_ID_INTEL
)
2025 /* We don't want to poke around undefined i740 registers */
2028 pci_read_config_dword(dev
, 0xfc, &address
);
2036 int acpi_video_register(void)
2039 if (register_count
) {
2041 * if the function of acpi_video_register is already called,
2042 * don't register the acpi_vide_bus again and return no error.
2047 mutex_init(&video_list_lock
);
2048 INIT_LIST_HEAD(&video_bus_head
);
2050 result
= acpi_bus_register_driver(&acpi_video_bus
);
2055 * When the acpi_video_bus is loaded successfully, increase
2056 * the counter reference.
2062 EXPORT_SYMBOL(acpi_video_register
);
2064 void acpi_video_unregister(void)
2066 if (!register_count
) {
2068 * If the acpi video bus is already unloaded, don't
2069 * unload it again and return directly.
2073 acpi_bus_unregister_driver(&acpi_video_bus
);
2079 EXPORT_SYMBOL(acpi_video_unregister
);
2081 void acpi_video_unregister_backlight(void)
2083 struct acpi_video_bus
*video
;
2085 if (!register_count
)
2088 mutex_lock(&video_list_lock
);
2089 list_for_each_entry(video
, &video_bus_head
, entry
)
2090 acpi_video_bus_unregister_backlight(video
);
2091 mutex_unlock(&video_list_lock
);
2093 EXPORT_SYMBOL(acpi_video_unregister_backlight
);
2096 * This is kind of nasty. Hardware using Intel chipsets may require
2097 * the video opregion code to be run first in order to initialise
2098 * state before any ACPI video calls are made. To handle this we defer
2099 * registration of the video class until the opregion code has run.
2102 static int __init
acpi_video_init(void)
2104 dmi_check_system(video_dmi_table
);
2106 if (intel_opregion_present())
2109 return acpi_video_register();
2112 static void __exit
acpi_video_exit(void)
2114 acpi_video_unregister();
2119 module_init(acpi_video_init
);
2120 module_exit(acpi_video_exit
);