blob: d00bc0ef87a67a0517a2406cc6149d922d0feea6 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Yu Luming2f3d0002006-11-11 02:40:34 +080047#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050050ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Len Brownf52fd662007-02-12 22:42:12 -050052MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050053MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_LICENSE("GPL");
55
Rafael J. Wysocki28437682014-07-14 19:35:45 +020056static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080057module_param(brightness_switch_enabled, bool, 0644);
58
Zhang Ruic504f8c2009-12-30 15:59:23 +080059/*
60 * By default, we don't allow duplicate ACPI video bus devices
61 * under the same VGA controller
62 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103063static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080064module_param(allow_duplicates, bool, 0644);
65
Hans de Goede654a1822015-06-09 10:32:25 +020066static int disable_backlight_sysfs_if = -1;
67module_param(disable_backlight_sysfs_if, int, 0444);
68
Hans de Goede05bc59a2015-12-22 19:09:51 +010069#define REPORT_OUTPUT_KEY_EVENTS 0x01
70#define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
71static int report_key_events = -1;
72module_param(report_key_events, int, 0644);
73MODULE_PARM_DESC(report_key_events,
74 "0: none, 1: output changes, 2: brightness changes, 3: all");
75
Aaron Lue50b9be2015-10-28 15:09:23 +080076static bool device_id_scheme = false;
77module_param(device_id_scheme, bool, 0444);
78
79static bool only_lcd = false;
80module_param(only_lcd, bool, 0444);
81
Hans de Goede970530c2016-01-14 09:41:45 +010082static int register_count;
83static DEFINE_MUTEX(register_count_mutex);
Hans de Goede14e93552016-01-14 09:41:46 +010084static DEFINE_MUTEX(video_list_lock);
85static LIST_HEAD(video_bus_head);
Len Brown4be44fc2005-08-05 00:44:28 -040086static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010087static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000088static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Hans de Goede93a291d2015-06-16 16:27:52 +020089void acpi_video_detect_exit(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Thomas Renninger1ba90e32007-07-23 14:44:41 +020091static const struct acpi_device_id video_device_ids[] = {
92 {ACPI_VIDEO_HID, 0},
93 {"", 0},
94};
95MODULE_DEVICE_TABLE(acpi, video_device_ids);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050098 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200100 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .ops = {
102 .add = acpi_video_bus_add,
103 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000104 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400105 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400109 u8 multihead:1; /* can switch video heads */
110 u8 rom:1; /* can retrieve a video rom */
111 u8 post:1; /* can configure the head to */
112 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500116 u8 _DOS:1; /* Enable/Disable output switching */
117 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
118 u8 _ROM:1; /* Get ROM Data */
119 u8 _GPD:1; /* Get POST Device */
120 u8 _SPD:1; /* Set POST Device */
121 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400122 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125struct acpi_video_device_attrib {
126 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500127 u32 display_port_attachment:4; /* This field differentiates the display type */
128 u32 display_type:4; /* Describe the specific type in use */
129 u32 vendor_specific:4; /* Chipset Vendor Specific */
130 u32 bios_can_detect:1; /* BIOS can detect the device */
131 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400132 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500133 u32 pipe_id:3; /* For VGA multiple-head devices. */
134 u32 reserved:10; /* Must be 0 */
135 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138struct acpi_video_enumerated_device {
139 union {
140 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 } value;
143 struct acpi_video_device *bind_info;
144};
145
146struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400147 struct acpi_device *device;
Hans de Goede99678ed2014-05-15 13:22:33 +0200148 bool backlight_registered;
Len Brown4be44fc2005-08-05 00:44:28 -0400149 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 u8 attached_count;
Aaron Lub4df4632014-12-15 16:01:29 +0800152 u8 child_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500156 struct mutex device_list_lock; /* protects video_device_list */
Aaron Lu67b662e2013-10-11 21:27:44 +0800157 struct list_head entry;
Luming Yue9dab192007-08-20 18:23:53 +0800158 struct input_dev *input;
159 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200160 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400164 u8 crt:1;
165 u8 lcd:1;
166 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500167 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 bios:1;
169 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000170 u8 notify:1;
171 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
174struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500175 u8 _ADR:1; /* Return the unique ID */
176 u8 _BCL:1; /* Query list of brightness control levels supported */
177 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800178 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800179 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500180 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400184 unsigned long device_id;
185 struct acpi_video_device_flags flags;
186 struct acpi_video_device_cap cap;
187 struct list_head entry;
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200188 struct delayed_work switch_brightness_work;
189 int switch_brightness_event;
Len Brown4be44fc2005-08-05 00:44:28 -0400190 struct acpi_video_bus *video;
191 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800193 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400194 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Len Brown4be44fc2005-08-05 00:44:28 -0400197static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
198static void acpi_video_device_rebind(struct acpi_video_bus *video);
199static void acpi_video_device_bind(struct acpi_video_bus *video,
200 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800202static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
203 int level);
204static int acpi_video_device_lcd_get_level_current(
205 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000206 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400207static int acpi_video_get_next_level(struct acpi_video_device *device,
208 u32 level_current, u32 event);
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200209static void acpi_video_switch_brightness(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Felipe Contreras21fcb342013-08-01 18:43:59 -0500211/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800212static int acpi_video_get_brightness(struct backlight_device *bd)
213{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400214 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000215 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200216 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800217
Danny Baumanna89803d2013-03-19 16:22:50 +0000218 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800219 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000220 for (i = 2; i < vd->brightness->count; i++) {
221 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500222 /*
223 * The first two entries are special - see page 575
224 * of the ACPI spec 3.0
225 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200226 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000227 }
228 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800229}
230
231static int acpi_video_set_brightness(struct backlight_device *bd)
232{
Zhang Rui24450c72009-03-18 16:27:10 +0800233 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200234 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800235
Linus Torvalds8ab58e82014-07-18 14:32:51 +0200236 cancel_delayed_work(&vd->switch_brightness_work);
Zhang Rui24450c72009-03-18 16:27:10 +0800237 return acpi_video_device_lcd_set_level(vd,
238 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800239}
240
Lionel Debrouxacc24722010-11-16 14:14:02 +0100241static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000242 .get_brightness = acpi_video_get_brightness,
243 .update_status = acpi_video_set_brightness,
244};
245
Zhang Rui702ed512008-01-17 15:51:22 +0800246/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400247static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000248 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800249{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400250 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800251 struct acpi_video_device *video = acpi_driver_data(device);
252
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000253 *state = video->brightness->count - 3;
254 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800255}
256
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400257static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000258 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800259{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400260 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800261 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400262 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000263 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800264
Danny Baumanna89803d2013-03-19 16:22:50 +0000265 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800266 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000267 for (offset = 2; offset < video->brightness->count; offset++)
268 if (level == video->brightness->levels[offset]) {
269 *state = video->brightness->count - offset - 1;
270 return 0;
271 }
Zhang Rui702ed512008-01-17 15:51:22 +0800272
273 return -EINVAL;
274}
275
276static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400277video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800278{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400279 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800280 struct acpi_video_device *video = acpi_driver_data(device);
281 int level;
282
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200283 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800284 return -EINVAL;
285
286 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200287 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800288 return acpi_video_device_lcd_set_level(video, level);
289}
290
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400291static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800292 .get_max_state = video_get_max_state,
293 .get_cur_state = video_get_cur_state,
294 .set_cur_state = video_set_cur_state,
295};
296
Felipe Contreras21fcb342013-08-01 18:43:59 -0500297/*
298 * --------------------------------------------------------------------------
299 * Video Management
300 * --------------------------------------------------------------------------
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static int
Aaron Lu05950092016-04-27 20:45:04 +0800304acpi_video_device_lcd_query_levels(acpi_handle handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400305 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Len Brown4be44fc2005-08-05 00:44:28 -0400307 int status;
308 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
309 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 *levels = NULL;
313
Aaron Lu05950092016-04-27 20:45:04 +0800314 status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400317 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500318 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400319 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 status = -EFAULT;
321 goto err;
322 }
323
324 *levels = obj;
325
Patrick Mocheld550d982006-06-27 00:41:40 -0400326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200328err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800329 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Patrick Mocheld550d982006-06-27 00:41:40 -0400331 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334static int
Len Brown4be44fc2005-08-05 00:44:28 -0400335acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Zhang Rui24450c72009-03-18 16:27:10 +0800337 int status;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800338 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jiang Liu0db98202013-06-29 00:24:39 +0800340 status = acpi_execute_simple_method(device->dev->handle,
341 "_BCM", level);
Zhang Rui24450c72009-03-18 16:27:10 +0800342 if (ACPI_FAILURE(status)) {
343 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
344 return -EIO;
345 }
346
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400347 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800348 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800349 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800350 if (device->backlight)
351 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800352 return 0;
353 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800354
Zhang Rui24450c72009-03-18 16:27:10 +0800355 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Zhang Rui45cb50e2009-04-24 12:13:18 -0400359/*
360 * For some buggy _BQC methods, we need to add a constant value to
361 * the _BQC return value to get the actual current brightness level
362 */
363
364static int bqc_offset_aml_bug_workaround;
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200365static int video_set_bqc_offset(const struct dmi_system_id *d)
Zhang Rui45cb50e2009-04-24 12:13:18 -0400366{
367 bqc_offset_aml_bug_workaround = 9;
368 return 0;
369}
370
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200371static int video_disable_backlight_sysfs_if(
Hans de Goede654a1822015-06-09 10:32:25 +0200372 const struct dmi_system_id *d)
373{
374 if (disable_backlight_sysfs_if == -1)
375 disable_backlight_sysfs_if = 1;
376 return 0;
377}
378
Aaron Lue50b9be2015-10-28 15:09:23 +0800379static int video_set_device_id_scheme(const struct dmi_system_id *d)
380{
381 device_id_scheme = true;
382 return 0;
383}
384
385static int video_enable_only_lcd(const struct dmi_system_id *d)
386{
387 only_lcd = true;
388 return 0;
389}
390
Hans de Goede4b4b3b22015-12-22 19:09:52 +0100391static int video_set_report_key_events(const struct dmi_system_id *id)
392{
393 if (report_key_events == -1)
394 report_key_events = (uintptr_t)id->driver_data;
395 return 0;
396}
397
Hans de Goede7ee33ba2015-06-16 16:27:53 +0200398static struct dmi_system_id video_dmi_table[] = {
Zhang Rui45cb50e2009-04-24 12:13:18 -0400399 /*
400 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
401 */
402 {
403 .callback = video_set_bqc_offset,
404 .ident = "Acer Aspire 5720",
405 .matches = {
406 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
407 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
408 },
409 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400410 {
411 .callback = video_set_bqc_offset,
412 .ident = "Acer Aspire 5710Z",
413 .matches = {
414 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
415 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
416 },
417 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400418 {
419 .callback = video_set_bqc_offset,
420 .ident = "eMachines E510",
421 .matches = {
422 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
423 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
424 },
425 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400426 {
427 .callback = video_set_bqc_offset,
428 .ident = "Acer Aspire 5315",
429 .matches = {
430 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
431 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
432 },
433 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800434 {
435 .callback = video_set_bqc_offset,
436 .ident = "Acer Aspire 7720",
437 .matches = {
438 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
439 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
440 },
441 },
Hans de Goede5f240792014-08-28 10:20:46 +0200442
443 /*
Hans de Goede654a1822015-06-09 10:32:25 +0200444 * Some machines have a broken acpi-video interface for brightness
445 * control, but still need an acpi_video_device_lcd_set_level() call
446 * on resume to turn the backlight power on. We Enable backlight
447 * control on these systems, but do not register a backlight sysfs
448 * as brightness control does not work.
449 */
450 {
Hans de Goedede588b82016-01-11 14:46:17 +0100451 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
452 .callback = video_disable_backlight_sysfs_if,
453 .ident = "Toshiba Portege R700",
454 .matches = {
455 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R700"),
457 },
458 },
459 {
Hans de Goede654a1822015-06-09 10:32:25 +0200460 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
461 .callback = video_disable_backlight_sysfs_if,
462 .ident = "Toshiba Portege R830",
463 .matches = {
464 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
465 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
466 },
467 },
Hans de Goedeb21f2e82016-01-14 14:24:39 +0100468 {
469 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
470 .callback = video_disable_backlight_sysfs_if,
471 .ident = "Toshiba Satellite R830",
472 .matches = {
473 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
474 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE R830"),
475 },
476 },
Aaron Lue50b9be2015-10-28 15:09:23 +0800477 /*
478 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
479 * but the IDs actually follow the Device ID Scheme.
480 */
481 {
482 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
483 .callback = video_set_device_id_scheme,
484 .ident = "ESPRIMO Mobile M9410",
485 .matches = {
486 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
487 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
488 },
489 },
490 /*
491 * Some machines have multiple video output devices, but only the one
492 * that is the type of LCD can do the backlight control so we should not
493 * register backlight interface for other video output devices.
494 */
495 {
496 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
497 .callback = video_enable_only_lcd,
498 .ident = "ESPRIMO Mobile M9410",
499 .matches = {
500 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
501 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
502 },
503 },
Hans de Goede4b4b3b22015-12-22 19:09:52 +0100504 /*
505 * Some machines report wrong key events on the acpi-bus, suppress
506 * key event reporting on these. Note this is only intended to work
507 * around events which are plain wrong. In some cases we get double
508 * events, in this case acpi-video is considered the canonical source
509 * and the events from the other source should be filtered. E.g.
510 * by calling acpi_video_handles_brightness_key_presses() from the
511 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
512 */
513 {
514 .callback = video_set_report_key_events,
515 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
516 .ident = "Dell Vostro V131",
517 .matches = {
518 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
519 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
520 },
521 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400522 {}
523};
524
Danny Baumann994fa632013-03-19 16:22:52 +0000525static unsigned long long
526acpi_video_bqc_value_to_level(struct acpi_video_device *device,
527 unsigned long long bqc_value)
528{
529 unsigned long long level;
530
531 if (device->brightness->flags._BQC_use_index) {
532 /*
533 * _BQC returns an index that doesn't account for
534 * the first 2 items with special meaning, so we need
535 * to compensate for that by offsetting ourselves
536 */
537 if (device->brightness->flags._BCL_reversed)
538 bqc_value = device->brightness->count - 3 - bqc_value;
539
540 level = device->brightness->levels[bqc_value + 2];
541 } else {
542 level = bqc_value;
543 }
544
545 level += bqc_offset_aml_bug_workaround;
546
547 return level;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static int
Len Brown4be44fc2005-08-05 00:44:28 -0400551acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000552 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Zhang Ruic8890f92009-03-18 16:27:08 +0800554 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800555 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800556
Zhang Ruic60d6382009-03-18 16:27:18 +0800557 if (device->cap._BQC || device->cap._BCQ) {
558 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
559
560 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800561 NULL, level);
562 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000563 if (raw) {
564 /*
565 * Caller has indicated he wants the raw
566 * value returned by _BQC, so don't furtherly
567 * mess with the value.
568 */
569 return 0;
570 }
571
Danny Baumann994fa632013-03-19 16:22:52 +0000572 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800573
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800574 for (i = 2; i < device->brightness->count; i++)
575 if (device->brightness->levels[i] == *level) {
576 device->brightness->curr = *level;
577 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200578 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000579 /*
580 * BQC returned an invalid level.
581 * Stop using it.
582 */
583 ACPI_WARNING((AE_INFO,
584 "%s returned an invalid level",
585 buf));
586 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800587 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500588 /*
589 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800590 * should we return an error or ignore this failure?
591 * dev->brightness->curr is a cached value which stores
592 * the correct current backlight level in most cases.
593 * ACPI video backlight still works w/ buggy _BQC.
594 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
595 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800596 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
597 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800598 }
599 }
600
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400601 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static int
Len Brown4be44fc2005-08-05 00:44:28 -0400606acpi_video_device_EDID(struct acpi_video_device *device,
607 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Len Brown4be44fc2005-08-05 00:44:28 -0400609 int status;
610 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
611 union acpi_object *obj;
612 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
613 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 *edid = NULL;
617
618 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400619 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (length == 128)
621 arg0.integer.value = 1;
622 else if (length == 256)
623 arg0.integer.value = 2;
624 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Patrick Mochel90130262006-05-19 16:54:48 -0400627 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200631 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (obj && obj->type == ACPI_TYPE_BUFFER)
634 *edid = obj;
635 else {
Len Brown64684632006-06-26 23:41:38 -0400636 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 status = -EFAULT;
638 kfree(obj);
639 }
640
Patrick Mocheld550d982006-06-27 00:41:40 -0400641 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/* bus */
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500648 * video : video bus device pointer
649 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * 0. The system BIOS should NOT automatically switch(toggle)
651 * the active display output.
652 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100653 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * 2. The _DGS value should be locked.
655 * 3. The system BIOS should not automatically switch (toggle) the
656 * active display output, but instead generate the display switch
657 * event notify code.
658 * lcd_flag :
659 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100660 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500661 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100662 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500663 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400664 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
666
667static int
Len Brown4be44fc2005-08-05 00:44:28 -0400668acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Igor Murzovea9f8852012-03-30 21:32:08 +0400670 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Zhang Ruib0373842012-06-20 09:48:43 +0800672 if (!video->cap._DOS)
673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Igor Murzovea9f8852012-03-30 21:32:08 +0400675 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
676 return -EINVAL;
Jiang Liu0db98202013-06-29 00:24:39 +0800677 video->dos_setting = (lcd_flag << 2) | bios_flag;
678 status = acpi_execute_simple_method(video->device->handle, "_DOS",
679 (lcd_flag << 2) | bios_flag);
Igor Murzovea9f8852012-03-30 21:32:08 +0400680 if (ACPI_FAILURE(status))
681 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Igor Murzovea9f8852012-03-30 21:32:08 +0400683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500687 * Simple comparison function used to sort backlight levels.
688 */
689
690static int
691acpi_video_cmp_level(const void *a, const void *b)
692{
693 return *(int *)a - *(int *)b;
694}
695
696/*
Aaron Lua50188d2013-04-22 14:08:32 +0200697 * Decides if _BQC/_BCQ for this system is usable
698 *
699 * We do this by changing the level first and then read out the current
700 * brightness level, if the value does not match, find out if it is using
701 * index. If not, clear the _BQC/_BCQ capability.
702 */
703static int acpi_video_bqc_quirk(struct acpi_video_device *device,
704 int max_level, int current_level)
705{
706 struct acpi_video_device_brightness *br = device->brightness;
707 int result;
708 unsigned long long level;
709 int test_level;
710
711 /* don't mess with existing known broken systems */
712 if (bqc_offset_aml_bug_workaround)
713 return 0;
714
715 /*
716 * Some systems always report current brightness level as maximum
717 * through _BQC, we need to test another value for them.
718 */
Felipe Contrerasb3b301c2013-08-03 23:00:25 +0200719 test_level = current_level == max_level ? br->levels[3] : max_level;
Aaron Lua50188d2013-04-22 14:08:32 +0200720
721 result = acpi_video_device_lcd_set_level(device, test_level);
722 if (result)
723 return result;
724
725 result = acpi_video_device_lcd_get_level_current(device, &level, true);
726 if (result)
727 return result;
728
729 if (level != test_level) {
730 /* buggy _BQC found, need to find out if it uses index */
731 if (level < br->count) {
732 if (br->flags._BCL_reversed)
733 level = br->count - 3 - level;
734 if (br->levels[level + 2] == test_level)
735 br->flags._BQC_use_index = 1;
736 }
737
738 if (!br->flags._BQC_use_index)
739 device->cap._BQC = device->cap._BCQ = 0;
740 }
741
742 return 0;
743}
744
Aaron Lu05950092016-04-27 20:45:04 +0800745int acpi_video_get_levels(struct acpi_device *device,
Aaron Lu9f9cd7e2016-05-21 15:30:46 +0800746 struct acpi_video_device_brightness **dev_br,
747 int *pmax_level)
Julia Jomantaite469778c2008-06-23 22:50:42 +0100748{
749 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800750 int i, max_level = 0, count = 0, level_ac_battery = 0;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100751 union acpi_object *o;
752 struct acpi_video_device_brightness *br = NULL;
Aaron Lu05950092016-04-27 20:45:04 +0800753 int result = 0;
Hans de Goedebd8ba202014-02-13 16:32:51 +0100754 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100755
Aaron Lu05950092016-04-27 20:45:04 +0800756 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device->handle,
757 &obj))) {
Julia Jomantaite469778c2008-06-23 22:50:42 +0100758 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
759 "LCD brightness level\n"));
Aaron Lu05950092016-04-27 20:45:04 +0800760 result = -ENODEV;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100761 goto out;
762 }
763
Aaron Lu05950092016-04-27 20:45:04 +0800764 if (obj->package.count < 2) {
765 result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100766 goto out;
Aaron Lu05950092016-04-27 20:45:04 +0800767 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100768
769 br = kzalloc(sizeof(*br), GFP_KERNEL);
770 if (!br) {
771 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800772 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100773 goto out;
774 }
775
Zhang Ruid32f6942009-03-18 16:27:12 +0800776 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100777 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800778 if (!br->levels) {
779 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100780 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800781 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100782
783 for (i = 0; i < obj->package.count; i++) {
784 o = (union acpi_object *)&obj->package.elements[i];
785 if (o->type != ACPI_TYPE_INTEGER) {
786 printk(KERN_ERR PREFIX "Invalid data\n");
787 continue;
788 }
Hans de Goedebd8ba202014-02-13 16:32:51 +0100789 value = (u32) o->integer.value;
790 /* Skip duplicate entries */
791 if (count > 2 && br->levels[count - 1] == value)
792 continue;
793
794 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100795
796 if (br->levels[count] > max_level)
797 max_level = br->levels[count];
798 count++;
799 }
800
Zhang Ruid32f6942009-03-18 16:27:12 +0800801 /*
802 * some buggy BIOS don't export the levels
803 * when machine is on AC/Battery in _BCL package.
804 * In this case, the first two elements in _BCL packages
805 * are also supported brightness levels that OS should take care of.
806 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800807 for (i = 2; i < count; i++) {
808 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800809 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800810 if (br->levels[i] == br->levels[1])
811 level_ac_battery++;
812 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500813
Zhang Ruid32f6942009-03-18 16:27:12 +0800814 if (level_ac_battery < 2) {
815 level_ac_battery = 2 - level_ac_battery;
816 br->flags._BCL_no_ac_battery_levels = 1;
817 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
818 br->levels[i] = br->levels[i - level_ac_battery];
819 count += level_ac_battery;
820 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000821 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800822
Zhang Ruid80fb992009-03-18 16:27:14 +0800823 /* Check if the _BCL package is in a reversed order */
824 if (max_level == br->levels[2]) {
825 br->flags._BCL_reversed = 1;
826 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
827 acpi_video_cmp_level, NULL);
828 } else if (max_level != br->levels[count - 1])
829 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000830 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100831
832 br->count = count;
Aaron Lu05950092016-04-27 20:45:04 +0800833 *dev_br = br;
Aaron Lu9f9cd7e2016-05-21 15:30:46 +0800834 if (pmax_level)
835 *pmax_level = max_level;
Aaron Lu05950092016-04-27 20:45:04 +0800836
837out:
838 kfree(obj);
839 return result;
840out_free:
841 kfree(br);
842 goto out;
843}
844EXPORT_SYMBOL(acpi_video_get_levels);
845
846/*
847 * Arg:
848 * device : video output device (LCD, CRT, ..)
849 *
850 * Return Value:
851 * Maximum brightness level
852 *
853 * Allocate and initialize device->brightness.
854 */
855
856static int
857acpi_video_init_brightness(struct acpi_video_device *device)
858{
859 int i, max_level = 0;
860 unsigned long long level, level_old;
861 struct acpi_video_device_brightness *br = NULL;
862 int result = -EINVAL;
863
Aaron Lu9f9cd7e2016-05-21 15:30:46 +0800864 result = acpi_video_get_levels(device->dev, &br, &max_level);
Aaron Lu05950092016-04-27 20:45:04 +0800865 if (result)
866 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100867 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800868
Zhang Rui1a7c6182009-03-18 16:27:16 +0800869 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400870 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800871
872 if (!device->cap._BQC)
873 goto set_level;
874
Danny Baumanna89803d2013-03-19 16:22:50 +0000875 result = acpi_video_device_lcd_get_level_current(device,
876 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800877 if (result)
878 goto out_free_levels;
879
Aaron Lua50188d2013-04-22 14:08:32 +0200880 result = acpi_video_bqc_quirk(device, max_level, level_old);
881 if (result)
882 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800883 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200884 * cap._BQC may get cleared due to _BQC is found to be broken
885 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800886 */
Aaron Lua50188d2013-04-22 14:08:32 +0200887 if (!device->cap._BQC)
888 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800889
Aaron Lu545ef362013-11-15 14:39:12 +0800890 level = acpi_video_bqc_value_to_level(device, level_old);
891 /*
892 * On some buggy laptops, _BQC returns an uninitialized
893 * value when invoked for the first time, i.e.
894 * level_old is invalid (no matter whether it's a level
895 * or an index). Set the backlight to max_level in this case.
896 */
897 for (i = 2; i < br->count; i++)
898 if (level == br->levels[i])
899 break;
900 if (i == br->count || !level)
901 level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800902
Zhang Ruie047cca2009-04-09 14:24:35 +0800903set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400904 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800905 if (result)
906 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800907
Zhang Ruid32f6942009-03-18 16:27:12 +0800908 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Aaron Lu05950092016-04-27 20:45:04 +0800909 "found %d brightness levels\n", br->count - 2));
910 return 0;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100911
912out_free_levels:
913 kfree(br->levels);
Julia Jomantaite469778c2008-06-23 22:50:42 +0100914 kfree(br);
Julia Jomantaite469778c2008-06-23 22:50:42 +0100915 device->brightness = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800916 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100917}
918
919/*
920 * Arg:
921 * device : video output device (LCD, CRT, ..)
922 *
923 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500924 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100926 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * device.
928 */
929
Len Brown4be44fc2005-08-05 00:44:28 -0400930static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Jiang Liu952c63e2013-06-29 00:24:38 +0800932 if (acpi_has_method(device->dev->handle, "_ADR"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 device->cap._ADR = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800934 if (acpi_has_method(device->dev->handle, "_BCL"))
Len Brown4be44fc2005-08-05 00:44:28 -0400935 device->cap._BCL = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800936 if (acpi_has_method(device->dev->handle, "_BCM"))
Len Brown4be44fc2005-08-05 00:44:28 -0400937 device->cap._BCM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800938 if (acpi_has_method(device->dev->handle, "_BQC")) {
Yu Luming2f3d0002006-11-11 02:40:34 +0800939 device->cap._BQC = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800940 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
Zhang Ruic60d6382009-03-18 16:27:18 +0800941 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
942 device->cap._BCQ = 1;
943 }
944
Jiang Liu952c63e2013-06-29 00:24:38 +0800945 if (acpi_has_method(device->dev->handle, "_DDC"))
Len Brown4be44fc2005-08-05 00:44:28 -0400946 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500950 * Arg:
951 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
953 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500954 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100956 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958
Len Brown4be44fc2005-08-05 00:44:28 -0400959static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jiang Liu952c63e2013-06-29 00:24:38 +0800961 if (acpi_has_method(video->device->handle, "_DOS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 video->cap._DOS = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800963 if (acpi_has_method(video->device->handle, "_DOD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 video->cap._DOD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800965 if (acpi_has_method(video->device->handle, "_ROM"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 video->cap._ROM = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800967 if (acpi_has_method(video->device->handle, "_GPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 video->cap._GPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800969 if (acpi_has_method(video->device->handle, "_SPD"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 video->cap._SPD = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +0800971 if (acpi_has_method(video->device->handle, "_VPO"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975/*
976 * Check whether the video bus device has required AML method to
977 * support the desired features
978 */
979
Len Brown4be44fc2005-08-05 00:44:28 -0400980static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Len Brown4be44fc2005-08-05 00:44:28 -0400982 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000983 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000988 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200989 if (!dev)
990 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000991 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200992
Felipe Contreras21fcb342013-08-01 18:43:59 -0500993 /*
994 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * to check well known required nodes.
996 */
997
Julius Volz98fb8fe2007-02-20 16:38:40 +0100998 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200999 if (video->cap._DOS || video->cap._DOD) {
1000 if (!video->cap._DOS) {
1001 printk(KERN_WARNING FW_BUG
1002 "ACPI(%s) defines _DOD but not _DOS\n",
1003 acpi_device_bid(video->device));
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 video->flags.multihead = 1;
1006 status = 0;
1007 }
1008
Julius Volz98fb8fe2007-02-20 16:38:40 +01001009 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001010 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 video->flags.rom = 1;
1012 status = 0;
1013 }
1014
Julius Volz98fb8fe2007-02-20 16:38:40 +01001015 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001016 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 video->flags.post = 1;
1018 status = 0;
1019 }
1020
Patrick Mocheld550d982006-06-27 00:41:40 -04001021 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Felipe Contreras21fcb342013-08-01 18:43:59 -05001024/*
1025 * --------------------------------------------------------------------------
1026 * Driver Interface
1027 * --------------------------------------------------------------------------
1028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001031static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001032acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1033{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001034 struct acpi_video_enumerated_device *ids;
1035 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001036
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001037 for (i = 0; i < video->attached_count; i++) {
1038 ids = &video->attached_array[i];
1039 if ((ids->value.int_val & 0xffff) == device_id)
1040 return &ids->value.attrib;
1041 }
1042
Rui Zhang82cae992007-01-03 23:40:53 -05001043 return NULL;
1044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001047acpi_video_get_device_type(struct acpi_video_bus *video,
1048 unsigned long device_id)
1049{
1050 struct acpi_video_enumerated_device *ids;
1051 int i;
1052
1053 for (i = 0; i < video->attached_count; i++) {
1054 ids = &video->attached_array[i];
1055 if ((ids->value.int_val & 0xffff) == device_id)
1056 return ids->value.int_val;
1057 }
1058
1059 return 0;
1060}
1061
1062static int
Len Brown4be44fc2005-08-05 00:44:28 -04001063acpi_video_bus_get_one_device(struct acpi_device *device,
1064 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001066 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001067 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001068 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001069 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Len Brown4be44fc2005-08-05 00:44:28 -04001071 status =
1072 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001073 /* Some device omits _ADR, we skip them instead of fail */
1074 if (ACPI_FAILURE(status))
1075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Aaron Lu91e13aa2013-04-25 02:47:49 +00001077 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1078 if (!data)
1079 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Aaron Lu91e13aa2013-04-25 02:47:49 +00001081 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1082 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1083 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Aaron Lu91e13aa2013-04-25 02:47:49 +00001085 data->device_id = device_id;
1086 data->video = video;
1087 data->dev = device;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001088 INIT_DELAYED_WORK(&data->switch_brightness_work,
1089 acpi_video_switch_brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Aaron Lu91e13aa2013-04-25 02:47:49 +00001091 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001092
Aaron Lue50b9be2015-10-28 15:09:23 +08001093 if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001094 switch (attribute->display_type) {
1095 case ACPI_VIDEO_DISPLAY_CRT:
1096 data->flags.crt = 1;
1097 break;
1098 case ACPI_VIDEO_DISPLAY_TV:
1099 data->flags.tvout = 1;
1100 break;
1101 case ACPI_VIDEO_DISPLAY_DVI:
1102 data->flags.dvi = 1;
1103 break;
1104 case ACPI_VIDEO_DISPLAY_LCD:
1105 data->flags.lcd = 1;
1106 break;
1107 default:
1108 data->flags.unknown = 1;
1109 break;
1110 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001111 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001112 data->flags.bios = 1;
1113 } else {
1114 /* Check for legacy IDs */
1115 device_type = acpi_video_get_device_type(video, device_id);
1116 /* Ignore bits 16 and 18-20 */
1117 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001118 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1119 data->flags.crt = 1;
1120 break;
1121 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1122 data->flags.lcd = 1;
1123 break;
1124 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1125 data->flags.tvout = 1;
1126 break;
1127 default:
1128 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Aaron Lu91e13aa2013-04-25 02:47:49 +00001132 acpi_video_device_bind(video, data);
1133 acpi_video_device_find_cap(data);
1134
Aaron Lu91e13aa2013-04-25 02:47:49 +00001135 mutex_lock(&video->device_list_lock);
1136 list_add_tail(&data->entry, &video->video_device_list);
1137 mutex_unlock(&video->device_list_lock);
1138
1139 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142/*
1143 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001144 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 *
1146 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001147 * none
1148 *
1149 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * bind the ids with the corresponding video devices
1151 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Len Brown4be44fc2005-08-05 00:44:28 -04001154static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001156 struct acpi_video_device *dev;
1157
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001158 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001159
1160 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001161 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001162
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001163 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001168 * video : video bus device
1169 * device : video output device under the video
1170 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *
1172 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001173 * none
1174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 * Bind the ids with the corresponding video devices
1176 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static void
Len Brown4be44fc2005-08-05 00:44:28 -04001180acpi_video_device_bind(struct acpi_video_bus *video,
1181 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001183 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001184 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001186 for (i = 0; i < video->attached_count; i++) {
1187 ids = &video->attached_array[i];
1188 if (device->device_id == (ids->value.int_val & 0xffff)) {
1189 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1191 }
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Aaron Lu0b8db272014-09-30 14:10:17 +08001195static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1196{
1197 struct acpi_video_bus *video = device->video;
1198 int i;
1199
Aaron Lub4df4632014-12-15 16:01:29 +08001200 /*
1201 * If we have a broken _DOD or we have more than 8 output devices
1202 * under the graphics controller node that we can't proper deal with
1203 * in the operation region code currently, no need to test.
1204 */
1205 if (!video->attached_count || video->child_count > 8)
Aaron Lu0b8db272014-09-30 14:10:17 +08001206 return true;
1207
1208 for (i = 0; i < video->attached_count; i++) {
Aaron Lu35d05652014-12-01 02:09:18 +01001209 if ((video->attached_array[i].value.int_val & 0xfff) ==
1210 (device->device_id & 0xfff))
Aaron Lu0b8db272014-09-30 14:10:17 +08001211 return true;
1212 }
1213
1214 return false;
1215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/*
1218 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001219 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 *
1221 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001222 * < 0 : error
1223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * Call _DOD to enumerate all devices attached to display adapter
1225 *
Len Brown4be44fc2005-08-05 00:44:28 -04001226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1229{
Len Brown4be44fc2005-08-05 00:44:28 -04001230 int status;
1231 int count;
1232 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001233 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1235 union acpi_object *dod = NULL;
1236 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Alex Hunge34fbba2016-05-27 15:47:06 +08001238 if (!video->cap._DOD)
1239 return AE_NOT_EXIST;
1240
Patrick Mochel90130262006-05-19 16:54:48 -04001241 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001243 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001244 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001247 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001249 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 status = -EFAULT;
1251 goto out;
1252 }
1253
1254 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001255 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001257 active_list = kcalloc(1 + dod->package.count,
1258 sizeof(struct acpi_video_enumerated_device),
1259 GFP_KERNEL);
1260 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 status = -ENOMEM;
1262 goto out;
1263 }
1264
1265 count = 0;
1266 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001267 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001270 printk(KERN_ERR PREFIX
1271 "Invalid _DOD data in element %d\n", i);
1272 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001274
1275 active_list[count].value.int_val = obj->integer.value;
1276 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1278 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 count++;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jesper Juhl6044ec82005-11-07 01:01:32 -08001282 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001283
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001284 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001286
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001287out:
Len Brown02438d82006-06-30 03:19:10 -04001288 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Len Brown4be44fc2005-08-05 00:44:28 -04001292static int
1293acpi_video_get_next_level(struct acpi_video_device *device,
1294 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001296 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001297 max = max_below = 0;
1298 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001299 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001300 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001301 l = device->brightness->levels[i];
1302 if (abs(l - level_current) < abs(delta)) {
1303 delta = l - level_current;
1304 if (!delta)
1305 break;
1306 }
1307 }
1308 /* Ajust level_current to closest available level */
1309 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001310 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001311 l = device->brightness->levels[i];
1312 if (l < min)
1313 min = l;
1314 if (l > max)
1315 max = l;
1316 if (l < min_above && l > level_current)
1317 min_above = l;
1318 if (l > max_below && l < level_current)
1319 max_below = l;
1320 }
1321
1322 switch (event) {
1323 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1324 return (level_current < max) ? min_above : min;
1325 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1326 return (level_current < max) ? min_above : max;
1327 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1328 return (level_current > min) ? max_below : min;
1329 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1330 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1331 return 0;
1332 default:
1333 return level_current;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001337static void
1338acpi_video_switch_brightness(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001340 struct acpi_video_device *device = container_of(to_delayed_work(work),
1341 struct acpi_video_device, switch_brightness_work);
Matthew Wilcox27663c52008-10-10 02:22:59 -04001342 unsigned long long level_current, level_next;
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001343 int event = device->switch_brightness_event;
Zhang Ruic8890f92009-03-18 16:27:08 +08001344 int result = -EINVAL;
1345
Aaron Lufbc9fe12013-10-11 21:27:45 +08001346 /* no warning message if acpi_backlight=vendor or a quirk is used */
Hans de Goede654a1822015-06-09 10:32:25 +02001347 if (!device->backlight)
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001348 return;
Zhang Rui28c32e92009-07-13 10:33:24 +08001349
Julia Jomantaite469778c2008-06-23 22:50:42 +01001350 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001351 goto out;
1352
1353 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001354 &level_current,
1355 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001356 if (result)
1357 goto out;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001360
Zhang Rui24450c72009-03-18 16:27:10 +08001361 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001362
Matthew Garrett36342742009-07-14 17:06:03 +01001363 if (!result)
1364 backlight_force_update(device->backlight,
1365 BACKLIGHT_UPDATE_HOTKEY);
1366
Zhang Ruic8890f92009-03-18 16:27:08 +08001367out:
1368 if (result)
1369 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Matthew Garrette92a7162010-01-12 14:17:03 -05001372int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1373 void **edid)
1374{
1375 struct acpi_video_bus *video;
1376 struct acpi_video_device *video_device;
1377 union acpi_object *buffer = NULL;
1378 acpi_status status;
1379 int i, length;
1380
1381 if (!device || !acpi_driver_data(device))
1382 return -EINVAL;
1383
1384 video = acpi_driver_data(device);
1385
1386 for (i = 0; i < video->attached_count; i++) {
1387 video_device = video->attached_array[i].bind_info;
1388 length = 256;
1389
1390 if (!video_device)
1391 continue;
1392
Zhang Rui82069552010-12-06 15:04:24 +08001393 if (!video_device->cap._DDC)
1394 continue;
1395
Matthew Garrette92a7162010-01-12 14:17:03 -05001396 if (type) {
1397 switch (type) {
1398 case ACPI_VIDEO_DISPLAY_CRT:
1399 if (!video_device->flags.crt)
1400 continue;
1401 break;
1402 case ACPI_VIDEO_DISPLAY_TV:
1403 if (!video_device->flags.tvout)
1404 continue;
1405 break;
1406 case ACPI_VIDEO_DISPLAY_DVI:
1407 if (!video_device->flags.dvi)
1408 continue;
1409 break;
1410 case ACPI_VIDEO_DISPLAY_LCD:
1411 if (!video_device->flags.lcd)
1412 continue;
1413 break;
1414 }
1415 } else if (video_device->device_id != device_id) {
1416 continue;
1417 }
1418
1419 status = acpi_video_device_EDID(video_device, &buffer, length);
1420
1421 if (ACPI_FAILURE(status) || !buffer ||
1422 buffer->type != ACPI_TYPE_BUFFER) {
1423 length = 128;
1424 status = acpi_video_device_EDID(video_device, &buffer,
1425 length);
1426 if (ACPI_FAILURE(status) || !buffer ||
1427 buffer->type != ACPI_TYPE_BUFFER) {
1428 continue;
1429 }
1430 }
1431
1432 *edid = buffer->buffer.pointer;
1433 return length;
1434 }
1435
1436 return -ENODEV;
1437}
1438EXPORT_SYMBOL(acpi_video_get_edid);
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static int
Len Brown4be44fc2005-08-05 00:44:28 -04001441acpi_video_bus_get_devices(struct acpi_video_bus *video,
1442 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Igor Murzovfba4e082012-10-13 04:41:25 +04001444 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001445 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Igor Murzovfba4e082012-10-13 04:41:25 +04001447 /*
1448 * There are systems where video module known to work fine regardless
1449 * of broken _DOD and ignoring returned value here doesn't cause
1450 * any issues later.
1451 */
1452 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001454 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001457 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001458 dev_err(&dev->dev, "Can't attach device\n");
1459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Aaron Lub4df4632014-12-15 16:01:29 +08001461 video->child_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/* acpi_video interface */
1467
Aaron Luefaa14c2013-07-16 13:08:05 +08001468/*
1469 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1470 * preform any automatic brightness change on receiving a notification.
1471 */
Len Brown4be44fc2005-08-05 00:44:28 -04001472static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Aaron Luefaa14c2013-07-16 13:08:05 +08001474 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001475 acpi_osi_is_win8() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Len Brown4be44fc2005-08-05 00:44:28 -04001478static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Aaron Luefaa14c2013-07-16 13:08:05 +08001480 return acpi_video_bus_DOS(video, 0,
Aaron Lufbc9fe12013-10-11 21:27:45 +08001481 acpi_osi_is_win8() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Bjorn Helgaas70155582009-04-07 15:37:11 +00001484static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001486 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001487 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001488 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001489
Aaron Lu67b662e2013-10-11 21:27:44 +08001490 if (!video || !video->input)
Patrick Mocheld550d982006-06-27 00:41:40 -04001491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Luming Yue9dab192007-08-20 18:23:53 +08001493 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001496 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * most likely via hotkey. */
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001498 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
1500
Julius Volz98fb8fe2007-02-20 16:38:40 +01001501 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * connector. */
1503 acpi_video_device_enumerate(video);
1504 acpi_video_device_rebind(video);
Luming Yue9dab192007-08-20 18:23:53 +08001505 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507
Len Brown4be44fc2005-08-05 00:44:28 -04001508 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001509 keycode = KEY_SWITCHVIDEOMODE;
1510 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001511 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001512 keycode = KEY_VIDEO_NEXT;
1513 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001514 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Luming Yue9dab192007-08-20 18:23:53 +08001515 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 break;
1517
1518 default:
1519 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001520 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522 }
1523
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001524 if (acpi_notifier_call_chain(device, event, 0))
1525 /* Something vetoed the keypress. */
1526 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001527
Hans de Goede05bc59a2015-12-22 19:09:51 +01001528 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
Matthew Garrett17c452f2009-12-11 17:40:46 -05001529 input_report_key(input, keycode, 1);
1530 input_sync(input);
1531 input_report_key(input, keycode, 0);
1532 input_sync(input);
1533 }
Luming Yue9dab192007-08-20 18:23:53 +08001534
Patrick Mocheld550d982006-06-27 00:41:40 -04001535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001538static void brightness_switch_event(struct acpi_video_device *video_device,
1539 u32 event)
1540{
1541 if (!brightness_switch_enabled)
1542 return;
1543
1544 video_device->switch_brightness_event = event;
1545 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1546}
1547
Len Brown4be44fc2005-08-05 00:44:28 -04001548static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001550 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001551 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001552 struct acpi_video_bus *bus;
1553 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001554 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001559 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001560 bus = video_device->video;
1561 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001564 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001565 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001566 keycode = KEY_BRIGHTNESS_CYCLE;
1567 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001568 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001569 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001570 keycode = KEY_BRIGHTNESSUP;
1571 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001572 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001573 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001574 keycode = KEY_BRIGHTNESSDOWN;
1575 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001576 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001577 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001578 keycode = KEY_BRIGHTNESS_ZERO;
1579 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001580 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Linus Torvalds8ab58e82014-07-18 14:32:51 +02001581 brightness_switch_event(video_device, event);
Luming Yue9dab192007-08-20 18:23:53 +08001582 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 default:
1585 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001586 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 }
Luming Yue9dab192007-08-20 18:23:53 +08001589
Zhang Rui7761f632008-01-25 14:48:12 +08001590 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001591
Hans de Goede05bc59a2015-12-22 19:09:51 +01001592 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
Matthew Garrett17c452f2009-12-11 17:40:46 -05001593 input_report_key(input, keycode, 1);
1594 input_sync(input);
1595 input_report_key(input, keycode, 0);
1596 input_sync(input);
1597 }
Luming Yue9dab192007-08-20 18:23:53 +08001598
Patrick Mocheld550d982006-06-27 00:41:40 -04001599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001602static int acpi_video_resume(struct notifier_block *nb,
1603 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001604{
1605 struct acpi_video_bus *video;
1606 struct acpi_video_device *video_device;
1607 int i;
1608
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001609 switch (val) {
1610 case PM_HIBERNATION_PREPARE:
1611 case PM_SUSPEND_PREPARE:
1612 case PM_RESTORE_PREPARE:
1613 return NOTIFY_DONE;
1614 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001615
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001616 video = container_of(nb, struct acpi_video_bus, pm_nb);
1617
1618 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001619
1620 for (i = 0; i < video->attached_count; i++) {
1621 video_device = video->attached_array[i].bind_info;
Hans de Goede654a1822015-06-09 10:32:25 +02001622 if (video_device && video_device->brightness)
1623 acpi_video_device_lcd_set_level(video_device,
1624 video_device->brightness->curr);
Matthew Garrett863c1492008-02-04 23:31:24 -08001625 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001626
1627 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001628}
1629
Zhang Ruic504f8c2009-12-30 15:59:23 +08001630static acpi_status
1631acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1632 void **return_value)
1633{
1634 struct acpi_device *device = context;
1635 struct acpi_device *sibling;
1636 int result;
1637
1638 if (handle == device->handle)
1639 return AE_CTRL_TERMINATE;
1640
1641 result = acpi_bus_get_device(handle, &sibling);
1642 if (result)
1643 return AE_OK;
1644
1645 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1646 return AE_ALREADY_EXISTS;
1647
1648 return AE_OK;
1649}
1650
Aaron Lu67b662e2013-10-11 21:27:44 +08001651static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1652{
Hans de Goede99678ed2014-05-15 13:22:33 +02001653 struct backlight_properties props;
1654 struct pci_dev *pdev;
1655 acpi_handle acpi_parent;
1656 struct device *parent = NULL;
1657 int result;
1658 static int count;
1659 char *name;
Aaron Lu67b662e2013-10-11 21:27:44 +08001660
Hans de Goede99678ed2014-05-15 13:22:33 +02001661 result = acpi_video_init_brightness(device);
1662 if (result)
1663 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001664
1665 if (disable_backlight_sysfs_if > 0)
1666 return;
1667
Hans de Goede99678ed2014-05-15 13:22:33 +02001668 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1669 if (!name)
1670 return;
1671 count++;
Aaron Lu67b662e2013-10-11 21:27:44 +08001672
Hans de Goede99678ed2014-05-15 13:22:33 +02001673 acpi_get_parent(device->dev->handle, &acpi_parent);
Aaron Lu67b662e2013-10-11 21:27:44 +08001674
Hans de Goede99678ed2014-05-15 13:22:33 +02001675 pdev = acpi_get_pci_dev(acpi_parent);
1676 if (pdev) {
1677 parent = &pdev->dev;
1678 pci_dev_put(pdev);
Aaron Lu67b662e2013-10-11 21:27:44 +08001679 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001680
1681 memset(&props, 0, sizeof(struct backlight_properties));
1682 props.type = BACKLIGHT_FIRMWARE;
1683 props.max_brightness = device->brightness->count - 3;
1684 device->backlight = backlight_device_register(name,
1685 parent,
1686 device,
1687 &acpi_backlight_ops,
1688 &props);
1689 kfree(name);
Hans de Goede654a1822015-06-09 10:32:25 +02001690 if (IS_ERR(device->backlight)) {
1691 device->backlight = NULL;
Hans de Goede99678ed2014-05-15 13:22:33 +02001692 return;
Hans de Goede654a1822015-06-09 10:32:25 +02001693 }
Hans de Goede99678ed2014-05-15 13:22:33 +02001694
1695 /*
1696 * Save current brightness level in case we have to restore it
1697 * before acpi_video_device_lcd_set_level() is called next time.
1698 */
1699 device->backlight->props.brightness =
1700 acpi_video_get_brightness(device->backlight);
1701
1702 device->cooling_dev = thermal_cooling_device_register("LCD",
1703 device->dev, &video_cooling_ops);
1704 if (IS_ERR(device->cooling_dev)) {
1705 /*
1706 * Set cooling_dev to NULL so we don't crash trying to free it.
1707 * Also, why the hell we are returning early and not attempt to
1708 * register video output if cooling device registration failed?
1709 * -- dtor
1710 */
1711 device->cooling_dev = NULL;
1712 return;
1713 }
1714
1715 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1716 device->cooling_dev->id);
1717 result = sysfs_create_link(&device->dev->dev.kobj,
1718 &device->cooling_dev->device.kobj,
1719 "thermal_cooling");
1720 if (result)
1721 printk(KERN_ERR PREFIX "Create sysfs link\n");
1722 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1723 &device->dev->dev.kobj, "device");
1724 if (result)
1725 printk(KERN_ERR PREFIX "Create sysfs link\n");
Aaron Lu67b662e2013-10-11 21:27:44 +08001726}
1727
Aaron Ludce4ec22014-10-28 14:35:59 +08001728static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1729{
1730 struct acpi_video_device *dev;
1731 union acpi_object *levels;
1732
1733 mutex_lock(&video->device_list_lock);
1734 list_for_each_entry(dev, &video->video_device_list, entry) {
Aaron Lu9f9cd7e2016-05-21 15:30:46 +08001735 if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
Aaron Ludce4ec22014-10-28 14:35:59 +08001736 kfree(levels);
1737 }
1738 mutex_unlock(&video->device_list_lock);
1739}
1740
Aaron Lue50b9be2015-10-28 15:09:23 +08001741static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1742{
1743 /*
1744 * Do not create backlight device for video output
1745 * device that is not in the enumerated list.
1746 */
1747 if (!acpi_video_device_in_dod(dev)) {
1748 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1749 return false;
1750 }
1751
1752 if (only_lcd)
1753 return dev->flags.lcd;
1754 return true;
1755}
1756
Aaron Lu67b662e2013-10-11 21:27:44 +08001757static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1758{
1759 struct acpi_video_device *dev;
1760
Hans de Goede53a92ba2014-05-21 15:39:55 +02001761 if (video->backlight_registered)
1762 return 0;
1763
Aaron Ludce4ec22014-10-28 14:35:59 +08001764 acpi_video_run_bcl_for_osi(video);
1765
Hans de Goede3bd6bce2015-06-16 16:27:51 +02001766 if (acpi_video_get_backlight_type() != acpi_backlight_video)
Hans de Goede99678ed2014-05-15 13:22:33 +02001767 return 0;
1768
Aaron Lu67b662e2013-10-11 21:27:44 +08001769 mutex_lock(&video->device_list_lock);
Aaron Lue50b9be2015-10-28 15:09:23 +08001770 list_for_each_entry(dev, &video->video_device_list, entry) {
1771 if (acpi_video_should_register_backlight(dev))
1772 acpi_video_dev_register_backlight(dev);
1773 }
Aaron Lu67b662e2013-10-11 21:27:44 +08001774 mutex_unlock(&video->device_list_lock);
1775
Hans de Goede99678ed2014-05-15 13:22:33 +02001776 video->backlight_registered = true;
1777
Aaron Lu67b662e2013-10-11 21:27:44 +08001778 video->pm_nb.notifier_call = acpi_video_resume;
1779 video->pm_nb.priority = 0;
1780 return register_pm_notifier(&video->pm_nb);
1781}
1782
1783static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1784{
1785 if (device->backlight) {
1786 backlight_device_unregister(device->backlight);
1787 device->backlight = NULL;
1788 }
1789 if (device->brightness) {
1790 kfree(device->brightness->levels);
1791 kfree(device->brightness);
1792 device->brightness = NULL;
1793 }
1794 if (device->cooling_dev) {
1795 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1796 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1797 thermal_cooling_device_unregister(device->cooling_dev);
1798 device->cooling_dev = NULL;
1799 }
1800}
1801
1802static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1803{
1804 struct acpi_video_device *dev;
Hans de Goede99678ed2014-05-15 13:22:33 +02001805 int error;
1806
1807 if (!video->backlight_registered)
1808 return 0;
1809
1810 error = unregister_pm_notifier(&video->pm_nb);
Aaron Lu67b662e2013-10-11 21:27:44 +08001811
1812 mutex_lock(&video->device_list_lock);
1813 list_for_each_entry(dev, &video->video_device_list, entry)
1814 acpi_video_dev_unregister_backlight(dev);
1815 mutex_unlock(&video->device_list_lock);
1816
Hans de Goede99678ed2014-05-15 13:22:33 +02001817 video->backlight_registered = false;
1818
Aaron Lu67b662e2013-10-11 21:27:44 +08001819 return error;
1820}
1821
1822static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1823{
1824 acpi_status status;
1825 struct acpi_device *adev = device->dev;
1826
1827 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1828 acpi_video_device_notify, device);
1829 if (ACPI_FAILURE(status))
1830 dev_err(&adev->dev, "Error installing notify handler\n");
1831 else
1832 device->flags.notify = 1;
1833}
1834
1835static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1836{
1837 struct input_dev *input;
1838 struct acpi_video_device *dev;
1839 int error;
1840
1841 video->input = input = input_allocate_device();
1842 if (!input) {
1843 error = -ENOMEM;
1844 goto out;
1845 }
1846
1847 error = acpi_video_bus_start_devices(video);
1848 if (error)
1849 goto err_free_input;
1850
1851 snprintf(video->phys, sizeof(video->phys),
1852 "%s/video/input0", acpi_device_hid(video->device));
1853
1854 input->name = acpi_device_name(video->device);
1855 input->phys = video->phys;
1856 input->id.bustype = BUS_HOST;
1857 input->id.product = 0x06;
1858 input->dev.parent = &video->device->dev;
1859 input->evbit[0] = BIT(EV_KEY);
1860 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1861 set_bit(KEY_VIDEO_NEXT, input->keybit);
1862 set_bit(KEY_VIDEO_PREV, input->keybit);
1863 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1864 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1865 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1866 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1867 set_bit(KEY_DISPLAY_OFF, input->keybit);
1868
1869 error = input_register_device(input);
1870 if (error)
1871 goto err_stop_dev;
1872
1873 mutex_lock(&video->device_list_lock);
1874 list_for_each_entry(dev, &video->video_device_list, entry)
1875 acpi_video_dev_add_notify_handler(dev);
1876 mutex_unlock(&video->device_list_lock);
1877
1878 return 0;
1879
1880err_stop_dev:
1881 acpi_video_bus_stop_devices(video);
1882err_free_input:
1883 input_free_device(input);
1884 video->input = NULL;
1885out:
1886 return error;
1887}
1888
1889static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1890{
1891 if (dev->flags.notify) {
1892 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1893 acpi_video_device_notify);
1894 dev->flags.notify = 0;
1895 }
1896}
1897
1898static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1899{
1900 struct acpi_video_device *dev;
1901
1902 mutex_lock(&video->device_list_lock);
1903 list_for_each_entry(dev, &video->video_device_list, entry)
1904 acpi_video_dev_remove_notify_handler(dev);
1905 mutex_unlock(&video->device_list_lock);
1906
1907 acpi_video_bus_stop_devices(video);
1908 input_unregister_device(video->input);
1909 video->input = NULL;
1910}
1911
1912static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1913{
1914 struct acpi_video_device *dev, *next;
1915
1916 mutex_lock(&video->device_list_lock);
1917 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1918 list_del(&dev->entry);
1919 kfree(dev);
1920 }
1921 mutex_unlock(&video->device_list_lock);
1922
1923 return 0;
1924}
1925
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001926static int instance;
1927
Len Brown4be44fc2005-08-05 00:44:28 -04001928static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001930 struct acpi_video_bus *video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001931 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001932 acpi_status status;
1933
1934 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1935 device->parent->handle, 1,
1936 acpi_video_bus_match, NULL,
1937 device, NULL);
1938 if (status == AE_ALREADY_EXISTS) {
1939 printk(KERN_WARNING FW_BUG
1940 "Duplicate ACPI video bus devices for the"
1941 " same VGA controller, please try module "
1942 "parameter \"video.allow_duplicates=1\""
1943 "if the current driver doesn't work.\n");
1944 if (!allow_duplicates)
1945 return -ENODEV;
1946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Burman Yan36bcbec2006-12-19 12:56:11 -08001948 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001950 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Zhang Ruie6d9da12007-08-25 02:23:31 -04001952 /* a hack to fix the duplicate name "VID" problem on T61 */
1953 if (!strcmp(device->pnp.bus_id, "VID")) {
1954 if (instance)
1955 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001956 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001957 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001958 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1959 if (!strcmp(device->pnp.bus_id, "VGA")) {
1960 if (instance)
1961 device->pnp.bus_id[3] = '0' + instance;
1962 instance++;
1963 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001964
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001965 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1967 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001968 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001971 error = acpi_video_bus_check(video);
1972 if (error)
1973 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001975 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 INIT_LIST_HEAD(&video->video_device_list);
1977
Igor Murzovea9f8852012-03-30 21:32:08 +04001978 error = acpi_video_bus_get_devices(video, device);
1979 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001980 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001983 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1984 video->flags.multihead ? "yes" : "no",
1985 video->flags.rom ? "yes" : "no",
1986 video->flags.post ? "yes" : "no");
Aaron Lu67b662e2013-10-11 21:27:44 +08001987 mutex_lock(&video_list_lock);
1988 list_add_tail(&video->entry, &video_bus_head);
1989 mutex_unlock(&video_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Aaron Lu67b662e2013-10-11 21:27:44 +08001991 acpi_video_bus_register_backlight(video);
1992 acpi_video_bus_add_notify_handler(video);
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001993
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Aaron Lu67b662e2013-10-11 21:27:44 +08001996err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001997 acpi_video_bus_put_devices(video);
1998 kfree(video->attached_array);
Aaron Lu67b662e2013-10-11 21:27:44 +08001999err_free_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002000 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002001 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002002
2003 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01002006static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Len Brown4be44fc2005-08-05 00:44:28 -04002008 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002012 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002014 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Aaron Lu67b662e2013-10-11 21:27:44 +08002016 acpi_video_bus_remove_notify_handler(video);
2017 acpi_video_bus_unregister_backlight(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Aaron Lu67b662e2013-10-11 21:27:44 +08002020 mutex_lock(&video_list_lock);
2021 list_del(&video->entry);
2022 mutex_unlock(&video_list_lock);
2023
Jesper Juhl6044ec82005-11-07 01:01:32 -08002024 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 kfree(video);
2026
Patrick Mocheld550d982006-06-27 00:41:40 -04002027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Alan Coxc6996bd2012-04-25 14:33:48 +01002030static int __init is_i740(struct pci_dev *dev)
2031{
2032 if (dev->device == 0x00D1)
2033 return 1;
2034 if (dev->device == 0x7000)
2035 return 1;
2036 return 0;
2037}
2038
Matthew Garrett74a365b2009-03-19 21:35:39 +00002039static int __init intel_opregion_present(void)
2040{
Alan Coxc6996bd2012-04-25 14:33:48 +01002041 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002042 struct pci_dev *dev = NULL;
2043 u32 address;
2044
2045 for_each_pci_dev(dev) {
2046 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2047 continue;
2048 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2049 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002050 /* We don't want to poke around undefined i740 registers */
2051 if (is_i740(dev))
2052 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002053 pci_read_config_dword(dev, 0xfc, &address);
2054 if (!address)
2055 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01002056 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002057 }
Alan Coxc6996bd2012-04-25 14:33:48 +01002058 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00002059}
2060
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002061int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002063 int ret = 0;
Chris Wilson28d63402015-03-01 10:41:38 +00002064
Hans de Goede970530c2016-01-14 09:41:45 +01002065 mutex_lock(&register_count_mutex);
2066 if (register_count) {
Zhao Yakui86e437f2009-06-16 11:23:13 +08002067 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002068 * if the function of acpi_video_register is already called,
2069 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08002070 */
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002071 goto leave;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Hans de Goede7ee33ba2015-06-16 16:27:53 +02002074 dmi_check_system(video_dmi_table);
2075
Chris Wilson28d63402015-03-01 10:41:38 +00002076 ret = acpi_bus_register_driver(&acpi_video_bus);
2077 if (ret)
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002078 goto leave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Zhao Yakui86e437f2009-06-16 11:23:13 +08002080 /*
2081 * When the acpi_video_bus is loaded successfully, increase
2082 * the counter reference.
2083 */
Hans de Goede970530c2016-01-14 09:41:45 +01002084 register_count = 1;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002085
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002086leave:
Hans de Goede970530c2016-01-14 09:41:45 +01002087 mutex_unlock(&register_count_mutex);
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02002090EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00002091
Zhao Yakui86e437f2009-06-16 11:23:13 +08002092void acpi_video_unregister(void)
2093{
Hans de Goede970530c2016-01-14 09:41:45 +01002094 mutex_lock(&register_count_mutex);
2095 if (register_count) {
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002096 acpi_bus_unregister_driver(&acpi_video_bus);
Hans de Goede970530c2016-01-14 09:41:45 +01002097 register_count = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002098 }
Hans de Goede970530c2016-01-14 09:41:45 +01002099 mutex_unlock(&register_count_mutex);
Zhao Yakui86e437f2009-06-16 11:23:13 +08002100}
2101EXPORT_SYMBOL(acpi_video_unregister);
2102
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002103void acpi_video_unregister_backlight(void)
2104{
2105 struct acpi_video_bus *video;
2106
Hans de Goede970530c2016-01-14 09:41:45 +01002107 mutex_lock(&register_count_mutex);
2108 if (register_count) {
Hans de Goede2a8b18e2015-06-16 16:27:54 +02002109 mutex_lock(&video_list_lock);
2110 list_for_each_entry(video, &video_bus_head, entry)
2111 acpi_video_bus_unregister_backlight(video);
2112 mutex_unlock(&video_list_lock);
2113 }
Hans de Goede970530c2016-01-14 09:41:45 +01002114 mutex_unlock(&register_count_mutex);
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002115}
Hans de Goede1b7f37e2014-05-17 10:48:01 +02002116
Hans de Goede90b066b2015-12-22 19:09:48 +01002117bool acpi_video_handles_brightness_key_presses(void)
2118{
2119 bool have_video_busses;
2120
2121 mutex_lock(&video_list_lock);
2122 have_video_busses = !list_empty(&video_bus_head);
2123 mutex_unlock(&video_list_lock);
2124
Hans de Goede05bc59a2015-12-22 19:09:51 +01002125 return have_video_busses &&
2126 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
Hans de Goede90b066b2015-12-22 19:09:48 +01002127}
2128EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
2129
Matthew Garrett74a365b2009-03-19 21:35:39 +00002130/*
2131 * This is kind of nasty. Hardware using Intel chipsets may require
2132 * the video opregion code to be run first in order to initialise
2133 * state before any ACPI video calls are made. To handle this we defer
2134 * registration of the video class until the opregion code has run.
2135 */
2136
2137static int __init acpi_video_init(void)
2138{
Chris Wilson6e17cb12015-03-01 10:41:37 +00002139 /*
2140 * Let the module load even if ACPI is disabled (e.g. due to
2141 * a broken BIOS) so that i915.ko can still be loaded on such
2142 * old systems without an AcpiOpRegion.
2143 *
2144 * acpi_video_register() will report -ENODEV later as well due
2145 * to acpi_disabled when i915.ko tries to register itself afterwards.
2146 */
2147 if (acpi_disabled)
2148 return 0;
2149
Matthew Garrett74a365b2009-03-19 21:35:39 +00002150 if (intel_opregion_present())
2151 return 0;
2152
2153 return acpi_video_register();
2154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Zhao Yakui86e437f2009-06-16 11:23:13 +08002156static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Hans de Goede93a291d2015-06-16 16:27:52 +02002158 acpi_video_detect_exit();
Zhao Yakui86e437f2009-06-16 11:23:13 +08002159 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Patrick Mocheld550d982006-06-27 00:41:40 -04002161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164module_init(acpi_video_init);
2165module_exit(acpi_video_exit);