xref: /linux/drivers/acpi/acpi_video.c (revision 5ad26161a371e4aa2d2553286f0cac580987a493)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
214ca7a47SHans de Goede /*
314ca7a47SHans de Goede  *  video.c - ACPI Video Driver
414ca7a47SHans de Goede  *
514ca7a47SHans de Goede  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
614ca7a47SHans de Goede  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
714ca7a47SHans de Goede  *  Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
814ca7a47SHans de Goede  */
914ca7a47SHans de Goede 
102924d2f8SRafael J. Wysocki #define pr_fmt(fmt) "ACPI: video: " fmt
112924d2f8SRafael J. Wysocki 
1214ca7a47SHans de Goede #include <linux/kernel.h>
1314ca7a47SHans de Goede #include <linux/module.h>
1414ca7a47SHans de Goede #include <linux/init.h>
1514ca7a47SHans de Goede #include <linux/types.h>
1614ca7a47SHans de Goede #include <linux/list.h>
1714ca7a47SHans de Goede #include <linux/mutex.h>
1814ca7a47SHans de Goede #include <linux/input.h>
1914ca7a47SHans de Goede #include <linux/backlight.h>
2014ca7a47SHans de Goede #include <linux/thermal.h>
2114ca7a47SHans de Goede #include <linux/sort.h>
2214ca7a47SHans de Goede #include <linux/pci.h>
2314ca7a47SHans de Goede #include <linux/pci_ids.h>
2414ca7a47SHans de Goede #include <linux/slab.h>
2514ca7a47SHans de Goede #include <linux/dmi.h>
2614ca7a47SHans de Goede #include <linux/suspend.h>
2714ca7a47SHans de Goede #include <linux/acpi.h>
2814ca7a47SHans de Goede #include <acpi/video.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
3014ca7a47SHans de Goede 
3114ca7a47SHans de Goede #define ACPI_VIDEO_BUS_NAME		"Video Bus"
3214ca7a47SHans de Goede #define ACPI_VIDEO_DEVICE_NAME		"Video Device"
3314ca7a47SHans de Goede 
3414ca7a47SHans de Goede #define MAX_NAME_LEN	20
3514ca7a47SHans de Goede 
3614ca7a47SHans de Goede MODULE_AUTHOR("Bruno Ducrot");
3714ca7a47SHans de Goede MODULE_DESCRIPTION("ACPI Video Driver");
3814ca7a47SHans de Goede MODULE_LICENSE("GPL");
3914ca7a47SHans de Goede 
4097e45dd6SGustavo A. R. Silva static bool brightness_switch_enabled = true;
4114ca7a47SHans de Goede module_param(brightness_switch_enabled, bool, 0644);
4214ca7a47SHans de Goede 
4314ca7a47SHans de Goede /*
4414ca7a47SHans de Goede  * By default, we don't allow duplicate ACPI video bus devices
4514ca7a47SHans de Goede  * under the same VGA controller
4614ca7a47SHans de Goede  */
4714ca7a47SHans de Goede static bool allow_duplicates;
4814ca7a47SHans de Goede module_param(allow_duplicates, bool, 0644);
4914ca7a47SHans de Goede 
5014ca7a47SHans de Goede static int disable_backlight_sysfs_if = -1;
5114ca7a47SHans de Goede module_param(disable_backlight_sysfs_if, int, 0444);
5214ca7a47SHans de Goede 
5305bc59a0SHans de Goede #define REPORT_OUTPUT_KEY_EVENTS		0x01
5405bc59a0SHans de Goede #define REPORT_BRIGHTNESS_KEY_EVENTS		0x02
5505bc59a0SHans de Goede static int report_key_events = -1;
5605bc59a0SHans de Goede module_param(report_key_events, int, 0644);
5705bc59a0SHans de Goede MODULE_PARM_DESC(report_key_events,
5805bc59a0SHans de Goede 	"0: none, 1: output changes, 2: brightness changes, 3: all");
5905bc59a0SHans de Goede 
604f7f9645SHans de Goede static int hw_changes_brightness = -1;
614f7f9645SHans de Goede module_param(hw_changes_brightness, int, 0644);
624f7f9645SHans de Goede MODULE_PARM_DESC(hw_changes_brightness,
634f7f9645SHans de Goede 	"Set this to 1 on buggy hw which changes the brightness itself when "
644f7f9645SHans de Goede 	"a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness");
654f7f9645SHans de Goede 
66592c8095SDmitry Frank /*
67592c8095SDmitry Frank  * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be
68592c8095SDmitry Frank  * assumed even if not actually set.
69592c8095SDmitry Frank  */
70e50b9be1SAaron Lu static bool device_id_scheme = false;
71e50b9be1SAaron Lu module_param(device_id_scheme, bool, 0444);
72e50b9be1SAaron Lu 
735928c281SHans de Goede static int only_lcd = -1;
745928c281SHans de Goede module_param(only_lcd, int, 0444);
75e50b9be1SAaron Lu 
76*5ad26161SHans de Goede static bool may_report_brightness_keys;
77970530cdSHans de Goede static int register_count;
78970530cdSHans de Goede static DEFINE_MUTEX(register_count_mutex);
7914e93553SHans de Goede static DEFINE_MUTEX(video_list_lock);
8014e93553SHans de Goede static LIST_HEAD(video_bus_head);
8114ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device);
8214ca7a47SHans de Goede static int acpi_video_bus_remove(struct acpi_device *device);
8314ca7a47SHans de Goede static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
8493a291dfSHans de Goede void acpi_video_detect_exit(void);
8514ca7a47SHans de Goede 
86d485ef43SDmitry Frank /*
87d485ef43SDmitry Frank  * Indices in the _BCL method response: the first two items are special,
88d485ef43SDmitry Frank  * the rest are all supported levels.
89d485ef43SDmitry Frank  *
90d485ef43SDmitry Frank  * See page 575 of the ACPI spec 3.0
91d485ef43SDmitry Frank  */
92d485ef43SDmitry Frank enum acpi_video_level_idx {
93d485ef43SDmitry Frank 	ACPI_VIDEO_AC_LEVEL,		/* level when machine has full power */
94d485ef43SDmitry Frank 	ACPI_VIDEO_BATTERY_LEVEL,	/* level when machine is on batteries */
95d485ef43SDmitry Frank 	ACPI_VIDEO_FIRST_LEVEL,		/* actual supported levels begin here */
96d485ef43SDmitry Frank };
97d485ef43SDmitry Frank 
9814ca7a47SHans de Goede static const struct acpi_device_id video_device_ids[] = {
9914ca7a47SHans de Goede 	{ACPI_VIDEO_HID, 0},
10014ca7a47SHans de Goede 	{"", 0},
10114ca7a47SHans de Goede };
10214ca7a47SHans de Goede MODULE_DEVICE_TABLE(acpi, video_device_ids);
10314ca7a47SHans de Goede 
10414ca7a47SHans de Goede static struct acpi_driver acpi_video_bus = {
10514ca7a47SHans de Goede 	.name = "video",
10614ca7a47SHans de Goede 	.class = ACPI_VIDEO_CLASS,
10714ca7a47SHans de Goede 	.ids = video_device_ids,
10814ca7a47SHans de Goede 	.ops = {
10914ca7a47SHans de Goede 		.add = acpi_video_bus_add,
11014ca7a47SHans de Goede 		.remove = acpi_video_bus_remove,
11114ca7a47SHans de Goede 		.notify = acpi_video_bus_notify,
11214ca7a47SHans de Goede 		},
11314ca7a47SHans de Goede };
11414ca7a47SHans de Goede 
11514ca7a47SHans de Goede struct acpi_video_bus_flags {
11614ca7a47SHans de Goede 	u8 multihead:1;		/* can switch video heads */
11714ca7a47SHans de Goede 	u8 rom:1;		/* can retrieve a video rom */
11814ca7a47SHans de Goede 	u8 post:1;		/* can configure the head to */
11914ca7a47SHans de Goede 	u8 reserved:5;
12014ca7a47SHans de Goede };
12114ca7a47SHans de Goede 
12214ca7a47SHans de Goede struct acpi_video_bus_cap {
12314ca7a47SHans de Goede 	u8 _DOS:1;		/* Enable/Disable output switching */
12414ca7a47SHans de Goede 	u8 _DOD:1;		/* Enumerate all devices attached to display adapter */
12514ca7a47SHans de Goede 	u8 _ROM:1;		/* Get ROM Data */
12614ca7a47SHans de Goede 	u8 _GPD:1;		/* Get POST Device */
12714ca7a47SHans de Goede 	u8 _SPD:1;		/* Set POST Device */
12814ca7a47SHans de Goede 	u8 _VPO:1;		/* Video POST Options */
12914ca7a47SHans de Goede 	u8 reserved:2;
13014ca7a47SHans de Goede };
13114ca7a47SHans de Goede 
13214ca7a47SHans de Goede struct acpi_video_device_attrib {
13314ca7a47SHans de Goede 	u32 display_index:4;	/* A zero-based instance of the Display */
13414ca7a47SHans de Goede 	u32 display_port_attachment:4;	/* This field differentiates the display type */
13514ca7a47SHans de Goede 	u32 display_type:4;	/* Describe the specific type in use */
13614ca7a47SHans de Goede 	u32 vendor_specific:4;	/* Chipset Vendor Specific */
13714ca7a47SHans de Goede 	u32 bios_can_detect:1;	/* BIOS can detect the device */
13814ca7a47SHans de Goede 	u32 depend_on_vga:1;	/* Non-VGA output device whose power is related to
13914ca7a47SHans de Goede 				   the VGA device. */
14014ca7a47SHans de Goede 	u32 pipe_id:3;		/* For VGA multiple-head devices. */
14114ca7a47SHans de Goede 	u32 reserved:10;	/* Must be 0 */
142592c8095SDmitry Frank 
143592c8095SDmitry Frank 	/*
144592c8095SDmitry Frank 	 * The device ID might not actually follow the scheme described by this
145592c8095SDmitry Frank 	 * struct acpi_video_device_attrib. If it does, then this bit
146592c8095SDmitry Frank 	 * device_id_scheme is set; otherwise, other fields should be ignored.
147592c8095SDmitry Frank 	 *
148592c8095SDmitry Frank 	 * (but also see the global flag device_id_scheme)
149592c8095SDmitry Frank 	 */
150592c8095SDmitry Frank 	u32 device_id_scheme:1;
15114ca7a47SHans de Goede };
15214ca7a47SHans de Goede 
15314ca7a47SHans de Goede struct acpi_video_enumerated_device {
15414ca7a47SHans de Goede 	union {
15514ca7a47SHans de Goede 		u32 int_val;
15614ca7a47SHans de Goede 		struct acpi_video_device_attrib attrib;
15714ca7a47SHans de Goede 	} value;
15814ca7a47SHans de Goede 	struct acpi_video_device *bind_info;
15914ca7a47SHans de Goede };
16014ca7a47SHans de Goede 
16114ca7a47SHans de Goede struct acpi_video_bus {
16214ca7a47SHans de Goede 	struct acpi_device *device;
16314ca7a47SHans de Goede 	bool backlight_registered;
16414ca7a47SHans de Goede 	u8 dos_setting;
16514ca7a47SHans de Goede 	struct acpi_video_enumerated_device *attached_array;
16614ca7a47SHans de Goede 	u8 attached_count;
16714ca7a47SHans de Goede 	u8 child_count;
16814ca7a47SHans de Goede 	struct acpi_video_bus_cap cap;
16914ca7a47SHans de Goede 	struct acpi_video_bus_flags flags;
17014ca7a47SHans de Goede 	struct list_head video_device_list;
17114ca7a47SHans de Goede 	struct mutex device_list_lock;	/* protects video_device_list */
17214ca7a47SHans de Goede 	struct list_head entry;
17314ca7a47SHans de Goede 	struct input_dev *input;
17414ca7a47SHans de Goede 	char phys[32];	/* for input device */
17514ca7a47SHans de Goede 	struct notifier_block pm_nb;
17614ca7a47SHans de Goede };
17714ca7a47SHans de Goede 
17814ca7a47SHans de Goede struct acpi_video_device_flags {
17914ca7a47SHans de Goede 	u8 crt:1;
18014ca7a47SHans de Goede 	u8 lcd:1;
18114ca7a47SHans de Goede 	u8 tvout:1;
18214ca7a47SHans de Goede 	u8 dvi:1;
18314ca7a47SHans de Goede 	u8 bios:1;
18414ca7a47SHans de Goede 	u8 unknown:1;
18514ca7a47SHans de Goede 	u8 notify:1;
18614ca7a47SHans de Goede 	u8 reserved:1;
18714ca7a47SHans de Goede };
18814ca7a47SHans de Goede 
18914ca7a47SHans de Goede struct acpi_video_device_cap {
19014ca7a47SHans de Goede 	u8 _ADR:1;		/* Return the unique ID */
19114ca7a47SHans de Goede 	u8 _BCL:1;		/* Query list of brightness control levels supported */
19214ca7a47SHans de Goede 	u8 _BCM:1;		/* Set the brightness level */
19314ca7a47SHans de Goede 	u8 _BQC:1;		/* Get current brightness level */
19414ca7a47SHans de Goede 	u8 _BCQ:1;		/* Some buggy BIOS uses _BCQ instead of _BQC */
19514ca7a47SHans de Goede 	u8 _DDC:1;		/* Return the EDID for this device */
19614ca7a47SHans de Goede };
19714ca7a47SHans de Goede 
19814ca7a47SHans de Goede struct acpi_video_device {
19914ca7a47SHans de Goede 	unsigned long device_id;
20014ca7a47SHans de Goede 	struct acpi_video_device_flags flags;
20114ca7a47SHans de Goede 	struct acpi_video_device_cap cap;
20214ca7a47SHans de Goede 	struct list_head entry;
20314ca7a47SHans de Goede 	struct delayed_work switch_brightness_work;
20414ca7a47SHans de Goede 	int switch_brightness_event;
20514ca7a47SHans de Goede 	struct acpi_video_bus *video;
20614ca7a47SHans de Goede 	struct acpi_device *dev;
20714ca7a47SHans de Goede 	struct acpi_video_device_brightness *brightness;
20814ca7a47SHans de Goede 	struct backlight_device *backlight;
20914ca7a47SHans de Goede 	struct thermal_cooling_device *cooling_dev;
21014ca7a47SHans de Goede };
21114ca7a47SHans de Goede 
21214ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
21314ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video);
21414ca7a47SHans de Goede static void acpi_video_device_bind(struct acpi_video_bus *video,
21514ca7a47SHans de Goede 				   struct acpi_video_device *device);
21614ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video);
21714ca7a47SHans de Goede static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
21814ca7a47SHans de Goede 			int level);
21914ca7a47SHans de Goede static int acpi_video_device_lcd_get_level_current(
22014ca7a47SHans de Goede 			struct acpi_video_device *device,
22114ca7a47SHans de Goede 			unsigned long long *level, bool raw);
22214ca7a47SHans de Goede static int acpi_video_get_next_level(struct acpi_video_device *device,
22314ca7a47SHans de Goede 				     u32 level_current, u32 event);
22414ca7a47SHans de Goede static void acpi_video_switch_brightness(struct work_struct *work);
22514ca7a47SHans de Goede 
22614ca7a47SHans de Goede /* backlight device sysfs support */
22714ca7a47SHans de Goede static int acpi_video_get_brightness(struct backlight_device *bd)
22814ca7a47SHans de Goede {
22914ca7a47SHans de Goede 	unsigned long long cur_level;
23014ca7a47SHans de Goede 	int i;
23114ca7a47SHans de Goede 	struct acpi_video_device *vd = bl_get_data(bd);
23214ca7a47SHans de Goede 
23314ca7a47SHans de Goede 	if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
23414ca7a47SHans de Goede 		return -EINVAL;
235d485ef43SDmitry Frank 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < vd->brightness->count; i++) {
23614ca7a47SHans de Goede 		if (vd->brightness->levels[i] == cur_level)
237d485ef43SDmitry Frank 			return i - ACPI_VIDEO_FIRST_LEVEL;
23814ca7a47SHans de Goede 	}
23914ca7a47SHans de Goede 	return 0;
24014ca7a47SHans de Goede }
24114ca7a47SHans de Goede 
24214ca7a47SHans de Goede static int acpi_video_set_brightness(struct backlight_device *bd)
24314ca7a47SHans de Goede {
244d485ef43SDmitry Frank 	int request_level = bd->props.brightness + ACPI_VIDEO_FIRST_LEVEL;
24514ca7a47SHans de Goede 	struct acpi_video_device *vd = bl_get_data(bd);
24614ca7a47SHans de Goede 
24714ca7a47SHans de Goede 	cancel_delayed_work(&vd->switch_brightness_work);
24814ca7a47SHans de Goede 	return acpi_video_device_lcd_set_level(vd,
24914ca7a47SHans de Goede 				vd->brightness->levels[request_level]);
25014ca7a47SHans de Goede }
25114ca7a47SHans de Goede 
25214ca7a47SHans de Goede static const struct backlight_ops acpi_backlight_ops = {
25314ca7a47SHans de Goede 	.get_brightness = acpi_video_get_brightness,
25414ca7a47SHans de Goede 	.update_status  = acpi_video_set_brightness,
25514ca7a47SHans de Goede };
25614ca7a47SHans de Goede 
25714ca7a47SHans de Goede /* thermal cooling device callbacks */
258d485ef43SDmitry Frank static int video_get_max_state(struct thermal_cooling_device *cooling_dev,
259d485ef43SDmitry Frank 			       unsigned long *state)
26014ca7a47SHans de Goede {
26114ca7a47SHans de Goede 	struct acpi_device *device = cooling_dev->devdata;
26214ca7a47SHans de Goede 	struct acpi_video_device *video = acpi_driver_data(device);
26314ca7a47SHans de Goede 
264d485ef43SDmitry Frank 	*state = video->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
26514ca7a47SHans de Goede 	return 0;
26614ca7a47SHans de Goede }
26714ca7a47SHans de Goede 
268d485ef43SDmitry Frank static int video_get_cur_state(struct thermal_cooling_device *cooling_dev,
269d485ef43SDmitry Frank 			       unsigned long *state)
27014ca7a47SHans de Goede {
27114ca7a47SHans de Goede 	struct acpi_device *device = cooling_dev->devdata;
27214ca7a47SHans de Goede 	struct acpi_video_device *video = acpi_driver_data(device);
27314ca7a47SHans de Goede 	unsigned long long level;
27414ca7a47SHans de Goede 	int offset;
27514ca7a47SHans de Goede 
27614ca7a47SHans de Goede 	if (acpi_video_device_lcd_get_level_current(video, &level, false))
27714ca7a47SHans de Goede 		return -EINVAL;
278d485ef43SDmitry Frank 	for (offset = ACPI_VIDEO_FIRST_LEVEL; offset < video->brightness->count;
279d485ef43SDmitry Frank 	     offset++)
28014ca7a47SHans de Goede 		if (level == video->brightness->levels[offset]) {
28114ca7a47SHans de Goede 			*state = video->brightness->count - offset - 1;
28214ca7a47SHans de Goede 			return 0;
28314ca7a47SHans de Goede 		}
28414ca7a47SHans de Goede 
28514ca7a47SHans de Goede 	return -EINVAL;
28614ca7a47SHans de Goede }
28714ca7a47SHans de Goede 
28814ca7a47SHans de Goede static int
28914ca7a47SHans de Goede video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
29014ca7a47SHans de Goede {
29114ca7a47SHans de Goede 	struct acpi_device *device = cooling_dev->devdata;
29214ca7a47SHans de Goede 	struct acpi_video_device *video = acpi_driver_data(device);
29314ca7a47SHans de Goede 	int level;
29414ca7a47SHans de Goede 
295d485ef43SDmitry Frank 	if (state >= video->brightness->count - ACPI_VIDEO_FIRST_LEVEL)
29614ca7a47SHans de Goede 		return -EINVAL;
29714ca7a47SHans de Goede 
29814ca7a47SHans de Goede 	state = video->brightness->count - state;
29914ca7a47SHans de Goede 	level = video->brightness->levels[state - 1];
30014ca7a47SHans de Goede 	return acpi_video_device_lcd_set_level(video, level);
30114ca7a47SHans de Goede }
30214ca7a47SHans de Goede 
30314ca7a47SHans de Goede static const struct thermal_cooling_device_ops video_cooling_ops = {
30414ca7a47SHans de Goede 	.get_max_state = video_get_max_state,
30514ca7a47SHans de Goede 	.get_cur_state = video_get_cur_state,
30614ca7a47SHans de Goede 	.set_cur_state = video_set_cur_state,
30714ca7a47SHans de Goede };
30814ca7a47SHans de Goede 
30914ca7a47SHans de Goede /*
31014ca7a47SHans de Goede  * --------------------------------------------------------------------------
31114ca7a47SHans de Goede  *                             Video Management
31214ca7a47SHans de Goede  * --------------------------------------------------------------------------
31314ca7a47SHans de Goede  */
31414ca7a47SHans de Goede 
31514ca7a47SHans de Goede static int
31605950094SAaron Lu acpi_video_device_lcd_query_levels(acpi_handle handle,
31714ca7a47SHans de Goede 				   union acpi_object **levels)
31814ca7a47SHans de Goede {
31914ca7a47SHans de Goede 	int status;
32014ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
32114ca7a47SHans de Goede 	union acpi_object *obj;
32214ca7a47SHans de Goede 
32314ca7a47SHans de Goede 
32414ca7a47SHans de Goede 	*levels = NULL;
32514ca7a47SHans de Goede 
32605950094SAaron Lu 	status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
3272924d2f8SRafael J. Wysocki 	if (ACPI_FAILURE(status))
32814ca7a47SHans de Goede 		return status;
32914ca7a47SHans de Goede 	obj = (union acpi_object *)buffer.pointer;
33014ca7a47SHans de Goede 	if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3312924d2f8SRafael J. Wysocki 		acpi_handle_info(handle, "Invalid _BCL data\n");
33214ca7a47SHans de Goede 		status = -EFAULT;
33314ca7a47SHans de Goede 		goto err;
33414ca7a47SHans de Goede 	}
33514ca7a47SHans de Goede 
33614ca7a47SHans de Goede 	*levels = obj;
33714ca7a47SHans de Goede 
33814ca7a47SHans de Goede 	return 0;
33914ca7a47SHans de Goede 
34014ca7a47SHans de Goede err:
34114ca7a47SHans de Goede 	kfree(buffer.pointer);
34214ca7a47SHans de Goede 
34314ca7a47SHans de Goede 	return status;
34414ca7a47SHans de Goede }
34514ca7a47SHans de Goede 
34614ca7a47SHans de Goede static int
34714ca7a47SHans de Goede acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
34814ca7a47SHans de Goede {
34914ca7a47SHans de Goede 	int status;
35014ca7a47SHans de Goede 	int state;
35114ca7a47SHans de Goede 
35214ca7a47SHans de Goede 	status = acpi_execute_simple_method(device->dev->handle,
35314ca7a47SHans de Goede 					    "_BCM", level);
35414ca7a47SHans de Goede 	if (ACPI_FAILURE(status)) {
3552924d2f8SRafael J. Wysocki 		acpi_handle_info(device->dev->handle, "_BCM evaluation failed\n");
35614ca7a47SHans de Goede 		return -EIO;
35714ca7a47SHans de Goede 	}
35814ca7a47SHans de Goede 
35914ca7a47SHans de Goede 	device->brightness->curr = level;
360d485ef43SDmitry Frank 	for (state = ACPI_VIDEO_FIRST_LEVEL; state < device->brightness->count;
361d485ef43SDmitry Frank 	     state++)
36214ca7a47SHans de Goede 		if (level == device->brightness->levels[state]) {
36314ca7a47SHans de Goede 			if (device->backlight)
364d485ef43SDmitry Frank 				device->backlight->props.brightness =
365d485ef43SDmitry Frank 					state - ACPI_VIDEO_FIRST_LEVEL;
36614ca7a47SHans de Goede 			return 0;
36714ca7a47SHans de Goede 		}
36814ca7a47SHans de Goede 
3692924d2f8SRafael J. Wysocki 	acpi_handle_info(device->dev->handle, "Current brightness invalid\n");
37014ca7a47SHans de Goede 	return -EINVAL;
37114ca7a47SHans de Goede }
37214ca7a47SHans de Goede 
37314ca7a47SHans de Goede /*
37414ca7a47SHans de Goede  * For some buggy _BQC methods, we need to add a constant value to
37514ca7a47SHans de Goede  * the _BQC return value to get the actual current brightness level
37614ca7a47SHans de Goede  */
37714ca7a47SHans de Goede 
37814ca7a47SHans de Goede static int bqc_offset_aml_bug_workaround;
3797ee33baaSHans de Goede static int video_set_bqc_offset(const struct dmi_system_id *d)
38014ca7a47SHans de Goede {
38114ca7a47SHans de Goede 	bqc_offset_aml_bug_workaround = 9;
38214ca7a47SHans de Goede 	return 0;
38314ca7a47SHans de Goede }
38414ca7a47SHans de Goede 
3857ee33baaSHans de Goede static int video_disable_backlight_sysfs_if(
38614ca7a47SHans de Goede 	const struct dmi_system_id *d)
38714ca7a47SHans de Goede {
38814ca7a47SHans de Goede 	if (disable_backlight_sysfs_if == -1)
38914ca7a47SHans de Goede 		disable_backlight_sysfs_if = 1;
39014ca7a47SHans de Goede 	return 0;
39114ca7a47SHans de Goede }
39214ca7a47SHans de Goede 
393e50b9be1SAaron Lu static int video_set_device_id_scheme(const struct dmi_system_id *d)
394e50b9be1SAaron Lu {
395e50b9be1SAaron Lu 	device_id_scheme = true;
396e50b9be1SAaron Lu 	return 0;
397e50b9be1SAaron Lu }
398e50b9be1SAaron Lu 
399e50b9be1SAaron Lu static int video_enable_only_lcd(const struct dmi_system_id *d)
400e50b9be1SAaron Lu {
401e50b9be1SAaron Lu 	only_lcd = true;
402e50b9be1SAaron Lu 	return 0;
403e50b9be1SAaron Lu }
404e50b9be1SAaron Lu 
4054b4b3b20SHans de Goede static int video_set_report_key_events(const struct dmi_system_id *id)
4064b4b3b20SHans de Goede {
4074b4b3b20SHans de Goede 	if (report_key_events == -1)
4084b4b3b20SHans de Goede 		report_key_events = (uintptr_t)id->driver_data;
4094b4b3b20SHans de Goede 	return 0;
4104b4b3b20SHans de Goede }
4114b4b3b20SHans de Goede 
4124f7f9645SHans de Goede static int video_hw_changes_brightness(
4134f7f9645SHans de Goede 	const struct dmi_system_id *d)
4144f7f9645SHans de Goede {
4154f7f9645SHans de Goede 	if (hw_changes_brightness == -1)
4164f7f9645SHans de Goede 		hw_changes_brightness = 1;
4174f7f9645SHans de Goede 	return 0;
4184f7f9645SHans de Goede }
4194f7f9645SHans de Goede 
4206faadbbbSChristoph Hellwig static const struct dmi_system_id video_dmi_table[] = {
42114ca7a47SHans de Goede 	/*
42214ca7a47SHans de Goede 	 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
42314ca7a47SHans de Goede 	 */
42414ca7a47SHans de Goede 	{
42514ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
42614ca7a47SHans de Goede 	 .ident = "Acer Aspire 5720",
42714ca7a47SHans de Goede 	 .matches = {
42814ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
42914ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
43014ca7a47SHans de Goede 		},
43114ca7a47SHans de Goede 	},
43214ca7a47SHans de Goede 	{
43314ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
43414ca7a47SHans de Goede 	 .ident = "Acer Aspire 5710Z",
43514ca7a47SHans de Goede 	 .matches = {
43614ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
43714ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
43814ca7a47SHans de Goede 		},
43914ca7a47SHans de Goede 	},
44014ca7a47SHans de Goede 	{
44114ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
44214ca7a47SHans de Goede 	 .ident = "eMachines E510",
44314ca7a47SHans de Goede 	 .matches = {
44414ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
44514ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
44614ca7a47SHans de Goede 		},
44714ca7a47SHans de Goede 	},
44814ca7a47SHans de Goede 	{
44914ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
45014ca7a47SHans de Goede 	 .ident = "Acer Aspire 5315",
45114ca7a47SHans de Goede 	 .matches = {
45214ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
45314ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
45414ca7a47SHans de Goede 		},
45514ca7a47SHans de Goede 	},
45614ca7a47SHans de Goede 	{
45714ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
45814ca7a47SHans de Goede 	 .ident = "Acer Aspire 7720",
45914ca7a47SHans de Goede 	 .matches = {
46014ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
46114ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
46214ca7a47SHans de Goede 		},
46314ca7a47SHans de Goede 	},
46414ca7a47SHans de Goede 
46514ca7a47SHans de Goede 	/*
46614ca7a47SHans de Goede 	 * Some machines have a broken acpi-video interface for brightness
46714ca7a47SHans de Goede 	 * control, but still need an acpi_video_device_lcd_set_level() call
46814ca7a47SHans de Goede 	 * on resume to turn the backlight power on.  We Enable backlight
46914ca7a47SHans de Goede 	 * control on these systems, but do not register a backlight sysfs
47014ca7a47SHans de Goede 	 * as brightness control does not work.
47114ca7a47SHans de Goede 	 */
47214ca7a47SHans de Goede 	{
473de588b8fSHans de Goede 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
474de588b8fSHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
475de588b8fSHans de Goede 	 .ident = "Toshiba Portege R700",
476de588b8fSHans de Goede 	 .matches = {
477de588b8fSHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
478de588b8fSHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R700"),
479de588b8fSHans de Goede 		},
480de588b8fSHans de Goede 	},
481de588b8fSHans de Goede 	{
48214ca7a47SHans de Goede 	 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
48314ca7a47SHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
48414ca7a47SHans de Goede 	 .ident = "Toshiba Portege R830",
48514ca7a47SHans de Goede 	 .matches = {
48614ca7a47SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
48714ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
48814ca7a47SHans de Goede 		},
48914ca7a47SHans de Goede 	},
490b21f2e81SHans de Goede 	{
491b21f2e81SHans de Goede 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
492b21f2e81SHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
493b21f2e81SHans de Goede 	 .ident = "Toshiba Satellite R830",
494b21f2e81SHans de Goede 	 .matches = {
495b21f2e81SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
496b21f2e81SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE R830"),
497b21f2e81SHans de Goede 		},
498b21f2e81SHans de Goede 	},
499e50b9be1SAaron Lu 	/*
500e50b9be1SAaron Lu 	 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
501e50b9be1SAaron Lu 	 * but the IDs actually follow the Device ID Scheme.
502e50b9be1SAaron Lu 	 */
503e50b9be1SAaron Lu 	{
504e50b9be1SAaron Lu 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
505e50b9be1SAaron Lu 	 .callback = video_set_device_id_scheme,
506e50b9be1SAaron Lu 	 .ident = "ESPRIMO Mobile M9410",
507e50b9be1SAaron Lu 	 .matches = {
508e50b9be1SAaron Lu 		DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
509e50b9be1SAaron Lu 		DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
510e50b9be1SAaron Lu 		},
511e50b9be1SAaron Lu 	},
512e50b9be1SAaron Lu 	/*
513e50b9be1SAaron Lu 	 * Some machines have multiple video output devices, but only the one
514e50b9be1SAaron Lu 	 * that is the type of LCD can do the backlight control so we should not
515e50b9be1SAaron Lu 	 * register backlight interface for other video output devices.
516e50b9be1SAaron Lu 	 */
517e50b9be1SAaron Lu 	{
518e50b9be1SAaron Lu 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
519e50b9be1SAaron Lu 	 .callback = video_enable_only_lcd,
520e50b9be1SAaron Lu 	 .ident = "ESPRIMO Mobile M9410",
521e50b9be1SAaron Lu 	 .matches = {
522e50b9be1SAaron Lu 		DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
523e50b9be1SAaron Lu 		DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
524e50b9be1SAaron Lu 		},
525e50b9be1SAaron Lu 	},
5264b4b3b20SHans de Goede 	/*
5274b4b3b20SHans de Goede 	 * Some machines report wrong key events on the acpi-bus, suppress
5284b4b3b20SHans de Goede 	 * key event reporting on these.  Note this is only intended to work
5294b4b3b20SHans de Goede 	 * around events which are plain wrong. In some cases we get double
5304b4b3b20SHans de Goede 	 * events, in this case acpi-video is considered the canonical source
5314b4b3b20SHans de Goede 	 * and the events from the other source should be filtered. E.g.
5324b4b3b20SHans de Goede 	 * by calling acpi_video_handles_brightness_key_presses() from the
5334b4b3b20SHans de Goede 	 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
5344b4b3b20SHans de Goede 	 */
5354b4b3b20SHans de Goede 	{
5364b4b3b20SHans de Goede 	 .callback = video_set_report_key_events,
5374b4b3b20SHans de Goede 	 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
5384b4b3b20SHans de Goede 	 .ident = "Dell Vostro V131",
5394b4b3b20SHans de Goede 	 .matches = {
5404b4b3b20SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
5414b4b3b20SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
5424b4b3b20SHans de Goede 		},
5434b4b3b20SHans de Goede 	},
5449249c32eSHans de Goede 	{
5459249c32eSHans de Goede 	 .callback = video_set_report_key_events,
5469249c32eSHans de Goede 	 .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS),
5479249c32eSHans de Goede 	 .ident = "Dell Vostro 3350",
5489249c32eSHans de Goede 	 .matches = {
5499249c32eSHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
5509249c32eSHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
5519249c32eSHans de Goede 		},
5529249c32eSHans de Goede 	},
5534f7f9645SHans de Goede 	/*
5544f7f9645SHans de Goede 	 * Some machines change the brightness themselves when a brightness
5554f7f9645SHans de Goede 	 * hotkey gets pressed, despite us telling them not to. In this case
5564f7f9645SHans de Goede 	 * acpi_video_device_notify() should only call backlight_force_update(
5574f7f9645SHans de Goede 	 * BACKLIGHT_UPDATE_HOTKEY) and not do anything else.
5584f7f9645SHans de Goede 	 */
5594f7f9645SHans de Goede 	{
5604f7f9645SHans de Goede 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=204077 */
5614f7f9645SHans de Goede 	 .callback = video_hw_changes_brightness,
5624f7f9645SHans de Goede 	 .ident = "Packard Bell EasyNote MZ35",
5634f7f9645SHans de Goede 	 .matches = {
5644f7f9645SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "Packard Bell"),
5654f7f9645SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "EasyNote MZ35"),
5664f7f9645SHans de Goede 		},
5674f7f9645SHans de Goede 	},
56814ca7a47SHans de Goede 	{}
56914ca7a47SHans de Goede };
57014ca7a47SHans de Goede 
57114ca7a47SHans de Goede static unsigned long long
57214ca7a47SHans de Goede acpi_video_bqc_value_to_level(struct acpi_video_device *device,
57314ca7a47SHans de Goede 			      unsigned long long bqc_value)
57414ca7a47SHans de Goede {
57514ca7a47SHans de Goede 	unsigned long long level;
57614ca7a47SHans de Goede 
57714ca7a47SHans de Goede 	if (device->brightness->flags._BQC_use_index) {
57814ca7a47SHans de Goede 		/*
579d485ef43SDmitry Frank 		 * _BQC returns an index that doesn't account for the first 2
580d485ef43SDmitry Frank 		 * items with special meaning (see enum acpi_video_level_idx),
581d485ef43SDmitry Frank 		 * so we need to compensate for that by offsetting ourselves
58214ca7a47SHans de Goede 		 */
58314ca7a47SHans de Goede 		if (device->brightness->flags._BCL_reversed)
584d485ef43SDmitry Frank 			bqc_value = device->brightness->count -
585d485ef43SDmitry Frank 				ACPI_VIDEO_FIRST_LEVEL - 1 - bqc_value;
58614ca7a47SHans de Goede 
587d485ef43SDmitry Frank 		level = device->brightness->levels[bqc_value +
588d485ef43SDmitry Frank 						   ACPI_VIDEO_FIRST_LEVEL];
58914ca7a47SHans de Goede 	} else {
59014ca7a47SHans de Goede 		level = bqc_value;
59114ca7a47SHans de Goede 	}
59214ca7a47SHans de Goede 
59314ca7a47SHans de Goede 	level += bqc_offset_aml_bug_workaround;
59414ca7a47SHans de Goede 
59514ca7a47SHans de Goede 	return level;
59614ca7a47SHans de Goede }
59714ca7a47SHans de Goede 
59814ca7a47SHans de Goede static int
59914ca7a47SHans de Goede acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
60014ca7a47SHans de Goede 					unsigned long long *level, bool raw)
60114ca7a47SHans de Goede {
60214ca7a47SHans de Goede 	acpi_status status = AE_OK;
60314ca7a47SHans de Goede 	int i;
60414ca7a47SHans de Goede 
60514ca7a47SHans de Goede 	if (device->cap._BQC || device->cap._BCQ) {
60614ca7a47SHans de Goede 		char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
60714ca7a47SHans de Goede 
60814ca7a47SHans de Goede 		status = acpi_evaluate_integer(device->dev->handle, buf,
60914ca7a47SHans de Goede 						NULL, level);
61014ca7a47SHans de Goede 		if (ACPI_SUCCESS(status)) {
61114ca7a47SHans de Goede 			if (raw) {
61214ca7a47SHans de Goede 				/*
61314ca7a47SHans de Goede 				 * Caller has indicated he wants the raw
61414ca7a47SHans de Goede 				 * value returned by _BQC, so don't furtherly
61514ca7a47SHans de Goede 				 * mess with the value.
61614ca7a47SHans de Goede 				 */
61714ca7a47SHans de Goede 				return 0;
61814ca7a47SHans de Goede 			}
61914ca7a47SHans de Goede 
62014ca7a47SHans de Goede 			*level = acpi_video_bqc_value_to_level(device, *level);
62114ca7a47SHans de Goede 
622d485ef43SDmitry Frank 			for (i = ACPI_VIDEO_FIRST_LEVEL;
623d485ef43SDmitry Frank 			     i < device->brightness->count; i++)
62414ca7a47SHans de Goede 				if (device->brightness->levels[i] == *level) {
62514ca7a47SHans de Goede 					device->brightness->curr = *level;
62614ca7a47SHans de Goede 					return 0;
62714ca7a47SHans de Goede 				}
62814ca7a47SHans de Goede 			/*
62914ca7a47SHans de Goede 			 * BQC returned an invalid level.
63014ca7a47SHans de Goede 			 * Stop using it.
63114ca7a47SHans de Goede 			 */
6322924d2f8SRafael J. Wysocki 			acpi_handle_info(device->dev->handle,
6332924d2f8SRafael J. Wysocki 					 "%s returned an invalid level", buf);
63414ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
63514ca7a47SHans de Goede 		} else {
63614ca7a47SHans de Goede 			/*
63714ca7a47SHans de Goede 			 * Fixme:
63814ca7a47SHans de Goede 			 * should we return an error or ignore this failure?
63914ca7a47SHans de Goede 			 * dev->brightness->curr is a cached value which stores
64014ca7a47SHans de Goede 			 * the correct current backlight level in most cases.
64114ca7a47SHans de Goede 			 * ACPI video backlight still works w/ buggy _BQC.
64214ca7a47SHans de Goede 			 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
64314ca7a47SHans de Goede 			 */
6442924d2f8SRafael J. Wysocki 			acpi_handle_info(device->dev->handle,
6452924d2f8SRafael J. Wysocki 					 "%s evaluation failed", buf);
64614ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
64714ca7a47SHans de Goede 		}
64814ca7a47SHans de Goede 	}
64914ca7a47SHans de Goede 
65014ca7a47SHans de Goede 	*level = device->brightness->curr;
65114ca7a47SHans de Goede 	return 0;
65214ca7a47SHans de Goede }
65314ca7a47SHans de Goede 
65414ca7a47SHans de Goede static int
65514ca7a47SHans de Goede acpi_video_device_EDID(struct acpi_video_device *device,
65614ca7a47SHans de Goede 		       union acpi_object **edid, ssize_t length)
65714ca7a47SHans de Goede {
65814ca7a47SHans de Goede 	int status;
65914ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
66014ca7a47SHans de Goede 	union acpi_object *obj;
66114ca7a47SHans de Goede 	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
66214ca7a47SHans de Goede 	struct acpi_object_list args = { 1, &arg0 };
66314ca7a47SHans de Goede 
66414ca7a47SHans de Goede 
66514ca7a47SHans de Goede 	*edid = NULL;
66614ca7a47SHans de Goede 
66714ca7a47SHans de Goede 	if (!device)
66814ca7a47SHans de Goede 		return -ENODEV;
66914ca7a47SHans de Goede 	if (length == 128)
67014ca7a47SHans de Goede 		arg0.integer.value = 1;
67114ca7a47SHans de Goede 	else if (length == 256)
67214ca7a47SHans de Goede 		arg0.integer.value = 2;
67314ca7a47SHans de Goede 	else
67414ca7a47SHans de Goede 		return -EINVAL;
67514ca7a47SHans de Goede 
67614ca7a47SHans de Goede 	status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
67714ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
67814ca7a47SHans de Goede 		return -ENODEV;
67914ca7a47SHans de Goede 
68014ca7a47SHans de Goede 	obj = buffer.pointer;
68114ca7a47SHans de Goede 
68214ca7a47SHans de Goede 	if (obj && obj->type == ACPI_TYPE_BUFFER)
68314ca7a47SHans de Goede 		*edid = obj;
68414ca7a47SHans de Goede 	else {
6852924d2f8SRafael J. Wysocki 		acpi_handle_info(device->dev->handle, "Invalid _DDC data\n");
68614ca7a47SHans de Goede 		status = -EFAULT;
68714ca7a47SHans de Goede 		kfree(obj);
68814ca7a47SHans de Goede 	}
68914ca7a47SHans de Goede 
69014ca7a47SHans de Goede 	return status;
69114ca7a47SHans de Goede }
69214ca7a47SHans de Goede 
69314ca7a47SHans de Goede /* bus */
69414ca7a47SHans de Goede 
69514ca7a47SHans de Goede /*
69614ca7a47SHans de Goede  *  Arg:
69714ca7a47SHans de Goede  *	video		: video bus device pointer
69814ca7a47SHans de Goede  *	bios_flag	:
69914ca7a47SHans de Goede  *		0.	The system BIOS should NOT automatically switch(toggle)
70014ca7a47SHans de Goede  *			the active display output.
70114ca7a47SHans de Goede  *		1.	The system BIOS should automatically switch (toggle) the
70214ca7a47SHans de Goede  *			active display output. No switch event.
70314ca7a47SHans de Goede  *		2.	The _DGS value should be locked.
70414ca7a47SHans de Goede  *		3.	The system BIOS should not automatically switch (toggle) the
70514ca7a47SHans de Goede  *			active display output, but instead generate the display switch
70614ca7a47SHans de Goede  *			event notify code.
70714ca7a47SHans de Goede  *	lcd_flag	:
70814ca7a47SHans de Goede  *		0.	The system BIOS should automatically control the brightness level
709b5b42b24SKacper Piwiński  *			of the LCD when:
710b5b42b24SKacper Piwiński  *			- the power changes from AC to DC (ACPI appendix B)
711b5b42b24SKacper Piwiński  *			- a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
71214ca7a47SHans de Goede  *		1.	The system BIOS should NOT automatically control the brightness
713b5b42b24SKacper Piwiński  *			level of the LCD when:
714b5b42b24SKacper Piwiński  *			- the power changes from AC to DC (ACPI appendix B)
715b5b42b24SKacper Piwiński  *			- a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
71614ca7a47SHans de Goede  *  Return Value:
71714ca7a47SHans de Goede  *		-EINVAL	wrong arg.
71814ca7a47SHans de Goede  */
71914ca7a47SHans de Goede 
72014ca7a47SHans de Goede static int
72114ca7a47SHans de Goede acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
72214ca7a47SHans de Goede {
72314ca7a47SHans de Goede 	acpi_status status;
72414ca7a47SHans de Goede 
72514ca7a47SHans de Goede 	if (!video->cap._DOS)
72614ca7a47SHans de Goede 		return 0;
72714ca7a47SHans de Goede 
72814ca7a47SHans de Goede 	if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
72914ca7a47SHans de Goede 		return -EINVAL;
73014ca7a47SHans de Goede 	video->dos_setting = (lcd_flag << 2) | bios_flag;
73114ca7a47SHans de Goede 	status = acpi_execute_simple_method(video->device->handle, "_DOS",
73214ca7a47SHans de Goede 					    (lcd_flag << 2) | bios_flag);
73314ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
73414ca7a47SHans de Goede 		return -EIO;
73514ca7a47SHans de Goede 
73614ca7a47SHans de Goede 	return 0;
73714ca7a47SHans de Goede }
73814ca7a47SHans de Goede 
73914ca7a47SHans de Goede /*
74014ca7a47SHans de Goede  * Simple comparison function used to sort backlight levels.
74114ca7a47SHans de Goede  */
74214ca7a47SHans de Goede 
74314ca7a47SHans de Goede static int
74414ca7a47SHans de Goede acpi_video_cmp_level(const void *a, const void *b)
74514ca7a47SHans de Goede {
74614ca7a47SHans de Goede 	return *(int *)a - *(int *)b;
74714ca7a47SHans de Goede }
74814ca7a47SHans de Goede 
74914ca7a47SHans de Goede /*
75014ca7a47SHans de Goede  * Decides if _BQC/_BCQ for this system is usable
75114ca7a47SHans de Goede  *
75214ca7a47SHans de Goede  * We do this by changing the level first and then read out the current
75314ca7a47SHans de Goede  * brightness level, if the value does not match, find out if it is using
75414ca7a47SHans de Goede  * index. If not, clear the _BQC/_BCQ capability.
75514ca7a47SHans de Goede  */
75614ca7a47SHans de Goede static int acpi_video_bqc_quirk(struct acpi_video_device *device,
75714ca7a47SHans de Goede 				int max_level, int current_level)
75814ca7a47SHans de Goede {
75914ca7a47SHans de Goede 	struct acpi_video_device_brightness *br = device->brightness;
76014ca7a47SHans de Goede 	int result;
76114ca7a47SHans de Goede 	unsigned long long level;
76214ca7a47SHans de Goede 	int test_level;
76314ca7a47SHans de Goede 
76414ca7a47SHans de Goede 	/* don't mess with existing known broken systems */
76514ca7a47SHans de Goede 	if (bqc_offset_aml_bug_workaround)
76614ca7a47SHans de Goede 		return 0;
76714ca7a47SHans de Goede 
76814ca7a47SHans de Goede 	/*
76914ca7a47SHans de Goede 	 * Some systems always report current brightness level as maximum
770592c8095SDmitry Frank 	 * through _BQC, we need to test another value for them. However,
771592c8095SDmitry Frank 	 * there is a subtlety:
772592c8095SDmitry Frank 	 *
773592c8095SDmitry Frank 	 * If the _BCL package ordering is descending, the first level
774592c8095SDmitry Frank 	 * (br->levels[2]) is likely to be 0, and if the number of levels
775592c8095SDmitry Frank 	 * matches the number of steps, we might confuse a returned level to
776592c8095SDmitry Frank 	 * mean the index.
777592c8095SDmitry Frank 	 *
778592c8095SDmitry Frank 	 * For example:
779592c8095SDmitry Frank 	 *
780592c8095SDmitry Frank 	 *     current_level = max_level = 100
781592c8095SDmitry Frank 	 *     test_level = 0
782592c8095SDmitry Frank 	 *     returned level = 100
783592c8095SDmitry Frank 	 *
784592c8095SDmitry Frank 	 * In this case 100 means the level, not the index, and _BCM failed.
785592c8095SDmitry Frank 	 * Still, if the _BCL package ordering is descending, the index of
786592c8095SDmitry Frank 	 * level 0 is also 100, so we assume _BQC is indexed, when it's not.
787592c8095SDmitry Frank 	 *
788592c8095SDmitry Frank 	 * This causes all _BQC calls to return bogus values causing weird
789592c8095SDmitry Frank 	 * behavior from the user's perspective.  For example:
790592c8095SDmitry Frank 	 *
791592c8095SDmitry Frank 	 * xbacklight -set 10; xbacklight -set 20;
792592c8095SDmitry Frank 	 *
793592c8095SDmitry Frank 	 * would flash to 90% and then slowly down to the desired level (20).
794592c8095SDmitry Frank 	 *
795592c8095SDmitry Frank 	 * The solution is simple; test anything other than the first level
796592c8095SDmitry Frank 	 * (e.g. 1).
79714ca7a47SHans de Goede 	 */
798d485ef43SDmitry Frank 	test_level = current_level == max_level
799d485ef43SDmitry Frank 		? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1]
800d485ef43SDmitry Frank 		: max_level;
80114ca7a47SHans de Goede 
80214ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, test_level);
80314ca7a47SHans de Goede 	if (result)
80414ca7a47SHans de Goede 		return result;
80514ca7a47SHans de Goede 
80614ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device, &level, true);
80714ca7a47SHans de Goede 	if (result)
80814ca7a47SHans de Goede 		return result;
80914ca7a47SHans de Goede 
81014ca7a47SHans de Goede 	if (level != test_level) {
81114ca7a47SHans de Goede 		/* buggy _BQC found, need to find out if it uses index */
81214ca7a47SHans de Goede 		if (level < br->count) {
81314ca7a47SHans de Goede 			if (br->flags._BCL_reversed)
814d485ef43SDmitry Frank 				level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level;
815d485ef43SDmitry Frank 			if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level)
81614ca7a47SHans de Goede 				br->flags._BQC_use_index = 1;
81714ca7a47SHans de Goede 		}
81814ca7a47SHans de Goede 
81914ca7a47SHans de Goede 		if (!br->flags._BQC_use_index)
82014ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
82114ca7a47SHans de Goede 	}
82214ca7a47SHans de Goede 
82314ca7a47SHans de Goede 	return 0;
82414ca7a47SHans de Goede }
82514ca7a47SHans de Goede 
82605950094SAaron Lu int acpi_video_get_levels(struct acpi_device *device,
8279f9cd7eeSAaron Lu 			  struct acpi_video_device_brightness **dev_br,
8289f9cd7eeSAaron Lu 			  int *pmax_level)
82914ca7a47SHans de Goede {
83014ca7a47SHans de Goede 	union acpi_object *obj = NULL;
83114ca7a47SHans de Goede 	int i, max_level = 0, count = 0, level_ac_battery = 0;
83214ca7a47SHans de Goede 	union acpi_object *o;
83314ca7a47SHans de Goede 	struct acpi_video_device_brightness *br = NULL;
83405950094SAaron Lu 	int result = 0;
83514ca7a47SHans de Goede 	u32 value;
83614ca7a47SHans de Goede 
8372924d2f8SRafael J. Wysocki 	if (ACPI_FAILURE(acpi_video_device_lcd_query_levels(device->handle, &obj))) {
8382924d2f8SRafael J. Wysocki 		acpi_handle_debug(device->handle,
8392924d2f8SRafael J. Wysocki 				  "Could not query available LCD brightness level\n");
84005950094SAaron Lu 		result = -ENODEV;
84114ca7a47SHans de Goede 		goto out;
84214ca7a47SHans de Goede 	}
84314ca7a47SHans de Goede 
844d485ef43SDmitry Frank 	if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) {
84505950094SAaron Lu 		result = -EINVAL;
84614ca7a47SHans de Goede 		goto out;
84705950094SAaron Lu 	}
84814ca7a47SHans de Goede 
84914ca7a47SHans de Goede 	br = kzalloc(sizeof(*br), GFP_KERNEL);
85014ca7a47SHans de Goede 	if (!br) {
85114ca7a47SHans de Goede 		result = -ENOMEM;
85214ca7a47SHans de Goede 		goto out;
85314ca7a47SHans de Goede 	}
85414ca7a47SHans de Goede 
855592c8095SDmitry Frank 	/*
856592c8095SDmitry Frank 	 * Note that we have to reserve 2 extra items (ACPI_VIDEO_FIRST_LEVEL),
857592c8095SDmitry Frank 	 * in order to account for buggy BIOS which don't export the first two
858592c8095SDmitry Frank 	 * special levels (see below)
859592c8095SDmitry Frank 	 */
8606da2ec56SKees Cook 	br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
8616da2ec56SKees Cook 				   sizeof(*br->levels),
8626da2ec56SKees Cook 				   GFP_KERNEL);
86314ca7a47SHans de Goede 	if (!br->levels) {
86414ca7a47SHans de Goede 		result = -ENOMEM;
86514ca7a47SHans de Goede 		goto out_free;
86614ca7a47SHans de Goede 	}
86714ca7a47SHans de Goede 
86814ca7a47SHans de Goede 	for (i = 0; i < obj->package.count; i++) {
86914ca7a47SHans de Goede 		o = (union acpi_object *)&obj->package.elements[i];
87014ca7a47SHans de Goede 		if (o->type != ACPI_TYPE_INTEGER) {
8712924d2f8SRafael J. Wysocki 			acpi_handle_info(device->handle, "Invalid data\n");
87214ca7a47SHans de Goede 			continue;
87314ca7a47SHans de Goede 		}
87414ca7a47SHans de Goede 		value = (u32) o->integer.value;
87514ca7a47SHans de Goede 		/* Skip duplicate entries */
876d485ef43SDmitry Frank 		if (count > ACPI_VIDEO_FIRST_LEVEL
877d485ef43SDmitry Frank 		    && br->levels[count - 1] == value)
87814ca7a47SHans de Goede 			continue;
87914ca7a47SHans de Goede 
88014ca7a47SHans de Goede 		br->levels[count] = value;
88114ca7a47SHans de Goede 
88214ca7a47SHans de Goede 		if (br->levels[count] > max_level)
88314ca7a47SHans de Goede 			max_level = br->levels[count];
88414ca7a47SHans de Goede 		count++;
88514ca7a47SHans de Goede 	}
88614ca7a47SHans de Goede 
88714ca7a47SHans de Goede 	/*
88814ca7a47SHans de Goede 	 * some buggy BIOS don't export the levels
88914ca7a47SHans de Goede 	 * when machine is on AC/Battery in _BCL package.
89014ca7a47SHans de Goede 	 * In this case, the first two elements in _BCL packages
89114ca7a47SHans de Goede 	 * are also supported brightness levels that OS should take care of.
89214ca7a47SHans de Goede 	 */
893d485ef43SDmitry Frank 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) {
894d485ef43SDmitry Frank 		if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL])
89514ca7a47SHans de Goede 			level_ac_battery++;
896d485ef43SDmitry Frank 		if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL])
89714ca7a47SHans de Goede 			level_ac_battery++;
89814ca7a47SHans de Goede 	}
89914ca7a47SHans de Goede 
900d485ef43SDmitry Frank 	if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) {
901d485ef43SDmitry Frank 		level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery;
90214ca7a47SHans de Goede 		br->flags._BCL_no_ac_battery_levels = 1;
903d485ef43SDmitry Frank 		for (i = (count - 1 + level_ac_battery);
904d485ef43SDmitry Frank 		     i >= ACPI_VIDEO_FIRST_LEVEL; i--)
90514ca7a47SHans de Goede 			br->levels[i] = br->levels[i - level_ac_battery];
90614ca7a47SHans de Goede 		count += level_ac_battery;
907d485ef43SDmitry Frank 	} else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL)
9082924d2f8SRafael J. Wysocki 		acpi_handle_info(device->handle,
9092924d2f8SRafael J. Wysocki 				 "Too many duplicates in _BCL package");
91014ca7a47SHans de Goede 
91114ca7a47SHans de Goede 	/* Check if the _BCL package is in a reversed order */
912d485ef43SDmitry Frank 	if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) {
91314ca7a47SHans de Goede 		br->flags._BCL_reversed = 1;
914d485ef43SDmitry Frank 		sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL],
915d485ef43SDmitry Frank 		     count - ACPI_VIDEO_FIRST_LEVEL,
916d485ef43SDmitry Frank 		     sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]),
91714ca7a47SHans de Goede 		     acpi_video_cmp_level, NULL);
91814ca7a47SHans de Goede 	} else if (max_level != br->levels[count - 1])
9192924d2f8SRafael J. Wysocki 		acpi_handle_info(device->handle,
9202924d2f8SRafael J. Wysocki 				 "Found unordered _BCL package");
92114ca7a47SHans de Goede 
92214ca7a47SHans de Goede 	br->count = count;
92305950094SAaron Lu 	*dev_br = br;
9249f9cd7eeSAaron Lu 	if (pmax_level)
9259f9cd7eeSAaron Lu 		*pmax_level = max_level;
92605950094SAaron Lu 
92705950094SAaron Lu out:
92805950094SAaron Lu 	kfree(obj);
92905950094SAaron Lu 	return result;
93005950094SAaron Lu out_free:
93105950094SAaron Lu 	kfree(br);
93205950094SAaron Lu 	goto out;
93305950094SAaron Lu }
93405950094SAaron Lu EXPORT_SYMBOL(acpi_video_get_levels);
93505950094SAaron Lu 
93605950094SAaron Lu /*
93705950094SAaron Lu  *  Arg:
93805950094SAaron Lu  *	device	: video output device (LCD, CRT, ..)
93905950094SAaron Lu  *
94005950094SAaron Lu  *  Return Value:
94105950094SAaron Lu  *	Maximum brightness level
94205950094SAaron Lu  *
94305950094SAaron Lu  *  Allocate and initialize device->brightness.
94405950094SAaron Lu  */
94505950094SAaron Lu 
94605950094SAaron Lu static int
94705950094SAaron Lu acpi_video_init_brightness(struct acpi_video_device *device)
94805950094SAaron Lu {
94905950094SAaron Lu 	int i, max_level = 0;
95005950094SAaron Lu 	unsigned long long level, level_old;
95105950094SAaron Lu 	struct acpi_video_device_brightness *br = NULL;
952966f58dfSColin Ian King 	int result;
95305950094SAaron Lu 
9549f9cd7eeSAaron Lu 	result = acpi_video_get_levels(device->dev, &br, &max_level);
95505950094SAaron Lu 	if (result)
95605950094SAaron Lu 		return result;
95714ca7a47SHans de Goede 	device->brightness = br;
95814ca7a47SHans de Goede 
95914ca7a47SHans de Goede 	/* _BQC uses INDEX while _BCL uses VALUE in some laptops */
96014ca7a47SHans de Goede 	br->curr = level = max_level;
96114ca7a47SHans de Goede 
96214ca7a47SHans de Goede 	if (!device->cap._BQC)
96314ca7a47SHans de Goede 		goto set_level;
96414ca7a47SHans de Goede 
96514ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device,
96614ca7a47SHans de Goede 							 &level_old, true);
96714ca7a47SHans de Goede 	if (result)
96814ca7a47SHans de Goede 		goto out_free_levels;
96914ca7a47SHans de Goede 
97014ca7a47SHans de Goede 	result = acpi_video_bqc_quirk(device, max_level, level_old);
97114ca7a47SHans de Goede 	if (result)
97214ca7a47SHans de Goede 		goto out_free_levels;
97314ca7a47SHans de Goede 	/*
97414ca7a47SHans de Goede 	 * cap._BQC may get cleared due to _BQC is found to be broken
97514ca7a47SHans de Goede 	 * in acpi_video_bqc_quirk, so check again here.
97614ca7a47SHans de Goede 	 */
97714ca7a47SHans de Goede 	if (!device->cap._BQC)
97814ca7a47SHans de Goede 		goto set_level;
97914ca7a47SHans de Goede 
98014ca7a47SHans de Goede 	level = acpi_video_bqc_value_to_level(device, level_old);
98114ca7a47SHans de Goede 	/*
98214ca7a47SHans de Goede 	 * On some buggy laptops, _BQC returns an uninitialized
98314ca7a47SHans de Goede 	 * value when invoked for the first time, i.e.
98414ca7a47SHans de Goede 	 * level_old is invalid (no matter whether it's a level
98514ca7a47SHans de Goede 	 * or an index). Set the backlight to max_level in this case.
98614ca7a47SHans de Goede 	 */
987d485ef43SDmitry Frank 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++)
98814ca7a47SHans de Goede 		if (level == br->levels[i])
98914ca7a47SHans de Goede 			break;
99014ca7a47SHans de Goede 	if (i == br->count || !level)
99114ca7a47SHans de Goede 		level = max_level;
99214ca7a47SHans de Goede 
99314ca7a47SHans de Goede set_level:
99414ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, level);
99514ca7a47SHans de Goede 	if (result)
99614ca7a47SHans de Goede 		goto out_free_levels;
99714ca7a47SHans de Goede 
9982924d2f8SRafael J. Wysocki 	acpi_handle_debug(device->dev->handle, "found %d brightness levels\n",
9992924d2f8SRafael J. Wysocki 			  br->count - ACPI_VIDEO_FIRST_LEVEL);
10002924d2f8SRafael J. Wysocki 
100105950094SAaron Lu 	return 0;
100214ca7a47SHans de Goede 
100314ca7a47SHans de Goede out_free_levels:
100414ca7a47SHans de Goede 	kfree(br->levels);
100514ca7a47SHans de Goede 	kfree(br);
100614ca7a47SHans de Goede 	device->brightness = NULL;
100714ca7a47SHans de Goede 	return result;
100814ca7a47SHans de Goede }
100914ca7a47SHans de Goede 
101014ca7a47SHans de Goede /*
101114ca7a47SHans de Goede  *  Arg:
101214ca7a47SHans de Goede  *	device	: video output device (LCD, CRT, ..)
101314ca7a47SHans de Goede  *
101414ca7a47SHans de Goede  *  Return Value:
101514ca7a47SHans de Goede  *	None
101614ca7a47SHans de Goede  *
101714ca7a47SHans de Goede  *  Find out all required AML methods defined under the output
101814ca7a47SHans de Goede  *  device.
101914ca7a47SHans de Goede  */
102014ca7a47SHans de Goede 
102114ca7a47SHans de Goede static void acpi_video_device_find_cap(struct acpi_video_device *device)
102214ca7a47SHans de Goede {
102314ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_ADR"))
102414ca7a47SHans de Goede 		device->cap._ADR = 1;
102514ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BCL"))
102614ca7a47SHans de Goede 		device->cap._BCL = 1;
102714ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BCM"))
102814ca7a47SHans de Goede 		device->cap._BCM = 1;
102914ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BQC")) {
103014ca7a47SHans de Goede 		device->cap._BQC = 1;
103114ca7a47SHans de Goede 	} else if (acpi_has_method(device->dev->handle, "_BCQ")) {
10322924d2f8SRafael J. Wysocki 		acpi_handle_info(device->dev->handle,
10332924d2f8SRafael J. Wysocki 				 "_BCQ is used instead of _BQC\n");
103414ca7a47SHans de Goede 		device->cap._BCQ = 1;
103514ca7a47SHans de Goede 	}
103614ca7a47SHans de Goede 
103714ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_DDC"))
103814ca7a47SHans de Goede 		device->cap._DDC = 1;
103914ca7a47SHans de Goede }
104014ca7a47SHans de Goede 
104114ca7a47SHans de Goede /*
104214ca7a47SHans de Goede  *  Arg:
104314ca7a47SHans de Goede  *	device	: video output device (VGA)
104414ca7a47SHans de Goede  *
104514ca7a47SHans de Goede  *  Return Value:
104614ca7a47SHans de Goede  *	None
104714ca7a47SHans de Goede  *
104814ca7a47SHans de Goede  *  Find out all required AML methods defined under the video bus device.
104914ca7a47SHans de Goede  */
105014ca7a47SHans de Goede 
105114ca7a47SHans de Goede static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
105214ca7a47SHans de Goede {
105314ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_DOS"))
105414ca7a47SHans de Goede 		video->cap._DOS = 1;
105514ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_DOD"))
105614ca7a47SHans de Goede 		video->cap._DOD = 1;
105714ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_ROM"))
105814ca7a47SHans de Goede 		video->cap._ROM = 1;
105914ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_GPD"))
106014ca7a47SHans de Goede 		video->cap._GPD = 1;
106114ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_SPD"))
106214ca7a47SHans de Goede 		video->cap._SPD = 1;
106314ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_VPO"))
106414ca7a47SHans de Goede 		video->cap._VPO = 1;
106514ca7a47SHans de Goede }
106614ca7a47SHans de Goede 
106714ca7a47SHans de Goede /*
106814ca7a47SHans de Goede  * Check whether the video bus device has required AML method to
106914ca7a47SHans de Goede  * support the desired features
107014ca7a47SHans de Goede  */
107114ca7a47SHans de Goede 
107214ca7a47SHans de Goede static int acpi_video_bus_check(struct acpi_video_bus *video)
107314ca7a47SHans de Goede {
107414ca7a47SHans de Goede 	acpi_status status = -ENOENT;
107514ca7a47SHans de Goede 	struct pci_dev *dev;
107614ca7a47SHans de Goede 
107714ca7a47SHans de Goede 	if (!video)
107814ca7a47SHans de Goede 		return -EINVAL;
107914ca7a47SHans de Goede 
108014ca7a47SHans de Goede 	dev = acpi_get_pci_dev(video->device->handle);
108114ca7a47SHans de Goede 	if (!dev)
108214ca7a47SHans de Goede 		return -ENODEV;
108314ca7a47SHans de Goede 	pci_dev_put(dev);
108414ca7a47SHans de Goede 
108514ca7a47SHans de Goede 	/*
108614ca7a47SHans de Goede 	 * Since there is no HID, CID and so on for VGA driver, we have
108714ca7a47SHans de Goede 	 * to check well known required nodes.
108814ca7a47SHans de Goede 	 */
108914ca7a47SHans de Goede 
109014ca7a47SHans de Goede 	/* Does this device support video switching? */
109114ca7a47SHans de Goede 	if (video->cap._DOS || video->cap._DOD) {
109214ca7a47SHans de Goede 		if (!video->cap._DOS) {
10932924d2f8SRafael J. Wysocki 			pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n",
109414ca7a47SHans de Goede 				acpi_device_bid(video->device));
109514ca7a47SHans de Goede 		}
109614ca7a47SHans de Goede 		video->flags.multihead = 1;
109714ca7a47SHans de Goede 		status = 0;
109814ca7a47SHans de Goede 	}
109914ca7a47SHans de Goede 
110014ca7a47SHans de Goede 	/* Does this device support retrieving a video ROM? */
110114ca7a47SHans de Goede 	if (video->cap._ROM) {
110214ca7a47SHans de Goede 		video->flags.rom = 1;
110314ca7a47SHans de Goede 		status = 0;
110414ca7a47SHans de Goede 	}
110514ca7a47SHans de Goede 
110614ca7a47SHans de Goede 	/* Does this device support configuring which video device to POST? */
110714ca7a47SHans de Goede 	if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
110814ca7a47SHans de Goede 		video->flags.post = 1;
110914ca7a47SHans de Goede 		status = 0;
111014ca7a47SHans de Goede 	}
111114ca7a47SHans de Goede 
111214ca7a47SHans de Goede 	return status;
111314ca7a47SHans de Goede }
111414ca7a47SHans de Goede 
111514ca7a47SHans de Goede /*
111614ca7a47SHans de Goede  * --------------------------------------------------------------------------
111714ca7a47SHans de Goede  *                               Driver Interface
111814ca7a47SHans de Goede  * --------------------------------------------------------------------------
111914ca7a47SHans de Goede  */
112014ca7a47SHans de Goede 
112114ca7a47SHans de Goede /* device interface */
112214ca7a47SHans de Goede static struct acpi_video_device_attrib *
112314ca7a47SHans de Goede acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
112414ca7a47SHans de Goede {
112514ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
112614ca7a47SHans de Goede 	int i;
112714ca7a47SHans de Goede 
112814ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
112914ca7a47SHans de Goede 		ids = &video->attached_array[i];
113014ca7a47SHans de Goede 		if ((ids->value.int_val & 0xffff) == device_id)
113114ca7a47SHans de Goede 			return &ids->value.attrib;
113214ca7a47SHans de Goede 	}
113314ca7a47SHans de Goede 
113414ca7a47SHans de Goede 	return NULL;
113514ca7a47SHans de Goede }
113614ca7a47SHans de Goede 
113714ca7a47SHans de Goede static int
113814ca7a47SHans de Goede acpi_video_get_device_type(struct acpi_video_bus *video,
113914ca7a47SHans de Goede 			   unsigned long device_id)
114014ca7a47SHans de Goede {
114114ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
114214ca7a47SHans de Goede 	int i;
114314ca7a47SHans de Goede 
114414ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
114514ca7a47SHans de Goede 		ids = &video->attached_array[i];
114614ca7a47SHans de Goede 		if ((ids->value.int_val & 0xffff) == device_id)
114714ca7a47SHans de Goede 			return ids->value.int_val;
114814ca7a47SHans de Goede 	}
114914ca7a47SHans de Goede 
115014ca7a47SHans de Goede 	return 0;
115114ca7a47SHans de Goede }
115214ca7a47SHans de Goede 
115314ca7a47SHans de Goede static int
115414ca7a47SHans de Goede acpi_video_bus_get_one_device(struct acpi_device *device,
115514ca7a47SHans de Goede 			      struct acpi_video_bus *video)
115614ca7a47SHans de Goede {
115714ca7a47SHans de Goede 	unsigned long long device_id;
115814ca7a47SHans de Goede 	int status, device_type;
115914ca7a47SHans de Goede 	struct acpi_video_device *data;
116014ca7a47SHans de Goede 	struct acpi_video_device_attrib *attribute;
116114ca7a47SHans de Goede 
116214ca7a47SHans de Goede 	status =
116314ca7a47SHans de Goede 	    acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
116414ca7a47SHans de Goede 	/* Some device omits _ADR, we skip them instead of fail */
116514ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
116614ca7a47SHans de Goede 		return 0;
116714ca7a47SHans de Goede 
116814ca7a47SHans de Goede 	data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
116914ca7a47SHans de Goede 	if (!data)
117014ca7a47SHans de Goede 		return -ENOMEM;
117114ca7a47SHans de Goede 
117214ca7a47SHans de Goede 	strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
117314ca7a47SHans de Goede 	strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
117414ca7a47SHans de Goede 	device->driver_data = data;
117514ca7a47SHans de Goede 
117614ca7a47SHans de Goede 	data->device_id = device_id;
117714ca7a47SHans de Goede 	data->video = video;
117814ca7a47SHans de Goede 	data->dev = device;
117914ca7a47SHans de Goede 	INIT_DELAYED_WORK(&data->switch_brightness_work,
118014ca7a47SHans de Goede 			  acpi_video_switch_brightness);
118114ca7a47SHans de Goede 
118214ca7a47SHans de Goede 	attribute = acpi_video_get_device_attr(video, device_id);
118314ca7a47SHans de Goede 
1184e50b9be1SAaron Lu 	if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
118514ca7a47SHans de Goede 		switch (attribute->display_type) {
118614ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_CRT:
118714ca7a47SHans de Goede 			data->flags.crt = 1;
118814ca7a47SHans de Goede 			break;
118914ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_TV:
119014ca7a47SHans de Goede 			data->flags.tvout = 1;
119114ca7a47SHans de Goede 			break;
119214ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_DVI:
119314ca7a47SHans de Goede 			data->flags.dvi = 1;
119414ca7a47SHans de Goede 			break;
119514ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LCD:
119614ca7a47SHans de Goede 			data->flags.lcd = 1;
119714ca7a47SHans de Goede 			break;
119814ca7a47SHans de Goede 		default:
119914ca7a47SHans de Goede 			data->flags.unknown = 1;
120014ca7a47SHans de Goede 			break;
120114ca7a47SHans de Goede 		}
120214ca7a47SHans de Goede 		if (attribute->bios_can_detect)
120314ca7a47SHans de Goede 			data->flags.bios = 1;
120414ca7a47SHans de Goede 	} else {
120514ca7a47SHans de Goede 		/* Check for legacy IDs */
120614ca7a47SHans de Goede 		device_type = acpi_video_get_device_type(video, device_id);
120714ca7a47SHans de Goede 		/* Ignore bits 16 and 18-20 */
120814ca7a47SHans de Goede 		switch (device_type & 0xffe2ffff) {
120914ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
121014ca7a47SHans de Goede 			data->flags.crt = 1;
121114ca7a47SHans de Goede 			break;
121214ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
121314ca7a47SHans de Goede 			data->flags.lcd = 1;
121414ca7a47SHans de Goede 			break;
121514ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_TV:
121614ca7a47SHans de Goede 			data->flags.tvout = 1;
121714ca7a47SHans de Goede 			break;
121814ca7a47SHans de Goede 		default:
121914ca7a47SHans de Goede 			data->flags.unknown = 1;
122014ca7a47SHans de Goede 		}
122114ca7a47SHans de Goede 	}
122214ca7a47SHans de Goede 
122314ca7a47SHans de Goede 	acpi_video_device_bind(video, data);
122414ca7a47SHans de Goede 	acpi_video_device_find_cap(data);
122514ca7a47SHans de Goede 
12263a0cf7abSHans de Goede 	if (data->cap._BCM && data->cap._BCL)
1227*5ad26161SHans de Goede 		may_report_brightness_keys = true;
12283a0cf7abSHans de Goede 
122914ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
123014ca7a47SHans de Goede 	list_add_tail(&data->entry, &video->video_device_list);
123114ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
123214ca7a47SHans de Goede 
123314ca7a47SHans de Goede 	return status;
123414ca7a47SHans de Goede }
123514ca7a47SHans de Goede 
123614ca7a47SHans de Goede /*
123714ca7a47SHans de Goede  *  Arg:
123814ca7a47SHans de Goede  *	video	: video bus device
123914ca7a47SHans de Goede  *
124014ca7a47SHans de Goede  *  Return:
124114ca7a47SHans de Goede  *	none
124214ca7a47SHans de Goede  *
124314ca7a47SHans de Goede  *  Enumerate the video device list of the video bus,
124414ca7a47SHans de Goede  *  bind the ids with the corresponding video devices
124514ca7a47SHans de Goede  *  under the video bus.
124614ca7a47SHans de Goede  */
124714ca7a47SHans de Goede 
124814ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video)
124914ca7a47SHans de Goede {
125014ca7a47SHans de Goede 	struct acpi_video_device *dev;
125114ca7a47SHans de Goede 
125214ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
125314ca7a47SHans de Goede 
125414ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
125514ca7a47SHans de Goede 		acpi_video_device_bind(video, dev);
125614ca7a47SHans de Goede 
125714ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
125814ca7a47SHans de Goede }
125914ca7a47SHans de Goede 
126014ca7a47SHans de Goede /*
126114ca7a47SHans de Goede  *  Arg:
126214ca7a47SHans de Goede  *	video	: video bus device
126314ca7a47SHans de Goede  *	device	: video output device under the video
126414ca7a47SHans de Goede  *		bus
126514ca7a47SHans de Goede  *
126614ca7a47SHans de Goede  *  Return:
126714ca7a47SHans de Goede  *	none
126814ca7a47SHans de Goede  *
126914ca7a47SHans de Goede  *  Bind the ids with the corresponding video devices
127014ca7a47SHans de Goede  *  under the video bus.
127114ca7a47SHans de Goede  */
127214ca7a47SHans de Goede 
127314ca7a47SHans de Goede static void
127414ca7a47SHans de Goede acpi_video_device_bind(struct acpi_video_bus *video,
127514ca7a47SHans de Goede 		       struct acpi_video_device *device)
127614ca7a47SHans de Goede {
127714ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
127814ca7a47SHans de Goede 	int i;
127914ca7a47SHans de Goede 
128014ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
128114ca7a47SHans de Goede 		ids = &video->attached_array[i];
128214ca7a47SHans de Goede 		if (device->device_id == (ids->value.int_val & 0xffff)) {
128314ca7a47SHans de Goede 			ids->bind_info = device;
12842924d2f8SRafael J. Wysocki 			acpi_handle_debug(video->device->handle, "%s: %d\n",
12852924d2f8SRafael J. Wysocki 					  __func__, i);
128614ca7a47SHans de Goede 		}
128714ca7a47SHans de Goede 	}
128814ca7a47SHans de Goede }
128914ca7a47SHans de Goede 
129014ca7a47SHans de Goede static bool acpi_video_device_in_dod(struct acpi_video_device *device)
129114ca7a47SHans de Goede {
129214ca7a47SHans de Goede 	struct acpi_video_bus *video = device->video;
129314ca7a47SHans de Goede 	int i;
129414ca7a47SHans de Goede 
129514ca7a47SHans de Goede 	/*
129614ca7a47SHans de Goede 	 * If we have a broken _DOD or we have more than 8 output devices
129714ca7a47SHans de Goede 	 * under the graphics controller node that we can't proper deal with
129814ca7a47SHans de Goede 	 * in the operation region code currently, no need to test.
129914ca7a47SHans de Goede 	 */
130014ca7a47SHans de Goede 	if (!video->attached_count || video->child_count > 8)
130114ca7a47SHans de Goede 		return true;
130214ca7a47SHans de Goede 
130314ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
130414ca7a47SHans de Goede 		if ((video->attached_array[i].value.int_val & 0xfff) ==
130514ca7a47SHans de Goede 		    (device->device_id & 0xfff))
130614ca7a47SHans de Goede 			return true;
130714ca7a47SHans de Goede 	}
130814ca7a47SHans de Goede 
130914ca7a47SHans de Goede 	return false;
131014ca7a47SHans de Goede }
131114ca7a47SHans de Goede 
131214ca7a47SHans de Goede /*
131314ca7a47SHans de Goede  *  Arg:
131414ca7a47SHans de Goede  *	video	: video bus device
131514ca7a47SHans de Goede  *
131614ca7a47SHans de Goede  *  Return:
131714ca7a47SHans de Goede  *	< 0	: error
131814ca7a47SHans de Goede  *
131914ca7a47SHans de Goede  *  Call _DOD to enumerate all devices attached to display adapter
132014ca7a47SHans de Goede  *
132114ca7a47SHans de Goede  */
132214ca7a47SHans de Goede 
132314ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video)
132414ca7a47SHans de Goede {
132514ca7a47SHans de Goede 	int status;
132614ca7a47SHans de Goede 	int count;
132714ca7a47SHans de Goede 	int i;
132814ca7a47SHans de Goede 	struct acpi_video_enumerated_device *active_list;
132914ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
133014ca7a47SHans de Goede 	union acpi_object *dod = NULL;
133114ca7a47SHans de Goede 	union acpi_object *obj;
133214ca7a47SHans de Goede 
1333e34fbbacSAlex Hung 	if (!video->cap._DOD)
1334e34fbbacSAlex Hung 		return AE_NOT_EXIST;
1335e34fbbacSAlex Hung 
133614ca7a47SHans de Goede 	status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
13372924d2f8SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
13382924d2f8SRafael J. Wysocki 		acpi_handle_info(video->device->handle,
13392924d2f8SRafael J. Wysocki 				 "_DOD evaluation failed: %s\n",
13402924d2f8SRafael J. Wysocki 				 acpi_format_exception(status));
134114ca7a47SHans de Goede 		return status;
134214ca7a47SHans de Goede 	}
134314ca7a47SHans de Goede 
134414ca7a47SHans de Goede 	dod = buffer.pointer;
134514ca7a47SHans de Goede 	if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
13462924d2f8SRafael J. Wysocki 		acpi_handle_info(video->device->handle, "Invalid _DOD data\n");
134714ca7a47SHans de Goede 		status = -EFAULT;
134814ca7a47SHans de Goede 		goto out;
134914ca7a47SHans de Goede 	}
135014ca7a47SHans de Goede 
13512924d2f8SRafael J. Wysocki 	acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n",
13522924d2f8SRafael J. Wysocki 			  dod->package.count);
135314ca7a47SHans de Goede 
135414ca7a47SHans de Goede 	active_list = kcalloc(1 + dod->package.count,
135514ca7a47SHans de Goede 			      sizeof(struct acpi_video_enumerated_device),
135614ca7a47SHans de Goede 			      GFP_KERNEL);
135714ca7a47SHans de Goede 	if (!active_list) {
135814ca7a47SHans de Goede 		status = -ENOMEM;
135914ca7a47SHans de Goede 		goto out;
136014ca7a47SHans de Goede 	}
136114ca7a47SHans de Goede 
136214ca7a47SHans de Goede 	count = 0;
136314ca7a47SHans de Goede 	for (i = 0; i < dod->package.count; i++) {
136414ca7a47SHans de Goede 		obj = &dod->package.elements[i];
136514ca7a47SHans de Goede 
136614ca7a47SHans de Goede 		if (obj->type != ACPI_TYPE_INTEGER) {
13672924d2f8SRafael J. Wysocki 			acpi_handle_info(video->device->handle,
136814ca7a47SHans de Goede 					 "Invalid _DOD data in element %d\n", i);
136914ca7a47SHans de Goede 			continue;
137014ca7a47SHans de Goede 		}
137114ca7a47SHans de Goede 
137214ca7a47SHans de Goede 		active_list[count].value.int_val = obj->integer.value;
137314ca7a47SHans de Goede 		active_list[count].bind_info = NULL;
13742924d2f8SRafael J. Wysocki 
13752924d2f8SRafael J. Wysocki 		acpi_handle_debug(video->device->handle,
13762924d2f8SRafael J. Wysocki 				  "_DOD element[%d] = %d\n", i,
13772924d2f8SRafael J. Wysocki 				  (int)obj->integer.value);
13782924d2f8SRafael J. Wysocki 
137914ca7a47SHans de Goede 		count++;
138014ca7a47SHans de Goede 	}
138114ca7a47SHans de Goede 
138214ca7a47SHans de Goede 	kfree(video->attached_array);
138314ca7a47SHans de Goede 
138414ca7a47SHans de Goede 	video->attached_array = active_list;
138514ca7a47SHans de Goede 	video->attached_count = count;
138614ca7a47SHans de Goede 
138714ca7a47SHans de Goede out:
138814ca7a47SHans de Goede 	kfree(buffer.pointer);
138914ca7a47SHans de Goede 	return status;
139014ca7a47SHans de Goede }
139114ca7a47SHans de Goede 
139214ca7a47SHans de Goede static int
139314ca7a47SHans de Goede acpi_video_get_next_level(struct acpi_video_device *device,
139414ca7a47SHans de Goede 			  u32 level_current, u32 event)
139514ca7a47SHans de Goede {
139614ca7a47SHans de Goede 	int min, max, min_above, max_below, i, l, delta = 255;
139714ca7a47SHans de Goede 	max = max_below = 0;
139814ca7a47SHans de Goede 	min = min_above = 255;
139914ca7a47SHans de Goede 	/* Find closest level to level_current */
1400d485ef43SDmitry Frank 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
140114ca7a47SHans de Goede 		l = device->brightness->levels[i];
140214ca7a47SHans de Goede 		if (abs(l - level_current) < abs(delta)) {
140314ca7a47SHans de Goede 			delta = l - level_current;
140414ca7a47SHans de Goede 			if (!delta)
140514ca7a47SHans de Goede 				break;
140614ca7a47SHans de Goede 		}
140714ca7a47SHans de Goede 	}
1408935ab850STom Saeger 	/* Adjust level_current to closest available level */
140914ca7a47SHans de Goede 	level_current += delta;
1410d485ef43SDmitry Frank 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
141114ca7a47SHans de Goede 		l = device->brightness->levels[i];
141214ca7a47SHans de Goede 		if (l < min)
141314ca7a47SHans de Goede 			min = l;
141414ca7a47SHans de Goede 		if (l > max)
141514ca7a47SHans de Goede 			max = l;
141614ca7a47SHans de Goede 		if (l < min_above && l > level_current)
141714ca7a47SHans de Goede 			min_above = l;
141814ca7a47SHans de Goede 		if (l > max_below && l < level_current)
141914ca7a47SHans de Goede 			max_below = l;
142014ca7a47SHans de Goede 	}
142114ca7a47SHans de Goede 
142214ca7a47SHans de Goede 	switch (event) {
142314ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
142414ca7a47SHans de Goede 		return (level_current < max) ? min_above : min;
142514ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
142614ca7a47SHans de Goede 		return (level_current < max) ? min_above : max;
142714ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
142814ca7a47SHans de Goede 		return (level_current > min) ? max_below : min;
142914ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
143014ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
143114ca7a47SHans de Goede 		return 0;
143214ca7a47SHans de Goede 	default:
143314ca7a47SHans de Goede 		return level_current;
143414ca7a47SHans de Goede 	}
143514ca7a47SHans de Goede }
143614ca7a47SHans de Goede 
143714ca7a47SHans de Goede static void
143814ca7a47SHans de Goede acpi_video_switch_brightness(struct work_struct *work)
143914ca7a47SHans de Goede {
144014ca7a47SHans de Goede 	struct acpi_video_device *device = container_of(to_delayed_work(work),
144114ca7a47SHans de Goede 			     struct acpi_video_device, switch_brightness_work);
144214ca7a47SHans de Goede 	unsigned long long level_current, level_next;
144314ca7a47SHans de Goede 	int event = device->switch_brightness_event;
144414ca7a47SHans de Goede 	int result = -EINVAL;
144514ca7a47SHans de Goede 
144614ca7a47SHans de Goede 	/* no warning message if acpi_backlight=vendor or a quirk is used */
144714ca7a47SHans de Goede 	if (!device->backlight)
144814ca7a47SHans de Goede 		return;
144914ca7a47SHans de Goede 
145014ca7a47SHans de Goede 	if (!device->brightness)
145114ca7a47SHans de Goede 		goto out;
145214ca7a47SHans de Goede 
145314ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device,
145414ca7a47SHans de Goede 							 &level_current,
145514ca7a47SHans de Goede 							 false);
145614ca7a47SHans de Goede 	if (result)
145714ca7a47SHans de Goede 		goto out;
145814ca7a47SHans de Goede 
145914ca7a47SHans de Goede 	level_next = acpi_video_get_next_level(device, level_current, event);
146014ca7a47SHans de Goede 
146114ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, level_next);
146214ca7a47SHans de Goede 
146314ca7a47SHans de Goede 	if (!result)
146414ca7a47SHans de Goede 		backlight_force_update(device->backlight,
146514ca7a47SHans de Goede 				       BACKLIGHT_UPDATE_HOTKEY);
146614ca7a47SHans de Goede 
146714ca7a47SHans de Goede out:
146814ca7a47SHans de Goede 	if (result)
14692924d2f8SRafael J. Wysocki 		acpi_handle_info(device->dev->handle,
14702924d2f8SRafael J. Wysocki 				 "Failed to switch brightness\n");
147114ca7a47SHans de Goede }
147214ca7a47SHans de Goede 
147314ca7a47SHans de Goede int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
147414ca7a47SHans de Goede 			void **edid)
147514ca7a47SHans de Goede {
147614ca7a47SHans de Goede 	struct acpi_video_bus *video;
147714ca7a47SHans de Goede 	struct acpi_video_device *video_device;
147814ca7a47SHans de Goede 	union acpi_object *buffer = NULL;
147914ca7a47SHans de Goede 	acpi_status status;
148014ca7a47SHans de Goede 	int i, length;
148114ca7a47SHans de Goede 
148214ca7a47SHans de Goede 	if (!device || !acpi_driver_data(device))
148314ca7a47SHans de Goede 		return -EINVAL;
148414ca7a47SHans de Goede 
148514ca7a47SHans de Goede 	video = acpi_driver_data(device);
148614ca7a47SHans de Goede 
148714ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
148814ca7a47SHans de Goede 		video_device = video->attached_array[i].bind_info;
148914ca7a47SHans de Goede 		length = 256;
149014ca7a47SHans de Goede 
149114ca7a47SHans de Goede 		if (!video_device)
149214ca7a47SHans de Goede 			continue;
149314ca7a47SHans de Goede 
149414ca7a47SHans de Goede 		if (!video_device->cap._DDC)
149514ca7a47SHans de Goede 			continue;
149614ca7a47SHans de Goede 
149714ca7a47SHans de Goede 		if (type) {
149814ca7a47SHans de Goede 			switch (type) {
149914ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_CRT:
150014ca7a47SHans de Goede 				if (!video_device->flags.crt)
150114ca7a47SHans de Goede 					continue;
150214ca7a47SHans de Goede 				break;
150314ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_TV:
150414ca7a47SHans de Goede 				if (!video_device->flags.tvout)
150514ca7a47SHans de Goede 					continue;
150614ca7a47SHans de Goede 				break;
150714ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_DVI:
150814ca7a47SHans de Goede 				if (!video_device->flags.dvi)
150914ca7a47SHans de Goede 					continue;
151014ca7a47SHans de Goede 				break;
151114ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_LCD:
151214ca7a47SHans de Goede 				if (!video_device->flags.lcd)
151314ca7a47SHans de Goede 					continue;
151414ca7a47SHans de Goede 				break;
151514ca7a47SHans de Goede 			}
151614ca7a47SHans de Goede 		} else if (video_device->device_id != device_id) {
151714ca7a47SHans de Goede 			continue;
151814ca7a47SHans de Goede 		}
151914ca7a47SHans de Goede 
152014ca7a47SHans de Goede 		status = acpi_video_device_EDID(video_device, &buffer, length);
152114ca7a47SHans de Goede 
152214ca7a47SHans de Goede 		if (ACPI_FAILURE(status) || !buffer ||
152314ca7a47SHans de Goede 		    buffer->type != ACPI_TYPE_BUFFER) {
152414ca7a47SHans de Goede 			length = 128;
152514ca7a47SHans de Goede 			status = acpi_video_device_EDID(video_device, &buffer,
152614ca7a47SHans de Goede 							length);
152714ca7a47SHans de Goede 			if (ACPI_FAILURE(status) || !buffer ||
152814ca7a47SHans de Goede 			    buffer->type != ACPI_TYPE_BUFFER) {
152914ca7a47SHans de Goede 				continue;
153014ca7a47SHans de Goede 			}
153114ca7a47SHans de Goede 		}
153214ca7a47SHans de Goede 
153314ca7a47SHans de Goede 		*edid = buffer->buffer.pointer;
153414ca7a47SHans de Goede 		return length;
153514ca7a47SHans de Goede 	}
153614ca7a47SHans de Goede 
153714ca7a47SHans de Goede 	return -ENODEV;
153814ca7a47SHans de Goede }
153914ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_get_edid);
154014ca7a47SHans de Goede 
154114ca7a47SHans de Goede static int
154214ca7a47SHans de Goede acpi_video_bus_get_devices(struct acpi_video_bus *video,
154314ca7a47SHans de Goede 			   struct acpi_device *device)
154414ca7a47SHans de Goede {
154514ca7a47SHans de Goede 	int status = 0;
154614ca7a47SHans de Goede 	struct acpi_device *dev;
154714ca7a47SHans de Goede 
154814ca7a47SHans de Goede 	/*
154914ca7a47SHans de Goede 	 * There are systems where video module known to work fine regardless
155014ca7a47SHans de Goede 	 * of broken _DOD and ignoring returned value here doesn't cause
155114ca7a47SHans de Goede 	 * any issues later.
155214ca7a47SHans de Goede 	 */
155314ca7a47SHans de Goede 	acpi_video_device_enumerate(video);
155414ca7a47SHans de Goede 
155514ca7a47SHans de Goede 	list_for_each_entry(dev, &device->children, node) {
155614ca7a47SHans de Goede 
155714ca7a47SHans de Goede 		status = acpi_video_bus_get_one_device(dev, video);
155814ca7a47SHans de Goede 		if (status) {
155914ca7a47SHans de Goede 			dev_err(&dev->dev, "Can't attach device\n");
156014ca7a47SHans de Goede 			break;
156114ca7a47SHans de Goede 		}
156214ca7a47SHans de Goede 		video->child_count++;
156314ca7a47SHans de Goede 	}
156414ca7a47SHans de Goede 	return status;
156514ca7a47SHans de Goede }
156614ca7a47SHans de Goede 
156714ca7a47SHans de Goede /* acpi_video interface */
156814ca7a47SHans de Goede 
156914ca7a47SHans de Goede /*
157014ca7a47SHans de Goede  * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1571935ab850STom Saeger  * perform any automatic brightness change on receiving a notification.
157214ca7a47SHans de Goede  */
157314ca7a47SHans de Goede static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
157414ca7a47SHans de Goede {
157514ca7a47SHans de Goede 	return acpi_video_bus_DOS(video, 0,
157614ca7a47SHans de Goede 				  acpi_osi_is_win8() ? 1 : 0);
157714ca7a47SHans de Goede }
157814ca7a47SHans de Goede 
157914ca7a47SHans de Goede static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
158014ca7a47SHans de Goede {
158114ca7a47SHans de Goede 	return acpi_video_bus_DOS(video, 0,
158214ca7a47SHans de Goede 				  acpi_osi_is_win8() ? 0 : 1);
158314ca7a47SHans de Goede }
158414ca7a47SHans de Goede 
158514ca7a47SHans de Goede static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
158614ca7a47SHans de Goede {
158714ca7a47SHans de Goede 	struct acpi_video_bus *video = acpi_driver_data(device);
158814ca7a47SHans de Goede 	struct input_dev *input;
158914ca7a47SHans de Goede 	int keycode = 0;
159014ca7a47SHans de Goede 
159114ca7a47SHans de Goede 	if (!video || !video->input)
159214ca7a47SHans de Goede 		return;
159314ca7a47SHans de Goede 
159414ca7a47SHans de Goede 	input = video->input;
159514ca7a47SHans de Goede 
159614ca7a47SHans de Goede 	switch (event) {
159714ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_SWITCH:	/* User requested a switch,
159814ca7a47SHans de Goede 					 * most likely via hotkey. */
159914ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
160014ca7a47SHans de Goede 		break;
160114ca7a47SHans de Goede 
160214ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_PROBE:	/* User plugged in or removed a video
160314ca7a47SHans de Goede 					 * connector. */
160414ca7a47SHans de Goede 		acpi_video_device_enumerate(video);
160514ca7a47SHans de Goede 		acpi_video_device_rebind(video);
160614ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
160714ca7a47SHans de Goede 		break;
160814ca7a47SHans de Goede 
160914ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE:	/* Cycle Display output hotkey pressed. */
161014ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
161114ca7a47SHans de Goede 		break;
161214ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:	/* Next Display output hotkey pressed. */
161314ca7a47SHans de Goede 		keycode = KEY_VIDEO_NEXT;
161414ca7a47SHans de Goede 		break;
161514ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:	/* previous Display output hotkey pressed. */
161614ca7a47SHans de Goede 		keycode = KEY_VIDEO_PREV;
161714ca7a47SHans de Goede 		break;
161814ca7a47SHans de Goede 
161914ca7a47SHans de Goede 	default:
16202924d2f8SRafael J. Wysocki 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
16212924d2f8SRafael J. Wysocki 				  event);
162214ca7a47SHans de Goede 		break;
162314ca7a47SHans de Goede 	}
162414ca7a47SHans de Goede 
162514ca7a47SHans de Goede 	if (acpi_notifier_call_chain(device, event, 0))
162614ca7a47SHans de Goede 		/* Something vetoed the keypress. */
162714ca7a47SHans de Goede 		keycode = 0;
162814ca7a47SHans de Goede 
162905bc59a0SHans de Goede 	if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
163014ca7a47SHans de Goede 		input_report_key(input, keycode, 1);
163114ca7a47SHans de Goede 		input_sync(input);
163214ca7a47SHans de Goede 		input_report_key(input, keycode, 0);
163314ca7a47SHans de Goede 		input_sync(input);
163414ca7a47SHans de Goede 	}
163514ca7a47SHans de Goede }
163614ca7a47SHans de Goede 
163714ca7a47SHans de Goede static void brightness_switch_event(struct acpi_video_device *video_device,
163814ca7a47SHans de Goede 				    u32 event)
163914ca7a47SHans de Goede {
164014ca7a47SHans de Goede 	if (!brightness_switch_enabled)
164114ca7a47SHans de Goede 		return;
164214ca7a47SHans de Goede 
164314ca7a47SHans de Goede 	video_device->switch_brightness_event = event;
164414ca7a47SHans de Goede 	schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
164514ca7a47SHans de Goede }
164614ca7a47SHans de Goede 
164714ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
164814ca7a47SHans de Goede {
164914ca7a47SHans de Goede 	struct acpi_video_device *video_device = data;
165014ca7a47SHans de Goede 	struct acpi_device *device = NULL;
165114ca7a47SHans de Goede 	struct acpi_video_bus *bus;
165214ca7a47SHans de Goede 	struct input_dev *input;
165314ca7a47SHans de Goede 	int keycode = 0;
165414ca7a47SHans de Goede 
165514ca7a47SHans de Goede 	if (!video_device)
165614ca7a47SHans de Goede 		return;
165714ca7a47SHans de Goede 
165814ca7a47SHans de Goede 	device = video_device->dev;
165914ca7a47SHans de Goede 	bus = video_device->video;
166014ca7a47SHans de Goede 	input = bus->input;
166114ca7a47SHans de Goede 
16624f7f9645SHans de Goede 	if (hw_changes_brightness > 0) {
16634f7f9645SHans de Goede 		if (video_device->backlight)
16644f7f9645SHans de Goede 			backlight_force_update(video_device->backlight,
16654f7f9645SHans de Goede 					       BACKLIGHT_UPDATE_HOTKEY);
16664f7f9645SHans de Goede 		acpi_notifier_call_chain(device, event, 0);
16674f7f9645SHans de Goede 		return;
16684f7f9645SHans de Goede 	}
16694f7f9645SHans de Goede 
167014ca7a47SHans de Goede 	switch (event) {
167114ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:	/* Cycle brightness */
167214ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
167314ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESS_CYCLE;
167414ca7a47SHans de Goede 		break;
167514ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:	/* Increase brightness */
167614ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
167714ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESSUP;
167814ca7a47SHans de Goede 		break;
167914ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:	/* Decrease brightness */
168014ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
168114ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESSDOWN;
168214ca7a47SHans de Goede 		break;
168314ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:	/* zero brightness */
168414ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
168514ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESS_ZERO;
168614ca7a47SHans de Goede 		break;
168714ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:	/* display device off */
168814ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
168914ca7a47SHans de Goede 		keycode = KEY_DISPLAY_OFF;
169014ca7a47SHans de Goede 		break;
169114ca7a47SHans de Goede 	default:
16922924d2f8SRafael J. Wysocki 		acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event);
169314ca7a47SHans de Goede 		break;
169414ca7a47SHans de Goede 	}
169514ca7a47SHans de Goede 
1696*5ad26161SHans de Goede 	if (keycode)
1697*5ad26161SHans de Goede 		may_report_brightness_keys = true;
1698*5ad26161SHans de Goede 
169914ca7a47SHans de Goede 	acpi_notifier_call_chain(device, event, 0);
170014ca7a47SHans de Goede 
170105bc59a0SHans de Goede 	if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
170214ca7a47SHans de Goede 		input_report_key(input, keycode, 1);
170314ca7a47SHans de Goede 		input_sync(input);
170414ca7a47SHans de Goede 		input_report_key(input, keycode, 0);
170514ca7a47SHans de Goede 		input_sync(input);
170614ca7a47SHans de Goede 	}
170714ca7a47SHans de Goede }
170814ca7a47SHans de Goede 
170914ca7a47SHans de Goede static int acpi_video_resume(struct notifier_block *nb,
171014ca7a47SHans de Goede 				unsigned long val, void *ign)
171114ca7a47SHans de Goede {
171214ca7a47SHans de Goede 	struct acpi_video_bus *video;
171314ca7a47SHans de Goede 	struct acpi_video_device *video_device;
171414ca7a47SHans de Goede 	int i;
171514ca7a47SHans de Goede 
171614ca7a47SHans de Goede 	switch (val) {
17171934fee6SZhang Rui 	case PM_POST_HIBERNATION:
17181934fee6SZhang Rui 	case PM_POST_SUSPEND:
17191934fee6SZhang Rui 	case PM_POST_RESTORE:
172014ca7a47SHans de Goede 		video = container_of(nb, struct acpi_video_bus, pm_nb);
172114ca7a47SHans de Goede 
172214ca7a47SHans de Goede 		dev_info(&video->device->dev, "Restoring backlight state\n");
172314ca7a47SHans de Goede 
172414ca7a47SHans de Goede 		for (i = 0; i < video->attached_count; i++) {
172514ca7a47SHans de Goede 			video_device = video->attached_array[i].bind_info;
172614ca7a47SHans de Goede 			if (video_device && video_device->brightness)
172714ca7a47SHans de Goede 				acpi_video_device_lcd_set_level(video_device,
172814ca7a47SHans de Goede 						video_device->brightness->curr);
172914ca7a47SHans de Goede 		}
173014ca7a47SHans de Goede 
173114ca7a47SHans de Goede 		return NOTIFY_OK;
173214ca7a47SHans de Goede 	}
17331934fee6SZhang Rui 	return NOTIFY_DONE;
17341934fee6SZhang Rui }
173514ca7a47SHans de Goede 
173614ca7a47SHans de Goede static acpi_status
173714ca7a47SHans de Goede acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
173814ca7a47SHans de Goede 			void **return_value)
173914ca7a47SHans de Goede {
174014ca7a47SHans de Goede 	struct acpi_device *device = context;
174114ca7a47SHans de Goede 	struct acpi_device *sibling;
174214ca7a47SHans de Goede 
174314ca7a47SHans de Goede 	if (handle == device->handle)
174414ca7a47SHans de Goede 		return AE_CTRL_TERMINATE;
174514ca7a47SHans de Goede 
174699ece713SRafael J. Wysocki 	sibling = acpi_fetch_acpi_dev(handle);
174799ece713SRafael J. Wysocki 	if (!sibling)
174814ca7a47SHans de Goede 		return AE_OK;
174914ca7a47SHans de Goede 
175014ca7a47SHans de Goede 	if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
175114ca7a47SHans de Goede 			return AE_ALREADY_EXISTS;
175214ca7a47SHans de Goede 
175314ca7a47SHans de Goede 	return AE_OK;
175414ca7a47SHans de Goede }
175514ca7a47SHans de Goede 
175614ca7a47SHans de Goede static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
175714ca7a47SHans de Goede {
175814ca7a47SHans de Goede 	struct backlight_properties props;
175914ca7a47SHans de Goede 	struct pci_dev *pdev;
176014ca7a47SHans de Goede 	acpi_handle acpi_parent;
176114ca7a47SHans de Goede 	struct device *parent = NULL;
176214ca7a47SHans de Goede 	int result;
176314ca7a47SHans de Goede 	static int count;
176414ca7a47SHans de Goede 	char *name;
176514ca7a47SHans de Goede 
176614ca7a47SHans de Goede 	result = acpi_video_init_brightness(device);
176714ca7a47SHans de Goede 	if (result)
176814ca7a47SHans de Goede 		return;
176914ca7a47SHans de Goede 
177014ca7a47SHans de Goede 	if (disable_backlight_sysfs_if > 0)
177114ca7a47SHans de Goede 		return;
177214ca7a47SHans de Goede 
177314ca7a47SHans de Goede 	name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
177414ca7a47SHans de Goede 	if (!name)
177514ca7a47SHans de Goede 		return;
177614ca7a47SHans de Goede 	count++;
177714ca7a47SHans de Goede 
177814ca7a47SHans de Goede 	acpi_get_parent(device->dev->handle, &acpi_parent);
177914ca7a47SHans de Goede 
178014ca7a47SHans de Goede 	pdev = acpi_get_pci_dev(acpi_parent);
178114ca7a47SHans de Goede 	if (pdev) {
178214ca7a47SHans de Goede 		parent = &pdev->dev;
178314ca7a47SHans de Goede 		pci_dev_put(pdev);
178414ca7a47SHans de Goede 	}
178514ca7a47SHans de Goede 
178614ca7a47SHans de Goede 	memset(&props, 0, sizeof(struct backlight_properties));
178714ca7a47SHans de Goede 	props.type = BACKLIGHT_FIRMWARE;
1788d485ef43SDmitry Frank 	props.max_brightness =
1789d485ef43SDmitry Frank 		device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
179014ca7a47SHans de Goede 	device->backlight = backlight_device_register(name,
179114ca7a47SHans de Goede 						      parent,
179214ca7a47SHans de Goede 						      device,
179314ca7a47SHans de Goede 						      &acpi_backlight_ops,
179414ca7a47SHans de Goede 						      &props);
179514ca7a47SHans de Goede 	kfree(name);
179614ca7a47SHans de Goede 	if (IS_ERR(device->backlight)) {
179714ca7a47SHans de Goede 		device->backlight = NULL;
179814ca7a47SHans de Goede 		return;
179914ca7a47SHans de Goede 	}
180014ca7a47SHans de Goede 
180114ca7a47SHans de Goede 	/*
180214ca7a47SHans de Goede 	 * Save current brightness level in case we have to restore it
180314ca7a47SHans de Goede 	 * before acpi_video_device_lcd_set_level() is called next time.
180414ca7a47SHans de Goede 	 */
180514ca7a47SHans de Goede 	device->backlight->props.brightness =
180614ca7a47SHans de Goede 			acpi_video_get_brightness(device->backlight);
180714ca7a47SHans de Goede 
180814ca7a47SHans de Goede 	device->cooling_dev = thermal_cooling_device_register("LCD",
180914ca7a47SHans de Goede 				device->dev, &video_cooling_ops);
181014ca7a47SHans de Goede 	if (IS_ERR(device->cooling_dev)) {
181114ca7a47SHans de Goede 		/*
181214ca7a47SHans de Goede 		 * Set cooling_dev to NULL so we don't crash trying to free it.
181314ca7a47SHans de Goede 		 * Also, why the hell we are returning early and not attempt to
181414ca7a47SHans de Goede 		 * register video output if cooling device registration failed?
181514ca7a47SHans de Goede 		 * -- dtor
181614ca7a47SHans de Goede 		 */
181714ca7a47SHans de Goede 		device->cooling_dev = NULL;
181814ca7a47SHans de Goede 		return;
181914ca7a47SHans de Goede 	}
182014ca7a47SHans de Goede 
182114ca7a47SHans de Goede 	dev_info(&device->dev->dev, "registered as cooling_device%d\n",
182214ca7a47SHans de Goede 		 device->cooling_dev->id);
182314ca7a47SHans de Goede 	result = sysfs_create_link(&device->dev->dev.kobj,
182414ca7a47SHans de Goede 			&device->cooling_dev->device.kobj,
182514ca7a47SHans de Goede 			"thermal_cooling");
182614ca7a47SHans de Goede 	if (result)
18272924d2f8SRafael J. Wysocki 		pr_info("sysfs link creation failed\n");
18282924d2f8SRafael J. Wysocki 
182914ca7a47SHans de Goede 	result = sysfs_create_link(&device->cooling_dev->device.kobj,
183014ca7a47SHans de Goede 			&device->dev->dev.kobj, "device");
183114ca7a47SHans de Goede 	if (result)
18322924d2f8SRafael J. Wysocki 		pr_info("Reverse sysfs link creation failed\n");
183314ca7a47SHans de Goede }
183414ca7a47SHans de Goede 
183514ca7a47SHans de Goede static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
183614ca7a47SHans de Goede {
183714ca7a47SHans de Goede 	struct acpi_video_device *dev;
183814ca7a47SHans de Goede 	union acpi_object *levels;
183914ca7a47SHans de Goede 
184014ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
184114ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry) {
18429f9cd7eeSAaron Lu 		if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
184314ca7a47SHans de Goede 			kfree(levels);
184414ca7a47SHans de Goede 	}
184514ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
184614ca7a47SHans de Goede }
184714ca7a47SHans de Goede 
1848e50b9be1SAaron Lu static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1849e50b9be1SAaron Lu {
1850e50b9be1SAaron Lu 	/*
1851e50b9be1SAaron Lu 	 * Do not create backlight device for video output
1852e50b9be1SAaron Lu 	 * device that is not in the enumerated list.
1853e50b9be1SAaron Lu 	 */
1854e50b9be1SAaron Lu 	if (!acpi_video_device_in_dod(dev)) {
1855e50b9be1SAaron Lu 		dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1856e50b9be1SAaron Lu 		return false;
1857e50b9be1SAaron Lu 	}
1858e50b9be1SAaron Lu 
1859e50b9be1SAaron Lu 	if (only_lcd)
1860e50b9be1SAaron Lu 		return dev->flags.lcd;
1861e50b9be1SAaron Lu 	return true;
1862e50b9be1SAaron Lu }
1863e50b9be1SAaron Lu 
186414ca7a47SHans de Goede static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
186514ca7a47SHans de Goede {
186614ca7a47SHans de Goede 	struct acpi_video_device *dev;
186714ca7a47SHans de Goede 
186814ca7a47SHans de Goede 	if (video->backlight_registered)
186914ca7a47SHans de Goede 		return 0;
187014ca7a47SHans de Goede 
187114ca7a47SHans de Goede 	acpi_video_run_bcl_for_osi(video);
187214ca7a47SHans de Goede 
18733bd6bce3SHans de Goede 	if (acpi_video_get_backlight_type() != acpi_backlight_video)
187414ca7a47SHans de Goede 		return 0;
187514ca7a47SHans de Goede 
187614ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
1877e50b9be1SAaron Lu 	list_for_each_entry(dev, &video->video_device_list, entry) {
1878e50b9be1SAaron Lu 		if (acpi_video_should_register_backlight(dev))
187914ca7a47SHans de Goede 			acpi_video_dev_register_backlight(dev);
1880e50b9be1SAaron Lu 	}
188114ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
188214ca7a47SHans de Goede 
188314ca7a47SHans de Goede 	video->backlight_registered = true;
188414ca7a47SHans de Goede 
188514ca7a47SHans de Goede 	video->pm_nb.notifier_call = acpi_video_resume;
188614ca7a47SHans de Goede 	video->pm_nb.priority = 0;
188714ca7a47SHans de Goede 	return register_pm_notifier(&video->pm_nb);
188814ca7a47SHans de Goede }
188914ca7a47SHans de Goede 
189014ca7a47SHans de Goede static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
189114ca7a47SHans de Goede {
189214ca7a47SHans de Goede 	if (device->backlight) {
189314ca7a47SHans de Goede 		backlight_device_unregister(device->backlight);
189414ca7a47SHans de Goede 		device->backlight = NULL;
189514ca7a47SHans de Goede 	}
189614ca7a47SHans de Goede 	if (device->brightness) {
189714ca7a47SHans de Goede 		kfree(device->brightness->levels);
189814ca7a47SHans de Goede 		kfree(device->brightness);
189914ca7a47SHans de Goede 		device->brightness = NULL;
190014ca7a47SHans de Goede 	}
190114ca7a47SHans de Goede 	if (device->cooling_dev) {
190214ca7a47SHans de Goede 		sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
190314ca7a47SHans de Goede 		sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
190414ca7a47SHans de Goede 		thermal_cooling_device_unregister(device->cooling_dev);
190514ca7a47SHans de Goede 		device->cooling_dev = NULL;
190614ca7a47SHans de Goede 	}
190714ca7a47SHans de Goede }
190814ca7a47SHans de Goede 
190914ca7a47SHans de Goede static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
191014ca7a47SHans de Goede {
191114ca7a47SHans de Goede 	struct acpi_video_device *dev;
191214ca7a47SHans de Goede 	int error;
191314ca7a47SHans de Goede 
191414ca7a47SHans de Goede 	if (!video->backlight_registered)
191514ca7a47SHans de Goede 		return 0;
191614ca7a47SHans de Goede 
191714ca7a47SHans de Goede 	error = unregister_pm_notifier(&video->pm_nb);
191814ca7a47SHans de Goede 
191914ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
192014ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
192114ca7a47SHans de Goede 		acpi_video_dev_unregister_backlight(dev);
192214ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
192314ca7a47SHans de Goede 
192414ca7a47SHans de Goede 	video->backlight_registered = false;
192514ca7a47SHans de Goede 
192614ca7a47SHans de Goede 	return error;
192714ca7a47SHans de Goede }
192814ca7a47SHans de Goede 
192914ca7a47SHans de Goede static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
193014ca7a47SHans de Goede {
193114ca7a47SHans de Goede 	acpi_status status;
193214ca7a47SHans de Goede 	struct acpi_device *adev = device->dev;
193314ca7a47SHans de Goede 
193414ca7a47SHans de Goede 	status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
193514ca7a47SHans de Goede 					     acpi_video_device_notify, device);
193614ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
193714ca7a47SHans de Goede 		dev_err(&adev->dev, "Error installing notify handler\n");
193814ca7a47SHans de Goede 	else
193914ca7a47SHans de Goede 		device->flags.notify = 1;
194014ca7a47SHans de Goede }
194114ca7a47SHans de Goede 
194214ca7a47SHans de Goede static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
194314ca7a47SHans de Goede {
194414ca7a47SHans de Goede 	struct input_dev *input;
194514ca7a47SHans de Goede 	struct acpi_video_device *dev;
194614ca7a47SHans de Goede 	int error;
194714ca7a47SHans de Goede 
194814ca7a47SHans de Goede 	video->input = input = input_allocate_device();
194914ca7a47SHans de Goede 	if (!input) {
195014ca7a47SHans de Goede 		error = -ENOMEM;
195114ca7a47SHans de Goede 		goto out;
195214ca7a47SHans de Goede 	}
195314ca7a47SHans de Goede 
195414ca7a47SHans de Goede 	error = acpi_video_bus_start_devices(video);
195514ca7a47SHans de Goede 	if (error)
195614ca7a47SHans de Goede 		goto err_free_input;
195714ca7a47SHans de Goede 
195814ca7a47SHans de Goede 	snprintf(video->phys, sizeof(video->phys),
195914ca7a47SHans de Goede 			"%s/video/input0", acpi_device_hid(video->device));
196014ca7a47SHans de Goede 
196114ca7a47SHans de Goede 	input->name = acpi_device_name(video->device);
196214ca7a47SHans de Goede 	input->phys = video->phys;
196314ca7a47SHans de Goede 	input->id.bustype = BUS_HOST;
196414ca7a47SHans de Goede 	input->id.product = 0x06;
196514ca7a47SHans de Goede 	input->dev.parent = &video->device->dev;
196614ca7a47SHans de Goede 	input->evbit[0] = BIT(EV_KEY);
196714ca7a47SHans de Goede 	set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
196814ca7a47SHans de Goede 	set_bit(KEY_VIDEO_NEXT, input->keybit);
196914ca7a47SHans de Goede 	set_bit(KEY_VIDEO_PREV, input->keybit);
197014ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
197114ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESSUP, input->keybit);
197214ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
197314ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
197414ca7a47SHans de Goede 	set_bit(KEY_DISPLAY_OFF, input->keybit);
197514ca7a47SHans de Goede 
197614ca7a47SHans de Goede 	error = input_register_device(input);
197714ca7a47SHans de Goede 	if (error)
197814ca7a47SHans de Goede 		goto err_stop_dev;
197914ca7a47SHans de Goede 
198014ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
198114ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
198214ca7a47SHans de Goede 		acpi_video_dev_add_notify_handler(dev);
198314ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
198414ca7a47SHans de Goede 
198514ca7a47SHans de Goede 	return 0;
198614ca7a47SHans de Goede 
198714ca7a47SHans de Goede err_stop_dev:
198814ca7a47SHans de Goede 	acpi_video_bus_stop_devices(video);
198914ca7a47SHans de Goede err_free_input:
199014ca7a47SHans de Goede 	input_free_device(input);
199114ca7a47SHans de Goede 	video->input = NULL;
199214ca7a47SHans de Goede out:
199314ca7a47SHans de Goede 	return error;
199414ca7a47SHans de Goede }
199514ca7a47SHans de Goede 
199614ca7a47SHans de Goede static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
199714ca7a47SHans de Goede {
199814ca7a47SHans de Goede 	if (dev->flags.notify) {
199914ca7a47SHans de Goede 		acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
200014ca7a47SHans de Goede 					   acpi_video_device_notify);
200114ca7a47SHans de Goede 		dev->flags.notify = 0;
200214ca7a47SHans de Goede 	}
200314ca7a47SHans de Goede }
200414ca7a47SHans de Goede 
200514ca7a47SHans de Goede static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
200614ca7a47SHans de Goede {
200714ca7a47SHans de Goede 	struct acpi_video_device *dev;
200814ca7a47SHans de Goede 
200914ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
201014ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
201114ca7a47SHans de Goede 		acpi_video_dev_remove_notify_handler(dev);
201214ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
201314ca7a47SHans de Goede 
201414ca7a47SHans de Goede 	acpi_video_bus_stop_devices(video);
201514ca7a47SHans de Goede 	input_unregister_device(video->input);
201614ca7a47SHans de Goede 	video->input = NULL;
201714ca7a47SHans de Goede }
201814ca7a47SHans de Goede 
201914ca7a47SHans de Goede static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
202014ca7a47SHans de Goede {
202114ca7a47SHans de Goede 	struct acpi_video_device *dev, *next;
202214ca7a47SHans de Goede 
202314ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
202414ca7a47SHans de Goede 	list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
202514ca7a47SHans de Goede 		list_del(&dev->entry);
202614ca7a47SHans de Goede 		kfree(dev);
202714ca7a47SHans de Goede 	}
202814ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
202914ca7a47SHans de Goede 
203014ca7a47SHans de Goede 	return 0;
203114ca7a47SHans de Goede }
203214ca7a47SHans de Goede 
203314ca7a47SHans de Goede static int instance;
203414ca7a47SHans de Goede 
203514ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device)
203614ca7a47SHans de Goede {
203714ca7a47SHans de Goede 	struct acpi_video_bus *video;
203814ca7a47SHans de Goede 	int error;
203914ca7a47SHans de Goede 	acpi_status status;
204014ca7a47SHans de Goede 
204114ca7a47SHans de Goede 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
204214ca7a47SHans de Goede 				device->parent->handle, 1,
204314ca7a47SHans de Goede 				acpi_video_bus_match, NULL,
204414ca7a47SHans de Goede 				device, NULL);
204514ca7a47SHans de Goede 	if (status == AE_ALREADY_EXISTS) {
20462924d2f8SRafael J. Wysocki 		pr_info(FW_BUG
204714ca7a47SHans de Goede 			"Duplicate ACPI video bus devices for the"
204814ca7a47SHans de Goede 			" same VGA controller, please try module "
204914ca7a47SHans de Goede 			"parameter \"video.allow_duplicates=1\""
205014ca7a47SHans de Goede 			"if the current driver doesn't work.\n");
205114ca7a47SHans de Goede 		if (!allow_duplicates)
205214ca7a47SHans de Goede 			return -ENODEV;
205314ca7a47SHans de Goede 	}
205414ca7a47SHans de Goede 
205514ca7a47SHans de Goede 	video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
205614ca7a47SHans de Goede 	if (!video)
205714ca7a47SHans de Goede 		return -ENOMEM;
205814ca7a47SHans de Goede 
205914ca7a47SHans de Goede 	/* a hack to fix the duplicate name "VID" problem on T61 */
206014ca7a47SHans de Goede 	if (!strcmp(device->pnp.bus_id, "VID")) {
206114ca7a47SHans de Goede 		if (instance)
206214ca7a47SHans de Goede 			device->pnp.bus_id[3] = '0' + instance;
206314ca7a47SHans de Goede 		instance++;
206414ca7a47SHans de Goede 	}
206514ca7a47SHans de Goede 	/* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
206614ca7a47SHans de Goede 	if (!strcmp(device->pnp.bus_id, "VGA")) {
206714ca7a47SHans de Goede 		if (instance)
206814ca7a47SHans de Goede 			device->pnp.bus_id[3] = '0' + instance;
206914ca7a47SHans de Goede 		instance++;
207014ca7a47SHans de Goede 	}
207114ca7a47SHans de Goede 
207214ca7a47SHans de Goede 	video->device = device;
207314ca7a47SHans de Goede 	strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
207414ca7a47SHans de Goede 	strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
207514ca7a47SHans de Goede 	device->driver_data = video;
207614ca7a47SHans de Goede 
207714ca7a47SHans de Goede 	acpi_video_bus_find_cap(video);
207814ca7a47SHans de Goede 	error = acpi_video_bus_check(video);
207914ca7a47SHans de Goede 	if (error)
208014ca7a47SHans de Goede 		goto err_free_video;
208114ca7a47SHans de Goede 
208214ca7a47SHans de Goede 	mutex_init(&video->device_list_lock);
208314ca7a47SHans de Goede 	INIT_LIST_HEAD(&video->video_device_list);
208414ca7a47SHans de Goede 
208514ca7a47SHans de Goede 	error = acpi_video_bus_get_devices(video, device);
208614ca7a47SHans de Goede 	if (error)
208714ca7a47SHans de Goede 		goto err_put_video;
208814ca7a47SHans de Goede 
20892924d2f8SRafael J. Wysocki 	pr_info("%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
209014ca7a47SHans de Goede 	       ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
209114ca7a47SHans de Goede 	       video->flags.multihead ? "yes" : "no",
209214ca7a47SHans de Goede 	       video->flags.rom ? "yes" : "no",
209314ca7a47SHans de Goede 	       video->flags.post ? "yes" : "no");
209414ca7a47SHans de Goede 	mutex_lock(&video_list_lock);
209514ca7a47SHans de Goede 	list_add_tail(&video->entry, &video_bus_head);
209614ca7a47SHans de Goede 	mutex_unlock(&video_list_lock);
209714ca7a47SHans de Goede 
209814ca7a47SHans de Goede 	acpi_video_bus_register_backlight(video);
209914ca7a47SHans de Goede 	acpi_video_bus_add_notify_handler(video);
210014ca7a47SHans de Goede 
210114ca7a47SHans de Goede 	return 0;
210214ca7a47SHans de Goede 
210314ca7a47SHans de Goede err_put_video:
210414ca7a47SHans de Goede 	acpi_video_bus_put_devices(video);
210514ca7a47SHans de Goede 	kfree(video->attached_array);
210614ca7a47SHans de Goede err_free_video:
210714ca7a47SHans de Goede 	kfree(video);
210814ca7a47SHans de Goede 	device->driver_data = NULL;
210914ca7a47SHans de Goede 
211014ca7a47SHans de Goede 	return error;
211114ca7a47SHans de Goede }
211214ca7a47SHans de Goede 
211314ca7a47SHans de Goede static int acpi_video_bus_remove(struct acpi_device *device)
211414ca7a47SHans de Goede {
211514ca7a47SHans de Goede 	struct acpi_video_bus *video = NULL;
211614ca7a47SHans de Goede 
211714ca7a47SHans de Goede 
211814ca7a47SHans de Goede 	if (!device || !acpi_driver_data(device))
211914ca7a47SHans de Goede 		return -EINVAL;
212014ca7a47SHans de Goede 
212114ca7a47SHans de Goede 	video = acpi_driver_data(device);
212214ca7a47SHans de Goede 
212314ca7a47SHans de Goede 	acpi_video_bus_remove_notify_handler(video);
212414ca7a47SHans de Goede 	acpi_video_bus_unregister_backlight(video);
212514ca7a47SHans de Goede 	acpi_video_bus_put_devices(video);
212614ca7a47SHans de Goede 
212714ca7a47SHans de Goede 	mutex_lock(&video_list_lock);
212814ca7a47SHans de Goede 	list_del(&video->entry);
212914ca7a47SHans de Goede 	mutex_unlock(&video_list_lock);
213014ca7a47SHans de Goede 
213114ca7a47SHans de Goede 	kfree(video->attached_array);
213214ca7a47SHans de Goede 	kfree(video);
213314ca7a47SHans de Goede 
213414ca7a47SHans de Goede 	return 0;
213514ca7a47SHans de Goede }
213614ca7a47SHans de Goede 
213714ca7a47SHans de Goede static int __init is_i740(struct pci_dev *dev)
213814ca7a47SHans de Goede {
213914ca7a47SHans de Goede 	if (dev->device == 0x00D1)
214014ca7a47SHans de Goede 		return 1;
214114ca7a47SHans de Goede 	if (dev->device == 0x7000)
214214ca7a47SHans de Goede 		return 1;
214314ca7a47SHans de Goede 	return 0;
214414ca7a47SHans de Goede }
214514ca7a47SHans de Goede 
214614ca7a47SHans de Goede static int __init intel_opregion_present(void)
214714ca7a47SHans de Goede {
214814ca7a47SHans de Goede 	int opregion = 0;
214914ca7a47SHans de Goede 	struct pci_dev *dev = NULL;
215014ca7a47SHans de Goede 	u32 address;
215114ca7a47SHans de Goede 
215214ca7a47SHans de Goede 	for_each_pci_dev(dev) {
215314ca7a47SHans de Goede 		if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
215414ca7a47SHans de Goede 			continue;
215514ca7a47SHans de Goede 		if (dev->vendor != PCI_VENDOR_ID_INTEL)
215614ca7a47SHans de Goede 			continue;
215714ca7a47SHans de Goede 		/* We don't want to poke around undefined i740 registers */
215814ca7a47SHans de Goede 		if (is_i740(dev))
215914ca7a47SHans de Goede 			continue;
216014ca7a47SHans de Goede 		pci_read_config_dword(dev, 0xfc, &address);
216114ca7a47SHans de Goede 		if (!address)
216214ca7a47SHans de Goede 			continue;
216314ca7a47SHans de Goede 		opregion = 1;
216414ca7a47SHans de Goede 	}
216514ca7a47SHans de Goede 	return opregion;
216614ca7a47SHans de Goede }
216714ca7a47SHans de Goede 
2168cecf3e3eSHans de Goede /* Check if the chassis-type indicates there is no builtin LCD panel */
216953fa1f6eSHans de Goede static bool dmi_is_desktop(void)
217053fa1f6eSHans de Goede {
217153fa1f6eSHans de Goede 	const char *chassis_type;
2172cecf3e3eSHans de Goede 	unsigned long type;
217353fa1f6eSHans de Goede 
217453fa1f6eSHans de Goede 	chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
217553fa1f6eSHans de Goede 	if (!chassis_type)
217653fa1f6eSHans de Goede 		return false;
217753fa1f6eSHans de Goede 
2178cecf3e3eSHans de Goede 	if (kstrtoul(chassis_type, 10, &type) != 0)
2179cecf3e3eSHans de Goede 		return false;
2180cecf3e3eSHans de Goede 
2181cecf3e3eSHans de Goede 	switch (type) {
2182cecf3e3eSHans de Goede 	case 0x03: /* Desktop */
2183cecf3e3eSHans de Goede 	case 0x04: /* Low Profile Desktop */
2184cecf3e3eSHans de Goede 	case 0x05: /* Pizza Box */
2185cecf3e3eSHans de Goede 	case 0x06: /* Mini Tower */
2186cecf3e3eSHans de Goede 	case 0x07: /* Tower */
2187d693c008SHans de Goede 	case 0x10: /* Lunch Box */
2188cecf3e3eSHans de Goede 	case 0x11: /* Main Server Chassis */
218953fa1f6eSHans de Goede 		return true;
2190cecf3e3eSHans de Goede 	}
219153fa1f6eSHans de Goede 
219253fa1f6eSHans de Goede 	return false;
219353fa1f6eSHans de Goede }
219453fa1f6eSHans de Goede 
219581cc7e99SHans de Goede /*
219681cc7e99SHans de Goede  * We're seeing a lot of bogus backlight interfaces on newer machines
219781cc7e99SHans de Goede  * without a LCD such as desktops, servers and HDMI sticks. Checking the
219881cc7e99SHans de Goede  * lcd flag fixes this, enable this by default on any machines which are:
219981cc7e99SHans de Goede  * 1.  Win8 ready (where we also prefer the native backlight driver, so
220081cc7e99SHans de Goede  *     normally the acpi_video code should not register there anyways); *and*
220181cc7e99SHans de Goede  * 2.1 Report a desktop/server DMI chassis-type, or
220281cc7e99SHans de Goede  * 2.2 Are an ACPI-reduced-hardware platform (and thus won't use the EC for
220381cc7e99SHans de Goede        backlight control)
220481cc7e99SHans de Goede  */
220581cc7e99SHans de Goede static bool should_check_lcd_flag(void)
220681cc7e99SHans de Goede {
220781cc7e99SHans de Goede 	if (!acpi_osi_is_win8())
220881cc7e99SHans de Goede 		return false;
220981cc7e99SHans de Goede 
221081cc7e99SHans de Goede 	if (dmi_is_desktop())
221181cc7e99SHans de Goede 		return true;
221281cc7e99SHans de Goede 
221381cc7e99SHans de Goede 	if (acpi_reduced_hardware())
221481cc7e99SHans de Goede 		return true;
221581cc7e99SHans de Goede 
221681cc7e99SHans de Goede 	return false;
221781cc7e99SHans de Goede }
221881cc7e99SHans de Goede 
221914ca7a47SHans de Goede int acpi_video_register(void)
222014ca7a47SHans de Goede {
22212a8b18e9SHans de Goede 	int ret = 0;
222214ca7a47SHans de Goede 
2223970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2224970530cdSHans de Goede 	if (register_count) {
222514ca7a47SHans de Goede 		/*
222614ca7a47SHans de Goede 		 * if the function of acpi_video_register is already called,
2227cbf6d033SKacper Piwiński 		 * don't register the acpi_video_bus again and return no error.
222814ca7a47SHans de Goede 		 */
22292a8b18e9SHans de Goede 		goto leave;
223014ca7a47SHans de Goede 	}
223114ca7a47SHans de Goede 
223281cc7e99SHans de Goede 	if (only_lcd == -1)
223381cc7e99SHans de Goede 		only_lcd = should_check_lcd_flag();
22345928c281SHans de Goede 
22357ee33baaSHans de Goede 	dmi_check_system(video_dmi_table);
22367ee33baaSHans de Goede 
223714ca7a47SHans de Goede 	ret = acpi_bus_register_driver(&acpi_video_bus);
223814ca7a47SHans de Goede 	if (ret)
22392a8b18e9SHans de Goede 		goto leave;
224014ca7a47SHans de Goede 
224114ca7a47SHans de Goede 	/*
224214ca7a47SHans de Goede 	 * When the acpi_video_bus is loaded successfully, increase
224314ca7a47SHans de Goede 	 * the counter reference.
224414ca7a47SHans de Goede 	 */
2245970530cdSHans de Goede 	register_count = 1;
224614ca7a47SHans de Goede 
22472a8b18e9SHans de Goede leave:
2248970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
22492a8b18e9SHans de Goede 	return ret;
225014ca7a47SHans de Goede }
225114ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_register);
225214ca7a47SHans de Goede 
225314ca7a47SHans de Goede void acpi_video_unregister(void)
225414ca7a47SHans de Goede {
2255970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2256970530cdSHans de Goede 	if (register_count) {
225714ca7a47SHans de Goede 		acpi_bus_unregister_driver(&acpi_video_bus);
2258970530cdSHans de Goede 		register_count = 0;
2259*5ad26161SHans de Goede 		may_report_brightness_keys = false;
22602a8b18e9SHans de Goede 	}
2261970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
226214ca7a47SHans de Goede }
226314ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_unregister);
226414ca7a47SHans de Goede 
226514ca7a47SHans de Goede void acpi_video_unregister_backlight(void)
226614ca7a47SHans de Goede {
226714ca7a47SHans de Goede 	struct acpi_video_bus *video;
226814ca7a47SHans de Goede 
2269970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2270970530cdSHans de Goede 	if (register_count) {
227114ca7a47SHans de Goede 		mutex_lock(&video_list_lock);
227214ca7a47SHans de Goede 		list_for_each_entry(video, &video_bus_head, entry)
227314ca7a47SHans de Goede 			acpi_video_bus_unregister_backlight(video);
227414ca7a47SHans de Goede 		mutex_unlock(&video_list_lock);
227514ca7a47SHans de Goede 	}
2276970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
22772a8b18e9SHans de Goede }
227814ca7a47SHans de Goede 
227990b066b1SHans de Goede bool acpi_video_handles_brightness_key_presses(void)
228090b066b1SHans de Goede {
2281*5ad26161SHans de Goede 	return may_report_brightness_keys &&
228205bc59a0SHans de Goede 	       (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
228390b066b1SHans de Goede }
228490b066b1SHans de Goede EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
228590b066b1SHans de Goede 
228614ca7a47SHans de Goede /*
228714ca7a47SHans de Goede  * This is kind of nasty. Hardware using Intel chipsets may require
228814ca7a47SHans de Goede  * the video opregion code to be run first in order to initialise
228914ca7a47SHans de Goede  * state before any ACPI video calls are made. To handle this we defer
229014ca7a47SHans de Goede  * registration of the video class until the opregion code has run.
229114ca7a47SHans de Goede  */
229214ca7a47SHans de Goede 
229314ca7a47SHans de Goede static int __init acpi_video_init(void)
229414ca7a47SHans de Goede {
229514ca7a47SHans de Goede 	/*
229614ca7a47SHans de Goede 	 * Let the module load even if ACPI is disabled (e.g. due to
229714ca7a47SHans de Goede 	 * a broken BIOS) so that i915.ko can still be loaded on such
229814ca7a47SHans de Goede 	 * old systems without an AcpiOpRegion.
229914ca7a47SHans de Goede 	 *
230014ca7a47SHans de Goede 	 * acpi_video_register() will report -ENODEV later as well due
230114ca7a47SHans de Goede 	 * to acpi_disabled when i915.ko tries to register itself afterwards.
230214ca7a47SHans de Goede 	 */
230314ca7a47SHans de Goede 	if (acpi_disabled)
230414ca7a47SHans de Goede 		return 0;
230514ca7a47SHans de Goede 
230614ca7a47SHans de Goede 	if (intel_opregion_present())
230714ca7a47SHans de Goede 		return 0;
230814ca7a47SHans de Goede 
230914ca7a47SHans de Goede 	return acpi_video_register();
231014ca7a47SHans de Goede }
231114ca7a47SHans de Goede 
231214ca7a47SHans de Goede static void __exit acpi_video_exit(void)
231314ca7a47SHans de Goede {
231493a291dfSHans de Goede 	acpi_video_detect_exit();
231514ca7a47SHans de Goede 	acpi_video_unregister();
231614ca7a47SHans de Goede }
231714ca7a47SHans de Goede 
231814ca7a47SHans de Goede module_init(acpi_video_init);
231914ca7a47SHans de Goede module_exit(acpi_video_exit);
2320