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
= 1;
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.
86 NATIVE_BACKLIGHT_NOT_SET
= -1,
91 static int use_native_backlight_param
= NATIVE_BACKLIGHT_NOT_SET
;
92 module_param_named(use_native_backlight
, use_native_backlight_param
, int, 0444);
93 static int use_native_backlight_dmi
= NATIVE_BACKLIGHT_NOT_SET
;
95 static int register_count
;
96 static struct mutex video_list_lock
;
97 static struct list_head video_bus_head
;
98 static int acpi_video_bus_add(struct acpi_device
*device
);
99 static int acpi_video_bus_remove(struct acpi_device
*device
);
100 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
);
102 static const struct acpi_device_id video_device_ids
[] = {
106 MODULE_DEVICE_TABLE(acpi
, video_device_ids
);
108 static struct acpi_driver acpi_video_bus
= {
110 .class = ACPI_VIDEO_CLASS
,
111 .ids
= video_device_ids
,
113 .add
= acpi_video_bus_add
,
114 .remove
= acpi_video_bus_remove
,
115 .notify
= acpi_video_bus_notify
,
119 struct acpi_video_bus_flags
{
120 u8 multihead
:1; /* can switch video heads */
121 u8 rom
:1; /* can retrieve a video rom */
122 u8 post
:1; /* can configure the head to */
126 struct acpi_video_bus_cap
{
127 u8 _DOS
:1; /* Enable/Disable output switching */
128 u8 _DOD
:1; /* Enumerate all devices attached to display adapter */
129 u8 _ROM
:1; /* Get ROM Data */
130 u8 _GPD
:1; /* Get POST Device */
131 u8 _SPD
:1; /* Set POST Device */
132 u8 _VPO
:1; /* Video POST Options */
136 struct acpi_video_device_attrib
{
137 u32 display_index
:4; /* A zero-based instance of the Display */
138 u32 display_port_attachment
:4; /* This field differentiates the display type */
139 u32 display_type
:4; /* Describe the specific type in use */
140 u32 vendor_specific
:4; /* Chipset Vendor Specific */
141 u32 bios_can_detect
:1; /* BIOS can detect the device */
142 u32 depend_on_vga
:1; /* Non-VGA output device whose power is related to
144 u32 pipe_id
:3; /* For VGA multiple-head devices. */
145 u32 reserved
:10; /* Must be 0 */
146 u32 device_id_scheme
:1; /* Device ID Scheme */
149 struct acpi_video_enumerated_device
{
152 struct acpi_video_device_attrib attrib
;
154 struct acpi_video_device
*bind_info
;
157 struct acpi_video_bus
{
158 struct acpi_device
*device
;
159 bool backlight_registered
;
160 bool backlight_notifier_registered
;
162 struct acpi_video_enumerated_device
*attached_array
;
165 struct acpi_video_bus_cap cap
;
166 struct acpi_video_bus_flags flags
;
167 struct list_head video_device_list
;
168 struct mutex device_list_lock
; /* protects video_device_list */
169 struct list_head entry
;
170 struct input_dev
*input
;
171 char phys
[32]; /* for input device */
172 struct notifier_block pm_nb
;
173 struct notifier_block backlight_nb
;
176 struct acpi_video_device_flags
{
187 struct acpi_video_device_cap
{
188 u8 _ADR
:1; /* Return the unique ID */
189 u8 _BCL
:1; /* Query list of brightness control levels supported */
190 u8 _BCM
:1; /* Set the brightness level */
191 u8 _BQC
:1; /* Get current brightness level */
192 u8 _BCQ
:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
193 u8 _DDC
:1; /* Return the EDID for this device */
196 struct acpi_video_brightness_flags
{
197 u8 _BCL_no_ac_battery_levels
:1; /* no AC/Battery levels in _BCL */
198 u8 _BCL_reversed
:1; /* _BCL package is in a reversed order */
199 u8 _BQC_use_index
:1; /* _BQC returns an index value */
202 struct acpi_video_device_brightness
{
206 struct acpi_video_brightness_flags flags
;
209 struct acpi_video_device
{
210 unsigned long device_id
;
211 struct acpi_video_device_flags flags
;
212 struct acpi_video_device_cap cap
;
213 struct list_head entry
;
214 struct delayed_work switch_brightness_work
;
215 int switch_brightness_event
;
216 struct acpi_video_bus
*video
;
217 struct acpi_device
*dev
;
218 struct acpi_video_device_brightness
*brightness
;
219 struct backlight_device
*backlight
;
220 struct thermal_cooling_device
*cooling_dev
;
223 static const char device_decode
[][30] = {
224 "motherboard VGA device",
230 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
);
231 static void acpi_video_device_rebind(struct acpi_video_bus
*video
);
232 static void acpi_video_device_bind(struct acpi_video_bus
*video
,
233 struct acpi_video_device
*device
);
234 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
);
235 static int acpi_video_device_lcd_set_level(struct acpi_video_device
*device
,
237 static int acpi_video_device_lcd_get_level_current(
238 struct acpi_video_device
*device
,
239 unsigned long long *level
, bool raw
);
240 static int acpi_video_get_next_level(struct acpi_video_device
*device
,
241 u32 level_current
, u32 event
);
242 static void acpi_video_switch_brightness(struct work_struct
*work
);
244 static bool acpi_video_use_native_backlight(void)
246 if (use_native_backlight_param
!= NATIVE_BACKLIGHT_NOT_SET
)
247 return use_native_backlight_param
;
248 else if (use_native_backlight_dmi
!= NATIVE_BACKLIGHT_NOT_SET
)
249 return use_native_backlight_dmi
;
250 return acpi_osi_is_win8();
253 bool acpi_video_verify_backlight_support(void)
255 if (acpi_video_use_native_backlight() &&
256 backlight_device_registered(BACKLIGHT_RAW
))
258 return acpi_video_backlight_support();
260 EXPORT_SYMBOL_GPL(acpi_video_verify_backlight_support
);
262 /* backlight device sysfs support */
263 static int acpi_video_get_brightness(struct backlight_device
*bd
)
265 unsigned long long cur_level
;
267 struct acpi_video_device
*vd
= bl_get_data(bd
);
269 if (acpi_video_device_lcd_get_level_current(vd
, &cur_level
, false))
271 for (i
= 2; i
< vd
->brightness
->count
; i
++) {
272 if (vd
->brightness
->levels
[i
] == cur_level
)
274 * The first two entries are special - see page 575
275 * of the ACPI spec 3.0
282 static int acpi_video_set_brightness(struct backlight_device
*bd
)
284 int request_level
= bd
->props
.brightness
+ 2;
285 struct acpi_video_device
*vd
= bl_get_data(bd
);
287 cancel_delayed_work(&vd
->switch_brightness_work
);
288 return acpi_video_device_lcd_set_level(vd
,
289 vd
->brightness
->levels
[request_level
]);
292 static const struct backlight_ops acpi_backlight_ops
= {
293 .get_brightness
= acpi_video_get_brightness
,
294 .update_status
= acpi_video_set_brightness
,
297 /* thermal cooling device callbacks */
298 static int video_get_max_state(struct thermal_cooling_device
*cooling_dev
, unsigned
301 struct acpi_device
*device
= cooling_dev
->devdata
;
302 struct acpi_video_device
*video
= acpi_driver_data(device
);
304 *state
= video
->brightness
->count
- 3;
308 static int video_get_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned
311 struct acpi_device
*device
= cooling_dev
->devdata
;
312 struct acpi_video_device
*video
= acpi_driver_data(device
);
313 unsigned long long level
;
316 if (acpi_video_device_lcd_get_level_current(video
, &level
, false))
318 for (offset
= 2; offset
< video
->brightness
->count
; offset
++)
319 if (level
== video
->brightness
->levels
[offset
]) {
320 *state
= video
->brightness
->count
- offset
- 1;
328 video_set_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned long state
)
330 struct acpi_device
*device
= cooling_dev
->devdata
;
331 struct acpi_video_device
*video
= acpi_driver_data(device
);
334 if (state
>= video
->brightness
->count
- 2)
337 state
= video
->brightness
->count
- state
;
338 level
= video
->brightness
->levels
[state
- 1];
339 return acpi_video_device_lcd_set_level(video
, level
);
342 static const struct thermal_cooling_device_ops video_cooling_ops
= {
343 .get_max_state
= video_get_max_state
,
344 .get_cur_state
= video_get_cur_state
,
345 .set_cur_state
= video_set_cur_state
,
349 * --------------------------------------------------------------------------
351 * --------------------------------------------------------------------------
355 acpi_video_device_lcd_query_levels(struct acpi_video_device
*device
,
356 union acpi_object
**levels
)
359 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
360 union acpi_object
*obj
;
365 status
= acpi_evaluate_object(device
->dev
->handle
, "_BCL", NULL
, &buffer
);
366 if (!ACPI_SUCCESS(status
))
368 obj
= (union acpi_object
*)buffer
.pointer
;
369 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
370 printk(KERN_ERR PREFIX
"Invalid _BCL data\n");
380 kfree(buffer
.pointer
);
386 acpi_video_device_lcd_set_level(struct acpi_video_device
*device
, int level
)
391 status
= acpi_execute_simple_method(device
->dev
->handle
,
393 if (ACPI_FAILURE(status
)) {
394 ACPI_ERROR((AE_INFO
, "Evaluating _BCM failed"));
398 device
->brightness
->curr
= level
;
399 for (state
= 2; state
< device
->brightness
->count
; state
++)
400 if (level
== device
->brightness
->levels
[state
]) {
401 if (device
->backlight
)
402 device
->backlight
->props
.brightness
= state
- 2;
406 ACPI_ERROR((AE_INFO
, "Current brightness invalid"));
411 * For some buggy _BQC methods, we need to add a constant value to
412 * the _BQC return value to get the actual current brightness level
415 static int bqc_offset_aml_bug_workaround
;
416 static int __init
video_set_bqc_offset(const struct dmi_system_id
*d
)
418 bqc_offset_aml_bug_workaround
= 9;
422 static int __init
video_disable_native_backlight(const struct dmi_system_id
*d
)
424 use_native_backlight_dmi
= NATIVE_BACKLIGHT_OFF
;
428 static int __init
video_enable_native_backlight(const struct dmi_system_id
*d
)
430 use_native_backlight_dmi
= NATIVE_BACKLIGHT_ON
;
434 static struct dmi_system_id video_dmi_table
[] __initdata
= {
436 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
439 .callback
= video_set_bqc_offset
,
440 .ident
= "Acer Aspire 5720",
442 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
443 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5720"),
447 .callback
= video_set_bqc_offset
,
448 .ident
= "Acer Aspire 5710Z",
450 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
451 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5710Z"),
455 .callback
= video_set_bqc_offset
,
456 .ident
= "eMachines E510",
458 DMI_MATCH(DMI_BOARD_VENDOR
, "EMACHINES"),
459 DMI_MATCH(DMI_PRODUCT_NAME
, "eMachines E510"),
463 .callback
= video_set_bqc_offset
,
464 .ident
= "Acer Aspire 5315",
466 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
467 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5315"),
471 .callback
= video_set_bqc_offset
,
472 .ident
= "Acer Aspire 7720",
474 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
475 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 7720"),
480 * These models have a working acpi_video backlight control, and using
481 * native backlight causes a regression where backlight does not work
482 * when userspace is not handling brightness key events. Disable
483 * native_backlight on these to fix this:
484 * https://bugzilla.kernel.org/show_bug.cgi?id=81691
487 .callback
= video_disable_native_backlight
,
488 .ident
= "ThinkPad T420",
490 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
491 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad T420"),
495 .callback
= video_disable_native_backlight
,
496 .ident
= "ThinkPad T520",
498 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
499 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad T520"),
503 .callback
= video_disable_native_backlight
,
504 .ident
= "ThinkPad X201s",
506 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
507 DMI_MATCH(DMI_PRODUCT_VERSION
, "ThinkPad X201s"),
511 /* The native backlight controls do not work on some older machines */
513 /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
514 .callback
= video_disable_native_backlight
,
515 .ident
= "HP ENVY 15 Notebook",
517 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
518 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ENVY 15 Notebook PC"),
523 .callback
= video_disable_native_backlight
,
524 .ident
= "SAMSUNG 870Z5E/880Z5E/680Z5E",
526 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
527 DMI_MATCH(DMI_PRODUCT_NAME
, "870Z5E/880Z5E/680Z5E"),
531 .callback
= video_disable_native_backlight
,
532 .ident
= "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
534 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
535 DMI_MATCH(DMI_PRODUCT_NAME
, "370R4E/370R4V/370R5E/3570RE/370R5V"),
539 /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
540 .callback
= video_disable_native_backlight
,
541 .ident
= "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
543 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
544 DMI_MATCH(DMI_PRODUCT_NAME
, "3570R/370R/470R/450R/510R/4450RV"),
548 /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
549 .callback
= video_disable_native_backlight
,
550 .ident
= "SAMSUNG 730U3E/740U3E",
552 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
553 DMI_MATCH(DMI_PRODUCT_NAME
, "730U3E/740U3E"),
557 /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
558 .callback
= video_disable_native_backlight
,
559 .ident
= "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
561 DMI_MATCH(DMI_SYS_VENDOR
, "SAMSUNG ELECTRONICS CO., LTD."),
562 DMI_MATCH(DMI_PRODUCT_NAME
, "900X3C/900X3D/900X3E/900X4C/900X4D"),
567 /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
568 .callback
= video_disable_native_backlight
,
569 .ident
= "Dell XPS15 L521X",
571 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
572 DMI_MATCH(DMI_PRODUCT_NAME
, "XPS L521X"),
576 /* Non win8 machines which need native backlight nevertheless */
578 /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
579 .callback
= video_enable_native_backlight
,
580 .ident
= "Lenovo Ideapad Z570",
582 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
583 DMI_MATCH(DMI_PRODUCT_NAME
, "102434U"),
589 static unsigned long long
590 acpi_video_bqc_value_to_level(struct acpi_video_device
*device
,
591 unsigned long long bqc_value
)
593 unsigned long long level
;
595 if (device
->brightness
->flags
._BQC_use_index
) {
597 * _BQC returns an index that doesn't account for
598 * the first 2 items with special meaning, so we need
599 * to compensate for that by offsetting ourselves
601 if (device
->brightness
->flags
._BCL_reversed
)
602 bqc_value
= device
->brightness
->count
- 3 - bqc_value
;
604 level
= device
->brightness
->levels
[bqc_value
+ 2];
609 level
+= bqc_offset_aml_bug_workaround
;
615 acpi_video_device_lcd_get_level_current(struct acpi_video_device
*device
,
616 unsigned long long *level
, bool raw
)
618 acpi_status status
= AE_OK
;
621 if (device
->cap
._BQC
|| device
->cap
._BCQ
) {
622 char *buf
= device
->cap
._BQC
? "_BQC" : "_BCQ";
624 status
= acpi_evaluate_integer(device
->dev
->handle
, buf
,
626 if (ACPI_SUCCESS(status
)) {
629 * Caller has indicated he wants the raw
630 * value returned by _BQC, so don't furtherly
631 * mess with the value.
636 *level
= acpi_video_bqc_value_to_level(device
, *level
);
638 for (i
= 2; i
< device
->brightness
->count
; i
++)
639 if (device
->brightness
->levels
[i
] == *level
) {
640 device
->brightness
->curr
= *level
;
644 * BQC returned an invalid level.
647 ACPI_WARNING((AE_INFO
,
648 "%s returned an invalid level",
650 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
654 * should we return an error or ignore this failure?
655 * dev->brightness->curr is a cached value which stores
656 * the correct current backlight level in most cases.
657 * ACPI video backlight still works w/ buggy _BQC.
658 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
660 ACPI_WARNING((AE_INFO
, "Evaluating %s failed", buf
));
661 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
665 *level
= device
->brightness
->curr
;
670 acpi_video_device_EDID(struct acpi_video_device
*device
,
671 union acpi_object
**edid
, ssize_t length
)
674 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
675 union acpi_object
*obj
;
676 union acpi_object arg0
= { ACPI_TYPE_INTEGER
};
677 struct acpi_object_list args
= { 1, &arg0
};
685 arg0
.integer
.value
= 1;
686 else if (length
== 256)
687 arg0
.integer
.value
= 2;
691 status
= acpi_evaluate_object(device
->dev
->handle
, "_DDC", &args
, &buffer
);
692 if (ACPI_FAILURE(status
))
695 obj
= buffer
.pointer
;
697 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
)
700 printk(KERN_ERR PREFIX
"Invalid _DDC data\n");
712 * video : video bus device pointer
714 * 0. The system BIOS should NOT automatically switch(toggle)
715 * the active display output.
716 * 1. The system BIOS should automatically switch (toggle) the
717 * active display output. No switch event.
718 * 2. The _DGS value should be locked.
719 * 3. The system BIOS should not automatically switch (toggle) the
720 * active display output, but instead generate the display switch
723 * 0. The system BIOS should automatically control the brightness level
724 * of the LCD when the power changes from AC to DC
725 * 1. The system BIOS should NOT automatically control the brightness
726 * level of the LCD when the power changes from AC to DC.
732 acpi_video_bus_DOS(struct acpi_video_bus
*video
, int bios_flag
, int lcd_flag
)
736 if (!video
->cap
._DOS
)
739 if (bios_flag
< 0 || bios_flag
> 3 || lcd_flag
< 0 || lcd_flag
> 1)
741 video
->dos_setting
= (lcd_flag
<< 2) | bios_flag
;
742 status
= acpi_execute_simple_method(video
->device
->handle
, "_DOS",
743 (lcd_flag
<< 2) | bios_flag
);
744 if (ACPI_FAILURE(status
))
751 * Simple comparison function used to sort backlight levels.
755 acpi_video_cmp_level(const void *a
, const void *b
)
757 return *(int *)a
- *(int *)b
;
761 * Decides if _BQC/_BCQ for this system is usable
763 * We do this by changing the level first and then read out the current
764 * brightness level, if the value does not match, find out if it is using
765 * index. If not, clear the _BQC/_BCQ capability.
767 static int acpi_video_bqc_quirk(struct acpi_video_device
*device
,
768 int max_level
, int current_level
)
770 struct acpi_video_device_brightness
*br
= device
->brightness
;
772 unsigned long long level
;
775 /* don't mess with existing known broken systems */
776 if (bqc_offset_aml_bug_workaround
)
780 * Some systems always report current brightness level as maximum
781 * through _BQC, we need to test another value for them.
783 test_level
= current_level
== max_level
? br
->levels
[3] : max_level
;
785 result
= acpi_video_device_lcd_set_level(device
, test_level
);
789 result
= acpi_video_device_lcd_get_level_current(device
, &level
, true);
793 if (level
!= test_level
) {
794 /* buggy _BQC found, need to find out if it uses index */
795 if (level
< br
->count
) {
796 if (br
->flags
._BCL_reversed
)
797 level
= br
->count
- 3 - level
;
798 if (br
->levels
[level
+ 2] == test_level
)
799 br
->flags
._BQC_use_index
= 1;
802 if (!br
->flags
._BQC_use_index
)
803 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
812 * device : video output device (LCD, CRT, ..)
815 * Maximum brightness level
817 * Allocate and initialize device->brightness.
821 acpi_video_init_brightness(struct acpi_video_device
*device
)
823 union acpi_object
*obj
= NULL
;
824 int i
, max_level
= 0, count
= 0, level_ac_battery
= 0;
825 unsigned long long level
, level_old
;
826 union acpi_object
*o
;
827 struct acpi_video_device_brightness
*br
= NULL
;
828 int result
= -EINVAL
;
831 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device
, &obj
))) {
832 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Could not query available "
833 "LCD brightness level\n"));
837 if (obj
->package
.count
< 2)
840 br
= kzalloc(sizeof(*br
), GFP_KERNEL
);
842 printk(KERN_ERR
"can't allocate memory\n");
847 br
->levels
= kmalloc((obj
->package
.count
+ 2) * sizeof *(br
->levels
),
854 for (i
= 0; i
< obj
->package
.count
; i
++) {
855 o
= (union acpi_object
*)&obj
->package
.elements
[i
];
856 if (o
->type
!= ACPI_TYPE_INTEGER
) {
857 printk(KERN_ERR PREFIX
"Invalid data\n");
860 value
= (u32
) o
->integer
.value
;
861 /* Skip duplicate entries */
862 if (count
> 2 && br
->levels
[count
- 1] == value
)
865 br
->levels
[count
] = value
;
867 if (br
->levels
[count
] > max_level
)
868 max_level
= br
->levels
[count
];
873 * some buggy BIOS don't export the levels
874 * when machine is on AC/Battery in _BCL package.
875 * In this case, the first two elements in _BCL packages
876 * are also supported brightness levels that OS should take care of.
878 for (i
= 2; i
< count
; i
++) {
879 if (br
->levels
[i
] == br
->levels
[0])
881 if (br
->levels
[i
] == br
->levels
[1])
885 if (level_ac_battery
< 2) {
886 level_ac_battery
= 2 - level_ac_battery
;
887 br
->flags
._BCL_no_ac_battery_levels
= 1;
888 for (i
= (count
- 1 + level_ac_battery
); i
>= 2; i
--)
889 br
->levels
[i
] = br
->levels
[i
- level_ac_battery
];
890 count
+= level_ac_battery
;
891 } else if (level_ac_battery
> 2)
892 ACPI_ERROR((AE_INFO
, "Too many duplicates in _BCL package"));
894 /* Check if the _BCL package is in a reversed order */
895 if (max_level
== br
->levels
[2]) {
896 br
->flags
._BCL_reversed
= 1;
897 sort(&br
->levels
[2], count
- 2, sizeof(br
->levels
[2]),
898 acpi_video_cmp_level
, NULL
);
899 } else if (max_level
!= br
->levels
[count
- 1])
901 "Found unordered _BCL package"));
904 device
->brightness
= br
;
906 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
907 br
->curr
= level
= max_level
;
909 if (!device
->cap
._BQC
)
912 result
= acpi_video_device_lcd_get_level_current(device
,
915 goto out_free_levels
;
917 result
= acpi_video_bqc_quirk(device
, max_level
, level_old
);
919 goto out_free_levels
;
921 * cap._BQC may get cleared due to _BQC is found to be broken
922 * in acpi_video_bqc_quirk, so check again here.
924 if (!device
->cap
._BQC
)
927 level
= acpi_video_bqc_value_to_level(device
, level_old
);
929 * On some buggy laptops, _BQC returns an uninitialized
930 * value when invoked for the first time, i.e.
931 * level_old is invalid (no matter whether it's a level
932 * or an index). Set the backlight to max_level in this case.
934 for (i
= 2; i
< br
->count
; i
++)
935 if (level
== br
->levels
[i
])
937 if (i
== br
->count
|| !level
)
941 result
= acpi_video_device_lcd_set_level(device
, level
);
943 goto out_free_levels
;
945 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
946 "found %d brightness levels\n", count
- 2));
955 device
->brightness
= NULL
;
962 * device : video output device (LCD, CRT, ..)
967 * Find out all required AML methods defined under the output
971 static void acpi_video_device_find_cap(struct acpi_video_device
*device
)
973 if (acpi_has_method(device
->dev
->handle
, "_ADR"))
974 device
->cap
._ADR
= 1;
975 if (acpi_has_method(device
->dev
->handle
, "_BCL"))
976 device
->cap
._BCL
= 1;
977 if (acpi_has_method(device
->dev
->handle
, "_BCM"))
978 device
->cap
._BCM
= 1;
979 if (acpi_has_method(device
->dev
->handle
, "_BQC")) {
980 device
->cap
._BQC
= 1;
981 } else if (acpi_has_method(device
->dev
->handle
, "_BCQ")) {
982 printk(KERN_WARNING FW_BUG
"_BCQ is used instead of _BQC\n");
983 device
->cap
._BCQ
= 1;
986 if (acpi_has_method(device
->dev
->handle
, "_DDC"))
987 device
->cap
._DDC
= 1;
992 * device : video output device (VGA)
997 * Find out all required AML methods defined under the video bus device.
1000 static void acpi_video_bus_find_cap(struct acpi_video_bus
*video
)
1002 if (acpi_has_method(video
->device
->handle
, "_DOS"))
1003 video
->cap
._DOS
= 1;
1004 if (acpi_has_method(video
->device
->handle
, "_DOD"))
1005 video
->cap
._DOD
= 1;
1006 if (acpi_has_method(video
->device
->handle
, "_ROM"))
1007 video
->cap
._ROM
= 1;
1008 if (acpi_has_method(video
->device
->handle
, "_GPD"))
1009 video
->cap
._GPD
= 1;
1010 if (acpi_has_method(video
->device
->handle
, "_SPD"))
1011 video
->cap
._SPD
= 1;
1012 if (acpi_has_method(video
->device
->handle
, "_VPO"))
1013 video
->cap
._VPO
= 1;
1017 * Check whether the video bus device has required AML method to
1018 * support the desired features
1021 static int acpi_video_bus_check(struct acpi_video_bus
*video
)
1023 acpi_status status
= -ENOENT
;
1024 struct pci_dev
*dev
;
1029 dev
= acpi_get_pci_dev(video
->device
->handle
);
1035 * Since there is no HID, CID and so on for VGA driver, we have
1036 * to check well known required nodes.
1039 /* Does this device support video switching? */
1040 if (video
->cap
._DOS
|| video
->cap
._DOD
) {
1041 if (!video
->cap
._DOS
) {
1042 printk(KERN_WARNING FW_BUG
1043 "ACPI(%s) defines _DOD but not _DOS\n",
1044 acpi_device_bid(video
->device
));
1046 video
->flags
.multihead
= 1;
1050 /* Does this device support retrieving a video ROM? */
1051 if (video
->cap
._ROM
) {
1052 video
->flags
.rom
= 1;
1056 /* Does this device support configuring which video device to POST? */
1057 if (video
->cap
._GPD
&& video
->cap
._SPD
&& video
->cap
._VPO
) {
1058 video
->flags
.post
= 1;
1066 * --------------------------------------------------------------------------
1068 * --------------------------------------------------------------------------
1071 /* device interface */
1072 static struct acpi_video_device_attrib
*
1073 acpi_video_get_device_attr(struct acpi_video_bus
*video
, unsigned long device_id
)
1075 struct acpi_video_enumerated_device
*ids
;
1078 for (i
= 0; i
< video
->attached_count
; i
++) {
1079 ids
= &video
->attached_array
[i
];
1080 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1081 return &ids
->value
.attrib
;
1088 acpi_video_get_device_type(struct acpi_video_bus
*video
,
1089 unsigned long device_id
)
1091 struct acpi_video_enumerated_device
*ids
;
1094 for (i
= 0; i
< video
->attached_count
; i
++) {
1095 ids
= &video
->attached_array
[i
];
1096 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1097 return ids
->value
.int_val
;
1104 acpi_video_bus_get_one_device(struct acpi_device
*device
,
1105 struct acpi_video_bus
*video
)
1107 unsigned long long device_id
;
1108 int status
, device_type
;
1109 struct acpi_video_device
*data
;
1110 struct acpi_video_device_attrib
*attribute
;
1113 acpi_evaluate_integer(device
->handle
, "_ADR", NULL
, &device_id
);
1114 /* Some device omits _ADR, we skip them instead of fail */
1115 if (ACPI_FAILURE(status
))
1118 data
= kzalloc(sizeof(struct acpi_video_device
), GFP_KERNEL
);
1122 strcpy(acpi_device_name(device
), ACPI_VIDEO_DEVICE_NAME
);
1123 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
1124 device
->driver_data
= data
;
1126 data
->device_id
= device_id
;
1127 data
->video
= video
;
1129 INIT_DELAYED_WORK(&data
->switch_brightness_work
,
1130 acpi_video_switch_brightness
);
1132 attribute
= acpi_video_get_device_attr(video
, device_id
);
1134 if (attribute
&& attribute
->device_id_scheme
) {
1135 switch (attribute
->display_type
) {
1136 case ACPI_VIDEO_DISPLAY_CRT
:
1137 data
->flags
.crt
= 1;
1139 case ACPI_VIDEO_DISPLAY_TV
:
1140 data
->flags
.tvout
= 1;
1142 case ACPI_VIDEO_DISPLAY_DVI
:
1143 data
->flags
.dvi
= 1;
1145 case ACPI_VIDEO_DISPLAY_LCD
:
1146 data
->flags
.lcd
= 1;
1149 data
->flags
.unknown
= 1;
1152 if (attribute
->bios_can_detect
)
1153 data
->flags
.bios
= 1;
1155 /* Check for legacy IDs */
1156 device_type
= acpi_video_get_device_type(video
, device_id
);
1157 /* Ignore bits 16 and 18-20 */
1158 switch (device_type
& 0xffe2ffff) {
1159 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR
:
1160 data
->flags
.crt
= 1;
1162 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL
:
1163 data
->flags
.lcd
= 1;
1165 case ACPI_VIDEO_DISPLAY_LEGACY_TV
:
1166 data
->flags
.tvout
= 1;
1169 data
->flags
.unknown
= 1;
1173 acpi_video_device_bind(video
, data
);
1174 acpi_video_device_find_cap(data
);
1176 mutex_lock(&video
->device_list_lock
);
1177 list_add_tail(&data
->entry
, &video
->video_device_list
);
1178 mutex_unlock(&video
->device_list_lock
);
1185 * video : video bus device
1190 * Enumerate the video device list of the video bus,
1191 * bind the ids with the corresponding video devices
1192 * under the video bus.
1195 static void acpi_video_device_rebind(struct acpi_video_bus
*video
)
1197 struct acpi_video_device
*dev
;
1199 mutex_lock(&video
->device_list_lock
);
1201 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1202 acpi_video_device_bind(video
, dev
);
1204 mutex_unlock(&video
->device_list_lock
);
1209 * video : video bus device
1210 * device : video output device under the video
1216 * Bind the ids with the corresponding video devices
1217 * under the video bus.
1221 acpi_video_device_bind(struct acpi_video_bus
*video
,
1222 struct acpi_video_device
*device
)
1224 struct acpi_video_enumerated_device
*ids
;
1227 for (i
= 0; i
< video
->attached_count
; i
++) {
1228 ids
= &video
->attached_array
[i
];
1229 if (device
->device_id
== (ids
->value
.int_val
& 0xffff)) {
1230 ids
->bind_info
= device
;
1231 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "device_bind %d\n", i
));
1236 static bool acpi_video_device_in_dod(struct acpi_video_device
*device
)
1238 struct acpi_video_bus
*video
= device
->video
;
1242 * If we have a broken _DOD or we have more than 8 output devices
1243 * under the graphics controller node that we can't proper deal with
1244 * in the operation region code currently, no need to test.
1246 if (!video
->attached_count
|| video
->child_count
> 8)
1249 for (i
= 0; i
< video
->attached_count
; i
++) {
1250 if ((video
->attached_array
[i
].value
.int_val
& 0xfff) ==
1251 (device
->device_id
& 0xfff))
1260 * video : video bus device
1265 * Call _DOD to enumerate all devices attached to display adapter
1269 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
)
1274 struct acpi_video_enumerated_device
*active_list
;
1275 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1276 union acpi_object
*dod
= NULL
;
1277 union acpi_object
*obj
;
1279 status
= acpi_evaluate_object(video
->device
->handle
, "_DOD", NULL
, &buffer
);
1280 if (!ACPI_SUCCESS(status
)) {
1281 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _DOD"));
1285 dod
= buffer
.pointer
;
1286 if (!dod
|| (dod
->type
!= ACPI_TYPE_PACKAGE
)) {
1287 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _DOD data"));
1292 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d video heads in _DOD\n",
1293 dod
->package
.count
));
1295 active_list
= kcalloc(1 + dod
->package
.count
,
1296 sizeof(struct acpi_video_enumerated_device
),
1304 for (i
= 0; i
< dod
->package
.count
; i
++) {
1305 obj
= &dod
->package
.elements
[i
];
1307 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
1308 printk(KERN_ERR PREFIX
1309 "Invalid _DOD data in element %d\n", i
);
1313 active_list
[count
].value
.int_val
= obj
->integer
.value
;
1314 active_list
[count
].bind_info
= NULL
;
1315 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "dod element[%d] = %d\n", i
,
1316 (int)obj
->integer
.value
));
1320 kfree(video
->attached_array
);
1322 video
->attached_array
= active_list
;
1323 video
->attached_count
= count
;
1326 kfree(buffer
.pointer
);
1331 acpi_video_get_next_level(struct acpi_video_device
*device
,
1332 u32 level_current
, u32 event
)
1334 int min
, max
, min_above
, max_below
, i
, l
, delta
= 255;
1335 max
= max_below
= 0;
1336 min
= min_above
= 255;
1337 /* Find closest level to level_current */
1338 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1339 l
= device
->brightness
->levels
[i
];
1340 if (abs(l
- level_current
) < abs(delta
)) {
1341 delta
= l
- level_current
;
1346 /* Ajust level_current to closest available level */
1347 level_current
+= delta
;
1348 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1349 l
= device
->brightness
->levels
[i
];
1354 if (l
< min_above
&& l
> level_current
)
1356 if (l
> max_below
&& l
< level_current
)
1361 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
:
1362 return (level_current
< max
) ? min_above
: min
;
1363 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
:
1364 return (level_current
< max
) ? min_above
: max
;
1365 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
:
1366 return (level_current
> min
) ? max_below
: min
;
1367 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
:
1368 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
:
1371 return level_current
;
1376 acpi_video_switch_brightness(struct work_struct
*work
)
1378 struct acpi_video_device
*device
= container_of(to_delayed_work(work
),
1379 struct acpi_video_device
, switch_brightness_work
);
1380 unsigned long long level_current
, level_next
;
1381 int event
= device
->switch_brightness_event
;
1382 int result
= -EINVAL
;
1384 /* no warning message if acpi_backlight=vendor or a quirk is used */
1385 if (!acpi_video_verify_backlight_support())
1388 if (!device
->brightness
)
1391 result
= acpi_video_device_lcd_get_level_current(device
,
1397 level_next
= acpi_video_get_next_level(device
, level_current
, event
);
1399 result
= acpi_video_device_lcd_set_level(device
, level_next
);
1402 backlight_force_update(device
->backlight
,
1403 BACKLIGHT_UPDATE_HOTKEY
);
1407 printk(KERN_ERR PREFIX
"Failed to switch the brightness\n");
1410 int acpi_video_get_edid(struct acpi_device
*device
, int type
, int device_id
,
1413 struct acpi_video_bus
*video
;
1414 struct acpi_video_device
*video_device
;
1415 union acpi_object
*buffer
= NULL
;
1419 if (!device
|| !acpi_driver_data(device
))
1422 video
= acpi_driver_data(device
);
1424 for (i
= 0; i
< video
->attached_count
; i
++) {
1425 video_device
= video
->attached_array
[i
].bind_info
;
1431 if (!video_device
->cap
._DDC
)
1436 case ACPI_VIDEO_DISPLAY_CRT
:
1437 if (!video_device
->flags
.crt
)
1440 case ACPI_VIDEO_DISPLAY_TV
:
1441 if (!video_device
->flags
.tvout
)
1444 case ACPI_VIDEO_DISPLAY_DVI
:
1445 if (!video_device
->flags
.dvi
)
1448 case ACPI_VIDEO_DISPLAY_LCD
:
1449 if (!video_device
->flags
.lcd
)
1453 } else if (video_device
->device_id
!= device_id
) {
1457 status
= acpi_video_device_EDID(video_device
, &buffer
, length
);
1459 if (ACPI_FAILURE(status
) || !buffer
||
1460 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1462 status
= acpi_video_device_EDID(video_device
, &buffer
,
1464 if (ACPI_FAILURE(status
) || !buffer
||
1465 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1470 *edid
= buffer
->buffer
.pointer
;
1476 EXPORT_SYMBOL(acpi_video_get_edid
);
1479 acpi_video_bus_get_devices(struct acpi_video_bus
*video
,
1480 struct acpi_device
*device
)
1483 struct acpi_device
*dev
;
1486 * There are systems where video module known to work fine regardless
1487 * of broken _DOD and ignoring returned value here doesn't cause
1490 acpi_video_device_enumerate(video
);
1492 list_for_each_entry(dev
, &device
->children
, node
) {
1494 status
= acpi_video_bus_get_one_device(dev
, video
);
1496 dev_err(&dev
->dev
, "Can't attach device\n");
1499 video
->child_count
++;
1504 /* acpi_video interface */
1507 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1508 * preform any automatic brightness change on receiving a notification.
1510 static int acpi_video_bus_start_devices(struct acpi_video_bus
*video
)
1512 return acpi_video_bus_DOS(video
, 0,
1513 acpi_osi_is_win8() ? 1 : 0);
1516 static int acpi_video_bus_stop_devices(struct acpi_video_bus
*video
)
1518 return acpi_video_bus_DOS(video
, 0,
1519 acpi_osi_is_win8() ? 0 : 1);
1522 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
)
1524 struct acpi_video_bus
*video
= acpi_driver_data(device
);
1525 struct input_dev
*input
;
1528 if (!video
|| !video
->input
)
1531 input
= video
->input
;
1534 case ACPI_VIDEO_NOTIFY_SWITCH
: /* User requested a switch,
1535 * most likely via hotkey. */
1536 keycode
= KEY_SWITCHVIDEOMODE
;
1539 case ACPI_VIDEO_NOTIFY_PROBE
: /* User plugged in or removed a video
1541 acpi_video_device_enumerate(video
);
1542 acpi_video_device_rebind(video
);
1543 keycode
= KEY_SWITCHVIDEOMODE
;
1546 case ACPI_VIDEO_NOTIFY_CYCLE
: /* Cycle Display output hotkey pressed. */
1547 keycode
= KEY_SWITCHVIDEOMODE
;
1549 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT
: /* Next Display output hotkey pressed. */
1550 keycode
= KEY_VIDEO_NEXT
;
1552 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT
: /* previous Display output hotkey pressed. */
1553 keycode
= KEY_VIDEO_PREV
;
1557 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1558 "Unsupported event [0x%x]\n", event
));
1562 if (acpi_notifier_call_chain(device
, event
, 0))
1563 /* Something vetoed the keypress. */
1567 input_report_key(input
, keycode
, 1);
1569 input_report_key(input
, keycode
, 0);
1576 static void brightness_switch_event(struct acpi_video_device
*video_device
,
1579 if (!brightness_switch_enabled
)
1582 video_device
->switch_brightness_event
= event
;
1583 schedule_delayed_work(&video_device
->switch_brightness_work
, HZ
/ 10);
1586 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
)
1588 struct acpi_video_device
*video_device
= data
;
1589 struct acpi_device
*device
= NULL
;
1590 struct acpi_video_bus
*bus
;
1591 struct input_dev
*input
;
1597 device
= video_device
->dev
;
1598 bus
= video_device
->video
;
1602 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
: /* Cycle brightness */
1603 brightness_switch_event(video_device
, event
);
1604 keycode
= KEY_BRIGHTNESS_CYCLE
;
1606 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
: /* Increase brightness */
1607 brightness_switch_event(video_device
, event
);
1608 keycode
= KEY_BRIGHTNESSUP
;
1610 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
: /* Decrease brightness */
1611 brightness_switch_event(video_device
, event
);
1612 keycode
= KEY_BRIGHTNESSDOWN
;
1614 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
: /* zero brightness */
1615 brightness_switch_event(video_device
, event
);
1616 keycode
= KEY_BRIGHTNESS_ZERO
;
1618 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
: /* display device off */
1619 brightness_switch_event(video_device
, event
);
1620 keycode
= KEY_DISPLAY_OFF
;
1623 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1624 "Unsupported event [0x%x]\n", event
));
1628 acpi_notifier_call_chain(device
, event
, 0);
1631 input_report_key(input
, keycode
, 1);
1633 input_report_key(input
, keycode
, 0);
1640 static int acpi_video_resume(struct notifier_block
*nb
,
1641 unsigned long val
, void *ign
)
1643 struct acpi_video_bus
*video
;
1644 struct acpi_video_device
*video_device
;
1648 case PM_HIBERNATION_PREPARE
:
1649 case PM_SUSPEND_PREPARE
:
1650 case PM_RESTORE_PREPARE
:
1654 video
= container_of(nb
, struct acpi_video_bus
, pm_nb
);
1656 dev_info(&video
->device
->dev
, "Restoring backlight state\n");
1658 for (i
= 0; i
< video
->attached_count
; i
++) {
1659 video_device
= video
->attached_array
[i
].bind_info
;
1660 if (video_device
&& video_device
->backlight
)
1661 acpi_video_set_brightness(video_device
->backlight
);
1668 acpi_video_bus_match(acpi_handle handle
, u32 level
, void *context
,
1669 void **return_value
)
1671 struct acpi_device
*device
= context
;
1672 struct acpi_device
*sibling
;
1675 if (handle
== device
->handle
)
1676 return AE_CTRL_TERMINATE
;
1678 result
= acpi_bus_get_device(handle
, &sibling
);
1682 if (!strcmp(acpi_device_name(sibling
), ACPI_VIDEO_BUS_NAME
))
1683 return AE_ALREADY_EXISTS
;
1688 static void acpi_video_dev_register_backlight(struct acpi_video_device
*device
)
1690 struct backlight_properties props
;
1691 struct pci_dev
*pdev
;
1692 acpi_handle acpi_parent
;
1693 struct device
*parent
= NULL
;
1699 * Do not create backlight device for video output
1700 * device that is not in the enumerated list.
1702 if (!acpi_video_device_in_dod(device
)) {
1703 dev_dbg(&device
->dev
->dev
, "not in _DOD list, ignore\n");
1707 result
= acpi_video_init_brightness(device
);
1710 name
= kasprintf(GFP_KERNEL
, "acpi_video%d", count
);
1715 acpi_get_parent(device
->dev
->handle
, &acpi_parent
);
1717 pdev
= acpi_get_pci_dev(acpi_parent
);
1719 parent
= &pdev
->dev
;
1723 memset(&props
, 0, sizeof(struct backlight_properties
));
1724 props
.type
= BACKLIGHT_FIRMWARE
;
1725 props
.max_brightness
= device
->brightness
->count
- 3;
1726 device
->backlight
= backlight_device_register(name
,
1729 &acpi_backlight_ops
,
1732 if (IS_ERR(device
->backlight
))
1736 * Save current brightness level in case we have to restore it
1737 * before acpi_video_device_lcd_set_level() is called next time.
1739 device
->backlight
->props
.brightness
=
1740 acpi_video_get_brightness(device
->backlight
);
1742 device
->cooling_dev
= thermal_cooling_device_register("LCD",
1743 device
->dev
, &video_cooling_ops
);
1744 if (IS_ERR(device
->cooling_dev
)) {
1746 * Set cooling_dev to NULL so we don't crash trying to free it.
1747 * Also, why the hell we are returning early and not attempt to
1748 * register video output if cooling device registration failed?
1751 device
->cooling_dev
= NULL
;
1755 dev_info(&device
->dev
->dev
, "registered as cooling_device%d\n",
1756 device
->cooling_dev
->id
);
1757 result
= sysfs_create_link(&device
->dev
->dev
.kobj
,
1758 &device
->cooling_dev
->device
.kobj
,
1761 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1762 result
= sysfs_create_link(&device
->cooling_dev
->device
.kobj
,
1763 &device
->dev
->dev
.kobj
, "device");
1765 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1768 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus
*video
)
1770 struct acpi_video_device
*dev
;
1771 union acpi_object
*levels
;
1773 mutex_lock(&video
->device_list_lock
);
1774 list_for_each_entry(dev
, &video
->video_device_list
, entry
) {
1775 if (!acpi_video_device_lcd_query_levels(dev
, &levels
))
1778 mutex_unlock(&video
->device_list_lock
);
1781 static int acpi_video_bus_register_backlight(struct acpi_video_bus
*video
)
1783 struct acpi_video_device
*dev
;
1785 if (video
->backlight_registered
)
1788 acpi_video_run_bcl_for_osi(video
);
1790 if (!acpi_video_verify_backlight_support())
1793 mutex_lock(&video
->device_list_lock
);
1794 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1795 acpi_video_dev_register_backlight(dev
);
1796 mutex_unlock(&video
->device_list_lock
);
1798 video
->backlight_registered
= true;
1800 video
->pm_nb
.notifier_call
= acpi_video_resume
;
1801 video
->pm_nb
.priority
= 0;
1802 return register_pm_notifier(&video
->pm_nb
);
1805 static void acpi_video_dev_unregister_backlight(struct acpi_video_device
*device
)
1807 if (device
->backlight
) {
1808 backlight_device_unregister(device
->backlight
);
1809 device
->backlight
= NULL
;
1811 if (device
->brightness
) {
1812 kfree(device
->brightness
->levels
);
1813 kfree(device
->brightness
);
1814 device
->brightness
= NULL
;
1816 if (device
->cooling_dev
) {
1817 sysfs_remove_link(&device
->dev
->dev
.kobj
, "thermal_cooling");
1818 sysfs_remove_link(&device
->cooling_dev
->device
.kobj
, "device");
1819 thermal_cooling_device_unregister(device
->cooling_dev
);
1820 device
->cooling_dev
= NULL
;
1824 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus
*video
)
1826 struct acpi_video_device
*dev
;
1829 if (!video
->backlight_registered
)
1832 error
= unregister_pm_notifier(&video
->pm_nb
);
1834 mutex_lock(&video
->device_list_lock
);
1835 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1836 acpi_video_dev_unregister_backlight(dev
);
1837 mutex_unlock(&video
->device_list_lock
);
1839 video
->backlight_registered
= false;
1844 static void acpi_video_dev_add_notify_handler(struct acpi_video_device
*device
)
1847 struct acpi_device
*adev
= device
->dev
;
1849 status
= acpi_install_notify_handler(adev
->handle
, ACPI_DEVICE_NOTIFY
,
1850 acpi_video_device_notify
, device
);
1851 if (ACPI_FAILURE(status
))
1852 dev_err(&adev
->dev
, "Error installing notify handler\n");
1854 device
->flags
.notify
= 1;
1857 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus
*video
)
1859 struct input_dev
*input
;
1860 struct acpi_video_device
*dev
;
1863 video
->input
= input
= input_allocate_device();
1869 error
= acpi_video_bus_start_devices(video
);
1871 goto err_free_input
;
1873 snprintf(video
->phys
, sizeof(video
->phys
),
1874 "%s/video/input0", acpi_device_hid(video
->device
));
1876 input
->name
= acpi_device_name(video
->device
);
1877 input
->phys
= video
->phys
;
1878 input
->id
.bustype
= BUS_HOST
;
1879 input
->id
.product
= 0x06;
1880 input
->dev
.parent
= &video
->device
->dev
;
1881 input
->evbit
[0] = BIT(EV_KEY
);
1882 set_bit(KEY_SWITCHVIDEOMODE
, input
->keybit
);
1883 set_bit(KEY_VIDEO_NEXT
, input
->keybit
);
1884 set_bit(KEY_VIDEO_PREV
, input
->keybit
);
1885 set_bit(KEY_BRIGHTNESS_CYCLE
, input
->keybit
);
1886 set_bit(KEY_BRIGHTNESSUP
, input
->keybit
);
1887 set_bit(KEY_BRIGHTNESSDOWN
, input
->keybit
);
1888 set_bit(KEY_BRIGHTNESS_ZERO
, input
->keybit
);
1889 set_bit(KEY_DISPLAY_OFF
, input
->keybit
);
1891 error
= input_register_device(input
);
1895 mutex_lock(&video
->device_list_lock
);
1896 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1897 acpi_video_dev_add_notify_handler(dev
);
1898 mutex_unlock(&video
->device_list_lock
);
1903 acpi_video_bus_stop_devices(video
);
1905 input_free_device(input
);
1906 video
->input
= NULL
;
1911 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device
*dev
)
1913 if (dev
->flags
.notify
) {
1914 acpi_remove_notify_handler(dev
->dev
->handle
, ACPI_DEVICE_NOTIFY
,
1915 acpi_video_device_notify
);
1916 dev
->flags
.notify
= 0;
1920 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus
*video
)
1922 struct acpi_video_device
*dev
;
1924 mutex_lock(&video
->device_list_lock
);
1925 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1926 acpi_video_dev_remove_notify_handler(dev
);
1927 mutex_unlock(&video
->device_list_lock
);
1929 acpi_video_bus_stop_devices(video
);
1930 input_unregister_device(video
->input
);
1931 video
->input
= NULL
;
1934 static int acpi_video_backlight_notify(struct notifier_block
*nb
,
1935 unsigned long val
, void *bd
)
1937 struct backlight_device
*backlight
= bd
;
1938 struct acpi_video_bus
*video
;
1940 /* acpi_video_verify_backlight_support only cares about raw devices */
1941 if (backlight
->props
.type
!= BACKLIGHT_RAW
)
1944 video
= container_of(nb
, struct acpi_video_bus
, backlight_nb
);
1947 case BACKLIGHT_REGISTERED
:
1948 if (!acpi_video_verify_backlight_support())
1949 acpi_video_bus_unregister_backlight(video
);
1951 case BACKLIGHT_UNREGISTERED
:
1952 acpi_video_bus_register_backlight(video
);
1959 static int acpi_video_bus_add_backlight_notify_handler(
1960 struct acpi_video_bus
*video
)
1964 video
->backlight_nb
.notifier_call
= acpi_video_backlight_notify
;
1965 video
->backlight_nb
.priority
= 0;
1966 error
= backlight_register_notifier(&video
->backlight_nb
);
1968 video
->backlight_notifier_registered
= true;
1973 static int acpi_video_bus_remove_backlight_notify_handler(
1974 struct acpi_video_bus
*video
)
1976 if (!video
->backlight_notifier_registered
)
1979 video
->backlight_notifier_registered
= false;
1981 return backlight_unregister_notifier(&video
->backlight_nb
);
1984 static int acpi_video_bus_put_devices(struct acpi_video_bus
*video
)
1986 struct acpi_video_device
*dev
, *next
;
1988 mutex_lock(&video
->device_list_lock
);
1989 list_for_each_entry_safe(dev
, next
, &video
->video_device_list
, entry
) {
1990 list_del(&dev
->entry
);
1993 mutex_unlock(&video
->device_list_lock
);
1998 static int instance
;
2000 static int acpi_video_bus_add(struct acpi_device
*device
)
2002 struct acpi_video_bus
*video
;
2006 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
,
2007 device
->parent
->handle
, 1,
2008 acpi_video_bus_match
, NULL
,
2010 if (status
== AE_ALREADY_EXISTS
) {
2011 printk(KERN_WARNING FW_BUG
2012 "Duplicate ACPI video bus devices for the"
2013 " same VGA controller, please try module "
2014 "parameter \"video.allow_duplicates=1\""
2015 "if the current driver doesn't work.\n");
2016 if (!allow_duplicates
)
2020 video
= kzalloc(sizeof(struct acpi_video_bus
), GFP_KERNEL
);
2024 /* a hack to fix the duplicate name "VID" problem on T61 */
2025 if (!strcmp(device
->pnp
.bus_id
, "VID")) {
2027 device
->pnp
.bus_id
[3] = '0' + instance
;
2030 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2031 if (!strcmp(device
->pnp
.bus_id
, "VGA")) {
2033 device
->pnp
.bus_id
[3] = '0' + instance
;
2037 video
->device
= device
;
2038 strcpy(acpi_device_name(device
), ACPI_VIDEO_BUS_NAME
);
2039 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
2040 device
->driver_data
= video
;
2042 acpi_video_bus_find_cap(video
);
2043 error
= acpi_video_bus_check(video
);
2045 goto err_free_video
;
2047 mutex_init(&video
->device_list_lock
);
2048 INIT_LIST_HEAD(&video
->video_device_list
);
2050 error
= acpi_video_bus_get_devices(video
, device
);
2054 printk(KERN_INFO PREFIX
"%s [%s] (multi-head: %s rom: %s post: %s)\n",
2055 ACPI_VIDEO_DEVICE_NAME
, acpi_device_bid(device
),
2056 video
->flags
.multihead
? "yes" : "no",
2057 video
->flags
.rom
? "yes" : "no",
2058 video
->flags
.post
? "yes" : "no");
2059 mutex_lock(&video_list_lock
);
2060 list_add_tail(&video
->entry
, &video_bus_head
);
2061 mutex_unlock(&video_list_lock
);
2063 acpi_video_bus_register_backlight(video
);
2064 acpi_video_bus_add_notify_handler(video
);
2065 acpi_video_bus_add_backlight_notify_handler(video
);
2070 acpi_video_bus_put_devices(video
);
2071 kfree(video
->attached_array
);
2074 device
->driver_data
= NULL
;
2079 static int acpi_video_bus_remove(struct acpi_device
*device
)
2081 struct acpi_video_bus
*video
= NULL
;
2084 if (!device
|| !acpi_driver_data(device
))
2087 video
= acpi_driver_data(device
);
2089 acpi_video_bus_remove_backlight_notify_handler(video
);
2090 acpi_video_bus_remove_notify_handler(video
);
2091 acpi_video_bus_unregister_backlight(video
);
2092 acpi_video_bus_put_devices(video
);
2094 mutex_lock(&video_list_lock
);
2095 list_del(&video
->entry
);
2096 mutex_unlock(&video_list_lock
);
2098 kfree(video
->attached_array
);
2104 static int __init
is_i740(struct pci_dev
*dev
)
2106 if (dev
->device
== 0x00D1)
2108 if (dev
->device
== 0x7000)
2113 static int __init
intel_opregion_present(void)
2116 struct pci_dev
*dev
= NULL
;
2119 for_each_pci_dev(dev
) {
2120 if ((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
2122 if (dev
->vendor
!= PCI_VENDOR_ID_INTEL
)
2124 /* We don't want to poke around undefined i740 registers */
2127 pci_read_config_dword(dev
, 0xfc, &address
);
2135 int acpi_video_register(void)
2139 if (register_count
) {
2141 * if the function of acpi_video_register is already called,
2142 * don't register the acpi_vide_bus again and return no error.
2147 mutex_init(&video_list_lock
);
2148 INIT_LIST_HEAD(&video_bus_head
);
2150 ret
= acpi_bus_register_driver(&acpi_video_bus
);
2155 * When the acpi_video_bus is loaded successfully, increase
2156 * the counter reference.
2162 EXPORT_SYMBOL(acpi_video_register
);
2164 void acpi_video_unregister(void)
2166 if (!register_count
) {
2168 * If the acpi video bus is already unloaded, don't
2169 * unload it again and return directly.
2173 acpi_bus_unregister_driver(&acpi_video_bus
);
2179 EXPORT_SYMBOL(acpi_video_unregister
);
2181 void acpi_video_unregister_backlight(void)
2183 struct acpi_video_bus
*video
;
2185 if (!register_count
)
2188 mutex_lock(&video_list_lock
);
2189 list_for_each_entry(video
, &video_bus_head
, entry
)
2190 acpi_video_bus_unregister_backlight(video
);
2191 mutex_unlock(&video_list_lock
);
2193 EXPORT_SYMBOL(acpi_video_unregister_backlight
);
2196 * This is kind of nasty. Hardware using Intel chipsets may require
2197 * the video opregion code to be run first in order to initialise
2198 * state before any ACPI video calls are made. To handle this we defer
2199 * registration of the video class until the opregion code has run.
2202 static int __init
acpi_video_init(void)
2205 * Let the module load even if ACPI is disabled (e.g. due to
2206 * a broken BIOS) so that i915.ko can still be loaded on such
2207 * old systems without an AcpiOpRegion.
2209 * acpi_video_register() will report -ENODEV later as well due
2210 * to acpi_disabled when i915.ko tries to register itself afterwards.
2215 dmi_check_system(video_dmi_table
);
2217 if (intel_opregion_present())
2220 return acpi_video_register();
2223 static void __exit
acpi_video_exit(void)
2225 acpi_video_unregister();
2230 module_init(acpi_video_init
);
2231 module_exit(acpi_video_exit
);