blob: 415448af36bf1e39c67937c3cd9297b5e5d88cf6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * video.c - ACPI Video Driver ($Revision:$)
3 *
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
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_VIDEO_BUS_NAME "Video Bus"
50#define ACPI_VIDEO_DEVICE_NAME "Video Device"
51#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
52#define ACPI_VIDEO_NOTIFY_PROBE 0x81
53#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
54#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
55#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
56
Thomas Tuttlef4715182006-12-19 12:56:14 -080057#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
58#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
59#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
60#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
61#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Yu Luming2f3d0002006-11-11 02:40:34 +080063#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050066ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Len Brownf52fd662007-02-12 22:42:12 -050068MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050069MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070MODULE_LICENSE("GPL");
71
Rusty Russell90ab5ee2012-01-13 09:32:20 +103072static bool brightness_switch_enabled = 1;
Zhang Rui8a681a42008-01-25 14:47:49 +080073module_param(brightness_switch_enabled, bool, 0644);
74
Zhang Ruic504f8c2009-12-30 15:59:23 +080075/*
76 * By default, we don't allow duplicate ACPI video bus devices
77 * under the same VGA controller
78 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103079static bool allow_duplicates;
Zhang Ruic504f8c2009-12-30 15:59:23 +080080module_param(allow_duplicates, bool, 0644);
81
Zhang Rui99fd1892010-12-06 15:04:27 +080082/*
83 * Some BIOSes claim they use minimum backlight at boot,
84 * and this may bring dimming screen after boot
85 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103086static bool use_bios_initial_backlight = 1;
Zhang Rui99fd1892010-12-06 15:04:27 +080087module_param(use_bios_initial_backlight, bool, 0644);
88
Zhao Yakui86e437f2009-06-16 11:23:13 +080089static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040090static int acpi_video_bus_add(struct acpi_device *device);
91static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000092static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Thomas Renninger1ba90e32007-07-23 14:44:41 +020094static const struct acpi_device_id video_device_ids[] = {
95 {ACPI_VIDEO_HID, 0},
96 {"", 0},
97};
98MODULE_DEVICE_TABLE(acpi, video_device_ids);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct acpi_driver acpi_video_bus = {
Len Brownc2b6705b2007-02-12 23:33:40 -0500101 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200103 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .ops = {
105 .add = acpi_video_bus_add,
106 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000107 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 u8 multihead:1; /* can switch video heads */
113 u8 rom:1; /* can retrieve a video rom */
114 u8 post:1; /* can configure the head to */
115 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400119 u8 _DOS:1; /*Enable/Disable output switching */
120 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
121 u8 _ROM:1; /*Get ROM Data */
122 u8 _GPD:1; /*Get POST Device */
123 u8 _SPD:1; /*Set POST Device */
124 u8 _VPO:1; /*Video POST Options */
125 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Len Brown4be44fc2005-08-05 00:44:28 -0400128struct acpi_video_device_attrib {
129 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100130 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400131 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100132 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400133 u32 bios_can_detect:1; /*BIOS can detect the device */
134 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
135 the VGA device. */
136 u32 pipe_id:3; /*For VGA multiple-head devices. */
137 u32 reserved:10; /*Must be 0 */
138 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct acpi_video_enumerated_device {
142 union {
143 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400144 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 } value;
146 struct acpi_video_device *bind_info;
147};
148
149struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400150 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 u8 attached_count;
154 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400156 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500157 struct mutex device_list_lock; /* protects video_device_list */
Luming Yue9dab192007-08-20 18:23:53 +0800158 struct input_dev *input;
159 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200160 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400164 u8 crt:1;
165 u8 lcd:1;
166 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500167 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 bios:1;
169 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500170 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400174 u8 _ADR:1; /*Return the unique ID */
175 u8 _BCL:1; /*Query list of brightness control levels supported */
176 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800177 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800178 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400179 u8 _DDC:1; /*Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Zhang Ruid32f6942009-03-18 16:27:12 +0800182struct acpi_video_brightness_flags {
183 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800184 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800185 u8 _BCL_use_index:1; /* levels in _BCL are index values */
186 u8 _BCM_use_index:1; /* input of _BCM is an index value */
187 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400191 int curr;
192 int count;
193 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800194 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
197struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400198 unsigned long device_id;
199 struct acpi_video_device_flags flags;
200 struct acpi_video_device_cap cap;
201 struct list_head entry;
202 struct acpi_video_bus *video;
203 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800205 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400206 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100209static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 "motherboard VGA device",
211 "PCI VGA device",
212 "AGP VGA device",
213 "UNKNOWN",
214};
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
217static void acpi_video_device_rebind(struct acpi_video_bus *video);
218static void acpi_video_device_bind(struct acpi_video_bus *video,
219 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800221static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
222 int level);
223static int acpi_video_device_lcd_get_level_current(
224 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500225 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400226static int acpi_video_get_next_level(struct acpi_video_device *device,
227 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800228static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400229 int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Yu Luming2f3d0002006-11-11 02:40:34 +0800231/*backlight device sysfs support*/
232static int acpi_video_get_brightness(struct backlight_device *bd)
233{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400234 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000235 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800236 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100237 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800238
Matthew Garrett70287db2010-02-16 16:53:50 -0500239 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800240 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000241 for (i = 2; i < vd->brightness->count; i++) {
242 if (vd->brightness->levels[i] == cur_level)
243 /* The first two entries are special - see page 575
244 of the ACPI spec 3.0 */
245 return i-2;
246 }
247 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800248}
249
250static int acpi_video_set_brightness(struct backlight_device *bd)
251{
Zhang Rui24450c72009-03-18 16:27:10 +0800252 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800253 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100254 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800255
256 return acpi_video_device_lcd_set_level(vd,
257 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800258}
259
Lionel Debrouxacc24722010-11-16 14:14:02 +0100260static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000261 .get_brightness = acpi_video_get_brightness,
262 .update_status = acpi_video_set_brightness,
263};
264
Zhang Rui702ed512008-01-17 15:51:22 +0800265/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400266static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000267 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800268{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400269 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800270 struct acpi_video_device *video = acpi_driver_data(device);
271
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000272 *state = video->brightness->count - 3;
273 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800274}
275
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400276static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000277 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800278{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400279 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800280 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400281 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000282 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800283
Matthew Garrett70287db2010-02-16 16:53:50 -0500284 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800285 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000286 for (offset = 2; offset < video->brightness->count; offset++)
287 if (level == video->brightness->levels[offset]) {
288 *state = video->brightness->count - offset - 1;
289 return 0;
290 }
Zhang Rui702ed512008-01-17 15:51:22 +0800291
292 return -EINVAL;
293}
294
295static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400296video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800297{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400298 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800299 struct acpi_video_device *video = acpi_driver_data(device);
300 int level;
301
302 if ( state >= video->brightness->count - 2)
303 return -EINVAL;
304
305 state = video->brightness->count - state;
306 level = video->brightness->levels[state -1];
307 return acpi_video_device_lcd_set_level(video, level);
308}
309
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400310static const struct thermal_cooling_device_ops video_cooling_ops = {
Zhang Rui702ed512008-01-17 15:51:22 +0800311 .get_max_state = video_get_max_state,
312 .get_cur_state = video_get_cur_state,
313 .set_cur_state = video_set_cur_state,
314};
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* --------------------------------------------------------------------------
317 Video Management
318 -------------------------------------------------------------------------- */
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static int
Len Brown4be44fc2005-08-05 00:44:28 -0400321acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
322 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Len Brown4be44fc2005-08-05 00:44:28 -0400324 int status;
325 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
326 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 *levels = NULL;
330
Patrick Mochel90130262006-05-19 16:54:48 -0400331 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400333 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400334 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500335 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400336 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 status = -EFAULT;
338 goto err;
339 }
340
341 *levels = obj;
342
Patrick Mocheld550d982006-06-27 00:41:40 -0400343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800346 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Patrick Mocheld550d982006-06-27 00:41:40 -0400348 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351static int
Len Brown4be44fc2005-08-05 00:44:28 -0400352acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Zhang Rui24450c72009-03-18 16:27:10 +0800354 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400355 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
356 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800357 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Zhang Rui24450c72009-03-18 16:27:10 +0800361 status = acpi_evaluate_object(device->dev->handle, "_BCM",
362 &args, NULL);
363 if (ACPI_FAILURE(status)) {
364 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
365 return -EIO;
366 }
367
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400368 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800369 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800370 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800371 if (device->backlight)
372 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800373 return 0;
374 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800375
Zhang Rui24450c72009-03-18 16:27:10 +0800376 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Zhang Rui45cb50e2009-04-24 12:13:18 -0400380/*
381 * For some buggy _BQC methods, we need to add a constant value to
382 * the _BQC return value to get the actual current brightness level
383 */
384
385static int bqc_offset_aml_bug_workaround;
386static int __init video_set_bqc_offset(const struct dmi_system_id *d)
387{
388 bqc_offset_aml_bug_workaround = 9;
389 return 0;
390}
391
Zhang Ruia5e247e2012-12-04 23:30:19 +0100392static int video_ignore_initial_backlight(const struct dmi_system_id *d)
393{
394 use_bios_initial_backlight = 0;
395 return 0;
396}
397
Zhang Rui45cb50e2009-04-24 12:13:18 -0400398static struct dmi_system_id video_dmi_table[] __initdata = {
399 /*
400 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
401 */
402 {
403 .callback = video_set_bqc_offset,
404 .ident = "Acer Aspire 5720",
405 .matches = {
406 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
407 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
408 },
409 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400410 {
411 .callback = video_set_bqc_offset,
412 .ident = "Acer Aspire 5710Z",
413 .matches = {
414 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
415 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
416 },
417 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400418 {
419 .callback = video_set_bqc_offset,
420 .ident = "eMachines E510",
421 .matches = {
422 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
423 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
424 },
425 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400426 {
427 .callback = video_set_bqc_offset,
428 .ident = "Acer Aspire 5315",
429 .matches = {
430 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
431 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
432 },
433 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800434 {
435 .callback = video_set_bqc_offset,
436 .ident = "Acer Aspire 7720",
437 .matches = {
438 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
439 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
440 },
441 },
Zhang Ruia5e247e2012-12-04 23:30:19 +0100442 {
443 .callback = video_ignore_initial_backlight,
444 .ident = "HP Folio 13-2000",
445 .matches = {
446 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
447 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13 - 2000 Notebook PC"),
448 },
449 },
Alex Hung871483f2013-05-28 02:05:09 +0000450 {
451 .callback = video_ignore_initial_backlight,
Ash Willis06f3ee42013-05-29 01:27:59 +0000452 .ident = "HP Pavilion g6 Notebook PC",
453 .matches = {
454 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
455 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion g6 Notebook PC"),
456 },
457 },
458 {
459 .callback = video_ignore_initial_backlight,
Alex Hung871483f2013-05-28 02:05:09 +0000460 .ident = "HP Pavilion m4",
461 .matches = {
462 DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
463 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion m4 Notebook PC"),
464 },
465 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400466 {}
467};
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static int
Len Brown4be44fc2005-08-05 00:44:28 -0400470acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500471 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Zhang Ruic8890f92009-03-18 16:27:08 +0800473 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800474 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800475
Zhang Ruic60d6382009-03-18 16:27:18 +0800476 if (device->cap._BQC || device->cap._BCQ) {
477 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
478
479 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800480 NULL, level);
481 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800482 if (device->brightness->flags._BQC_use_index) {
483 if (device->brightness->flags._BCL_reversed)
484 *level = device->brightness->count
485 - 3 - (*level);
486 *level = device->brightness->levels[*level + 2];
487
488 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400489 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800490 for (i = 2; i < device->brightness->count; i++)
491 if (device->brightness->levels[i] == *level) {
492 device->brightness->curr = *level;
493 return 0;
494 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500495 if (!init) {
496 /*
497 * BQC returned an invalid level.
498 * Stop using it.
499 */
500 ACPI_WARNING((AE_INFO,
501 "%s returned an invalid level",
502 buf));
503 device->cap._BQC = device->cap._BCQ = 0;
504 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800505 } else {
506 /* Fixme:
507 * should we return an error or ignore this failure?
508 * dev->brightness->curr is a cached value which stores
509 * the correct current backlight level in most cases.
510 * ACPI video backlight still works w/ buggy _BQC.
511 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
512 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800513 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
514 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800515 }
516 }
517
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400518 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static int
Len Brown4be44fc2005-08-05 00:44:28 -0400523acpi_video_device_EDID(struct acpi_video_device *device,
524 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Len Brown4be44fc2005-08-05 00:44:28 -0400526 int status;
527 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
528 union acpi_object *obj;
529 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
530 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 *edid = NULL;
534
535 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400536 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (length == 128)
538 arg0.integer.value = 1;
539 else if (length == 256)
540 arg0.integer.value = 2;
541 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Patrick Mochel90130262006-05-19 16:54:48 -0400544 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400546 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200548 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (obj && obj->type == ACPI_TYPE_BUFFER)
551 *edid = obj;
552 else {
Len Brown64684632006-06-26 23:41:38 -0400553 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 status = -EFAULT;
555 kfree(obj);
556 }
557
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561/* bus */
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * Arg:
565 * video : video bus device pointer
566 * bios_flag :
567 * 0. The system BIOS should NOT automatically switch(toggle)
568 * the active display output.
569 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100570 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * 2. The _DGS value should be locked.
572 * 3. The system BIOS should not automatically switch (toggle) the
573 * active display output, but instead generate the display switch
574 * event notify code.
575 * lcd_flag :
576 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100577 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100579 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * Return Value:
Igor Murzovea9f8852012-03-30 21:32:08 +0400581 * -EINVAL wrong arg.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583
584static int
Len Brown4be44fc2005-08-05 00:44:28 -0400585acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Igor Murzovea9f8852012-03-30 21:32:08 +0400587 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400588 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
589 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Zhang Rui4849c072012-06-20 09:48:43 +0800591 if (!video->cap._DOS)
592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Igor Murzovea9f8852012-03-30 21:32:08 +0400594 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
595 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 arg0.integer.value = (lcd_flag << 2) | bios_flag;
597 video->dos_setting = arg0.integer.value;
Igor Murzovea9f8852012-03-30 21:32:08 +0400598 status = acpi_evaluate_object(video->device->handle, "_DOS",
599 &args, NULL);
600 if (ACPI_FAILURE(status))
601 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Igor Murzovea9f8852012-03-30 21:32:08 +0400603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500607 * Simple comparison function used to sort backlight levels.
608 */
609
610static int
611acpi_video_cmp_level(const void *a, const void *b)
612{
613 return *(int *)a - *(int *)b;
614}
615
616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * Arg:
618 * device : video output device (LCD, CRT, ..)
619 *
620 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100621 * Maximum brightness level
622 *
623 * Allocate and initialize device->brightness.
624 */
625
626static int
627acpi_video_init_brightness(struct acpi_video_device *device)
628{
629 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800630 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800631 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100632 union acpi_object *o;
633 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800634 int result = -EINVAL;
Hans de Goedeb0b0c262014-02-13 16:32:51 +0100635 u32 value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100636
637 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
638 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
639 "LCD brightness level\n"));
640 goto out;
641 }
642
643 if (obj->package.count < 2)
644 goto out;
645
646 br = kzalloc(sizeof(*br), GFP_KERNEL);
647 if (!br) {
648 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800649 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100650 goto out;
651 }
652
Zhang Ruid32f6942009-03-18 16:27:12 +0800653 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100654 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800655 if (!br->levels) {
656 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100657 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800658 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100659
660 for (i = 0; i < obj->package.count; i++) {
661 o = (union acpi_object *)&obj->package.elements[i];
662 if (o->type != ACPI_TYPE_INTEGER) {
663 printk(KERN_ERR PREFIX "Invalid data\n");
664 continue;
665 }
Hans de Goedeb0b0c262014-02-13 16:32:51 +0100666 value = (u32) o->integer.value;
667 /* Skip duplicate entries */
668 if (count > 2 && br->levels[count - 1] == value)
669 continue;
670
671 br->levels[count] = value;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100672
673 if (br->levels[count] > max_level)
674 max_level = br->levels[count];
675 count++;
676 }
677
Zhang Ruid32f6942009-03-18 16:27:12 +0800678 /*
679 * some buggy BIOS don't export the levels
680 * when machine is on AC/Battery in _BCL package.
681 * In this case, the first two elements in _BCL packages
682 * are also supported brightness levels that OS should take care of.
683 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800684 for (i = 2; i < count; i++) {
685 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800686 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800687 if (br->levels[i] == br->levels[1])
688 level_ac_battery++;
689 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500690
Zhang Ruid32f6942009-03-18 16:27:12 +0800691 if (level_ac_battery < 2) {
692 level_ac_battery = 2 - level_ac_battery;
693 br->flags._BCL_no_ac_battery_levels = 1;
694 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
695 br->levels[i] = br->levels[i - level_ac_battery];
696 count += level_ac_battery;
697 } else if (level_ac_battery > 2)
698 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
699
Zhang Ruid80fb992009-03-18 16:27:14 +0800700 /* Check if the _BCL package is in a reversed order */
701 if (max_level == br->levels[2]) {
702 br->flags._BCL_reversed = 1;
703 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
704 acpi_video_cmp_level, NULL);
705 } else if (max_level != br->levels[count - 1])
706 ACPI_ERROR((AE_INFO,
707 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100708
709 br->count = count;
710 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800711
712 /* Check the input/output of _BQC/_BCL/_BCM */
713 if ((max_level < 100) && (max_level <= (count - 2)))
714 br->flags._BCL_use_index = 1;
715
716 /*
717 * _BCM is always consistent with _BCL,
718 * at least for all the laptops we have ever seen.
719 */
720 br->flags._BCM_use_index = br->flags._BCL_use_index;
721
722 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400723 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800724
725 if (!device->cap._BQC)
726 goto set_level;
727
Matthew Garrett70287db2010-02-16 16:53:50 -0500728 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800729 if (result)
730 goto out_free_levels;
731
Zhang Ruie047cca2009-04-09 14:24:35 +0800732 /*
733 * Set the level to maximum and check if _BQC uses indexed value
734 */
735 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800736 if (result)
737 goto out_free_levels;
738
Matthew Garrett70287db2010-02-16 16:53:50 -0500739 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800740 if (result)
741 goto out_free_levels;
742
Zhang Ruie047cca2009-04-09 14:24:35 +0800743 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800744
Zhang Rui90c53ca2009-08-31 12:39:54 -0400745 if (!br->flags._BQC_use_index) {
746 /*
747 * Set the backlight to the initial state.
748 * On some buggy laptops, _BQC returns an uninitialized value
749 * when invoked for the first time, i.e. level_old is invalid.
750 * set the backlight to max_level in this case
751 */
Zhang Rui99fd1892010-12-06 15:04:27 +0800752 if (use_bios_initial_backlight) {
753 for (i = 2; i < br->count; i++)
754 if (level_old == br->levels[i])
755 level = level_old;
756 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800757 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400758 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800759
760 if (br->flags._BCL_reversed)
761 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400762 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800763
764set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400765 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800766 if (result)
767 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800768
Zhang Ruid32f6942009-03-18 16:27:12 +0800769 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
770 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100771 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800772 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100773
774out_free_levels:
775 kfree(br->levels);
776out_free:
777 kfree(br);
778out:
779 device->brightness = NULL;
780 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800781 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100782}
783
784/*
785 * Arg:
786 * device : video output device (LCD, CRT, ..)
787 *
788 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * None
790 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100791 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * device.
793 */
794
Len Brown4be44fc2005-08-05 00:44:28 -0400795static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Patrick Mochel90130262006-05-19 16:54:48 -0400799 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 device->cap._ADR = 1;
801 }
Patrick Mochel90130262006-05-19 16:54:48 -0400802 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400803 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Patrick Mochel90130262006-05-19 16:54:48 -0400805 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400806 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800808 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
809 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800810 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
811 &h_dummy1))) {
812 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
813 device->cap._BCQ = 1;
814 }
815
Patrick Mochel90130262006-05-19 16:54:48 -0400816 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400817 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Zhang Rui1a7c6182009-03-18 16:27:16 +0800820 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500821 struct backlight_properties props;
Matthew Garrett9661e922011-03-22 16:30:25 -0700822 struct pci_dev *pdev;
823 acpi_handle acpi_parent;
824 struct device *parent = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +0800825 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800826 static int count = 0;
827 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800828
829 result = acpi_video_init_brightness(device);
830 if (result)
831 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700832 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +0800833 if (!name)
834 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700835 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +0800836
Matthew Garrett9661e922011-03-22 16:30:25 -0700837 acpi_get_parent(device->dev->handle, &acpi_parent);
838
839 pdev = acpi_get_pci_dev(acpi_parent);
840 if (pdev) {
841 parent = &pdev->dev;
842 pci_dev_put(pdev);
843 }
844
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500845 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700846 props.type = BACKLIGHT_FIRMWARE;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500847 props.max_brightness = device->brightness->count - 3;
Matthew Garrett9661e922011-03-22 16:30:25 -0700848 device->backlight = backlight_device_register(name,
849 parent,
850 device,
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500851 &acpi_backlight_ops,
852 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +0800853 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +0800854 if (IS_ERR(device->backlight))
855 return;
Zhang Rui702ed512008-01-17 15:51:22 +0800856
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200857 /*
858 * Save current brightness level in case we have to restore it
859 * before acpi_video_device_lcd_set_level() is called next time.
860 */
861 device->backlight->props.brightness =
862 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +0800863
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400864 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +0800865 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400866 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700867 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400868 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700869 * free it.
870 * Also, why the hell we are returning early and
871 * not attempt to register video output if cooling
872 * device registration failed?
873 * -- dtor
874 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400875 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500876 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700877 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500878
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200879 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400880 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800881 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400882 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800883 "thermal_cooling");
884 if (result)
885 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400886 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800887 &device->dev->dev.kobj, "device");
888 if (result)
889 printk(KERN_ERR PREFIX "Create sysfs link\n");
890
Yu Luming2f3d0002006-11-11 02:40:34 +0800891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/*
895 * Arg:
896 * device : video output device (VGA)
897 *
898 * Return Value:
899 * None
900 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100901 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
903
Len Brown4be44fc2005-08-05 00:44:28 -0400904static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Len Brown4be44fc2005-08-05 00:44:28 -0400906 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Patrick Mochel90130262006-05-19 16:54:48 -0400908 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 video->cap._DOS = 1;
910 }
Patrick Mochel90130262006-05-19 16:54:48 -0400911 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 video->cap._DOD = 1;
913 }
Patrick Mochel90130262006-05-19 16:54:48 -0400914 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 video->cap._ROM = 1;
916 }
Patrick Mochel90130262006-05-19 16:54:48 -0400917 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 video->cap._GPD = 1;
919 }
Patrick Mochel90130262006-05-19 16:54:48 -0400920 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 video->cap._SPD = 1;
922 }
Patrick Mochel90130262006-05-19 16:54:48 -0400923 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 video->cap._VPO = 1;
925 }
926}
927
928/*
929 * Check whether the video bus device has required AML method to
930 * support the desired features
931 */
932
Len Brown4be44fc2005-08-05 00:44:28 -0400933static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Len Brown4be44fc2005-08-05 00:44:28 -0400935 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000936 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000941 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200942 if (!dev)
943 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000944 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* Since there is no HID, CID and so on for VGA driver, we have
947 * to check well known required nodes.
948 */
949
Julius Volz98fb8fe2007-02-20 16:38:40 +0100950 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200951 if (video->cap._DOS || video->cap._DOD) {
952 if (!video->cap._DOS) {
953 printk(KERN_WARNING FW_BUG
954 "ACPI(%s) defines _DOD but not _DOS\n",
955 acpi_device_bid(video->device));
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 video->flags.multihead = 1;
958 status = 0;
959 }
960
Julius Volz98fb8fe2007-02-20 16:38:40 +0100961 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400962 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 video->flags.rom = 1;
964 status = 0;
965 }
966
Julius Volz98fb8fe2007-02-20 16:38:40 +0100967 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400968 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 video->flags.post = 1;
970 status = 0;
971 }
972
Patrick Mocheld550d982006-06-27 00:41:40 -0400973 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 Driver Interface
978 -------------------------------------------------------------------------- */
979
980/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -0500981static struct acpi_video_device_attrib*
982acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
983{
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500984 struct acpi_video_enumerated_device *ids;
985 int i;
Rui Zhang82cae992007-01-03 23:40:53 -0500986
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500987 for (i = 0; i < video->attached_count; i++) {
988 ids = &video->attached_array[i];
989 if ((ids->value.int_val & 0xffff) == device_id)
990 return &ids->value.attrib;
991 }
992
Rui Zhang82cae992007-01-03 23:40:53 -0500993 return NULL;
994}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996static int
Matthew Garrette92a7162010-01-12 14:17:03 -0500997acpi_video_get_device_type(struct acpi_video_bus *video,
998 unsigned long device_id)
999{
1000 struct acpi_video_enumerated_device *ids;
1001 int i;
1002
1003 for (i = 0; i < video->attached_count; i++) {
1004 ids = &video->attached_array[i];
1005 if ((ids->value.int_val & 0xffff) == device_id)
1006 return ids->value.int_val;
1007 }
1008
1009 return 0;
1010}
1011
1012static int
Len Brown4be44fc2005-08-05 00:44:28 -04001013acpi_video_bus_get_one_device(struct acpi_device *device,
1014 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001016 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001017 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001018 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001019 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Len Brown4be44fc2005-08-05 00:44:28 -04001024 status =
1025 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ACPI_SUCCESS(status)) {
1027
Burman Yan36bcbec2006-12-19 12:56:11 -08001028 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001030 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1033 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001034 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 data->device_id = device_id;
1037 data->video = video;
1038 data->dev = device;
1039
Rui Zhang82cae992007-01-03 23:40:53 -05001040 attribute = acpi_video_get_device_attr(video, device_id);
1041
1042 if((attribute != NULL) && attribute->device_id_scheme) {
1043 switch (attribute->display_type) {
1044 case ACPI_VIDEO_DISPLAY_CRT:
1045 data->flags.crt = 1;
1046 break;
1047 case ACPI_VIDEO_DISPLAY_TV:
1048 data->flags.tvout = 1;
1049 break;
1050 case ACPI_VIDEO_DISPLAY_DVI:
1051 data->flags.dvi = 1;
1052 break;
1053 case ACPI_VIDEO_DISPLAY_LCD:
1054 data->flags.lcd = 1;
1055 break;
1056 default:
1057 data->flags.unknown = 1;
1058 break;
1059 }
1060 if(attribute->bios_can_detect)
1061 data->flags.bios = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001062 } else {
1063 /* Check for legacy IDs */
1064 device_type = acpi_video_get_device_type(video,
1065 device_id);
1066 /* Ignore bits 16 and 18-20 */
1067 switch (device_type & 0xffe2ffff) {
1068 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1069 data->flags.crt = 1;
1070 break;
1071 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1072 data->flags.lcd = 1;
1073 break;
1074 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1075 data->flags.tvout = 1;
1076 break;
1077 default:
1078 data->flags.unknown = 1;
1079 }
1080 }
Len Brown4be44fc2005-08-05 00:44:28 -04001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 acpi_video_device_bind(video, data);
1083 acpi_video_device_find_cap(data);
1084
Patrick Mochel90130262006-05-19 16:54:48 -04001085 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001086 ACPI_DEVICE_NOTIFY,
1087 acpi_video_device_notify,
1088 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001090 printk(KERN_ERR PREFIX
1091 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001092 if(data->brightness)
1093 kfree(data->brightness->levels);
1094 kfree(data->brightness);
1095 kfree(data);
1096 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001099 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001101 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Patrick Mocheld550d982006-06-27 00:41:40 -04001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
Patrick Mocheld550d982006-06-27 00:41:40 -04001106 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109/*
1110 * Arg:
1111 * video : video bus device
1112 *
1113 * Return:
1114 * none
1115 *
1116 * Enumerate the video device list of the video bus,
1117 * bind the ids with the corresponding video devices
1118 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Len Brown4be44fc2005-08-05 00:44:28 -04001121static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001123 struct acpi_video_device *dev;
1124
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001125 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001126
1127 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001128 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001129
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001130 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/*
1134 * Arg:
1135 * video : video bus device
1136 * device : video output device under the video
1137 * bus
1138 *
1139 * Return:
1140 * none
1141 *
1142 * Bind the ids with the corresponding video devices
1143 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146static void
Len Brown4be44fc2005-08-05 00:44:28 -04001147acpi_video_device_bind(struct acpi_video_bus *video,
1148 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001150 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001153 for (i = 0; i < video->attached_count; i++) {
1154 ids = &video->attached_array[i];
1155 if (device->device_id == (ids->value.int_val & 0xffff)) {
1156 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1158 }
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162/*
1163 * Arg:
1164 * video : video bus device
1165 *
1166 * Return:
1167 * < 0 : error
1168 *
1169 * Call _DOD to enumerate all devices attached to display adapter
1170 *
Len Brown4be44fc2005-08-05 00:44:28 -04001171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1174{
Len Brown4be44fc2005-08-05 00:44:28 -04001175 int status;
1176 int count;
1177 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001178 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001179 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1180 union acpi_object *dod = NULL;
1181 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Patrick Mochel90130262006-05-19 16:54:48 -04001183 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001185 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001186 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001189 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001191 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 status = -EFAULT;
1193 goto out;
1194 }
1195
1196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001197 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001199 active_list = kcalloc(1 + dod->package.count,
1200 sizeof(struct acpi_video_enumerated_device),
1201 GFP_KERNEL);
1202 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 status = -ENOMEM;
1204 goto out;
1205 }
1206
1207 count = 0;
1208 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001209 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001212 printk(KERN_ERR PREFIX
1213 "Invalid _DOD data in element %d\n", i);
1214 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001216
1217 active_list[count].value.int_val = obj->integer.value;
1218 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1220 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 count++;
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jesper Juhl6044ec82005-11-07 01:01:32 -08001224 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001225
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001226 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001228
1229 out:
Len Brown02438d82006-06-30 03:19:10 -04001230 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001231 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Len Brown4be44fc2005-08-05 00:44:28 -04001234static int
1235acpi_video_get_next_level(struct acpi_video_device *device,
1236 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001238 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001239 max = max_below = 0;
1240 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001241 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001242 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001243 l = device->brightness->levels[i];
1244 if (abs(l - level_current) < abs(delta)) {
1245 delta = l - level_current;
1246 if (!delta)
1247 break;
1248 }
1249 }
1250 /* Ajust level_current to closest available level */
1251 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001252 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001253 l = device->brightness->levels[i];
1254 if (l < min)
1255 min = l;
1256 if (l > max)
1257 max = l;
1258 if (l < min_above && l > level_current)
1259 min_above = l;
1260 if (l > max_below && l < level_current)
1261 max_below = l;
1262 }
1263
1264 switch (event) {
1265 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1266 return (level_current < max) ? min_above : min;
1267 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1268 return (level_current < max) ? min_above : max;
1269 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1270 return (level_current > min) ? max_below : min;
1271 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1272 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1273 return 0;
1274 default:
1275 return level_current;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Zhang Ruic8890f92009-03-18 16:27:08 +08001279static int
Len Brown4be44fc2005-08-05 00:44:28 -04001280acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001282 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001283 int result = -EINVAL;
1284
Zhang Rui28c32e92009-07-13 10:33:24 +08001285 /* no warning message if acpi_backlight=vendor is used */
1286 if (!acpi_video_backlight_support())
1287 return 0;
1288
Julia Jomantaite469778c2008-06-23 22:50:42 +01001289 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001290 goto out;
1291
1292 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05001293 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08001294 if (result)
1295 goto out;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001298
Zhang Rui24450c72009-03-18 16:27:10 +08001299 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001300
Matthew Garrett36342742009-07-14 17:06:03 +01001301 if (!result)
1302 backlight_force_update(device->backlight,
1303 BACKLIGHT_UPDATE_HOTKEY);
1304
Zhang Ruic8890f92009-03-18 16:27:08 +08001305out:
1306 if (result)
1307 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1308
1309 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Matthew Garrette92a7162010-01-12 14:17:03 -05001312int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1313 void **edid)
1314{
1315 struct acpi_video_bus *video;
1316 struct acpi_video_device *video_device;
1317 union acpi_object *buffer = NULL;
1318 acpi_status status;
1319 int i, length;
1320
1321 if (!device || !acpi_driver_data(device))
1322 return -EINVAL;
1323
1324 video = acpi_driver_data(device);
1325
1326 for (i = 0; i < video->attached_count; i++) {
1327 video_device = video->attached_array[i].bind_info;
1328 length = 256;
1329
1330 if (!video_device)
1331 continue;
1332
Zhang Rui82069552010-12-06 15:04:24 +08001333 if (!video_device->cap._DDC)
1334 continue;
1335
Matthew Garrette92a7162010-01-12 14:17:03 -05001336 if (type) {
1337 switch (type) {
1338 case ACPI_VIDEO_DISPLAY_CRT:
1339 if (!video_device->flags.crt)
1340 continue;
1341 break;
1342 case ACPI_VIDEO_DISPLAY_TV:
1343 if (!video_device->flags.tvout)
1344 continue;
1345 break;
1346 case ACPI_VIDEO_DISPLAY_DVI:
1347 if (!video_device->flags.dvi)
1348 continue;
1349 break;
1350 case ACPI_VIDEO_DISPLAY_LCD:
1351 if (!video_device->flags.lcd)
1352 continue;
1353 break;
1354 }
1355 } else if (video_device->device_id != device_id) {
1356 continue;
1357 }
1358
1359 status = acpi_video_device_EDID(video_device, &buffer, length);
1360
1361 if (ACPI_FAILURE(status) || !buffer ||
1362 buffer->type != ACPI_TYPE_BUFFER) {
1363 length = 128;
1364 status = acpi_video_device_EDID(video_device, &buffer,
1365 length);
1366 if (ACPI_FAILURE(status) || !buffer ||
1367 buffer->type != ACPI_TYPE_BUFFER) {
1368 continue;
1369 }
1370 }
1371
1372 *edid = buffer->buffer.pointer;
1373 return length;
1374 }
1375
1376 return -ENODEV;
1377}
1378EXPORT_SYMBOL(acpi_video_get_edid);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static int
Len Brown4be44fc2005-08-05 00:44:28 -04001381acpi_video_bus_get_devices(struct acpi_video_bus *video,
1382 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Igor Murzov8217df02012-10-13 04:41:25 +04001384 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001385 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Igor Murzov8217df02012-10-13 04:41:25 +04001387 /*
1388 * There are systems where video module known to work fine regardless
1389 * of broken _DOD and ignoring returned value here doesn't cause
1390 * any issues later.
1391 */
1392 acpi_video_device_enumerate(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001394 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 status = acpi_video_bus_get_one_device(dev, video);
Igor Murzovea9f8852012-03-30 21:32:08 +04001397 if (status) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001398 printk(KERN_WARNING PREFIX
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001399 "Can't attach device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 continue;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Len Brown4be44fc2005-08-05 00:44:28 -04001406static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Karol Kozimor031ec772005-07-30 04:18:00 -04001408 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001411 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Patrick Mochel90130262006-05-19 16:54:48 -04001413 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001414 ACPI_DEVICE_NOTIFY,
1415 acpi_video_device_notify);
Andi Kleencfa806f2010-07-20 15:18:36 -07001416 if (ACPI_FAILURE(status)) {
1417 printk(KERN_WARNING PREFIX
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001418 "Can't remove video notify handler\n");
Andi Kleencfa806f2010-07-20 15:18:36 -07001419 }
Keith Packarde29b3ee2009-08-06 15:57:54 -07001420 if (device->backlight) {
Keith Packarde29b3ee2009-08-06 15:57:54 -07001421 backlight_device_unregister(device->backlight);
1422 device->backlight = NULL;
1423 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001424 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08001425 sysfs_remove_link(&device->dev->dev.kobj,
1426 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001427 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08001428 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001429 thermal_cooling_device_unregister(device->cooling_dev);
1430 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08001431 }
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001432
Patrick Mocheld550d982006-06-27 00:41:40 -04001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Len Brown4be44fc2005-08-05 00:44:28 -04001436static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Len Brown4be44fc2005-08-05 00:44:28 -04001438 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001439 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001441 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001443 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001445 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001446 if (ACPI_FAILURE(status))
1447 printk(KERN_WARNING PREFIX
1448 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001450 if (dev->brightness) {
1451 kfree(dev->brightness->levels);
1452 kfree(dev->brightness);
1453 }
1454 list_del(&dev->entry);
1455 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001458 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001459
Patrick Mocheld550d982006-06-27 00:41:40 -04001460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
1463/* acpi_video interface */
1464
Len Brown4be44fc2005-08-05 00:44:28 -04001465static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Zhang Ruia21101c2007-09-14 11:46:22 +08001467 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Len Brown4be44fc2005-08-05 00:44:28 -04001470static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 return acpi_video_bus_DOS(video, 0, 1);
1473}
1474
Bjorn Helgaas70155582009-04-07 15:37:11 +00001475static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001477 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001478 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001479 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Luming Yue9dab192007-08-20 18:23:53 +08001484 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001487 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001489 acpi_bus_generate_proc_event(device, event, 0);
Matthew Garrettf5a3d0c2011-07-12 18:30:52 -04001490 if (!acpi_notifier_call_chain(device, event, 0))
1491 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
1493
Julius Volz98fb8fe2007-02-20 16:38:40 +01001494 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * connector. */
1496 acpi_video_device_enumerate(video);
1497 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001498 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001499 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
1501
Len Brown4be44fc2005-08-05 00:44:28 -04001502 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001503 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001504 keycode = KEY_SWITCHVIDEOMODE;
1505 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001506 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001507 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001508 keycode = KEY_VIDEO_NEXT;
1509 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001510 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001511 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001512 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514
1515 default:
1516 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001517 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 break;
1519 }
1520
Matthew Garrettf5a3d0c2011-07-12 18:30:52 -04001521 if (event != ACPI_VIDEO_NOTIFY_SWITCH)
1522 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001523
1524 if (keycode) {
1525 input_report_key(input, keycode, 1);
1526 input_sync(input);
1527 input_report_key(input, keycode, 0);
1528 input_sync(input);
1529 }
Luming Yue9dab192007-08-20 18:23:53 +08001530
Patrick Mocheld550d982006-06-27 00:41:40 -04001531 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Len Brown4be44fc2005-08-05 00:44:28 -04001534static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001536 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001537 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001538 struct acpi_video_bus *bus;
1539 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001540 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001545 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001546 bus = video_device->video;
1547 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001550 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001551 if (brightness_switch_enabled)
1552 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001553 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001554 keycode = KEY_BRIGHTNESS_CYCLE;
1555 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001556 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001557 if (brightness_switch_enabled)
1558 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001559 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001560 keycode = KEY_BRIGHTNESSUP;
1561 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001562 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001563 if (brightness_switch_enabled)
1564 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001565 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001566 keycode = KEY_BRIGHTNESSDOWN;
1567 break;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001568 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001569 if (brightness_switch_enabled)
1570 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001571 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001572 keycode = KEY_BRIGHTNESS_ZERO;
1573 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001574 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001575 if (brightness_switch_enabled)
1576 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001577 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001578 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 default:
1581 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001582 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 }
Luming Yue9dab192007-08-20 18:23:53 +08001585
Zhang Rui7761f632008-01-25 14:48:12 +08001586 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001587
1588 if (keycode) {
1589 input_report_key(input, keycode, 1);
1590 input_sync(input);
1591 input_report_key(input, keycode, 0);
1592 input_sync(input);
1593 }
Luming Yue9dab192007-08-20 18:23:53 +08001594
Patrick Mocheld550d982006-06-27 00:41:40 -04001595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001598static int acpi_video_resume(struct notifier_block *nb,
1599 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001600{
1601 struct acpi_video_bus *video;
1602 struct acpi_video_device *video_device;
1603 int i;
1604
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001605 switch (val) {
1606 case PM_HIBERNATION_PREPARE:
1607 case PM_SUSPEND_PREPARE:
1608 case PM_RESTORE_PREPARE:
1609 return NOTIFY_DONE;
1610 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001611
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001612 video = container_of(nb, struct acpi_video_bus, pm_nb);
1613
1614 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001615
1616 for (i = 0; i < video->attached_count; i++) {
1617 video_device = video->attached_array[i].bind_info;
1618 if (video_device && video_device->backlight)
1619 acpi_video_set_brightness(video_device->backlight);
1620 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001621
1622 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001623}
1624
Zhang Ruic504f8c2009-12-30 15:59:23 +08001625static acpi_status
1626acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1627 void **return_value)
1628{
1629 struct acpi_device *device = context;
1630 struct acpi_device *sibling;
1631 int result;
1632
1633 if (handle == device->handle)
1634 return AE_CTRL_TERMINATE;
1635
1636 result = acpi_bus_get_device(handle, &sibling);
1637 if (result)
1638 return AE_OK;
1639
1640 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1641 return AE_ALREADY_EXISTS;
1642
1643 return AE_OK;
1644}
1645
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001646static int instance;
1647
Len Brown4be44fc2005-08-05 00:44:28 -04001648static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001650 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001651 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001652 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001653 acpi_status status;
1654
1655 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1656 device->parent->handle, 1,
1657 acpi_video_bus_match, NULL,
1658 device, NULL);
1659 if (status == AE_ALREADY_EXISTS) {
1660 printk(KERN_WARNING FW_BUG
1661 "Duplicate ACPI video bus devices for the"
1662 " same VGA controller, please try module "
1663 "parameter \"video.allow_duplicates=1\""
1664 "if the current driver doesn't work.\n");
1665 if (!allow_duplicates)
1666 return -ENODEV;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Burman Yan36bcbec2006-12-19 12:56:11 -08001669 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001671 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Zhang Ruie6d9da12007-08-25 02:23:31 -04001673 /* a hack to fix the duplicate name "VID" problem on T61 */
1674 if (!strcmp(device->pnp.bus_id, "VID")) {
1675 if (instance)
1676 device->pnp.bus_id[3] = '0' + instance;
1677 instance ++;
1678 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001679 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1680 if (!strcmp(device->pnp.bus_id, "VGA")) {
1681 if (instance)
1682 device->pnp.bus_id[3] = '0' + instance;
1683 instance++;
1684 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001685
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001686 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1688 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001689 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001692 error = acpi_video_bus_check(video);
1693 if (error)
1694 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001696 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 INIT_LIST_HEAD(&video->video_device_list);
1698
Igor Murzovea9f8852012-03-30 21:32:08 +04001699 error = acpi_video_bus_get_devices(video, device);
1700 if (error)
1701 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Luming Yue9dab192007-08-20 18:23:53 +08001703 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001704 if (!input) {
1705 error = -ENOMEM;
Igor Murzovb60e7f62012-03-30 21:32:09 +04001706 goto err_put_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001707 }
Luming Yue9dab192007-08-20 18:23:53 +08001708
Igor Murzovb60e7f62012-03-30 21:32:09 +04001709 error = acpi_video_bus_start_devices(video);
1710 if (error)
1711 goto err_free_input_dev;
1712
Luming Yue9dab192007-08-20 18:23:53 +08001713 snprintf(video->phys, sizeof(video->phys),
1714 "%s/video/input0", acpi_device_hid(video->device));
1715
1716 input->name = acpi_device_name(video->device);
1717 input->phys = video->phys;
1718 input->id.bustype = BUS_HOST;
1719 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05001720 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001721 input->evbit[0] = BIT(EV_KEY);
1722 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1723 set_bit(KEY_VIDEO_NEXT, input->keybit);
1724 set_bit(KEY_VIDEO_PREV, input->keybit);
1725 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1726 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1727 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1728 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1729 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08001730
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001731 error = input_register_device(input);
1732 if (error)
Igor Murzovb60e7f62012-03-30 21:32:09 +04001733 goto err_stop_video;
Luming Yue9dab192007-08-20 18:23:53 +08001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001736 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1737 video->flags.multihead ? "yes" : "no",
1738 video->flags.rom ? "yes" : "no",
1739 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001741 video->pm_nb.notifier_call = acpi_video_resume;
1742 video->pm_nb.priority = 0;
Igor Murzovea9f8852012-03-30 21:32:08 +04001743 error = register_pm_notifier(&video->pm_nb);
1744 if (error)
1745 goto err_unregister_input_dev;
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001746
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Igor Murzovea9f8852012-03-30 21:32:08 +04001749 err_unregister_input_dev:
1750 input_unregister_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001751 err_stop_video:
1752 acpi_video_bus_stop_devices(video);
Igor Murzovb60e7f62012-03-30 21:32:09 +04001753 err_free_input_dev:
1754 input_free_device(input);
Igor Murzovea9f8852012-03-30 21:32:08 +04001755 err_put_video:
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001756 acpi_video_bus_put_devices(video);
1757 kfree(video->attached_array);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001758 err_free_video:
1759 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001760 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001761
1762 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Len Brown4be44fc2005-08-05 00:44:28 -04001765static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Len Brown4be44fc2005-08-05 00:44:28 -04001767 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001773 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001775 unregister_pm_notifier(&video->pm_nb);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Luming Yue9dab192007-08-20 18:23:53 +08001780 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001781 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 kfree(video);
1783
Patrick Mocheld550d982006-06-27 00:41:40 -04001784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Matthew Garrett74a365b2009-03-19 21:35:39 +00001787static int __init intel_opregion_present(void)
1788{
Alan Cox3e2b0c72012-04-25 14:33:33 +01001789 int i915 = 0;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001790#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1791 struct pci_dev *dev = NULL;
1792 u32 address;
1793
1794 for_each_pci_dev(dev) {
1795 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1796 continue;
1797 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1798 continue;
1799 pci_read_config_dword(dev, 0xfc, &address);
1800 if (!address)
1801 continue;
Alan Cox3e2b0c72012-04-25 14:33:33 +01001802 i915 = 1;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001803 }
1804#endif
Alan Cox3e2b0c72012-04-25 14:33:33 +01001805 return i915;
Matthew Garrett74a365b2009-03-19 21:35:39 +00001806}
1807
1808int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Len Brown4be44fc2005-08-05 00:44:28 -04001810 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001811 if (register_count) {
1812 /*
1813 * if the function of acpi_video_register is already called,
1814 * don't register the acpi_vide_bus again and return no error.
1815 */
1816 return 0;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08001820 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04001821 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Zhao Yakui86e437f2009-06-16 11:23:13 +08001823 /*
1824 * When the acpi_video_bus is loaded successfully, increase
1825 * the counter reference.
1826 */
1827 register_count = 1;
1828
Patrick Mocheld550d982006-06-27 00:41:40 -04001829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
Matthew Garrett74a365b2009-03-19 21:35:39 +00001831EXPORT_SYMBOL(acpi_video_register);
1832
Zhao Yakui86e437f2009-06-16 11:23:13 +08001833void acpi_video_unregister(void)
1834{
1835 if (!register_count) {
1836 /*
1837 * If the acpi video bus is already unloaded, don't
1838 * unload it again and return directly.
1839 */
1840 return;
1841 }
1842 acpi_bus_unregister_driver(&acpi_video_bus);
1843
Zhao Yakui86e437f2009-06-16 11:23:13 +08001844 register_count = 0;
1845
1846 return;
1847}
1848EXPORT_SYMBOL(acpi_video_unregister);
1849
Matthew Garrett74a365b2009-03-19 21:35:39 +00001850/*
1851 * This is kind of nasty. Hardware using Intel chipsets may require
1852 * the video opregion code to be run first in order to initialise
1853 * state before any ACPI video calls are made. To handle this we defer
1854 * registration of the video class until the opregion code has run.
1855 */
1856
1857static int __init acpi_video_init(void)
1858{
Zhang Rui45cb50e2009-04-24 12:13:18 -04001859 dmi_check_system(video_dmi_table);
1860
Matthew Garrett74a365b2009-03-19 21:35:39 +00001861 if (intel_opregion_present())
1862 return 0;
1863
1864 return acpi_video_register();
1865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Zhao Yakui86e437f2009-06-16 11:23:13 +08001867static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Zhao Yakui86e437f2009-06-16 11:23:13 +08001869 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Patrick Mocheld550d982006-06-27 00:41:40 -04001871 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874module_init(acpi_video_init);
1875module_exit(acpi_video_exit);