blob: 89f19a8fc726c5d7a0aeb292cfc5123e159d5b4f [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
Zhang Rui99fd1892010-12-06 15:04:27 +080084/*
85 * Some BIOSes claim they use minimum backlight at boot,
86 * and this may bring dimming screen after boot
87 */
88static int use_bios_initial_backlight = 1;
89module_param(use_bios_initial_backlight, bool, 0644);
90
Zhao Yakui86e437f2009-06-16 11:23:13 +080091static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040092static int acpi_video_bus_add(struct acpi_device *device);
93static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000094static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Thomas Renninger1ba90e32007-07-23 14:44:41 +020096static const struct acpi_device_id video_device_ids[] = {
97 {ACPI_VIDEO_HID, 0},
98 {"", 0},
99};
100MODULE_DEVICE_TABLE(acpi, video_device_ids);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500103 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200105 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .ops = {
107 .add = acpi_video_bus_add,
108 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000109 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 u8 multihead:1; /* can switch video heads */
115 u8 rom:1; /* can retrieve a video rom */
116 u8 post:1; /* can configure the head to */
117 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 _DOS:1; /*Enable/Disable output switching */
122 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
123 u8 _ROM:1; /*Get ROM Data */
124 u8 _GPD:1; /*Get POST Device */
125 u8 _SPD:1; /*Set POST Device */
126 u8 _VPO:1; /*Video POST Options */
127 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130struct acpi_video_device_attrib {
131 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100132 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400133 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100134 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400135 u32 bios_can_detect:1; /*BIOS can detect the device */
136 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
137 the VGA device. */
138 u32 pipe_id:3; /*For VGA multiple-head devices. */
139 u32 reserved:10; /*Must be 0 */
140 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_video_enumerated_device {
144 union {
145 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } value;
148 struct acpi_video_device *bind_info;
149};
150
151struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400152 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 attached_count;
156 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500159 struct mutex device_list_lock; /* protects video_device_list */
Luming Yue9dab192007-08-20 18:23:53 +0800160 struct input_dev *input;
161 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200162 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 crt:1;
167 u8 lcd:1;
168 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500169 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 u8 bios:1;
171 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500172 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
175struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400176 u8 _ADR:1; /*Return the unique ID */
177 u8 _BCL:1; /*Query list of brightness control levels supported */
178 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800179 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800180 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400181 u8 _DDC:1; /*Return the EDID for this device */
182 u8 _DCS:1; /*Return status of output device */
183 u8 _DGS:1; /*Query graphics state */
184 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Zhang Ruid32f6942009-03-18 16:27:12 +0800187struct acpi_video_brightness_flags {
188 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800189 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800190 u8 _BCL_use_index:1; /* levels in _BCL are index values */
191 u8 _BCM_use_index:1; /* input of _BCM is an index value */
192 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800193};
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400196 int curr;
197 int count;
198 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800199 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
202struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400203 unsigned long device_id;
204 struct acpi_video_device_flags flags;
205 struct acpi_video_device_cap cap;
206 struct list_head entry;
207 struct acpi_video_bus *video;
208 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800210 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400211 struct thermal_cooling_device *cooling_dev;
Luming Yu23b0f012007-05-09 21:07:05 +0800212 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100215static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 "motherboard VGA device",
217 "PCI VGA device",
218 "AGP VGA device",
219 "UNKNOWN",
220};
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
223static void acpi_video_device_rebind(struct acpi_video_bus *video);
224static void acpi_video_device_bind(struct acpi_video_bus *video,
225 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800227static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
228 int level);
229static int acpi_video_device_lcd_get_level_current(
230 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500231 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400232static int acpi_video_get_next_level(struct acpi_video_device *device,
233 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800234static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400235 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800236static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400237 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800238static int acpi_video_output_get(struct output_device *od);
239static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Yu Luming2f3d0002006-11-11 02:40:34 +0800241/*backlight device sysfs support*/
242static int acpi_video_get_brightness(struct backlight_device *bd)
243{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400244 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000245 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800246 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100247 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800248
Matthew Garrett70287db2010-02-16 16:53:50 -0500249 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800250 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000251 for (i = 2; i < vd->brightness->count; i++) {
252 if (vd->brightness->levels[i] == cur_level)
253 /* The first two entries are special - see page 575
254 of the ACPI spec 3.0 */
255 return i-2;
256 }
257 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800258}
259
260static int acpi_video_set_brightness(struct backlight_device *bd)
261{
Zhang Rui24450c72009-03-18 16:27:10 +0800262 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800263 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100264 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800265
266 return acpi_video_device_lcd_set_level(vd,
267 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800268}
269
Richard Purdie599a52d2007-02-10 23:07:48 +0000270static struct backlight_ops acpi_backlight_ops = {
271 .get_brightness = acpi_video_get_brightness,
272 .update_status = acpi_video_set_brightness,
273};
274
Luming Yu23b0f012007-05-09 21:07:05 +0800275/*video output device sysfs support*/
276static int acpi_video_output_get(struct output_device *od)
277{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400278 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800279 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700280 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800281 acpi_video_device_get_state(vd, &state);
282 return (int)state;
283}
284
285static int acpi_video_output_set(struct output_device *od)
286{
287 unsigned long state = od->request_state;
288 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700289 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800290 return acpi_video_device_set_state(vd, state);
291}
292
293static struct output_properties acpi_output_properties = {
294 .set_state = acpi_video_output_set,
295 .get_status = acpi_video_output_get,
296};
Zhang Rui702ed512008-01-17 15:51:22 +0800297
298
299/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400300static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000301 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800302{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400303 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800304 struct acpi_video_device *video = acpi_driver_data(device);
305
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000306 *state = video->brightness->count - 3;
307 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800308}
309
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400310static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000311 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800312{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400313 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800314 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400315 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000316 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800317
Matthew Garrett70287db2010-02-16 16:53:50 -0500318 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800319 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000320 for (offset = 2; offset < video->brightness->count; offset++)
321 if (level == video->brightness->levels[offset]) {
322 *state = video->brightness->count - offset - 1;
323 return 0;
324 }
Zhang Rui702ed512008-01-17 15:51:22 +0800325
326 return -EINVAL;
327}
328
329static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400330video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800331{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400332 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800333 struct acpi_video_device *video = acpi_driver_data(device);
334 int level;
335
336 if ( state >= video->brightness->count - 2)
337 return -EINVAL;
338
339 state = video->brightness->count - state;
340 level = video->brightness->levels[state -1];
341 return acpi_video_device_lcd_set_level(video, level);
342}
343
344static struct thermal_cooling_device_ops video_cooling_ops = {
345 .get_max_state = video_get_max_state,
346 .get_cur_state = video_get_cur_state,
347 .set_cur_state = video_set_cur_state,
348};
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* --------------------------------------------------------------------------
351 Video Management
352 -------------------------------------------------------------------------- */
353
354/* device */
355
356static int
Len Brown4be44fc2005-08-05 00:44:28 -0400357acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400358 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Len Brown4be44fc2005-08-05 00:44:28 -0400360 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Patrick Mochel90130262006-05-19 16:54:48 -0400362 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Patrick Mocheld550d982006-06-27 00:41:40 -0400364 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367static int
Len Brown4be44fc2005-08-05 00:44:28 -0400368acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Len Brown4be44fc2005-08-05 00:44:28 -0400370 int status;
371 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
372 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400373 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400377 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Patrick Mocheld550d982006-06-27 00:41:40 -0400379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382static int
Len Brown4be44fc2005-08-05 00:44:28 -0400383acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
384 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Len Brown4be44fc2005-08-05 00:44:28 -0400386 int status;
387 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
388 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 *levels = NULL;
392
Patrick Mochel90130262006-05-19 16:54:48 -0400393 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400395 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400396 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500397 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400398 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 status = -EFAULT;
400 goto err;
401 }
402
403 *levels = obj;
404
Patrick Mocheld550d982006-06-27 00:41:40 -0400405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800408 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Patrick Mocheld550d982006-06-27 00:41:40 -0400410 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413static int
Len Brown4be44fc2005-08-05 00:44:28 -0400414acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Zhang Rui24450c72009-03-18 16:27:10 +0800416 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400417 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
418 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800419 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Zhang Rui24450c72009-03-18 16:27:10 +0800423 status = acpi_evaluate_object(device->dev->handle, "_BCM",
424 &args, NULL);
425 if (ACPI_FAILURE(status)) {
426 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
427 return -EIO;
428 }
429
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400430 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800431 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800432 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800433 if (device->backlight)
434 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800435 return 0;
436 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800437
Zhang Rui24450c72009-03-18 16:27:10 +0800438 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Zhang Rui45cb50e2009-04-24 12:13:18 -0400442/*
443 * For some buggy _BQC methods, we need to add a constant value to
444 * the _BQC return value to get the actual current brightness level
445 */
446
447static int bqc_offset_aml_bug_workaround;
448static int __init video_set_bqc_offset(const struct dmi_system_id *d)
449{
450 bqc_offset_aml_bug_workaround = 9;
451 return 0;
452}
453
454static struct dmi_system_id video_dmi_table[] __initdata = {
455 /*
456 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
457 */
458 {
459 .callback = video_set_bqc_offset,
460 .ident = "Acer Aspire 5720",
461 .matches = {
462 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
463 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
464 },
465 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400466 {
467 .callback = video_set_bqc_offset,
468 .ident = "Acer Aspire 5710Z",
469 .matches = {
470 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
471 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
472 },
473 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400474 {
475 .callback = video_set_bqc_offset,
476 .ident = "eMachines E510",
477 .matches = {
478 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
479 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
480 },
481 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400482 {
483 .callback = video_set_bqc_offset,
484 .ident = "Acer Aspire 5315",
485 .matches = {
486 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
487 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
488 },
489 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800490 {
491 .callback = video_set_bqc_offset,
492 .ident = "Acer Aspire 7720",
493 .matches = {
494 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
495 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
496 },
497 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400498 {}
499};
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static int
Len Brown4be44fc2005-08-05 00:44:28 -0400502acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500503 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Zhang Ruic8890f92009-03-18 16:27:08 +0800505 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800506 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800507
Zhang Ruic60d6382009-03-18 16:27:18 +0800508 if (device->cap._BQC || device->cap._BCQ) {
509 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
510
511 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800512 NULL, level);
513 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800514 if (device->brightness->flags._BQC_use_index) {
515 if (device->brightness->flags._BCL_reversed)
516 *level = device->brightness->count
517 - 3 - (*level);
518 *level = device->brightness->levels[*level + 2];
519
520 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400521 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800522 for (i = 2; i < device->brightness->count; i++)
523 if (device->brightness->levels[i] == *level) {
524 device->brightness->curr = *level;
525 return 0;
526 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500527 if (!init) {
528 /*
529 * BQC returned an invalid level.
530 * Stop using it.
531 */
532 ACPI_WARNING((AE_INFO,
533 "%s returned an invalid level",
534 buf));
535 device->cap._BQC = device->cap._BCQ = 0;
536 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800537 } else {
538 /* Fixme:
539 * should we return an error or ignore this failure?
540 * dev->brightness->curr is a cached value which stores
541 * the correct current backlight level in most cases.
542 * ACPI video backlight still works w/ buggy _BQC.
543 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
544 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800545 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
546 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800547 }
548 }
549
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400550 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554static int
Len Brown4be44fc2005-08-05 00:44:28 -0400555acpi_video_device_EDID(struct acpi_video_device *device,
556 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Len Brown4be44fc2005-08-05 00:44:28 -0400558 int status;
559 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
560 union acpi_object *obj;
561 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
562 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 *edid = NULL;
566
567 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400568 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (length == 128)
570 arg0.integer.value = 1;
571 else if (length == 256)
572 arg0.integer.value = 2;
573 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Patrick Mochel90130262006-05-19 16:54:48 -0400576 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200580 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (obj && obj->type == ACPI_TYPE_BUFFER)
583 *edid = obj;
584 else {
Len Brown64684632006-06-26 23:41:38 -0400585 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 status = -EFAULT;
587 kfree(obj);
588 }
589
Patrick Mocheld550d982006-06-27 00:41:40 -0400590 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/* bus */
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * Arg:
597 * video : video bus device pointer
598 * bios_flag :
599 * 0. The system BIOS should NOT automatically switch(toggle)
600 * the active display output.
601 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100602 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * 2. The _DGS value should be locked.
604 * 3. The system BIOS should not automatically switch (toggle) the
605 * active display output, but instead generate the display switch
606 * event notify code.
607 * lcd_flag :
608 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100609 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100611 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * Return Value:
613 * -1 wrong arg.
614 */
615
616static int
Len Brown4be44fc2005-08-05 00:44:28 -0400617acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Lin Ming439913f2010-01-28 10:53:19 +0800619 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400620 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
621 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Len Brown4be44fc2005-08-05 00:44:28 -0400624 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 status = -1;
626 goto Failed;
627 }
628 arg0.integer.value = (lcd_flag << 2) | bios_flag;
629 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400630 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400633 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500637 * Simple comparison function used to sort backlight levels.
638 */
639
640static int
641acpi_video_cmp_level(const void *a, const void *b)
642{
643 return *(int *)a - *(int *)b;
644}
645
646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * Arg:
648 * device : video output device (LCD, CRT, ..)
649 *
650 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100651 * Maximum brightness level
652 *
653 * Allocate and initialize device->brightness.
654 */
655
656static int
657acpi_video_init_brightness(struct acpi_video_device *device)
658{
659 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800660 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800661 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100662 union acpi_object *o;
663 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800664 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100665
666 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
667 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
668 "LCD brightness level\n"));
669 goto out;
670 }
671
672 if (obj->package.count < 2)
673 goto out;
674
675 br = kzalloc(sizeof(*br), GFP_KERNEL);
676 if (!br) {
677 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800678 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100679 goto out;
680 }
681
Zhang Ruid32f6942009-03-18 16:27:12 +0800682 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100683 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800684 if (!br->levels) {
685 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100686 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800687 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100688
689 for (i = 0; i < obj->package.count; i++) {
690 o = (union acpi_object *)&obj->package.elements[i];
691 if (o->type != ACPI_TYPE_INTEGER) {
692 printk(KERN_ERR PREFIX "Invalid data\n");
693 continue;
694 }
695 br->levels[count] = (u32) o->integer.value;
696
697 if (br->levels[count] > max_level)
698 max_level = br->levels[count];
699 count++;
700 }
701
Zhang Ruid32f6942009-03-18 16:27:12 +0800702 /*
703 * some buggy BIOS don't export the levels
704 * when machine is on AC/Battery in _BCL package.
705 * In this case, the first two elements in _BCL packages
706 * are also supported brightness levels that OS should take care of.
707 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800708 for (i = 2; i < count; i++) {
709 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800710 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800711 if (br->levels[i] == br->levels[1])
712 level_ac_battery++;
713 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500714
Zhang Ruid32f6942009-03-18 16:27:12 +0800715 if (level_ac_battery < 2) {
716 level_ac_battery = 2 - level_ac_battery;
717 br->flags._BCL_no_ac_battery_levels = 1;
718 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
719 br->levels[i] = br->levels[i - level_ac_battery];
720 count += level_ac_battery;
721 } else if (level_ac_battery > 2)
722 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
723
Zhang Ruid80fb992009-03-18 16:27:14 +0800724 /* Check if the _BCL package is in a reversed order */
725 if (max_level == br->levels[2]) {
726 br->flags._BCL_reversed = 1;
727 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
728 acpi_video_cmp_level, NULL);
729 } else if (max_level != br->levels[count - 1])
730 ACPI_ERROR((AE_INFO,
731 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100732
733 br->count = count;
734 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800735
736 /* Check the input/output of _BQC/_BCL/_BCM */
737 if ((max_level < 100) && (max_level <= (count - 2)))
738 br->flags._BCL_use_index = 1;
739
740 /*
741 * _BCM is always consistent with _BCL,
742 * at least for all the laptops we have ever seen.
743 */
744 br->flags._BCM_use_index = br->flags._BCL_use_index;
745
746 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400747 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800748
749 if (!device->cap._BQC)
750 goto set_level;
751
Matthew Garrett70287db2010-02-16 16:53:50 -0500752 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800753 if (result)
754 goto out_free_levels;
755
Zhang Ruie047cca2009-04-09 14:24:35 +0800756 /*
757 * Set the level to maximum and check if _BQC uses indexed value
758 */
759 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800760 if (result)
761 goto out_free_levels;
762
Matthew Garrett70287db2010-02-16 16:53:50 -0500763 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800764 if (result)
765 goto out_free_levels;
766
Zhang Ruie047cca2009-04-09 14:24:35 +0800767 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800768
Zhang Rui90c53ca2009-08-31 12:39:54 -0400769 if (!br->flags._BQC_use_index) {
770 /*
771 * Set the backlight to the initial state.
772 * On some buggy laptops, _BQC returns an uninitialized value
773 * when invoked for the first time, i.e. level_old is invalid.
774 * set the backlight to max_level in this case
775 */
Zhang Rui99fd1892010-12-06 15:04:27 +0800776 if (use_bios_initial_backlight) {
777 for (i = 2; i < br->count; i++)
778 if (level_old == br->levels[i])
779 level = level_old;
780 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800781 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400782 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800783
784 if (br->flags._BCL_reversed)
785 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400786 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800787
788set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400789 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800790 if (result)
791 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800792
Zhang Ruid32f6942009-03-18 16:27:12 +0800793 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
794 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100795 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800796 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100797
798out_free_levels:
799 kfree(br->levels);
800out_free:
801 kfree(br);
802out:
803 device->brightness = NULL;
804 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800805 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100806}
807
808/*
809 * Arg:
810 * device : video output device (LCD, CRT, ..)
811 *
812 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * None
814 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100815 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * device.
817 */
818
Len Brown4be44fc2005-08-05 00:44:28 -0400819static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Patrick Mochel90130262006-05-19 16:54:48 -0400823 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 device->cap._ADR = 1;
825 }
Patrick Mochel90130262006-05-19 16:54:48 -0400826 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400827 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Patrick Mochel90130262006-05-19 16:54:48 -0400829 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400830 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800832 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
833 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800834 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
835 &h_dummy1))) {
836 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
837 device->cap._BCQ = 1;
838 }
839
Patrick Mochel90130262006-05-19 16:54:48 -0400840 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400841 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Patrick Mochel90130262006-05-19 16:54:48 -0400843 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 device->cap._DCS = 1;
845 }
Patrick Mochel90130262006-05-19 16:54:48 -0400846 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 device->cap._DGS = 1;
848 }
Patrick Mochel90130262006-05-19 16:54:48 -0400849 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 device->cap._DSS = 1;
851 }
852
Zhang Rui1a7c6182009-03-18 16:27:16 +0800853 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500854 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +0800855 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800856 static int count = 0;
857 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800858
859 result = acpi_video_init_brightness(device);
860 if (result)
861 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700862 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +0800863 if (!name)
864 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700865 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +0800866
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500867 memset(&props, 0, sizeof(struct backlight_properties));
868 props.max_brightness = device->brightness->count - 3;
869 device->backlight = backlight_device_register(name, NULL, device,
870 &acpi_backlight_ops,
871 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +0800872 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +0800873 if (IS_ERR(device->backlight))
874 return;
Zhang Rui702ed512008-01-17 15:51:22 +0800875
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200876 /*
877 * Save current brightness level in case we have to restore it
878 * before acpi_video_device_lcd_set_level() is called next time.
879 */
880 device->backlight->props.brightness =
881 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +0800882
Zhang Rui056c3082009-06-22 11:31:14 +0800883 result = sysfs_create_link(&device->backlight->dev.kobj,
884 &device->dev->dev.kobj, "device");
885 if (result)
886 printk(KERN_ERR PREFIX "Create sysfs link\n");
887
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400888 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +0800889 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400890 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700891 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400892 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700893 * free it.
894 * Also, why the hell we are returning early and
895 * not attempt to register video output if cooling
896 * device registration failed?
897 * -- dtor
898 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400899 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500900 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -0700901 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500902
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200903 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400904 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800905 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400906 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800907 "thermal_cooling");
908 if (result)
909 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400910 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +0800911 &device->dev->dev.kobj, "device");
912 if (result)
913 printk(KERN_ERR PREFIX "Create sysfs link\n");
914
Yu Luming2f3d0002006-11-11 02:40:34 +0800915 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200916
917 if (acpi_video_display_switch_support()) {
918
919 if (device->cap._DCS && device->cap._DSS) {
920 static int count;
921 char *name;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700922 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200923 if (!name)
924 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -0700925 count++;
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200926 device->output_dev = video_output_register(name,
927 NULL, device, &acpi_output_properties);
928 kfree(name);
929 }
Luming Yu23b0f012007-05-09 21:07:05 +0800930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933/*
934 * Arg:
935 * device : video output device (VGA)
936 *
937 * Return Value:
938 * None
939 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100940 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
942
Len Brown4be44fc2005-08-05 00:44:28 -0400943static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Len Brown4be44fc2005-08-05 00:44:28 -0400945 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Patrick Mochel90130262006-05-19 16:54:48 -0400947 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 video->cap._DOS = 1;
949 }
Patrick Mochel90130262006-05-19 16:54:48 -0400950 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 video->cap._DOD = 1;
952 }
Patrick Mochel90130262006-05-19 16:54:48 -0400953 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 video->cap._ROM = 1;
955 }
Patrick Mochel90130262006-05-19 16:54:48 -0400956 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 video->cap._GPD = 1;
958 }
Patrick Mochel90130262006-05-19 16:54:48 -0400959 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 video->cap._SPD = 1;
961 }
Patrick Mochel90130262006-05-19 16:54:48 -0400962 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 video->cap._VPO = 1;
964 }
965}
966
967/*
968 * Check whether the video bus device has required AML method to
969 * support the desired features
970 */
971
Len Brown4be44fc2005-08-05 00:44:28 -0400972static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Len Brown4be44fc2005-08-05 00:44:28 -0400974 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000975 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000980 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200981 if (!dev)
982 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +0000983 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +0200984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* Since there is no HID, CID and so on for VGA driver, we have
986 * to check well known required nodes.
987 */
988
Julius Volz98fb8fe2007-02-20 16:38:40 +0100989 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +0200990 if (video->cap._DOS || video->cap._DOD) {
991 if (!video->cap._DOS) {
992 printk(KERN_WARNING FW_BUG
993 "ACPI(%s) defines _DOD but not _DOS\n",
994 acpi_device_bid(video->device));
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 video->flags.multihead = 1;
997 status = 0;
998 }
999
Julius Volz98fb8fe2007-02-20 16:38:40 +01001000 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001001 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 video->flags.rom = 1;
1003 status = 0;
1004 }
1005
Julius Volz98fb8fe2007-02-20 16:38:40 +01001006 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001007 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 video->flags.post = 1;
1009 status = 0;
1010 }
1011
Patrick Mocheld550d982006-06-27 00:41:40 -04001012 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 Driver Interface
1017 -------------------------------------------------------------------------- */
1018
1019/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001020static struct acpi_video_device_attrib*
1021acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1022{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001023 struct acpi_video_enumerated_device *ids;
1024 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001025
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001026 for (i = 0; i < video->attached_count; i++) {
1027 ids = &video->attached_array[i];
1028 if ((ids->value.int_val & 0xffff) == device_id)
1029 return &ids->value.attrib;
1030 }
1031
Rui Zhang82cae992007-01-03 23:40:53 -05001032 return NULL;
1033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001036acpi_video_get_device_type(struct acpi_video_bus *video,
1037 unsigned long device_id)
1038{
1039 struct acpi_video_enumerated_device *ids;
1040 int i;
1041
1042 for (i = 0; i < video->attached_count; i++) {
1043 ids = &video->attached_array[i];
1044 if ((ids->value.int_val & 0xffff) == device_id)
1045 return ids->value.int_val;
1046 }
1047
1048 return 0;
1049}
1050
1051static int
Len Brown4be44fc2005-08-05 00:44:28 -04001052acpi_video_bus_get_one_device(struct acpi_device *device,
1053 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001055 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001056 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001057 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001058 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Len Brown4be44fc2005-08-05 00:44:28 -04001063 status =
1064 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (ACPI_SUCCESS(status)) {
1066
Burman Yan36bcbec2006-12-19 12:56:11 -08001067 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001069 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1072 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001073 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 data->device_id = device_id;
1076 data->video = video;
1077 data->dev = device;
1078
Rui Zhang82cae992007-01-03 23:40:53 -05001079 attribute = acpi_video_get_device_attr(video, device_id);
1080
1081 if((attribute != NULL) && attribute->device_id_scheme) {
1082 switch (attribute->display_type) {
1083 case ACPI_VIDEO_DISPLAY_CRT:
1084 data->flags.crt = 1;
1085 break;
1086 case ACPI_VIDEO_DISPLAY_TV:
1087 data->flags.tvout = 1;
1088 break;
1089 case ACPI_VIDEO_DISPLAY_DVI:
1090 data->flags.dvi = 1;
1091 break;
1092 case ACPI_VIDEO_DISPLAY_LCD:
1093 data->flags.lcd = 1;
1094 break;
1095 default:
1096 data->flags.unknown = 1;
1097 break;
1098 }
1099 if(attribute->bios_can_detect)
1100 data->flags.bios = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001101 } else {
1102 /* Check for legacy IDs */
1103 device_type = acpi_video_get_device_type(video,
1104 device_id);
1105 /* Ignore bits 16 and 18-20 */
1106 switch (device_type & 0xffe2ffff) {
1107 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1108 data->flags.crt = 1;
1109 break;
1110 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1111 data->flags.lcd = 1;
1112 break;
1113 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1114 data->flags.tvout = 1;
1115 break;
1116 default:
1117 data->flags.unknown = 1;
1118 }
1119 }
Len Brown4be44fc2005-08-05 00:44:28 -04001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 acpi_video_device_bind(video, data);
1122 acpi_video_device_find_cap(data);
1123
Patrick Mochel90130262006-05-19 16:54:48 -04001124 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001125 ACPI_DEVICE_NOTIFY,
1126 acpi_video_device_notify,
1127 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001129 printk(KERN_ERR PREFIX
1130 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001131 if(data->brightness)
1132 kfree(data->brightness->levels);
1133 kfree(data->brightness);
1134 kfree(data);
1135 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001138 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001140 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Patrick Mocheld550d982006-06-27 00:41:40 -04001145 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/*
1149 * Arg:
1150 * video : video bus device
1151 *
1152 * Return:
1153 * none
1154 *
1155 * Enumerate the video device list of the video bus,
1156 * bind the ids with the corresponding video devices
1157 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Len Brown4be44fc2005-08-05 00:44:28 -04001160static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001162 struct acpi_video_device *dev;
1163
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001164 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001165
1166 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001167 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001168
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001169 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/*
1173 * Arg:
1174 * video : video bus device
1175 * device : video output device under the video
1176 * bus
1177 *
1178 * Return:
1179 * none
1180 *
1181 * Bind the ids with the corresponding video devices
1182 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void
Len Brown4be44fc2005-08-05 00:44:28 -04001186acpi_video_device_bind(struct acpi_video_bus *video,
1187 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001189 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001190 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001192 for (i = 0; i < video->attached_count; i++) {
1193 ids = &video->attached_array[i];
1194 if (device->device_id == (ids->value.int_val & 0xffff)) {
1195 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1197 }
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/*
1202 * Arg:
1203 * video : video bus device
1204 *
1205 * Return:
1206 * < 0 : error
1207 *
1208 * Call _DOD to enumerate all devices attached to display adapter
1209 *
Len Brown4be44fc2005-08-05 00:44:28 -04001210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1213{
Len Brown4be44fc2005-08-05 00:44:28 -04001214 int status;
1215 int count;
1216 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001217 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001218 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1219 union acpi_object *dod = NULL;
1220 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Patrick Mochel90130262006-05-19 16:54:48 -04001222 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001224 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001225 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001228 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001230 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 status = -EFAULT;
1232 goto out;
1233 }
1234
1235 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001236 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001238 active_list = kcalloc(1 + dod->package.count,
1239 sizeof(struct acpi_video_enumerated_device),
1240 GFP_KERNEL);
1241 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 status = -ENOMEM;
1243 goto out;
1244 }
1245
1246 count = 0;
1247 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001248 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001251 printk(KERN_ERR PREFIX
1252 "Invalid _DOD data in element %d\n", i);
1253 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001255
1256 active_list[count].value.int_val = obj->integer.value;
1257 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001258 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1259 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 count++;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jesper Juhl6044ec82005-11-07 01:01:32 -08001263 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001264
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001265 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001267
1268 out:
Len Brown02438d82006-06-30 03:19:10 -04001269 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Len Brown4be44fc2005-08-05 00:44:28 -04001273static int
1274acpi_video_get_next_level(struct acpi_video_device *device,
1275 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001277 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001278 max = max_below = 0;
1279 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001280 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001281 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001282 l = device->brightness->levels[i];
1283 if (abs(l - level_current) < abs(delta)) {
1284 delta = l - level_current;
1285 if (!delta)
1286 break;
1287 }
1288 }
1289 /* Ajust level_current to closest available level */
1290 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001291 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001292 l = device->brightness->levels[i];
1293 if (l < min)
1294 min = l;
1295 if (l > max)
1296 max = l;
1297 if (l < min_above && l > level_current)
1298 min_above = l;
1299 if (l > max_below && l < level_current)
1300 max_below = l;
1301 }
1302
1303 switch (event) {
1304 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1305 return (level_current < max) ? min_above : min;
1306 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1307 return (level_current < max) ? min_above : max;
1308 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1309 return (level_current > min) ? max_below : min;
1310 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1311 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1312 return 0;
1313 default:
1314 return level_current;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Zhang Ruic8890f92009-03-18 16:27:08 +08001318static int
Len Brown4be44fc2005-08-05 00:44:28 -04001319acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001321 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001322 int result = -EINVAL;
1323
Zhang Rui28c32e92009-07-13 10:33:24 +08001324 /* no warning message if acpi_backlight=vendor is used */
1325 if (!acpi_video_backlight_support())
1326 return 0;
1327
Julia Jomantaite469778c2008-06-23 22:50:42 +01001328 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001329 goto out;
1330
1331 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05001332 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08001333 if (result)
1334 goto out;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001337
Zhang Rui24450c72009-03-18 16:27:10 +08001338 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001339
Matthew Garrett36342742009-07-14 17:06:03 +01001340 if (!result)
1341 backlight_force_update(device->backlight,
1342 BACKLIGHT_UPDATE_HOTKEY);
1343
Zhang Ruic8890f92009-03-18 16:27:08 +08001344out:
1345 if (result)
1346 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1347
1348 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Matthew Garrette92a7162010-01-12 14:17:03 -05001351int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1352 void **edid)
1353{
1354 struct acpi_video_bus *video;
1355 struct acpi_video_device *video_device;
1356 union acpi_object *buffer = NULL;
1357 acpi_status status;
1358 int i, length;
1359
1360 if (!device || !acpi_driver_data(device))
1361 return -EINVAL;
1362
1363 video = acpi_driver_data(device);
1364
1365 for (i = 0; i < video->attached_count; i++) {
1366 video_device = video->attached_array[i].bind_info;
1367 length = 256;
1368
1369 if (!video_device)
1370 continue;
1371
1372 if (type) {
1373 switch (type) {
1374 case ACPI_VIDEO_DISPLAY_CRT:
1375 if (!video_device->flags.crt)
1376 continue;
1377 break;
1378 case ACPI_VIDEO_DISPLAY_TV:
1379 if (!video_device->flags.tvout)
1380 continue;
1381 break;
1382 case ACPI_VIDEO_DISPLAY_DVI:
1383 if (!video_device->flags.dvi)
1384 continue;
1385 break;
1386 case ACPI_VIDEO_DISPLAY_LCD:
1387 if (!video_device->flags.lcd)
1388 continue;
1389 break;
1390 }
1391 } else if (video_device->device_id != device_id) {
1392 continue;
1393 }
1394
1395 status = acpi_video_device_EDID(video_device, &buffer, length);
1396
1397 if (ACPI_FAILURE(status) || !buffer ||
1398 buffer->type != ACPI_TYPE_BUFFER) {
1399 length = 128;
1400 status = acpi_video_device_EDID(video_device, &buffer,
1401 length);
1402 if (ACPI_FAILURE(status) || !buffer ||
1403 buffer->type != ACPI_TYPE_BUFFER) {
1404 continue;
1405 }
1406 }
1407
1408 *edid = buffer->buffer.pointer;
1409 return length;
1410 }
1411
1412 return -ENODEV;
1413}
1414EXPORT_SYMBOL(acpi_video_get_edid);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static int
Len Brown4be44fc2005-08-05 00:44:28 -04001417acpi_video_bus_get_devices(struct acpi_video_bus *video,
1418 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Len Brown4be44fc2005-08-05 00:44:28 -04001420 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001421 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 acpi_video_device_enumerate(video);
1424
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001425 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 status = acpi_video_bus_get_one_device(dev, video);
1428 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001429 printk(KERN_WARNING PREFIX
Andi Kleencfa806f2010-07-20 15:18:36 -07001430 "Cant attach device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 continue;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001434 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Len Brown4be44fc2005-08-05 00:44:28 -04001437static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Karol Kozimor031ec772005-07-30 04:18:00 -04001439 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001442 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Patrick Mochel90130262006-05-19 16:54:48 -04001444 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001445 ACPI_DEVICE_NOTIFY,
1446 acpi_video_device_notify);
Andi Kleencfa806f2010-07-20 15:18:36 -07001447 if (ACPI_FAILURE(status)) {
1448 printk(KERN_WARNING PREFIX
1449 "Cant remove video notify handler\n");
1450 }
Keith Packarde29b3ee2009-08-06 15:57:54 -07001451 if (device->backlight) {
1452 sysfs_remove_link(&device->backlight->dev.kobj, "device");
1453 backlight_device_unregister(device->backlight);
1454 device->backlight = NULL;
1455 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001456 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08001457 sysfs_remove_link(&device->dev->dev.kobj,
1458 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001459 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08001460 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001461 thermal_cooling_device_unregister(device->cooling_dev);
1462 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08001463 }
Luming Yu23b0f012007-05-09 21:07:05 +08001464 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001465
Patrick Mocheld550d982006-06-27 00:41:40 -04001466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Len Brown4be44fc2005-08-05 00:44:28 -04001469static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Len Brown4be44fc2005-08-05 00:44:28 -04001471 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001472 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001474 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001476 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001478 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001479 if (ACPI_FAILURE(status))
1480 printk(KERN_WARNING PREFIX
1481 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001483 if (dev->brightness) {
1484 kfree(dev->brightness->levels);
1485 kfree(dev->brightness);
1486 }
1487 list_del(&dev->entry);
1488 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001491 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001492
Patrick Mocheld550d982006-06-27 00:41:40 -04001493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496/* acpi_video interface */
1497
Len Brown4be44fc2005-08-05 00:44:28 -04001498static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Zhang Ruia21101c2007-09-14 11:46:22 +08001500 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Len Brown4be44fc2005-08-05 00:44:28 -04001503static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 return acpi_video_bus_DOS(video, 0, 1);
1506}
1507
Bjorn Helgaas70155582009-04-07 15:37:11 +00001508static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Bjorn Helgaas70155582009-04-07 15:37:11 +00001510 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08001511 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001512 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001515 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Luming Yue9dab192007-08-20 18:23:53 +08001517 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001520 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 * most likely via hotkey. */
Len Brown14e04fb32007-08-23 15:20:26 -04001522 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001523 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525
Julius Volz98fb8fe2007-02-20 16:38:40 +01001526 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 * connector. */
1528 acpi_video_device_enumerate(video);
1529 acpi_video_device_rebind(video);
Len Brown14e04fb32007-08-23 15:20:26 -04001530 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001531 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533
Len Brown4be44fc2005-08-05 00:44:28 -04001534 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001535 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001536 keycode = KEY_SWITCHVIDEOMODE;
1537 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001538 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001539 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001540 keycode = KEY_VIDEO_NEXT;
1541 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001542 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb32007-08-23 15:20:26 -04001543 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001544 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
1546
1547 default:
1548 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001549 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
1551 }
1552
Zhang Rui7761f632008-01-25 14:48:12 +08001553 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001554
1555 if (keycode) {
1556 input_report_key(input, keycode, 1);
1557 input_sync(input);
1558 input_report_key(input, keycode, 0);
1559 input_sync(input);
1560 }
Luming Yue9dab192007-08-20 18:23:53 +08001561
Patrick Mocheld550d982006-06-27 00:41:40 -04001562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Len Brown4be44fc2005-08-05 00:44:28 -04001565static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001567 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001568 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001569 struct acpi_video_bus *bus;
1570 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05001571 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001576 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001577 bus = video_device->video;
1578 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001581 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001582 if (brightness_switch_enabled)
1583 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001584 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001585 keycode = KEY_BRIGHTNESS_CYCLE;
1586 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001587 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001588 if (brightness_switch_enabled)
1589 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001590 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001591 keycode = KEY_BRIGHTNESSUP;
1592 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001593 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001594 if (brightness_switch_enabled)
1595 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001596 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001597 keycode = KEY_BRIGHTNESSDOWN;
1598 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001599 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001600 if (brightness_switch_enabled)
1601 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001602 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001603 keycode = KEY_BRIGHTNESS_ZERO;
1604 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001605 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001606 if (brightness_switch_enabled)
1607 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb32007-08-23 15:20:26 -04001608 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001609 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611 default:
1612 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001613 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 break;
1615 }
Luming Yue9dab192007-08-20 18:23:53 +08001616
Zhang Rui7761f632008-01-25 14:48:12 +08001617 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05001618
1619 if (keycode) {
1620 input_report_key(input, keycode, 1);
1621 input_sync(input);
1622 input_report_key(input, keycode, 0);
1623 input_sync(input);
1624 }
Luming Yue9dab192007-08-20 18:23:53 +08001625
Patrick Mocheld550d982006-06-27 00:41:40 -04001626 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001629static int acpi_video_resume(struct notifier_block *nb,
1630 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08001631{
1632 struct acpi_video_bus *video;
1633 struct acpi_video_device *video_device;
1634 int i;
1635
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001636 switch (val) {
1637 case PM_HIBERNATION_PREPARE:
1638 case PM_SUSPEND_PREPARE:
1639 case PM_RESTORE_PREPARE:
1640 return NOTIFY_DONE;
1641 }
Matthew Garrett863c1492008-02-04 23:31:24 -08001642
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001643 video = container_of(nb, struct acpi_video_bus, pm_nb);
1644
1645 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08001646
1647 for (i = 0; i < video->attached_count; i++) {
1648 video_device = video->attached_array[i].bind_info;
1649 if (video_device && video_device->backlight)
1650 acpi_video_set_brightness(video_device->backlight);
1651 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001652
1653 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08001654}
1655
Zhang Ruic504f8c2009-12-30 15:59:23 +08001656static acpi_status
1657acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1658 void **return_value)
1659{
1660 struct acpi_device *device = context;
1661 struct acpi_device *sibling;
1662 int result;
1663
1664 if (handle == device->handle)
1665 return AE_CTRL_TERMINATE;
1666
1667 result = acpi_bus_get_device(handle, &sibling);
1668 if (result)
1669 return AE_OK;
1670
1671 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1672 return AE_ALREADY_EXISTS;
1673
1674 return AE_OK;
1675}
1676
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001677static int instance;
1678
Len Brown4be44fc2005-08-05 00:44:28 -04001679static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001681 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001682 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001683 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08001684 acpi_status status;
1685
1686 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1687 device->parent->handle, 1,
1688 acpi_video_bus_match, NULL,
1689 device, NULL);
1690 if (status == AE_ALREADY_EXISTS) {
1691 printk(KERN_WARNING FW_BUG
1692 "Duplicate ACPI video bus devices for the"
1693 " same VGA controller, please try module "
1694 "parameter \"video.allow_duplicates=1\""
1695 "if the current driver doesn't work.\n");
1696 if (!allow_duplicates)
1697 return -ENODEV;
1698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Burman Yan36bcbec2006-12-19 12:56:11 -08001700 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Zhang Ruie6d9da12007-08-25 02:23:31 -04001704 /* a hack to fix the duplicate name "VID" problem on T61 */
1705 if (!strcmp(device->pnp.bus_id, "VID")) {
1706 if (instance)
1707 device->pnp.bus_id[3] = '0' + instance;
1708 instance ++;
1709 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05001710 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1711 if (!strcmp(device->pnp.bus_id, "VGA")) {
1712 if (instance)
1713 device->pnp.bus_id[3] = '0' + instance;
1714 instance++;
1715 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04001716
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001717 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1719 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001720 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001723 error = acpi_video_bus_check(video);
1724 if (error)
1725 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001727 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 INIT_LIST_HEAD(&video->video_device_list);
1729
1730 acpi_video_bus_get_devices(video, device);
1731 acpi_video_bus_start_devices(video);
1732
Luming Yue9dab192007-08-20 18:23:53 +08001733 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001734 if (!input) {
1735 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00001736 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001737 }
Luming Yue9dab192007-08-20 18:23:53 +08001738
1739 snprintf(video->phys, sizeof(video->phys),
1740 "%s/video/input0", acpi_device_hid(video->device));
1741
1742 input->name = acpi_device_name(video->device);
1743 input->phys = video->phys;
1744 input->id.bustype = BUS_HOST;
1745 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05001746 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001747 input->evbit[0] = BIT(EV_KEY);
1748 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1749 set_bit(KEY_VIDEO_NEXT, input->keybit);
1750 set_bit(KEY_VIDEO_PREV, input->keybit);
1751 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1752 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1753 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1754 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1755 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08001756
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001757 error = input_register_device(input);
1758 if (error)
1759 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001762 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1763 video->flags.multihead ? "yes" : "no",
1764 video->flags.rom ? "yes" : "no",
1765 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001767 video->pm_nb.notifier_call = acpi_video_resume;
1768 video->pm_nb.priority = 0;
1769 register_pm_notifier(&video->pm_nb);
1770
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001773 err_free_input_dev:
1774 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001775 err_stop_video:
1776 acpi_video_bus_stop_devices(video);
1777 acpi_video_bus_put_devices(video);
1778 kfree(video->attached_array);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001779 err_free_video:
1780 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001781 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001782
1783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Len Brown4be44fc2005-08-05 00:44:28 -04001786static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Len Brown4be44fc2005-08-05 00:44:28 -04001788 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001792 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001794 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001796 unregister_pm_notifier(&video->pm_nb);
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 acpi_video_bus_put_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Luming Yue9dab192007-08-20 18:23:53 +08001801 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001802 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 kfree(video);
1804
Patrick Mocheld550d982006-06-27 00:41:40 -04001805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Matthew Garrett74a365b2009-03-19 21:35:39 +00001808static int __init intel_opregion_present(void)
1809{
1810#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1811 struct pci_dev *dev = NULL;
1812 u32 address;
1813
1814 for_each_pci_dev(dev) {
1815 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1816 continue;
1817 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1818 continue;
1819 pci_read_config_dword(dev, 0xfc, &address);
1820 if (!address)
1821 continue;
1822 return 1;
1823 }
1824#endif
1825 return 0;
1826}
1827
1828int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Len Brown4be44fc2005-08-05 00:44:28 -04001830 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08001831 if (register_count) {
1832 /*
1833 * if the function of acpi_video_register is already called,
1834 * don't register the acpi_vide_bus again and return no error.
1835 */
1836 return 0;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 result = acpi_bus_register_driver(&acpi_video_bus);
Zhang Rui39fe3942010-10-08 13:55:11 +08001840 if (result < 0)
Patrick Mocheld550d982006-06-27 00:41:40 -04001841 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Zhao Yakui86e437f2009-06-16 11:23:13 +08001843 /*
1844 * When the acpi_video_bus is loaded successfully, increase
1845 * the counter reference.
1846 */
1847 register_count = 1;
1848
Patrick Mocheld550d982006-06-27 00:41:40 -04001849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
Matthew Garrett74a365b2009-03-19 21:35:39 +00001851EXPORT_SYMBOL(acpi_video_register);
1852
Zhao Yakui86e437f2009-06-16 11:23:13 +08001853void acpi_video_unregister(void)
1854{
1855 if (!register_count) {
1856 /*
1857 * If the acpi video bus is already unloaded, don't
1858 * unload it again and return directly.
1859 */
1860 return;
1861 }
1862 acpi_bus_unregister_driver(&acpi_video_bus);
1863
Zhao Yakui86e437f2009-06-16 11:23:13 +08001864 register_count = 0;
1865
1866 return;
1867}
1868EXPORT_SYMBOL(acpi_video_unregister);
1869
Matthew Garrett74a365b2009-03-19 21:35:39 +00001870/*
1871 * This is kind of nasty. Hardware using Intel chipsets may require
1872 * the video opregion code to be run first in order to initialise
1873 * state before any ACPI video calls are made. To handle this we defer
1874 * registration of the video class until the opregion code has run.
1875 */
1876
1877static int __init acpi_video_init(void)
1878{
Zhang Rui45cb50e2009-04-24 12:13:18 -04001879 dmi_check_system(video_dmi_table);
1880
Matthew Garrett74a365b2009-03-19 21:35:39 +00001881 if (intel_opregion_present())
1882 return 0;
1883
1884 return acpi_video_register();
1885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Zhao Yakui86e437f2009-06-16 11:23:13 +08001887static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Zhao Yakui86e437f2009-06-16 11:23:13 +08001889 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Patrick Mocheld550d982006-06-27 00:41:40 -04001891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894module_init(acpi_video_init);
1895module_exit(acpi_video_exit);