xref: /linux/drivers/acpi/acpi_video.c (revision 059500940defe285222d3b189b366dfe7f299cae)
114ca7a47SHans de Goede /*
214ca7a47SHans de Goede  *  video.c - ACPI Video Driver
314ca7a47SHans de Goede  *
414ca7a47SHans de Goede  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
514ca7a47SHans de Goede  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
614ca7a47SHans de Goede  *  Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
714ca7a47SHans de Goede  *
814ca7a47SHans de Goede  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
914ca7a47SHans de Goede  *
1014ca7a47SHans de Goede  *  This program is free software; you can redistribute it and/or modify
1114ca7a47SHans de Goede  *  it under the terms of the GNU General Public License as published by
1214ca7a47SHans de Goede  *  the Free Software Foundation; either version 2 of the License, or (at
1314ca7a47SHans de Goede  *  your option) any later version.
1414ca7a47SHans de Goede  *
1514ca7a47SHans de Goede  *  This program is distributed in the hope that it will be useful, but
1614ca7a47SHans de Goede  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1714ca7a47SHans de Goede  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1814ca7a47SHans de Goede  *  General Public License for more details.
1914ca7a47SHans de Goede  *
2014ca7a47SHans de Goede  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2114ca7a47SHans de Goede  */
2214ca7a47SHans de Goede 
2314ca7a47SHans de Goede #include <linux/kernel.h>
2414ca7a47SHans de Goede #include <linux/module.h>
2514ca7a47SHans de Goede #include <linux/init.h>
2614ca7a47SHans de Goede #include <linux/types.h>
2714ca7a47SHans de Goede #include <linux/list.h>
2814ca7a47SHans de Goede #include <linux/mutex.h>
2914ca7a47SHans de Goede #include <linux/input.h>
3014ca7a47SHans de Goede #include <linux/backlight.h>
3114ca7a47SHans de Goede #include <linux/thermal.h>
3214ca7a47SHans de Goede #include <linux/sort.h>
3314ca7a47SHans de Goede #include <linux/pci.h>
3414ca7a47SHans de Goede #include <linux/pci_ids.h>
3514ca7a47SHans de Goede #include <linux/slab.h>
3614ca7a47SHans de Goede #include <linux/dmi.h>
3714ca7a47SHans de Goede #include <linux/suspend.h>
3814ca7a47SHans de Goede #include <linux/acpi.h>
3914ca7a47SHans de Goede #include <acpi/video.h>
4014ca7a47SHans de Goede #include <asm/uaccess.h>
4114ca7a47SHans de Goede 
4214ca7a47SHans de Goede #define PREFIX "ACPI: "
4314ca7a47SHans de Goede 
4414ca7a47SHans de Goede #define ACPI_VIDEO_BUS_NAME		"Video Bus"
4514ca7a47SHans de Goede #define ACPI_VIDEO_DEVICE_NAME		"Video Device"
4614ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_SWITCH	0x80
4714ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_PROBE		0x81
4814ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_CYCLE		0x82
4914ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT	0x83
5014ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT	0x84
5114ca7a47SHans de Goede 
5214ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS	0x85
5314ca7a47SHans de Goede #define	ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS	0x86
5414ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS	0x87
5514ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS	0x88
5614ca7a47SHans de Goede #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF		0x89
5714ca7a47SHans de Goede 
5814ca7a47SHans de Goede #define MAX_NAME_LEN	20
5914ca7a47SHans de Goede 
6014ca7a47SHans de Goede #define _COMPONENT		ACPI_VIDEO_COMPONENT
6114ca7a47SHans de Goede ACPI_MODULE_NAME("video");
6214ca7a47SHans de Goede 
6314ca7a47SHans de Goede MODULE_AUTHOR("Bruno Ducrot");
6414ca7a47SHans de Goede MODULE_DESCRIPTION("ACPI Video Driver");
6514ca7a47SHans de Goede MODULE_LICENSE("GPL");
6614ca7a47SHans de Goede 
6714ca7a47SHans de Goede static bool brightness_switch_enabled = 1;
6814ca7a47SHans de Goede module_param(brightness_switch_enabled, bool, 0644);
6914ca7a47SHans de Goede 
7014ca7a47SHans de Goede /*
7114ca7a47SHans de Goede  * By default, we don't allow duplicate ACPI video bus devices
7214ca7a47SHans de Goede  * under the same VGA controller
7314ca7a47SHans de Goede  */
7414ca7a47SHans de Goede static bool allow_duplicates;
7514ca7a47SHans de Goede module_param(allow_duplicates, bool, 0644);
7614ca7a47SHans de Goede 
7714ca7a47SHans de Goede static int disable_backlight_sysfs_if = -1;
7814ca7a47SHans de Goede module_param(disable_backlight_sysfs_if, int, 0444);
7914ca7a47SHans de Goede 
8005bc59a0SHans de Goede #define REPORT_OUTPUT_KEY_EVENTS		0x01
8105bc59a0SHans de Goede #define REPORT_BRIGHTNESS_KEY_EVENTS		0x02
8205bc59a0SHans de Goede static int report_key_events = -1;
8305bc59a0SHans de Goede module_param(report_key_events, int, 0644);
8405bc59a0SHans de Goede MODULE_PARM_DESC(report_key_events,
8505bc59a0SHans de Goede 	"0: none, 1: output changes, 2: brightness changes, 3: all");
8605bc59a0SHans de Goede 
87e50b9be1SAaron Lu static bool device_id_scheme = false;
88e50b9be1SAaron Lu module_param(device_id_scheme, bool, 0444);
89e50b9be1SAaron Lu 
90e50b9be1SAaron Lu static bool only_lcd = false;
91e50b9be1SAaron Lu module_param(only_lcd, bool, 0444);
92e50b9be1SAaron Lu 
93970530cdSHans de Goede static int register_count;
94970530cdSHans de Goede static DEFINE_MUTEX(register_count_mutex);
9514e93553SHans de Goede static DEFINE_MUTEX(video_list_lock);
9614e93553SHans de Goede static LIST_HEAD(video_bus_head);
9714ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device);
9814ca7a47SHans de Goede static int acpi_video_bus_remove(struct acpi_device *device);
9914ca7a47SHans de Goede static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
10093a291dfSHans de Goede void acpi_video_detect_exit(void);
10114ca7a47SHans de Goede 
10214ca7a47SHans de Goede static const struct acpi_device_id video_device_ids[] = {
10314ca7a47SHans de Goede 	{ACPI_VIDEO_HID, 0},
10414ca7a47SHans de Goede 	{"", 0},
10514ca7a47SHans de Goede };
10614ca7a47SHans de Goede MODULE_DEVICE_TABLE(acpi, video_device_ids);
10714ca7a47SHans de Goede 
10814ca7a47SHans de Goede static struct acpi_driver acpi_video_bus = {
10914ca7a47SHans de Goede 	.name = "video",
11014ca7a47SHans de Goede 	.class = ACPI_VIDEO_CLASS,
11114ca7a47SHans de Goede 	.ids = video_device_ids,
11214ca7a47SHans de Goede 	.ops = {
11314ca7a47SHans de Goede 		.add = acpi_video_bus_add,
11414ca7a47SHans de Goede 		.remove = acpi_video_bus_remove,
11514ca7a47SHans de Goede 		.notify = acpi_video_bus_notify,
11614ca7a47SHans de Goede 		},
11714ca7a47SHans de Goede };
11814ca7a47SHans de Goede 
11914ca7a47SHans de Goede struct acpi_video_bus_flags {
12014ca7a47SHans de Goede 	u8 multihead:1;		/* can switch video heads */
12114ca7a47SHans de Goede 	u8 rom:1;		/* can retrieve a video rom */
12214ca7a47SHans de Goede 	u8 post:1;		/* can configure the head to */
12314ca7a47SHans de Goede 	u8 reserved:5;
12414ca7a47SHans de Goede };
12514ca7a47SHans de Goede 
12614ca7a47SHans de Goede struct acpi_video_bus_cap {
12714ca7a47SHans de Goede 	u8 _DOS:1;		/* Enable/Disable output switching */
12814ca7a47SHans de Goede 	u8 _DOD:1;		/* Enumerate all devices attached to display adapter */
12914ca7a47SHans de Goede 	u8 _ROM:1;		/* Get ROM Data */
13014ca7a47SHans de Goede 	u8 _GPD:1;		/* Get POST Device */
13114ca7a47SHans de Goede 	u8 _SPD:1;		/* Set POST Device */
13214ca7a47SHans de Goede 	u8 _VPO:1;		/* Video POST Options */
13314ca7a47SHans de Goede 	u8 reserved:2;
13414ca7a47SHans de Goede };
13514ca7a47SHans de Goede 
13614ca7a47SHans de Goede struct acpi_video_device_attrib {
13714ca7a47SHans de Goede 	u32 display_index:4;	/* A zero-based instance of the Display */
13814ca7a47SHans de Goede 	u32 display_port_attachment:4;	/* This field differentiates the display type */
13914ca7a47SHans de Goede 	u32 display_type:4;	/* Describe the specific type in use */
14014ca7a47SHans de Goede 	u32 vendor_specific:4;	/* Chipset Vendor Specific */
14114ca7a47SHans de Goede 	u32 bios_can_detect:1;	/* BIOS can detect the device */
14214ca7a47SHans de Goede 	u32 depend_on_vga:1;	/* Non-VGA output device whose power is related to
14314ca7a47SHans de Goede 				   the VGA device. */
14414ca7a47SHans de Goede 	u32 pipe_id:3;		/* For VGA multiple-head devices. */
14514ca7a47SHans de Goede 	u32 reserved:10;	/* Must be 0 */
14614ca7a47SHans de Goede 	u32 device_id_scheme:1;	/* Device ID Scheme */
14714ca7a47SHans de Goede };
14814ca7a47SHans de Goede 
14914ca7a47SHans de Goede struct acpi_video_enumerated_device {
15014ca7a47SHans de Goede 	union {
15114ca7a47SHans de Goede 		u32 int_val;
15214ca7a47SHans de Goede 		struct acpi_video_device_attrib attrib;
15314ca7a47SHans de Goede 	} value;
15414ca7a47SHans de Goede 	struct acpi_video_device *bind_info;
15514ca7a47SHans de Goede };
15614ca7a47SHans de Goede 
15714ca7a47SHans de Goede struct acpi_video_bus {
15814ca7a47SHans de Goede 	struct acpi_device *device;
15914ca7a47SHans de Goede 	bool backlight_registered;
16014ca7a47SHans de Goede 	u8 dos_setting;
16114ca7a47SHans de Goede 	struct acpi_video_enumerated_device *attached_array;
16214ca7a47SHans de Goede 	u8 attached_count;
16314ca7a47SHans de Goede 	u8 child_count;
16414ca7a47SHans de Goede 	struct acpi_video_bus_cap cap;
16514ca7a47SHans de Goede 	struct acpi_video_bus_flags flags;
16614ca7a47SHans de Goede 	struct list_head video_device_list;
16714ca7a47SHans de Goede 	struct mutex device_list_lock;	/* protects video_device_list */
16814ca7a47SHans de Goede 	struct list_head entry;
16914ca7a47SHans de Goede 	struct input_dev *input;
17014ca7a47SHans de Goede 	char phys[32];	/* for input device */
17114ca7a47SHans de Goede 	struct notifier_block pm_nb;
17214ca7a47SHans de Goede };
17314ca7a47SHans de Goede 
17414ca7a47SHans de Goede struct acpi_video_device_flags {
17514ca7a47SHans de Goede 	u8 crt:1;
17614ca7a47SHans de Goede 	u8 lcd:1;
17714ca7a47SHans de Goede 	u8 tvout:1;
17814ca7a47SHans de Goede 	u8 dvi:1;
17914ca7a47SHans de Goede 	u8 bios:1;
18014ca7a47SHans de Goede 	u8 unknown:1;
18114ca7a47SHans de Goede 	u8 notify:1;
18214ca7a47SHans de Goede 	u8 reserved:1;
18314ca7a47SHans de Goede };
18414ca7a47SHans de Goede 
18514ca7a47SHans de Goede struct acpi_video_device_cap {
18614ca7a47SHans de Goede 	u8 _ADR:1;		/* Return the unique ID */
18714ca7a47SHans de Goede 	u8 _BCL:1;		/* Query list of brightness control levels supported */
18814ca7a47SHans de Goede 	u8 _BCM:1;		/* Set the brightness level */
18914ca7a47SHans de Goede 	u8 _BQC:1;		/* Get current brightness level */
19014ca7a47SHans de Goede 	u8 _BCQ:1;		/* Some buggy BIOS uses _BCQ instead of _BQC */
19114ca7a47SHans de Goede 	u8 _DDC:1;		/* Return the EDID for this device */
19214ca7a47SHans de Goede };
19314ca7a47SHans de Goede 
19414ca7a47SHans de Goede struct acpi_video_device {
19514ca7a47SHans de Goede 	unsigned long device_id;
19614ca7a47SHans de Goede 	struct acpi_video_device_flags flags;
19714ca7a47SHans de Goede 	struct acpi_video_device_cap cap;
19814ca7a47SHans de Goede 	struct list_head entry;
19914ca7a47SHans de Goede 	struct delayed_work switch_brightness_work;
20014ca7a47SHans de Goede 	int switch_brightness_event;
20114ca7a47SHans de Goede 	struct acpi_video_bus *video;
20214ca7a47SHans de Goede 	struct acpi_device *dev;
20314ca7a47SHans de Goede 	struct acpi_video_device_brightness *brightness;
20414ca7a47SHans de Goede 	struct backlight_device *backlight;
20514ca7a47SHans de Goede 	struct thermal_cooling_device *cooling_dev;
20614ca7a47SHans de Goede };
20714ca7a47SHans de Goede 
20814ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
20914ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video);
21014ca7a47SHans de Goede static void acpi_video_device_bind(struct acpi_video_bus *video,
21114ca7a47SHans de Goede 				   struct acpi_video_device *device);
21214ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video);
21314ca7a47SHans de Goede static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
21414ca7a47SHans de Goede 			int level);
21514ca7a47SHans de Goede static int acpi_video_device_lcd_get_level_current(
21614ca7a47SHans de Goede 			struct acpi_video_device *device,
21714ca7a47SHans de Goede 			unsigned long long *level, bool raw);
21814ca7a47SHans de Goede static int acpi_video_get_next_level(struct acpi_video_device *device,
21914ca7a47SHans de Goede 				     u32 level_current, u32 event);
22014ca7a47SHans de Goede static void acpi_video_switch_brightness(struct work_struct *work);
22114ca7a47SHans de Goede 
22214ca7a47SHans de Goede /* backlight device sysfs support */
22314ca7a47SHans de Goede static int acpi_video_get_brightness(struct backlight_device *bd)
22414ca7a47SHans de Goede {
22514ca7a47SHans de Goede 	unsigned long long cur_level;
22614ca7a47SHans de Goede 	int i;
22714ca7a47SHans de Goede 	struct acpi_video_device *vd = bl_get_data(bd);
22814ca7a47SHans de Goede 
22914ca7a47SHans de Goede 	if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
23014ca7a47SHans de Goede 		return -EINVAL;
23114ca7a47SHans de Goede 	for (i = 2; i < vd->brightness->count; i++) {
23214ca7a47SHans de Goede 		if (vd->brightness->levels[i] == cur_level)
23314ca7a47SHans de Goede 			/*
23414ca7a47SHans de Goede 			 * The first two entries are special - see page 575
23514ca7a47SHans de Goede 			 * of the ACPI spec 3.0
23614ca7a47SHans de Goede 			 */
23714ca7a47SHans de Goede 			return i - 2;
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 {
24414ca7a47SHans de Goede 	int request_level = bd->props.brightness + 2;
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 */
25814ca7a47SHans de Goede static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
25914ca7a47SHans de Goede 			       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 
26414ca7a47SHans de Goede 	*state = video->brightness->count - 3;
26514ca7a47SHans de Goede 	return 0;
26614ca7a47SHans de Goede }
26714ca7a47SHans de Goede 
26814ca7a47SHans de Goede static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
26914ca7a47SHans de Goede 			       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;
27814ca7a47SHans de Goede 	for (offset = 2; offset < video->brightness->count; offset++)
27914ca7a47SHans de Goede 		if (level == video->brightness->levels[offset]) {
28014ca7a47SHans de Goede 			*state = video->brightness->count - offset - 1;
28114ca7a47SHans de Goede 			return 0;
28214ca7a47SHans de Goede 		}
28314ca7a47SHans de Goede 
28414ca7a47SHans de Goede 	return -EINVAL;
28514ca7a47SHans de Goede }
28614ca7a47SHans de Goede 
28714ca7a47SHans de Goede static int
28814ca7a47SHans de Goede video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
28914ca7a47SHans de Goede {
29014ca7a47SHans de Goede 	struct acpi_device *device = cooling_dev->devdata;
29114ca7a47SHans de Goede 	struct acpi_video_device *video = acpi_driver_data(device);
29214ca7a47SHans de Goede 	int level;
29314ca7a47SHans de Goede 
29414ca7a47SHans de Goede 	if (state >= video->brightness->count - 2)
29514ca7a47SHans de Goede 		return -EINVAL;
29614ca7a47SHans de Goede 
29714ca7a47SHans de Goede 	state = video->brightness->count - state;
29814ca7a47SHans de Goede 	level = video->brightness->levels[state - 1];
29914ca7a47SHans de Goede 	return acpi_video_device_lcd_set_level(video, level);
30014ca7a47SHans de Goede }
30114ca7a47SHans de Goede 
30214ca7a47SHans de Goede static const struct thermal_cooling_device_ops video_cooling_ops = {
30314ca7a47SHans de Goede 	.get_max_state = video_get_max_state,
30414ca7a47SHans de Goede 	.get_cur_state = video_get_cur_state,
30514ca7a47SHans de Goede 	.set_cur_state = video_set_cur_state,
30614ca7a47SHans de Goede };
30714ca7a47SHans de Goede 
30814ca7a47SHans de Goede /*
30914ca7a47SHans de Goede  * --------------------------------------------------------------------------
31014ca7a47SHans de Goede  *                             Video Management
31114ca7a47SHans de Goede  * --------------------------------------------------------------------------
31214ca7a47SHans de Goede  */
31314ca7a47SHans de Goede 
31414ca7a47SHans de Goede static int
315*05950094SAaron Lu acpi_video_device_lcd_query_levels(acpi_handle handle,
31614ca7a47SHans de Goede 				   union acpi_object **levels)
31714ca7a47SHans de Goede {
31814ca7a47SHans de Goede 	int status;
31914ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
32014ca7a47SHans de Goede 	union acpi_object *obj;
32114ca7a47SHans de Goede 
32214ca7a47SHans de Goede 
32314ca7a47SHans de Goede 	*levels = NULL;
32414ca7a47SHans de Goede 
325*05950094SAaron Lu 	status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
32614ca7a47SHans de Goede 	if (!ACPI_SUCCESS(status))
32714ca7a47SHans de Goede 		return status;
32814ca7a47SHans de Goede 	obj = (union acpi_object *)buffer.pointer;
32914ca7a47SHans de Goede 	if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
33014ca7a47SHans de Goede 		printk(KERN_ERR PREFIX "Invalid _BCL data\n");
33114ca7a47SHans de Goede 		status = -EFAULT;
33214ca7a47SHans de Goede 		goto err;
33314ca7a47SHans de Goede 	}
33414ca7a47SHans de Goede 
33514ca7a47SHans de Goede 	*levels = obj;
33614ca7a47SHans de Goede 
33714ca7a47SHans de Goede 	return 0;
33814ca7a47SHans de Goede 
33914ca7a47SHans de Goede err:
34014ca7a47SHans de Goede 	kfree(buffer.pointer);
34114ca7a47SHans de Goede 
34214ca7a47SHans de Goede 	return status;
34314ca7a47SHans de Goede }
34414ca7a47SHans de Goede 
34514ca7a47SHans de Goede static int
34614ca7a47SHans de Goede acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
34714ca7a47SHans de Goede {
34814ca7a47SHans de Goede 	int status;
34914ca7a47SHans de Goede 	int state;
35014ca7a47SHans de Goede 
35114ca7a47SHans de Goede 	status = acpi_execute_simple_method(device->dev->handle,
35214ca7a47SHans de Goede 					    "_BCM", level);
35314ca7a47SHans de Goede 	if (ACPI_FAILURE(status)) {
35414ca7a47SHans de Goede 		ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
35514ca7a47SHans de Goede 		return -EIO;
35614ca7a47SHans de Goede 	}
35714ca7a47SHans de Goede 
35814ca7a47SHans de Goede 	device->brightness->curr = level;
35914ca7a47SHans de Goede 	for (state = 2; state < device->brightness->count; state++)
36014ca7a47SHans de Goede 		if (level == device->brightness->levels[state]) {
36114ca7a47SHans de Goede 			if (device->backlight)
36214ca7a47SHans de Goede 				device->backlight->props.brightness = state - 2;
36314ca7a47SHans de Goede 			return 0;
36414ca7a47SHans de Goede 		}
36514ca7a47SHans de Goede 
36614ca7a47SHans de Goede 	ACPI_ERROR((AE_INFO, "Current brightness invalid"));
36714ca7a47SHans de Goede 	return -EINVAL;
36814ca7a47SHans de Goede }
36914ca7a47SHans de Goede 
37014ca7a47SHans de Goede /*
37114ca7a47SHans de Goede  * For some buggy _BQC methods, we need to add a constant value to
37214ca7a47SHans de Goede  * the _BQC return value to get the actual current brightness level
37314ca7a47SHans de Goede  */
37414ca7a47SHans de Goede 
37514ca7a47SHans de Goede static int bqc_offset_aml_bug_workaround;
3767ee33baaSHans de Goede static int video_set_bqc_offset(const struct dmi_system_id *d)
37714ca7a47SHans de Goede {
37814ca7a47SHans de Goede 	bqc_offset_aml_bug_workaround = 9;
37914ca7a47SHans de Goede 	return 0;
38014ca7a47SHans de Goede }
38114ca7a47SHans de Goede 
3827ee33baaSHans de Goede static int video_disable_backlight_sysfs_if(
38314ca7a47SHans de Goede 	const struct dmi_system_id *d)
38414ca7a47SHans de Goede {
38514ca7a47SHans de Goede 	if (disable_backlight_sysfs_if == -1)
38614ca7a47SHans de Goede 		disable_backlight_sysfs_if = 1;
38714ca7a47SHans de Goede 	return 0;
38814ca7a47SHans de Goede }
38914ca7a47SHans de Goede 
390e50b9be1SAaron Lu static int video_set_device_id_scheme(const struct dmi_system_id *d)
391e50b9be1SAaron Lu {
392e50b9be1SAaron Lu 	device_id_scheme = true;
393e50b9be1SAaron Lu 	return 0;
394e50b9be1SAaron Lu }
395e50b9be1SAaron Lu 
396e50b9be1SAaron Lu static int video_enable_only_lcd(const struct dmi_system_id *d)
397e50b9be1SAaron Lu {
398e50b9be1SAaron Lu 	only_lcd = true;
399e50b9be1SAaron Lu 	return 0;
400e50b9be1SAaron Lu }
401e50b9be1SAaron Lu 
4024b4b3b20SHans de Goede static int video_set_report_key_events(const struct dmi_system_id *id)
4034b4b3b20SHans de Goede {
4044b4b3b20SHans de Goede 	if (report_key_events == -1)
4054b4b3b20SHans de Goede 		report_key_events = (uintptr_t)id->driver_data;
4064b4b3b20SHans de Goede 	return 0;
4074b4b3b20SHans de Goede }
4084b4b3b20SHans de Goede 
4097ee33baaSHans de Goede static struct dmi_system_id video_dmi_table[] = {
41014ca7a47SHans de Goede 	/*
41114ca7a47SHans de Goede 	 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
41214ca7a47SHans de Goede 	 */
41314ca7a47SHans de Goede 	{
41414ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
41514ca7a47SHans de Goede 	 .ident = "Acer Aspire 5720",
41614ca7a47SHans de Goede 	 .matches = {
41714ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
41814ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
41914ca7a47SHans de Goede 		},
42014ca7a47SHans de Goede 	},
42114ca7a47SHans de Goede 	{
42214ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
42314ca7a47SHans de Goede 	 .ident = "Acer Aspire 5710Z",
42414ca7a47SHans de Goede 	 .matches = {
42514ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
42614ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
42714ca7a47SHans de Goede 		},
42814ca7a47SHans de Goede 	},
42914ca7a47SHans de Goede 	{
43014ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
43114ca7a47SHans de Goede 	 .ident = "eMachines E510",
43214ca7a47SHans de Goede 	 .matches = {
43314ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
43414ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
43514ca7a47SHans de Goede 		},
43614ca7a47SHans de Goede 	},
43714ca7a47SHans de Goede 	{
43814ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
43914ca7a47SHans de Goede 	 .ident = "Acer Aspire 5315",
44014ca7a47SHans de Goede 	 .matches = {
44114ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
44214ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
44314ca7a47SHans de Goede 		},
44414ca7a47SHans de Goede 	},
44514ca7a47SHans de Goede 	{
44614ca7a47SHans de Goede 	 .callback = video_set_bqc_offset,
44714ca7a47SHans de Goede 	 .ident = "Acer Aspire 7720",
44814ca7a47SHans de Goede 	 .matches = {
44914ca7a47SHans de Goede 		DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
45014ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
45114ca7a47SHans de Goede 		},
45214ca7a47SHans de Goede 	},
45314ca7a47SHans de Goede 
45414ca7a47SHans de Goede 	/*
45514ca7a47SHans de Goede 	 * Some machines have a broken acpi-video interface for brightness
45614ca7a47SHans de Goede 	 * control, but still need an acpi_video_device_lcd_set_level() call
45714ca7a47SHans de Goede 	 * on resume to turn the backlight power on.  We Enable backlight
45814ca7a47SHans de Goede 	 * control on these systems, but do not register a backlight sysfs
45914ca7a47SHans de Goede 	 * as brightness control does not work.
46014ca7a47SHans de Goede 	 */
46114ca7a47SHans de Goede 	{
462de588b8fSHans de Goede 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
463de588b8fSHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
464de588b8fSHans de Goede 	 .ident = "Toshiba Portege R700",
465de588b8fSHans de Goede 	 .matches = {
466de588b8fSHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
467de588b8fSHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R700"),
468de588b8fSHans de Goede 		},
469de588b8fSHans de Goede 	},
470de588b8fSHans de Goede 	{
47114ca7a47SHans de Goede 	 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
47214ca7a47SHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
47314ca7a47SHans de Goede 	 .ident = "Toshiba Portege R830",
47414ca7a47SHans de Goede 	 .matches = {
47514ca7a47SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
47614ca7a47SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
47714ca7a47SHans de Goede 		},
47814ca7a47SHans de Goede 	},
479b21f2e81SHans de Goede 	{
480b21f2e81SHans de Goede 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
481b21f2e81SHans de Goede 	 .callback = video_disable_backlight_sysfs_if,
482b21f2e81SHans de Goede 	 .ident = "Toshiba Satellite R830",
483b21f2e81SHans de Goede 	 .matches = {
484b21f2e81SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
485b21f2e81SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE R830"),
486b21f2e81SHans de Goede 		},
487b21f2e81SHans de Goede 	},
488e50b9be1SAaron Lu 	/*
489e50b9be1SAaron Lu 	 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
490e50b9be1SAaron Lu 	 * but the IDs actually follow the Device ID Scheme.
491e50b9be1SAaron Lu 	 */
492e50b9be1SAaron Lu 	{
493e50b9be1SAaron Lu 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
494e50b9be1SAaron Lu 	 .callback = video_set_device_id_scheme,
495e50b9be1SAaron Lu 	 .ident = "ESPRIMO Mobile M9410",
496e50b9be1SAaron Lu 	 .matches = {
497e50b9be1SAaron Lu 		DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
498e50b9be1SAaron Lu 		DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
499e50b9be1SAaron Lu 		},
500e50b9be1SAaron Lu 	},
501e50b9be1SAaron Lu 	/*
502e50b9be1SAaron Lu 	 * Some machines have multiple video output devices, but only the one
503e50b9be1SAaron Lu 	 * that is the type of LCD can do the backlight control so we should not
504e50b9be1SAaron Lu 	 * register backlight interface for other video output devices.
505e50b9be1SAaron Lu 	 */
506e50b9be1SAaron Lu 	{
507e50b9be1SAaron Lu 	 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
508e50b9be1SAaron Lu 	 .callback = video_enable_only_lcd,
509e50b9be1SAaron Lu 	 .ident = "ESPRIMO Mobile M9410",
510e50b9be1SAaron Lu 	 .matches = {
511e50b9be1SAaron Lu 		DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
512e50b9be1SAaron Lu 		DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
513e50b9be1SAaron Lu 		},
514e50b9be1SAaron Lu 	},
5154b4b3b20SHans de Goede 	/*
5164b4b3b20SHans de Goede 	 * Some machines report wrong key events on the acpi-bus, suppress
5174b4b3b20SHans de Goede 	 * key event reporting on these.  Note this is only intended to work
5184b4b3b20SHans de Goede 	 * around events which are plain wrong. In some cases we get double
5194b4b3b20SHans de Goede 	 * events, in this case acpi-video is considered the canonical source
5204b4b3b20SHans de Goede 	 * and the events from the other source should be filtered. E.g.
5214b4b3b20SHans de Goede 	 * by calling acpi_video_handles_brightness_key_presses() from the
5224b4b3b20SHans de Goede 	 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
5234b4b3b20SHans de Goede 	 */
5244b4b3b20SHans de Goede 	{
5254b4b3b20SHans de Goede 	 .callback = video_set_report_key_events,
5264b4b3b20SHans de Goede 	 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
5274b4b3b20SHans de Goede 	 .ident = "Dell Vostro V131",
5284b4b3b20SHans de Goede 	 .matches = {
5294b4b3b20SHans de Goede 		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
5304b4b3b20SHans de Goede 		DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
5314b4b3b20SHans de Goede 		},
5324b4b3b20SHans de Goede 	},
53314ca7a47SHans de Goede 	{}
53414ca7a47SHans de Goede };
53514ca7a47SHans de Goede 
53614ca7a47SHans de Goede static unsigned long long
53714ca7a47SHans de Goede acpi_video_bqc_value_to_level(struct acpi_video_device *device,
53814ca7a47SHans de Goede 			      unsigned long long bqc_value)
53914ca7a47SHans de Goede {
54014ca7a47SHans de Goede 	unsigned long long level;
54114ca7a47SHans de Goede 
54214ca7a47SHans de Goede 	if (device->brightness->flags._BQC_use_index) {
54314ca7a47SHans de Goede 		/*
54414ca7a47SHans de Goede 		 * _BQC returns an index that doesn't account for
54514ca7a47SHans de Goede 		 * the first 2 items with special meaning, so we need
54614ca7a47SHans de Goede 		 * to compensate for that by offsetting ourselves
54714ca7a47SHans de Goede 		 */
54814ca7a47SHans de Goede 		if (device->brightness->flags._BCL_reversed)
54914ca7a47SHans de Goede 			bqc_value = device->brightness->count - 3 - bqc_value;
55014ca7a47SHans de Goede 
55114ca7a47SHans de Goede 		level = device->brightness->levels[bqc_value + 2];
55214ca7a47SHans de Goede 	} else {
55314ca7a47SHans de Goede 		level = bqc_value;
55414ca7a47SHans de Goede 	}
55514ca7a47SHans de Goede 
55614ca7a47SHans de Goede 	level += bqc_offset_aml_bug_workaround;
55714ca7a47SHans de Goede 
55814ca7a47SHans de Goede 	return level;
55914ca7a47SHans de Goede }
56014ca7a47SHans de Goede 
56114ca7a47SHans de Goede static int
56214ca7a47SHans de Goede acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
56314ca7a47SHans de Goede 					unsigned long long *level, bool raw)
56414ca7a47SHans de Goede {
56514ca7a47SHans de Goede 	acpi_status status = AE_OK;
56614ca7a47SHans de Goede 	int i;
56714ca7a47SHans de Goede 
56814ca7a47SHans de Goede 	if (device->cap._BQC || device->cap._BCQ) {
56914ca7a47SHans de Goede 		char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
57014ca7a47SHans de Goede 
57114ca7a47SHans de Goede 		status = acpi_evaluate_integer(device->dev->handle, buf,
57214ca7a47SHans de Goede 						NULL, level);
57314ca7a47SHans de Goede 		if (ACPI_SUCCESS(status)) {
57414ca7a47SHans de Goede 			if (raw) {
57514ca7a47SHans de Goede 				/*
57614ca7a47SHans de Goede 				 * Caller has indicated he wants the raw
57714ca7a47SHans de Goede 				 * value returned by _BQC, so don't furtherly
57814ca7a47SHans de Goede 				 * mess with the value.
57914ca7a47SHans de Goede 				 */
58014ca7a47SHans de Goede 				return 0;
58114ca7a47SHans de Goede 			}
58214ca7a47SHans de Goede 
58314ca7a47SHans de Goede 			*level = acpi_video_bqc_value_to_level(device, *level);
58414ca7a47SHans de Goede 
58514ca7a47SHans de Goede 			for (i = 2; i < device->brightness->count; i++)
58614ca7a47SHans de Goede 				if (device->brightness->levels[i] == *level) {
58714ca7a47SHans de Goede 					device->brightness->curr = *level;
58814ca7a47SHans de Goede 					return 0;
58914ca7a47SHans de Goede 				}
59014ca7a47SHans de Goede 			/*
59114ca7a47SHans de Goede 			 * BQC returned an invalid level.
59214ca7a47SHans de Goede 			 * Stop using it.
59314ca7a47SHans de Goede 			 */
59414ca7a47SHans de Goede 			ACPI_WARNING((AE_INFO,
59514ca7a47SHans de Goede 				      "%s returned an invalid level",
59614ca7a47SHans de Goede 				      buf));
59714ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
59814ca7a47SHans de Goede 		} else {
59914ca7a47SHans de Goede 			/*
60014ca7a47SHans de Goede 			 * Fixme:
60114ca7a47SHans de Goede 			 * should we return an error or ignore this failure?
60214ca7a47SHans de Goede 			 * dev->brightness->curr is a cached value which stores
60314ca7a47SHans de Goede 			 * the correct current backlight level in most cases.
60414ca7a47SHans de Goede 			 * ACPI video backlight still works w/ buggy _BQC.
60514ca7a47SHans de Goede 			 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
60614ca7a47SHans de Goede 			 */
60714ca7a47SHans de Goede 			ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
60814ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
60914ca7a47SHans de Goede 		}
61014ca7a47SHans de Goede 	}
61114ca7a47SHans de Goede 
61214ca7a47SHans de Goede 	*level = device->brightness->curr;
61314ca7a47SHans de Goede 	return 0;
61414ca7a47SHans de Goede }
61514ca7a47SHans de Goede 
61614ca7a47SHans de Goede static int
61714ca7a47SHans de Goede acpi_video_device_EDID(struct acpi_video_device *device,
61814ca7a47SHans de Goede 		       union acpi_object **edid, ssize_t length)
61914ca7a47SHans de Goede {
62014ca7a47SHans de Goede 	int status;
62114ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
62214ca7a47SHans de Goede 	union acpi_object *obj;
62314ca7a47SHans de Goede 	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
62414ca7a47SHans de Goede 	struct acpi_object_list args = { 1, &arg0 };
62514ca7a47SHans de Goede 
62614ca7a47SHans de Goede 
62714ca7a47SHans de Goede 	*edid = NULL;
62814ca7a47SHans de Goede 
62914ca7a47SHans de Goede 	if (!device)
63014ca7a47SHans de Goede 		return -ENODEV;
63114ca7a47SHans de Goede 	if (length == 128)
63214ca7a47SHans de Goede 		arg0.integer.value = 1;
63314ca7a47SHans de Goede 	else if (length == 256)
63414ca7a47SHans de Goede 		arg0.integer.value = 2;
63514ca7a47SHans de Goede 	else
63614ca7a47SHans de Goede 		return -EINVAL;
63714ca7a47SHans de Goede 
63814ca7a47SHans de Goede 	status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
63914ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
64014ca7a47SHans de Goede 		return -ENODEV;
64114ca7a47SHans de Goede 
64214ca7a47SHans de Goede 	obj = buffer.pointer;
64314ca7a47SHans de Goede 
64414ca7a47SHans de Goede 	if (obj && obj->type == ACPI_TYPE_BUFFER)
64514ca7a47SHans de Goede 		*edid = obj;
64614ca7a47SHans de Goede 	else {
64714ca7a47SHans de Goede 		printk(KERN_ERR PREFIX "Invalid _DDC data\n");
64814ca7a47SHans de Goede 		status = -EFAULT;
64914ca7a47SHans de Goede 		kfree(obj);
65014ca7a47SHans de Goede 	}
65114ca7a47SHans de Goede 
65214ca7a47SHans de Goede 	return status;
65314ca7a47SHans de Goede }
65414ca7a47SHans de Goede 
65514ca7a47SHans de Goede /* bus */
65614ca7a47SHans de Goede 
65714ca7a47SHans de Goede /*
65814ca7a47SHans de Goede  *  Arg:
65914ca7a47SHans de Goede  *	video		: video bus device pointer
66014ca7a47SHans de Goede  *	bios_flag	:
66114ca7a47SHans de Goede  *		0.	The system BIOS should NOT automatically switch(toggle)
66214ca7a47SHans de Goede  *			the active display output.
66314ca7a47SHans de Goede  *		1.	The system BIOS should automatically switch (toggle) the
66414ca7a47SHans de Goede  *			active display output. No switch event.
66514ca7a47SHans de Goede  *		2.	The _DGS value should be locked.
66614ca7a47SHans de Goede  *		3.	The system BIOS should not automatically switch (toggle) the
66714ca7a47SHans de Goede  *			active display output, but instead generate the display switch
66814ca7a47SHans de Goede  *			event notify code.
66914ca7a47SHans de Goede  *	lcd_flag	:
67014ca7a47SHans de Goede  *		0.	The system BIOS should automatically control the brightness level
67114ca7a47SHans de Goede  *			of the LCD when the power changes from AC to DC
67214ca7a47SHans de Goede  *		1.	The system BIOS should NOT automatically control the brightness
67314ca7a47SHans de Goede  *			level of the LCD when the power changes from AC to DC.
67414ca7a47SHans de Goede  *  Return Value:
67514ca7a47SHans de Goede  *		-EINVAL	wrong arg.
67614ca7a47SHans de Goede  */
67714ca7a47SHans de Goede 
67814ca7a47SHans de Goede static int
67914ca7a47SHans de Goede acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
68014ca7a47SHans de Goede {
68114ca7a47SHans de Goede 	acpi_status status;
68214ca7a47SHans de Goede 
68314ca7a47SHans de Goede 	if (!video->cap._DOS)
68414ca7a47SHans de Goede 		return 0;
68514ca7a47SHans de Goede 
68614ca7a47SHans de Goede 	if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
68714ca7a47SHans de Goede 		return -EINVAL;
68814ca7a47SHans de Goede 	video->dos_setting = (lcd_flag << 2) | bios_flag;
68914ca7a47SHans de Goede 	status = acpi_execute_simple_method(video->device->handle, "_DOS",
69014ca7a47SHans de Goede 					    (lcd_flag << 2) | bios_flag);
69114ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
69214ca7a47SHans de Goede 		return -EIO;
69314ca7a47SHans de Goede 
69414ca7a47SHans de Goede 	return 0;
69514ca7a47SHans de Goede }
69614ca7a47SHans de Goede 
69714ca7a47SHans de Goede /*
69814ca7a47SHans de Goede  * Simple comparison function used to sort backlight levels.
69914ca7a47SHans de Goede  */
70014ca7a47SHans de Goede 
70114ca7a47SHans de Goede static int
70214ca7a47SHans de Goede acpi_video_cmp_level(const void *a, const void *b)
70314ca7a47SHans de Goede {
70414ca7a47SHans de Goede 	return *(int *)a - *(int *)b;
70514ca7a47SHans de Goede }
70614ca7a47SHans de Goede 
70714ca7a47SHans de Goede /*
70814ca7a47SHans de Goede  * Decides if _BQC/_BCQ for this system is usable
70914ca7a47SHans de Goede  *
71014ca7a47SHans de Goede  * We do this by changing the level first and then read out the current
71114ca7a47SHans de Goede  * brightness level, if the value does not match, find out if it is using
71214ca7a47SHans de Goede  * index. If not, clear the _BQC/_BCQ capability.
71314ca7a47SHans de Goede  */
71414ca7a47SHans de Goede static int acpi_video_bqc_quirk(struct acpi_video_device *device,
71514ca7a47SHans de Goede 				int max_level, int current_level)
71614ca7a47SHans de Goede {
71714ca7a47SHans de Goede 	struct acpi_video_device_brightness *br = device->brightness;
71814ca7a47SHans de Goede 	int result;
71914ca7a47SHans de Goede 	unsigned long long level;
72014ca7a47SHans de Goede 	int test_level;
72114ca7a47SHans de Goede 
72214ca7a47SHans de Goede 	/* don't mess with existing known broken systems */
72314ca7a47SHans de Goede 	if (bqc_offset_aml_bug_workaround)
72414ca7a47SHans de Goede 		return 0;
72514ca7a47SHans de Goede 
72614ca7a47SHans de Goede 	/*
72714ca7a47SHans de Goede 	 * Some systems always report current brightness level as maximum
72814ca7a47SHans de Goede 	 * through _BQC, we need to test another value for them.
72914ca7a47SHans de Goede 	 */
73014ca7a47SHans de Goede 	test_level = current_level == max_level ? br->levels[3] : max_level;
73114ca7a47SHans de Goede 
73214ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, test_level);
73314ca7a47SHans de Goede 	if (result)
73414ca7a47SHans de Goede 		return result;
73514ca7a47SHans de Goede 
73614ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device, &level, true);
73714ca7a47SHans de Goede 	if (result)
73814ca7a47SHans de Goede 		return result;
73914ca7a47SHans de Goede 
74014ca7a47SHans de Goede 	if (level != test_level) {
74114ca7a47SHans de Goede 		/* buggy _BQC found, need to find out if it uses index */
74214ca7a47SHans de Goede 		if (level < br->count) {
74314ca7a47SHans de Goede 			if (br->flags._BCL_reversed)
74414ca7a47SHans de Goede 				level = br->count - 3 - level;
74514ca7a47SHans de Goede 			if (br->levels[level + 2] == test_level)
74614ca7a47SHans de Goede 				br->flags._BQC_use_index = 1;
74714ca7a47SHans de Goede 		}
74814ca7a47SHans de Goede 
74914ca7a47SHans de Goede 		if (!br->flags._BQC_use_index)
75014ca7a47SHans de Goede 			device->cap._BQC = device->cap._BCQ = 0;
75114ca7a47SHans de Goede 	}
75214ca7a47SHans de Goede 
75314ca7a47SHans de Goede 	return 0;
75414ca7a47SHans de Goede }
75514ca7a47SHans de Goede 
756*05950094SAaron Lu int acpi_video_get_levels(struct acpi_device *device,
757*05950094SAaron Lu 			  struct acpi_video_device_brightness **dev_br)
75814ca7a47SHans de Goede {
75914ca7a47SHans de Goede 	union acpi_object *obj = NULL;
76014ca7a47SHans de Goede 	int i, max_level = 0, count = 0, level_ac_battery = 0;
76114ca7a47SHans de Goede 	union acpi_object *o;
76214ca7a47SHans de Goede 	struct acpi_video_device_brightness *br = NULL;
763*05950094SAaron Lu 	int result = 0;
76414ca7a47SHans de Goede 	u32 value;
76514ca7a47SHans de Goede 
766*05950094SAaron Lu 	if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device->handle,
767*05950094SAaron Lu 								&obj))) {
76814ca7a47SHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
76914ca7a47SHans de Goede 						"LCD brightness level\n"));
770*05950094SAaron Lu 		result = -ENODEV;
77114ca7a47SHans de Goede 		goto out;
77214ca7a47SHans de Goede 	}
77314ca7a47SHans de Goede 
774*05950094SAaron Lu 	if (obj->package.count < 2) {
775*05950094SAaron Lu 		result = -EINVAL;
77614ca7a47SHans de Goede 		goto out;
777*05950094SAaron Lu 	}
77814ca7a47SHans de Goede 
77914ca7a47SHans de Goede 	br = kzalloc(sizeof(*br), GFP_KERNEL);
78014ca7a47SHans de Goede 	if (!br) {
78114ca7a47SHans de Goede 		printk(KERN_ERR "can't allocate memory\n");
78214ca7a47SHans de Goede 		result = -ENOMEM;
78314ca7a47SHans de Goede 		goto out;
78414ca7a47SHans de Goede 	}
78514ca7a47SHans de Goede 
78614ca7a47SHans de Goede 	br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
78714ca7a47SHans de Goede 				GFP_KERNEL);
78814ca7a47SHans de Goede 	if (!br->levels) {
78914ca7a47SHans de Goede 		result = -ENOMEM;
79014ca7a47SHans de Goede 		goto out_free;
79114ca7a47SHans de Goede 	}
79214ca7a47SHans de Goede 
79314ca7a47SHans de Goede 	for (i = 0; i < obj->package.count; i++) {
79414ca7a47SHans de Goede 		o = (union acpi_object *)&obj->package.elements[i];
79514ca7a47SHans de Goede 		if (o->type != ACPI_TYPE_INTEGER) {
79614ca7a47SHans de Goede 			printk(KERN_ERR PREFIX "Invalid data\n");
79714ca7a47SHans de Goede 			continue;
79814ca7a47SHans de Goede 		}
79914ca7a47SHans de Goede 		value = (u32) o->integer.value;
80014ca7a47SHans de Goede 		/* Skip duplicate entries */
80114ca7a47SHans de Goede 		if (count > 2 && br->levels[count - 1] == value)
80214ca7a47SHans de Goede 			continue;
80314ca7a47SHans de Goede 
80414ca7a47SHans de Goede 		br->levels[count] = value;
80514ca7a47SHans de Goede 
80614ca7a47SHans de Goede 		if (br->levels[count] > max_level)
80714ca7a47SHans de Goede 			max_level = br->levels[count];
80814ca7a47SHans de Goede 		count++;
80914ca7a47SHans de Goede 	}
81014ca7a47SHans de Goede 
81114ca7a47SHans de Goede 	/*
81214ca7a47SHans de Goede 	 * some buggy BIOS don't export the levels
81314ca7a47SHans de Goede 	 * when machine is on AC/Battery in _BCL package.
81414ca7a47SHans de Goede 	 * In this case, the first two elements in _BCL packages
81514ca7a47SHans de Goede 	 * are also supported brightness levels that OS should take care of.
81614ca7a47SHans de Goede 	 */
81714ca7a47SHans de Goede 	for (i = 2; i < count; i++) {
81814ca7a47SHans de Goede 		if (br->levels[i] == br->levels[0])
81914ca7a47SHans de Goede 			level_ac_battery++;
82014ca7a47SHans de Goede 		if (br->levels[i] == br->levels[1])
82114ca7a47SHans de Goede 			level_ac_battery++;
82214ca7a47SHans de Goede 	}
82314ca7a47SHans de Goede 
82414ca7a47SHans de Goede 	if (level_ac_battery < 2) {
82514ca7a47SHans de Goede 		level_ac_battery = 2 - level_ac_battery;
82614ca7a47SHans de Goede 		br->flags._BCL_no_ac_battery_levels = 1;
82714ca7a47SHans de Goede 		for (i = (count - 1 + level_ac_battery); i >= 2; i--)
82814ca7a47SHans de Goede 			br->levels[i] = br->levels[i - level_ac_battery];
82914ca7a47SHans de Goede 		count += level_ac_battery;
83014ca7a47SHans de Goede 	} else if (level_ac_battery > 2)
83114ca7a47SHans de Goede 		ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
83214ca7a47SHans de Goede 
83314ca7a47SHans de Goede 	/* Check if the _BCL package is in a reversed order */
83414ca7a47SHans de Goede 	if (max_level == br->levels[2]) {
83514ca7a47SHans de Goede 		br->flags._BCL_reversed = 1;
83614ca7a47SHans de Goede 		sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
83714ca7a47SHans de Goede 			acpi_video_cmp_level, NULL);
83814ca7a47SHans de Goede 	} else if (max_level != br->levels[count - 1])
83914ca7a47SHans de Goede 		ACPI_ERROR((AE_INFO,
84014ca7a47SHans de Goede 			    "Found unordered _BCL package"));
84114ca7a47SHans de Goede 
84214ca7a47SHans de Goede 	br->count = count;
843*05950094SAaron Lu 	*dev_br = br;
844*05950094SAaron Lu 
845*05950094SAaron Lu out:
846*05950094SAaron Lu 	kfree(obj);
847*05950094SAaron Lu 	return result;
848*05950094SAaron Lu out_free:
849*05950094SAaron Lu 	kfree(br);
850*05950094SAaron Lu 	goto out;
851*05950094SAaron Lu }
852*05950094SAaron Lu EXPORT_SYMBOL(acpi_video_get_levels);
853*05950094SAaron Lu 
854*05950094SAaron Lu /*
855*05950094SAaron Lu  *  Arg:
856*05950094SAaron Lu  *	device	: video output device (LCD, CRT, ..)
857*05950094SAaron Lu  *
858*05950094SAaron Lu  *  Return Value:
859*05950094SAaron Lu  *	Maximum brightness level
860*05950094SAaron Lu  *
861*05950094SAaron Lu  *  Allocate and initialize device->brightness.
862*05950094SAaron Lu  */
863*05950094SAaron Lu 
864*05950094SAaron Lu static int
865*05950094SAaron Lu acpi_video_init_brightness(struct acpi_video_device *device)
866*05950094SAaron Lu {
867*05950094SAaron Lu 	int i, max_level = 0;
868*05950094SAaron Lu 	unsigned long long level, level_old;
869*05950094SAaron Lu 	struct acpi_video_device_brightness *br = NULL;
870*05950094SAaron Lu 	int result = -EINVAL;
871*05950094SAaron Lu 
872*05950094SAaron Lu 	result = acpi_video_get_levels(device->dev, &br);
873*05950094SAaron Lu 	if (result)
874*05950094SAaron Lu 		return result;
87514ca7a47SHans de Goede 	device->brightness = br;
87614ca7a47SHans de Goede 
87714ca7a47SHans de Goede 	/* _BQC uses INDEX while _BCL uses VALUE in some laptops */
87814ca7a47SHans de Goede 	br->curr = level = max_level;
87914ca7a47SHans de Goede 
88014ca7a47SHans de Goede 	if (!device->cap._BQC)
88114ca7a47SHans de Goede 		goto set_level;
88214ca7a47SHans de Goede 
88314ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device,
88414ca7a47SHans de Goede 							 &level_old, true);
88514ca7a47SHans de Goede 	if (result)
88614ca7a47SHans de Goede 		goto out_free_levels;
88714ca7a47SHans de Goede 
88814ca7a47SHans de Goede 	result = acpi_video_bqc_quirk(device, max_level, level_old);
88914ca7a47SHans de Goede 	if (result)
89014ca7a47SHans de Goede 		goto out_free_levels;
89114ca7a47SHans de Goede 	/*
89214ca7a47SHans de Goede 	 * cap._BQC may get cleared due to _BQC is found to be broken
89314ca7a47SHans de Goede 	 * in acpi_video_bqc_quirk, so check again here.
89414ca7a47SHans de Goede 	 */
89514ca7a47SHans de Goede 	if (!device->cap._BQC)
89614ca7a47SHans de Goede 		goto set_level;
89714ca7a47SHans de Goede 
89814ca7a47SHans de Goede 	level = acpi_video_bqc_value_to_level(device, level_old);
89914ca7a47SHans de Goede 	/*
90014ca7a47SHans de Goede 	 * On some buggy laptops, _BQC returns an uninitialized
90114ca7a47SHans de Goede 	 * value when invoked for the first time, i.e.
90214ca7a47SHans de Goede 	 * level_old is invalid (no matter whether it's a level
90314ca7a47SHans de Goede 	 * or an index). Set the backlight to max_level in this case.
90414ca7a47SHans de Goede 	 */
90514ca7a47SHans de Goede 	for (i = 2; i < br->count; i++)
90614ca7a47SHans de Goede 		if (level == br->levels[i])
90714ca7a47SHans de Goede 			break;
90814ca7a47SHans de Goede 	if (i == br->count || !level)
90914ca7a47SHans de Goede 		level = max_level;
91014ca7a47SHans de Goede 
91114ca7a47SHans de Goede set_level:
91214ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, level);
91314ca7a47SHans de Goede 	if (result)
91414ca7a47SHans de Goede 		goto out_free_levels;
91514ca7a47SHans de Goede 
91614ca7a47SHans de Goede 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
917*05950094SAaron Lu 			  "found %d brightness levels\n", br->count - 2));
918*05950094SAaron Lu 	return 0;
91914ca7a47SHans de Goede 
92014ca7a47SHans de Goede out_free_levels:
92114ca7a47SHans de Goede 	kfree(br->levels);
92214ca7a47SHans de Goede 	kfree(br);
92314ca7a47SHans de Goede 	device->brightness = NULL;
92414ca7a47SHans de Goede 	return result;
92514ca7a47SHans de Goede }
92614ca7a47SHans de Goede 
92714ca7a47SHans de Goede /*
92814ca7a47SHans de Goede  *  Arg:
92914ca7a47SHans de Goede  *	device	: video output device (LCD, CRT, ..)
93014ca7a47SHans de Goede  *
93114ca7a47SHans de Goede  *  Return Value:
93214ca7a47SHans de Goede  *	None
93314ca7a47SHans de Goede  *
93414ca7a47SHans de Goede  *  Find out all required AML methods defined under the output
93514ca7a47SHans de Goede  *  device.
93614ca7a47SHans de Goede  */
93714ca7a47SHans de Goede 
93814ca7a47SHans de Goede static void acpi_video_device_find_cap(struct acpi_video_device *device)
93914ca7a47SHans de Goede {
94014ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_ADR"))
94114ca7a47SHans de Goede 		device->cap._ADR = 1;
94214ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BCL"))
94314ca7a47SHans de Goede 		device->cap._BCL = 1;
94414ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BCM"))
94514ca7a47SHans de Goede 		device->cap._BCM = 1;
94614ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_BQC")) {
94714ca7a47SHans de Goede 		device->cap._BQC = 1;
94814ca7a47SHans de Goede 	} else if (acpi_has_method(device->dev->handle, "_BCQ")) {
94914ca7a47SHans de Goede 		printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
95014ca7a47SHans de Goede 		device->cap._BCQ = 1;
95114ca7a47SHans de Goede 	}
95214ca7a47SHans de Goede 
95314ca7a47SHans de Goede 	if (acpi_has_method(device->dev->handle, "_DDC"))
95414ca7a47SHans de Goede 		device->cap._DDC = 1;
95514ca7a47SHans de Goede }
95614ca7a47SHans de Goede 
95714ca7a47SHans de Goede /*
95814ca7a47SHans de Goede  *  Arg:
95914ca7a47SHans de Goede  *	device	: video output device (VGA)
96014ca7a47SHans de Goede  *
96114ca7a47SHans de Goede  *  Return Value:
96214ca7a47SHans de Goede  *	None
96314ca7a47SHans de Goede  *
96414ca7a47SHans de Goede  *  Find out all required AML methods defined under the video bus device.
96514ca7a47SHans de Goede  */
96614ca7a47SHans de Goede 
96714ca7a47SHans de Goede static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
96814ca7a47SHans de Goede {
96914ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_DOS"))
97014ca7a47SHans de Goede 		video->cap._DOS = 1;
97114ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_DOD"))
97214ca7a47SHans de Goede 		video->cap._DOD = 1;
97314ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_ROM"))
97414ca7a47SHans de Goede 		video->cap._ROM = 1;
97514ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_GPD"))
97614ca7a47SHans de Goede 		video->cap._GPD = 1;
97714ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_SPD"))
97814ca7a47SHans de Goede 		video->cap._SPD = 1;
97914ca7a47SHans de Goede 	if (acpi_has_method(video->device->handle, "_VPO"))
98014ca7a47SHans de Goede 		video->cap._VPO = 1;
98114ca7a47SHans de Goede }
98214ca7a47SHans de Goede 
98314ca7a47SHans de Goede /*
98414ca7a47SHans de Goede  * Check whether the video bus device has required AML method to
98514ca7a47SHans de Goede  * support the desired features
98614ca7a47SHans de Goede  */
98714ca7a47SHans de Goede 
98814ca7a47SHans de Goede static int acpi_video_bus_check(struct acpi_video_bus *video)
98914ca7a47SHans de Goede {
99014ca7a47SHans de Goede 	acpi_status status = -ENOENT;
99114ca7a47SHans de Goede 	struct pci_dev *dev;
99214ca7a47SHans de Goede 
99314ca7a47SHans de Goede 	if (!video)
99414ca7a47SHans de Goede 		return -EINVAL;
99514ca7a47SHans de Goede 
99614ca7a47SHans de Goede 	dev = acpi_get_pci_dev(video->device->handle);
99714ca7a47SHans de Goede 	if (!dev)
99814ca7a47SHans de Goede 		return -ENODEV;
99914ca7a47SHans de Goede 	pci_dev_put(dev);
100014ca7a47SHans de Goede 
100114ca7a47SHans de Goede 	/*
100214ca7a47SHans de Goede 	 * Since there is no HID, CID and so on for VGA driver, we have
100314ca7a47SHans de Goede 	 * to check well known required nodes.
100414ca7a47SHans de Goede 	 */
100514ca7a47SHans de Goede 
100614ca7a47SHans de Goede 	/* Does this device support video switching? */
100714ca7a47SHans de Goede 	if (video->cap._DOS || video->cap._DOD) {
100814ca7a47SHans de Goede 		if (!video->cap._DOS) {
100914ca7a47SHans de Goede 			printk(KERN_WARNING FW_BUG
101014ca7a47SHans de Goede 				"ACPI(%s) defines _DOD but not _DOS\n",
101114ca7a47SHans de Goede 				acpi_device_bid(video->device));
101214ca7a47SHans de Goede 		}
101314ca7a47SHans de Goede 		video->flags.multihead = 1;
101414ca7a47SHans de Goede 		status = 0;
101514ca7a47SHans de Goede 	}
101614ca7a47SHans de Goede 
101714ca7a47SHans de Goede 	/* Does this device support retrieving a video ROM? */
101814ca7a47SHans de Goede 	if (video->cap._ROM) {
101914ca7a47SHans de Goede 		video->flags.rom = 1;
102014ca7a47SHans de Goede 		status = 0;
102114ca7a47SHans de Goede 	}
102214ca7a47SHans de Goede 
102314ca7a47SHans de Goede 	/* Does this device support configuring which video device to POST? */
102414ca7a47SHans de Goede 	if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
102514ca7a47SHans de Goede 		video->flags.post = 1;
102614ca7a47SHans de Goede 		status = 0;
102714ca7a47SHans de Goede 	}
102814ca7a47SHans de Goede 
102914ca7a47SHans de Goede 	return status;
103014ca7a47SHans de Goede }
103114ca7a47SHans de Goede 
103214ca7a47SHans de Goede /*
103314ca7a47SHans de Goede  * --------------------------------------------------------------------------
103414ca7a47SHans de Goede  *                               Driver Interface
103514ca7a47SHans de Goede  * --------------------------------------------------------------------------
103614ca7a47SHans de Goede  */
103714ca7a47SHans de Goede 
103814ca7a47SHans de Goede /* device interface */
103914ca7a47SHans de Goede static struct acpi_video_device_attrib *
104014ca7a47SHans de Goede acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
104114ca7a47SHans de Goede {
104214ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
104314ca7a47SHans de Goede 	int i;
104414ca7a47SHans de Goede 
104514ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
104614ca7a47SHans de Goede 		ids = &video->attached_array[i];
104714ca7a47SHans de Goede 		if ((ids->value.int_val & 0xffff) == device_id)
104814ca7a47SHans de Goede 			return &ids->value.attrib;
104914ca7a47SHans de Goede 	}
105014ca7a47SHans de Goede 
105114ca7a47SHans de Goede 	return NULL;
105214ca7a47SHans de Goede }
105314ca7a47SHans de Goede 
105414ca7a47SHans de Goede static int
105514ca7a47SHans de Goede acpi_video_get_device_type(struct acpi_video_bus *video,
105614ca7a47SHans de Goede 			   unsigned long device_id)
105714ca7a47SHans de Goede {
105814ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
105914ca7a47SHans de Goede 	int i;
106014ca7a47SHans de Goede 
106114ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
106214ca7a47SHans de Goede 		ids = &video->attached_array[i];
106314ca7a47SHans de Goede 		if ((ids->value.int_val & 0xffff) == device_id)
106414ca7a47SHans de Goede 			return ids->value.int_val;
106514ca7a47SHans de Goede 	}
106614ca7a47SHans de Goede 
106714ca7a47SHans de Goede 	return 0;
106814ca7a47SHans de Goede }
106914ca7a47SHans de Goede 
107014ca7a47SHans de Goede static int
107114ca7a47SHans de Goede acpi_video_bus_get_one_device(struct acpi_device *device,
107214ca7a47SHans de Goede 			      struct acpi_video_bus *video)
107314ca7a47SHans de Goede {
107414ca7a47SHans de Goede 	unsigned long long device_id;
107514ca7a47SHans de Goede 	int status, device_type;
107614ca7a47SHans de Goede 	struct acpi_video_device *data;
107714ca7a47SHans de Goede 	struct acpi_video_device_attrib *attribute;
107814ca7a47SHans de Goede 
107914ca7a47SHans de Goede 	status =
108014ca7a47SHans de Goede 	    acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
108114ca7a47SHans de Goede 	/* Some device omits _ADR, we skip them instead of fail */
108214ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
108314ca7a47SHans de Goede 		return 0;
108414ca7a47SHans de Goede 
108514ca7a47SHans de Goede 	data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
108614ca7a47SHans de Goede 	if (!data)
108714ca7a47SHans de Goede 		return -ENOMEM;
108814ca7a47SHans de Goede 
108914ca7a47SHans de Goede 	strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
109014ca7a47SHans de Goede 	strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
109114ca7a47SHans de Goede 	device->driver_data = data;
109214ca7a47SHans de Goede 
109314ca7a47SHans de Goede 	data->device_id = device_id;
109414ca7a47SHans de Goede 	data->video = video;
109514ca7a47SHans de Goede 	data->dev = device;
109614ca7a47SHans de Goede 	INIT_DELAYED_WORK(&data->switch_brightness_work,
109714ca7a47SHans de Goede 			  acpi_video_switch_brightness);
109814ca7a47SHans de Goede 
109914ca7a47SHans de Goede 	attribute = acpi_video_get_device_attr(video, device_id);
110014ca7a47SHans de Goede 
1101e50b9be1SAaron Lu 	if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
110214ca7a47SHans de Goede 		switch (attribute->display_type) {
110314ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_CRT:
110414ca7a47SHans de Goede 			data->flags.crt = 1;
110514ca7a47SHans de Goede 			break;
110614ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_TV:
110714ca7a47SHans de Goede 			data->flags.tvout = 1;
110814ca7a47SHans de Goede 			break;
110914ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_DVI:
111014ca7a47SHans de Goede 			data->flags.dvi = 1;
111114ca7a47SHans de Goede 			break;
111214ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LCD:
111314ca7a47SHans de Goede 			data->flags.lcd = 1;
111414ca7a47SHans de Goede 			break;
111514ca7a47SHans de Goede 		default:
111614ca7a47SHans de Goede 			data->flags.unknown = 1;
111714ca7a47SHans de Goede 			break;
111814ca7a47SHans de Goede 		}
111914ca7a47SHans de Goede 		if (attribute->bios_can_detect)
112014ca7a47SHans de Goede 			data->flags.bios = 1;
112114ca7a47SHans de Goede 	} else {
112214ca7a47SHans de Goede 		/* Check for legacy IDs */
112314ca7a47SHans de Goede 		device_type = acpi_video_get_device_type(video, device_id);
112414ca7a47SHans de Goede 		/* Ignore bits 16 and 18-20 */
112514ca7a47SHans de Goede 		switch (device_type & 0xffe2ffff) {
112614ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
112714ca7a47SHans de Goede 			data->flags.crt = 1;
112814ca7a47SHans de Goede 			break;
112914ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
113014ca7a47SHans de Goede 			data->flags.lcd = 1;
113114ca7a47SHans de Goede 			break;
113214ca7a47SHans de Goede 		case ACPI_VIDEO_DISPLAY_LEGACY_TV:
113314ca7a47SHans de Goede 			data->flags.tvout = 1;
113414ca7a47SHans de Goede 			break;
113514ca7a47SHans de Goede 		default:
113614ca7a47SHans de Goede 			data->flags.unknown = 1;
113714ca7a47SHans de Goede 		}
113814ca7a47SHans de Goede 	}
113914ca7a47SHans de Goede 
114014ca7a47SHans de Goede 	acpi_video_device_bind(video, data);
114114ca7a47SHans de Goede 	acpi_video_device_find_cap(data);
114214ca7a47SHans de Goede 
114314ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
114414ca7a47SHans de Goede 	list_add_tail(&data->entry, &video->video_device_list);
114514ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
114614ca7a47SHans de Goede 
114714ca7a47SHans de Goede 	return status;
114814ca7a47SHans de Goede }
114914ca7a47SHans de Goede 
115014ca7a47SHans de Goede /*
115114ca7a47SHans de Goede  *  Arg:
115214ca7a47SHans de Goede  *	video	: video bus device
115314ca7a47SHans de Goede  *
115414ca7a47SHans de Goede  *  Return:
115514ca7a47SHans de Goede  *	none
115614ca7a47SHans de Goede  *
115714ca7a47SHans de Goede  *  Enumerate the video device list of the video bus,
115814ca7a47SHans de Goede  *  bind the ids with the corresponding video devices
115914ca7a47SHans de Goede  *  under the video bus.
116014ca7a47SHans de Goede  */
116114ca7a47SHans de Goede 
116214ca7a47SHans de Goede static void acpi_video_device_rebind(struct acpi_video_bus *video)
116314ca7a47SHans de Goede {
116414ca7a47SHans de Goede 	struct acpi_video_device *dev;
116514ca7a47SHans de Goede 
116614ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
116714ca7a47SHans de Goede 
116814ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
116914ca7a47SHans de Goede 		acpi_video_device_bind(video, dev);
117014ca7a47SHans de Goede 
117114ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
117214ca7a47SHans de Goede }
117314ca7a47SHans de Goede 
117414ca7a47SHans de Goede /*
117514ca7a47SHans de Goede  *  Arg:
117614ca7a47SHans de Goede  *	video	: video bus device
117714ca7a47SHans de Goede  *	device	: video output device under the video
117814ca7a47SHans de Goede  *		bus
117914ca7a47SHans de Goede  *
118014ca7a47SHans de Goede  *  Return:
118114ca7a47SHans de Goede  *	none
118214ca7a47SHans de Goede  *
118314ca7a47SHans de Goede  *  Bind the ids with the corresponding video devices
118414ca7a47SHans de Goede  *  under the video bus.
118514ca7a47SHans de Goede  */
118614ca7a47SHans de Goede 
118714ca7a47SHans de Goede static void
118814ca7a47SHans de Goede acpi_video_device_bind(struct acpi_video_bus *video,
118914ca7a47SHans de Goede 		       struct acpi_video_device *device)
119014ca7a47SHans de Goede {
119114ca7a47SHans de Goede 	struct acpi_video_enumerated_device *ids;
119214ca7a47SHans de Goede 	int i;
119314ca7a47SHans de Goede 
119414ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
119514ca7a47SHans de Goede 		ids = &video->attached_array[i];
119614ca7a47SHans de Goede 		if (device->device_id == (ids->value.int_val & 0xffff)) {
119714ca7a47SHans de Goede 			ids->bind_info = device;
119814ca7a47SHans de Goede 			ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
119914ca7a47SHans de Goede 		}
120014ca7a47SHans de Goede 	}
120114ca7a47SHans de Goede }
120214ca7a47SHans de Goede 
120314ca7a47SHans de Goede static bool acpi_video_device_in_dod(struct acpi_video_device *device)
120414ca7a47SHans de Goede {
120514ca7a47SHans de Goede 	struct acpi_video_bus *video = device->video;
120614ca7a47SHans de Goede 	int i;
120714ca7a47SHans de Goede 
120814ca7a47SHans de Goede 	/*
120914ca7a47SHans de Goede 	 * If we have a broken _DOD or we have more than 8 output devices
121014ca7a47SHans de Goede 	 * under the graphics controller node that we can't proper deal with
121114ca7a47SHans de Goede 	 * in the operation region code currently, no need to test.
121214ca7a47SHans de Goede 	 */
121314ca7a47SHans de Goede 	if (!video->attached_count || video->child_count > 8)
121414ca7a47SHans de Goede 		return true;
121514ca7a47SHans de Goede 
121614ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
121714ca7a47SHans de Goede 		if ((video->attached_array[i].value.int_val & 0xfff) ==
121814ca7a47SHans de Goede 		    (device->device_id & 0xfff))
121914ca7a47SHans de Goede 			return true;
122014ca7a47SHans de Goede 	}
122114ca7a47SHans de Goede 
122214ca7a47SHans de Goede 	return false;
122314ca7a47SHans de Goede }
122414ca7a47SHans de Goede 
122514ca7a47SHans de Goede /*
122614ca7a47SHans de Goede  *  Arg:
122714ca7a47SHans de Goede  *	video	: video bus device
122814ca7a47SHans de Goede  *
122914ca7a47SHans de Goede  *  Return:
123014ca7a47SHans de Goede  *	< 0	: error
123114ca7a47SHans de Goede  *
123214ca7a47SHans de Goede  *  Call _DOD to enumerate all devices attached to display adapter
123314ca7a47SHans de Goede  *
123414ca7a47SHans de Goede  */
123514ca7a47SHans de Goede 
123614ca7a47SHans de Goede static int acpi_video_device_enumerate(struct acpi_video_bus *video)
123714ca7a47SHans de Goede {
123814ca7a47SHans de Goede 	int status;
123914ca7a47SHans de Goede 	int count;
124014ca7a47SHans de Goede 	int i;
124114ca7a47SHans de Goede 	struct acpi_video_enumerated_device *active_list;
124214ca7a47SHans de Goede 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
124314ca7a47SHans de Goede 	union acpi_object *dod = NULL;
124414ca7a47SHans de Goede 	union acpi_object *obj;
124514ca7a47SHans de Goede 
124614ca7a47SHans de Goede 	status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
124714ca7a47SHans de Goede 	if (!ACPI_SUCCESS(status)) {
124814ca7a47SHans de Goede 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
124914ca7a47SHans de Goede 		return status;
125014ca7a47SHans de Goede 	}
125114ca7a47SHans de Goede 
125214ca7a47SHans de Goede 	dod = buffer.pointer;
125314ca7a47SHans de Goede 	if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
125414ca7a47SHans de Goede 		ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
125514ca7a47SHans de Goede 		status = -EFAULT;
125614ca7a47SHans de Goede 		goto out;
125714ca7a47SHans de Goede 	}
125814ca7a47SHans de Goede 
125914ca7a47SHans de Goede 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
126014ca7a47SHans de Goede 			  dod->package.count));
126114ca7a47SHans de Goede 
126214ca7a47SHans de Goede 	active_list = kcalloc(1 + dod->package.count,
126314ca7a47SHans de Goede 			      sizeof(struct acpi_video_enumerated_device),
126414ca7a47SHans de Goede 			      GFP_KERNEL);
126514ca7a47SHans de Goede 	if (!active_list) {
126614ca7a47SHans de Goede 		status = -ENOMEM;
126714ca7a47SHans de Goede 		goto out;
126814ca7a47SHans de Goede 	}
126914ca7a47SHans de Goede 
127014ca7a47SHans de Goede 	count = 0;
127114ca7a47SHans de Goede 	for (i = 0; i < dod->package.count; i++) {
127214ca7a47SHans de Goede 		obj = &dod->package.elements[i];
127314ca7a47SHans de Goede 
127414ca7a47SHans de Goede 		if (obj->type != ACPI_TYPE_INTEGER) {
127514ca7a47SHans de Goede 			printk(KERN_ERR PREFIX
127614ca7a47SHans de Goede 				"Invalid _DOD data in element %d\n", i);
127714ca7a47SHans de Goede 			continue;
127814ca7a47SHans de Goede 		}
127914ca7a47SHans de Goede 
128014ca7a47SHans de Goede 		active_list[count].value.int_val = obj->integer.value;
128114ca7a47SHans de Goede 		active_list[count].bind_info = NULL;
128214ca7a47SHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
128314ca7a47SHans de Goede 				  (int)obj->integer.value));
128414ca7a47SHans de Goede 		count++;
128514ca7a47SHans de Goede 	}
128614ca7a47SHans de Goede 
128714ca7a47SHans de Goede 	kfree(video->attached_array);
128814ca7a47SHans de Goede 
128914ca7a47SHans de Goede 	video->attached_array = active_list;
129014ca7a47SHans de Goede 	video->attached_count = count;
129114ca7a47SHans de Goede 
129214ca7a47SHans de Goede out:
129314ca7a47SHans de Goede 	kfree(buffer.pointer);
129414ca7a47SHans de Goede 	return status;
129514ca7a47SHans de Goede }
129614ca7a47SHans de Goede 
129714ca7a47SHans de Goede static int
129814ca7a47SHans de Goede acpi_video_get_next_level(struct acpi_video_device *device,
129914ca7a47SHans de Goede 			  u32 level_current, u32 event)
130014ca7a47SHans de Goede {
130114ca7a47SHans de Goede 	int min, max, min_above, max_below, i, l, delta = 255;
130214ca7a47SHans de Goede 	max = max_below = 0;
130314ca7a47SHans de Goede 	min = min_above = 255;
130414ca7a47SHans de Goede 	/* Find closest level to level_current */
130514ca7a47SHans de Goede 	for (i = 2; i < device->brightness->count; i++) {
130614ca7a47SHans de Goede 		l = device->brightness->levels[i];
130714ca7a47SHans de Goede 		if (abs(l - level_current) < abs(delta)) {
130814ca7a47SHans de Goede 			delta = l - level_current;
130914ca7a47SHans de Goede 			if (!delta)
131014ca7a47SHans de Goede 				break;
131114ca7a47SHans de Goede 		}
131214ca7a47SHans de Goede 	}
131314ca7a47SHans de Goede 	/* Ajust level_current to closest available level */
131414ca7a47SHans de Goede 	level_current += delta;
131514ca7a47SHans de Goede 	for (i = 2; i < device->brightness->count; i++) {
131614ca7a47SHans de Goede 		l = device->brightness->levels[i];
131714ca7a47SHans de Goede 		if (l < min)
131814ca7a47SHans de Goede 			min = l;
131914ca7a47SHans de Goede 		if (l > max)
132014ca7a47SHans de Goede 			max = l;
132114ca7a47SHans de Goede 		if (l < min_above && l > level_current)
132214ca7a47SHans de Goede 			min_above = l;
132314ca7a47SHans de Goede 		if (l > max_below && l < level_current)
132414ca7a47SHans de Goede 			max_below = l;
132514ca7a47SHans de Goede 	}
132614ca7a47SHans de Goede 
132714ca7a47SHans de Goede 	switch (event) {
132814ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
132914ca7a47SHans de Goede 		return (level_current < max) ? min_above : min;
133014ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
133114ca7a47SHans de Goede 		return (level_current < max) ? min_above : max;
133214ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
133314ca7a47SHans de Goede 		return (level_current > min) ? max_below : min;
133414ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
133514ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
133614ca7a47SHans de Goede 		return 0;
133714ca7a47SHans de Goede 	default:
133814ca7a47SHans de Goede 		return level_current;
133914ca7a47SHans de Goede 	}
134014ca7a47SHans de Goede }
134114ca7a47SHans de Goede 
134214ca7a47SHans de Goede static void
134314ca7a47SHans de Goede acpi_video_switch_brightness(struct work_struct *work)
134414ca7a47SHans de Goede {
134514ca7a47SHans de Goede 	struct acpi_video_device *device = container_of(to_delayed_work(work),
134614ca7a47SHans de Goede 			     struct acpi_video_device, switch_brightness_work);
134714ca7a47SHans de Goede 	unsigned long long level_current, level_next;
134814ca7a47SHans de Goede 	int event = device->switch_brightness_event;
134914ca7a47SHans de Goede 	int result = -EINVAL;
135014ca7a47SHans de Goede 
135114ca7a47SHans de Goede 	/* no warning message if acpi_backlight=vendor or a quirk is used */
135214ca7a47SHans de Goede 	if (!device->backlight)
135314ca7a47SHans de Goede 		return;
135414ca7a47SHans de Goede 
135514ca7a47SHans de Goede 	if (!device->brightness)
135614ca7a47SHans de Goede 		goto out;
135714ca7a47SHans de Goede 
135814ca7a47SHans de Goede 	result = acpi_video_device_lcd_get_level_current(device,
135914ca7a47SHans de Goede 							 &level_current,
136014ca7a47SHans de Goede 							 false);
136114ca7a47SHans de Goede 	if (result)
136214ca7a47SHans de Goede 		goto out;
136314ca7a47SHans de Goede 
136414ca7a47SHans de Goede 	level_next = acpi_video_get_next_level(device, level_current, event);
136514ca7a47SHans de Goede 
136614ca7a47SHans de Goede 	result = acpi_video_device_lcd_set_level(device, level_next);
136714ca7a47SHans de Goede 
136814ca7a47SHans de Goede 	if (!result)
136914ca7a47SHans de Goede 		backlight_force_update(device->backlight,
137014ca7a47SHans de Goede 				       BACKLIGHT_UPDATE_HOTKEY);
137114ca7a47SHans de Goede 
137214ca7a47SHans de Goede out:
137314ca7a47SHans de Goede 	if (result)
137414ca7a47SHans de Goede 		printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
137514ca7a47SHans de Goede }
137614ca7a47SHans de Goede 
137714ca7a47SHans de Goede int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
137814ca7a47SHans de Goede 			void **edid)
137914ca7a47SHans de Goede {
138014ca7a47SHans de Goede 	struct acpi_video_bus *video;
138114ca7a47SHans de Goede 	struct acpi_video_device *video_device;
138214ca7a47SHans de Goede 	union acpi_object *buffer = NULL;
138314ca7a47SHans de Goede 	acpi_status status;
138414ca7a47SHans de Goede 	int i, length;
138514ca7a47SHans de Goede 
138614ca7a47SHans de Goede 	if (!device || !acpi_driver_data(device))
138714ca7a47SHans de Goede 		return -EINVAL;
138814ca7a47SHans de Goede 
138914ca7a47SHans de Goede 	video = acpi_driver_data(device);
139014ca7a47SHans de Goede 
139114ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
139214ca7a47SHans de Goede 		video_device = video->attached_array[i].bind_info;
139314ca7a47SHans de Goede 		length = 256;
139414ca7a47SHans de Goede 
139514ca7a47SHans de Goede 		if (!video_device)
139614ca7a47SHans de Goede 			continue;
139714ca7a47SHans de Goede 
139814ca7a47SHans de Goede 		if (!video_device->cap._DDC)
139914ca7a47SHans de Goede 			continue;
140014ca7a47SHans de Goede 
140114ca7a47SHans de Goede 		if (type) {
140214ca7a47SHans de Goede 			switch (type) {
140314ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_CRT:
140414ca7a47SHans de Goede 				if (!video_device->flags.crt)
140514ca7a47SHans de Goede 					continue;
140614ca7a47SHans de Goede 				break;
140714ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_TV:
140814ca7a47SHans de Goede 				if (!video_device->flags.tvout)
140914ca7a47SHans de Goede 					continue;
141014ca7a47SHans de Goede 				break;
141114ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_DVI:
141214ca7a47SHans de Goede 				if (!video_device->flags.dvi)
141314ca7a47SHans de Goede 					continue;
141414ca7a47SHans de Goede 				break;
141514ca7a47SHans de Goede 			case ACPI_VIDEO_DISPLAY_LCD:
141614ca7a47SHans de Goede 				if (!video_device->flags.lcd)
141714ca7a47SHans de Goede 					continue;
141814ca7a47SHans de Goede 				break;
141914ca7a47SHans de Goede 			}
142014ca7a47SHans de Goede 		} else if (video_device->device_id != device_id) {
142114ca7a47SHans de Goede 			continue;
142214ca7a47SHans de Goede 		}
142314ca7a47SHans de Goede 
142414ca7a47SHans de Goede 		status = acpi_video_device_EDID(video_device, &buffer, length);
142514ca7a47SHans de Goede 
142614ca7a47SHans de Goede 		if (ACPI_FAILURE(status) || !buffer ||
142714ca7a47SHans de Goede 		    buffer->type != ACPI_TYPE_BUFFER) {
142814ca7a47SHans de Goede 			length = 128;
142914ca7a47SHans de Goede 			status = acpi_video_device_EDID(video_device, &buffer,
143014ca7a47SHans de Goede 							length);
143114ca7a47SHans de Goede 			if (ACPI_FAILURE(status) || !buffer ||
143214ca7a47SHans de Goede 			    buffer->type != ACPI_TYPE_BUFFER) {
143314ca7a47SHans de Goede 				continue;
143414ca7a47SHans de Goede 			}
143514ca7a47SHans de Goede 		}
143614ca7a47SHans de Goede 
143714ca7a47SHans de Goede 		*edid = buffer->buffer.pointer;
143814ca7a47SHans de Goede 		return length;
143914ca7a47SHans de Goede 	}
144014ca7a47SHans de Goede 
144114ca7a47SHans de Goede 	return -ENODEV;
144214ca7a47SHans de Goede }
144314ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_get_edid);
144414ca7a47SHans de Goede 
144514ca7a47SHans de Goede static int
144614ca7a47SHans de Goede acpi_video_bus_get_devices(struct acpi_video_bus *video,
144714ca7a47SHans de Goede 			   struct acpi_device *device)
144814ca7a47SHans de Goede {
144914ca7a47SHans de Goede 	int status = 0;
145014ca7a47SHans de Goede 	struct acpi_device *dev;
145114ca7a47SHans de Goede 
145214ca7a47SHans de Goede 	/*
145314ca7a47SHans de Goede 	 * There are systems where video module known to work fine regardless
145414ca7a47SHans de Goede 	 * of broken _DOD and ignoring returned value here doesn't cause
145514ca7a47SHans de Goede 	 * any issues later.
145614ca7a47SHans de Goede 	 */
145714ca7a47SHans de Goede 	acpi_video_device_enumerate(video);
145814ca7a47SHans de Goede 
145914ca7a47SHans de Goede 	list_for_each_entry(dev, &device->children, node) {
146014ca7a47SHans de Goede 
146114ca7a47SHans de Goede 		status = acpi_video_bus_get_one_device(dev, video);
146214ca7a47SHans de Goede 		if (status) {
146314ca7a47SHans de Goede 			dev_err(&dev->dev, "Can't attach device\n");
146414ca7a47SHans de Goede 			break;
146514ca7a47SHans de Goede 		}
146614ca7a47SHans de Goede 		video->child_count++;
146714ca7a47SHans de Goede 	}
146814ca7a47SHans de Goede 	return status;
146914ca7a47SHans de Goede }
147014ca7a47SHans de Goede 
147114ca7a47SHans de Goede /* acpi_video interface */
147214ca7a47SHans de Goede 
147314ca7a47SHans de Goede /*
147414ca7a47SHans de Goede  * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
147514ca7a47SHans de Goede  * preform any automatic brightness change on receiving a notification.
147614ca7a47SHans de Goede  */
147714ca7a47SHans de Goede static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
147814ca7a47SHans de Goede {
147914ca7a47SHans de Goede 	return acpi_video_bus_DOS(video, 0,
148014ca7a47SHans de Goede 				  acpi_osi_is_win8() ? 1 : 0);
148114ca7a47SHans de Goede }
148214ca7a47SHans de Goede 
148314ca7a47SHans de Goede static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
148414ca7a47SHans de Goede {
148514ca7a47SHans de Goede 	return acpi_video_bus_DOS(video, 0,
148614ca7a47SHans de Goede 				  acpi_osi_is_win8() ? 0 : 1);
148714ca7a47SHans de Goede }
148814ca7a47SHans de Goede 
148914ca7a47SHans de Goede static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
149014ca7a47SHans de Goede {
149114ca7a47SHans de Goede 	struct acpi_video_bus *video = acpi_driver_data(device);
149214ca7a47SHans de Goede 	struct input_dev *input;
149314ca7a47SHans de Goede 	int keycode = 0;
149414ca7a47SHans de Goede 
149514ca7a47SHans de Goede 	if (!video || !video->input)
149614ca7a47SHans de Goede 		return;
149714ca7a47SHans de Goede 
149814ca7a47SHans de Goede 	input = video->input;
149914ca7a47SHans de Goede 
150014ca7a47SHans de Goede 	switch (event) {
150114ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_SWITCH:	/* User requested a switch,
150214ca7a47SHans de Goede 					 * most likely via hotkey. */
150314ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
150414ca7a47SHans de Goede 		break;
150514ca7a47SHans de Goede 
150614ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_PROBE:	/* User plugged in or removed a video
150714ca7a47SHans de Goede 					 * connector. */
150814ca7a47SHans de Goede 		acpi_video_device_enumerate(video);
150914ca7a47SHans de Goede 		acpi_video_device_rebind(video);
151014ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
151114ca7a47SHans de Goede 		break;
151214ca7a47SHans de Goede 
151314ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE:	/* Cycle Display output hotkey pressed. */
151414ca7a47SHans de Goede 		keycode = KEY_SWITCHVIDEOMODE;
151514ca7a47SHans de Goede 		break;
151614ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:	/* Next Display output hotkey pressed. */
151714ca7a47SHans de Goede 		keycode = KEY_VIDEO_NEXT;
151814ca7a47SHans de Goede 		break;
151914ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:	/* previous Display output hotkey pressed. */
152014ca7a47SHans de Goede 		keycode = KEY_VIDEO_PREV;
152114ca7a47SHans de Goede 		break;
152214ca7a47SHans de Goede 
152314ca7a47SHans de Goede 	default:
152414ca7a47SHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
152514ca7a47SHans de Goede 				  "Unsupported event [0x%x]\n", event));
152614ca7a47SHans de Goede 		break;
152714ca7a47SHans de Goede 	}
152814ca7a47SHans de Goede 
152914ca7a47SHans de Goede 	if (acpi_notifier_call_chain(device, event, 0))
153014ca7a47SHans de Goede 		/* Something vetoed the keypress. */
153114ca7a47SHans de Goede 		keycode = 0;
153214ca7a47SHans de Goede 
153305bc59a0SHans de Goede 	if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
153414ca7a47SHans de Goede 		input_report_key(input, keycode, 1);
153514ca7a47SHans de Goede 		input_sync(input);
153614ca7a47SHans de Goede 		input_report_key(input, keycode, 0);
153714ca7a47SHans de Goede 		input_sync(input);
153814ca7a47SHans de Goede 	}
153914ca7a47SHans de Goede 
154014ca7a47SHans de Goede 	return;
154114ca7a47SHans de Goede }
154214ca7a47SHans de Goede 
154314ca7a47SHans de Goede static void brightness_switch_event(struct acpi_video_device *video_device,
154414ca7a47SHans de Goede 				    u32 event)
154514ca7a47SHans de Goede {
154614ca7a47SHans de Goede 	if (!brightness_switch_enabled)
154714ca7a47SHans de Goede 		return;
154814ca7a47SHans de Goede 
154914ca7a47SHans de Goede 	video_device->switch_brightness_event = event;
155014ca7a47SHans de Goede 	schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
155114ca7a47SHans de Goede }
155214ca7a47SHans de Goede 
155314ca7a47SHans de Goede static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
155414ca7a47SHans de Goede {
155514ca7a47SHans de Goede 	struct acpi_video_device *video_device = data;
155614ca7a47SHans de Goede 	struct acpi_device *device = NULL;
155714ca7a47SHans de Goede 	struct acpi_video_bus *bus;
155814ca7a47SHans de Goede 	struct input_dev *input;
155914ca7a47SHans de Goede 	int keycode = 0;
156014ca7a47SHans de Goede 
156114ca7a47SHans de Goede 	if (!video_device)
156214ca7a47SHans de Goede 		return;
156314ca7a47SHans de Goede 
156414ca7a47SHans de Goede 	device = video_device->dev;
156514ca7a47SHans de Goede 	bus = video_device->video;
156614ca7a47SHans de Goede 	input = bus->input;
156714ca7a47SHans de Goede 
156814ca7a47SHans de Goede 	switch (event) {
156914ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:	/* Cycle brightness */
157014ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
157114ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESS_CYCLE;
157214ca7a47SHans de Goede 		break;
157314ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:	/* Increase brightness */
157414ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
157514ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESSUP;
157614ca7a47SHans de Goede 		break;
157714ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:	/* Decrease brightness */
157814ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
157914ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESSDOWN;
158014ca7a47SHans de Goede 		break;
158114ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:	/* zero brightness */
158214ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
158314ca7a47SHans de Goede 		keycode = KEY_BRIGHTNESS_ZERO;
158414ca7a47SHans de Goede 		break;
158514ca7a47SHans de Goede 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:	/* display device off */
158614ca7a47SHans de Goede 		brightness_switch_event(video_device, event);
158714ca7a47SHans de Goede 		keycode = KEY_DISPLAY_OFF;
158814ca7a47SHans de Goede 		break;
158914ca7a47SHans de Goede 	default:
159014ca7a47SHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
159114ca7a47SHans de Goede 				  "Unsupported event [0x%x]\n", event));
159214ca7a47SHans de Goede 		break;
159314ca7a47SHans de Goede 	}
159414ca7a47SHans de Goede 
159514ca7a47SHans de Goede 	acpi_notifier_call_chain(device, event, 0);
159614ca7a47SHans de Goede 
159705bc59a0SHans de Goede 	if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
159814ca7a47SHans de Goede 		input_report_key(input, keycode, 1);
159914ca7a47SHans de Goede 		input_sync(input);
160014ca7a47SHans de Goede 		input_report_key(input, keycode, 0);
160114ca7a47SHans de Goede 		input_sync(input);
160214ca7a47SHans de Goede 	}
160314ca7a47SHans de Goede 
160414ca7a47SHans de Goede 	return;
160514ca7a47SHans de Goede }
160614ca7a47SHans de Goede 
160714ca7a47SHans de Goede static int acpi_video_resume(struct notifier_block *nb,
160814ca7a47SHans de Goede 				unsigned long val, void *ign)
160914ca7a47SHans de Goede {
161014ca7a47SHans de Goede 	struct acpi_video_bus *video;
161114ca7a47SHans de Goede 	struct acpi_video_device *video_device;
161214ca7a47SHans de Goede 	int i;
161314ca7a47SHans de Goede 
161414ca7a47SHans de Goede 	switch (val) {
161514ca7a47SHans de Goede 	case PM_HIBERNATION_PREPARE:
161614ca7a47SHans de Goede 	case PM_SUSPEND_PREPARE:
161714ca7a47SHans de Goede 	case PM_RESTORE_PREPARE:
161814ca7a47SHans de Goede 		return NOTIFY_DONE;
161914ca7a47SHans de Goede 	}
162014ca7a47SHans de Goede 
162114ca7a47SHans de Goede 	video = container_of(nb, struct acpi_video_bus, pm_nb);
162214ca7a47SHans de Goede 
162314ca7a47SHans de Goede 	dev_info(&video->device->dev, "Restoring backlight state\n");
162414ca7a47SHans de Goede 
162514ca7a47SHans de Goede 	for (i = 0; i < video->attached_count; i++) {
162614ca7a47SHans de Goede 		video_device = video->attached_array[i].bind_info;
162714ca7a47SHans de Goede 		if (video_device && video_device->brightness)
162814ca7a47SHans de Goede 			acpi_video_device_lcd_set_level(video_device,
162914ca7a47SHans de Goede 					video_device->brightness->curr);
163014ca7a47SHans de Goede 	}
163114ca7a47SHans de Goede 
163214ca7a47SHans de Goede 	return NOTIFY_OK;
163314ca7a47SHans de Goede }
163414ca7a47SHans de Goede 
163514ca7a47SHans de Goede static acpi_status
163614ca7a47SHans de Goede acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
163714ca7a47SHans de Goede 			void **return_value)
163814ca7a47SHans de Goede {
163914ca7a47SHans de Goede 	struct acpi_device *device = context;
164014ca7a47SHans de Goede 	struct acpi_device *sibling;
164114ca7a47SHans de Goede 	int result;
164214ca7a47SHans de Goede 
164314ca7a47SHans de Goede 	if (handle == device->handle)
164414ca7a47SHans de Goede 		return AE_CTRL_TERMINATE;
164514ca7a47SHans de Goede 
164614ca7a47SHans de Goede 	result = acpi_bus_get_device(handle, &sibling);
164714ca7a47SHans de Goede 	if (result)
164814ca7a47SHans de Goede 		return AE_OK;
164914ca7a47SHans de Goede 
165014ca7a47SHans de Goede 	if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
165114ca7a47SHans de Goede 			return AE_ALREADY_EXISTS;
165214ca7a47SHans de Goede 
165314ca7a47SHans de Goede 	return AE_OK;
165414ca7a47SHans de Goede }
165514ca7a47SHans de Goede 
165614ca7a47SHans de Goede static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
165714ca7a47SHans de Goede {
165814ca7a47SHans de Goede 	struct backlight_properties props;
165914ca7a47SHans de Goede 	struct pci_dev *pdev;
166014ca7a47SHans de Goede 	acpi_handle acpi_parent;
166114ca7a47SHans de Goede 	struct device *parent = NULL;
166214ca7a47SHans de Goede 	int result;
166314ca7a47SHans de Goede 	static int count;
166414ca7a47SHans de Goede 	char *name;
166514ca7a47SHans de Goede 
166614ca7a47SHans de Goede 	result = acpi_video_init_brightness(device);
166714ca7a47SHans de Goede 	if (result)
166814ca7a47SHans de Goede 		return;
166914ca7a47SHans de Goede 
167014ca7a47SHans de Goede 	if (disable_backlight_sysfs_if > 0)
167114ca7a47SHans de Goede 		return;
167214ca7a47SHans de Goede 
167314ca7a47SHans de Goede 	name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
167414ca7a47SHans de Goede 	if (!name)
167514ca7a47SHans de Goede 		return;
167614ca7a47SHans de Goede 	count++;
167714ca7a47SHans de Goede 
167814ca7a47SHans de Goede 	acpi_get_parent(device->dev->handle, &acpi_parent);
167914ca7a47SHans de Goede 
168014ca7a47SHans de Goede 	pdev = acpi_get_pci_dev(acpi_parent);
168114ca7a47SHans de Goede 	if (pdev) {
168214ca7a47SHans de Goede 		parent = &pdev->dev;
168314ca7a47SHans de Goede 		pci_dev_put(pdev);
168414ca7a47SHans de Goede 	}
168514ca7a47SHans de Goede 
168614ca7a47SHans de Goede 	memset(&props, 0, sizeof(struct backlight_properties));
168714ca7a47SHans de Goede 	props.type = BACKLIGHT_FIRMWARE;
168814ca7a47SHans de Goede 	props.max_brightness = device->brightness->count - 3;
168914ca7a47SHans de Goede 	device->backlight = backlight_device_register(name,
169014ca7a47SHans de Goede 						      parent,
169114ca7a47SHans de Goede 						      device,
169214ca7a47SHans de Goede 						      &acpi_backlight_ops,
169314ca7a47SHans de Goede 						      &props);
169414ca7a47SHans de Goede 	kfree(name);
169514ca7a47SHans de Goede 	if (IS_ERR(device->backlight)) {
169614ca7a47SHans de Goede 		device->backlight = NULL;
169714ca7a47SHans de Goede 		return;
169814ca7a47SHans de Goede 	}
169914ca7a47SHans de Goede 
170014ca7a47SHans de Goede 	/*
170114ca7a47SHans de Goede 	 * Save current brightness level in case we have to restore it
170214ca7a47SHans de Goede 	 * before acpi_video_device_lcd_set_level() is called next time.
170314ca7a47SHans de Goede 	 */
170414ca7a47SHans de Goede 	device->backlight->props.brightness =
170514ca7a47SHans de Goede 			acpi_video_get_brightness(device->backlight);
170614ca7a47SHans de Goede 
170714ca7a47SHans de Goede 	device->cooling_dev = thermal_cooling_device_register("LCD",
170814ca7a47SHans de Goede 				device->dev, &video_cooling_ops);
170914ca7a47SHans de Goede 	if (IS_ERR(device->cooling_dev)) {
171014ca7a47SHans de Goede 		/*
171114ca7a47SHans de Goede 		 * Set cooling_dev to NULL so we don't crash trying to free it.
171214ca7a47SHans de Goede 		 * Also, why the hell we are returning early and not attempt to
171314ca7a47SHans de Goede 		 * register video output if cooling device registration failed?
171414ca7a47SHans de Goede 		 * -- dtor
171514ca7a47SHans de Goede 		 */
171614ca7a47SHans de Goede 		device->cooling_dev = NULL;
171714ca7a47SHans de Goede 		return;
171814ca7a47SHans de Goede 	}
171914ca7a47SHans de Goede 
172014ca7a47SHans de Goede 	dev_info(&device->dev->dev, "registered as cooling_device%d\n",
172114ca7a47SHans de Goede 		 device->cooling_dev->id);
172214ca7a47SHans de Goede 	result = sysfs_create_link(&device->dev->dev.kobj,
172314ca7a47SHans de Goede 			&device->cooling_dev->device.kobj,
172414ca7a47SHans de Goede 			"thermal_cooling");
172514ca7a47SHans de Goede 	if (result)
172614ca7a47SHans de Goede 		printk(KERN_ERR PREFIX "Create sysfs link\n");
172714ca7a47SHans de Goede 	result = sysfs_create_link(&device->cooling_dev->device.kobj,
172814ca7a47SHans de Goede 			&device->dev->dev.kobj, "device");
172914ca7a47SHans de Goede 	if (result)
173014ca7a47SHans de Goede 		printk(KERN_ERR PREFIX "Create sysfs link\n");
173114ca7a47SHans de Goede }
173214ca7a47SHans de Goede 
173314ca7a47SHans de Goede static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
173414ca7a47SHans de Goede {
173514ca7a47SHans de Goede 	struct acpi_video_device *dev;
173614ca7a47SHans de Goede 	union acpi_object *levels;
173714ca7a47SHans de Goede 
173814ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
173914ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry) {
174014ca7a47SHans de Goede 		if (!acpi_video_device_lcd_query_levels(dev, &levels))
174114ca7a47SHans de Goede 			kfree(levels);
174214ca7a47SHans de Goede 	}
174314ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
174414ca7a47SHans de Goede }
174514ca7a47SHans de Goede 
1746e50b9be1SAaron Lu static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1747e50b9be1SAaron Lu {
1748e50b9be1SAaron Lu 	/*
1749e50b9be1SAaron Lu 	 * Do not create backlight device for video output
1750e50b9be1SAaron Lu 	 * device that is not in the enumerated list.
1751e50b9be1SAaron Lu 	 */
1752e50b9be1SAaron Lu 	if (!acpi_video_device_in_dod(dev)) {
1753e50b9be1SAaron Lu 		dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1754e50b9be1SAaron Lu 		return false;
1755e50b9be1SAaron Lu 	}
1756e50b9be1SAaron Lu 
1757e50b9be1SAaron Lu 	if (only_lcd)
1758e50b9be1SAaron Lu 		return dev->flags.lcd;
1759e50b9be1SAaron Lu 	return true;
1760e50b9be1SAaron Lu }
1761e50b9be1SAaron Lu 
176214ca7a47SHans de Goede static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
176314ca7a47SHans de Goede {
176414ca7a47SHans de Goede 	struct acpi_video_device *dev;
176514ca7a47SHans de Goede 
176614ca7a47SHans de Goede 	if (video->backlight_registered)
176714ca7a47SHans de Goede 		return 0;
176814ca7a47SHans de Goede 
176914ca7a47SHans de Goede 	acpi_video_run_bcl_for_osi(video);
177014ca7a47SHans de Goede 
17713bd6bce3SHans de Goede 	if (acpi_video_get_backlight_type() != acpi_backlight_video)
177214ca7a47SHans de Goede 		return 0;
177314ca7a47SHans de Goede 
177414ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
1775e50b9be1SAaron Lu 	list_for_each_entry(dev, &video->video_device_list, entry) {
1776e50b9be1SAaron Lu 		if (acpi_video_should_register_backlight(dev))
177714ca7a47SHans de Goede 			acpi_video_dev_register_backlight(dev);
1778e50b9be1SAaron Lu 	}
177914ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
178014ca7a47SHans de Goede 
178114ca7a47SHans de Goede 	video->backlight_registered = true;
178214ca7a47SHans de Goede 
178314ca7a47SHans de Goede 	video->pm_nb.notifier_call = acpi_video_resume;
178414ca7a47SHans de Goede 	video->pm_nb.priority = 0;
178514ca7a47SHans de Goede 	return register_pm_notifier(&video->pm_nb);
178614ca7a47SHans de Goede }
178714ca7a47SHans de Goede 
178814ca7a47SHans de Goede static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
178914ca7a47SHans de Goede {
179014ca7a47SHans de Goede 	if (device->backlight) {
179114ca7a47SHans de Goede 		backlight_device_unregister(device->backlight);
179214ca7a47SHans de Goede 		device->backlight = NULL;
179314ca7a47SHans de Goede 	}
179414ca7a47SHans de Goede 	if (device->brightness) {
179514ca7a47SHans de Goede 		kfree(device->brightness->levels);
179614ca7a47SHans de Goede 		kfree(device->brightness);
179714ca7a47SHans de Goede 		device->brightness = NULL;
179814ca7a47SHans de Goede 	}
179914ca7a47SHans de Goede 	if (device->cooling_dev) {
180014ca7a47SHans de Goede 		sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
180114ca7a47SHans de Goede 		sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
180214ca7a47SHans de Goede 		thermal_cooling_device_unregister(device->cooling_dev);
180314ca7a47SHans de Goede 		device->cooling_dev = NULL;
180414ca7a47SHans de Goede 	}
180514ca7a47SHans de Goede }
180614ca7a47SHans de Goede 
180714ca7a47SHans de Goede static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
180814ca7a47SHans de Goede {
180914ca7a47SHans de Goede 	struct acpi_video_device *dev;
181014ca7a47SHans de Goede 	int error;
181114ca7a47SHans de Goede 
181214ca7a47SHans de Goede 	if (!video->backlight_registered)
181314ca7a47SHans de Goede 		return 0;
181414ca7a47SHans de Goede 
181514ca7a47SHans de Goede 	error = unregister_pm_notifier(&video->pm_nb);
181614ca7a47SHans de Goede 
181714ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
181814ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
181914ca7a47SHans de Goede 		acpi_video_dev_unregister_backlight(dev);
182014ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
182114ca7a47SHans de Goede 
182214ca7a47SHans de Goede 	video->backlight_registered = false;
182314ca7a47SHans de Goede 
182414ca7a47SHans de Goede 	return error;
182514ca7a47SHans de Goede }
182614ca7a47SHans de Goede 
182714ca7a47SHans de Goede static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
182814ca7a47SHans de Goede {
182914ca7a47SHans de Goede 	acpi_status status;
183014ca7a47SHans de Goede 	struct acpi_device *adev = device->dev;
183114ca7a47SHans de Goede 
183214ca7a47SHans de Goede 	status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
183314ca7a47SHans de Goede 					     acpi_video_device_notify, device);
183414ca7a47SHans de Goede 	if (ACPI_FAILURE(status))
183514ca7a47SHans de Goede 		dev_err(&adev->dev, "Error installing notify handler\n");
183614ca7a47SHans de Goede 	else
183714ca7a47SHans de Goede 		device->flags.notify = 1;
183814ca7a47SHans de Goede }
183914ca7a47SHans de Goede 
184014ca7a47SHans de Goede static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
184114ca7a47SHans de Goede {
184214ca7a47SHans de Goede 	struct input_dev *input;
184314ca7a47SHans de Goede 	struct acpi_video_device *dev;
184414ca7a47SHans de Goede 	int error;
184514ca7a47SHans de Goede 
184614ca7a47SHans de Goede 	video->input = input = input_allocate_device();
184714ca7a47SHans de Goede 	if (!input) {
184814ca7a47SHans de Goede 		error = -ENOMEM;
184914ca7a47SHans de Goede 		goto out;
185014ca7a47SHans de Goede 	}
185114ca7a47SHans de Goede 
185214ca7a47SHans de Goede 	error = acpi_video_bus_start_devices(video);
185314ca7a47SHans de Goede 	if (error)
185414ca7a47SHans de Goede 		goto err_free_input;
185514ca7a47SHans de Goede 
185614ca7a47SHans de Goede 	snprintf(video->phys, sizeof(video->phys),
185714ca7a47SHans de Goede 			"%s/video/input0", acpi_device_hid(video->device));
185814ca7a47SHans de Goede 
185914ca7a47SHans de Goede 	input->name = acpi_device_name(video->device);
186014ca7a47SHans de Goede 	input->phys = video->phys;
186114ca7a47SHans de Goede 	input->id.bustype = BUS_HOST;
186214ca7a47SHans de Goede 	input->id.product = 0x06;
186314ca7a47SHans de Goede 	input->dev.parent = &video->device->dev;
186414ca7a47SHans de Goede 	input->evbit[0] = BIT(EV_KEY);
186514ca7a47SHans de Goede 	set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
186614ca7a47SHans de Goede 	set_bit(KEY_VIDEO_NEXT, input->keybit);
186714ca7a47SHans de Goede 	set_bit(KEY_VIDEO_PREV, input->keybit);
186814ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
186914ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESSUP, input->keybit);
187014ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
187114ca7a47SHans de Goede 	set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
187214ca7a47SHans de Goede 	set_bit(KEY_DISPLAY_OFF, input->keybit);
187314ca7a47SHans de Goede 
187414ca7a47SHans de Goede 	error = input_register_device(input);
187514ca7a47SHans de Goede 	if (error)
187614ca7a47SHans de Goede 		goto err_stop_dev;
187714ca7a47SHans de Goede 
187814ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
187914ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
188014ca7a47SHans de Goede 		acpi_video_dev_add_notify_handler(dev);
188114ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
188214ca7a47SHans de Goede 
188314ca7a47SHans de Goede 	return 0;
188414ca7a47SHans de Goede 
188514ca7a47SHans de Goede err_stop_dev:
188614ca7a47SHans de Goede 	acpi_video_bus_stop_devices(video);
188714ca7a47SHans de Goede err_free_input:
188814ca7a47SHans de Goede 	input_free_device(input);
188914ca7a47SHans de Goede 	video->input = NULL;
189014ca7a47SHans de Goede out:
189114ca7a47SHans de Goede 	return error;
189214ca7a47SHans de Goede }
189314ca7a47SHans de Goede 
189414ca7a47SHans de Goede static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
189514ca7a47SHans de Goede {
189614ca7a47SHans de Goede 	if (dev->flags.notify) {
189714ca7a47SHans de Goede 		acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
189814ca7a47SHans de Goede 					   acpi_video_device_notify);
189914ca7a47SHans de Goede 		dev->flags.notify = 0;
190014ca7a47SHans de Goede 	}
190114ca7a47SHans de Goede }
190214ca7a47SHans de Goede 
190314ca7a47SHans de Goede static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
190414ca7a47SHans de Goede {
190514ca7a47SHans de Goede 	struct acpi_video_device *dev;
190614ca7a47SHans de Goede 
190714ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
190814ca7a47SHans de Goede 	list_for_each_entry(dev, &video->video_device_list, entry)
190914ca7a47SHans de Goede 		acpi_video_dev_remove_notify_handler(dev);
191014ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
191114ca7a47SHans de Goede 
191214ca7a47SHans de Goede 	acpi_video_bus_stop_devices(video);
191314ca7a47SHans de Goede 	input_unregister_device(video->input);
191414ca7a47SHans de Goede 	video->input = NULL;
191514ca7a47SHans de Goede }
191614ca7a47SHans de Goede 
191714ca7a47SHans de Goede static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
191814ca7a47SHans de Goede {
191914ca7a47SHans de Goede 	struct acpi_video_device *dev, *next;
192014ca7a47SHans de Goede 
192114ca7a47SHans de Goede 	mutex_lock(&video->device_list_lock);
192214ca7a47SHans de Goede 	list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
192314ca7a47SHans de Goede 		list_del(&dev->entry);
192414ca7a47SHans de Goede 		kfree(dev);
192514ca7a47SHans de Goede 	}
192614ca7a47SHans de Goede 	mutex_unlock(&video->device_list_lock);
192714ca7a47SHans de Goede 
192814ca7a47SHans de Goede 	return 0;
192914ca7a47SHans de Goede }
193014ca7a47SHans de Goede 
193114ca7a47SHans de Goede static int instance;
193214ca7a47SHans de Goede 
193314ca7a47SHans de Goede static int acpi_video_bus_add(struct acpi_device *device)
193414ca7a47SHans de Goede {
193514ca7a47SHans de Goede 	struct acpi_video_bus *video;
193614ca7a47SHans de Goede 	int error;
193714ca7a47SHans de Goede 	acpi_status status;
193814ca7a47SHans de Goede 
193914ca7a47SHans de Goede 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
194014ca7a47SHans de Goede 				device->parent->handle, 1,
194114ca7a47SHans de Goede 				acpi_video_bus_match, NULL,
194214ca7a47SHans de Goede 				device, NULL);
194314ca7a47SHans de Goede 	if (status == AE_ALREADY_EXISTS) {
194414ca7a47SHans de Goede 		printk(KERN_WARNING FW_BUG
194514ca7a47SHans de Goede 			"Duplicate ACPI video bus devices for the"
194614ca7a47SHans de Goede 			" same VGA controller, please try module "
194714ca7a47SHans de Goede 			"parameter \"video.allow_duplicates=1\""
194814ca7a47SHans de Goede 			"if the current driver doesn't work.\n");
194914ca7a47SHans de Goede 		if (!allow_duplicates)
195014ca7a47SHans de Goede 			return -ENODEV;
195114ca7a47SHans de Goede 	}
195214ca7a47SHans de Goede 
195314ca7a47SHans de Goede 	video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
195414ca7a47SHans de Goede 	if (!video)
195514ca7a47SHans de Goede 		return -ENOMEM;
195614ca7a47SHans de Goede 
195714ca7a47SHans de Goede 	/* a hack to fix the duplicate name "VID" problem on T61 */
195814ca7a47SHans de Goede 	if (!strcmp(device->pnp.bus_id, "VID")) {
195914ca7a47SHans de Goede 		if (instance)
196014ca7a47SHans de Goede 			device->pnp.bus_id[3] = '0' + instance;
196114ca7a47SHans de Goede 		instance++;
196214ca7a47SHans de Goede 	}
196314ca7a47SHans de Goede 	/* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
196414ca7a47SHans de Goede 	if (!strcmp(device->pnp.bus_id, "VGA")) {
196514ca7a47SHans de Goede 		if (instance)
196614ca7a47SHans de Goede 			device->pnp.bus_id[3] = '0' + instance;
196714ca7a47SHans de Goede 		instance++;
196814ca7a47SHans de Goede 	}
196914ca7a47SHans de Goede 
197014ca7a47SHans de Goede 	video->device = device;
197114ca7a47SHans de Goede 	strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
197214ca7a47SHans de Goede 	strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
197314ca7a47SHans de Goede 	device->driver_data = video;
197414ca7a47SHans de Goede 
197514ca7a47SHans de Goede 	acpi_video_bus_find_cap(video);
197614ca7a47SHans de Goede 	error = acpi_video_bus_check(video);
197714ca7a47SHans de Goede 	if (error)
197814ca7a47SHans de Goede 		goto err_free_video;
197914ca7a47SHans de Goede 
198014ca7a47SHans de Goede 	mutex_init(&video->device_list_lock);
198114ca7a47SHans de Goede 	INIT_LIST_HEAD(&video->video_device_list);
198214ca7a47SHans de Goede 
198314ca7a47SHans de Goede 	error = acpi_video_bus_get_devices(video, device);
198414ca7a47SHans de Goede 	if (error)
198514ca7a47SHans de Goede 		goto err_put_video;
198614ca7a47SHans de Goede 
198714ca7a47SHans de Goede 	printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
198814ca7a47SHans de Goede 	       ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
198914ca7a47SHans de Goede 	       video->flags.multihead ? "yes" : "no",
199014ca7a47SHans de Goede 	       video->flags.rom ? "yes" : "no",
199114ca7a47SHans de Goede 	       video->flags.post ? "yes" : "no");
199214ca7a47SHans de Goede 	mutex_lock(&video_list_lock);
199314ca7a47SHans de Goede 	list_add_tail(&video->entry, &video_bus_head);
199414ca7a47SHans de Goede 	mutex_unlock(&video_list_lock);
199514ca7a47SHans de Goede 
199614ca7a47SHans de Goede 	acpi_video_bus_register_backlight(video);
199714ca7a47SHans de Goede 	acpi_video_bus_add_notify_handler(video);
199814ca7a47SHans de Goede 
199914ca7a47SHans de Goede 	return 0;
200014ca7a47SHans de Goede 
200114ca7a47SHans de Goede err_put_video:
200214ca7a47SHans de Goede 	acpi_video_bus_put_devices(video);
200314ca7a47SHans de Goede 	kfree(video->attached_array);
200414ca7a47SHans de Goede err_free_video:
200514ca7a47SHans de Goede 	kfree(video);
200614ca7a47SHans de Goede 	device->driver_data = NULL;
200714ca7a47SHans de Goede 
200814ca7a47SHans de Goede 	return error;
200914ca7a47SHans de Goede }
201014ca7a47SHans de Goede 
201114ca7a47SHans de Goede static int acpi_video_bus_remove(struct acpi_device *device)
201214ca7a47SHans de Goede {
201314ca7a47SHans de Goede 	struct acpi_video_bus *video = NULL;
201414ca7a47SHans de Goede 
201514ca7a47SHans de Goede 
201614ca7a47SHans de Goede 	if (!device || !acpi_driver_data(device))
201714ca7a47SHans de Goede 		return -EINVAL;
201814ca7a47SHans de Goede 
201914ca7a47SHans de Goede 	video = acpi_driver_data(device);
202014ca7a47SHans de Goede 
202114ca7a47SHans de Goede 	acpi_video_bus_remove_notify_handler(video);
202214ca7a47SHans de Goede 	acpi_video_bus_unregister_backlight(video);
202314ca7a47SHans de Goede 	acpi_video_bus_put_devices(video);
202414ca7a47SHans de Goede 
202514ca7a47SHans de Goede 	mutex_lock(&video_list_lock);
202614ca7a47SHans de Goede 	list_del(&video->entry);
202714ca7a47SHans de Goede 	mutex_unlock(&video_list_lock);
202814ca7a47SHans de Goede 
202914ca7a47SHans de Goede 	kfree(video->attached_array);
203014ca7a47SHans de Goede 	kfree(video);
203114ca7a47SHans de Goede 
203214ca7a47SHans de Goede 	return 0;
203314ca7a47SHans de Goede }
203414ca7a47SHans de Goede 
203514ca7a47SHans de Goede static int __init is_i740(struct pci_dev *dev)
203614ca7a47SHans de Goede {
203714ca7a47SHans de Goede 	if (dev->device == 0x00D1)
203814ca7a47SHans de Goede 		return 1;
203914ca7a47SHans de Goede 	if (dev->device == 0x7000)
204014ca7a47SHans de Goede 		return 1;
204114ca7a47SHans de Goede 	return 0;
204214ca7a47SHans de Goede }
204314ca7a47SHans de Goede 
204414ca7a47SHans de Goede static int __init intel_opregion_present(void)
204514ca7a47SHans de Goede {
204614ca7a47SHans de Goede 	int opregion = 0;
204714ca7a47SHans de Goede 	struct pci_dev *dev = NULL;
204814ca7a47SHans de Goede 	u32 address;
204914ca7a47SHans de Goede 
205014ca7a47SHans de Goede 	for_each_pci_dev(dev) {
205114ca7a47SHans de Goede 		if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
205214ca7a47SHans de Goede 			continue;
205314ca7a47SHans de Goede 		if (dev->vendor != PCI_VENDOR_ID_INTEL)
205414ca7a47SHans de Goede 			continue;
205514ca7a47SHans de Goede 		/* We don't want to poke around undefined i740 registers */
205614ca7a47SHans de Goede 		if (is_i740(dev))
205714ca7a47SHans de Goede 			continue;
205814ca7a47SHans de Goede 		pci_read_config_dword(dev, 0xfc, &address);
205914ca7a47SHans de Goede 		if (!address)
206014ca7a47SHans de Goede 			continue;
206114ca7a47SHans de Goede 		opregion = 1;
206214ca7a47SHans de Goede 	}
206314ca7a47SHans de Goede 	return opregion;
206414ca7a47SHans de Goede }
206514ca7a47SHans de Goede 
206614ca7a47SHans de Goede int acpi_video_register(void)
206714ca7a47SHans de Goede {
20682a8b18e9SHans de Goede 	int ret = 0;
206914ca7a47SHans de Goede 
2070970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2071970530cdSHans de Goede 	if (register_count) {
207214ca7a47SHans de Goede 		/*
207314ca7a47SHans de Goede 		 * if the function of acpi_video_register is already called,
207414ca7a47SHans de Goede 		 * don't register the acpi_vide_bus again and return no error.
207514ca7a47SHans de Goede 		 */
20762a8b18e9SHans de Goede 		goto leave;
207714ca7a47SHans de Goede 	}
207814ca7a47SHans de Goede 
20797ee33baaSHans de Goede 	dmi_check_system(video_dmi_table);
20807ee33baaSHans de Goede 
208114ca7a47SHans de Goede 	ret = acpi_bus_register_driver(&acpi_video_bus);
208214ca7a47SHans de Goede 	if (ret)
20832a8b18e9SHans de Goede 		goto leave;
208414ca7a47SHans de Goede 
208514ca7a47SHans de Goede 	/*
208614ca7a47SHans de Goede 	 * When the acpi_video_bus is loaded successfully, increase
208714ca7a47SHans de Goede 	 * the counter reference.
208814ca7a47SHans de Goede 	 */
2089970530cdSHans de Goede 	register_count = 1;
209014ca7a47SHans de Goede 
20912a8b18e9SHans de Goede leave:
2092970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
20932a8b18e9SHans de Goede 	return ret;
209414ca7a47SHans de Goede }
209514ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_register);
209614ca7a47SHans de Goede 
209714ca7a47SHans de Goede void acpi_video_unregister(void)
209814ca7a47SHans de Goede {
2099970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2100970530cdSHans de Goede 	if (register_count) {
210114ca7a47SHans de Goede 		acpi_bus_unregister_driver(&acpi_video_bus);
2102970530cdSHans de Goede 		register_count = 0;
21032a8b18e9SHans de Goede 	}
2104970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
210514ca7a47SHans de Goede }
210614ca7a47SHans de Goede EXPORT_SYMBOL(acpi_video_unregister);
210714ca7a47SHans de Goede 
210814ca7a47SHans de Goede void acpi_video_unregister_backlight(void)
210914ca7a47SHans de Goede {
211014ca7a47SHans de Goede 	struct acpi_video_bus *video;
211114ca7a47SHans de Goede 
2112970530cdSHans de Goede 	mutex_lock(&register_count_mutex);
2113970530cdSHans de Goede 	if (register_count) {
211414ca7a47SHans de Goede 		mutex_lock(&video_list_lock);
211514ca7a47SHans de Goede 		list_for_each_entry(video, &video_bus_head, entry)
211614ca7a47SHans de Goede 			acpi_video_bus_unregister_backlight(video);
211714ca7a47SHans de Goede 		mutex_unlock(&video_list_lock);
211814ca7a47SHans de Goede 	}
2119970530cdSHans de Goede 	mutex_unlock(&register_count_mutex);
21202a8b18e9SHans de Goede }
212114ca7a47SHans de Goede 
212290b066b1SHans de Goede bool acpi_video_handles_brightness_key_presses(void)
212390b066b1SHans de Goede {
212490b066b1SHans de Goede 	bool have_video_busses;
212590b066b1SHans de Goede 
212690b066b1SHans de Goede 	mutex_lock(&video_list_lock);
212790b066b1SHans de Goede 	have_video_busses = !list_empty(&video_bus_head);
212890b066b1SHans de Goede 	mutex_unlock(&video_list_lock);
212990b066b1SHans de Goede 
213005bc59a0SHans de Goede 	return have_video_busses &&
213105bc59a0SHans de Goede 	       (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
213290b066b1SHans de Goede }
213390b066b1SHans de Goede EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
213490b066b1SHans de Goede 
213514ca7a47SHans de Goede /*
213614ca7a47SHans de Goede  * This is kind of nasty. Hardware using Intel chipsets may require
213714ca7a47SHans de Goede  * the video opregion code to be run first in order to initialise
213814ca7a47SHans de Goede  * state before any ACPI video calls are made. To handle this we defer
213914ca7a47SHans de Goede  * registration of the video class until the opregion code has run.
214014ca7a47SHans de Goede  */
214114ca7a47SHans de Goede 
214214ca7a47SHans de Goede static int __init acpi_video_init(void)
214314ca7a47SHans de Goede {
214414ca7a47SHans de Goede 	/*
214514ca7a47SHans de Goede 	 * Let the module load even if ACPI is disabled (e.g. due to
214614ca7a47SHans de Goede 	 * a broken BIOS) so that i915.ko can still be loaded on such
214714ca7a47SHans de Goede 	 * old systems without an AcpiOpRegion.
214814ca7a47SHans de Goede 	 *
214914ca7a47SHans de Goede 	 * acpi_video_register() will report -ENODEV later as well due
215014ca7a47SHans de Goede 	 * to acpi_disabled when i915.ko tries to register itself afterwards.
215114ca7a47SHans de Goede 	 */
215214ca7a47SHans de Goede 	if (acpi_disabled)
215314ca7a47SHans de Goede 		return 0;
215414ca7a47SHans de Goede 
215514ca7a47SHans de Goede 	if (intel_opregion_present())
215614ca7a47SHans de Goede 		return 0;
215714ca7a47SHans de Goede 
215814ca7a47SHans de Goede 	return acpi_video_register();
215914ca7a47SHans de Goede }
216014ca7a47SHans de Goede 
216114ca7a47SHans de Goede static void __exit acpi_video_exit(void)
216214ca7a47SHans de Goede {
216393a291dfSHans de Goede 	acpi_video_detect_exit();
216414ca7a47SHans de Goede 	acpi_video_unregister();
216514ca7a47SHans de Goede 
216614ca7a47SHans de Goede 	return;
216714ca7a47SHans de Goede }
216814ca7a47SHans de Goede 
216914ca7a47SHans de Goede module_init(acpi_video_init);
217014ca7a47SHans de Goede module_exit(acpi_video_exit);
2171