blob: 15a0fde4b32ab9ff6aa2a690a74dda73e7f1bd5b [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>
Luming Yu23b0f012007-05-09 21:07:05 +080036#include <linux/video_output.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050037#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000038#include <linux/pci.h>
39#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040042#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020045#include <linux/suspend.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050046#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Len Browna192a952009-07-28 16:45:54 -040048#define PREFIX "ACPI: "
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_VIDEO_BUS_NAME "Video Bus"
52#define ACPI_VIDEO_DEVICE_NAME "Video Device"
53#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
54#define ACPI_VIDEO_NOTIFY_PROBE 0x81
55#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
56#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
57#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
58
Thomas Tuttlef4715182006-12-19 12:56:14 -080059#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
60#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
61#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
62#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
63#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Yu Luming2f3d0002006-11-11 02:40:34 +080065#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050068ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Len Brownf52fd662007-02-12 22:42:12 -050070MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050071MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_LICENSE("GPL");
73
Zhang Rui8a681a42008-01-25 14:47:49 +080074static int brightness_switch_enabled = 1;
75module_param(brightness_switch_enabled, bool, 0644);
76
Zhang Ruic504f8c2009-12-30 15:59:23 +080077/*
78 * By default, we don't allow duplicate ACPI video bus devices
79 * under the same VGA controller
80 */
81static int allow_duplicates;
82module_param(allow_duplicates, bool, 0644);
83
Zhao Yakui86e437f2009-06-16 11:23:13 +080084static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040085static int acpi_video_bus_add(struct acpi_device *device);
86static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000087static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
92};
93MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050096 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020098 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000102 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400103 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400107 u8 multihead:1; /* can switch video heads */
108 u8 rom:1; /* can retrieve a video rom */
109 u8 post:1; /* can configure the head to */
110 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 u8 _DOS:1; /*Enable/Disable output switching */
115 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
116 u8 _ROM:1; /*Get ROM Data */
117 u8 _GPD:1; /*Get POST Device */
118 u8 _SPD:1; /*Set POST Device */
119 u8 _VPO:1; /*Video POST Options */
120 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100125 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400126 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100127 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400128 u32 bios_can_detect:1; /*BIOS can detect the device */
129 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
130 the VGA device. */
131 u32 pipe_id:3; /*For VGA multiple-head devices. */
132 u32 reserved:10; /*Must be 0 */
133 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 } value;
141 struct acpi_video_device *bind_info;
142};
143
144struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400145 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400148 u8 attached_count;
149 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500152 struct mutex device_list_lock; /* protects video_device_list */
Luming Yue9dab192007-08-20 18:23:53 +0800153 struct input_dev *input;
154 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200155 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 crt:1;
160 u8 lcd:1;
161 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500162 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400163 u8 bios:1;
164 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500165 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400169 u8 _ADR:1; /*Return the unique ID */
170 u8 _BCL:1; /*Query list of brightness control levels supported */
171 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800172 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800173 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400174 u8 _DDC:1; /*Return the EDID for this device */
175 u8 _DCS:1; /*Return status of output device */
176 u8 _DGS:1; /*Query graphics state */
177 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Zhang Ruid32f6942009-03-18 16:27:12 +0800180struct acpi_video_brightness_flags {
181 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800182 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800183 u8 _BCL_use_index:1; /* levels in _BCL are index values */
184 u8 _BCM_use_index:1; /* input of _BCM is an index value */
185 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400189 int curr;
190 int count;
191 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800192 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
195struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 unsigned long device_id;
197 struct acpi_video_device_flags flags;
198 struct acpi_video_device_cap cap;
199 struct list_head entry;
200 struct acpi_video_bus *video;
201 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800203 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400204 struct thermal_cooling_device *cooling_dev;
Luming Yu23b0f012007-05-09 21:07:05 +0800205 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100208static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 "motherboard VGA device",
210 "PCI VGA device",
211 "AGP VGA device",
212 "UNKNOWN",
213};
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216static void acpi_video_device_rebind(struct acpi_video_bus *video);
217static void acpi_video_device_bind(struct acpi_video_bus *video,
218 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800220static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221 int level);
222static int acpi_video_device_lcd_get_level_current(
223 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500224 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400225static int acpi_video_get_next_level(struct acpi_video_device *device,
226 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800227static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800229static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400230 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800231static int acpi_video_output_get(struct output_device *od);
232static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Yu Luming2f3d0002006-11-11 02:40:34 +0800234/*backlight device sysfs support*/
235static int acpi_video_get_brightness(struct backlight_device *bd)
236{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400237 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000238 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800239 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100240 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800241
Matthew Garrett70287db2010-02-16 16:53:50 -0500242 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800243 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000244 for (i = 2; i < vd->brightness->count; i++) {
245 if (vd->brightness->levels[i] == cur_level)
246 /* The first two entries are special - see page 575
247 of the ACPI spec 3.0 */
248 return i-2;
249 }
250 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800251}
252
253static int acpi_video_set_brightness(struct backlight_device *bd)
254{
Zhang Rui24450c72009-03-18 16:27:10 +0800255 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800256 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100257 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800258
259 return acpi_video_device_lcd_set_level(vd,
260 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800261}
262
Lionel Debrouxacc24722010-11-16 14:14:02 +0100263static const struct backlight_ops acpi_backlight_ops = {
Richard Purdie599a52d2007-02-10 23:07:48 +0000264 .get_brightness = acpi_video_get_brightness,
265 .update_status = acpi_video_set_brightness,
266};
267
Luming Yu23b0f012007-05-09 21:07:05 +0800268/*video output device sysfs support*/
269static int acpi_video_output_get(struct output_device *od)
270{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400271 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800272 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700273 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800274 acpi_video_device_get_state(vd, &state);
275 return (int)state;
276}
277
278static int acpi_video_output_set(struct output_device *od)
279{
280 unsigned long state = od->request_state;
281 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700282 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800283 return acpi_video_device_set_state(vd, state);
284}
285
286static struct output_properties acpi_output_properties = {
287 .set_state = acpi_video_output_set,
288 .get_status = acpi_video_output_get,
289};
Zhang Rui702ed512008-01-17 15:51:22 +0800290
291
292/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400293static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000294 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800295{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400296 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800297 struct acpi_video_device *video = acpi_driver_data(device);
298
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000299 *state = video->brightness->count - 3;
300 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800301}
302
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400303static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000304 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800305{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400306 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800307 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400308 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000309 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800310
Matthew Garrett70287db2010-02-16 16:53:50 -0500311 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800312 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000313 for (offset = 2; offset < video->brightness->count; offset++)
314 if (level == video->brightness->levels[offset]) {
315 *state = video->brightness->count - offset - 1;
316 return 0;
317 }
Zhang Rui702ed512008-01-17 15:51:22 +0800318
319 return -EINVAL;
320}
321
322static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400323video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800324{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400325 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800326 struct acpi_video_device *video = acpi_driver_data(device);
327 int level;
328
329 if ( state >= video->brightness->count - 2)
330 return -EINVAL;
331
332 state = video->brightness->count - state;
333 level = video->brightness->levels[state -1];
334 return acpi_video_device_lcd_set_level(video, level);
335}
336
337static struct thermal_cooling_device_ops video_cooling_ops = {
338 .get_max_state = video_get_max_state,
339 .get_cur_state = video_get_cur_state,
340 .set_cur_state = video_set_cur_state,
341};
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/* --------------------------------------------------------------------------
344 Video Management
345 -------------------------------------------------------------------------- */
346
347/* device */
348
349static int
Len Brown4be44fc2005-08-05 00:44:28 -0400350acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400351 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Len Brown4be44fc2005-08-05 00:44:28 -0400353 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Patrick Mochel90130262006-05-19 16:54:48 -0400355 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static int
Len Brown4be44fc2005-08-05 00:44:28 -0400361acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Len Brown4be44fc2005-08-05 00:44:28 -0400363 int status;
364 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
365 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400366 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400370 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static int
Len Brown4be44fc2005-08-05 00:44:28 -0400376acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
377 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Len Brown4be44fc2005-08-05 00:44:28 -0400379 int status;
380 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
381 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 *levels = NULL;
385
Patrick Mochel90130262006-05-19 16:54:48 -0400386 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400388 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400389 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500390 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400391 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 status = -EFAULT;
393 goto err;
394 }
395
396 *levels = obj;
397
Patrick Mocheld550d982006-06-27 00:41:40 -0400398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800401 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Patrick Mocheld550d982006-06-27 00:41:40 -0400403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static int
Len Brown4be44fc2005-08-05 00:44:28 -0400407acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Zhang Rui24450c72009-03-18 16:27:10 +0800409 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400410 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
411 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800412 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Zhang Rui24450c72009-03-18 16:27:10 +0800416 status = acpi_evaluate_object(device->dev->handle, "_BCM",
417 &args, NULL);
418 if (ACPI_FAILURE(status)) {
419 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
420 return -EIO;
421 }
422
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400423 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800424 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800425 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800426 if (device->backlight)
427 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800428 return 0;
429 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800430
Zhang Rui24450c72009-03-18 16:27:10 +0800431 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
432 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Zhang Rui45cb50e2009-04-24 12:13:18 -0400435/*
436 * For some buggy _BQC methods, we need to add a constant value to
437 * the _BQC return value to get the actual current brightness level
438 */
439
440static int bqc_offset_aml_bug_workaround;
441static int __init video_set_bqc_offset(const struct dmi_system_id *d)
442{
443 bqc_offset_aml_bug_workaround = 9;
444 return 0;
445}
446
447static struct dmi_system_id video_dmi_table[] __initdata = {
448 /*
449 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
450 */
451 {
452 .callback = video_set_bqc_offset,
453 .ident = "Acer Aspire 5720",
454 .matches = {
455 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
457 },
458 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400459 {
460 .callback = video_set_bqc_offset,
461 .ident = "Acer Aspire 5710Z",
462 .matches = {
463 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
464 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
465 },
466 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400467 {
468 .callback = video_set_bqc_offset,
469 .ident = "eMachines E510",
470 .matches = {
471 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
472 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
473 },
474 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400475 {
476 .callback = video_set_bqc_offset,
477 .ident = "Acer Aspire 5315",
478 .matches = {
479 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
480 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
481 },
482 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800483 {
484 .callback = video_set_bqc_offset,
485 .ident = "Acer Aspire 7720",
486 .matches = {
487 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
488 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
489 },
490 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400491 {}
492};
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static int
Len Brown4be44fc2005-08-05 00:44:28 -0400495acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500496 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Zhang Ruic8890f92009-03-18 16:27:08 +0800498 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800499 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800500
Zhang Ruic60d6382009-03-18 16:27:18 +0800501 if (device->cap._BQC || device->cap._BCQ) {
502 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
503
504 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800505 NULL, level);
506 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800507 if (device->brightness->flags._BQC_use_index) {
508 if (device->brightness->flags._BCL_reversed)
509 *level = device->brightness->count
510 - 3 - (*level);
511 *level = device->brightness->levels[*level + 2];
512
513 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400514 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800515 for (i = 2; i < device->brightness->count; i++)
516 if (device->brightness->levels[i] == *level) {
517 device->brightness->curr = *level;
518 return 0;
519 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500520 if (!init) {
521 /*
522 * BQC returned an invalid level.
523 * Stop using it.
524 */
525 ACPI_WARNING((AE_INFO,
526 "%s returned an invalid level",
527 buf));
528 device->cap._BQC = device->cap._BCQ = 0;
529 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800530 } else {
531 /* Fixme:
532 * should we return an error or ignore this failure?
533 * dev->brightness->curr is a cached value which stores
534 * the correct current backlight level in most cases.
535 * ACPI video backlight still works w/ buggy _BQC.
536 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
537 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800538 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
539 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800540 }
541 }
542
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400543 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static int
Len Brown4be44fc2005-08-05 00:44:28 -0400548acpi_video_device_EDID(struct acpi_video_device *device,
549 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Len Brown4be44fc2005-08-05 00:44:28 -0400551 int status;
552 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
553 union acpi_object *obj;
554 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
555 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 *edid = NULL;
559
560 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400561 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (length == 128)
563 arg0.integer.value = 1;
564 else if (length == 256)
565 arg0.integer.value = 2;
566 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Patrick Mochel90130262006-05-19 16:54:48 -0400569 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400571 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200573 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (obj && obj->type == ACPI_TYPE_BUFFER)
576 *edid = obj;
577 else {
Len Brown64684632006-06-26 23:41:38 -0400578 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 status = -EFAULT;
580 kfree(obj);
581 }
582
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/* bus */
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
589 * Arg:
590 * video : video bus device pointer
591 * bios_flag :
592 * 0. The system BIOS should NOT automatically switch(toggle)
593 * the active display output.
594 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100595 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * 2. The _DGS value should be locked.
597 * 3. The system BIOS should not automatically switch (toggle) the
598 * active display output, but instead generate the display switch
599 * event notify code.
600 * lcd_flag :
601 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100602 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100604 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Return Value:
606 * -1 wrong arg.
607 */
608
609static int
Len Brown4be44fc2005-08-05 00:44:28 -0400610acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Lin Ming439913f2010-01-28 10:53:19 +0800612 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400613 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
614 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Len Brown4be44fc2005-08-05 00:44:28 -0400617 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 status = -1;
619 goto Failed;
620 }
621 arg0.integer.value = (lcd_flag << 2) | bios_flag;
622 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400623 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Len Brown4be44fc2005-08-05 00:44:28 -0400625 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500630 * Simple comparison function used to sort backlight levels.
631 */
632
633static int
634acpi_video_cmp_level(const void *a, const void *b)
635{
636 return *(int *)a - *(int *)b;
637}
638
639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Arg:
641 * device : video output device (LCD, CRT, ..)
642 *
643 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100644 * Maximum brightness level
645 *
646 * Allocate and initialize device->brightness.
647 */
648
649static int
650acpi_video_init_brightness(struct acpi_video_device *device)
651{
652 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800653 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800654 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100655 union acpi_object *o;
656 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800657 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100658
659 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
660 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
661 "LCD brightness level\n"));
662 goto out;
663 }
664
665 if (obj->package.count < 2)
666 goto out;
667
668 br = kzalloc(sizeof(*br), GFP_KERNEL);
669 if (!br) {
670 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800671 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100672 goto out;
673 }
674
Zhang Ruid32f6942009-03-18 16:27:12 +0800675 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100676 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800677 if (!br->levels) {
678 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100679 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800680 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100681
682 for (i = 0; i < obj->package.count; i++) {
683 o = (union acpi_object *)&obj->package.elements[i];
684 if (o->type != ACPI_TYPE_INTEGER) {
685 printk(KERN_ERR PREFIX "Invalid data\n");
686 continue;
687 }
688 br->levels[count] = (u32) o->integer.value;
689
690 if (br->levels[count] > max_level)
691 max_level = br->levels[count];
692 count++;
693 }
694
Zhang Ruid32f6942009-03-18 16:27:12 +0800695 /*
696 * some buggy BIOS don't export the levels
697 * when machine is on AC/Battery in _BCL package.
698 * In this case, the first two elements in _BCL packages
699 * are also supported brightness levels that OS should take care of.
700 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800701 for (i = 2; i < count; i++) {
702 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800703 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800704 if (br->levels[i] == br->levels[1])
705 level_ac_battery++;
706 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500707
Zhang Ruid32f6942009-03-18 16:27:12 +0800708 if (level_ac_battery < 2) {
709 level_ac_battery = 2 - level_ac_battery;
710 br->flags._BCL_no_ac_battery_levels = 1;
711 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
712 br->levels[i] = br->levels[i - level_ac_battery];
713 count += level_ac_battery;
714 } else if (level_ac_battery > 2)
715 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
716
Zhang Ruid80fb992009-03-18 16:27:14 +0800717 /* Check if the _BCL package is in a reversed order */
718 if (max_level == br->levels[2]) {
719 br->flags._BCL_reversed = 1;
720 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
721 acpi_video_cmp_level, NULL);
722 } else if (max_level != br->levels[count - 1])
723 ACPI_ERROR((AE_INFO,
724 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100725
726 br->count = count;
727 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800728
729 /* Check the input/output of _BQC/_BCL/_BCM */
730 if ((max_level < 100) && (max_level <= (count - 2)))
731 br->flags._BCL_use_index = 1;
732
733 /*
734 * _BCM is always consistent with _BCL,
735 * at least for all the laptops we have ever seen.
736 */
737 br->flags._BCM_use_index = br->flags._BCL_use_index;
738
739 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400740 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800741
742 if (!device->cap._BQC)
743 goto set_level;
744
Matthew Garrett70287db2010-02-16 16:53:50 -0500745 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800746 if (result)
747 goto out_free_levels;
748
Zhang Ruie047cca2009-04-09 14:24:35 +0800749 /*
750 * Set the level to maximum and check if _BQC uses indexed value
751 */
752 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800753 if (result)
754 goto out_free_levels;
755
Matthew Garrett70287db2010-02-16 16:53:50 -0500756 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800757 if (result)
758 goto out_free_levels;
759
Zhang Ruie047cca2009-04-09 14:24:35 +0800760 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800761
Zhang Rui90c53ca2009-08-31 12:39:54 -0400762 if (!br->flags._BQC_use_index) {
763 /*
764 * Set the backlight to the initial state.
765 * On some buggy laptops, _BQC returns an uninitialized value
766 * when invoked for the first time, i.e. level_old is invalid.
767 * set the backlight to max_level in this case
768 */
769 for (i = 2; i < br->count; i++)
770 if (level_old == br->levels[i])
771 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800772 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400773 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800774
775 if (br->flags._BCL_reversed)
776 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400777 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800778
779set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400780 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800781 if (result)
782 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800783
Zhang Ruid32f6942009-03-18 16:27:12 +0800784 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
785 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100786 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800787 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100788
789out_free_levels:
790 kfree(br->levels);
791out_free:
792 kfree(br);
793out:
794 device->brightness = NULL;
795 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800796 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100797}
798
799/*
800 * Arg:
801 * device : video output device (LCD, CRT, ..)
802 *
803 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * None
805 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100806 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * device.
808 */
809
Len Brown4be44fc2005-08-05 00:44:28 -0400810static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Patrick Mochel90130262006-05-19 16:54:48 -0400814 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 device->cap._ADR = 1;
816 }
Patrick Mochel90130262006-05-19 16:54:48 -0400817 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400818 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Patrick Mochel90130262006-05-19 16:54:48 -0400820 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400821 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800823 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
824 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800825 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
826 &h_dummy1))) {
827 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
828 device->cap._BCQ = 1;
829 }
830
Patrick Mochel90130262006-05-19 16:54:48 -0400831 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400832 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Patrick Mochel90130262006-05-19 16:54:48 -0400834 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 device->cap._DCS = 1;
836 }
Patrick Mochel90130262006-05-19 16:54:48 -0400837 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 device->cap._DGS = 1;
839 }
Patrick Mochel90130262006-05-19 16:54:48 -0400840 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 device->cap._DSS = 1;
842 }
843
Zhang Rui1a7c6182009-03-18 16:27:16 +0800844 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500845 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +0800846 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800847 static int count = 0;
848 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800849
850 result = acpi_video_init_brightness(device);
851 if (result)
852 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700853 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +0800854 if (!name)
855 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700856 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +0800857
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500858 memset(&props, 0, sizeof(struct backlight_properties));
859 props.max_brightness = device->brightness->count - 3;
860 device->backlight = backlight_device_register(name, NULL, device,
861 &acpi_backlight_ops,
862 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +0800863 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +0800864 if (IS_ERR(device->backlight))
865 return;
Zhang Rui702ed512008-01-17 15:51:22 +0800866
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200867 /*
868 * Save current brightness level in case we have to restore it
869 * before acpi_video_device_lcd_set_level() is called next time.
870 */
871 device->backlight->props.brightness =
872 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +0800873
Zhang Rui056c3082009-06-22 11:31:14 +0800874 result = sysfs_create_link(&device->backlight->dev.kobj,
875 &device->dev->dev.kobj, "device");
876 if (result)
877 printk(KERN_ERR PREFIX "Create sysfs link\n");
878
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400879 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +0800880 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400881 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700882 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400883 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700884 * free it.
885 * Also, why the hell we are returning early and
886 * not attempt to register video output if cooling
887 * device registration failed?
888 * -- dtor
889 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400890 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500891 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700892 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500893
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200894 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400895 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800896 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400897 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800898 "thermal_cooling");
899 if (result)
900 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400901 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800902 &device->dev->dev.kobj, "device");
903 if (result)
904 printk(KERN_ERR PREFIX "Create sysfs link\n");
905
Yu Luming2f3d0002006-11-11 02:40:34 +0800906 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200907
908 if (acpi_video_display_switch_support()) {
909
910 if (device->cap._DCS && device->cap._DSS) {
911 static int count;
912 char *name;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700913 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200914 if (!name)
915 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700916 count++;
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200917 device->output_dev = video_output_register(name,
918 NULL, device, &acpi_output_properties);
919 kfree(name);
920 }
Luming Yu23b0f012007-05-09 21:07:05 +0800921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/*
925 * Arg:
926 * device : video output device (VGA)
927 *
928 * Return Value:
929 * None
930 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100931 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933
Len Brown4be44fc2005-08-05 00:44:28 -0400934static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Len Brown4be44fc2005-08-05 00:44:28 -0400936 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Patrick Mochel90130262006-05-19 16:54:48 -0400938 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 video->cap._DOS = 1;
940 }
Patrick Mochel90130262006-05-19 16:54:48 -0400941 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 video->cap._DOD = 1;
943 }
Patrick Mochel90130262006-05-19 16:54:48 -0400944 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 video->cap._ROM = 1;
946 }
Patrick Mochel90130262006-05-19 16:54:48 -0400947 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 video->cap._GPD = 1;
949 }
Patrick Mochel90130262006-05-19 16:54:48 -0400950 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 video->cap._SPD = 1;
952 }
Patrick Mochel90130262006-05-19 16:54:48 -0400953 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 video->cap._VPO = 1;
955 }
956}
957
958/*
959 * Check whether the video bus device has required AML method to
960 * support the desired features
961 */
962
Len Brown4be44fc2005-08-05 00:44:28 -0400963static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Len Brown4be44fc2005-08-05 00:44:28 -0400965 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000966 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000971 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200972 if (!dev)
973 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000974 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Since there is no HID, CID and so on for VGA driver, we have
977 * to check well known required nodes.
978 */
979
Julius Volz98fb8fe2007-02-20 16:38:40 +0100980 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200981 if (video->cap._DOS || video->cap._DOD) {
982 if (!video->cap._DOS) {
983 printk(KERN_WARNING FW_BUG
984 "ACPI(%s) defines _DOD but not _DOS\n",
985 acpi_device_bid(video->device));
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 video->flags.multihead = 1;
988 status = 0;
989 }
990
Julius Volz98fb8fe2007-02-20 16:38:40 +0100991 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400992 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 video->flags.rom = 1;
994 status = 0;
995 }
996
Julius Volz98fb8fe2007-02-20 16:38:40 +0100997 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400998 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 video->flags.post = 1;
1000 status = 0;
1001 }
1002
Patrick Mocheld550d982006-06-27 00:41:40 -04001003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 Driver Interface
1008 -------------------------------------------------------------------------- */
1009
1010/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001011static struct acpi_video_device_attrib*
1012acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1013{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001014 struct acpi_video_enumerated_device *ids;
1015 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001016
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001017 for (i = 0; i < video->attached_count; i++) {
1018 ids = &video->attached_array[i];
1019 if ((ids->value.int_val & 0xffff) == device_id)
1020 return &ids->value.attrib;
1021 }
1022
Rui Zhang82cae992007-01-03 23:40:53 -05001023 return NULL;
1024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001027acpi_video_get_device_type(struct acpi_video_bus *video,
1028 unsigned long device_id)
1029{
1030 struct acpi_video_enumerated_device *ids;
1031 int i;
1032
1033 for (i = 0; i < video->attached_count; i++) {
1034 ids = &video->attached_array[i];
1035 if ((ids->value.int_val & 0xffff) == device_id)
1036 return ids->value.int_val;
1037 }
1038
1039 return 0;
1040}
1041
1042static int
Len Brown4be44fc2005-08-05 00:44:28 -04001043acpi_video_bus_get_one_device(struct acpi_device *device,
1044 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001046 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001047 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001048 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001049 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001052 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Len Brown4be44fc2005-08-05 00:44:28 -04001054 status =
1055 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (ACPI_SUCCESS(status)) {
1057
Burman Yan36bcbec2006-12-19 12:56:11 -08001058 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001060 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1063 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001064 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 data->device_id = device_id;
1067 data->video = video;
1068 data->dev = device;
1069
Rui Zhang82cae992007-01-03 23:40:53 -05001070 attribute = acpi_video_get_device_attr(video, device_id);
1071
1072 if((attribute != NULL) && attribute->device_id_scheme) {
1073 switch (attribute->display_type) {
1074 case ACPI_VIDEO_DISPLAY_CRT:
1075 data->flags.crt = 1;
1076 break;
1077 case ACPI_VIDEO_DISPLAY_TV:
1078 data->flags.tvout = 1;
1079 break;
1080 case ACPI_VIDEO_DISPLAY_DVI:
1081 data->flags.dvi = 1;
1082 break;
1083 case ACPI_VIDEO_DISPLAY_LCD:
1084 data->flags.lcd = 1;
1085 break;
1086 default:
1087 data->flags.unknown = 1;
1088 break;
1089 }
1090 if(attribute->bios_can_detect)
1091 data->flags.bios = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001092 } else {
1093 /* Check for legacy IDs */
1094 device_type = acpi_video_get_device_type(video,
1095 device_id);
1096 /* Ignore bits 16 and 18-20 */
1097 switch (device_type & 0xffe2ffff) {
1098 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1099 data->flags.crt = 1;
1100 break;
1101 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1102 data->flags.lcd = 1;
1103 break;
1104 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1105 data->flags.tvout = 1;
1106 break;
1107 default:
1108 data->flags.unknown = 1;
1109 }
1110 }
Len Brown4be44fc2005-08-05 00:44:28 -04001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 acpi_video_device_bind(video, data);
1113 acpi_video_device_find_cap(data);
1114
Patrick Mochel90130262006-05-19 16:54:48 -04001115 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001116 ACPI_DEVICE_NOTIFY,
1117 acpi_video_device_notify,
1118 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001120 printk(KERN_ERR PREFIX
1121 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001122 if(data->brightness)
1123 kfree(data->brightness->levels);
1124 kfree(data->brightness);
1125 kfree(data);
1126 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001129 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001131 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Patrick Mocheld550d982006-06-27 00:41:40 -04001136 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139/*
1140 * Arg:
1141 * video : video bus device
1142 *
1143 * Return:
1144 * none
1145 *
1146 * Enumerate the video device list of the video bus,
1147 * bind the ids with the corresponding video devices
1148 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Len Brown4be44fc2005-08-05 00:44:28 -04001151static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001153 struct acpi_video_device *dev;
1154
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001155 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001156
1157 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001158 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001159
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001160 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163/*
1164 * Arg:
1165 * video : video bus device
1166 * device : video output device under the video
1167 * bus
1168 *
1169 * Return:
1170 * none
1171 *
1172 * Bind the ids with the corresponding video devices
1173 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176static void
Len Brown4be44fc2005-08-05 00:44:28 -04001177acpi_video_device_bind(struct acpi_video_bus *video,
1178 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001180 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001181 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001183 for (i = 0; i < video->attached_count; i++) {
1184 ids = &video->attached_array[i];
1185 if (device->device_id == (ids->value.int_val & 0xffff)) {
1186 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1188 }
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192/*
1193 * Arg:
1194 * video : video bus device
1195 *
1196 * Return:
1197 * < 0 : error
1198 *
1199 * Call _DOD to enumerate all devices attached to display adapter
1200 *
Len Brown4be44fc2005-08-05 00:44:28 -04001201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1204{
Len Brown4be44fc2005-08-05 00:44:28 -04001205 int status;
1206 int count;
1207 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001208 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001209 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1210 union acpi_object *dod = NULL;
1211 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Patrick Mochel90130262006-05-19 16:54:48 -04001213 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001215 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001219 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001221 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 status = -EFAULT;
1223 goto out;
1224 }
1225
1226 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001227 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001229 active_list = kcalloc(1 + dod->package.count,
1230 sizeof(struct acpi_video_enumerated_device),
1231 GFP_KERNEL);
1232 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 status = -ENOMEM;
1234 goto out;
1235 }
1236
1237 count = 0;
1238 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001239 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001242 printk(KERN_ERR PREFIX
1243 "Invalid _DOD data in element %d\n", i);
1244 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001246
1247 active_list[count].value.int_val = obj->integer.value;
1248 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001249 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1250 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 count++;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jesper Juhl6044ec82005-11-07 01:01:32 -08001254 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001255
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001256 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001258
1259 out:
Len Brown02438d82006-06-30 03:19:10 -04001260 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Len Brown4be44fc2005-08-05 00:44:28 -04001264static int
1265acpi_video_get_next_level(struct acpi_video_device *device,
1266 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001268 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001269 max = max_below = 0;
1270 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001271 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001272 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001273 l = device->brightness->levels[i];
1274 if (abs(l - level_current) < abs(delta)) {
1275 delta = l - level_current;
1276 if (!delta)
1277 break;
1278 }
1279 }
1280 /* Ajust level_current to closest available level */
1281 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001282 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001283 l = device->brightness->levels[i];
1284 if (l < min)
1285 min = l;
1286 if (l > max)
1287 max = l;
1288 if (l < min_above && l > level_current)
1289 min_above = l;
1290 if (l > max_below && l < level_current)
1291 max_below = l;
1292 }
1293
1294 switch (event) {
1295 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1296 return (level_current < max) ? min_above : min;
1297 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1298 return (level_current < max) ? min_above : max;
1299 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1300 return (level_current > min) ? max_below : min;
1301 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1302 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1303 return 0;
1304 default:
1305 return level_current;
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Zhang Ruic8890f92009-03-18 16:27:08 +08001309static int
Len Brown4be44fc2005-08-05 00:44:28 -04001310acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001312 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001313 int result = -EINVAL;
1314
Zhang Rui28c32e92009-07-13 10:33:24 +08001315 /* no warning message if acpi_backlight=vendor is used */
1316 if (!acpi_video_backlight_support())
1317 return 0;
1318
Julia Jomantaite469778c2008-06-23 22:50:42 +01001319 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001320 goto out;
1321
1322 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05001323 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08001324 if (result)
1325 goto out;
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001328
Zhang Rui24450c72009-03-18 16:27:10 +08001329 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001330
Matthew Garrett36342742009-07-14 17:06:03 +01001331 if (!result)
1332 backlight_force_update(device->backlight,
1333 BACKLIGHT_UPDATE_HOTKEY);
1334
Zhang Ruic8890f92009-03-18 16:27:08 +08001335out:
1336 if (result)
1337 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1338
1339 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
Matthew Garrette92a7162010-01-12 14:17:03 -05001342int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1343 void **edid)
1344{
1345 struct acpi_video_bus *video;
1346 struct acpi_video_device *video_device;
1347 union acpi_object *buffer = NULL;
1348 acpi_status status;
1349 int i, length;
1350
1351 if (!device || !acpi_driver_data(device))
1352 return -EINVAL;
1353
1354 video = acpi_driver_data(device);
1355
1356 for (i = 0; i < video->attached_count; i++) {
1357 video_device = video->attached_array[i].bind_info;
1358 length = 256;
1359
1360 if (!video_device)
1361 continue;
1362
1363 if (type) {
1364 switch (type) {
1365 case ACPI_VIDEO_DISPLAY_CRT:
1366 if (!video_device->flags.crt)
1367 continue;
1368 break;
1369 case ACPI_VIDEO_DISPLAY_TV:
1370 if (!video_device->flags.tvout)
1371 continue;
1372 break;
1373 case ACPI_VIDEO_DISPLAY_DVI:
1374 if (!video_device->flags.dvi)
1375 continue;
1376 break;
1377 case ACPI_VIDEO_DISPLAY_LCD:
1378 if (!video_device->flags.lcd)
1379 continue;
1380 break;
1381 }
1382 } else if (video_device->device_id != device_id) {
1383 continue;
1384 }
1385
1386 status = acpi_video_device_EDID(video_device, &buffer, length);
1387
1388 if (ACPI_FAILURE(status) || !buffer ||
1389 buffer->type != ACPI_TYPE_BUFFER) {
1390 length = 128;
1391 status = acpi_video_device_EDID(video_device, &buffer,
1392 length);
1393 if (ACPI_FAILURE(status) || !buffer ||
1394 buffer->type != ACPI_TYPE_BUFFER) {
1395 continue;
1396 }
1397 }
1398
1399 *edid = buffer->buffer.pointer;
1400 return length;
1401 }
1402
1403 return -ENODEV;
1404}
1405EXPORT_SYMBOL(acpi_video_get_edid);
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407static int
Len Brown4be44fc2005-08-05 00:44:28 -04001408acpi_video_bus_get_devices(struct acpi_video_bus *video,
1409 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Len Brown4be44fc2005-08-05 00:44:28 -04001411 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001412 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 acpi_video_device_enumerate(video);
1415
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001416 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 status = acpi_video_bus_get_one_device(dev, video);
1419 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001420 printk(KERN_WARNING PREFIX
Andi Kleencfa806f2010-07-20 15:18:36 -07001421 "Cant attach device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 continue;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Len Brown4be44fc2005-08-05 00:44:28 -04001428static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Karol Kozimor031ec772005-07-30 04:18:00 -04001430 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001433 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Patrick Mochel90130262006-05-19 16:54:48 -04001435 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001436 ACPI_DEVICE_NOTIFY,
1437 acpi_video_device_notify);
Andi Kleencfa806f2010-07-20 15:18:36 -07001438 if (ACPI_FAILURE(status)) {
1439 printk(KERN_WARNING PREFIX
1440 "Cant remove video notify handler\n");
1441 }
Keith Packarde29b3ee2009-08-06 15:57:54 -07001442 if (device->backlight) {
1443 sysfs_remove_link(&device->backlight->dev.kobj, "device");
1444 backlight_device_unregister(device->backlight);
1445 device->backlight = NULL;
1446 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001447 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08001448 sysfs_remove_link(&device->dev->dev.kobj,
1449 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001450 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08001451 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001452 thermal_cooling_device_unregister(device->cooling_dev);
1453 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08001454 }
Luming Yu23b0f012007-05-09 21:07:05 +08001455 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001456
Patrick Mocheld550d982006-06-27 00:41:40 -04001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Len Brown4be44fc2005-08-05 00:44:28 -04001460static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Len Brown4be44fc2005-08-05 00:44:28 -04001462 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001463 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001465 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001467 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001469 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001470 if (ACPI_FAILURE(status))
1471 printk(KERN_WARNING PREFIX
1472 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001474 if (dev->brightness) {
1475 kfree(dev->brightness->levels);
1476 kfree(dev->brightness);
1477 }
1478 list_del(&dev->entry);
1479 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001482 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001483
Patrick Mocheld550d982006-06-27 00:41:40 -04001484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/* acpi_video interface */
1488
Len Brown4be44fc2005-08-05 00:44:28 -04001489static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Zhang Ruia21101c2007-09-14 11:46:22 +08001491 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Len Brown4be44fc2005-08-05 00:44:28 -04001494static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 return acpi_video_bus_DOS(video, 0, 1);
1497}
1498
Bjorn Helgaas70155582009-04-07 15:37:11 +00001499static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001501 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001502 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001503 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Luming Yue9dab192007-08-20 18:23:53 +08001508 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001511 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001513 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001514 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
1516
Julius Volz98fb8fe2007-02-20 16:38:40 +01001517 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * connector. */
1519 acpi_video_device_enumerate(video);
1520 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001521 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001522 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
1524
Len Brown4be44fc2005-08-05 00:44:28 -04001525 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001526 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001527 keycode = KEY_SWITCHVIDEOMODE;
1528 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001529 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001530 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001531 keycode = KEY_VIDEO_NEXT;
1532 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001533 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001534 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001535 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 break;
1537
1538 default:
1539 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001540 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542 }
1543
Zhang Rui7761f632008-01-25 14:48:12 +08001544 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001545
1546 if (keycode) {
1547 input_report_key(input, keycode, 1);
1548 input_sync(input);
1549 input_report_key(input, keycode, 0);
1550 input_sync(input);
1551 }
Luming Yue9dab192007-08-20 18:23:53 +08001552
Patrick Mocheld550d982006-06-27 00:41:40 -04001553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Len Brown4be44fc2005-08-05 00:44:28 -04001556static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001558 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001559 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001560 struct acpi_video_bus *bus;
1561 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001562 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001567 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001568 bus = video_device->video;
1569 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001572 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001573 if (brightness_switch_enabled)
1574 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001575 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001576 keycode = KEY_BRIGHTNESS_CYCLE;
1577 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001578 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001579 if (brightness_switch_enabled)
1580 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001581 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001582 keycode = KEY_BRIGHTNESSUP;
1583 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001584 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001585 if (brightness_switch_enabled)
1586 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001587 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001588 keycode = KEY_BRIGHTNESSDOWN;
1589 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001590 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001591 if (brightness_switch_enabled)
1592 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001593 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001594 keycode = KEY_BRIGHTNESS_ZERO;
1595 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001596 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001597 if (brightness_switch_enabled)
1598 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001599 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001600 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 default:
1603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001604 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 }
Luming Yue9dab192007-08-20 18:23:53 +08001607
Zhang Rui7761f632008-01-25 14:48:12 +08001608 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001609
1610 if (keycode) {
1611 input_report_key(input, keycode, 1);
1612 input_sync(input);
1613 input_report_key(input, keycode, 0);
1614 input_sync(input);
1615 }
Luming Yue9dab192007-08-20 18:23:53 +08001616
Patrick Mocheld550d982006-06-27 00:41:40 -04001617 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001620static int acpi_video_resume(struct notifier_block *nb,
1621 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001622{
1623 struct acpi_video_bus *video;
1624 struct acpi_video_device *video_device;
1625 int i;
1626
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001627 switch (val) {
1628 case PM_HIBERNATION_PREPARE:
1629 case PM_SUSPEND_PREPARE:
1630 case PM_RESTORE_PREPARE:
1631 return NOTIFY_DONE;
1632 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001633
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001634 video = container_of(nb, struct acpi_video_bus, pm_nb);
1635
1636 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001637
1638 for (i = 0; i < video->attached_count; i++) {
1639 video_device = video->attached_array[i].bind_info;
1640 if (video_device && video_device->backlight)
1641 acpi_video_set_brightness(video_device->backlight);
1642 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001643
1644 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001645}
1646
Zhang Ruic504f8c2009-12-30 15:59:23 +08001647static acpi_status
1648acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1649 void **return_value)
1650{
1651 struct acpi_device *device = context;
1652 struct acpi_device *sibling;
1653 int result;
1654
1655 if (handle == device->handle)
1656 return AE_CTRL_TERMINATE;
1657
1658 result = acpi_bus_get_device(handle, &sibling);
1659 if (result)
1660 return AE_OK;
1661
1662 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1663 return AE_ALREADY_EXISTS;
1664
1665 return AE_OK;
1666}
1667
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001668static int instance;
1669
Len Brown4be44fc2005-08-05 00:44:28 -04001670static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001672 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001673 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001674 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001675 acpi_status status;
1676
1677 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1678 device->parent->handle, 1,
1679 acpi_video_bus_match, NULL,
1680 device, NULL);
1681 if (status == AE_ALREADY_EXISTS) {
1682 printk(KERN_WARNING FW_BUG
1683 "Duplicate ACPI video bus devices for the"
1684 " same VGA controller, please try module "
1685 "parameter \"video.allow_duplicates=1\""
1686 "if the current driver doesn't work.\n");
1687 if (!allow_duplicates)
1688 return -ENODEV;
1689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Burman Yan36bcbec2006-12-19 12:56:11 -08001691 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Zhang Ruie6d9da12007-08-25 02:23:31 -04001695 /* a hack to fix the duplicate name "VID" problem on T61 */
1696 if (!strcmp(device->pnp.bus_id, "VID")) {
1697 if (instance)
1698 device->pnp.bus_id[3] = '0' + instance;
1699 instance ++;
1700 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001701 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1702 if (!strcmp(device->pnp.bus_id, "VGA")) {
1703 if (instance)
1704 device->pnp.bus_id[3] = '0' + instance;
1705 instance++;
1706 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001707
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001708 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1710 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001711 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001714 error = acpi_video_bus_check(video);
1715 if (error)
1716 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001718 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 INIT_LIST_HEAD(&video->video_device_list);
1720
1721 acpi_video_bus_get_devices(video, device);
1722 acpi_video_bus_start_devices(video);
1723
Luming Yue9dab192007-08-20 18:23:53 +08001724 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001725 if (!input) {
1726 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00001727 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001728 }
Luming Yue9dab192007-08-20 18:23:53 +08001729
1730 snprintf(video->phys, sizeof(video->phys),
1731 "%s/video/input0", acpi_device_hid(video->device));
1732
1733 input->name = acpi_device_name(video->device);
1734 input->phys = video->phys;
1735 input->id.bustype = BUS_HOST;
1736 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05001737 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001738 input->evbit[0] = BIT(EV_KEY);
1739 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1740 set_bit(KEY_VIDEO_NEXT, input->keybit);
1741 set_bit(KEY_VIDEO_PREV, input->keybit);
1742 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1743 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1744 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1745 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1746 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08001747
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001748 error = input_register_device(input);
1749 if (error)
1750 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001753 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1754 video->flags.multihead ? "yes" : "no",
1755 video->flags.rom ? "yes" : "no",
1756 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001758 video->pm_nb.notifier_call = acpi_video_resume;
1759 video->pm_nb.priority = 0;
1760 register_pm_notifier(&video->pm_nb);
1761
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001764 err_free_input_dev:
1765 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001766 err_stop_video:
1767 acpi_video_bus_stop_devices(video);
1768 acpi_video_bus_put_devices(video);
1769 kfree(video->attached_array);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001770 err_free_video:
1771 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001772 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001773
1774 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Len Brown4be44fc2005-08-05 00:44:28 -04001777static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Len Brown4be44fc2005-08-05 00:44:28 -04001779 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001785 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001787 unregister_pm_notifier(&video->pm_nb);
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Luming Yue9dab192007-08-20 18:23:53 +08001792 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001793 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 kfree(video);
1795
Patrick Mocheld550d982006-06-27 00:41:40 -04001796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Matthew Garrett74a365b2009-03-19 21:35:39 +00001799static int __init intel_opregion_present(void)
1800{
1801#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1802 struct pci_dev *dev = NULL;
1803 u32 address;
1804
1805 for_each_pci_dev(dev) {
1806 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1807 continue;
1808 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1809 continue;
1810 pci_read_config_dword(dev, 0xfc, &address);
1811 if (!address)
1812 continue;
1813 return 1;
1814 }
1815#endif
1816 return 0;
1817}
1818
1819int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Len Brown4be44fc2005-08-05 00:44:28 -04001821 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001822 if (register_count) {
1823 /*
1824 * if the function of acpi_video_register is already called,
1825 * don't register the acpi_vide_bus again and return no error.
1826 */
1827 return 0;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08001831 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04001832 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Zhao Yakui86e437f2009-06-16 11:23:13 +08001834 /*
1835 * When the acpi_video_bus is loaded successfully, increase
1836 * the counter reference.
1837 */
1838 register_count = 1;
1839
Patrick Mocheld550d982006-06-27 00:41:40 -04001840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
Matthew Garrett74a365b2009-03-19 21:35:39 +00001842EXPORT_SYMBOL(acpi_video_register);
1843
Zhao Yakui86e437f2009-06-16 11:23:13 +08001844void acpi_video_unregister(void)
1845{
1846 if (!register_count) {
1847 /*
1848 * If the acpi video bus is already unloaded, don't
1849 * unload it again and return directly.
1850 */
1851 return;
1852 }
1853 acpi_bus_unregister_driver(&acpi_video_bus);
1854
Zhao Yakui86e437f2009-06-16 11:23:13 +08001855 register_count = 0;
1856
1857 return;
1858}
1859EXPORT_SYMBOL(acpi_video_unregister);
1860
Matthew Garrett74a365b2009-03-19 21:35:39 +00001861/*
1862 * This is kind of nasty. Hardware using Intel chipsets may require
1863 * the video opregion code to be run first in order to initialise
1864 * state before any ACPI video calls are made. To handle this we defer
1865 * registration of the video class until the opregion code has run.
1866 */
1867
1868static int __init acpi_video_init(void)
1869{
Zhang Rui45cb50e2009-04-24 12:13:18 -04001870 dmi_check_system(video_dmi_table);
1871
Matthew Garrett74a365b2009-03-19 21:35:39 +00001872 if (intel_opregion_present())
1873 return 0;
1874
1875 return acpi_video_register();
1876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Zhao Yakui86e437f2009-06-16 11:23:13 +08001878static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Zhao Yakui86e437f2009-06-16 11:23:13 +08001880 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Patrick Mocheld550d982006-06-27 00:41:40 -04001882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885module_init(acpi_video_init);
1886module_exit(acpi_video_exit);