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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/mutex.h>
29 #include <linux/input.h>
30 #include <linux/backlight.h>
31 #include <linux/thermal.h>
32 #include <linux/sort.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
35 #include <linux/slab.h>
36 #include <linux/dmi.h>
37 #include <linux/suspend.h>
38 #include <linux/acpi.h>
39 #include <acpi/video.h>
40 #include <linux/uaccess.h>
42 #define PREFIX "ACPI: "
44 #define ACPI_VIDEO_BUS_NAME "Video Bus"
45 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
47 #define MAX_NAME_LEN 20
49 #define _COMPONENT ACPI_VIDEO_COMPONENT
50 ACPI_MODULE_NAME("video");
52 MODULE_AUTHOR("Bruno Ducrot");
53 MODULE_DESCRIPTION("ACPI Video Driver");
54 MODULE_LICENSE("GPL");
56 static bool brightness_switch_enabled
= 1;
57 module_param(brightness_switch_enabled
, bool, 0644);
60 * By default, we don't allow duplicate ACPI video bus devices
61 * under the same VGA controller
63 static bool allow_duplicates
;
64 module_param(allow_duplicates
, bool, 0644);
66 static int disable_backlight_sysfs_if
= -1;
67 module_param(disable_backlight_sysfs_if
, int, 0444);
69 #define REPORT_OUTPUT_KEY_EVENTS 0x01
70 #define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
71 static int report_key_events
= -1;
72 module_param(report_key_events
, int, 0644);
73 MODULE_PARM_DESC(report_key_events
,
74 "0: none, 1: output changes, 2: brightness changes, 3: all");
76 static bool device_id_scheme
= false;
77 module_param(device_id_scheme
, bool, 0444);
79 static bool only_lcd
= false;
80 module_param(only_lcd
, bool, 0444);
82 static int register_count
;
83 static DEFINE_MUTEX(register_count_mutex
);
84 static DEFINE_MUTEX(video_list_lock
);
85 static LIST_HEAD(video_bus_head
);
86 static int acpi_video_bus_add(struct acpi_device
*device
);
87 static int acpi_video_bus_remove(struct acpi_device
*device
);
88 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
);
89 void acpi_video_detect_exit(void);
91 static const struct acpi_device_id video_device_ids
[] = {
95 MODULE_DEVICE_TABLE(acpi
, video_device_ids
);
97 static struct acpi_driver acpi_video_bus
= {
99 .class = ACPI_VIDEO_CLASS
,
100 .ids
= video_device_ids
,
102 .add
= acpi_video_bus_add
,
103 .remove
= acpi_video_bus_remove
,
104 .notify
= acpi_video_bus_notify
,
108 struct acpi_video_bus_flags
{
109 u8 multihead
:1; /* can switch video heads */
110 u8 rom
:1; /* can retrieve a video rom */
111 u8 post
:1; /* can configure the head to */
115 struct acpi_video_bus_cap
{
116 u8 _DOS
:1; /* Enable/Disable output switching */
117 u8 _DOD
:1; /* Enumerate all devices attached to display adapter */
118 u8 _ROM
:1; /* Get ROM Data */
119 u8 _GPD
:1; /* Get POST Device */
120 u8 _SPD
:1; /* Set POST Device */
121 u8 _VPO
:1; /* Video POST Options */
125 struct acpi_video_device_attrib
{
126 u32 display_index
:4; /* A zero-based instance of the Display */
127 u32 display_port_attachment
:4; /* This field differentiates the display type */
128 u32 display_type
:4; /* Describe the specific type in use */
129 u32 vendor_specific
:4; /* Chipset Vendor Specific */
130 u32 bios_can_detect
:1; /* BIOS can detect the device */
131 u32 depend_on_vga
:1; /* Non-VGA output device whose power is related to
133 u32 pipe_id
:3; /* For VGA multiple-head devices. */
134 u32 reserved
:10; /* Must be 0 */
135 u32 device_id_scheme
:1; /* Device ID Scheme */
138 struct acpi_video_enumerated_device
{
141 struct acpi_video_device_attrib attrib
;
143 struct acpi_video_device
*bind_info
;
146 struct acpi_video_bus
{
147 struct acpi_device
*device
;
148 bool backlight_registered
;
150 struct acpi_video_enumerated_device
*attached_array
;
153 struct acpi_video_bus_cap cap
;
154 struct acpi_video_bus_flags flags
;
155 struct list_head video_device_list
;
156 struct mutex device_list_lock
; /* protects video_device_list */
157 struct list_head entry
;
158 struct input_dev
*input
;
159 char phys
[32]; /* for input device */
160 struct notifier_block pm_nb
;
163 struct acpi_video_device_flags
{
174 struct acpi_video_device_cap
{
175 u8 _ADR
:1; /* Return the unique ID */
176 u8 _BCL
:1; /* Query list of brightness control levels supported */
177 u8 _BCM
:1; /* Set the brightness level */
178 u8 _BQC
:1; /* Get current brightness level */
179 u8 _BCQ
:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
180 u8 _DDC
:1; /* Return the EDID for this device */
183 struct acpi_video_device
{
184 unsigned long device_id
;
185 struct acpi_video_device_flags flags
;
186 struct acpi_video_device_cap cap
;
187 struct list_head entry
;
188 struct delayed_work switch_brightness_work
;
189 int switch_brightness_event
;
190 struct acpi_video_bus
*video
;
191 struct acpi_device
*dev
;
192 struct acpi_video_device_brightness
*brightness
;
193 struct backlight_device
*backlight
;
194 struct thermal_cooling_device
*cooling_dev
;
197 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
);
198 static void acpi_video_device_rebind(struct acpi_video_bus
*video
);
199 static void acpi_video_device_bind(struct acpi_video_bus
*video
,
200 struct acpi_video_device
*device
);
201 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
);
202 static int acpi_video_device_lcd_set_level(struct acpi_video_device
*device
,
204 static int acpi_video_device_lcd_get_level_current(
205 struct acpi_video_device
*device
,
206 unsigned long long *level
, bool raw
);
207 static int acpi_video_get_next_level(struct acpi_video_device
*device
,
208 u32 level_current
, u32 event
);
209 static void acpi_video_switch_brightness(struct work_struct
*work
);
211 /* backlight device sysfs support */
212 static int acpi_video_get_brightness(struct backlight_device
*bd
)
214 unsigned long long cur_level
;
216 struct acpi_video_device
*vd
= bl_get_data(bd
);
218 if (acpi_video_device_lcd_get_level_current(vd
, &cur_level
, false))
220 for (i
= 2; i
< vd
->brightness
->count
; i
++) {
221 if (vd
->brightness
->levels
[i
] == cur_level
)
223 * The first two entries are special - see page 575
224 * of the ACPI spec 3.0
231 static int acpi_video_set_brightness(struct backlight_device
*bd
)
233 int request_level
= bd
->props
.brightness
+ 2;
234 struct acpi_video_device
*vd
= bl_get_data(bd
);
236 cancel_delayed_work(&vd
->switch_brightness_work
);
237 return acpi_video_device_lcd_set_level(vd
,
238 vd
->brightness
->levels
[request_level
]);
241 static const struct backlight_ops acpi_backlight_ops
= {
242 .get_brightness
= acpi_video_get_brightness
,
243 .update_status
= acpi_video_set_brightness
,
246 /* thermal cooling device callbacks */
247 static int video_get_max_state(struct thermal_cooling_device
*cooling_dev
, unsigned
250 struct acpi_device
*device
= cooling_dev
->devdata
;
251 struct acpi_video_device
*video
= acpi_driver_data(device
);
253 *state
= video
->brightness
->count
- 3;
257 static int video_get_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned
260 struct acpi_device
*device
= cooling_dev
->devdata
;
261 struct acpi_video_device
*video
= acpi_driver_data(device
);
262 unsigned long long level
;
265 if (acpi_video_device_lcd_get_level_current(video
, &level
, false))
267 for (offset
= 2; offset
< video
->brightness
->count
; offset
++)
268 if (level
== video
->brightness
->levels
[offset
]) {
269 *state
= video
->brightness
->count
- offset
- 1;
277 video_set_cur_state(struct thermal_cooling_device
*cooling_dev
, unsigned long state
)
279 struct acpi_device
*device
= cooling_dev
->devdata
;
280 struct acpi_video_device
*video
= acpi_driver_data(device
);
283 if (state
>= video
->brightness
->count
- 2)
286 state
= video
->brightness
->count
- state
;
287 level
= video
->brightness
->levels
[state
- 1];
288 return acpi_video_device_lcd_set_level(video
, level
);
291 static const struct thermal_cooling_device_ops video_cooling_ops
= {
292 .get_max_state
= video_get_max_state
,
293 .get_cur_state
= video_get_cur_state
,
294 .set_cur_state
= video_set_cur_state
,
298 * --------------------------------------------------------------------------
300 * --------------------------------------------------------------------------
304 acpi_video_device_lcd_query_levels(acpi_handle handle
,
305 union acpi_object
**levels
)
308 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
309 union acpi_object
*obj
;
314 status
= acpi_evaluate_object(handle
, "_BCL", NULL
, &buffer
);
315 if (!ACPI_SUCCESS(status
))
317 obj
= (union acpi_object
*)buffer
.pointer
;
318 if (!obj
|| (obj
->type
!= ACPI_TYPE_PACKAGE
)) {
319 printk(KERN_ERR PREFIX
"Invalid _BCL data\n");
329 kfree(buffer
.pointer
);
335 acpi_video_device_lcd_set_level(struct acpi_video_device
*device
, int level
)
340 status
= acpi_execute_simple_method(device
->dev
->handle
,
342 if (ACPI_FAILURE(status
)) {
343 ACPI_ERROR((AE_INFO
, "Evaluating _BCM failed"));
347 device
->brightness
->curr
= level
;
348 for (state
= 2; state
< device
->brightness
->count
; state
++)
349 if (level
== device
->brightness
->levels
[state
]) {
350 if (device
->backlight
)
351 device
->backlight
->props
.brightness
= state
- 2;
355 ACPI_ERROR((AE_INFO
, "Current brightness invalid"));
360 * For some buggy _BQC methods, we need to add a constant value to
361 * the _BQC return value to get the actual current brightness level
364 static int bqc_offset_aml_bug_workaround
;
365 static int video_set_bqc_offset(const struct dmi_system_id
*d
)
367 bqc_offset_aml_bug_workaround
= 9;
371 static int video_disable_backlight_sysfs_if(
372 const struct dmi_system_id
*d
)
374 if (disable_backlight_sysfs_if
== -1)
375 disable_backlight_sysfs_if
= 1;
379 static int video_set_device_id_scheme(const struct dmi_system_id
*d
)
381 device_id_scheme
= true;
385 static int video_enable_only_lcd(const struct dmi_system_id
*d
)
391 static int video_set_report_key_events(const struct dmi_system_id
*id
)
393 if (report_key_events
== -1)
394 report_key_events
= (uintptr_t)id
->driver_data
;
398 static struct dmi_system_id video_dmi_table
[] = {
400 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
403 .callback
= video_set_bqc_offset
,
404 .ident
= "Acer Aspire 5720",
406 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
407 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5720"),
411 .callback
= video_set_bqc_offset
,
412 .ident
= "Acer Aspire 5710Z",
414 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
415 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5710Z"),
419 .callback
= video_set_bqc_offset
,
420 .ident
= "eMachines E510",
422 DMI_MATCH(DMI_BOARD_VENDOR
, "EMACHINES"),
423 DMI_MATCH(DMI_PRODUCT_NAME
, "eMachines E510"),
427 .callback
= video_set_bqc_offset
,
428 .ident
= "Acer Aspire 5315",
430 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
431 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 5315"),
435 .callback
= video_set_bqc_offset
,
436 .ident
= "Acer Aspire 7720",
438 DMI_MATCH(DMI_BOARD_VENDOR
, "Acer"),
439 DMI_MATCH(DMI_PRODUCT_NAME
, "Aspire 7720"),
444 * Some machines have a broken acpi-video interface for brightness
445 * control, but still need an acpi_video_device_lcd_set_level() call
446 * on resume to turn the backlight power on. We Enable backlight
447 * control on these systems, but do not register a backlight sysfs
448 * as brightness control does not work.
451 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
452 .callback
= video_disable_backlight_sysfs_if
,
453 .ident
= "Toshiba Portege R700",
455 DMI_MATCH(DMI_SYS_VENDOR
, "TOSHIBA"),
456 DMI_MATCH(DMI_PRODUCT_NAME
, "PORTEGE R700"),
460 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
461 .callback
= video_disable_backlight_sysfs_if
,
462 .ident
= "Toshiba Portege R830",
464 DMI_MATCH(DMI_SYS_VENDOR
, "TOSHIBA"),
465 DMI_MATCH(DMI_PRODUCT_NAME
, "PORTEGE R830"),
469 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
470 .callback
= video_disable_backlight_sysfs_if
,
471 .ident
= "Toshiba Satellite R830",
473 DMI_MATCH(DMI_SYS_VENDOR
, "TOSHIBA"),
474 DMI_MATCH(DMI_PRODUCT_NAME
, "SATELLITE R830"),
478 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
479 * but the IDs actually follow the Device ID Scheme.
482 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
483 .callback
= video_set_device_id_scheme
,
484 .ident
= "ESPRIMO Mobile M9410",
486 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU SIEMENS"),
487 DMI_MATCH(DMI_PRODUCT_NAME
, "ESPRIMO Mobile M9410"),
491 * Some machines have multiple video output devices, but only the one
492 * that is the type of LCD can do the backlight control so we should not
493 * register backlight interface for other video output devices.
496 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
497 .callback
= video_enable_only_lcd
,
498 .ident
= "ESPRIMO Mobile M9410",
500 DMI_MATCH(DMI_SYS_VENDOR
, "FUJITSU SIEMENS"),
501 DMI_MATCH(DMI_PRODUCT_NAME
, "ESPRIMO Mobile M9410"),
505 * Some machines report wrong key events on the acpi-bus, suppress
506 * key event reporting on these. Note this is only intended to work
507 * around events which are plain wrong. In some cases we get double
508 * events, in this case acpi-video is considered the canonical source
509 * and the events from the other source should be filtered. E.g.
510 * by calling acpi_video_handles_brightness_key_presses() from the
511 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
514 .callback
= video_set_report_key_events
,
515 .driver_data
= (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS
),
516 .ident
= "Dell Vostro V131",
518 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Inc."),
519 DMI_MATCH(DMI_PRODUCT_NAME
, "Vostro V131"),
525 static unsigned long long
526 acpi_video_bqc_value_to_level(struct acpi_video_device
*device
,
527 unsigned long long bqc_value
)
529 unsigned long long level
;
531 if (device
->brightness
->flags
._BQC_use_index
) {
533 * _BQC returns an index that doesn't account for
534 * the first 2 items with special meaning, so we need
535 * to compensate for that by offsetting ourselves
537 if (device
->brightness
->flags
._BCL_reversed
)
538 bqc_value
= device
->brightness
->count
- 3 - bqc_value
;
540 level
= device
->brightness
->levels
[bqc_value
+ 2];
545 level
+= bqc_offset_aml_bug_workaround
;
551 acpi_video_device_lcd_get_level_current(struct acpi_video_device
*device
,
552 unsigned long long *level
, bool raw
)
554 acpi_status status
= AE_OK
;
557 if (device
->cap
._BQC
|| device
->cap
._BCQ
) {
558 char *buf
= device
->cap
._BQC
? "_BQC" : "_BCQ";
560 status
= acpi_evaluate_integer(device
->dev
->handle
, buf
,
562 if (ACPI_SUCCESS(status
)) {
565 * Caller has indicated he wants the raw
566 * value returned by _BQC, so don't furtherly
567 * mess with the value.
572 *level
= acpi_video_bqc_value_to_level(device
, *level
);
574 for (i
= 2; i
< device
->brightness
->count
; i
++)
575 if (device
->brightness
->levels
[i
] == *level
) {
576 device
->brightness
->curr
= *level
;
580 * BQC returned an invalid level.
583 ACPI_WARNING((AE_INFO
,
584 "%s returned an invalid level",
586 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
590 * should we return an error or ignore this failure?
591 * dev->brightness->curr is a cached value which stores
592 * the correct current backlight level in most cases.
593 * ACPI video backlight still works w/ buggy _BQC.
594 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
596 ACPI_WARNING((AE_INFO
, "Evaluating %s failed", buf
));
597 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
601 *level
= device
->brightness
->curr
;
606 acpi_video_device_EDID(struct acpi_video_device
*device
,
607 union acpi_object
**edid
, ssize_t length
)
610 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
611 union acpi_object
*obj
;
612 union acpi_object arg0
= { ACPI_TYPE_INTEGER
};
613 struct acpi_object_list args
= { 1, &arg0
};
621 arg0
.integer
.value
= 1;
622 else if (length
== 256)
623 arg0
.integer
.value
= 2;
627 status
= acpi_evaluate_object(device
->dev
->handle
, "_DDC", &args
, &buffer
);
628 if (ACPI_FAILURE(status
))
631 obj
= buffer
.pointer
;
633 if (obj
&& obj
->type
== ACPI_TYPE_BUFFER
)
636 printk(KERN_ERR PREFIX
"Invalid _DDC data\n");
648 * video : video bus device pointer
650 * 0. The system BIOS should NOT automatically switch(toggle)
651 * the active display output.
652 * 1. The system BIOS should automatically switch (toggle) the
653 * active display output. No switch event.
654 * 2. The _DGS value should be locked.
655 * 3. The system BIOS should not automatically switch (toggle) the
656 * active display output, but instead generate the display switch
659 * 0. The system BIOS should automatically control the brightness level
660 * of the LCD when the power changes from AC to DC
661 * 1. The system BIOS should NOT automatically control the brightness
662 * level of the LCD when the power changes from AC to DC.
668 acpi_video_bus_DOS(struct acpi_video_bus
*video
, int bios_flag
, int lcd_flag
)
672 if (!video
->cap
._DOS
)
675 if (bios_flag
< 0 || bios_flag
> 3 || lcd_flag
< 0 || lcd_flag
> 1)
677 video
->dos_setting
= (lcd_flag
<< 2) | bios_flag
;
678 status
= acpi_execute_simple_method(video
->device
->handle
, "_DOS",
679 (lcd_flag
<< 2) | bios_flag
);
680 if (ACPI_FAILURE(status
))
687 * Simple comparison function used to sort backlight levels.
691 acpi_video_cmp_level(const void *a
, const void *b
)
693 return *(int *)a
- *(int *)b
;
697 * Decides if _BQC/_BCQ for this system is usable
699 * We do this by changing the level first and then read out the current
700 * brightness level, if the value does not match, find out if it is using
701 * index. If not, clear the _BQC/_BCQ capability.
703 static int acpi_video_bqc_quirk(struct acpi_video_device
*device
,
704 int max_level
, int current_level
)
706 struct acpi_video_device_brightness
*br
= device
->brightness
;
708 unsigned long long level
;
711 /* don't mess with existing known broken systems */
712 if (bqc_offset_aml_bug_workaround
)
716 * Some systems always report current brightness level as maximum
717 * through _BQC, we need to test another value for them.
719 test_level
= current_level
== max_level
? br
->levels
[3] : max_level
;
721 result
= acpi_video_device_lcd_set_level(device
, test_level
);
725 result
= acpi_video_device_lcd_get_level_current(device
, &level
, true);
729 if (level
!= test_level
) {
730 /* buggy _BQC found, need to find out if it uses index */
731 if (level
< br
->count
) {
732 if (br
->flags
._BCL_reversed
)
733 level
= br
->count
- 3 - level
;
734 if (br
->levels
[level
+ 2] == test_level
)
735 br
->flags
._BQC_use_index
= 1;
738 if (!br
->flags
._BQC_use_index
)
739 device
->cap
._BQC
= device
->cap
._BCQ
= 0;
745 int acpi_video_get_levels(struct acpi_device
*device
,
746 struct acpi_video_device_brightness
**dev_br
,
749 union acpi_object
*obj
= NULL
;
750 int i
, max_level
= 0, count
= 0, level_ac_battery
= 0;
751 union acpi_object
*o
;
752 struct acpi_video_device_brightness
*br
= NULL
;
756 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device
->handle
,
758 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Could not query available "
759 "LCD brightness level\n"));
764 if (obj
->package
.count
< 2) {
769 br
= kzalloc(sizeof(*br
), GFP_KERNEL
);
771 printk(KERN_ERR
"can't allocate memory\n");
776 br
->levels
= kmalloc((obj
->package
.count
+ 2) * sizeof *(br
->levels
),
783 for (i
= 0; i
< obj
->package
.count
; i
++) {
784 o
= (union acpi_object
*)&obj
->package
.elements
[i
];
785 if (o
->type
!= ACPI_TYPE_INTEGER
) {
786 printk(KERN_ERR PREFIX
"Invalid data\n");
789 value
= (u32
) o
->integer
.value
;
790 /* Skip duplicate entries */
791 if (count
> 2 && br
->levels
[count
- 1] == value
)
794 br
->levels
[count
] = value
;
796 if (br
->levels
[count
] > max_level
)
797 max_level
= br
->levels
[count
];
802 * some buggy BIOS don't export the levels
803 * when machine is on AC/Battery in _BCL package.
804 * In this case, the first two elements in _BCL packages
805 * are also supported brightness levels that OS should take care of.
807 for (i
= 2; i
< count
; i
++) {
808 if (br
->levels
[i
] == br
->levels
[0])
810 if (br
->levels
[i
] == br
->levels
[1])
814 if (level_ac_battery
< 2) {
815 level_ac_battery
= 2 - level_ac_battery
;
816 br
->flags
._BCL_no_ac_battery_levels
= 1;
817 for (i
= (count
- 1 + level_ac_battery
); i
>= 2; i
--)
818 br
->levels
[i
] = br
->levels
[i
- level_ac_battery
];
819 count
+= level_ac_battery
;
820 } else if (level_ac_battery
> 2)
821 ACPI_ERROR((AE_INFO
, "Too many duplicates in _BCL package"));
823 /* Check if the _BCL package is in a reversed order */
824 if (max_level
== br
->levels
[2]) {
825 br
->flags
._BCL_reversed
= 1;
826 sort(&br
->levels
[2], count
- 2, sizeof(br
->levels
[2]),
827 acpi_video_cmp_level
, NULL
);
828 } else if (max_level
!= br
->levels
[count
- 1])
830 "Found unordered _BCL package"));
835 *pmax_level
= max_level
;
844 EXPORT_SYMBOL(acpi_video_get_levels
);
848 * device : video output device (LCD, CRT, ..)
851 * Maximum brightness level
853 * Allocate and initialize device->brightness.
857 acpi_video_init_brightness(struct acpi_video_device
*device
)
859 int i
, max_level
= 0;
860 unsigned long long level
, level_old
;
861 struct acpi_video_device_brightness
*br
= NULL
;
862 int result
= -EINVAL
;
864 result
= acpi_video_get_levels(device
->dev
, &br
, &max_level
);
867 device
->brightness
= br
;
869 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
870 br
->curr
= level
= max_level
;
872 if (!device
->cap
._BQC
)
875 result
= acpi_video_device_lcd_get_level_current(device
,
878 goto out_free_levels
;
880 result
= acpi_video_bqc_quirk(device
, max_level
, level_old
);
882 goto out_free_levels
;
884 * cap._BQC may get cleared due to _BQC is found to be broken
885 * in acpi_video_bqc_quirk, so check again here.
887 if (!device
->cap
._BQC
)
890 level
= acpi_video_bqc_value_to_level(device
, level_old
);
892 * On some buggy laptops, _BQC returns an uninitialized
893 * value when invoked for the first time, i.e.
894 * level_old is invalid (no matter whether it's a level
895 * or an index). Set the backlight to max_level in this case.
897 for (i
= 2; i
< br
->count
; i
++)
898 if (level
== br
->levels
[i
])
900 if (i
== br
->count
|| !level
)
904 result
= acpi_video_device_lcd_set_level(device
, level
);
906 goto out_free_levels
;
908 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
909 "found %d brightness levels\n", br
->count
- 2));
915 device
->brightness
= NULL
;
921 * device : video output device (LCD, CRT, ..)
926 * Find out all required AML methods defined under the output
930 static void acpi_video_device_find_cap(struct acpi_video_device
*device
)
932 if (acpi_has_method(device
->dev
->handle
, "_ADR"))
933 device
->cap
._ADR
= 1;
934 if (acpi_has_method(device
->dev
->handle
, "_BCL"))
935 device
->cap
._BCL
= 1;
936 if (acpi_has_method(device
->dev
->handle
, "_BCM"))
937 device
->cap
._BCM
= 1;
938 if (acpi_has_method(device
->dev
->handle
, "_BQC")) {
939 device
->cap
._BQC
= 1;
940 } else if (acpi_has_method(device
->dev
->handle
, "_BCQ")) {
941 printk(KERN_WARNING FW_BUG
"_BCQ is used instead of _BQC\n");
942 device
->cap
._BCQ
= 1;
945 if (acpi_has_method(device
->dev
->handle
, "_DDC"))
946 device
->cap
._DDC
= 1;
951 * device : video output device (VGA)
956 * Find out all required AML methods defined under the video bus device.
959 static void acpi_video_bus_find_cap(struct acpi_video_bus
*video
)
961 if (acpi_has_method(video
->device
->handle
, "_DOS"))
963 if (acpi_has_method(video
->device
->handle
, "_DOD"))
965 if (acpi_has_method(video
->device
->handle
, "_ROM"))
967 if (acpi_has_method(video
->device
->handle
, "_GPD"))
969 if (acpi_has_method(video
->device
->handle
, "_SPD"))
971 if (acpi_has_method(video
->device
->handle
, "_VPO"))
976 * Check whether the video bus device has required AML method to
977 * support the desired features
980 static int acpi_video_bus_check(struct acpi_video_bus
*video
)
982 acpi_status status
= -ENOENT
;
988 dev
= acpi_get_pci_dev(video
->device
->handle
);
994 * Since there is no HID, CID and so on for VGA driver, we have
995 * to check well known required nodes.
998 /* Does this device support video switching? */
999 if (video
->cap
._DOS
|| video
->cap
._DOD
) {
1000 if (!video
->cap
._DOS
) {
1001 printk(KERN_WARNING FW_BUG
1002 "ACPI(%s) defines _DOD but not _DOS\n",
1003 acpi_device_bid(video
->device
));
1005 video
->flags
.multihead
= 1;
1009 /* Does this device support retrieving a video ROM? */
1010 if (video
->cap
._ROM
) {
1011 video
->flags
.rom
= 1;
1015 /* Does this device support configuring which video device to POST? */
1016 if (video
->cap
._GPD
&& video
->cap
._SPD
&& video
->cap
._VPO
) {
1017 video
->flags
.post
= 1;
1025 * --------------------------------------------------------------------------
1027 * --------------------------------------------------------------------------
1030 /* device interface */
1031 static struct acpi_video_device_attrib
*
1032 acpi_video_get_device_attr(struct acpi_video_bus
*video
, unsigned long device_id
)
1034 struct acpi_video_enumerated_device
*ids
;
1037 for (i
= 0; i
< video
->attached_count
; i
++) {
1038 ids
= &video
->attached_array
[i
];
1039 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1040 return &ids
->value
.attrib
;
1047 acpi_video_get_device_type(struct acpi_video_bus
*video
,
1048 unsigned long device_id
)
1050 struct acpi_video_enumerated_device
*ids
;
1053 for (i
= 0; i
< video
->attached_count
; i
++) {
1054 ids
= &video
->attached_array
[i
];
1055 if ((ids
->value
.int_val
& 0xffff) == device_id
)
1056 return ids
->value
.int_val
;
1063 acpi_video_bus_get_one_device(struct acpi_device
*device
,
1064 struct acpi_video_bus
*video
)
1066 unsigned long long device_id
;
1067 int status
, device_type
;
1068 struct acpi_video_device
*data
;
1069 struct acpi_video_device_attrib
*attribute
;
1072 acpi_evaluate_integer(device
->handle
, "_ADR", NULL
, &device_id
);
1073 /* Some device omits _ADR, we skip them instead of fail */
1074 if (ACPI_FAILURE(status
))
1077 data
= kzalloc(sizeof(struct acpi_video_device
), GFP_KERNEL
);
1081 strcpy(acpi_device_name(device
), ACPI_VIDEO_DEVICE_NAME
);
1082 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
1083 device
->driver_data
= data
;
1085 data
->device_id
= device_id
;
1086 data
->video
= video
;
1088 INIT_DELAYED_WORK(&data
->switch_brightness_work
,
1089 acpi_video_switch_brightness
);
1091 attribute
= acpi_video_get_device_attr(video
, device_id
);
1093 if (attribute
&& (attribute
->device_id_scheme
|| device_id_scheme
)) {
1094 switch (attribute
->display_type
) {
1095 case ACPI_VIDEO_DISPLAY_CRT
:
1096 data
->flags
.crt
= 1;
1098 case ACPI_VIDEO_DISPLAY_TV
:
1099 data
->flags
.tvout
= 1;
1101 case ACPI_VIDEO_DISPLAY_DVI
:
1102 data
->flags
.dvi
= 1;
1104 case ACPI_VIDEO_DISPLAY_LCD
:
1105 data
->flags
.lcd
= 1;
1108 data
->flags
.unknown
= 1;
1111 if (attribute
->bios_can_detect
)
1112 data
->flags
.bios
= 1;
1114 /* Check for legacy IDs */
1115 device_type
= acpi_video_get_device_type(video
, device_id
);
1116 /* Ignore bits 16 and 18-20 */
1117 switch (device_type
& 0xffe2ffff) {
1118 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR
:
1119 data
->flags
.crt
= 1;
1121 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL
:
1122 data
->flags
.lcd
= 1;
1124 case ACPI_VIDEO_DISPLAY_LEGACY_TV
:
1125 data
->flags
.tvout
= 1;
1128 data
->flags
.unknown
= 1;
1132 acpi_video_device_bind(video
, data
);
1133 acpi_video_device_find_cap(data
);
1135 mutex_lock(&video
->device_list_lock
);
1136 list_add_tail(&data
->entry
, &video
->video_device_list
);
1137 mutex_unlock(&video
->device_list_lock
);
1144 * video : video bus device
1149 * Enumerate the video device list of the video bus,
1150 * bind the ids with the corresponding video devices
1151 * under the video bus.
1154 static void acpi_video_device_rebind(struct acpi_video_bus
*video
)
1156 struct acpi_video_device
*dev
;
1158 mutex_lock(&video
->device_list_lock
);
1160 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1161 acpi_video_device_bind(video
, dev
);
1163 mutex_unlock(&video
->device_list_lock
);
1168 * video : video bus device
1169 * device : video output device under the video
1175 * Bind the ids with the corresponding video devices
1176 * under the video bus.
1180 acpi_video_device_bind(struct acpi_video_bus
*video
,
1181 struct acpi_video_device
*device
)
1183 struct acpi_video_enumerated_device
*ids
;
1186 for (i
= 0; i
< video
->attached_count
; i
++) {
1187 ids
= &video
->attached_array
[i
];
1188 if (device
->device_id
== (ids
->value
.int_val
& 0xffff)) {
1189 ids
->bind_info
= device
;
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "device_bind %d\n", i
));
1195 static bool acpi_video_device_in_dod(struct acpi_video_device
*device
)
1197 struct acpi_video_bus
*video
= device
->video
;
1201 * If we have a broken _DOD or we have more than 8 output devices
1202 * under the graphics controller node that we can't proper deal with
1203 * in the operation region code currently, no need to test.
1205 if (!video
->attached_count
|| video
->child_count
> 8)
1208 for (i
= 0; i
< video
->attached_count
; i
++) {
1209 if ((video
->attached_array
[i
].value
.int_val
& 0xfff) ==
1210 (device
->device_id
& 0xfff))
1219 * video : video bus device
1224 * Call _DOD to enumerate all devices attached to display adapter
1228 static int acpi_video_device_enumerate(struct acpi_video_bus
*video
)
1233 struct acpi_video_enumerated_device
*active_list
;
1234 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1235 union acpi_object
*dod
= NULL
;
1236 union acpi_object
*obj
;
1238 if (!video
->cap
._DOD
)
1239 return AE_NOT_EXIST
;
1241 status
= acpi_evaluate_object(video
->device
->handle
, "_DOD", NULL
, &buffer
);
1242 if (!ACPI_SUCCESS(status
)) {
1243 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _DOD"));
1247 dod
= buffer
.pointer
;
1248 if (!dod
|| (dod
->type
!= ACPI_TYPE_PACKAGE
)) {
1249 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _DOD data"));
1254 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d video heads in _DOD\n",
1255 dod
->package
.count
));
1257 active_list
= kcalloc(1 + dod
->package
.count
,
1258 sizeof(struct acpi_video_enumerated_device
),
1266 for (i
= 0; i
< dod
->package
.count
; i
++) {
1267 obj
= &dod
->package
.elements
[i
];
1269 if (obj
->type
!= ACPI_TYPE_INTEGER
) {
1270 printk(KERN_ERR PREFIX
1271 "Invalid _DOD data in element %d\n", i
);
1275 active_list
[count
].value
.int_val
= obj
->integer
.value
;
1276 active_list
[count
].bind_info
= NULL
;
1277 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "dod element[%d] = %d\n", i
,
1278 (int)obj
->integer
.value
));
1282 kfree(video
->attached_array
);
1284 video
->attached_array
= active_list
;
1285 video
->attached_count
= count
;
1288 kfree(buffer
.pointer
);
1293 acpi_video_get_next_level(struct acpi_video_device
*device
,
1294 u32 level_current
, u32 event
)
1296 int min
, max
, min_above
, max_below
, i
, l
, delta
= 255;
1297 max
= max_below
= 0;
1298 min
= min_above
= 255;
1299 /* Find closest level to level_current */
1300 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1301 l
= device
->brightness
->levels
[i
];
1302 if (abs(l
- level_current
) < abs(delta
)) {
1303 delta
= l
- level_current
;
1308 /* Ajust level_current to closest available level */
1309 level_current
+= delta
;
1310 for (i
= 2; i
< device
->brightness
->count
; i
++) {
1311 l
= device
->brightness
->levels
[i
];
1316 if (l
< min_above
&& l
> level_current
)
1318 if (l
> max_below
&& l
< level_current
)
1323 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
:
1324 return (level_current
< max
) ? min_above
: min
;
1325 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
:
1326 return (level_current
< max
) ? min_above
: max
;
1327 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
:
1328 return (level_current
> min
) ? max_below
: min
;
1329 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
:
1330 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
:
1333 return level_current
;
1338 acpi_video_switch_brightness(struct work_struct
*work
)
1340 struct acpi_video_device
*device
= container_of(to_delayed_work(work
),
1341 struct acpi_video_device
, switch_brightness_work
);
1342 unsigned long long level_current
, level_next
;
1343 int event
= device
->switch_brightness_event
;
1344 int result
= -EINVAL
;
1346 /* no warning message if acpi_backlight=vendor or a quirk is used */
1347 if (!device
->backlight
)
1350 if (!device
->brightness
)
1353 result
= acpi_video_device_lcd_get_level_current(device
,
1359 level_next
= acpi_video_get_next_level(device
, level_current
, event
);
1361 result
= acpi_video_device_lcd_set_level(device
, level_next
);
1364 backlight_force_update(device
->backlight
,
1365 BACKLIGHT_UPDATE_HOTKEY
);
1369 printk(KERN_ERR PREFIX
"Failed to switch the brightness\n");
1372 int acpi_video_get_edid(struct acpi_device
*device
, int type
, int device_id
,
1375 struct acpi_video_bus
*video
;
1376 struct acpi_video_device
*video_device
;
1377 union acpi_object
*buffer
= NULL
;
1381 if (!device
|| !acpi_driver_data(device
))
1384 video
= acpi_driver_data(device
);
1386 for (i
= 0; i
< video
->attached_count
; i
++) {
1387 video_device
= video
->attached_array
[i
].bind_info
;
1393 if (!video_device
->cap
._DDC
)
1398 case ACPI_VIDEO_DISPLAY_CRT
:
1399 if (!video_device
->flags
.crt
)
1402 case ACPI_VIDEO_DISPLAY_TV
:
1403 if (!video_device
->flags
.tvout
)
1406 case ACPI_VIDEO_DISPLAY_DVI
:
1407 if (!video_device
->flags
.dvi
)
1410 case ACPI_VIDEO_DISPLAY_LCD
:
1411 if (!video_device
->flags
.lcd
)
1415 } else if (video_device
->device_id
!= device_id
) {
1419 status
= acpi_video_device_EDID(video_device
, &buffer
, length
);
1421 if (ACPI_FAILURE(status
) || !buffer
||
1422 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1424 status
= acpi_video_device_EDID(video_device
, &buffer
,
1426 if (ACPI_FAILURE(status
) || !buffer
||
1427 buffer
->type
!= ACPI_TYPE_BUFFER
) {
1432 *edid
= buffer
->buffer
.pointer
;
1438 EXPORT_SYMBOL(acpi_video_get_edid
);
1441 acpi_video_bus_get_devices(struct acpi_video_bus
*video
,
1442 struct acpi_device
*device
)
1445 struct acpi_device
*dev
;
1448 * There are systems where video module known to work fine regardless
1449 * of broken _DOD and ignoring returned value here doesn't cause
1452 acpi_video_device_enumerate(video
);
1454 list_for_each_entry(dev
, &device
->children
, node
) {
1456 status
= acpi_video_bus_get_one_device(dev
, video
);
1458 dev_err(&dev
->dev
, "Can't attach device\n");
1461 video
->child_count
++;
1466 /* acpi_video interface */
1469 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1470 * preform any automatic brightness change on receiving a notification.
1472 static int acpi_video_bus_start_devices(struct acpi_video_bus
*video
)
1474 return acpi_video_bus_DOS(video
, 0,
1475 acpi_osi_is_win8() ? 1 : 0);
1478 static int acpi_video_bus_stop_devices(struct acpi_video_bus
*video
)
1480 return acpi_video_bus_DOS(video
, 0,
1481 acpi_osi_is_win8() ? 0 : 1);
1484 static void acpi_video_bus_notify(struct acpi_device
*device
, u32 event
)
1486 struct acpi_video_bus
*video
= acpi_driver_data(device
);
1487 struct input_dev
*input
;
1490 if (!video
|| !video
->input
)
1493 input
= video
->input
;
1496 case ACPI_VIDEO_NOTIFY_SWITCH
: /* User requested a switch,
1497 * most likely via hotkey. */
1498 keycode
= KEY_SWITCHVIDEOMODE
;
1501 case ACPI_VIDEO_NOTIFY_PROBE
: /* User plugged in or removed a video
1503 acpi_video_device_enumerate(video
);
1504 acpi_video_device_rebind(video
);
1505 keycode
= KEY_SWITCHVIDEOMODE
;
1508 case ACPI_VIDEO_NOTIFY_CYCLE
: /* Cycle Display output hotkey pressed. */
1509 keycode
= KEY_SWITCHVIDEOMODE
;
1511 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT
: /* Next Display output hotkey pressed. */
1512 keycode
= KEY_VIDEO_NEXT
;
1514 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT
: /* previous Display output hotkey pressed. */
1515 keycode
= KEY_VIDEO_PREV
;
1519 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1520 "Unsupported event [0x%x]\n", event
));
1524 if (acpi_notifier_call_chain(device
, event
, 0))
1525 /* Something vetoed the keypress. */
1528 if (keycode
&& (report_key_events
& REPORT_OUTPUT_KEY_EVENTS
)) {
1529 input_report_key(input
, keycode
, 1);
1531 input_report_key(input
, keycode
, 0);
1538 static void brightness_switch_event(struct acpi_video_device
*video_device
,
1541 if (!brightness_switch_enabled
)
1544 video_device
->switch_brightness_event
= event
;
1545 schedule_delayed_work(&video_device
->switch_brightness_work
, HZ
/ 10);
1548 static void acpi_video_device_notify(acpi_handle handle
, u32 event
, void *data
)
1550 struct acpi_video_device
*video_device
= data
;
1551 struct acpi_device
*device
= NULL
;
1552 struct acpi_video_bus
*bus
;
1553 struct input_dev
*input
;
1559 device
= video_device
->dev
;
1560 bus
= video_device
->video
;
1564 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS
: /* Cycle brightness */
1565 brightness_switch_event(video_device
, event
);
1566 keycode
= KEY_BRIGHTNESS_CYCLE
;
1568 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS
: /* Increase brightness */
1569 brightness_switch_event(video_device
, event
);
1570 keycode
= KEY_BRIGHTNESSUP
;
1572 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS
: /* Decrease brightness */
1573 brightness_switch_event(video_device
, event
);
1574 keycode
= KEY_BRIGHTNESSDOWN
;
1576 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS
: /* zero brightness */
1577 brightness_switch_event(video_device
, event
);
1578 keycode
= KEY_BRIGHTNESS_ZERO
;
1580 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF
: /* display device off */
1581 brightness_switch_event(video_device
, event
);
1582 keycode
= KEY_DISPLAY_OFF
;
1585 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1586 "Unsupported event [0x%x]\n", event
));
1590 acpi_notifier_call_chain(device
, event
, 0);
1592 if (keycode
&& (report_key_events
& REPORT_BRIGHTNESS_KEY_EVENTS
)) {
1593 input_report_key(input
, keycode
, 1);
1595 input_report_key(input
, keycode
, 0);
1602 static int acpi_video_resume(struct notifier_block
*nb
,
1603 unsigned long val
, void *ign
)
1605 struct acpi_video_bus
*video
;
1606 struct acpi_video_device
*video_device
;
1610 case PM_HIBERNATION_PREPARE
:
1611 case PM_SUSPEND_PREPARE
:
1612 case PM_RESTORE_PREPARE
:
1616 video
= container_of(nb
, struct acpi_video_bus
, pm_nb
);
1618 dev_info(&video
->device
->dev
, "Restoring backlight state\n");
1620 for (i
= 0; i
< video
->attached_count
; i
++) {
1621 video_device
= video
->attached_array
[i
].bind_info
;
1622 if (video_device
&& video_device
->brightness
)
1623 acpi_video_device_lcd_set_level(video_device
,
1624 video_device
->brightness
->curr
);
1631 acpi_video_bus_match(acpi_handle handle
, u32 level
, void *context
,
1632 void **return_value
)
1634 struct acpi_device
*device
= context
;
1635 struct acpi_device
*sibling
;
1638 if (handle
== device
->handle
)
1639 return AE_CTRL_TERMINATE
;
1641 result
= acpi_bus_get_device(handle
, &sibling
);
1645 if (!strcmp(acpi_device_name(sibling
), ACPI_VIDEO_BUS_NAME
))
1646 return AE_ALREADY_EXISTS
;
1651 static void acpi_video_dev_register_backlight(struct acpi_video_device
*device
)
1653 struct backlight_properties props
;
1654 struct pci_dev
*pdev
;
1655 acpi_handle acpi_parent
;
1656 struct device
*parent
= NULL
;
1661 result
= acpi_video_init_brightness(device
);
1665 if (disable_backlight_sysfs_if
> 0)
1668 name
= kasprintf(GFP_KERNEL
, "acpi_video%d", count
);
1673 acpi_get_parent(device
->dev
->handle
, &acpi_parent
);
1675 pdev
= acpi_get_pci_dev(acpi_parent
);
1677 parent
= &pdev
->dev
;
1681 memset(&props
, 0, sizeof(struct backlight_properties
));
1682 props
.type
= BACKLIGHT_FIRMWARE
;
1683 props
.max_brightness
= device
->brightness
->count
- 3;
1684 device
->backlight
= backlight_device_register(name
,
1687 &acpi_backlight_ops
,
1690 if (IS_ERR(device
->backlight
)) {
1691 device
->backlight
= NULL
;
1696 * Save current brightness level in case we have to restore it
1697 * before acpi_video_device_lcd_set_level() is called next time.
1699 device
->backlight
->props
.brightness
=
1700 acpi_video_get_brightness(device
->backlight
);
1702 device
->cooling_dev
= thermal_cooling_device_register("LCD",
1703 device
->dev
, &video_cooling_ops
);
1704 if (IS_ERR(device
->cooling_dev
)) {
1706 * Set cooling_dev to NULL so we don't crash trying to free it.
1707 * Also, why the hell we are returning early and not attempt to
1708 * register video output if cooling device registration failed?
1711 device
->cooling_dev
= NULL
;
1715 dev_info(&device
->dev
->dev
, "registered as cooling_device%d\n",
1716 device
->cooling_dev
->id
);
1717 result
= sysfs_create_link(&device
->dev
->dev
.kobj
,
1718 &device
->cooling_dev
->device
.kobj
,
1721 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1722 result
= sysfs_create_link(&device
->cooling_dev
->device
.kobj
,
1723 &device
->dev
->dev
.kobj
, "device");
1725 printk(KERN_ERR PREFIX
"Create sysfs link\n");
1728 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus
*video
)
1730 struct acpi_video_device
*dev
;
1731 union acpi_object
*levels
;
1733 mutex_lock(&video
->device_list_lock
);
1734 list_for_each_entry(dev
, &video
->video_device_list
, entry
) {
1735 if (!acpi_video_device_lcd_query_levels(dev
->dev
->handle
, &levels
))
1738 mutex_unlock(&video
->device_list_lock
);
1741 static bool acpi_video_should_register_backlight(struct acpi_video_device
*dev
)
1744 * Do not create backlight device for video output
1745 * device that is not in the enumerated list.
1747 if (!acpi_video_device_in_dod(dev
)) {
1748 dev_dbg(&dev
->dev
->dev
, "not in _DOD list, ignore\n");
1753 return dev
->flags
.lcd
;
1757 static int acpi_video_bus_register_backlight(struct acpi_video_bus
*video
)
1759 struct acpi_video_device
*dev
;
1761 if (video
->backlight_registered
)
1764 acpi_video_run_bcl_for_osi(video
);
1766 if (acpi_video_get_backlight_type() != acpi_backlight_video
)
1769 mutex_lock(&video
->device_list_lock
);
1770 list_for_each_entry(dev
, &video
->video_device_list
, entry
) {
1771 if (acpi_video_should_register_backlight(dev
))
1772 acpi_video_dev_register_backlight(dev
);
1774 mutex_unlock(&video
->device_list_lock
);
1776 video
->backlight_registered
= true;
1778 video
->pm_nb
.notifier_call
= acpi_video_resume
;
1779 video
->pm_nb
.priority
= 0;
1780 return register_pm_notifier(&video
->pm_nb
);
1783 static void acpi_video_dev_unregister_backlight(struct acpi_video_device
*device
)
1785 if (device
->backlight
) {
1786 backlight_device_unregister(device
->backlight
);
1787 device
->backlight
= NULL
;
1789 if (device
->brightness
) {
1790 kfree(device
->brightness
->levels
);
1791 kfree(device
->brightness
);
1792 device
->brightness
= NULL
;
1794 if (device
->cooling_dev
) {
1795 sysfs_remove_link(&device
->dev
->dev
.kobj
, "thermal_cooling");
1796 sysfs_remove_link(&device
->cooling_dev
->device
.kobj
, "device");
1797 thermal_cooling_device_unregister(device
->cooling_dev
);
1798 device
->cooling_dev
= NULL
;
1802 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus
*video
)
1804 struct acpi_video_device
*dev
;
1807 if (!video
->backlight_registered
)
1810 error
= unregister_pm_notifier(&video
->pm_nb
);
1812 mutex_lock(&video
->device_list_lock
);
1813 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1814 acpi_video_dev_unregister_backlight(dev
);
1815 mutex_unlock(&video
->device_list_lock
);
1817 video
->backlight_registered
= false;
1822 static void acpi_video_dev_add_notify_handler(struct acpi_video_device
*device
)
1825 struct acpi_device
*adev
= device
->dev
;
1827 status
= acpi_install_notify_handler(adev
->handle
, ACPI_DEVICE_NOTIFY
,
1828 acpi_video_device_notify
, device
);
1829 if (ACPI_FAILURE(status
))
1830 dev_err(&adev
->dev
, "Error installing notify handler\n");
1832 device
->flags
.notify
= 1;
1835 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus
*video
)
1837 struct input_dev
*input
;
1838 struct acpi_video_device
*dev
;
1841 video
->input
= input
= input_allocate_device();
1847 error
= acpi_video_bus_start_devices(video
);
1849 goto err_free_input
;
1851 snprintf(video
->phys
, sizeof(video
->phys
),
1852 "%s/video/input0", acpi_device_hid(video
->device
));
1854 input
->name
= acpi_device_name(video
->device
);
1855 input
->phys
= video
->phys
;
1856 input
->id
.bustype
= BUS_HOST
;
1857 input
->id
.product
= 0x06;
1858 input
->dev
.parent
= &video
->device
->dev
;
1859 input
->evbit
[0] = BIT(EV_KEY
);
1860 set_bit(KEY_SWITCHVIDEOMODE
, input
->keybit
);
1861 set_bit(KEY_VIDEO_NEXT
, input
->keybit
);
1862 set_bit(KEY_VIDEO_PREV
, input
->keybit
);
1863 set_bit(KEY_BRIGHTNESS_CYCLE
, input
->keybit
);
1864 set_bit(KEY_BRIGHTNESSUP
, input
->keybit
);
1865 set_bit(KEY_BRIGHTNESSDOWN
, input
->keybit
);
1866 set_bit(KEY_BRIGHTNESS_ZERO
, input
->keybit
);
1867 set_bit(KEY_DISPLAY_OFF
, input
->keybit
);
1869 error
= input_register_device(input
);
1873 mutex_lock(&video
->device_list_lock
);
1874 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1875 acpi_video_dev_add_notify_handler(dev
);
1876 mutex_unlock(&video
->device_list_lock
);
1881 acpi_video_bus_stop_devices(video
);
1883 input_free_device(input
);
1884 video
->input
= NULL
;
1889 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device
*dev
)
1891 if (dev
->flags
.notify
) {
1892 acpi_remove_notify_handler(dev
->dev
->handle
, ACPI_DEVICE_NOTIFY
,
1893 acpi_video_device_notify
);
1894 dev
->flags
.notify
= 0;
1898 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus
*video
)
1900 struct acpi_video_device
*dev
;
1902 mutex_lock(&video
->device_list_lock
);
1903 list_for_each_entry(dev
, &video
->video_device_list
, entry
)
1904 acpi_video_dev_remove_notify_handler(dev
);
1905 mutex_unlock(&video
->device_list_lock
);
1907 acpi_video_bus_stop_devices(video
);
1908 input_unregister_device(video
->input
);
1909 video
->input
= NULL
;
1912 static int acpi_video_bus_put_devices(struct acpi_video_bus
*video
)
1914 struct acpi_video_device
*dev
, *next
;
1916 mutex_lock(&video
->device_list_lock
);
1917 list_for_each_entry_safe(dev
, next
, &video
->video_device_list
, entry
) {
1918 list_del(&dev
->entry
);
1921 mutex_unlock(&video
->device_list_lock
);
1926 static int instance
;
1928 static int acpi_video_bus_add(struct acpi_device
*device
)
1930 struct acpi_video_bus
*video
;
1934 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
,
1935 device
->parent
->handle
, 1,
1936 acpi_video_bus_match
, NULL
,
1938 if (status
== AE_ALREADY_EXISTS
) {
1939 printk(KERN_WARNING FW_BUG
1940 "Duplicate ACPI video bus devices for the"
1941 " same VGA controller, please try module "
1942 "parameter \"video.allow_duplicates=1\""
1943 "if the current driver doesn't work.\n");
1944 if (!allow_duplicates
)
1948 video
= kzalloc(sizeof(struct acpi_video_bus
), GFP_KERNEL
);
1952 /* a hack to fix the duplicate name "VID" problem on T61 */
1953 if (!strcmp(device
->pnp
.bus_id
, "VID")) {
1955 device
->pnp
.bus_id
[3] = '0' + instance
;
1958 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1959 if (!strcmp(device
->pnp
.bus_id
, "VGA")) {
1961 device
->pnp
.bus_id
[3] = '0' + instance
;
1965 video
->device
= device
;
1966 strcpy(acpi_device_name(device
), ACPI_VIDEO_BUS_NAME
);
1967 strcpy(acpi_device_class(device
), ACPI_VIDEO_CLASS
);
1968 device
->driver_data
= video
;
1970 acpi_video_bus_find_cap(video
);
1971 error
= acpi_video_bus_check(video
);
1973 goto err_free_video
;
1975 mutex_init(&video
->device_list_lock
);
1976 INIT_LIST_HEAD(&video
->video_device_list
);
1978 error
= acpi_video_bus_get_devices(video
, device
);
1982 printk(KERN_INFO PREFIX
"%s [%s] (multi-head: %s rom: %s post: %s)\n",
1983 ACPI_VIDEO_DEVICE_NAME
, acpi_device_bid(device
),
1984 video
->flags
.multihead
? "yes" : "no",
1985 video
->flags
.rom
? "yes" : "no",
1986 video
->flags
.post
? "yes" : "no");
1987 mutex_lock(&video_list_lock
);
1988 list_add_tail(&video
->entry
, &video_bus_head
);
1989 mutex_unlock(&video_list_lock
);
1991 acpi_video_bus_register_backlight(video
);
1992 acpi_video_bus_add_notify_handler(video
);
1997 acpi_video_bus_put_devices(video
);
1998 kfree(video
->attached_array
);
2001 device
->driver_data
= NULL
;
2006 static int acpi_video_bus_remove(struct acpi_device
*device
)
2008 struct acpi_video_bus
*video
= NULL
;
2011 if (!device
|| !acpi_driver_data(device
))
2014 video
= acpi_driver_data(device
);
2016 acpi_video_bus_remove_notify_handler(video
);
2017 acpi_video_bus_unregister_backlight(video
);
2018 acpi_video_bus_put_devices(video
);
2020 mutex_lock(&video_list_lock
);
2021 list_del(&video
->entry
);
2022 mutex_unlock(&video_list_lock
);
2024 kfree(video
->attached_array
);
2030 static int __init
is_i740(struct pci_dev
*dev
)
2032 if (dev
->device
== 0x00D1)
2034 if (dev
->device
== 0x7000)
2039 static int __init
intel_opregion_present(void)
2042 struct pci_dev
*dev
= NULL
;
2045 for_each_pci_dev(dev
) {
2046 if ((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
2048 if (dev
->vendor
!= PCI_VENDOR_ID_INTEL
)
2050 /* We don't want to poke around undefined i740 registers */
2053 pci_read_config_dword(dev
, 0xfc, &address
);
2061 int acpi_video_register(void)
2065 mutex_lock(®ister_count_mutex
);
2066 if (register_count
) {
2068 * if the function of acpi_video_register is already called,
2069 * don't register the acpi_vide_bus again and return no error.
2074 dmi_check_system(video_dmi_table
);
2076 ret
= acpi_bus_register_driver(&acpi_video_bus
);
2081 * When the acpi_video_bus is loaded successfully, increase
2082 * the counter reference.
2087 mutex_unlock(®ister_count_mutex
);
2090 EXPORT_SYMBOL(acpi_video_register
);
2092 void acpi_video_unregister(void)
2094 mutex_lock(®ister_count_mutex
);
2095 if (register_count
) {
2096 acpi_bus_unregister_driver(&acpi_video_bus
);
2099 mutex_unlock(®ister_count_mutex
);
2101 EXPORT_SYMBOL(acpi_video_unregister
);
2103 void acpi_video_unregister_backlight(void)
2105 struct acpi_video_bus
*video
;
2107 mutex_lock(®ister_count_mutex
);
2108 if (register_count
) {
2109 mutex_lock(&video_list_lock
);
2110 list_for_each_entry(video
, &video_bus_head
, entry
)
2111 acpi_video_bus_unregister_backlight(video
);
2112 mutex_unlock(&video_list_lock
);
2114 mutex_unlock(®ister_count_mutex
);
2117 bool acpi_video_handles_brightness_key_presses(void)
2119 bool have_video_busses
;
2121 mutex_lock(&video_list_lock
);
2122 have_video_busses
= !list_empty(&video_bus_head
);
2123 mutex_unlock(&video_list_lock
);
2125 return have_video_busses
&&
2126 (report_key_events
& REPORT_BRIGHTNESS_KEY_EVENTS
);
2128 EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses
);
2131 * This is kind of nasty. Hardware using Intel chipsets may require
2132 * the video opregion code to be run first in order to initialise
2133 * state before any ACPI video calls are made. To handle this we defer
2134 * registration of the video class until the opregion code has run.
2137 static int __init
acpi_video_init(void)
2140 * Let the module load even if ACPI is disabled (e.g. due to
2141 * a broken BIOS) so that i915.ko can still be loaded on such
2142 * old systems without an AcpiOpRegion.
2144 * acpi_video_register() will report -ENODEV later as well due
2145 * to acpi_disabled when i915.ko tries to register itself afterwards.
2150 if (intel_opregion_present())
2153 return acpi_video_register();
2156 static void __exit
acpi_video_exit(void)
2158 acpi_video_detect_exit();
2159 acpi_video_unregister();
2164 module_init(acpi_video_init
);
2165 module_exit(acpi_video_exit
);