blob: 2971154fdd62f2f08954d51874591a3f0bce132d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Felipe Contreras21fcb342013-08-01 18:43:59 -05002 * video.c - ACPI Video Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050028#include <linux/mutex.h>
Luming Yue9dab192007-08-20 18:23:53 +080029#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080030#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080031#include <linux/thermal.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050032#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000033#include <linux/pci.h>
34#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Len Browneb27cae2009-07-06 23:40:19 -040036#include <linux/dmi.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020037#include <linux/suspend.h>
Lv Zheng8b484632013-12-03 08:49:16 +080038#include <linux/acpi.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050039#include <acpi/video.h>
Lv Zheng8b484632013-12-03 08:49:16 +080040#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Hans de Goede14ca7a472015-06-16 16:27:47 +020042#define PREFIX "ACPI: "
Rafael J. Wysocki8c5bd7a2013-07-18 02:08:06 +020043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_VIDEO_BUS_NAME "Video Bus"
45#define ACPI_VIDEO_DEVICE_NAME "Video Device"
46#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
47#define ACPI_VIDEO_NOTIFY_PROBE 0x81
48#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
49#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
50#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
51
Thomas Tuttlef4715182006-12-19 12:56:14 -080052#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
53#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
54#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
55#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
56#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Yu Luming2f3d0002006-11-11 02:40:34 +080058#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050061ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Len Brownf52fd662007-02-12 22:42:12 -050063MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050064MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_LICENSE("GPL");
66
Rafael J. Wysocki28437682014-07-14 19:35:45 +020067static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080068module_param(brightness_switch_enabled, bool, 0644);
69
Zhang Ruic504f8c2009-12-30 15:59:23 +080070/*
71 * By default, we don't allow duplicate ACPI video bus devices
72 * under the same VGA controller
73 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080075module_param(allow_duplicates, bool, 0644);
76
Hans de Goede654a1822015-06-09 10:32:25 +020077static int disable_backlight_sysfs_if = -1;
78module_param(disable_backlight_sysfs_if, int, 0444);
79
Hans de Goede05bc59a2015-12-22 19:09:51 +010080#define REPORT_OUTPUT_KEY_EVENTS 0x01
81#define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
82static int report_key_events = -1;
83module_param(report_key_events, int, 0644);
84MODULE_PARM_DESC(report_key_events,
85 "0: none, 1: output changes, 2: brightness changes, 3: all");
86
Aaron Lue50b9be2015-10-28 15:09:23 +080087static bool device_id_scheme = false;
88module_param(device_id_scheme, bool, 0444);
89
90static bool only_lcd = false;
91module_param(only_lcd, bool, 0444);
92
Felipe Contreras915ea7e2013-08-03 22:12:50 +020093static int register_count;
Hans de Goede2a8b18e2015-06-16 16:27:54 +020094static DEFINE_MUTEX(register_count_mutex);
Aaron Lu67b662e2013-10-11 21:27:44 +080095static struct mutex video_list_lock;
96static struct list_head video_bus_head;
Len Brown4be44fc2005-08-05 00:44:28 -040097static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010098static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000099static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Hans de Goede93a291d2015-06-16 16:27:52 +0200100void acpi_video_detect_exit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200102static const struct acpi_device_id video_device_ids[] = {
103 {ACPI_VIDEO_HID, 0},
104 {"", 0},
105};
106MODULE_DEVICE_TABLE(acpi, video_device_ids);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500109 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200111 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .ops = {
113 .add = acpi_video_bus_add,
114 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000115 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400116 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u8 multihead:1; /* can switch video heads */
121 u8 rom:1; /* can retrieve a video rom */
122 u8 post:1; /* can configure the head to */
123 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500127 u8 _DOS:1; /* Enable/Disable output switching */
128 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
129 u8 _ROM:1; /* Get ROM Data */
130 u8 _GPD:1; /* Get POST Device */
131 u8 _SPD:1; /* Set POST Device */
132 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400133 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136struct acpi_video_device_attrib {
137 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500138 u32 display_port_attachment:4; /* This field differentiates the display type */
139 u32 display_type:4; /* Describe the specific type in use */
140 u32 vendor_specific:4; /* Chipset Vendor Specific */
141 u32 bios_can_detect:1; /* BIOS can detect the device */
142 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400143 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500144 u32 pipe_id:3; /* For VGA multiple-head devices. */
145 u32 reserved:10; /* Must be 0 */
146 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_video_enumerated_device {
150 union {
151 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } value;
154 struct acpi_video_device *bind_info;
155};
156
157struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400158 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200159 bool backlight_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400160 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800163 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400164 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500167 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800168 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800169 struct input_dev *input;
170 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200171 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
174struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400175 u8 crt:1;
176 u8 lcd:1;
177 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500178 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400179 u8 bios:1;
180 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000181 u8 notify:1;
182 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
185struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500186 u8 _ADR:1; /* Return the unique ID */
187 u8 _BCL:1; /* Query list of brightness control levels supported */
188 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800189 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800190 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500191 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Zhang Ruid32f6942009-03-18 16:27:12 +0800194struct acpi_video_brightness_flags {
195 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500196 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800197 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 int curr;
202 int count;
203 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800204 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400208 unsigned long device_id;
209 struct acpi_video_device_flags flags;
210 struct acpi_video_device_cap cap;
211 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200212 struct delayed_work switch_brightness_work;
213 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400214 struct acpi_video_bus *video;
215 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800217 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400218 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100221static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "motherboard VGA device",
223 "PCI VGA device",
224 "AGP VGA device",
225 "UNKNOWN",
226};
227
Len Brown4be44fc2005-08-05 00:44:28 -0400228static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
229static void acpi_video_device_rebind(struct acpi_video_bus *video);
230static void acpi_video_device_bind(struct acpi_video_bus *video,
231 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800233static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
234 int level);
235static int acpi_video_device_lcd_get_level_current(
236 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000237 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400238static int acpi_video_get_next_level(struct acpi_video_device *device,
239 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200240static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Felipe Contreras21fcb342013-08-01 18:43:59 -0500242/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800243static int acpi_video_get_brightness(struct backlight_device *bd)
244{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400245 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000246 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200247 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800248
Danny Baumanna89803d2013-03-19 16:22:50 +0000249 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800250 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000251 for (i = 2; i < vd->brightness->count; i++) {
252 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500253 /*
254 * The first two entries are special - see page 575
255 * of the ACPI spec 3.0
256 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200257 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000258 }
259 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800260}
261
262static int acpi_video_set_brightness(struct backlight_device *bd)
263{
Zhang Rui24450c72009-03-18 16:27:10 +0800264 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200265 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800266
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200267 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800268 return acpi_video_device_lcd_set_level(vd,
269 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800270}
271
Lionel Debrouxacc24722010-11-16 14:14:02 +0100272static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000273 .get_brightness = acpi_video_get_brightness,
274 .update_status = acpi_video_set_brightness,
275};
276
Zhang Rui702ed512008-01-17 15:51:22 +0800277/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400278static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000279 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800280{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400281 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800282 struct acpi_video_device *video = acpi_driver_data(device);
283
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000284 *state = video->brightness->count - 3;
285 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800286}
287
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400288static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000289 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800290{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400291 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800292 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400293 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000294 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800295
Danny Baumanna89803d2013-03-19 16:22:50 +0000296 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800297 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000298 for (offset = 2; offset < video->brightness->count; offset++)
299 if (level == video->brightness->levels[offset]) {
300 *state = video->brightness->count - offset - 1;
301 return 0;
302 }
Zhang Rui702ed512008-01-17 15:51:22 +0800303
304 return -EINVAL;
305}
306
307static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400308video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800309{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400310 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800311 struct acpi_video_device *video = acpi_driver_data(device);
312 int level;
313
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200314 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800315 return -EINVAL;
316
317 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200318 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800319 return acpi_video_device_lcd_set_level(video, level);
320}
321
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400322static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800323 .get_max_state = video_get_max_state,
324 .get_cur_state = video_get_cur_state,
325 .set_cur_state = video_set_cur_state,
326};
327
Felipe Contreras21fcb342013-08-01 18:43:59 -0500328/*
329 * --------------------------------------------------------------------------
330 * Video Management
331 * --------------------------------------------------------------------------
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static int
Len Brown4be44fc2005-08-05 00:44:28 -0400335acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
336 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Len Brown4be44fc2005-08-05 00:44:28 -0400338 int status;
339 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
340 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 *levels = NULL;
344
Patrick Mochel90130262006-05-19 16:54:48 -0400345 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400347 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400348 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500349 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400350 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 status = -EFAULT;
352 goto err;
353 }
354
355 *levels = obj;
356
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200359err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800360 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Patrick Mocheld550d982006-06-27 00:41:40 -0400362 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static int
Len Brown4be44fc2005-08-05 00:44:28 -0400366acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Zhang Rui24450c72009-03-18 16:27:10 +0800368 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800369 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jiang Liu0db98202013-06-29 00:24:39 +0800371 status = acpi_execute_simple_method(device->dev->handle,
372 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800373 if (ACPI_FAILURE(status)) {
374 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
375 return -EIO;
376 }
377
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400378 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800379 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800380 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800381 if (device->backlight)
382 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800383 return 0;
384 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800385
Zhang Rui24450c72009-03-18 16:27:10 +0800386 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Zhang Rui45cb50e2009-04-24 12:13:18 -0400390/*
391 * For some buggy _BQC methods, we need to add a constant value to
392 * the _BQC return value to get the actual current brightness level
393 */
394
395static int bqc_offset_aml_bug_workaround;
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200396static int video_set_bqc_offset(const struct dmi_system_id *d)
Zhang Rui45cb50e2009-04-24 12:13:18 -0400397{
398 bqc_offset_aml_bug_workaround = 9;
399 return 0;
400}
401
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200402static int video_disable_backlight_sysfs_if(
Hans de Goede654a1822015-06-09 10:32:25 +0200403 const struct dmi_system_id *d)
404{
405 if (disable_backlight_sysfs_if == -1)
406 disable_backlight_sysfs_if = 1;
407 return 0;
408}
409
Aaron Lue50b9be2015-10-28 15:09:23 +0800410static int video_set_device_id_scheme(const struct dmi_system_id *d)
411{
412 device_id_scheme = true;
413 return 0;
414}
415
416static int video_enable_only_lcd(const struct dmi_system_id *d)
417{
418 only_lcd = true;
419 return 0;
420}
421
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200422static struct dmi_system_id video_dmi_table[] = {
Zhang Rui45cb50e2009-04-24 12:13:18 -0400423 /*
424 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
425 */
426 {
427 .callback = video_set_bqc_offset,
428 .ident = "Acer Aspire 5720",
429 .matches = {
430 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
431 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
432 },
433 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400434 {
435 .callback = video_set_bqc_offset,
436 .ident = "Acer Aspire 5710Z",
437 .matches = {
438 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
439 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
440 },
441 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400442 {
443 .callback = video_set_bqc_offset,
444 .ident = "eMachines E510",
445 .matches = {
446 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
447 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
448 },
449 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400450 {
451 .callback = video_set_bqc_offset,
452 .ident = "Acer Aspire 5315",
453 .matches = {
454 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
455 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
456 },
457 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800458 {
459 .callback = video_set_bqc_offset,
460 .ident = "Acer Aspire 7720",
461 .matches = {
462 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
463 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
464 },
465 },
Hans de Goede5f240792014-08-28 10:20:46 +0200466
467 /*
Hans de Goede654a1822015-06-09 10:32:25 +0200468 * Some machines have a broken acpi-video interface for brightness
469 * control, but still need an acpi_video_device_lcd_set_level() call
470 * on resume to turn the backlight power on. We Enable backlight
471 * control on these systems, but do not register a backlight sysfs
472 * as brightness control does not work.
473 */
474 {
475 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
476 .callback = video_disable_backlight_sysfs_if,
477 .ident = "Toshiba Portege R830",
478 .matches = {
479 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
480 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
481 },
482 },
Aaron Lue50b9be2015-10-28 15:09:23 +0800483 /*
484 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
485 * but the IDs actually follow the Device ID Scheme.
486 */
487 {
488 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
489 .callback = video_set_device_id_scheme,
490 .ident = "ESPRIMO Mobile M9410",
491 .matches = {
492 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
493 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
494 },
495 },
496 /*
497 * Some machines have multiple video output devices, but only the one
498 * that is the type of LCD can do the backlight control so we should not
499 * register backlight interface for other video output devices.
500 */
501 {
502 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
503 .callback = video_enable_only_lcd,
504 .ident = "ESPRIMO Mobile M9410",
505 .matches = {
506 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
507 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
508 },
509 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400510 {}
511};
512
Danny Baumann994fa632013-03-19 16:22:52 +0000513static unsigned long long
514acpi_video_bqc_value_to_level(struct acpi_video_device *device,
515 unsigned long long bqc_value)
516{
517 unsigned long long level;
518
519 if (device->brightness->flags._BQC_use_index) {
520 /*
521 * _BQC returns an index that doesn't account for
522 * the first 2 items with special meaning, so we need
523 * to compensate for that by offsetting ourselves
524 */
525 if (device->brightness->flags._BCL_reversed)
526 bqc_value = device->brightness->count - 3 - bqc_value;
527
528 level = device->brightness->levels[bqc_value + 2];
529 } else {
530 level = bqc_value;
531 }
532
533 level += bqc_offset_aml_bug_workaround;
534
535 return level;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538static int
Len Brown4be44fc2005-08-05 00:44:28 -0400539acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000540 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Zhang Ruic8890f92009-03-18 16:27:08 +0800542 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800543 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800544
Zhang Ruic60d6382009-03-18 16:27:18 +0800545 if (device->cap._BQC || device->cap._BCQ) {
546 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
547
548 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800549 NULL, level);
550 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000551 if (raw) {
552 /*
553 * Caller has indicated he wants the raw
554 * value returned by _BQC, so don't furtherly
555 * mess with the value.
556 */
557 return 0;
558 }
559
Danny Baumann994fa632013-03-19 16:22:52 +0000560 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800561
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800562 for (i = 2; i < device->brightness->count; i++)
563 if (device->brightness->levels[i] == *level) {
564 device->brightness->curr = *level;
565 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200566 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000567 /*
568 * BQC returned an invalid level.
569 * Stop using it.
570 */
571 ACPI_WARNING((AE_INFO,
572 "%s returned an invalid level",
573 buf));
574 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800575 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500576 /*
577 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800578 * should we return an error or ignore this failure?
579 * dev->brightness->curr is a cached value which stores
580 * the correct current backlight level in most cases.
581 * ACPI video backlight still works w/ buggy _BQC.
582 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
583 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800584 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
585 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800586 }
587 }
588
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400589 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static int
Len Brown4be44fc2005-08-05 00:44:28 -0400594acpi_video_device_EDID(struct acpi_video_device *device,
595 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Len Brown4be44fc2005-08-05 00:44:28 -0400597 int status;
598 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
599 union acpi_object *obj;
600 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
601 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 *edid = NULL;
605
606 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (length == 128)
609 arg0.integer.value = 1;
610 else if (length == 256)
611 arg0.integer.value = 2;
612 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Patrick Mochel90130262006-05-19 16:54:48 -0400615 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200619 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (obj && obj->type == ACPI_TYPE_BUFFER)
622 *edid = obj;
623 else {
Len Brown64684632006-06-26 23:41:38 -0400624 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 status = -EFAULT;
626 kfree(obj);
627 }
628
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/* bus */
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500636 * video : video bus device pointer
637 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * 0. The system BIOS should NOT automatically switch(toggle)
639 * the active display output.
640 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100641 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * 2. The _DGS value should be locked.
643 * 3. The system BIOS should not automatically switch (toggle) the
644 * active display output, but instead generate the display switch
645 * event notify code.
646 * lcd_flag :
647 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100648 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500649 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100650 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500651 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400652 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654
655static int
Len Brown4be44fc2005-08-05 00:44:28 -0400656acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Igor Murzovea9f8852012-03-30 21:32:08 +0400658 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Zhang Ruib0373842012-06-20 09:48:43 +0800660 if (!video->cap._DOS)
661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Igor Murzovea9f8852012-03-30 21:32:08 +0400663 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
664 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800665 video->dos_setting = (lcd_flag << 2) | bios_flag;
666 status = acpi_execute_simple_method(video->device->handle, "_DOS",
667 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400668 if (ACPI_FAILURE(status))
669 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Igor Murzovea9f8852012-03-30 21:32:08 +0400671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500675 * Simple comparison function used to sort backlight levels.
676 */
677
678static int
679acpi_video_cmp_level(const void *a, const void *b)
680{
681 return *(int *)a - *(int *)b;
682}
683
684/*
Aaron Lua50188d2013-04-22 14:08:32 +0200685 * Decides if _BQC/_BCQ for this system is usable
686 *
687 * We do this by changing the level first and then read out the current
688 * brightness level, if the value does not match, find out if it is using
689 * index. If not, clear the _BQC/_BCQ capability.
690 */
691static int acpi_video_bqc_quirk(struct acpi_video_device *device,
692 int max_level, int current_level)
693{
694 struct acpi_video_device_brightness *br = device->brightness;
695 int result;
696 unsigned long long level;
697 int test_level;
698
699 /* don't mess with existing known broken systems */
700 if (bqc_offset_aml_bug_workaround)
701 return 0;
702
703 /*
704 * Some systems always report current brightness level as maximum
705 * through _BQC, we need to test another value for them.
706 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200707 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200708
709 result = acpi_video_device_lcd_set_level(device, test_level);
710 if (result)
711 return result;
712
713 result = acpi_video_device_lcd_get_level_current(device, &level, true);
714 if (result)
715 return result;
716
717 if (level != test_level) {
718 /* buggy _BQC found, need to find out if it uses index */
719 if (level < br->count) {
720 if (br->flags._BCL_reversed)
721 level = br->count - 3 - level;
722 if (br->levels[level + 2] == test_level)
723 br->flags._BQC_use_index = 1;
724 }
725
726 if (!br->flags._BQC_use_index)
727 device->cap._BQC = device->cap._BCQ = 0;
728 }
729
730 return 0;
731}
732
733
734/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500735 * Arg:
736 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
738 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100739 * Maximum brightness level
740 *
741 * Allocate and initialize device->brightness.
742 */
743
744static int
745acpi_video_init_brightness(struct acpi_video_device *device)
746{
747 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800748 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800749 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100750 union acpi_object *o;
751 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800752 int result = -EINVAL;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100753 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100754
755 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
756 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
757 "LCD brightness level\n"));
758 goto out;
759 }
760
761 if (obj->package.count < 2)
762 goto out;
763
764 br = kzalloc(sizeof(*br), GFP_KERNEL);
765 if (!br) {
766 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800767 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100768 goto out;
769 }
770
Zhang Ruid32f6942009-03-18 16:27:12 +0800771 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100772 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800773 if (!br->levels) {
774 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100775 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800776 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100777
778 for (i = 0; i < obj->package.count; i++) {
779 o = (union acpi_object *)&obj->package.elements[i];
780 if (o->type != ACPI_TYPE_INTEGER) {
781 printk(KERN_ERR PREFIX "Invalid data\n");
782 continue;
783 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100784 value = (u32) o->integer.value;
785 /* Skip duplicate entries */
786 if (count > 2 && br->levels[count - 1] == value)
787 continue;
788
789 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100790
791 if (br->levels[count] > max_level)
792 max_level = br->levels[count];
793 count++;
794 }
795
Zhang Ruid32f6942009-03-18 16:27:12 +0800796 /*
797 * some buggy BIOS don't export the levels
798 * when machine is on AC/Battery in _BCL package.
799 * In this case, the first two elements in _BCL packages
800 * are also supported brightness levels that OS should take care of.
801 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800802 for (i = 2; i < count; i++) {
803 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800804 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800805 if (br->levels[i] == br->levels[1])
806 level_ac_battery++;
807 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500808
Zhang Ruid32f6942009-03-18 16:27:12 +0800809 if (level_ac_battery < 2) {
810 level_ac_battery = 2 - level_ac_battery;
811 br->flags._BCL_no_ac_battery_levels = 1;
812 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
813 br->levels[i] = br->levels[i - level_ac_battery];
814 count += level_ac_battery;
815 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000816 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800817
Zhang Ruid80fb992009-03-18 16:27:14 +0800818 /* Check if the _BCL package is in a reversed order */
819 if (max_level == br->levels[2]) {
820 br->flags._BCL_reversed = 1;
821 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
822 acpi_video_cmp_level, NULL);
823 } else if (max_level != br->levels[count - 1])
824 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000825 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100826
827 br->count = count;
828 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800829
Zhang Rui1a7c6182009-03-18 16:27:16 +0800830 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400831 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800832
833 if (!device->cap._BQC)
834 goto set_level;
835
Danny Baumanna89803d2013-03-19 16:22:50 +0000836 result = acpi_video_device_lcd_get_level_current(device,
837 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800838 if (result)
839 goto out_free_levels;
840
Aaron Lua50188d2013-04-22 14:08:32 +0200841 result = acpi_video_bqc_quirk(device, max_level, level_old);
842 if (result)
843 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800844 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200845 * cap._BQC may get cleared due to _BQC is found to be broken
846 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800847 */
Aaron Lua50188d2013-04-22 14:08:32 +0200848 if (!device->cap._BQC)
849 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800850
Aaron Lu545ef362013-11-15 14:39:12 +0800851 level = acpi_video_bqc_value_to_level(device, level_old);
852 /*
853 * On some buggy laptops, _BQC returns an uninitialized
854 * value when invoked for the first time, i.e.
855 * level_old is invalid (no matter whether it's a level
856 * or an index). Set the backlight to max_level in this case.
857 */
858 for (i = 2; i < br->count; i++)
859 if (level == br->levels[i])
860 break;
861 if (i == br->count || !level)
862 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800863
Zhang Ruie047cca2009-04-09 14:24:35 +0800864set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400865 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800866 if (result)
867 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800868
Zhang Ruid32f6942009-03-18 16:27:12 +0800869 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
870 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100871 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800872 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100873
874out_free_levels:
875 kfree(br->levels);
876out_free:
877 kfree(br);
878out:
879 device->brightness = NULL;
880 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800881 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100882}
883
884/*
885 * Arg:
886 * device : video output device (LCD, CRT, ..)
887 *
888 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500889 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100891 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * device.
893 */
894
Len Brown4be44fc2005-08-05 00:44:28 -0400895static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Jiang Liu952c63e2013-06-29 00:24:38 +0800897 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800899 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400900 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800901 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400902 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800903 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800904 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800905 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800906 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
907 device->cap._BCQ = 1;
908 }
909
Jiang Liu952c63e2013-06-29 00:24:38 +0800910 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400911 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500915 * Arg:
916 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500919 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100921 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923
Len Brown4be44fc2005-08-05 00:44:28 -0400924static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Jiang Liu952c63e2013-06-29 00:24:38 +0800926 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800928 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800930 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800932 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800934 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800936 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
941 * Check whether the video bus device has required AML method to
942 * support the desired features
943 */
944
Len Brown4be44fc2005-08-05 00:44:28 -0400945static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Len Brown4be44fc2005-08-05 00:44:28 -0400947 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000948 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400951 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000953 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200954 if (!dev)
955 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000956 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200957
Felipe Contreras21fcb342013-08-01 18:43:59 -0500958 /*
959 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * to check well known required nodes.
961 */
962
Julius Volz98fb8fe2007-02-20 16:38:40 +0100963 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200964 if (video->cap._DOS || video->cap._DOD) {
965 if (!video->cap._DOS) {
966 printk(KERN_WARNING FW_BUG
967 "ACPI(%s) defines _DOD but not _DOS\n",
968 acpi_device_bid(video->device));
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 video->flags.multihead = 1;
971 status = 0;
972 }
973
Julius Volz98fb8fe2007-02-20 16:38:40 +0100974 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400975 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 video->flags.rom = 1;
977 status = 0;
978 }
979
Julius Volz98fb8fe2007-02-20 16:38:40 +0100980 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400981 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 video->flags.post = 1;
983 status = 0;
984 }
985
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Felipe Contreras21fcb342013-08-01 18:43:59 -0500989/*
990 * --------------------------------------------------------------------------
991 * Driver Interface
992 * --------------------------------------------------------------------------
993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200996static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -0500997acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
998{
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500999 struct acpi_video_enumerated_device *ids;
1000 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001001
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001002 for (i = 0; i < video->attached_count; i++) {
1003 ids = &video->attached_array[i];
1004 if ((ids->value.int_val & 0xffff) == device_id)
1005 return &ids->value.attrib;
1006 }
1007
Rui Zhang82cae992007-01-03 23:40:53 -05001008 return NULL;
1009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001012acpi_video_get_device_type(struct acpi_video_bus *video,
1013 unsigned long device_id)
1014{
1015 struct acpi_video_enumerated_device *ids;
1016 int i;
1017
1018 for (i = 0; i < video->attached_count; i++) {
1019 ids = &video->attached_array[i];
1020 if ((ids->value.int_val & 0xffff) == device_id)
1021 return ids->value.int_val;
1022 }
1023
1024 return 0;
1025}
1026
1027static int
Len Brown4be44fc2005-08-05 00:44:28 -04001028acpi_video_bus_get_one_device(struct acpi_device *device,
1029 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001031 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001032 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001033 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001034 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Len Brown4be44fc2005-08-05 00:44:28 -04001036 status =
1037 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001038 /* Some device omits _ADR, we skip them instead of fail */
1039 if (ACPI_FAILURE(status))
1040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Aaron Lu91e13aa2013-04-25 02:47:49 +00001042 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1043 if (!data)
1044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Aaron Lu91e13aa2013-04-25 02:47:49 +00001046 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1047 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1048 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Aaron Lu91e13aa2013-04-25 02:47:49 +00001050 data->device_id = device_id;
1051 data->video = video;
1052 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001053 INIT_DELAYED_WORK(&data->switch_brightness_work,
1054 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Aaron Lu91e13aa2013-04-25 02:47:49 +00001056 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001057
Aaron Lue50b9be2015-10-28 15:09:23 +08001058 if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001059 switch (attribute->display_type) {
1060 case ACPI_VIDEO_DISPLAY_CRT:
1061 data->flags.crt = 1;
1062 break;
1063 case ACPI_VIDEO_DISPLAY_TV:
1064 data->flags.tvout = 1;
1065 break;
1066 case ACPI_VIDEO_DISPLAY_DVI:
1067 data->flags.dvi = 1;
1068 break;
1069 case ACPI_VIDEO_DISPLAY_LCD:
1070 data->flags.lcd = 1;
1071 break;
1072 default:
1073 data->flags.unknown = 1;
1074 break;
1075 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001076 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001077 data->flags.bios = 1;
1078 } else {
1079 /* Check for legacy IDs */
1080 device_type = acpi_video_get_device_type(video, device_id);
1081 /* Ignore bits 16 and 18-20 */
1082 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001083 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1084 data->flags.crt = 1;
1085 break;
1086 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1087 data->flags.lcd = 1;
1088 break;
1089 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1090 data->flags.tvout = 1;
1091 break;
1092 default:
1093 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Aaron Lu91e13aa2013-04-25 02:47:49 +00001097 acpi_video_device_bind(video, data);
1098 acpi_video_device_find_cap(data);
1099
Aaron Lu91e13aa2013-04-25 02:47:49 +00001100 mutex_lock(&video->device_list_lock);
1101 list_add_tail(&data->entry, &video->video_device_list);
1102 mutex_unlock(&video->device_list_lock);
1103
1104 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/*
1108 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001109 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
1111 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001112 * none
1113 *
1114 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * bind the ids with the corresponding video devices
1116 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Len Brown4be44fc2005-08-05 00:44:28 -04001119static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001121 struct acpi_video_device *dev;
1122
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001123 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001124
1125 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001126 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001127
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001128 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131/*
1132 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001133 * video : video bus device
1134 * device : video output device under the video
1135 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
1137 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001138 * none
1139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * Bind the ids with the corresponding video devices
1141 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144static void
Len Brown4be44fc2005-08-05 00:44:28 -04001145acpi_video_device_bind(struct acpi_video_bus *video,
1146 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001148 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001149 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001151 for (i = 0; i < video->attached_count; i++) {
1152 ids = &video->attached_array[i];
1153 if (device->device_id == (ids->value.int_val & 0xffff)) {
1154 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1156 }
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Aaron Lu0b8db272014-09-30 14:10:17 +08001160static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1161{
1162 struct acpi_video_bus *video = device->video;
1163 int i;
1164
Aaron Lub4df4632014-12-15 16:01:29 +08001165 /*
1166 * If we have a broken _DOD or we have more than 8 output devices
1167 * under the graphics controller node that we can't proper deal with
1168 * in the operation region code currently, no need to test.
1169 */
1170 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001171 return true;
1172
1173 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001174 if ((video->attached_array[i].value.int_val & 0xfff) ==
1175 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001176 return true;
1177 }
1178
1179 return false;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/*
1183 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001184 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 *
1186 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001187 * < 0 : error
1188 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * Call _DOD to enumerate all devices attached to display adapter
1190 *
Len Brown4be44fc2005-08-05 00:44:28 -04001191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1194{
Len Brown4be44fc2005-08-05 00:44:28 -04001195 int status;
1196 int count;
1197 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001198 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001199 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1200 union acpi_object *dod = NULL;
1201 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Patrick Mochel90130262006-05-19 16:54:48 -04001203 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001205 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001206 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001209 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001211 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 status = -EFAULT;
1213 goto out;
1214 }
1215
1216 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001217 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001219 active_list = kcalloc(1 + dod->package.count,
1220 sizeof(struct acpi_video_enumerated_device),
1221 GFP_KERNEL);
1222 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 status = -ENOMEM;
1224 goto out;
1225 }
1226
1227 count = 0;
1228 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001229 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001232 printk(KERN_ERR PREFIX
1233 "Invalid _DOD data in element %d\n", i);
1234 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001236
1237 active_list[count].value.int_val = obj->integer.value;
1238 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001239 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1240 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 count++;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jesper Juhl6044ec82005-11-07 01:01:32 -08001244 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001245
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001246 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001248
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001249out:
Len Brown02438d82006-06-30 03:19:10 -04001250 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001251 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Len Brown4be44fc2005-08-05 00:44:28 -04001254static int
1255acpi_video_get_next_level(struct acpi_video_device *device,
1256 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001258 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001259 max = max_below = 0;
1260 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001261 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001262 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001263 l = device->brightness->levels[i];
1264 if (abs(l - level_current) < abs(delta)) {
1265 delta = l - level_current;
1266 if (!delta)
1267 break;
1268 }
1269 }
1270 /* Ajust level_current to closest available level */
1271 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001272 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001273 l = device->brightness->levels[i];
1274 if (l < min)
1275 min = l;
1276 if (l > max)
1277 max = l;
1278 if (l < min_above && l > level_current)
1279 min_above = l;
1280 if (l > max_below && l < level_current)
1281 max_below = l;
1282 }
1283
1284 switch (event) {
1285 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1286 return (level_current < max) ? min_above : min;
1287 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1288 return (level_current < max) ? min_above : max;
1289 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1290 return (level_current > min) ? max_below : min;
1291 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1292 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1293 return 0;
1294 default:
1295 return level_current;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001299static void
1300acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001302 struct acpi_video_device *device = container_of(to_delayed_work(work),
1303 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001304 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001305 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001306 int result = -EINVAL;
1307
Aaron Lufbc9fe12013-10-11 21:27:45 +08001308 /* no warning message if acpi_backlight=vendor or a quirk is used */
Hans de Goede654a1822015-06-09 10:32:25 +02001309 if (!device->backlight)
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001310 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001311
Julia Jomantaite469778c2008-06-23 22:50:42 +01001312 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001313 goto out;
1314
1315 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001316 &level_current,
1317 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001318 if (result)
1319 goto out;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001322
Zhang Rui24450c72009-03-18 16:27:10 +08001323 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001324
Matthew Garrett36342742009-07-14 17:06:03 +01001325 if (!result)
1326 backlight_force_update(device->backlight,
1327 BACKLIGHT_UPDATE_HOTKEY);
1328
Zhang Ruic8890f92009-03-18 16:27:08 +08001329out:
1330 if (result)
1331 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Matthew Garrette92a7162010-01-12 14:17:03 -05001334int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1335 void **edid)
1336{
1337 struct acpi_video_bus *video;
1338 struct acpi_video_device *video_device;
1339 union acpi_object *buffer = NULL;
1340 acpi_status status;
1341 int i, length;
1342
1343 if (!device || !acpi_driver_data(device))
1344 return -EINVAL;
1345
1346 video = acpi_driver_data(device);
1347
1348 for (i = 0; i < video->attached_count; i++) {
1349 video_device = video->attached_array[i].bind_info;
1350 length = 256;
1351
1352 if (!video_device)
1353 continue;
1354
Zhang Rui82069552010-12-06 15:04:24 +08001355 if (!video_device->cap._DDC)
1356 continue;
1357
Matthew Garrette92a7162010-01-12 14:17:03 -05001358 if (type) {
1359 switch (type) {
1360 case ACPI_VIDEO_DISPLAY_CRT:
1361 if (!video_device->flags.crt)
1362 continue;
1363 break;
1364 case ACPI_VIDEO_DISPLAY_TV:
1365 if (!video_device->flags.tvout)
1366 continue;
1367 break;
1368 case ACPI_VIDEO_DISPLAY_DVI:
1369 if (!video_device->flags.dvi)
1370 continue;
1371 break;
1372 case ACPI_VIDEO_DISPLAY_LCD:
1373 if (!video_device->flags.lcd)
1374 continue;
1375 break;
1376 }
1377 } else if (video_device->device_id != device_id) {
1378 continue;
1379 }
1380
1381 status = acpi_video_device_EDID(video_device, &buffer, length);
1382
1383 if (ACPI_FAILURE(status) || !buffer ||
1384 buffer->type != ACPI_TYPE_BUFFER) {
1385 length = 128;
1386 status = acpi_video_device_EDID(video_device, &buffer,
1387 length);
1388 if (ACPI_FAILURE(status) || !buffer ||
1389 buffer->type != ACPI_TYPE_BUFFER) {
1390 continue;
1391 }
1392 }
1393
1394 *edid = buffer->buffer.pointer;
1395 return length;
1396 }
1397
1398 return -ENODEV;
1399}
1400EXPORT_SYMBOL(acpi_video_get_edid);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static int
Len Brown4be44fc2005-08-05 00:44:28 -04001403acpi_video_bus_get_devices(struct acpi_video_bus *video,
1404 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Igor Murzovfba4e082012-10-13 04:41:25 +04001406 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001407 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Igor Murzovfba4e082012-10-13 04:41:25 +04001409 /*
1410 * There are systems where video module known to work fine regardless
1411 * of broken _DOD and ignoring returned value here doesn't cause
1412 * any issues later.
1413 */
1414 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001416 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001419 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001420 dev_err(&dev->dev, "Can't attach device\n");
1421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Aaron Lub4df4632014-12-15 16:01:29 +08001423 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/* acpi_video interface */
1429
Aaron Luefaa14c2013-07-16 13:08:05 +08001430/*
1431 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1432 * preform any automatic brightness change on receiving a notification.
1433 */
Len Brown4be44fc2005-08-05 00:44:28 -04001434static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Aaron Luefaa14c2013-07-16 13:08:05 +08001436 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001437 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Len Brown4be44fc2005-08-05 00:44:28 -04001440static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Aaron Luefaa14c2013-07-16 13:08:05 +08001442 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001443 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Bjorn Helgaas70155582009-04-07 15:37:11 +00001446static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001448 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001449 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001450 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001451
Aaron Lu67b662e2013-10-11 21:27:44 +08001452 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001453 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Luming Yue9dab192007-08-20 18:23:53 +08001455 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001458 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001460 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 break;
1462
Julius Volz98fb8fe2007-02-20 16:38:40 +01001463 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * connector. */
1465 acpi_video_device_enumerate(video);
1466 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001467 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 break;
1469
Len Brown4be44fc2005-08-05 00:44:28 -04001470 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001471 keycode = KEY_SWITCHVIDEOMODE;
1472 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001473 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001474 keycode = KEY_VIDEO_NEXT;
1475 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001476 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001477 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479
1480 default:
1481 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001482 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 break;
1484 }
1485
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001486 if (acpi_notifier_call_chain(device, event, 0))
1487 /* Something vetoed the keypress. */
1488 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001489
Hans de Goede05bc59a2015-12-22 19:09:51 +01001490 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
Matthew Garrett17c452f2009-12-11 17:40:46 -05001491 input_report_key(input, keycode, 1);
1492 input_sync(input);
1493 input_report_key(input, keycode, 0);
1494 input_sync(input);
1495 }
Luming Yue9dab192007-08-20 18:23:53 +08001496
Patrick Mocheld550d982006-06-27 00:41:40 -04001497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001500static void brightness_switch_event(struct acpi_video_device *video_device,
1501 u32 event)
1502{
1503 if (!brightness_switch_enabled)
1504 return;
1505
1506 video_device->switch_brightness_event = event;
1507 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1508}
1509
Len Brown4be44fc2005-08-05 00:44:28 -04001510static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001512 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001513 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001514 struct acpi_video_bus *bus;
1515 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001516 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001521 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001522 bus = video_device->video;
1523 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001526 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001527 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001528 keycode = KEY_BRIGHTNESS_CYCLE;
1529 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001530 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001531 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001532 keycode = KEY_BRIGHTNESSUP;
1533 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001534 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001535 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001536 keycode = KEY_BRIGHTNESSDOWN;
1537 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001538 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001539 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001540 keycode = KEY_BRIGHTNESS_ZERO;
1541 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001542 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001543 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001544 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
1546 default:
1547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001548 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 break;
1550 }
Luming Yue9dab192007-08-20 18:23:53 +08001551
Zhang Rui7761f632008-01-25 14:48:12 +08001552 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001553
Hans de Goede05bc59a2015-12-22 19:09:51 +01001554 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
Matthew Garrett17c452f2009-12-11 17:40:46 -05001555 input_report_key(input, keycode, 1);
1556 input_sync(input);
1557 input_report_key(input, keycode, 0);
1558 input_sync(input);
1559 }
Luming Yue9dab192007-08-20 18:23:53 +08001560
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001564static int acpi_video_resume(struct notifier_block *nb,
1565 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001566{
1567 struct acpi_video_bus *video;
1568 struct acpi_video_device *video_device;
1569 int i;
1570
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001571 switch (val) {
1572 case PM_HIBERNATION_PREPARE:
1573 case PM_SUSPEND_PREPARE:
1574 case PM_RESTORE_PREPARE:
1575 return NOTIFY_DONE;
1576 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001577
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001578 video = container_of(nb, struct acpi_video_bus, pm_nb);
1579
1580 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001581
1582 for (i = 0; i < video->attached_count; i++) {
1583 video_device = video->attached_array[i].bind_info;
Hans de Goede654a1822015-06-09 10:32:25 +02001584 if (video_device && video_device->brightness)
1585 acpi_video_device_lcd_set_level(video_device,
1586 video_device->brightness->curr);
Matthew Garrett863c1492008-02-04 23:31:24 -08001587 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001588
1589 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001590}
1591
Zhang Ruic504f8c2009-12-30 15:59:23 +08001592static acpi_status
1593acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1594 void **return_value)
1595{
1596 struct acpi_device *device = context;
1597 struct acpi_device *sibling;
1598 int result;
1599
1600 if (handle == device->handle)
1601 return AE_CTRL_TERMINATE;
1602
1603 result = acpi_bus_get_device(handle, &sibling);
1604 if (result)
1605 return AE_OK;
1606
1607 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1608 return AE_ALREADY_EXISTS;
1609
1610 return AE_OK;
1611}
1612
Aaron Lu67b662e2013-10-11 21:27:44 +08001613static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1614{
Hans de Goede99678ed2014-05-15 13:22:33 +02001615 struct backlight_properties props;
1616 struct pci_dev *pdev;
1617 acpi_handle acpi_parent;
1618 struct device *parent = NULL;
1619 int result;
1620 static int count;
1621 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001622
Hans de Goede99678ed2014-05-15 13:22:33 +02001623 result = acpi_video_init_brightness(device);
1624 if (result)
1625 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001626
1627 if (disable_backlight_sysfs_if > 0)
1628 return;
1629
Hans de Goede99678ed2014-05-15 13:22:33 +02001630 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1631 if (!name)
1632 return;
1633 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001634
Hans de Goede99678ed2014-05-15 13:22:33 +02001635 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001636
Hans de Goede99678ed2014-05-15 13:22:33 +02001637 pdev = acpi_get_pci_dev(acpi_parent);
1638 if (pdev) {
1639 parent = &pdev->dev;
1640 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001641 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001642
1643 memset(&props, 0, sizeof(struct backlight_properties));
1644 props.type = BACKLIGHT_FIRMWARE;
1645 props.max_brightness = device->brightness->count - 3;
1646 device->backlight = backlight_device_register(name,
1647 parent,
1648 device,
1649 &acpi_backlight_ops,
1650 &props);
1651 kfree(name);
Hans de Goede654a1822015-06-09 10:32:25 +02001652 if (IS_ERR(device->backlight)) {
1653 device->backlight = NULL;
Hans de Goede99678ed2014-05-15 13:22:33 +02001654 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001655 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001656
1657 /*
1658 * Save current brightness level in case we have to restore it
1659 * before acpi_video_device_lcd_set_level() is called next time.
1660 */
1661 device->backlight->props.brightness =
1662 acpi_video_get_brightness(device->backlight);
1663
1664 device->cooling_dev = thermal_cooling_device_register("LCD",
1665 device->dev, &video_cooling_ops);
1666 if (IS_ERR(device->cooling_dev)) {
1667 /*
1668 * Set cooling_dev to NULL so we don't crash trying to free it.
1669 * Also, why the hell we are returning early and not attempt to
1670 * register video output if cooling device registration failed?
1671 * -- dtor
1672 */
1673 device->cooling_dev = NULL;
1674 return;
1675 }
1676
1677 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1678 device->cooling_dev->id);
1679 result = sysfs_create_link(&device->dev->dev.kobj,
1680 &device->cooling_dev->device.kobj,
1681 "thermal_cooling");
1682 if (result)
1683 printk(KERN_ERR PREFIX "Create sysfs link\n");
1684 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1685 &device->dev->dev.kobj, "device");
1686 if (result)
1687 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001688}
1689
Aaron Ludce4ec22014-10-28 14:35:59 +08001690static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1691{
1692 struct acpi_video_device *dev;
1693 union acpi_object *levels;
1694
1695 mutex_lock(&video->device_list_lock);
1696 list_for_each_entry(dev, &video->video_device_list, entry) {
1697 if (!acpi_video_device_lcd_query_levels(dev, &levels))
1698 kfree(levels);
1699 }
1700 mutex_unlock(&video->device_list_lock);
1701}
1702
Aaron Lue50b9be2015-10-28 15:09:23 +08001703static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1704{
1705 /*
1706 * Do not create backlight device for video output
1707 * device that is not in the enumerated list.
1708 */
1709 if (!acpi_video_device_in_dod(dev)) {
1710 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1711 return false;
1712 }
1713
1714 if (only_lcd)
1715 return dev->flags.lcd;
1716 return true;
1717}
1718
Aaron Lu67b662e2013-10-11 21:27:44 +08001719static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1720{
1721 struct acpi_video_device *dev;
1722
Hans de Goede53a92ba2014-05-21 15:39:55 +02001723 if (video->backlight_registered)
1724 return 0;
1725
Aaron Ludce4ec22014-10-28 14:35:59 +08001726 acpi_video_run_bcl_for_osi(video);
1727
Hans de Goede3bd6bce2015-06-16 16:27:51 +02001728 if (acpi_video_get_backlight_type() != acpi_backlight_video)
Hans de Goede99678ed2014-05-15 13:22:33 +02001729 return 0;
1730
Aaron Lu67b662e2013-10-11 21:27:44 +08001731 mutex_lock(&video->device_list_lock);
Aaron Lue50b9be2015-10-28 15:09:23 +08001732 list_for_each_entry(dev, &video->video_device_list, entry) {
1733 if (acpi_video_should_register_backlight(dev))
1734 acpi_video_dev_register_backlight(dev);
1735 }
Aaron Lu67b662e2013-10-11 21:27:44 +08001736 mutex_unlock(&video->device_list_lock);
1737
Hans de Goede99678ed2014-05-15 13:22:33 +02001738 video->backlight_registered = true;
1739
Aaron Lu67b662e2013-10-11 21:27:44 +08001740 video->pm_nb.notifier_call = acpi_video_resume;
1741 video->pm_nb.priority = 0;
1742 return register_pm_notifier(&video->pm_nb);
1743}
1744
1745static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1746{
1747 if (device->backlight) {
1748 backlight_device_unregister(device->backlight);
1749 device->backlight = NULL;
1750 }
1751 if (device->brightness) {
1752 kfree(device->brightness->levels);
1753 kfree(device->brightness);
1754 device->brightness = NULL;
1755 }
1756 if (device->cooling_dev) {
1757 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1758 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1759 thermal_cooling_device_unregister(device->cooling_dev);
1760 device->cooling_dev = NULL;
1761 }
1762}
1763
1764static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1765{
1766 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001767 int error;
1768
1769 if (!video->backlight_registered)
1770 return 0;
1771
1772 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001773
1774 mutex_lock(&video->device_list_lock);
1775 list_for_each_entry(dev, &video->video_device_list, entry)
1776 acpi_video_dev_unregister_backlight(dev);
1777 mutex_unlock(&video->device_list_lock);
1778
Hans de Goede99678ed2014-05-15 13:22:33 +02001779 video->backlight_registered = false;
1780
Aaron Lu67b662e2013-10-11 21:27:44 +08001781 return error;
1782}
1783
1784static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1785{
1786 acpi_status status;
1787 struct acpi_device *adev = device->dev;
1788
1789 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1790 acpi_video_device_notify, device);
1791 if (ACPI_FAILURE(status))
1792 dev_err(&adev->dev, "Error installing notify handler\n");
1793 else
1794 device->flags.notify = 1;
1795}
1796
1797static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1798{
1799 struct input_dev *input;
1800 struct acpi_video_device *dev;
1801 int error;
1802
1803 video->input = input = input_allocate_device();
1804 if (!input) {
1805 error = -ENOMEM;
1806 goto out;
1807 }
1808
1809 error = acpi_video_bus_start_devices(video);
1810 if (error)
1811 goto err_free_input;
1812
1813 snprintf(video->phys, sizeof(video->phys),
1814 "%s/video/input0", acpi_device_hid(video->device));
1815
1816 input->name = acpi_device_name(video->device);
1817 input->phys = video->phys;
1818 input->id.bustype = BUS_HOST;
1819 input->id.product = 0x06;
1820 input->dev.parent = &video->device->dev;
1821 input->evbit[0] = BIT(EV_KEY);
1822 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1823 set_bit(KEY_VIDEO_NEXT, input->keybit);
1824 set_bit(KEY_VIDEO_PREV, input->keybit);
1825 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1826 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1827 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1828 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1829 set_bit(KEY_DISPLAY_OFF, input->keybit);
1830
1831 error = input_register_device(input);
1832 if (error)
1833 goto err_stop_dev;
1834
1835 mutex_lock(&video->device_list_lock);
1836 list_for_each_entry(dev, &video->video_device_list, entry)
1837 acpi_video_dev_add_notify_handler(dev);
1838 mutex_unlock(&video->device_list_lock);
1839
1840 return 0;
1841
1842err_stop_dev:
1843 acpi_video_bus_stop_devices(video);
1844err_free_input:
1845 input_free_device(input);
1846 video->input = NULL;
1847out:
1848 return error;
1849}
1850
1851static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1852{
1853 if (dev->flags.notify) {
1854 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1855 acpi_video_device_notify);
1856 dev->flags.notify = 0;
1857 }
1858}
1859
1860static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1861{
1862 struct acpi_video_device *dev;
1863
1864 mutex_lock(&video->device_list_lock);
1865 list_for_each_entry(dev, &video->video_device_list, entry)
1866 acpi_video_dev_remove_notify_handler(dev);
1867 mutex_unlock(&video->device_list_lock);
1868
1869 acpi_video_bus_stop_devices(video);
1870 input_unregister_device(video->input);
1871 video->input = NULL;
1872}
1873
1874static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1875{
1876 struct acpi_video_device *dev, *next;
1877
1878 mutex_lock(&video->device_list_lock);
1879 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1880 list_del(&dev->entry);
1881 kfree(dev);
1882 }
1883 mutex_unlock(&video->device_list_lock);
1884
1885 return 0;
1886}
1887
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001888static int instance;
1889
Len Brown4be44fc2005-08-05 00:44:28 -04001890static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001892 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001893 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001894 acpi_status status;
1895
1896 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1897 device->parent->handle, 1,
1898 acpi_video_bus_match, NULL,
1899 device, NULL);
1900 if (status == AE_ALREADY_EXISTS) {
1901 printk(KERN_WARNING FW_BUG
1902 "Duplicate ACPI video bus devices for the"
1903 " same VGA controller, please try module "
1904 "parameter \"video.allow_duplicates=1\""
1905 "if the current driver doesn't work.\n");
1906 if (!allow_duplicates)
1907 return -ENODEV;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Burman Yan36bcbec2006-12-19 12:56:11 -08001910 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001912 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Zhang Ruie6d9da12007-08-25 02:23:31 -04001914 /* a hack to fix the duplicate name "VID" problem on T61 */
1915 if (!strcmp(device->pnp.bus_id, "VID")) {
1916 if (instance)
1917 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001918 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001919 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001920 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1921 if (!strcmp(device->pnp.bus_id, "VGA")) {
1922 if (instance)
1923 device->pnp.bus_id[3] = '0' + instance;
1924 instance++;
1925 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001926
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001927 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1929 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001930 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001933 error = acpi_video_bus_check(video);
1934 if (error)
1935 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001937 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 INIT_LIST_HEAD(&video->video_device_list);
1939
Igor Murzovea9f8852012-03-30 21:32:08 +04001940 error = acpi_video_bus_get_devices(video, device);
1941 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001942 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001945 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1946 video->flags.multihead ? "yes" : "no",
1947 video->flags.rom ? "yes" : "no",
1948 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08001949 mutex_lock(&video_list_lock);
1950 list_add_tail(&video->entry, &video_bus_head);
1951 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Aaron Lu67b662e2013-10-11 21:27:44 +08001953 acpi_video_bus_register_backlight(video);
1954 acpi_video_bus_add_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001955
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Aaron Lu67b662e2013-10-11 21:27:44 +08001958err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001959 acpi_video_bus_put_devices(video);
1960 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08001961err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001962 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001963 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001964
1965 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001968static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Len Brown4be44fc2005-08-05 00:44:28 -04001970 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001976 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Aaron Lu67b662e2013-10-11 21:27:44 +08001978 acpi_video_bus_remove_notify_handler(video);
1979 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Aaron Lu67b662e2013-10-11 21:27:44 +08001982 mutex_lock(&video_list_lock);
1983 list_del(&video->entry);
1984 mutex_unlock(&video_list_lock);
1985
Jesper Juhl6044ec82005-11-07 01:01:32 -08001986 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 kfree(video);
1988
Patrick Mocheld550d982006-06-27 00:41:40 -04001989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Alan Coxc6996bd2012-04-25 14:33:48 +01001992static int __init is_i740(struct pci_dev *dev)
1993{
1994 if (dev->device == 0x00D1)
1995 return 1;
1996 if (dev->device == 0x7000)
1997 return 1;
1998 return 0;
1999}
2000
Matthew Garrett74a365b2009-03-19 21:35:39 +00002001static int __init intel_opregion_present(void)
2002{
Alan Coxc6996bd2012-04-25 14:33:48 +01002003 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002004 struct pci_dev *dev = NULL;
2005 u32 address;
2006
2007 for_each_pci_dev(dev) {
2008 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2009 continue;
2010 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2011 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002012 /* We don't want to poke around undefined i740 registers */
2013 if (is_i740(dev))
2014 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002015 pci_read_config_dword(dev, 0xfc, &address);
2016 if (!address)
2017 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002018 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002019 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002020 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002021}
2022
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002023int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002025 int ret = 0;
Chris Wilson28d63402015-03-01 10:41:38 +00002026
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002027 mutex_lock(&register_count_mutex);
Zhao Yakui86e437f2009-06-16 11:23:13 +08002028 if (register_count) {
2029 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002030 * if the function of acpi_video_register is already called,
2031 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002032 */
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002033 goto leave;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Aaron Lu67b662e2013-10-11 21:27:44 +08002036 mutex_init(&video_list_lock);
2037 INIT_LIST_HEAD(&video_bus_head);
2038
Hans de Goede7ee33ba2015-06-16 16:27:53 +02002039 dmi_check_system(video_dmi_table);
2040
Chris Wilson28d63402015-03-01 10:41:38 +00002041 ret = acpi_bus_register_driver(&acpi_video_bus);
2042 if (ret)
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002043 goto leave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Zhao Yakui86e437f2009-06-16 11:23:13 +08002045 /*
2046 * When the acpi_video_bus is loaded successfully, increase
2047 * the counter reference.
2048 */
2049 register_count = 1;
2050
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002051leave:
2052 mutex_unlock(&register_count_mutex);
2053 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002055EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002056
Zhao Yakui86e437f2009-06-16 11:23:13 +08002057void acpi_video_unregister(void)
2058{
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002059 mutex_lock(&register_count_mutex);
2060 if (register_count) {
2061 acpi_bus_unregister_driver(&acpi_video_bus);
2062 register_count = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002063 }
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002064 mutex_unlock(&register_count_mutex);
Zhao Yakui86e437f2009-06-16 11:23:13 +08002065}
2066EXPORT_SYMBOL(acpi_video_unregister);
2067
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002068void acpi_video_unregister_backlight(void)
2069{
2070 struct acpi_video_bus *video;
2071
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002072 mutex_lock(&register_count_mutex);
2073 if (register_count) {
2074 mutex_lock(&video_list_lock);
2075 list_for_each_entry(video, &video_bus_head, entry)
2076 acpi_video_bus_unregister_backlight(video);
2077 mutex_unlock(&video_list_lock);
2078 }
2079 mutex_unlock(&register_count_mutex);
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002080}
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002081
Hans de Goede90b066b2015-12-22 19:09:48 +01002082bool acpi_video_handles_brightness_key_presses(void)
2083{
2084 bool have_video_busses;
2085
2086 mutex_lock(&video_list_lock);
2087 have_video_busses = !list_empty(&video_bus_head);
2088 mutex_unlock(&video_list_lock);
2089
Hans de Goede05bc59a2015-12-22 19:09:51 +01002090 return have_video_busses &&
2091 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
Hans de Goede90b066b2015-12-22 19:09:48 +01002092}
2093EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
2094
Matthew Garrett74a365b2009-03-19 21:35:39 +00002095/*
2096 * This is kind of nasty. Hardware using Intel chipsets may require
2097 * the video opregion code to be run first in order to initialise
2098 * state before any ACPI video calls are made. To handle this we defer
2099 * registration of the video class until the opregion code has run.
2100 */
2101
2102static int __init acpi_video_init(void)
2103{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002104 /*
2105 * Let the module load even if ACPI is disabled (e.g. due to
2106 * a broken BIOS) so that i915.ko can still be loaded on such
2107 * old systems without an AcpiOpRegion.
2108 *
2109 * acpi_video_register() will report -ENODEV later as well due
2110 * to acpi_disabled when i915.ko tries to register itself afterwards.
2111 */
2112 if (acpi_disabled)
2113 return 0;
2114
Matthew Garrett74a365b2009-03-19 21:35:39 +00002115 if (intel_opregion_present())
2116 return 0;
2117
2118 return acpi_video_register();
2119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Zhao Yakui86e437f2009-06-16 11:23:13 +08002121static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Hans de Goede93a291d2015-06-16 16:27:52 +02002123 acpi_video_detect_exit();
Zhao Yakui86e437f2009-06-16 11:23:13 +08002124 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Patrick Mocheld550d982006-06-27 00:41:40 -04002126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129module_init(acpi_video_init);
2130module_exit(acpi_video_exit);