blob: 8681727e318569d07b17d3d3306030e9a46aa134 [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 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Luming Yue9dab192007-08-20 18:23:53 +080033#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080034#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080035#include <linux/thermal.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050036#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000037#include <linux/pci.h>
38#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040041#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020044#include <linux/suspend.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050045#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Rafael J. Wysocki8c5bd7a2013-07-18 02:08:06 +020047#include "internal.h"
48
Len Browna192a952009-07-28 16:45:54 -040049#define PREFIX "ACPI: "
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_VIDEO_BUS_NAME "Video Bus"
52#define ACPI_VIDEO_DEVICE_NAME "Video Device"
53#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
54#define ACPI_VIDEO_NOTIFY_PROBE 0x81
55#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
56#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
57#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
58
Thomas Tuttlef4715182006-12-19 12:56:14 -080059#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
60#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
61#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
62#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
63#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Yu Luming2f3d0002006-11-11 02:40:34 +080065#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050068ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Len Brownf52fd662007-02-12 22:42:12 -050070MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050071MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_LICENSE("GPL");
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080075module_param(brightness_switch_enabled, bool, 0644);
76
Zhang Ruic504f8c2009-12-30 15:59:23 +080077/*
78 * By default, we don't allow duplicate ACPI video bus devices
79 * under the same VGA controller
80 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103081static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080082module_param(allow_duplicates, bool, 0644);
83
Zhang Rui99fd1892010-12-06 15:04:27 +080084/*
85 * Some BIOSes claim they use minimum backlight at boot,
86 * and this may bring dimming screen after boot
87 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103088static bool use_bios_initial_backlight = 1;
Zhang Rui99fd1892010-12-06 15:04:27 +080089module_param(use_bios_initial_backlight, bool, 0644);
90
Felipe Contreras915ea7e2013-08-03 22:12:50 +020091static int register_count;
Len Brown4be44fc2005-08-05 00:44:28 -040092static int acpi_video_bus_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010093static int acpi_video_bus_remove(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000094static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Thomas Renninger1ba90e32007-07-23 14:44:41 +020096static const struct acpi_device_id video_device_ids[] = {
97 {ACPI_VIDEO_HID, 0},
98 {"", 0},
99};
100MODULE_DEVICE_TABLE(acpi, video_device_ids);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500103 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200105 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .ops = {
107 .add = acpi_video_bus_add,
108 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000109 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 u8 multihead:1; /* can switch video heads */
115 u8 rom:1; /* can retrieve a video rom */
116 u8 post:1; /* can configure the head to */
117 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_video_bus_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500121 u8 _DOS:1; /* Enable/Disable output switching */
122 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
123 u8 _ROM:1; /* Get ROM Data */
124 u8 _GPD:1; /* Get POST Device */
125 u8 _SPD:1; /* Set POST Device */
126 u8 _VPO:1; /* Video POST Options */
Len Brown4be44fc2005-08-05 00:44:28 -0400127 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130struct acpi_video_device_attrib {
131 u32 display_index:4; /* A zero-based instance of the Display */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500132 u32 display_port_attachment:4; /* This field differentiates the display type */
133 u32 display_type:4; /* Describe the specific type in use */
134 u32 vendor_specific:4; /* Chipset Vendor Specific */
135 u32 bios_can_detect:1; /* BIOS can detect the device */
136 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
Len Brown4be44fc2005-08-05 00:44:28 -0400137 the VGA device. */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500138 u32 pipe_id:3; /* For VGA multiple-head devices. */
139 u32 reserved:10; /* Must be 0 */
140 u32 device_id_scheme:1; /* Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_video_enumerated_device {
144 union {
145 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } value;
148 struct acpi_video_device *bind_info;
149};
150
151struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400152 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 attached_count;
156 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500159 struct mutex device_list_lock; /* protects video_device_list */
Luming Yue9dab192007-08-20 18:23:53 +0800160 struct input_dev *input;
161 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200162 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 crt:1;
167 u8 lcd:1;
168 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500169 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 u8 bios:1;
171 u8 unknown:1;
Aaron Lu91e13aa2013-04-25 02:47:49 +0000172 u8 notify:1;
173 u8 reserved:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
176struct acpi_video_device_cap {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500177 u8 _ADR:1; /* Return the unique ID */
178 u8 _BCL:1; /* Query list of brightness control levels supported */
179 u8 _BCM:1; /* Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800180 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800181 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500182 u8 _DDC:1; /* Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Zhang Ruid32f6942009-03-18 16:27:12 +0800185struct acpi_video_brightness_flags {
186 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Felipe Contreras21fcb342013-08-01 18:43:59 -0500187 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
Zhang Rui1a7c6182009-03-18 16:27:16 +0800188 u8 _BCL_use_index:1; /* levels in _BCL are index values */
189 u8 _BCM_use_index:1; /* input of _BCM is an index value */
190 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400194 int curr;
195 int count;
196 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800197 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 unsigned long device_id;
202 struct acpi_video_device_flags flags;
203 struct acpi_video_device_cap cap;
204 struct list_head entry;
205 struct acpi_video_bus *video;
206 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800208 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400209 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100212static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 "motherboard VGA device",
214 "PCI VGA device",
215 "AGP VGA device",
216 "UNKNOWN",
217};
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
220static void acpi_video_device_rebind(struct acpi_video_bus *video);
221static void acpi_video_device_bind(struct acpi_video_bus *video,
222 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800224static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
225 int level);
226static int acpi_video_device_lcd_get_level_current(
227 struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000228 unsigned long long *level, bool raw);
Len Brown4be44fc2005-08-05 00:44:28 -0400229static int acpi_video_get_next_level(struct acpi_video_device *device,
230 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800231static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400232 int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Felipe Contreras21fcb342013-08-01 18:43:59 -0500234/* backlight device sysfs support */
Yu Luming2f3d0002006-11-11 02:40:34 +0800235static int acpi_video_get_brightness(struct backlight_device *bd)
236{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400237 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000238 int i;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200239 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800240
Danny Baumanna89803d2013-03-19 16:22:50 +0000241 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800242 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000243 for (i = 2; i < vd->brightness->count; i++) {
244 if (vd->brightness->levels[i] == cur_level)
Felipe Contreras21fcb342013-08-01 18:43:59 -0500245 /*
246 * The first two entries are special - see page 575
247 * of the ACPI spec 3.0
248 */
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200249 return i - 2;
Matthew Garrett38531e62007-12-26 02:03:26 +0000250 }
251 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800252}
253
254static int acpi_video_set_brightness(struct backlight_device *bd)
255{
Zhang Rui24450c72009-03-18 16:27:10 +0800256 int request_level = bd->props.brightness + 2;
Felipe Contreras7c364e72013-08-03 22:15:21 +0200257 struct acpi_video_device *vd = bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800258
259 return acpi_video_device_lcd_set_level(vd,
260 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800261}
262
Lionel Debrouxacc24722010-11-16 14:14:02 +0100263static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000264 .get_brightness = acpi_video_get_brightness,
265 .update_status = acpi_video_set_brightness,
266};
267
Zhang Rui702ed512008-01-17 15:51:22 +0800268/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400269static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000270 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800271{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400272 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800273 struct acpi_video_device *video = acpi_driver_data(device);
274
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000275 *state = video->brightness->count - 3;
276 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800277}
278
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400279static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000280 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800281{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400282 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800283 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400284 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000285 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800286
Danny Baumanna89803d2013-03-19 16:22:50 +0000287 if (acpi_video_device_lcd_get_level_current(video, &level, false))
Zhang Ruic8890f92009-03-18 16:27:08 +0800288 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000289 for (offset = 2; offset < video->brightness->count; offset++)
290 if (level == video->brightness->levels[offset]) {
291 *state = video->brightness->count - offset - 1;
292 return 0;
293 }
Zhang Rui702ed512008-01-17 15:51:22 +0800294
295 return -EINVAL;
296}
297
298static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400299video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800300{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400301 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800302 struct acpi_video_device *video = acpi_driver_data(device);
303 int level;
304
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200305 if (state >= video->brightness->count - 2)
Zhang Rui702ed512008-01-17 15:51:22 +0800306 return -EINVAL;
307
308 state = video->brightness->count - state;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200309 level = video->brightness->levels[state - 1];
Zhang Rui702ed512008-01-17 15:51:22 +0800310 return acpi_video_device_lcd_set_level(video, level);
311}
312
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400313static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800314 .get_max_state = video_get_max_state,
315 .get_cur_state = video_get_cur_state,
316 .set_cur_state = video_set_cur_state,
317};
318
Felipe Contreras21fcb342013-08-01 18:43:59 -0500319/*
320 * --------------------------------------------------------------------------
321 * Video Management
322 * --------------------------------------------------------------------------
323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int
Len Brown4be44fc2005-08-05 00:44:28 -0400326acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
327 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Len Brown4be44fc2005-08-05 00:44:28 -0400329 int status;
330 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
331 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 *levels = NULL;
335
Patrick Mochel90130262006-05-19 16:54:48 -0400336 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400338 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400339 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500340 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400341 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 status = -EFAULT;
343 goto err;
344 }
345
346 *levels = obj;
347
Patrick Mocheld550d982006-06-27 00:41:40 -0400348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200350err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800351 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Patrick Mocheld550d982006-06-27 00:41:40 -0400353 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static int
Len Brown4be44fc2005-08-05 00:44:28 -0400357acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Zhang Rui24450c72009-03-18 16:27:10 +0800359 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400360 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
361 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800362 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Zhang Rui24450c72009-03-18 16:27:10 +0800366 status = acpi_evaluate_object(device->dev->handle, "_BCM",
367 &args, NULL);
368 if (ACPI_FAILURE(status)) {
369 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
370 return -EIO;
371 }
372
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400373 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800374 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800375 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800376 if (device->backlight)
377 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800378 return 0;
379 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800380
Zhang Rui24450c72009-03-18 16:27:10 +0800381 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
382 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Zhang Rui45cb50e2009-04-24 12:13:18 -0400385/*
386 * For some buggy _BQC methods, we need to add a constant value to
387 * the _BQC return value to get the actual current brightness level
388 */
389
390static int bqc_offset_aml_bug_workaround;
391static int __init video_set_bqc_offset(const struct dmi_system_id *d)
392{
393 bqc_offset_aml_bug_workaround = 9;
394 return 0;
395}
396
Zhang Rui129ff8f2012-12-04 23:30:19 +0100397static int video_ignore_initial_backlight(const struct dmi_system_id *d)
398{
399 use_bios_initial_backlight = 0;
400 return 0;
401}
402
Zhang Rui45cb50e2009-04-24 12:13:18 -0400403static struct dmi_system_id video_dmi_table[] __initdata = {
404 /*
405 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
406 */
407 {
408 .callback = video_set_bqc_offset,
409 .ident = "Acer Aspire 5720",
410 .matches = {
411 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
412 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
413 },
414 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400415 {
416 .callback = video_set_bqc_offset,
417 .ident = "Acer Aspire 5710Z",
418 .matches = {
419 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
420 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
421 },
422 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400423 {
424 .callback = video_set_bqc_offset,
425 .ident = "eMachines E510",
426 .matches = {
427 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
428 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
429 },
430 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400431 {
432 .callback = video_set_bqc_offset,
433 .ident = "Acer Aspire 5315",
434 .matches = {
435 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
436 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
437 },
438 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800439 {
440 .callback = video_set_bqc_offset,
441 .ident = "Acer Aspire 7720",
442 .matches = {
443 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
444 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
445 },
446 },
Zhang Rui129ff8f2012-12-04 23:30:19 +0100447 {
448 .callback = video_ignore_initial_backlight,
449 .ident = "HP Folio 13-2000",
450 .matches = {
451 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
452 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13 - 2000 Notebook PC"),
453 },
454 },
Gustavo Maciel Dias Vieira771d09b2013-03-04 15:23:37 +0000455 {
456 .callback = video_ignore_initial_backlight,
Lan Tianyu9657a562013-07-16 10:07:21 +0800457 .ident = "Fujitsu E753",
458 .matches = {
459 DMI_MATCH(DMI_BOARD_VENDOR, "FUJITSU"),
460 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK E753"),
461 },
462 },
463 {
464 .callback = video_ignore_initial_backlight,
Gustavo Maciel Dias Vieira771d09b2013-03-04 15:23:37 +0000465 .ident = "HP Pavilion dm4",
466 .matches = {
467 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
468 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion dm4 Notebook PC"),
469 },
470 },
Alex Hung4ef366c2013-05-06 08:23:43 +0000471 {
472 .callback = video_ignore_initial_backlight,
Ash Willis780a6ec2013-05-29 01:27:59 +0000473 .ident = "HP Pavilion g6 Notebook PC",
474 .matches = {
475 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
476 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion g6 Notebook PC"),
477 },
478 },
479 {
480 .callback = video_ignore_initial_backlight,
Alex Hung4ef366c2013-05-06 08:23:43 +0000481 .ident = "HP 1000 Notebook PC",
482 .matches = {
483 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
484 DMI_MATCH(DMI_PRODUCT_NAME, "HP 1000 Notebook PC"),
485 },
486 },
Alex Hungfedbe9b2013-05-28 02:05:09 +0000487 {
488 .callback = video_ignore_initial_backlight,
489 .ident = "HP Pavilion m4",
490 .matches = {
491 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
492 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion m4 Notebook PC"),
493 },
494 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400495 {}
496};
497
Danny Baumann994fa632013-03-19 16:22:52 +0000498static unsigned long long
499acpi_video_bqc_value_to_level(struct acpi_video_device *device,
500 unsigned long long bqc_value)
501{
502 unsigned long long level;
503
504 if (device->brightness->flags._BQC_use_index) {
505 /*
506 * _BQC returns an index that doesn't account for
507 * the first 2 items with special meaning, so we need
508 * to compensate for that by offsetting ourselves
509 */
510 if (device->brightness->flags._BCL_reversed)
511 bqc_value = device->brightness->count - 3 - bqc_value;
512
513 level = device->brightness->levels[bqc_value + 2];
514 } else {
515 level = bqc_value;
516 }
517
518 level += bqc_offset_aml_bug_workaround;
519
520 return level;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static int
Len Brown4be44fc2005-08-05 00:44:28 -0400524acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Danny Baumanna89803d2013-03-19 16:22:50 +0000525 unsigned long long *level, bool raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Zhang Ruic8890f92009-03-18 16:27:08 +0800527 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800528 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800529
Zhang Ruic60d6382009-03-18 16:27:18 +0800530 if (device->cap._BQC || device->cap._BCQ) {
531 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
532
533 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800534 NULL, level);
535 if (ACPI_SUCCESS(status)) {
Danny Baumanna89803d2013-03-19 16:22:50 +0000536 if (raw) {
537 /*
538 * Caller has indicated he wants the raw
539 * value returned by _BQC, so don't furtherly
540 * mess with the value.
541 */
542 return 0;
543 }
544
Danny Baumann994fa632013-03-19 16:22:52 +0000545 *level = acpi_video_bqc_value_to_level(device, *level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800546
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800547 for (i = 2; i < device->brightness->count; i++)
548 if (device->brightness->levels[i] == *level) {
549 device->brightness->curr = *level;
550 return 0;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200551 }
Danny Baumanna89803d2013-03-19 16:22:50 +0000552 /*
553 * BQC returned an invalid level.
554 * Stop using it.
555 */
556 ACPI_WARNING((AE_INFO,
557 "%s returned an invalid level",
558 buf));
559 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800560 } else {
Felipe Contreras21fcb342013-08-01 18:43:59 -0500561 /*
562 * Fixme:
Zhang Ruic8890f92009-03-18 16:27:08 +0800563 * should we return an error or ignore this failure?
564 * dev->brightness->curr is a cached value which stores
565 * the correct current backlight level in most cases.
566 * ACPI video backlight still works w/ buggy _BQC.
567 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
568 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800569 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
570 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800571 }
572 }
573
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400574 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578static int
Len Brown4be44fc2005-08-05 00:44:28 -0400579acpi_video_device_EDID(struct acpi_video_device *device,
580 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Len Brown4be44fc2005-08-05 00:44:28 -0400582 int status;
583 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
584 union acpi_object *obj;
585 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
586 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 *edid = NULL;
590
591 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (length == 128)
594 arg0.integer.value = 1;
595 else if (length == 256)
596 arg0.integer.value = 2;
597 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400598 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Patrick Mochel90130262006-05-19 16:54:48 -0400600 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200604 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (obj && obj->type == ACPI_TYPE_BUFFER)
607 *edid = obj;
608 else {
Len Brown64684632006-06-26 23:41:38 -0400609 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 status = -EFAULT;
611 kfree(obj);
612 }
613
Patrick Mocheld550d982006-06-27 00:41:40 -0400614 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/* bus */
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500621 * video : video bus device pointer
622 * bios_flag :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * 0. The system BIOS should NOT automatically switch(toggle)
624 * the active display output.
625 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100626 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * 2. The _DGS value should be locked.
628 * 3. The system BIOS should not automatically switch (toggle) the
629 * active display output, but instead generate the display switch
630 * event notify code.
631 * lcd_flag :
632 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100633 * of the LCD when the power changes from AC to DC
Felipe Contreras21fcb342013-08-01 18:43:59 -0500634 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100635 * level of the LCD when the power changes from AC to DC.
Felipe Contreras21fcb342013-08-01 18:43:59 -0500636 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400637 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
639
640static int
Len Brown4be44fc2005-08-05 00:44:28 -0400641acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Igor Murzovea9f8852012-03-30 21:32:08 +0400643 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400644 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
645 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Zhang Ruib0373842012-06-20 09:48:43 +0800647 if (!video->cap._DOS)
648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Igor Murzovea9f8852012-03-30 21:32:08 +0400650 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
651 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 arg0.integer.value = (lcd_flag << 2) | bios_flag;
653 video->dos_setting = arg0.integer.value;
Igor Murzovea9f8852012-03-30 21:32:08 +0400654 status = acpi_evaluate_object(video->device->handle, "_DOS",
655 &args, NULL);
656 if (ACPI_FAILURE(status))
657 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Igor Murzovea9f8852012-03-30 21:32:08 +0400659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500663 * Simple comparison function used to sort backlight levels.
664 */
665
666static int
667acpi_video_cmp_level(const void *a, const void *b)
668{
669 return *(int *)a - *(int *)b;
670}
671
672/*
Aaron Lua50188d2013-04-22 14:08:32 +0200673 * Decides if _BQC/_BCQ for this system is usable
674 *
675 * We do this by changing the level first and then read out the current
676 * brightness level, if the value does not match, find out if it is using
677 * index. If not, clear the _BQC/_BCQ capability.
678 */
679static int acpi_video_bqc_quirk(struct acpi_video_device *device,
680 int max_level, int current_level)
681{
682 struct acpi_video_device_brightness *br = device->brightness;
683 int result;
684 unsigned long long level;
685 int test_level;
686
687 /* don't mess with existing known broken systems */
688 if (bqc_offset_aml_bug_workaround)
689 return 0;
690
691 /*
692 * Some systems always report current brightness level as maximum
693 * through _BQC, we need to test another value for them.
694 */
695 test_level = current_level == max_level ? br->levels[2] : max_level;
696
697 result = acpi_video_device_lcd_set_level(device, test_level);
698 if (result)
699 return result;
700
701 result = acpi_video_device_lcd_get_level_current(device, &level, true);
702 if (result)
703 return result;
704
705 if (level != test_level) {
706 /* buggy _BQC found, need to find out if it uses index */
707 if (level < br->count) {
708 if (br->flags._BCL_reversed)
709 level = br->count - 3 - level;
710 if (br->levels[level + 2] == test_level)
711 br->flags._BQC_use_index = 1;
712 }
713
714 if (!br->flags._BQC_use_index)
715 device->cap._BQC = device->cap._BCQ = 0;
716 }
717
718 return 0;
719}
720
721
722/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500723 * Arg:
724 * device : video output device (LCD, CRT, ..)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *
726 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100727 * Maximum brightness level
728 *
729 * Allocate and initialize device->brightness.
730 */
731
732static int
733acpi_video_init_brightness(struct acpi_video_device *device)
734{
735 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800736 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800737 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100738 union acpi_object *o;
739 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800740 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100741
742 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
743 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
744 "LCD brightness level\n"));
745 goto out;
746 }
747
748 if (obj->package.count < 2)
749 goto out;
750
751 br = kzalloc(sizeof(*br), GFP_KERNEL);
752 if (!br) {
753 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800754 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100755 goto out;
756 }
757
Zhang Ruid32f6942009-03-18 16:27:12 +0800758 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100759 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800760 if (!br->levels) {
761 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100762 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800763 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100764
765 for (i = 0; i < obj->package.count; i++) {
766 o = (union acpi_object *)&obj->package.elements[i];
767 if (o->type != ACPI_TYPE_INTEGER) {
768 printk(KERN_ERR PREFIX "Invalid data\n");
769 continue;
770 }
771 br->levels[count] = (u32) o->integer.value;
772
773 if (br->levels[count] > max_level)
774 max_level = br->levels[count];
775 count++;
776 }
777
Zhang Ruid32f6942009-03-18 16:27:12 +0800778 /*
779 * some buggy BIOS don't export the levels
780 * when machine is on AC/Battery in _BCL package.
781 * In this case, the first two elements in _BCL packages
782 * are also supported brightness levels that OS should take care of.
783 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800784 for (i = 2; i < count; i++) {
785 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800786 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800787 if (br->levels[i] == br->levels[1])
788 level_ac_battery++;
789 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500790
Zhang Ruid32f6942009-03-18 16:27:12 +0800791 if (level_ac_battery < 2) {
792 level_ac_battery = 2 - level_ac_battery;
793 br->flags._BCL_no_ac_battery_levels = 1;
794 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
795 br->levels[i] = br->levels[i - level_ac_battery];
796 count += level_ac_battery;
797 } else if (level_ac_battery > 2)
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000798 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
Zhang Ruid32f6942009-03-18 16:27:12 +0800799
Zhang Ruid80fb992009-03-18 16:27:14 +0800800 /* Check if the _BCL package is in a reversed order */
801 if (max_level == br->levels[2]) {
802 br->flags._BCL_reversed = 1;
803 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
804 acpi_video_cmp_level, NULL);
805 } else if (max_level != br->levels[count - 1])
806 ACPI_ERROR((AE_INFO,
Colin Ian Kingbf6787e2012-11-29 11:53:13 +0000807 "Found unordered _BCL package"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100808
809 br->count = count;
810 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800811
812 /* Check the input/output of _BQC/_BCL/_BCM */
813 if ((max_level < 100) && (max_level <= (count - 2)))
814 br->flags._BCL_use_index = 1;
815
816 /*
817 * _BCM is always consistent with _BCL,
818 * at least for all the laptops we have ever seen.
819 */
820 br->flags._BCM_use_index = br->flags._BCL_use_index;
821
822 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400823 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800824
825 if (!device->cap._BQC)
826 goto set_level;
827
Danny Baumanna89803d2013-03-19 16:22:50 +0000828 result = acpi_video_device_lcd_get_level_current(device,
829 &level_old, true);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800830 if (result)
831 goto out_free_levels;
832
Aaron Lua50188d2013-04-22 14:08:32 +0200833 result = acpi_video_bqc_quirk(device, max_level, level_old);
834 if (result)
835 goto out_free_levels;
Zhang Ruie047cca2009-04-09 14:24:35 +0800836 /*
Aaron Lua50188d2013-04-22 14:08:32 +0200837 * cap._BQC may get cleared due to _BQC is found to be broken
838 * in acpi_video_bqc_quirk, so check again here.
Zhang Ruie047cca2009-04-09 14:24:35 +0800839 */
Aaron Lua50188d2013-04-22 14:08:32 +0200840 if (!device->cap._BQC)
841 goto set_level;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800842
Danny Baumann994fa632013-03-19 16:22:52 +0000843 if (use_bios_initial_backlight) {
844 level = acpi_video_bqc_value_to_level(device, level_old);
Zhang Rui90c53ca2009-08-31 12:39:54 -0400845 /*
Danny Baumann994fa632013-03-19 16:22:52 +0000846 * On some buggy laptops, _BQC returns an uninitialized
847 * value when invoked for the first time, i.e.
848 * level_old is invalid (no matter whether it's a level
849 * or an index). Set the backlight to max_level in this case.
Zhang Rui90c53ca2009-08-31 12:39:54 -0400850 */
Danny Baumann994fa632013-03-19 16:22:52 +0000851 for (i = 2; i < br->count; i++)
852 if (level_old == br->levels[i])
853 break;
854 if (i == br->count)
855 level = max_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400856 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800857
Zhang Ruie047cca2009-04-09 14:24:35 +0800858set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400859 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800860 if (result)
861 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800862
Zhang Ruid32f6942009-03-18 16:27:12 +0800863 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
864 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100865 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800866 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100867
868out_free_levels:
869 kfree(br->levels);
870out_free:
871 kfree(br);
872out:
873 device->brightness = NULL;
874 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800875 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100876}
877
878/*
879 * Arg:
880 * device : video output device (LCD, CRT, ..)
881 *
882 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500883 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100885 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * device.
887 */
888
Len Brown4be44fc2005-08-05 00:44:28 -0400889static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200893 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 device->cap._ADR = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200895 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1)))
Len Brown4be44fc2005-08-05 00:44:28 -0400896 device->cap._BCL = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200897 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1)))
Len Brown4be44fc2005-08-05 00:44:28 -0400898 device->cap._BCM = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200899 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BQC", &h_dummy1)))
Yu Luming2f3d0002006-11-11 02:40:34 +0800900 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800901 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
902 &h_dummy1))) {
903 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
904 device->cap._BCQ = 1;
905 }
906
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200907 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1)))
Len Brown4be44fc2005-08-05 00:44:28 -0400908 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Matthew Garrettc04c6972013-07-16 17:08:16 +0000910 if (acpi_video_init_brightness(device))
911 return;
912
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +0200913 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500914 struct backlight_properties props;
Matthew Garrett9661e922011-03-22 16:30:25 -0700915 struct pci_dev *pdev;
916 acpi_handle acpi_parent;
917 struct device *parent = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +0800918 int result;
Felipe Contreras915ea7e2013-08-03 22:12:50 +0200919 static int count;
Yu Luming2f3d0002006-11-11 02:40:34 +0800920 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800921
Julia Lawallaeb834d2010-04-27 14:06:04 -0700922 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +0800923 if (!name)
924 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700925 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +0800926
Matthew Garrett9661e922011-03-22 16:30:25 -0700927 acpi_get_parent(device->dev->handle, &acpi_parent);
928
929 pdev = acpi_get_pci_dev(acpi_parent);
930 if (pdev) {
931 parent = &pdev->dev;
932 pci_dev_put(pdev);
933 }
934
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500935 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700936 props.type = BACKLIGHT_FIRMWARE;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500937 props.max_brightness = device->brightness->count - 3;
Matthew Garrett9661e922011-03-22 16:30:25 -0700938 device->backlight = backlight_device_register(name,
939 parent,
940 device,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500941 &acpi_backlight_ops,
942 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +0800943 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +0800944 if (IS_ERR(device->backlight))
945 return;
Zhang Rui702ed512008-01-17 15:51:22 +0800946
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200947 /*
948 * Save current brightness level in case we have to restore it
949 * before acpi_video_device_lcd_set_level() is called next time.
950 */
951 device->backlight->props.brightness =
952 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +0800953
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400954 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +0800955 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400956 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700957 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400958 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700959 * free it.
960 * Also, why the hell we are returning early and
961 * not attempt to register video output if cooling
962 * device registration failed?
963 * -- dtor
964 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400965 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500966 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700967 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500968
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200969 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400970 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800971 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400972 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800973 "thermal_cooling");
974 if (result)
975 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400976 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800977 &device->dev->dev.kobj, "device");
978 if (result)
979 printk(KERN_ERR PREFIX "Create sysfs link\n");
980
Matthew Garrettc04c6972013-07-16 17:08:16 +0000981 } else {
982 /* Remove the brightness object. */
983 kfree(device->brightness->levels);
984 kfree(device->brightness);
985 device->brightness = NULL;
Yu Luming2f3d0002006-11-11 02:40:34 +0800986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/*
Felipe Contreras21fcb342013-08-01 18:43:59 -0500990 * Arg:
991 * device : video output device (VGA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *
993 * Return Value:
Felipe Contreras21fcb342013-08-01 18:43:59 -0500994 * None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100996 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
998
Len Brown4be44fc2005-08-05 00:44:28 -0400999static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Len Brown4be44fc2005-08-05 00:44:28 -04001001 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001003 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 video->cap._DOS = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001005 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 video->cap._DOD = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001007 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 video->cap._ROM = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001009 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 video->cap._GPD = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001011 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 video->cap._SPD = 1;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001013 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 video->cap._VPO = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * Check whether the video bus device has required AML method to
1019 * support the desired features
1020 */
1021
Len Brown4be44fc2005-08-05 00:44:28 -04001022static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Len Brown4be44fc2005-08-05 00:44:28 -04001024 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001025 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001028 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001030 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001031 if (!dev)
1032 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001033 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001034
Felipe Contreras21fcb342013-08-01 18:43:59 -05001035 /*
1036 * Since there is no HID, CID and so on for VGA driver, we have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * to check well known required nodes.
1038 */
1039
Julius Volz98fb8fe2007-02-20 16:38:40 +01001040 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001041 if (video->cap._DOS || video->cap._DOD) {
1042 if (!video->cap._DOS) {
1043 printk(KERN_WARNING FW_BUG
1044 "ACPI(%s) defines _DOD but not _DOS\n",
1045 acpi_device_bid(video->device));
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 video->flags.multihead = 1;
1048 status = 0;
1049 }
1050
Julius Volz98fb8fe2007-02-20 16:38:40 +01001051 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001052 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 video->flags.rom = 1;
1054 status = 0;
1055 }
1056
Julius Volz98fb8fe2007-02-20 16:38:40 +01001057 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001058 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 video->flags.post = 1;
1060 status = 0;
1061 }
1062
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Felipe Contreras21fcb342013-08-01 18:43:59 -05001066/*
1067 * --------------------------------------------------------------------------
1068 * Driver Interface
1069 * --------------------------------------------------------------------------
1070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072/* device interface */
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001073static struct acpi_video_device_attrib *
Rui Zhang82cae992007-01-03 23:40:53 -05001074acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1075{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001076 struct acpi_video_enumerated_device *ids;
1077 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001078
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001079 for (i = 0; i < video->attached_count; i++) {
1080 ids = &video->attached_array[i];
1081 if ((ids->value.int_val & 0xffff) == device_id)
1082 return &ids->value.attrib;
1083 }
1084
Rui Zhang82cae992007-01-03 23:40:53 -05001085 return NULL;
1086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001089acpi_video_get_device_type(struct acpi_video_bus *video,
1090 unsigned long device_id)
1091{
1092 struct acpi_video_enumerated_device *ids;
1093 int i;
1094
1095 for (i = 0; i < video->attached_count; i++) {
1096 ids = &video->attached_array[i];
1097 if ((ids->value.int_val & 0xffff) == device_id)
1098 return ids->value.int_val;
1099 }
1100
1101 return 0;
1102}
1103
1104static int
Len Brown4be44fc2005-08-05 00:44:28 -04001105acpi_video_bus_get_one_device(struct acpi_device *device,
1106 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001108 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001109 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001110 struct acpi_video_device *data;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001111 struct acpi_video_device_attrib *attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Len Brown4be44fc2005-08-05 00:44:28 -04001113 status =
1114 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Aaron Lu91e13aa2013-04-25 02:47:49 +00001115 /* Some device omits _ADR, we skip them instead of fail */
1116 if (ACPI_FAILURE(status))
1117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Aaron Lu91e13aa2013-04-25 02:47:49 +00001119 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1120 if (!data)
1121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Aaron Lu91e13aa2013-04-25 02:47:49 +00001123 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1124 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1125 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Aaron Lu91e13aa2013-04-25 02:47:49 +00001127 data->device_id = device_id;
1128 data->video = video;
1129 data->dev = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Aaron Lu91e13aa2013-04-25 02:47:49 +00001131 attribute = acpi_video_get_device_attr(video, device_id);
Rui Zhang82cae992007-01-03 23:40:53 -05001132
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001133 if (attribute && attribute->device_id_scheme) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001134 switch (attribute->display_type) {
1135 case ACPI_VIDEO_DISPLAY_CRT:
1136 data->flags.crt = 1;
1137 break;
1138 case ACPI_VIDEO_DISPLAY_TV:
1139 data->flags.tvout = 1;
1140 break;
1141 case ACPI_VIDEO_DISPLAY_DVI:
1142 data->flags.dvi = 1;
1143 break;
1144 case ACPI_VIDEO_DISPLAY_LCD:
1145 data->flags.lcd = 1;
1146 break;
1147 default:
1148 data->flags.unknown = 1;
1149 break;
1150 }
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001151 if (attribute->bios_can_detect)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001152 data->flags.bios = 1;
1153 } else {
1154 /* Check for legacy IDs */
1155 device_type = acpi_video_get_device_type(video, device_id);
1156 /* Ignore bits 16 and 18-20 */
1157 switch (device_type & 0xffe2ffff) {
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001158 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1159 data->flags.crt = 1;
1160 break;
1161 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1162 data->flags.lcd = 1;
1163 break;
1164 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1165 data->flags.tvout = 1;
1166 break;
1167 default:
1168 data->flags.unknown = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
Aaron Lu91e13aa2013-04-25 02:47:49 +00001172 acpi_video_device_bind(video, data);
1173 acpi_video_device_find_cap(data);
1174
1175 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1176 acpi_video_device_notify, data);
1177 if (ACPI_FAILURE(status))
1178 dev_err(&device->dev, "Error installing notify handler\n");
1179 else
1180 data->flags.notify = 1;
1181
1182 mutex_lock(&video->device_list_lock);
1183 list_add_tail(&data->entry, &video->video_device_list);
1184 mutex_unlock(&video->device_list_lock);
1185
1186 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189/*
1190 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001191 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 *
1193 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001194 * none
1195 *
1196 * Enumerate the video device list of the video bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 * bind the ids with the corresponding video devices
1198 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Len Brown4be44fc2005-08-05 00:44:28 -04001201static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001203 struct acpi_video_device *dev;
1204
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001205 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001206
1207 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001208 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001209
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001210 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/*
1214 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001215 * video : video bus device
1216 * device : video output device under the video
1217 * bus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 *
1219 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001220 * none
1221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * Bind the ids with the corresponding video devices
1223 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226static void
Len Brown4be44fc2005-08-05 00:44:28 -04001227acpi_video_device_bind(struct acpi_video_bus *video,
1228 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001230 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001231 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001233 for (i = 0; i < video->attached_count; i++) {
1234 ids = &video->attached_array[i];
1235 if (device->device_id == (ids->value.int_val & 0xffff)) {
1236 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1238 }
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * Arg:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001244 * video : video bus device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 *
1246 * Return:
Felipe Contreras21fcb342013-08-01 18:43:59 -05001247 * < 0 : error
1248 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * Call _DOD to enumerate all devices attached to display adapter
1250 *
Len Brown4be44fc2005-08-05 00:44:28 -04001251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1254{
Len Brown4be44fc2005-08-05 00:44:28 -04001255 int status;
1256 int count;
1257 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001258 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001259 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1260 union acpi_object *dod = NULL;
1261 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Patrick Mochel90130262006-05-19 16:54:48 -04001263 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001265 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001269 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001271 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 status = -EFAULT;
1273 goto out;
1274 }
1275
1276 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001277 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001279 active_list = kcalloc(1 + dod->package.count,
1280 sizeof(struct acpi_video_enumerated_device),
1281 GFP_KERNEL);
1282 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 status = -ENOMEM;
1284 goto out;
1285 }
1286
1287 count = 0;
1288 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001289 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001292 printk(KERN_ERR PREFIX
1293 "Invalid _DOD data in element %d\n", i);
1294 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001296
1297 active_list[count].value.int_val = obj->integer.value;
1298 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001299 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1300 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 count++;
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jesper Juhl6044ec82005-11-07 01:01:32 -08001304 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001305
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001306 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001308
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001309out:
Len Brown02438d82006-06-30 03:19:10 -04001310 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001311 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Len Brown4be44fc2005-08-05 00:44:28 -04001314static int
1315acpi_video_get_next_level(struct acpi_video_device *device,
1316 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001318 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001319 max = max_below = 0;
1320 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001321 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001322 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001323 l = device->brightness->levels[i];
1324 if (abs(l - level_current) < abs(delta)) {
1325 delta = l - level_current;
1326 if (!delta)
1327 break;
1328 }
1329 }
1330 /* Ajust level_current to closest available level */
1331 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001332 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001333 l = device->brightness->levels[i];
1334 if (l < min)
1335 min = l;
1336 if (l > max)
1337 max = l;
1338 if (l < min_above && l > level_current)
1339 min_above = l;
1340 if (l > max_below && l < level_current)
1341 max_below = l;
1342 }
1343
1344 switch (event) {
1345 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1346 return (level_current < max) ? min_above : min;
1347 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1348 return (level_current < max) ? min_above : max;
1349 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1350 return (level_current > min) ? max_below : min;
1351 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1352 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1353 return 0;
1354 default:
1355 return level_current;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Zhang Ruic8890f92009-03-18 16:27:08 +08001359static int
Len Brown4be44fc2005-08-05 00:44:28 -04001360acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001362 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001363 int result = -EINVAL;
1364
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001365 /* no warning message if acpi_backlight=vendor is used */
1366 if (!acpi_video_backlight_support())
Zhang Rui28c32e92009-07-13 10:33:24 +08001367 return 0;
1368
Julia Jomantaite469778c2008-06-23 22:50:42 +01001369 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001370 goto out;
1371
1372 result = acpi_video_device_lcd_get_level_current(device,
Danny Baumanna89803d2013-03-19 16:22:50 +00001373 &level_current,
1374 false);
Zhang Ruic8890f92009-03-18 16:27:08 +08001375 if (result)
1376 goto out;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001379
Zhang Rui24450c72009-03-18 16:27:10 +08001380 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001381
Matthew Garrett36342742009-07-14 17:06:03 +01001382 if (!result)
1383 backlight_force_update(device->backlight,
1384 BACKLIGHT_UPDATE_HOTKEY);
1385
Zhang Ruic8890f92009-03-18 16:27:08 +08001386out:
1387 if (result)
1388 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1389
1390 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Matthew Garrette92a7162010-01-12 14:17:03 -05001393int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1394 void **edid)
1395{
1396 struct acpi_video_bus *video;
1397 struct acpi_video_device *video_device;
1398 union acpi_object *buffer = NULL;
1399 acpi_status status;
1400 int i, length;
1401
1402 if (!device || !acpi_driver_data(device))
1403 return -EINVAL;
1404
1405 video = acpi_driver_data(device);
1406
1407 for (i = 0; i < video->attached_count; i++) {
1408 video_device = video->attached_array[i].bind_info;
1409 length = 256;
1410
1411 if (!video_device)
1412 continue;
1413
Zhang Rui82069552010-12-06 15:04:24 +08001414 if (!video_device->cap._DDC)
1415 continue;
1416
Matthew Garrette92a7162010-01-12 14:17:03 -05001417 if (type) {
1418 switch (type) {
1419 case ACPI_VIDEO_DISPLAY_CRT:
1420 if (!video_device->flags.crt)
1421 continue;
1422 break;
1423 case ACPI_VIDEO_DISPLAY_TV:
1424 if (!video_device->flags.tvout)
1425 continue;
1426 break;
1427 case ACPI_VIDEO_DISPLAY_DVI:
1428 if (!video_device->flags.dvi)
1429 continue;
1430 break;
1431 case ACPI_VIDEO_DISPLAY_LCD:
1432 if (!video_device->flags.lcd)
1433 continue;
1434 break;
1435 }
1436 } else if (video_device->device_id != device_id) {
1437 continue;
1438 }
1439
1440 status = acpi_video_device_EDID(video_device, &buffer, length);
1441
1442 if (ACPI_FAILURE(status) || !buffer ||
1443 buffer->type != ACPI_TYPE_BUFFER) {
1444 length = 128;
1445 status = acpi_video_device_EDID(video_device, &buffer,
1446 length);
1447 if (ACPI_FAILURE(status) || !buffer ||
1448 buffer->type != ACPI_TYPE_BUFFER) {
1449 continue;
1450 }
1451 }
1452
1453 *edid = buffer->buffer.pointer;
1454 return length;
1455 }
1456
1457 return -ENODEV;
1458}
1459EXPORT_SYMBOL(acpi_video_get_edid);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461static int
Len Brown4be44fc2005-08-05 00:44:28 -04001462acpi_video_bus_get_devices(struct acpi_video_bus *video,
1463 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Igor Murzovfba4e082012-10-13 04:41:25 +04001465 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001466 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Igor Murzovfba4e082012-10-13 04:41:25 +04001468 /*
1469 * There are systems where video module known to work fine regardless
1470 * of broken _DOD and ignoring returned value here doesn't cause
1471 * any issues later.
1472 */
1473 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001475 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001478 if (status) {
Aaron Lu91e13aa2013-04-25 02:47:49 +00001479 dev_err(&dev->dev, "Can't attach device\n");
1480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001483 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Len Brown4be44fc2005-08-05 00:44:28 -04001486static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Karol Kozimor031ec772005-07-30 04:18:00 -04001488 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001491 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Aaron Lu91e13aa2013-04-25 02:47:49 +00001493 if (device->flags.notify) {
1494 status = acpi_remove_notify_handler(device->dev->handle,
1495 ACPI_DEVICE_NOTIFY, acpi_video_device_notify);
1496 if (ACPI_FAILURE(status))
1497 dev_err(&device->dev->dev,
1498 "Can't remove video notify handler\n");
Andi Kleencfa806f2010-07-20 15:18:36 -07001499 }
Aaron Lu91e13aa2013-04-25 02:47:49 +00001500
Keith Packarde29b3ee2009-08-06 15:57:54 -07001501 if (device->backlight) {
Keith Packarde29b3ee2009-08-06 15:57:54 -07001502 backlight_device_unregister(device->backlight);
1503 device->backlight = NULL;
1504 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001505 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08001506 sysfs_remove_link(&device->dev->dev.kobj,
1507 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001508 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08001509 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001510 thermal_cooling_device_unregister(device->cooling_dev);
1511 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08001512 }
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001513
Patrick Mocheld550d982006-06-27 00:41:40 -04001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Len Brown4be44fc2005-08-05 00:44:28 -04001517static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Len Brown4be44fc2005-08-05 00:44:28 -04001519 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001520 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001522 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001524 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001526 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001527 if (ACPI_FAILURE(status))
1528 printk(KERN_WARNING PREFIX
1529 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001531 if (dev->brightness) {
1532 kfree(dev->brightness->levels);
1533 kfree(dev->brightness);
1534 }
1535 list_del(&dev->entry);
1536 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001539 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001540
Patrick Mocheld550d982006-06-27 00:41:40 -04001541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544/* acpi_video interface */
1545
Aaron Luefaa14c2013-07-16 13:08:05 +08001546/*
1547 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1548 * preform any automatic brightness change on receiving a notification.
1549 */
Len Brown4be44fc2005-08-05 00:44:28 -04001550static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Aaron Luefaa14c2013-07-16 13:08:05 +08001552 return acpi_video_bus_DOS(video, 0,
1553 acpi_video_backlight_quirks() ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Len Brown4be44fc2005-08-05 00:44:28 -04001556static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Aaron Luefaa14c2013-07-16 13:08:05 +08001558 return acpi_video_bus_DOS(video, 0,
1559 acpi_video_backlight_quirks() ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Bjorn Helgaas70155582009-04-07 15:37:11 +00001562static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001564 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001565 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001566 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001569 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Luming Yue9dab192007-08-20 18:23:53 +08001571 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001574 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001576 acpi_bus_generate_proc_event(device, event, 0);
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001577 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
1579
Julius Volz98fb8fe2007-02-20 16:38:40 +01001580 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * connector. */
1582 acpi_video_device_enumerate(video);
1583 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001584 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001585 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587
Len Brown4be44fc2005-08-05 00:44:28 -04001588 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001589 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001590 keycode = KEY_SWITCHVIDEOMODE;
1591 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001592 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001593 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001594 keycode = KEY_VIDEO_NEXT;
1595 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001596 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001597 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001598 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 break;
1600
1601 default:
1602 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001603 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
1605 }
1606
Luca Tettamanti8a37c652012-08-02 15:30:27 +02001607 if (acpi_notifier_call_chain(device, event, 0))
1608 /* Something vetoed the keypress. */
1609 keycode = 0;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001610
1611 if (keycode) {
1612 input_report_key(input, keycode, 1);
1613 input_sync(input);
1614 input_report_key(input, keycode, 0);
1615 input_sync(input);
1616 }
Luming Yue9dab192007-08-20 18:23:53 +08001617
Patrick Mocheld550d982006-06-27 00:41:40 -04001618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
Len Brown4be44fc2005-08-05 00:44:28 -04001621static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001623 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001624 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001625 struct acpi_video_bus *bus;
1626 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001627 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001632 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001633 bus = video_device->video;
1634 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001637 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001638 if (brightness_switch_enabled)
1639 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001640 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001641 keycode = KEY_BRIGHTNESS_CYCLE;
1642 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001643 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001644 if (brightness_switch_enabled)
1645 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001646 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001647 keycode = KEY_BRIGHTNESSUP;
1648 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001649 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001650 if (brightness_switch_enabled)
1651 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001652 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001653 keycode = KEY_BRIGHTNESSDOWN;
1654 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001655 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001656 if (brightness_switch_enabled)
1657 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001658 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001659 keycode = KEY_BRIGHTNESS_ZERO;
1660 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001661 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001662 if (brightness_switch_enabled)
1663 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001664 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001665 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 default:
1668 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001669 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
1671 }
Luming Yue9dab192007-08-20 18:23:53 +08001672
Zhang Rui7761f632008-01-25 14:48:12 +08001673 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001674
1675 if (keycode) {
1676 input_report_key(input, keycode, 1);
1677 input_sync(input);
1678 input_report_key(input, keycode, 0);
1679 input_sync(input);
1680 }
Luming Yue9dab192007-08-20 18:23:53 +08001681
Patrick Mocheld550d982006-06-27 00:41:40 -04001682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001685static int acpi_video_resume(struct notifier_block *nb,
1686 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001687{
1688 struct acpi_video_bus *video;
1689 struct acpi_video_device *video_device;
1690 int i;
1691
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001692 switch (val) {
1693 case PM_HIBERNATION_PREPARE:
1694 case PM_SUSPEND_PREPARE:
1695 case PM_RESTORE_PREPARE:
1696 return NOTIFY_DONE;
1697 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001698
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001699 video = container_of(nb, struct acpi_video_bus, pm_nb);
1700
1701 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001702
1703 for (i = 0; i < video->attached_count; i++) {
1704 video_device = video->attached_array[i].bind_info;
1705 if (video_device && video_device->backlight)
1706 acpi_video_set_brightness(video_device->backlight);
1707 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001708
1709 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001710}
1711
Zhang Ruic504f8c2009-12-30 15:59:23 +08001712static acpi_status
1713acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1714 void **return_value)
1715{
1716 struct acpi_device *device = context;
1717 struct acpi_device *sibling;
1718 int result;
1719
1720 if (handle == device->handle)
1721 return AE_CTRL_TERMINATE;
1722
1723 result = acpi_bus_get_device(handle, &sibling);
1724 if (result)
1725 return AE_OK;
1726
1727 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1728 return AE_ALREADY_EXISTS;
1729
1730 return AE_OK;
1731}
1732
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001733static int instance;
1734
Len Brown4be44fc2005-08-05 00:44:28 -04001735static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001737 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001738 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001739 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001740 acpi_status status;
1741
1742 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1743 device->parent->handle, 1,
1744 acpi_video_bus_match, NULL,
1745 device, NULL);
1746 if (status == AE_ALREADY_EXISTS) {
1747 printk(KERN_WARNING FW_BUG
1748 "Duplicate ACPI video bus devices for the"
1749 " same VGA controller, please try module "
1750 "parameter \"video.allow_duplicates=1\""
1751 "if the current driver doesn't work.\n");
1752 if (!allow_duplicates)
1753 return -ENODEV;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Burman Yan36bcbec2006-12-19 12:56:11 -08001756 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001758 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Zhang Ruie6d9da12007-08-25 02:23:31 -04001760 /* a hack to fix the duplicate name "VID" problem on T61 */
1761 if (!strcmp(device->pnp.bus_id, "VID")) {
1762 if (instance)
1763 device->pnp.bus_id[3] = '0' + instance;
Felipe Contreras915ea7e2013-08-03 22:12:50 +02001764 instance++;
Zhang Ruie6d9da12007-08-25 02:23:31 -04001765 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001766 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1767 if (!strcmp(device->pnp.bus_id, "VGA")) {
1768 if (instance)
1769 device->pnp.bus_id[3] = '0' + instance;
1770 instance++;
1771 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001772
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001773 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1775 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001776 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001779 error = acpi_video_bus_check(video);
1780 if (error)
1781 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001783 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 INIT_LIST_HEAD(&video->video_device_list);
1785
Igor Murzovea9f8852012-03-30 21:32:08 +04001786 error = acpi_video_bus_get_devices(video, device);
1787 if (error)
Aaron Lu91e13aa2013-04-25 02:47:49 +00001788 goto err_put_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Luming Yue9dab192007-08-20 18:23:53 +08001790 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001791 if (!input) {
1792 error = -ENOMEM;
Igor Murzovb60e7f62012-03-30 21:32:09 +04001793 goto err_put_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001794 }
Luming Yue9dab192007-08-20 18:23:53 +08001795
Igor Murzovb60e7f62012-03-30 21:32:09 +04001796 error = acpi_video_bus_start_devices(video);
1797 if (error)
1798 goto err_free_input_dev;
1799
Luming Yue9dab192007-08-20 18:23:53 +08001800 snprintf(video->phys, sizeof(video->phys),
1801 "%s/video/input0", acpi_device_hid(video->device));
1802
1803 input->name = acpi_device_name(video->device);
1804 input->phys = video->phys;
1805 input->id.bustype = BUS_HOST;
1806 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05001807 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001808 input->evbit[0] = BIT(EV_KEY);
1809 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1810 set_bit(KEY_VIDEO_NEXT, input->keybit);
1811 set_bit(KEY_VIDEO_PREV, input->keybit);
1812 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1813 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1814 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1815 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1816 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001819 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1820 video->flags.multihead ? "yes" : "no",
1821 video->flags.rom ? "yes" : "no",
1822 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001824 video->pm_nb.notifier_call = acpi_video_resume;
1825 video->pm_nb.priority = 0;
Igor Murzovea9f8852012-03-30 21:32:08 +04001826 error = register_pm_notifier(&video->pm_nb);
1827 if (error)
Dan Carpenter301f33f2012-04-04 14:19:16 +03001828 goto err_stop_video;
1829
1830 error = input_register_device(input);
1831 if (error)
1832 goto err_unregister_pm_notifier;
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001833
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Dan Carpenter301f33f2012-04-04 14:19:16 +03001836 err_unregister_pm_notifier:
1837 unregister_pm_notifier(&video->pm_nb);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001838 err_stop_video:
1839 acpi_video_bus_stop_devices(video);
Igor Murzovb60e7f62012-03-30 21:32:09 +04001840 err_free_input_dev:
1841 input_free_device(input);
Igor Murzovea9f8852012-03-30 21:32:08 +04001842 err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001843 acpi_video_bus_put_devices(video);
1844 kfree(video->attached_array);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001845 err_free_video:
1846 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001847 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001848
1849 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001852static int acpi_video_bus_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Len Brown4be44fc2005-08-05 00:44:28 -04001854 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001858 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001860 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001862 unregister_pm_notifier(&video->pm_nb);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Luming Yue9dab192007-08-20 18:23:53 +08001867 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001868 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 kfree(video);
1870
Patrick Mocheld550d982006-06-27 00:41:40 -04001871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Alan Coxc6996bd2012-04-25 14:33:48 +01001874static int __init is_i740(struct pci_dev *dev)
1875{
1876 if (dev->device == 0x00D1)
1877 return 1;
1878 if (dev->device == 0x7000)
1879 return 1;
1880 return 0;
1881}
1882
Matthew Garrett74a365b2009-03-19 21:35:39 +00001883static int __init intel_opregion_present(void)
1884{
Alan Coxc6996bd2012-04-25 14:33:48 +01001885 int opregion = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001886 struct pci_dev *dev = NULL;
1887 u32 address;
1888
1889 for_each_pci_dev(dev) {
1890 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1891 continue;
1892 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1893 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001894 /* We don't want to poke around undefined i740 registers */
1895 if (is_i740(dev))
1896 continue;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001897 pci_read_config_dword(dev, 0xfc, &address);
1898 if (!address)
1899 continue;
Alan Coxc6996bd2012-04-25 14:33:48 +01001900 opregion = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001901 }
Alan Coxc6996bd2012-04-25 14:33:48 +01001902 return opregion;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001903}
1904
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001905int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001907 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001908 if (register_count) {
1909 /*
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001910 * if the function of acpi_video_register is already called,
1911 * don't register the acpi_vide_bus again and return no error.
Zhao Yakui86e437f2009-06-16 11:23:13 +08001912 */
1913 return 0;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08001917 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04001918 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Zhao Yakui86e437f2009-06-16 11:23:13 +08001920 /*
1921 * When the acpi_video_bus is loaded successfully, increase
1922 * the counter reference.
1923 */
1924 register_count = 1;
1925
Patrick Mocheld550d982006-06-27 00:41:40 -04001926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001928EXPORT_SYMBOL(acpi_video_register);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001929
Zhao Yakui86e437f2009-06-16 11:23:13 +08001930void acpi_video_unregister(void)
1931{
1932 if (!register_count) {
1933 /*
1934 * If the acpi video bus is already unloaded, don't
1935 * unload it again and return directly.
1936 */
1937 return;
1938 }
1939 acpi_bus_unregister_driver(&acpi_video_bus);
1940
Zhao Yakui86e437f2009-06-16 11:23:13 +08001941 register_count = 0;
1942
1943 return;
1944}
1945EXPORT_SYMBOL(acpi_video_unregister);
1946
Matthew Garrett74a365b2009-03-19 21:35:39 +00001947/*
1948 * This is kind of nasty. Hardware using Intel chipsets may require
1949 * the video opregion code to be run first in order to initialise
1950 * state before any ACPI video calls are made. To handle this we defer
1951 * registration of the video class until the opregion code has run.
1952 */
1953
1954static int __init acpi_video_init(void)
1955{
Zhang Rui45cb50e2009-04-24 12:13:18 -04001956 dmi_check_system(video_dmi_table);
1957
Matthew Garrett74a365b2009-03-19 21:35:39 +00001958 if (intel_opregion_present())
1959 return 0;
1960
1961 return acpi_video_register();
1962}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Zhao Yakui86e437f2009-06-16 11:23:13 +08001964static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Zhao Yakui86e437f2009-06-16 11:23:13 +08001966 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Patrick Mocheld550d982006-06-27 00:41:40 -04001968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971module_init(acpi_video_init);
1972module_exit(acpi_video_exit);