blob: 177b4ddc34790f10360ee140da28785d38c36473 [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_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_VIDEO_BUS_NAME "Video Bus"
51#define ACPI_VIDEO_DEVICE_NAME "Video Device"
52#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
53#define ACPI_VIDEO_NOTIFY_PROBE 0x81
54#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
55#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
56#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
57
Thomas Tuttlef4715182006-12-19 12:56:14 -080058#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
59#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
60#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
61#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
62#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yu Luming2f3d0002006-11-11 02:40:34 +080064#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050067ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Len Brownf52fd662007-02-12 22:42:12 -050069MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050070MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071MODULE_LICENSE("GPL");
72
Zhang Rui8a681a42008-01-25 14:47:49 +080073static int brightness_switch_enabled = 1;
74module_param(brightness_switch_enabled, bool, 0644);
75
Zhang Ruic504f8c2009-12-30 15:59:23 +080076/*
77 * By default, we don't allow duplicate ACPI video bus devices
78 * under the same VGA controller
79 */
80static int allow_duplicates;
81module_param(allow_duplicates, bool, 0644);
82
Zhao Yakui86e437f2009-06-16 11:23:13 +080083static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040084static int acpi_video_bus_add(struct acpi_device *device);
85static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000086static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Thomas Renninger1ba90e32007-07-23 14:44:41 +020088static const struct acpi_device_id video_device_ids[] = {
89 {ACPI_VIDEO_HID, 0},
90 {"", 0},
91};
92MODULE_DEVICE_TABLE(acpi, video_device_ids);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050095 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020097 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .ops = {
99 .add = acpi_video_bus_add,
100 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000101 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400102 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u8 multihead:1; /* can switch video heads */
107 u8 rom:1; /* can retrieve a video rom */
108 u8 post:1; /* can configure the head to */
109 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400113 u8 _DOS:1; /*Enable/Disable output switching */
114 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
115 u8 _ROM:1; /*Get ROM Data */
116 u8 _GPD:1; /*Get POST Device */
117 u8 _SPD:1; /*Set POST Device */
118 u8 _VPO:1; /*Video POST Options */
119 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122struct acpi_video_device_attrib {
123 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100124 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400125 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100126 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400127 u32 bios_can_detect:1; /*BIOS can detect the device */
128 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
129 the VGA device. */
130 u32 pipe_id:3; /*For VGA multiple-head devices. */
131 u32 reserved:10; /*Must be 0 */
132 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct acpi_video_enumerated_device {
136 union {
137 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } value;
140 struct acpi_video_device *bind_info;
141};
142
143struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400144 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 u8 attached_count;
148 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500151 struct mutex device_list_lock; /* protects video_device_list */
Luming Yue9dab192007-08-20 18:23:53 +0800152 struct input_dev *input;
153 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200154 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400158 u8 crt:1;
159 u8 lcd:1;
160 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500161 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 u8 bios:1;
163 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500164 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 _ADR:1; /*Return the unique ID */
169 u8 _BCL:1; /*Query list of brightness control levels supported */
170 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800171 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800172 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400173 u8 _DDC:1; /*Return the EDID for this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
Zhang Ruid32f6942009-03-18 16:27:12 +0800176struct acpi_video_brightness_flags {
177 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800178 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800179 u8 _BCL_use_index:1; /* levels in _BCL are index values */
180 u8 _BCM_use_index:1; /* input of _BCM is an index value */
181 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int curr;
186 int count;
187 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800188 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400192 unsigned long device_id;
193 struct acpi_video_device_flags flags;
194 struct acpi_video_device_cap cap;
195 struct list_head entry;
196 struct acpi_video_bus *video;
197 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800199 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400200 struct thermal_cooling_device *cooling_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100203static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 "motherboard VGA device",
205 "PCI VGA device",
206 "AGP VGA device",
207 "UNKNOWN",
208};
209
Len Brown4be44fc2005-08-05 00:44:28 -0400210static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
211static void acpi_video_device_rebind(struct acpi_video_bus *video);
212static void acpi_video_device_bind(struct acpi_video_bus *video,
213 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800215static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
216 int level);
217static int acpi_video_device_lcd_get_level_current(
218 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500219 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400220static int acpi_video_get_next_level(struct acpi_video_device *device,
221 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800222static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400223 int event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Yu Luming2f3d0002006-11-11 02:40:34 +0800225/*backlight device sysfs support*/
226static int acpi_video_get_brightness(struct backlight_device *bd)
227{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400228 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000229 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800230 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100231 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800232
Matthew Garrett70287db2010-02-16 16:53:50 -0500233 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800234 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000235 for (i = 2; i < vd->brightness->count; i++) {
236 if (vd->brightness->levels[i] == cur_level)
237 /* The first two entries are special - see page 575
238 of the ACPI spec 3.0 */
239 return i-2;
240 }
241 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800242}
243
244static int acpi_video_set_brightness(struct backlight_device *bd)
245{
Zhang Rui24450c72009-03-18 16:27:10 +0800246 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800247 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100248 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800249
250 return acpi_video_device_lcd_set_level(vd,
251 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800252}
253
Richard Purdie599a52d2007-02-10 23:07:48 +0000254static struct backlight_ops acpi_backlight_ops = {
255 .get_brightness = acpi_video_get_brightness,
256 .update_status = acpi_video_set_brightness,
257};
258
Zhang Rui702ed512008-01-17 15:51:22 +0800259/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400260static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000261 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800262{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400263 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800264 struct acpi_video_device *video = acpi_driver_data(device);
265
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000266 *state = video->brightness->count - 3;
267 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800268}
269
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400270static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000271 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800272{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400273 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800274 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400275 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000276 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800277
Matthew Garrett70287db2010-02-16 16:53:50 -0500278 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800279 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000280 for (offset = 2; offset < video->brightness->count; offset++)
281 if (level == video->brightness->levels[offset]) {
282 *state = video->brightness->count - offset - 1;
283 return 0;
284 }
Zhang Rui702ed512008-01-17 15:51:22 +0800285
286 return -EINVAL;
287}
288
289static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400290video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800291{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400292 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800293 struct acpi_video_device *video = acpi_driver_data(device);
294 int level;
295
296 if ( state >= video->brightness->count - 2)
297 return -EINVAL;
298
299 state = video->brightness->count - state;
300 level = video->brightness->levels[state -1];
301 return acpi_video_device_lcd_set_level(video, level);
302}
303
304static struct thermal_cooling_device_ops video_cooling_ops = {
305 .get_max_state = video_get_max_state,
306 .get_cur_state = video_get_cur_state,
307 .set_cur_state = video_set_cur_state,
308};
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* --------------------------------------------------------------------------
311 Video Management
312 -------------------------------------------------------------------------- */
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int
Len Brown4be44fc2005-08-05 00:44:28 -0400315acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
316 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Len Brown4be44fc2005-08-05 00:44:28 -0400318 int status;
319 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
320 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 *levels = NULL;
324
Patrick Mochel90130262006-05-19 16:54:48 -0400325 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400328 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500329 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400330 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 status = -EFAULT;
332 goto err;
333 }
334
335 *levels = obj;
336
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Len Brown4be44fc2005-08-05 00:44:28 -0400339 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800340 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Patrick Mocheld550d982006-06-27 00:41:40 -0400342 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static int
Len Brown4be44fc2005-08-05 00:44:28 -0400346acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Zhang Rui24450c72009-03-18 16:27:10 +0800348 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400349 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
350 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800351 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Zhang Rui24450c72009-03-18 16:27:10 +0800355 status = acpi_evaluate_object(device->dev->handle, "_BCM",
356 &args, NULL);
357 if (ACPI_FAILURE(status)) {
358 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
359 return -EIO;
360 }
361
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400362 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800363 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800364 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800365 if (device->backlight)
366 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800367 return 0;
368 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800369
Zhang Rui24450c72009-03-18 16:27:10 +0800370 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Zhang Rui45cb50e2009-04-24 12:13:18 -0400374/*
375 * For some buggy _BQC methods, we need to add a constant value to
376 * the _BQC return value to get the actual current brightness level
377 */
378
379static int bqc_offset_aml_bug_workaround;
380static int __init video_set_bqc_offset(const struct dmi_system_id *d)
381{
382 bqc_offset_aml_bug_workaround = 9;
383 return 0;
384}
385
386static struct dmi_system_id video_dmi_table[] __initdata = {
387 /*
388 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
389 */
390 {
391 .callback = video_set_bqc_offset,
392 .ident = "Acer Aspire 5720",
393 .matches = {
394 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
395 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
396 },
397 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400398 {
399 .callback = video_set_bqc_offset,
400 .ident = "Acer Aspire 5710Z",
401 .matches = {
402 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
403 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
404 },
405 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400406 {
407 .callback = video_set_bqc_offset,
408 .ident = "eMachines E510",
409 .matches = {
410 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
411 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
412 },
413 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400414 {
415 .callback = video_set_bqc_offset,
416 .ident = "Acer Aspire 5315",
417 .matches = {
418 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
419 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
420 },
421 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800422 {
423 .callback = video_set_bqc_offset,
424 .ident = "Acer Aspire 7720",
425 .matches = {
426 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
427 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
428 },
429 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400430 {}
431};
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int
Len Brown4be44fc2005-08-05 00:44:28 -0400434acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500435 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Zhang Ruic8890f92009-03-18 16:27:08 +0800437 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800438 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800439
Zhang Ruic60d6382009-03-18 16:27:18 +0800440 if (device->cap._BQC || device->cap._BCQ) {
441 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
442
443 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800444 NULL, level);
445 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800446 if (device->brightness->flags._BQC_use_index) {
447 if (device->brightness->flags._BCL_reversed)
448 *level = device->brightness->count
449 - 3 - (*level);
450 *level = device->brightness->levels[*level + 2];
451
452 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400453 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800454 for (i = 2; i < device->brightness->count; i++)
455 if (device->brightness->levels[i] == *level) {
456 device->brightness->curr = *level;
457 return 0;
458 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500459 if (!init) {
460 /*
461 * BQC returned an invalid level.
462 * Stop using it.
463 */
464 ACPI_WARNING((AE_INFO,
465 "%s returned an invalid level",
466 buf));
467 device->cap._BQC = device->cap._BCQ = 0;
468 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800469 } else {
470 /* Fixme:
471 * should we return an error or ignore this failure?
472 * dev->brightness->curr is a cached value which stores
473 * the correct current backlight level in most cases.
474 * ACPI video backlight still works w/ buggy _BQC.
475 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
476 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800477 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
478 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800479 }
480 }
481
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400482 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static int
Len Brown4be44fc2005-08-05 00:44:28 -0400487acpi_video_device_EDID(struct acpi_video_device *device,
488 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Len Brown4be44fc2005-08-05 00:44:28 -0400490 int status;
491 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
492 union acpi_object *obj;
493 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
494 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 *edid = NULL;
498
499 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (length == 128)
502 arg0.integer.value = 1;
503 else if (length == 256)
504 arg0.integer.value = 2;
505 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Patrick Mochel90130262006-05-19 16:54:48 -0400508 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200512 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (obj && obj->type == ACPI_TYPE_BUFFER)
515 *edid = obj;
516 else {
Len Brown64684632006-06-26 23:41:38 -0400517 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 status = -EFAULT;
519 kfree(obj);
520 }
521
Patrick Mocheld550d982006-06-27 00:41:40 -0400522 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/* bus */
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * Arg:
529 * video : video bus device pointer
530 * bios_flag :
531 * 0. The system BIOS should NOT automatically switch(toggle)
532 * the active display output.
533 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100534 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * 2. The _DGS value should be locked.
536 * 3. The system BIOS should not automatically switch (toggle) the
537 * active display output, but instead generate the display switch
538 * event notify code.
539 * lcd_flag :
540 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100541 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100543 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * Return Value:
545 * -1 wrong arg.
546 */
547
548static int
Len Brown4be44fc2005-08-05 00:44:28 -0400549acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Lin Ming439913f2010-01-28 10:53:19 +0800551 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400552 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
553 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Len Brown4be44fc2005-08-05 00:44:28 -0400556 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 status = -1;
558 goto Failed;
559 }
560 arg0.integer.value = (lcd_flag << 2) | bios_flag;
561 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400562 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Len Brown4be44fc2005-08-05 00:44:28 -0400564 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400565 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500569 * Simple comparison function used to sort backlight levels.
570 */
571
572static int
573acpi_video_cmp_level(const void *a, const void *b)
574{
575 return *(int *)a - *(int *)b;
576}
577
578/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * Arg:
580 * device : video output device (LCD, CRT, ..)
581 *
582 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100583 * Maximum brightness level
584 *
585 * Allocate and initialize device->brightness.
586 */
587
588static int
589acpi_video_init_brightness(struct acpi_video_device *device)
590{
591 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800592 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800593 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100594 union acpi_object *o;
595 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800596 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100597
598 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
599 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
600 "LCD brightness level\n"));
601 goto out;
602 }
603
604 if (obj->package.count < 2)
605 goto out;
606
607 br = kzalloc(sizeof(*br), GFP_KERNEL);
608 if (!br) {
609 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800610 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100611 goto out;
612 }
613
Zhang Ruid32f6942009-03-18 16:27:12 +0800614 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100615 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800616 if (!br->levels) {
617 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100618 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800619 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100620
621 for (i = 0; i < obj->package.count; i++) {
622 o = (union acpi_object *)&obj->package.elements[i];
623 if (o->type != ACPI_TYPE_INTEGER) {
624 printk(KERN_ERR PREFIX "Invalid data\n");
625 continue;
626 }
627 br->levels[count] = (u32) o->integer.value;
628
629 if (br->levels[count] > max_level)
630 max_level = br->levels[count];
631 count++;
632 }
633
Zhang Ruid32f6942009-03-18 16:27:12 +0800634 /*
635 * some buggy BIOS don't export the levels
636 * when machine is on AC/Battery in _BCL package.
637 * In this case, the first two elements in _BCL packages
638 * are also supported brightness levels that OS should take care of.
639 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800640 for (i = 2; i < count; i++) {
641 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800642 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800643 if (br->levels[i] == br->levels[1])
644 level_ac_battery++;
645 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500646
Zhang Ruid32f6942009-03-18 16:27:12 +0800647 if (level_ac_battery < 2) {
648 level_ac_battery = 2 - level_ac_battery;
649 br->flags._BCL_no_ac_battery_levels = 1;
650 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
651 br->levels[i] = br->levels[i - level_ac_battery];
652 count += level_ac_battery;
653 } else if (level_ac_battery > 2)
654 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
655
Zhang Ruid80fb992009-03-18 16:27:14 +0800656 /* Check if the _BCL package is in a reversed order */
657 if (max_level == br->levels[2]) {
658 br->flags._BCL_reversed = 1;
659 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
660 acpi_video_cmp_level, NULL);
661 } else if (max_level != br->levels[count - 1])
662 ACPI_ERROR((AE_INFO,
663 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100664
665 br->count = count;
666 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800667
668 /* Check the input/output of _BQC/_BCL/_BCM */
669 if ((max_level < 100) && (max_level <= (count - 2)))
670 br->flags._BCL_use_index = 1;
671
672 /*
673 * _BCM is always consistent with _BCL,
674 * at least for all the laptops we have ever seen.
675 */
676 br->flags._BCM_use_index = br->flags._BCL_use_index;
677
678 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400679 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800680
681 if (!device->cap._BQC)
682 goto set_level;
683
Matthew Garrett70287db2010-02-16 16:53:50 -0500684 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800685 if (result)
686 goto out_free_levels;
687
Zhang Ruie047cca2009-04-09 14:24:35 +0800688 /*
689 * Set the level to maximum and check if _BQC uses indexed value
690 */
691 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800692 if (result)
693 goto out_free_levels;
694
Matthew Garrett70287db2010-02-16 16:53:50 -0500695 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800696 if (result)
697 goto out_free_levels;
698
Zhang Ruie047cca2009-04-09 14:24:35 +0800699 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800700
Zhang Rui90c53ca2009-08-31 12:39:54 -0400701 if (!br->flags._BQC_use_index) {
702 /*
703 * Set the backlight to the initial state.
704 * On some buggy laptops, _BQC returns an uninitialized value
705 * when invoked for the first time, i.e. level_old is invalid.
706 * set the backlight to max_level in this case
707 */
708 for (i = 2; i < br->count; i++)
709 if (level_old == br->levels[i])
710 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800711 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400712 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800713
714 if (br->flags._BCL_reversed)
715 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400716 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800717
718set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400719 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800720 if (result)
721 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800722
Zhang Ruid32f6942009-03-18 16:27:12 +0800723 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
724 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100725 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800726 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100727
728out_free_levels:
729 kfree(br->levels);
730out_free:
731 kfree(br);
732out:
733 device->brightness = NULL;
734 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800735 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100736}
737
738/*
739 * Arg:
740 * device : video output device (LCD, CRT, ..)
741 *
742 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * None
744 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100745 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * device.
747 */
748
Len Brown4be44fc2005-08-05 00:44:28 -0400749static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Patrick Mochel90130262006-05-19 16:54:48 -0400753 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 device->cap._ADR = 1;
755 }
Patrick Mochel90130262006-05-19 16:54:48 -0400756 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400757 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Patrick Mochel90130262006-05-19 16:54:48 -0400759 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400760 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800762 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
763 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800764 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
765 &h_dummy1))) {
766 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
767 device->cap._BCQ = 1;
768 }
769
Patrick Mochel90130262006-05-19 16:54:48 -0400770 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400771 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Zhang Rui1a7c6182009-03-18 16:27:16 +0800774 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500775 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +0800776 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800777 static int count = 0;
778 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800779
780 result = acpi_video_init_brightness(device);
781 if (result)
782 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700783 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +0800784 if (!name)
785 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700786 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +0800787
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500788 memset(&props, 0, sizeof(struct backlight_properties));
789 props.max_brightness = device->brightness->count - 3;
790 device->backlight = backlight_device_register(name, NULL, device,
791 &acpi_backlight_ops,
792 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +0800793 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +0800794 if (IS_ERR(device->backlight))
795 return;
Zhang Rui702ed512008-01-17 15:51:22 +0800796
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200797 /*
798 * Save current brightness level in case we have to restore it
799 * before acpi_video_device_lcd_set_level() is called next time.
800 */
801 device->backlight->props.brightness =
802 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +0800803
Zhang Rui056c3082009-06-22 11:31:14 +0800804 result = sysfs_create_link(&device->backlight->dev.kobj,
805 &device->dev->dev.kobj, "device");
806 if (result)
807 printk(KERN_ERR PREFIX "Create sysfs link\n");
808
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400809 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +0800810 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400811 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700812 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400813 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700814 * free it.
815 * Also, why the hell we are returning early and
816 * not attempt to register video output if cooling
817 * device registration failed?
818 * -- dtor
819 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400820 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500821 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700822 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500823
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200824 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400825 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800826 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400827 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800828 "thermal_cooling");
829 if (result)
830 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400831 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800832 &device->dev->dev.kobj, "device");
833 if (result)
834 printk(KERN_ERR PREFIX "Create sysfs link\n");
835
Yu Luming2f3d0002006-11-11 02:40:34 +0800836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/*
840 * Arg:
841 * device : video output device (VGA)
842 *
843 * Return Value:
844 * None
845 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100846 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
848
Len Brown4be44fc2005-08-05 00:44:28 -0400849static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Len Brown4be44fc2005-08-05 00:44:28 -0400851 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Patrick Mochel90130262006-05-19 16:54:48 -0400853 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 video->cap._DOS = 1;
855 }
Patrick Mochel90130262006-05-19 16:54:48 -0400856 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 video->cap._DOD = 1;
858 }
Patrick Mochel90130262006-05-19 16:54:48 -0400859 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 video->cap._ROM = 1;
861 }
Patrick Mochel90130262006-05-19 16:54:48 -0400862 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 video->cap._GPD = 1;
864 }
Patrick Mochel90130262006-05-19 16:54:48 -0400865 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 video->cap._SPD = 1;
867 }
Patrick Mochel90130262006-05-19 16:54:48 -0400868 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 video->cap._VPO = 1;
870 }
871}
872
873/*
874 * Check whether the video bus device has required AML method to
875 * support the desired features
876 */
877
Len Brown4be44fc2005-08-05 00:44:28 -0400878static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Len Brown4be44fc2005-08-05 00:44:28 -0400880 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000881 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400884 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000886 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200887 if (!dev)
888 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000889 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Since there is no HID, CID and so on for VGA driver, we have
892 * to check well known required nodes.
893 */
894
Julius Volz98fb8fe2007-02-20 16:38:40 +0100895 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200896 if (video->cap._DOS || video->cap._DOD) {
897 if (!video->cap._DOS) {
898 printk(KERN_WARNING FW_BUG
899 "ACPI(%s) defines _DOD but not _DOS\n",
900 acpi_device_bid(video->device));
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 video->flags.multihead = 1;
903 status = 0;
904 }
905
Julius Volz98fb8fe2007-02-20 16:38:40 +0100906 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400907 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 video->flags.rom = 1;
909 status = 0;
910 }
911
Julius Volz98fb8fe2007-02-20 16:38:40 +0100912 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400913 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 video->flags.post = 1;
915 status = 0;
916 }
917
Patrick Mocheld550d982006-06-27 00:41:40 -0400918 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 Driver Interface
923 -------------------------------------------------------------------------- */
924
925/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -0500926static struct acpi_video_device_attrib*
927acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
928{
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500929 struct acpi_video_enumerated_device *ids;
930 int i;
Rui Zhang82cae992007-01-03 23:40:53 -0500931
Dmitry Torokhov78eed022007-11-05 11:43:33 -0500932 for (i = 0; i < video->attached_count; i++) {
933 ids = &video->attached_array[i];
934 if ((ids->value.int_val & 0xffff) == device_id)
935 return &ids->value.attrib;
936 }
937
Rui Zhang82cae992007-01-03 23:40:53 -0500938 return NULL;
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941static int
Matthew Garrette92a7162010-01-12 14:17:03 -0500942acpi_video_get_device_type(struct acpi_video_bus *video,
943 unsigned long device_id)
944{
945 struct acpi_video_enumerated_device *ids;
946 int i;
947
948 for (i = 0; i < video->attached_count; i++) {
949 ids = &video->attached_array[i];
950 if ((ids->value.int_val & 0xffff) == device_id)
951 return ids->value.int_val;
952 }
953
954 return 0;
955}
956
957static int
Len Brown4be44fc2005-08-05 00:44:28 -0400958acpi_video_bus_get_one_device(struct acpi_device *device,
959 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400961 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -0500962 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -0400963 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -0500964 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Len Brown4be44fc2005-08-05 00:44:28 -0400969 status =
970 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (ACPI_SUCCESS(status)) {
972
Burman Yan36bcbec2006-12-19 12:56:11 -0800973 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400975 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
978 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700979 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 data->device_id = device_id;
982 data->video = video;
983 data->dev = device;
984
Rui Zhang82cae992007-01-03 23:40:53 -0500985 attribute = acpi_video_get_device_attr(video, device_id);
986
987 if((attribute != NULL) && attribute->device_id_scheme) {
988 switch (attribute->display_type) {
989 case ACPI_VIDEO_DISPLAY_CRT:
990 data->flags.crt = 1;
991 break;
992 case ACPI_VIDEO_DISPLAY_TV:
993 data->flags.tvout = 1;
994 break;
995 case ACPI_VIDEO_DISPLAY_DVI:
996 data->flags.dvi = 1;
997 break;
998 case ACPI_VIDEO_DISPLAY_LCD:
999 data->flags.lcd = 1;
1000 break;
1001 default:
1002 data->flags.unknown = 1;
1003 break;
1004 }
1005 if(attribute->bios_can_detect)
1006 data->flags.bios = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001007 } else {
1008 /* Check for legacy IDs */
1009 device_type = acpi_video_get_device_type(video,
1010 device_id);
1011 /* Ignore bits 16 and 18-20 */
1012 switch (device_type & 0xffe2ffff) {
1013 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1014 data->flags.crt = 1;
1015 break;
1016 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1017 data->flags.lcd = 1;
1018 break;
1019 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1020 data->flags.tvout = 1;
1021 break;
1022 default:
1023 data->flags.unknown = 1;
1024 }
1025 }
Len Brown4be44fc2005-08-05 00:44:28 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 acpi_video_device_bind(video, data);
1028 acpi_video_device_find_cap(data);
1029
Patrick Mochel90130262006-05-19 16:54:48 -04001030 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001031 ACPI_DEVICE_NOTIFY,
1032 acpi_video_device_notify,
1033 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001035 printk(KERN_ERR PREFIX
1036 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001037 if(data->brightness)
1038 kfree(data->brightness->levels);
1039 kfree(data->brightness);
1040 kfree(data);
1041 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001044 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001046 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Patrick Mocheld550d982006-06-27 00:41:40 -04001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Patrick Mocheld550d982006-06-27 00:41:40 -04001051 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/*
1055 * Arg:
1056 * video : video bus device
1057 *
1058 * Return:
1059 * none
1060 *
1061 * Enumerate the video device list of the video bus,
1062 * bind the ids with the corresponding video devices
1063 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Len Brown4be44fc2005-08-05 00:44:28 -04001066static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001068 struct acpi_video_device *dev;
1069
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001070 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001071
1072 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001073 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001074
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001075 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/*
1079 * Arg:
1080 * video : video bus device
1081 * device : video output device under the video
1082 * bus
1083 *
1084 * Return:
1085 * none
1086 *
1087 * Bind the ids with the corresponding video devices
1088 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091static void
Len Brown4be44fc2005-08-05 00:44:28 -04001092acpi_video_device_bind(struct acpi_video_bus *video,
1093 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001095 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001096 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001098 for (i = 0; i < video->attached_count; i++) {
1099 ids = &video->attached_array[i];
1100 if (device->device_id == (ids->value.int_val & 0xffff)) {
1101 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1103 }
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/*
1108 * Arg:
1109 * video : video bus device
1110 *
1111 * Return:
1112 * < 0 : error
1113 *
1114 * Call _DOD to enumerate all devices attached to display adapter
1115 *
Len Brown4be44fc2005-08-05 00:44:28 -04001116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1119{
Len Brown4be44fc2005-08-05 00:44:28 -04001120 int status;
1121 int count;
1122 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001123 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001124 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1125 union acpi_object *dod = NULL;
1126 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Patrick Mochel90130262006-05-19 16:54:48 -04001128 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001130 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001131 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001134 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001136 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 status = -EFAULT;
1138 goto out;
1139 }
1140
1141 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001142 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001144 active_list = kcalloc(1 + dod->package.count,
1145 sizeof(struct acpi_video_enumerated_device),
1146 GFP_KERNEL);
1147 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 status = -ENOMEM;
1149 goto out;
1150 }
1151
1152 count = 0;
1153 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001154 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001157 printk(KERN_ERR PREFIX
1158 "Invalid _DOD data in element %d\n", i);
1159 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001161
1162 active_list[count].value.int_val = obj->integer.value;
1163 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001164 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1165 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 count++;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Jesper Juhl6044ec82005-11-07 01:01:32 -08001169 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001170
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001171 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001173
1174 out:
Len Brown02438d82006-06-30 03:19:10 -04001175 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Len Brown4be44fc2005-08-05 00:44:28 -04001179static int
1180acpi_video_get_next_level(struct acpi_video_device *device,
1181 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001183 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001184 max = max_below = 0;
1185 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001186 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001187 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001188 l = device->brightness->levels[i];
1189 if (abs(l - level_current) < abs(delta)) {
1190 delta = l - level_current;
1191 if (!delta)
1192 break;
1193 }
1194 }
1195 /* Ajust level_current to closest available level */
1196 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001197 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001198 l = device->brightness->levels[i];
1199 if (l < min)
1200 min = l;
1201 if (l > max)
1202 max = l;
1203 if (l < min_above && l > level_current)
1204 min_above = l;
1205 if (l > max_below && l < level_current)
1206 max_below = l;
1207 }
1208
1209 switch (event) {
1210 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1211 return (level_current < max) ? min_above : min;
1212 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1213 return (level_current < max) ? min_above : max;
1214 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1215 return (level_current > min) ? max_below : min;
1216 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1217 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1218 return 0;
1219 default:
1220 return level_current;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Zhang Ruic8890f92009-03-18 16:27:08 +08001224static int
Len Brown4be44fc2005-08-05 00:44:28 -04001225acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001227 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001228 int result = -EINVAL;
1229
Zhang Rui28c32e92009-07-13 10:33:24 +08001230 /* no warning message if acpi_backlight=vendor is used */
1231 if (!acpi_video_backlight_support())
1232 return 0;
1233
Julia Jomantaite469778c2008-06-23 22:50:42 +01001234 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001235 goto out;
1236
1237 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05001238 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08001239 if (result)
1240 goto out;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001243
Zhang Rui24450c72009-03-18 16:27:10 +08001244 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001245
Matthew Garrett36342742009-07-14 17:06:03 +01001246 if (!result)
1247 backlight_force_update(device->backlight,
1248 BACKLIGHT_UPDATE_HOTKEY);
1249
Zhang Ruic8890f92009-03-18 16:27:08 +08001250out:
1251 if (result)
1252 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1253
1254 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Matthew Garrette92a7162010-01-12 14:17:03 -05001257int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1258 void **edid)
1259{
1260 struct acpi_video_bus *video;
1261 struct acpi_video_device *video_device;
1262 union acpi_object *buffer = NULL;
1263 acpi_status status;
1264 int i, length;
1265
1266 if (!device || !acpi_driver_data(device))
1267 return -EINVAL;
1268
1269 video = acpi_driver_data(device);
1270
1271 for (i = 0; i < video->attached_count; i++) {
1272 video_device = video->attached_array[i].bind_info;
1273 length = 256;
1274
1275 if (!video_device)
1276 continue;
1277
Zhang Rui82069552010-12-06 15:04:24 +08001278 if (!video_device->cap._DDC)
1279 continue;
1280
Matthew Garrette92a7162010-01-12 14:17:03 -05001281 if (type) {
1282 switch (type) {
1283 case ACPI_VIDEO_DISPLAY_CRT:
1284 if (!video_device->flags.crt)
1285 continue;
1286 break;
1287 case ACPI_VIDEO_DISPLAY_TV:
1288 if (!video_device->flags.tvout)
1289 continue;
1290 break;
1291 case ACPI_VIDEO_DISPLAY_DVI:
1292 if (!video_device->flags.dvi)
1293 continue;
1294 break;
1295 case ACPI_VIDEO_DISPLAY_LCD:
1296 if (!video_device->flags.lcd)
1297 continue;
1298 break;
1299 }
1300 } else if (video_device->device_id != device_id) {
1301 continue;
1302 }
1303
1304 status = acpi_video_device_EDID(video_device, &buffer, length);
1305
1306 if (ACPI_FAILURE(status) || !buffer ||
1307 buffer->type != ACPI_TYPE_BUFFER) {
1308 length = 128;
1309 status = acpi_video_device_EDID(video_device, &buffer,
1310 length);
1311 if (ACPI_FAILURE(status) || !buffer ||
1312 buffer->type != ACPI_TYPE_BUFFER) {
1313 continue;
1314 }
1315 }
1316
1317 *edid = buffer->buffer.pointer;
1318 return length;
1319 }
1320
1321 return -ENODEV;
1322}
1323EXPORT_SYMBOL(acpi_video_get_edid);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325static int
Len Brown4be44fc2005-08-05 00:44:28 -04001326acpi_video_bus_get_devices(struct acpi_video_bus *video,
1327 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Len Brown4be44fc2005-08-05 00:44:28 -04001329 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001330 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 acpi_video_device_enumerate(video);
1333
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001334 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 status = acpi_video_bus_get_one_device(dev, video);
1337 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001338 printk(KERN_WARNING PREFIX
Andi Kleencfa806f2010-07-20 15:18:36 -07001339 "Cant attach device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 continue;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001343 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Len Brown4be44fc2005-08-05 00:44:28 -04001346static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Karol Kozimor031ec772005-07-30 04:18:00 -04001348 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001351 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Patrick Mochel90130262006-05-19 16:54:48 -04001353 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001354 ACPI_DEVICE_NOTIFY,
1355 acpi_video_device_notify);
Andi Kleencfa806f2010-07-20 15:18:36 -07001356 if (ACPI_FAILURE(status)) {
1357 printk(KERN_WARNING PREFIX
1358 "Cant remove video notify handler\n");
1359 }
Keith Packarde29b3ee2009-08-06 15:57:54 -07001360 if (device->backlight) {
1361 sysfs_remove_link(&device->backlight->dev.kobj, "device");
1362 backlight_device_unregister(device->backlight);
1363 device->backlight = NULL;
1364 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001365 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08001366 sysfs_remove_link(&device->dev->dev.kobj,
1367 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001368 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08001369 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001370 thermal_cooling_device_unregister(device->cooling_dev);
1371 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08001372 }
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001373
Patrick Mocheld550d982006-06-27 00:41:40 -04001374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Len Brown4be44fc2005-08-05 00:44:28 -04001377static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Len Brown4be44fc2005-08-05 00:44:28 -04001379 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001380 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001382 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001384 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001386 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001387 if (ACPI_FAILURE(status))
1388 printk(KERN_WARNING PREFIX
1389 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001391 if (dev->brightness) {
1392 kfree(dev->brightness->levels);
1393 kfree(dev->brightness);
1394 }
1395 list_del(&dev->entry);
1396 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001399 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001400
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404/* acpi_video interface */
1405
Len Brown4be44fc2005-08-05 00:44:28 -04001406static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Zhang Ruia21101c2007-09-14 11:46:22 +08001408 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Len Brown4be44fc2005-08-05 00:44:28 -04001411static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 return acpi_video_bus_DOS(video, 0, 1);
1414}
1415
Bjorn Helgaas70155582009-04-07 15:37:11 +00001416static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001418 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001419 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001420 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001423 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Luming Yue9dab192007-08-20 18:23:53 +08001425 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001428 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * most likely via hotkey. */
Len Brown14e04fb32007-08-23 15:20:26 -04001430 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001431 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433
Julius Volz98fb8fe2007-02-20 16:38:40 +01001434 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * connector. */
1436 acpi_video_device_enumerate(video);
1437 acpi_video_device_rebind(video);
Len Brown14e04fb32007-08-23 15:20:26 -04001438 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001439 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break;
1441
Len Brown4be44fc2005-08-05 00:44:28 -04001442 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001443 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001444 keycode = KEY_SWITCHVIDEOMODE;
1445 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001446 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001447 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001448 keycode = KEY_VIDEO_NEXT;
1449 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001450 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb32007-08-23 15:20:26 -04001451 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001452 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454
1455 default:
1456 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001457 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
1459 }
1460
Zhang Rui7761f632008-01-25 14:48:12 +08001461 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001462
1463 if (keycode) {
1464 input_report_key(input, keycode, 1);
1465 input_sync(input);
1466 input_report_key(input, keycode, 0);
1467 input_sync(input);
1468 }
Luming Yue9dab192007-08-20 18:23:53 +08001469
Patrick Mocheld550d982006-06-27 00:41:40 -04001470 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Len Brown4be44fc2005-08-05 00:44:28 -04001473static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001475 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001476 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001477 struct acpi_video_bus *bus;
1478 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001479 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001484 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001485 bus = video_device->video;
1486 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001489 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001490 if (brightness_switch_enabled)
1491 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001492 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001493 keycode = KEY_BRIGHTNESS_CYCLE;
1494 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001495 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001496 if (brightness_switch_enabled)
1497 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001498 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001499 keycode = KEY_BRIGHTNESSUP;
1500 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001501 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001502 if (brightness_switch_enabled)
1503 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001504 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001505 keycode = KEY_BRIGHTNESSDOWN;
1506 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001507 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001508 if (brightness_switch_enabled)
1509 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001510 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001511 keycode = KEY_BRIGHTNESS_ZERO;
1512 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001513 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001514 if (brightness_switch_enabled)
1515 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb32007-08-23 15:20:26 -04001516 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001517 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 break;
1519 default:
1520 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001521 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 break;
1523 }
Luming Yue9dab192007-08-20 18:23:53 +08001524
Zhang Rui7761f632008-01-25 14:48:12 +08001525 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001526
1527 if (keycode) {
1528 input_report_key(input, keycode, 1);
1529 input_sync(input);
1530 input_report_key(input, keycode, 0);
1531 input_sync(input);
1532 }
Luming Yue9dab192007-08-20 18:23:53 +08001533
Patrick Mocheld550d982006-06-27 00:41:40 -04001534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001537static int acpi_video_resume(struct notifier_block *nb,
1538 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001539{
1540 struct acpi_video_bus *video;
1541 struct acpi_video_device *video_device;
1542 int i;
1543
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001544 switch (val) {
1545 case PM_HIBERNATION_PREPARE:
1546 case PM_SUSPEND_PREPARE:
1547 case PM_RESTORE_PREPARE:
1548 return NOTIFY_DONE;
1549 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001550
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001551 video = container_of(nb, struct acpi_video_bus, pm_nb);
1552
1553 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001554
1555 for (i = 0; i < video->attached_count; i++) {
1556 video_device = video->attached_array[i].bind_info;
1557 if (video_device && video_device->backlight)
1558 acpi_video_set_brightness(video_device->backlight);
1559 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001560
1561 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001562}
1563
Zhang Ruic504f8c2009-12-30 15:59:23 +08001564static acpi_status
1565acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1566 void **return_value)
1567{
1568 struct acpi_device *device = context;
1569 struct acpi_device *sibling;
1570 int result;
1571
1572 if (handle == device->handle)
1573 return AE_CTRL_TERMINATE;
1574
1575 result = acpi_bus_get_device(handle, &sibling);
1576 if (result)
1577 return AE_OK;
1578
1579 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1580 return AE_ALREADY_EXISTS;
1581
1582 return AE_OK;
1583}
1584
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001585static int instance;
1586
Len Brown4be44fc2005-08-05 00:44:28 -04001587static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001589 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001590 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001591 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001592 acpi_status status;
1593
1594 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1595 device->parent->handle, 1,
1596 acpi_video_bus_match, NULL,
1597 device, NULL);
1598 if (status == AE_ALREADY_EXISTS) {
1599 printk(KERN_WARNING FW_BUG
1600 "Duplicate ACPI video bus devices for the"
1601 " same VGA controller, please try module "
1602 "parameter \"video.allow_duplicates=1\""
1603 "if the current driver doesn't work.\n");
1604 if (!allow_duplicates)
1605 return -ENODEV;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Burman Yan36bcbec2006-12-19 12:56:11 -08001608 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001610 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Zhang Ruie6d9da12007-08-25 02:23:31 -04001612 /* a hack to fix the duplicate name "VID" problem on T61 */
1613 if (!strcmp(device->pnp.bus_id, "VID")) {
1614 if (instance)
1615 device->pnp.bus_id[3] = '0' + instance;
1616 instance ++;
1617 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001618 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1619 if (!strcmp(device->pnp.bus_id, "VGA")) {
1620 if (instance)
1621 device->pnp.bus_id[3] = '0' + instance;
1622 instance++;
1623 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001624
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001625 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1627 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001628 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001631 error = acpi_video_bus_check(video);
1632 if (error)
1633 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001635 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 INIT_LIST_HEAD(&video->video_device_list);
1637
1638 acpi_video_bus_get_devices(video, device);
1639 acpi_video_bus_start_devices(video);
1640
Luming Yue9dab192007-08-20 18:23:53 +08001641 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001642 if (!input) {
1643 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00001644 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001645 }
Luming Yue9dab192007-08-20 18:23:53 +08001646
1647 snprintf(video->phys, sizeof(video->phys),
1648 "%s/video/input0", acpi_device_hid(video->device));
1649
1650 input->name = acpi_device_name(video->device);
1651 input->phys = video->phys;
1652 input->id.bustype = BUS_HOST;
1653 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05001654 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001655 input->evbit[0] = BIT(EV_KEY);
1656 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1657 set_bit(KEY_VIDEO_NEXT, input->keybit);
1658 set_bit(KEY_VIDEO_PREV, input->keybit);
1659 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1660 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1661 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1662 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1663 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08001664
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001665 error = input_register_device(input);
1666 if (error)
1667 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001670 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1671 video->flags.multihead ? "yes" : "no",
1672 video->flags.rom ? "yes" : "no",
1673 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001675 video->pm_nb.notifier_call = acpi_video_resume;
1676 video->pm_nb.priority = 0;
1677 register_pm_notifier(&video->pm_nb);
1678
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001681 err_free_input_dev:
1682 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001683 err_stop_video:
1684 acpi_video_bus_stop_devices(video);
1685 acpi_video_bus_put_devices(video);
1686 kfree(video->attached_array);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001687 err_free_video:
1688 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001689 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001690
1691 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Len Brown4be44fc2005-08-05 00:44:28 -04001694static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Len Brown4be44fc2005-08-05 00:44:28 -04001696 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001700 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001702 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001704 unregister_pm_notifier(&video->pm_nb);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Luming Yue9dab192007-08-20 18:23:53 +08001709 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001710 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 kfree(video);
1712
Patrick Mocheld550d982006-06-27 00:41:40 -04001713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Matthew Garrett74a365b2009-03-19 21:35:39 +00001716static int __init intel_opregion_present(void)
1717{
1718#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1719 struct pci_dev *dev = NULL;
1720 u32 address;
1721
1722 for_each_pci_dev(dev) {
1723 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1724 continue;
1725 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1726 continue;
1727 pci_read_config_dword(dev, 0xfc, &address);
1728 if (!address)
1729 continue;
1730 return 1;
1731 }
1732#endif
1733 return 0;
1734}
1735
1736int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Len Brown4be44fc2005-08-05 00:44:28 -04001738 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001739 if (register_count) {
1740 /*
1741 * if the function of acpi_video_register is already called,
1742 * don't register the acpi_vide_bus again and return no error.
1743 */
1744 return 0;
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08001748 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04001749 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Zhao Yakui86e437f2009-06-16 11:23:13 +08001751 /*
1752 * When the acpi_video_bus is loaded successfully, increase
1753 * the counter reference.
1754 */
1755 register_count = 1;
1756
Patrick Mocheld550d982006-06-27 00:41:40 -04001757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
Matthew Garrett74a365b2009-03-19 21:35:39 +00001759EXPORT_SYMBOL(acpi_video_register);
1760
Zhao Yakui86e437f2009-06-16 11:23:13 +08001761void acpi_video_unregister(void)
1762{
1763 if (!register_count) {
1764 /*
1765 * If the acpi video bus is already unloaded, don't
1766 * unload it again and return directly.
1767 */
1768 return;
1769 }
1770 acpi_bus_unregister_driver(&acpi_video_bus);
1771
Zhao Yakui86e437f2009-06-16 11:23:13 +08001772 register_count = 0;
1773
1774 return;
1775}
1776EXPORT_SYMBOL(acpi_video_unregister);
1777
Matthew Garrett74a365b2009-03-19 21:35:39 +00001778/*
1779 * This is kind of nasty. Hardware using Intel chipsets may require
1780 * the video opregion code to be run first in order to initialise
1781 * state before any ACPI video calls are made. To handle this we defer
1782 * registration of the video class until the opregion code has run.
1783 */
1784
1785static int __init acpi_video_init(void)
1786{
Zhang Rui45cb50e2009-04-24 12:13:18 -04001787 dmi_check_system(video_dmi_table);
1788
Matthew Garrett74a365b2009-03-19 21:35:39 +00001789 if (intel_opregion_present())
1790 return 0;
1791
1792 return acpi_video_register();
1793}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Zhao Yakui86e437f2009-06-16 11:23:13 +08001795static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Zhao Yakui86e437f2009-06-16 11:23:13 +08001797 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Patrick Mocheld550d982006-06-27 00:41:40 -04001799 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802module_init(acpi_video_init);
1803module_exit(acpi_video_exit);