blob: a0c93b3214827a69bd4ada20fb5742b4dbbfc381 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
Luming Yue9dab192007-08-20 18:23:53 +080035#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080036#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080037#include <linux/thermal.h>
Luming Yu23b0f012007-05-09 21:07:05 +080038#include <linux/video_output.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050039#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000040#include <linux/pci.h>
41#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040044#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020047#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Len Browna192a952009-07-28 16:45:54 -040049#define PREFIX "ACPI: "
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_VIDEO_BUS_NAME "Video Bus"
53#define ACPI_VIDEO_DEVICE_NAME "Video Device"
54#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
55#define ACPI_VIDEO_NOTIFY_PROBE 0x81
56#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
57#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
58#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
59
Thomas Tuttlef4715182006-12-19 12:56:14 -080060#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
61#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
62#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
63#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
64#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Yu Luming2f3d0002006-11-11 02:40:34 +080066#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Rui Zhang82cae992007-01-03 23:40:53 -050068#define ACPI_VIDEO_DISPLAY_CRT 1
69#define ACPI_VIDEO_DISPLAY_TV 2
70#define ACPI_VIDEO_DISPLAY_DVI 3
71#define ACPI_VIDEO_DISPLAY_LCD 4
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050074ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Len Brownf52fd662007-02-12 22:42:12 -050076MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050077MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078MODULE_LICENSE("GPL");
79
Zhang Rui8a681a42008-01-25 14:47:49 +080080static int brightness_switch_enabled = 1;
81module_param(brightness_switch_enabled, bool, 0644);
82
Zhang Ruic504f8c2009-12-30 15:59:23 +080083/*
84 * By default, we don't allow duplicate ACPI video bus devices
85 * under the same VGA controller
86 */
87static int allow_duplicates;
88module_param(allow_duplicates, bool, 0644);
89
Zhao Yakui86e437f2009-06-16 11:23:13 +080090static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040091static int acpi_video_bus_add(struct acpi_device *device);
92static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000093static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Renninger1ba90e32007-07-23 14:44:41 +020095static const struct acpi_device_id video_device_ids[] = {
96 {ACPI_VIDEO_HID, 0},
97 {"", 0},
98};
99MODULE_DEVICE_TABLE(acpi, video_device_ids);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500102 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200104 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .ops = {
106 .add = acpi_video_bus_add,
107 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000108 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400113 u8 multihead:1; /* can switch video heads */
114 u8 rom:1; /* can retrieve a video rom */
115 u8 post:1; /* can configure the head to */
116 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u8 _DOS:1; /*Enable/Disable output switching */
121 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
122 u8 _ROM:1; /*Get ROM Data */
123 u8 _GPD:1; /*Get POST Device */
124 u8 _SPD:1; /*Set POST Device */
125 u8 _VPO:1; /*Video POST Options */
126 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129struct acpi_video_device_attrib {
130 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100131 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400132 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100133 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400134 u32 bios_can_detect:1; /*BIOS can detect the device */
135 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
136 the VGA device. */
137 u32 pipe_id:3; /*For VGA multiple-head devices. */
138 u32 reserved:10; /*Must be 0 */
139 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142struct acpi_video_enumerated_device {
143 union {
144 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } value;
147 struct acpi_video_device *bind_info;
148};
149
150struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400151 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 u8 attached_count;
155 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400157 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500158 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400159 struct proc_dir_entry *dir;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* bus */
216static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100217static const struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700218 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 .open = acpi_video_bus_info_open_fs,
220 .read = seq_read,
221 .llseek = seq_lseek,
222 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
225static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100226static const struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700227 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 .open = acpi_video_bus_ROM_open_fs,
229 .read = seq_read,
230 .llseek = seq_lseek,
231 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
235 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100236static const struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700237 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400238 .open = acpi_video_bus_POST_info_open_fs,
239 .read = seq_read,
240 .llseek = seq_lseek,
241 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
244static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400245static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100246 const char __user *buffer, size_t count, loff_t *data);
247static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700248 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400249 .open = acpi_video_bus_POST_open_fs,
250 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100251 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 .llseek = seq_lseek,
253 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400257static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100258 const char __user *buffer, size_t count, loff_t *data);
259static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700260 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400261 .open = acpi_video_bus_DOS_open_fs,
262 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100263 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
268/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400269static int acpi_video_device_info_open_fs(struct inode *inode,
270 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100271static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700272 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400273 .open = acpi_video_device_info_open_fs,
274 .read = seq_read,
275 .llseek = seq_lseek,
276 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Len Brown4be44fc2005-08-05 00:44:28 -0400279static int acpi_video_device_state_open_fs(struct inode *inode,
280 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400281static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100282 const char __user *buffer, size_t count, loff_t *data);
283static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700284 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400285 .open = acpi_video_device_state_open_fs,
286 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100287 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400288 .llseek = seq_lseek,
289 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
Len Brown4be44fc2005-08-05 00:44:28 -0400292static int acpi_video_device_brightness_open_fs(struct inode *inode,
293 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400294static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100295 const char __user *buffer, size_t count, loff_t *data);
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700296static const struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700297 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400298 .open = acpi_video_device_brightness_open_fs,
299 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100300 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400301 .llseek = seq_lseek,
302 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Len Brown4be44fc2005-08-05 00:44:28 -0400305static int acpi_video_device_EDID_open_fs(struct inode *inode,
306 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100307static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700308 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400309 .open = acpi_video_device_EDID_open_fs,
310 .read = seq_read,
311 .llseek = seq_lseek,
312 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100315static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 "motherboard VGA device",
317 "PCI VGA device",
318 "AGP VGA device",
319 "UNKNOWN",
320};
321
Len Brown4be44fc2005-08-05 00:44:28 -0400322static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
323static void acpi_video_device_rebind(struct acpi_video_bus *video);
324static void acpi_video_device_bind(struct acpi_video_bus *video,
325 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800327static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
328 int level);
329static int acpi_video_device_lcd_get_level_current(
330 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500331 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400332static int acpi_video_get_next_level(struct acpi_video_device *device,
333 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800334static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400335 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800336static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400337 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800338static int acpi_video_output_get(struct output_device *od);
339static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Yu Luming2f3d0002006-11-11 02:40:34 +0800341/*backlight device sysfs support*/
342static int acpi_video_get_brightness(struct backlight_device *bd)
343{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400344 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000345 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800346 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100347 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800348
Matthew Garrett70287db2010-02-16 16:53:50 -0500349 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800350 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000351 for (i = 2; i < vd->brightness->count; i++) {
352 if (vd->brightness->levels[i] == cur_level)
353 /* The first two entries are special - see page 575
354 of the ACPI spec 3.0 */
355 return i-2;
356 }
357 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800358}
359
360static int acpi_video_set_brightness(struct backlight_device *bd)
361{
Zhang Rui24450c72009-03-18 16:27:10 +0800362 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800363 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100364 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800365
366 return acpi_video_device_lcd_set_level(vd,
367 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800368}
369
Richard Purdie599a52d2007-02-10 23:07:48 +0000370static struct backlight_ops acpi_backlight_ops = {
371 .get_brightness = acpi_video_get_brightness,
372 .update_status = acpi_video_set_brightness,
373};
374
Luming Yu23b0f012007-05-09 21:07:05 +0800375/*video output device sysfs support*/
376static int acpi_video_output_get(struct output_device *od)
377{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400378 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800379 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700380 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800381 acpi_video_device_get_state(vd, &state);
382 return (int)state;
383}
384
385static int acpi_video_output_set(struct output_device *od)
386{
387 unsigned long state = od->request_state;
388 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700389 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800390 return acpi_video_device_set_state(vd, state);
391}
392
393static struct output_properties acpi_output_properties = {
394 .set_state = acpi_video_output_set,
395 .get_status = acpi_video_output_get,
396};
Zhang Rui702ed512008-01-17 15:51:22 +0800397
398
399/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400400static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000401 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800402{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400403 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800404 struct acpi_video_device *video = acpi_driver_data(device);
405
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000406 *state = video->brightness->count - 3;
407 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800408}
409
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400410static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000411 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800412{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400413 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800414 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400415 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000416 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800417
Matthew Garrett70287db2010-02-16 16:53:50 -0500418 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800419 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000420 for (offset = 2; offset < video->brightness->count; offset++)
421 if (level == video->brightness->levels[offset]) {
422 *state = video->brightness->count - offset - 1;
423 return 0;
424 }
Zhang Rui702ed512008-01-17 15:51:22 +0800425
426 return -EINVAL;
427}
428
429static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400430video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800431{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400432 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800433 struct acpi_video_device *video = acpi_driver_data(device);
434 int level;
435
436 if ( state >= video->brightness->count - 2)
437 return -EINVAL;
438
439 state = video->brightness->count - state;
440 level = video->brightness->levels[state -1];
441 return acpi_video_device_lcd_set_level(video, level);
442}
443
444static struct thermal_cooling_device_ops video_cooling_ops = {
445 .get_max_state = video_get_max_state,
446 .get_cur_state = video_get_cur_state,
447 .set_cur_state = video_set_cur_state,
448};
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/* --------------------------------------------------------------------------
451 Video Management
452 -------------------------------------------------------------------------- */
453
454/* device */
455
456static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400457acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Len Brown4be44fc2005-08-05 00:44:28 -0400459 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400460
461 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static int
Len Brown4be44fc2005-08-05 00:44:28 -0400467acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400468 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Len Brown4be44fc2005-08-05 00:44:28 -0400470 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Patrick Mochel90130262006-05-19 16:54:48 -0400472 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static int
Len Brown4be44fc2005-08-05 00:44:28 -0400478acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Len Brown4be44fc2005-08-05 00:44:28 -0400480 int status;
481 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
482 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400483 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400487 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static int
Len Brown4be44fc2005-08-05 00:44:28 -0400493acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
494 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Len Brown4be44fc2005-08-05 00:44:28 -0400496 int status;
497 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
498 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 *levels = NULL;
502
Patrick Mochel90130262006-05-19 16:54:48 -0400503 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400505 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400506 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500507 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400508 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 status = -EFAULT;
510 goto err;
511 }
512
513 *levels = obj;
514
Patrick Mocheld550d982006-06-27 00:41:40 -0400515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Len Brown4be44fc2005-08-05 00:44:28 -0400517 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800518 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Patrick Mocheld550d982006-06-27 00:41:40 -0400520 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523static int
Len Brown4be44fc2005-08-05 00:44:28 -0400524acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Zhang Rui24450c72009-03-18 16:27:10 +0800526 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400527 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
528 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800529 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Zhang Rui24450c72009-03-18 16:27:10 +0800533 status = acpi_evaluate_object(device->dev->handle, "_BCM",
534 &args, NULL);
535 if (ACPI_FAILURE(status)) {
536 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
537 return -EIO;
538 }
539
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400540 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800541 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800542 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800543 if (device->backlight)
544 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800545 return 0;
546 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800547
Zhang Rui24450c72009-03-18 16:27:10 +0800548 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Zhang Rui45cb50e2009-04-24 12:13:18 -0400552/*
553 * For some buggy _BQC methods, we need to add a constant value to
554 * the _BQC return value to get the actual current brightness level
555 */
556
557static int bqc_offset_aml_bug_workaround;
558static int __init video_set_bqc_offset(const struct dmi_system_id *d)
559{
560 bqc_offset_aml_bug_workaround = 9;
561 return 0;
562}
563
564static struct dmi_system_id video_dmi_table[] __initdata = {
565 /*
566 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
567 */
568 {
569 .callback = video_set_bqc_offset,
570 .ident = "Acer Aspire 5720",
571 .matches = {
572 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
573 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
574 },
575 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400576 {
577 .callback = video_set_bqc_offset,
578 .ident = "Acer Aspire 5710Z",
579 .matches = {
580 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
581 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
582 },
583 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400584 {
585 .callback = video_set_bqc_offset,
586 .ident = "eMachines E510",
587 .matches = {
588 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
589 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
590 },
591 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400592 {
593 .callback = video_set_bqc_offset,
594 .ident = "Acer Aspire 5315",
595 .matches = {
596 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
597 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
598 },
599 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800600 {
601 .callback = video_set_bqc_offset,
602 .ident = "Acer Aspire 7720",
603 .matches = {
604 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
605 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
606 },
607 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400608 {}
609};
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611static int
Len Brown4be44fc2005-08-05 00:44:28 -0400612acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500613 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Zhang Ruic8890f92009-03-18 16:27:08 +0800615 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800616 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800617
Zhang Ruic60d6382009-03-18 16:27:18 +0800618 if (device->cap._BQC || device->cap._BCQ) {
619 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
620
621 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800622 NULL, level);
623 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800624 if (device->brightness->flags._BQC_use_index) {
625 if (device->brightness->flags._BCL_reversed)
626 *level = device->brightness->count
627 - 3 - (*level);
628 *level = device->brightness->levels[*level + 2];
629
630 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400631 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800632 for (i = 2; i < device->brightness->count; i++)
633 if (device->brightness->levels[i] == *level) {
634 device->brightness->curr = *level;
635 return 0;
636 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500637 if (!init) {
638 /*
639 * BQC returned an invalid level.
640 * Stop using it.
641 */
642 ACPI_WARNING((AE_INFO,
643 "%s returned an invalid level",
644 buf));
645 device->cap._BQC = device->cap._BCQ = 0;
646 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800647 } else {
648 /* Fixme:
649 * should we return an error or ignore this failure?
650 * dev->brightness->curr is a cached value which stores
651 * the correct current backlight level in most cases.
652 * ACPI video backlight still works w/ buggy _BQC.
653 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
654 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800655 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
656 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800657 }
658 }
659
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400660 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static int
Len Brown4be44fc2005-08-05 00:44:28 -0400665acpi_video_device_EDID(struct acpi_video_device *device,
666 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Len Brown4be44fc2005-08-05 00:44:28 -0400668 int status;
669 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
670 union acpi_object *obj;
671 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
672 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 *edid = NULL;
676
677 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (length == 128)
680 arg0.integer.value = 1;
681 else if (length == 256)
682 arg0.integer.value = 2;
683 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick Mochel90130262006-05-19 16:54:48 -0400686 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200690 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (obj && obj->type == ACPI_TYPE_BUFFER)
693 *edid = obj;
694 else {
Len Brown64684632006-06-26 23:41:38 -0400695 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 status = -EFAULT;
697 kfree(obj);
698 }
699
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/* bus */
704
705static int
Len Brown4be44fc2005-08-05 00:44:28 -0400706acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Len Brown4be44fc2005-08-05 00:44:28 -0400708 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400709 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400710 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
711 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 arg0.integer.value = option;
715
Patrick Mochel90130262006-05-19 16:54:48 -0400716 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400718 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400724acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 int status;
727
Patrick Mochel90130262006-05-19 16:54:48 -0400728 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733static int
Len Brown4be44fc2005-08-05 00:44:28 -0400734acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400735 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Len Brown4be44fc2005-08-05 00:44:28 -0400737 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Patrick Mochel90130262006-05-19 16:54:48 -0400739 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *options &= 3;
741
Patrick Mocheld550d982006-06-27 00:41:40 -0400742 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
746 * Arg:
747 * video : video bus device pointer
748 * bios_flag :
749 * 0. The system BIOS should NOT automatically switch(toggle)
750 * the active display output.
751 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100752 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * 2. The _DGS value should be locked.
754 * 3. The system BIOS should not automatically switch (toggle) the
755 * active display output, but instead generate the display switch
756 * event notify code.
757 * lcd_flag :
758 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100759 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100761 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * Return Value:
763 * -1 wrong arg.
764 */
765
766static int
Len Brown4be44fc2005-08-05 00:44:28 -0400767acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Lin Ming439913f2010-01-28 10:53:19 +0800769 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400770 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
771 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Len Brown4be44fc2005-08-05 00:44:28 -0400774 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 status = -1;
776 goto Failed;
777 }
778 arg0.integer.value = (lcd_flag << 2) | bios_flag;
779 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400780 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Len Brown4be44fc2005-08-05 00:44:28 -0400782 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500787 * Simple comparison function used to sort backlight levels.
788 */
789
790static int
791acpi_video_cmp_level(const void *a, const void *b)
792{
793 return *(int *)a - *(int *)b;
794}
795
796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * Arg:
798 * device : video output device (LCD, CRT, ..)
799 *
800 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100801 * Maximum brightness level
802 *
803 * Allocate and initialize device->brightness.
804 */
805
806static int
807acpi_video_init_brightness(struct acpi_video_device *device)
808{
809 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800810 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800811 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100812 union acpi_object *o;
813 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800814 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100815
816 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
817 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
818 "LCD brightness level\n"));
819 goto out;
820 }
821
822 if (obj->package.count < 2)
823 goto out;
824
825 br = kzalloc(sizeof(*br), GFP_KERNEL);
826 if (!br) {
827 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800828 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100829 goto out;
830 }
831
Zhang Ruid32f6942009-03-18 16:27:12 +0800832 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100833 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800834 if (!br->levels) {
835 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100836 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800837 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100838
839 for (i = 0; i < obj->package.count; i++) {
840 o = (union acpi_object *)&obj->package.elements[i];
841 if (o->type != ACPI_TYPE_INTEGER) {
842 printk(KERN_ERR PREFIX "Invalid data\n");
843 continue;
844 }
845 br->levels[count] = (u32) o->integer.value;
846
847 if (br->levels[count] > max_level)
848 max_level = br->levels[count];
849 count++;
850 }
851
Zhang Ruid32f6942009-03-18 16:27:12 +0800852 /*
853 * some buggy BIOS don't export the levels
854 * when machine is on AC/Battery in _BCL package.
855 * In this case, the first two elements in _BCL packages
856 * are also supported brightness levels that OS should take care of.
857 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800858 for (i = 2; i < count; i++) {
859 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800860 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800861 if (br->levels[i] == br->levels[1])
862 level_ac_battery++;
863 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500864
Zhang Ruid32f6942009-03-18 16:27:12 +0800865 if (level_ac_battery < 2) {
866 level_ac_battery = 2 - level_ac_battery;
867 br->flags._BCL_no_ac_battery_levels = 1;
868 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
869 br->levels[i] = br->levels[i - level_ac_battery];
870 count += level_ac_battery;
871 } else if (level_ac_battery > 2)
872 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
873
Zhang Ruid80fb992009-03-18 16:27:14 +0800874 /* Check if the _BCL package is in a reversed order */
875 if (max_level == br->levels[2]) {
876 br->flags._BCL_reversed = 1;
877 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
878 acpi_video_cmp_level, NULL);
879 } else if (max_level != br->levels[count - 1])
880 ACPI_ERROR((AE_INFO,
881 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100882
883 br->count = count;
884 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800885
886 /* Check the input/output of _BQC/_BCL/_BCM */
887 if ((max_level < 100) && (max_level <= (count - 2)))
888 br->flags._BCL_use_index = 1;
889
890 /*
891 * _BCM is always consistent with _BCL,
892 * at least for all the laptops we have ever seen.
893 */
894 br->flags._BCM_use_index = br->flags._BCL_use_index;
895
896 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400897 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800898
899 if (!device->cap._BQC)
900 goto set_level;
901
Matthew Garrett70287db2010-02-16 16:53:50 -0500902 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800903 if (result)
904 goto out_free_levels;
905
Zhang Ruie047cca2009-04-09 14:24:35 +0800906 /*
907 * Set the level to maximum and check if _BQC uses indexed value
908 */
909 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800910 if (result)
911 goto out_free_levels;
912
Matthew Garrett70287db2010-02-16 16:53:50 -0500913 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800914 if (result)
915 goto out_free_levels;
916
Zhang Ruie047cca2009-04-09 14:24:35 +0800917 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800918
Zhang Rui90c53ca2009-08-31 12:39:54 -0400919 if (!br->flags._BQC_use_index) {
920 /*
921 * Set the backlight to the initial state.
922 * On some buggy laptops, _BQC returns an uninitialized value
923 * when invoked for the first time, i.e. level_old is invalid.
924 * set the backlight to max_level in this case
925 */
926 for (i = 2; i < br->count; i++)
927 if (level_old == br->levels[i])
928 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800929 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400930 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800931
932 if (br->flags._BCL_reversed)
933 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400934 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800935
936set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400937 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800938 if (result)
939 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800940
Zhang Ruid32f6942009-03-18 16:27:12 +0800941 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
942 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100943 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800944 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100945
946out_free_levels:
947 kfree(br->levels);
948out_free:
949 kfree(br);
950out:
951 device->brightness = NULL;
952 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800953 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100954}
955
956/*
957 * Arg:
958 * device : video output device (LCD, CRT, ..)
959 *
960 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * None
962 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100963 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * device.
965 */
966
Len Brown4be44fc2005-08-05 00:44:28 -0400967static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Patrick Mochel90130262006-05-19 16:54:48 -0400971 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 device->cap._ADR = 1;
973 }
Patrick Mochel90130262006-05-19 16:54:48 -0400974 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400975 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Patrick Mochel90130262006-05-19 16:54:48 -0400977 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400978 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800980 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
981 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800982 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
983 &h_dummy1))) {
984 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
985 device->cap._BCQ = 1;
986 }
987
Patrick Mochel90130262006-05-19 16:54:48 -0400988 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400989 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Patrick Mochel90130262006-05-19 16:54:48 -0400991 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 device->cap._DCS = 1;
993 }
Patrick Mochel90130262006-05-19 16:54:48 -0400994 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 device->cap._DGS = 1;
996 }
Patrick Mochel90130262006-05-19 16:54:48 -0400997 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 device->cap._DSS = 1;
999 }
1000
Zhang Rui1a7c6182009-03-18 16:27:16 +08001001 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001002 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +08001003 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +08001004 static int count = 0;
1005 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +08001006
1007 result = acpi_video_init_brightness(device);
1008 if (result)
1009 return;
Yu Luming2f3d0002006-11-11 02:40:34 +08001010 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1011 if (!name)
1012 return;
1013
Yu Luming2f3d0002006-11-11 02:40:34 +08001014 sprintf(name, "acpi_video%d", count++);
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001015 memset(&props, 0, sizeof(struct backlight_properties));
1016 props.max_brightness = device->brightness->count - 3;
1017 device->backlight = backlight_device_register(name, NULL, device,
1018 &acpi_backlight_ops,
1019 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +08001020 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +08001021 if (IS_ERR(device->backlight))
1022 return;
Zhang Rui702ed512008-01-17 15:51:22 +08001023
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001024 /*
1025 * Save current brightness level in case we have to restore it
1026 * before acpi_video_device_lcd_set_level() is called next time.
1027 */
1028 device->backlight->props.brightness =
1029 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001030
Zhang Rui056c3082009-06-22 11:31:14 +08001031 result = sysfs_create_link(&device->backlight->dev.kobj,
1032 &device->dev->dev.kobj, "device");
1033 if (result)
1034 printk(KERN_ERR PREFIX "Create sysfs link\n");
1035
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001036 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001037 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001038 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001039 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001040 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001041 * free it.
1042 * Also, why the hell we are returning early and
1043 * not attempt to register video output if cooling
1044 * device registration failed?
1045 * -- dtor
1046 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001047 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001048 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001049 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001050
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001051 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001052 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001053 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001054 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001055 "thermal_cooling");
1056 if (result)
1057 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001058 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001059 &device->dev->dev.kobj, "device");
1060 if (result)
1061 printk(KERN_ERR PREFIX "Create sysfs link\n");
1062
Yu Luming2f3d0002006-11-11 02:40:34 +08001063 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001064
1065 if (acpi_video_display_switch_support()) {
1066
1067 if (device->cap._DCS && device->cap._DSS) {
1068 static int count;
1069 char *name;
1070 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1071 if (!name)
1072 return;
1073 sprintf(name, "acpi_video%d", count++);
1074 device->output_dev = video_output_register(name,
1075 NULL, device, &acpi_output_properties);
1076 kfree(name);
1077 }
Luming Yu23b0f012007-05-09 21:07:05 +08001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081/*
1082 * Arg:
1083 * device : video output device (VGA)
1084 *
1085 * Return Value:
1086 * None
1087 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001088 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 */
1090
Len Brown4be44fc2005-08-05 00:44:28 -04001091static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Len Brown4be44fc2005-08-05 00:44:28 -04001093 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Patrick Mochel90130262006-05-19 16:54:48 -04001095 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 video->cap._DOS = 1;
1097 }
Patrick Mochel90130262006-05-19 16:54:48 -04001098 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 video->cap._DOD = 1;
1100 }
Patrick Mochel90130262006-05-19 16:54:48 -04001101 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 video->cap._ROM = 1;
1103 }
Patrick Mochel90130262006-05-19 16:54:48 -04001104 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 video->cap._GPD = 1;
1106 }
Patrick Mochel90130262006-05-19 16:54:48 -04001107 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 video->cap._SPD = 1;
1109 }
Patrick Mochel90130262006-05-19 16:54:48 -04001110 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 video->cap._VPO = 1;
1112 }
1113}
1114
1115/*
1116 * Check whether the video bus device has required AML method to
1117 * support the desired features
1118 */
1119
Len Brown4be44fc2005-08-05 00:44:28 -04001120static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Len Brown4be44fc2005-08-05 00:44:28 -04001122 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001123 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001128 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001129 if (!dev)
1130 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001131 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* Since there is no HID, CID and so on for VGA driver, we have
1134 * to check well known required nodes.
1135 */
1136
Julius Volz98fb8fe2007-02-20 16:38:40 +01001137 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001138 if (video->cap._DOS || video->cap._DOD) {
1139 if (!video->cap._DOS) {
1140 printk(KERN_WARNING FW_BUG
1141 "ACPI(%s) defines _DOD but not _DOS\n",
1142 acpi_device_bid(video->device));
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 video->flags.multihead = 1;
1145 status = 0;
1146 }
1147
Julius Volz98fb8fe2007-02-20 16:38:40 +01001148 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001149 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 video->flags.rom = 1;
1151 status = 0;
1152 }
1153
Julius Volz98fb8fe2007-02-20 16:38:40 +01001154 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001155 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 video->flags.post = 1;
1157 status = 0;
1158 }
1159
Patrick Mocheld550d982006-06-27 00:41:40 -04001160 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163/* --------------------------------------------------------------------------
1164 FS Interface (/proc)
1165 -------------------------------------------------------------------------- */
1166
Len Brown4be44fc2005-08-05 00:44:28 -04001167static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169/* video devices */
1170
Len Brown4be44fc2005-08-05 00:44:28 -04001171static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001173 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if (!dev)
1177 goto end;
1178
1179 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1180 seq_printf(seq, "type: ");
1181 if (dev->flags.crt)
1182 seq_printf(seq, "CRT\n");
1183 else if (dev->flags.lcd)
1184 seq_printf(seq, "LCD\n");
1185 else if (dev->flags.tvout)
1186 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001187 else if (dev->flags.dvi)
1188 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 else
1190 seq_printf(seq, "UNKNOWN\n");
1191
Len Brown4be44fc2005-08-05 00:44:28 -04001192 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Len Brown4be44fc2005-08-05 00:44:28 -04001194 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198static int
Len Brown4be44fc2005-08-05 00:44:28 -04001199acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 return single_open(file, acpi_video_device_info_seq_show,
1202 PDE(inode)->data);
1203}
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Len Brown4be44fc2005-08-05 00:44:28 -04001207 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001208 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001209 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (!dev)
1213 goto end;
1214
1215 status = acpi_video_device_get_state(dev, &state);
1216 seq_printf(seq, "state: ");
1217 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001218 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 else
1220 seq_printf(seq, "<not supported>\n");
1221
1222 status = acpi_video_device_query(dev, &state);
1223 seq_printf(seq, "query: ");
1224 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001225 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 else
1227 seq_printf(seq, "<not supported>\n");
1228
Len Brown4be44fc2005-08-05 00:44:28 -04001229 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int
Len Brown4be44fc2005-08-05 00:44:28 -04001234acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 return single_open(file, acpi_video_device_state_seq_show,
1237 PDE(inode)->data);
1238}
1239
1240static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001241acpi_video_device_write_state(struct file *file,
1242 const char __user * buffer,
1243 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Len Brown4be44fc2005-08-05 00:44:28 -04001245 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001246 struct seq_file *m = file->private_data;
1247 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001248 char str[12] = { 0 };
1249 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001252 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001253 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001256 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 str[count] = 0;
1259 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001260 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 status = acpi_video_device_set_state(dev, state);
1263
1264 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Patrick Mocheld550d982006-06-27 00:41:40 -04001267 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270static int
Len Brown4be44fc2005-08-05 00:44:28 -04001271acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001273 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001274 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (!dev || !dev->brightness) {
1278 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001283 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 seq_printf(seq, " %d", dev->brightness->levels[i]);
1285 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1286
Patrick Mocheld550d982006-06-27 00:41:40 -04001287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290static int
Len Brown4be44fc2005-08-05 00:44:28 -04001291acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 return single_open(file, acpi_video_device_brightness_seq_show,
1294 PDE(inode)->data);
1295}
1296
1297static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001298acpi_video_device_write_brightness(struct file *file,
1299 const char __user * buffer,
1300 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001302 struct seq_file *m = file->private_data;
1303 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001304 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001305 unsigned int level = 0;
1306 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001309 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001313 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 str[count] = 0;
1316 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001319 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Julius Volz98fb8fe2007-02-20 16:38:40 +01001321 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001322 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001324 if (!acpi_video_device_lcd_set_level(dev, level))
1325 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
1327 }
1328
Zhang Rui24450c72009-03-18 16:27:10 +08001329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Len Brown4be44fc2005-08-05 00:44:28 -04001332static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001334 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001335 int status;
1336 int i;
1337 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 if (!dev)
1341 goto out;
1342
Len Brown4be44fc2005-08-05 00:44:28 -04001343 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001345 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347
1348 if (ACPI_FAILURE(status)) {
1349 goto out;
1350 }
1351
1352 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1353 for (i = 0; i < edid->buffer.length; i++)
1354 seq_putc(seq, edid->buffer.pointer[i]);
1355 }
1356
Len Brown4be44fc2005-08-05 00:44:28 -04001357 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!edid)
1359 seq_printf(seq, "<not supported>\n");
1360 else
1361 kfree(edid);
1362
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366static int
Len Brown4be44fc2005-08-05 00:44:28 -04001367acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 return single_open(file, acpi_video_device_EDID_seq_show,
1370 PDE(inode)->data);
1371}
1372
Len Brown4be44fc2005-08-05 00:44:28 -04001373static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001375 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct acpi_video_device *vid_dev;
1377
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001378 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001380 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001382 device_dir = proc_mkdir(acpi_device_bid(device),
1383 vid_dev->video->dir);
1384 if (!device_dir)
1385 return -ENOMEM;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001388 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001389 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001391 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001394 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001395 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001396 &acpi_video_device_state_fops,
1397 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001399 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001402 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001403 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001404 &acpi_video_device_brightness_fops,
1405 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001407 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001410 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001411 &acpi_video_device_EDID_fops,
1412 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001414 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001416 acpi_device_dir(device) = device_dir;
1417
Patrick Mocheld550d982006-06-27 00:41:40 -04001418 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001419
1420 err_remove_brightness:
1421 remove_proc_entry("brightness", device_dir);
1422 err_remove_state:
1423 remove_proc_entry("state", device_dir);
1424 err_remove_info:
1425 remove_proc_entry("info", device_dir);
1426 err_remove_dir:
1427 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Len Brown4be44fc2005-08-05 00:44:28 -04001431static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001434 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001436 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001438 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001440 device_dir = acpi_device_dir(device);
1441 if (device_dir) {
1442 remove_proc_entry("info", device_dir);
1443 remove_proc_entry("state", device_dir);
1444 remove_proc_entry("brightness", device_dir);
1445 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001446 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 acpi_device_dir(device) = NULL;
1448 }
1449
Patrick Mocheld550d982006-06-27 00:41:40 -04001450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001454static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001456 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if (!video)
1460 goto end;
1461
1462 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001463 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001465 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001467 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Len Brown4be44fc2005-08-05 00:44:28 -04001469 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Len Brown4be44fc2005-08-05 00:44:28 -04001473static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Len Brown4be44fc2005-08-05 00:44:28 -04001475 return single_open(file, acpi_video_bus_info_seq_show,
1476 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Len Brown4be44fc2005-08-05 00:44:28 -04001479static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001481 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (!video)
1485 goto end;
1486
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001487 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 seq_printf(seq, "<TODO>\n");
1489
Len Brown4be44fc2005-08-05 00:44:28 -04001490 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Len Brown4be44fc2005-08-05 00:44:28 -04001494static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1497}
1498
Len Brown4be44fc2005-08-05 00:44:28 -04001499static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001501 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001502 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001503 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 if (!video)
1507 goto end;
1508
1509 status = acpi_video_bus_POST_options(video, &options);
1510 if (ACPI_SUCCESS(status)) {
1511 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001512 printk(KERN_WARNING PREFIX
1513 "The motherboard VGA device is not listed as a possible POST device.\n");
1514 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001515 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Frank Seidel4d939152009-02-04 17:03:07 +01001517 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001518 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (options & 2)
1520 seq_printf(seq, " <PCI video>");
1521 if (options & 4)
1522 seq_printf(seq, " <AGP video>");
1523 seq_putc(seq, '\n');
1524 } else
1525 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001526 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530static int
Len Brown4be44fc2005-08-05 00:44:28 -04001531acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Len Brown4be44fc2005-08-05 00:44:28 -04001533 return single_open(file, acpi_video_bus_POST_info_seq_show,
1534 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Len Brown4be44fc2005-08-05 00:44:28 -04001537static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001539 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001540 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001541 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 if (!video)
1545 goto end;
1546
Len Brown4be44fc2005-08-05 00:44:28 -04001547 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (!ACPI_SUCCESS(status)) {
1549 seq_printf(seq, "<not supported>\n");
1550 goto end;
1551 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001552 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Len Brown4be44fc2005-08-05 00:44:28 -04001554 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Len Brown4be44fc2005-08-05 00:44:28 -04001558static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001560 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Len Brown4be44fc2005-08-05 00:44:28 -04001563 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Patrick Mocheld550d982006-06-27 00:41:40 -04001565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Len Brown4be44fc2005-08-05 00:44:28 -04001568static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Len Brown4be44fc2005-08-05 00:44:28 -04001570 return single_open(file, acpi_video_bus_POST_seq_show,
1571 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Len Brown4be44fc2005-08-05 00:44:28 -04001574static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1577}
1578
1579static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001580acpi_video_bus_write_POST(struct file *file,
1581 const char __user * buffer,
1582 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Len Brown4be44fc2005-08-05 00:44:28 -04001584 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001585 struct seq_file *m = file->private_data;
1586 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001587 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001588 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001591 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 status = acpi_video_bus_POST_options(video, &options);
1595 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001596 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001599 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 str[count] = 0;
1602 opt = strtoul(str, NULL, 0);
1603 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001604 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Julius Volz98fb8fe2007-02-20 16:38:40 +01001606 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 options |= 1;
1608
1609 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001610 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001612 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 }
1615
Patrick Mocheld550d982006-06-27 00:41:40 -04001616 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001620acpi_video_bus_write_DOS(struct file *file,
1621 const char __user * buffer,
1622 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Len Brown4be44fc2005-08-05 00:44:28 -04001624 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001625 struct seq_file *m = file->private_data;
1626 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001627 char str[12] = { 0 };
1628 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001631 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001632 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001635 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 str[count] = 0;
1638 opt = strtoul(str, NULL, 0);
1639 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001640 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Len Brown4be44fc2005-08-05 00:44:28 -04001642 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001645 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Patrick Mocheld550d982006-06-27 00:41:40 -04001647 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Len Brown4be44fc2005-08-05 00:44:28 -04001650static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001652 struct acpi_video_bus *video = acpi_driver_data(device);
1653 struct proc_dir_entry *device_dir;
1654 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001656 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1657 if (!device_dir)
1658 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001661 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001662 &acpi_video_bus_info_fops,
1663 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001665 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001668 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001669 &acpi_video_bus_ROM_fops,
1670 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001672 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001675 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001676 &acpi_video_bus_POST_info_fops,
1677 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001679 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001682 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001683 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001684 &acpi_video_bus_POST_fops,
1685 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001687 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001690 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001691 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001692 &acpi_video_bus_DOS_fops,
1693 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001695 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001697 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001698 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001699
1700 err_remove_post:
1701 remove_proc_entry("POST", device_dir);
1702 err_remove_post_info:
1703 remove_proc_entry("POST_info", device_dir);
1704 err_remove_rom:
1705 remove_proc_entry("ROM", device_dir);
1706 err_remove_info:
1707 remove_proc_entry("info", device_dir);
1708 err_remove_dir:
1709 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1710 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Len Brown4be44fc2005-08-05 00:44:28 -04001713static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001715 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001717 if (device_dir) {
1718 remove_proc_entry("info", device_dir);
1719 remove_proc_entry("ROM", device_dir);
1720 remove_proc_entry("POST_info", device_dir);
1721 remove_proc_entry("POST", device_dir);
1722 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001723 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 acpi_device_dir(device) = NULL;
1725 }
1726
Patrick Mocheld550d982006-06-27 00:41:40 -04001727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/* --------------------------------------------------------------------------
1731 Driver Interface
1732 -------------------------------------------------------------------------- */
1733
1734/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001735static struct acpi_video_device_attrib*
1736acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1737{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001738 struct acpi_video_enumerated_device *ids;
1739 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001740
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001741 for (i = 0; i < video->attached_count; i++) {
1742 ids = &video->attached_array[i];
1743 if ((ids->value.int_val & 0xffff) == device_id)
1744 return &ids->value.attrib;
1745 }
1746
Rui Zhang82cae992007-01-03 23:40:53 -05001747 return NULL;
1748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750static int
Len Brown4be44fc2005-08-05 00:44:28 -04001751acpi_video_bus_get_one_device(struct acpi_device *device,
1752 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001754 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001755 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001756 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001757 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Len Brown4be44fc2005-08-05 00:44:28 -04001762 status =
1763 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (ACPI_SUCCESS(status)) {
1765
Burman Yan36bcbec2006-12-19 12:56:11 -08001766 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001768 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1771 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001772 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 data->device_id = device_id;
1775 data->video = video;
1776 data->dev = device;
1777
Rui Zhang82cae992007-01-03 23:40:53 -05001778 attribute = acpi_video_get_device_attr(video, device_id);
1779
1780 if((attribute != NULL) && attribute->device_id_scheme) {
1781 switch (attribute->display_type) {
1782 case ACPI_VIDEO_DISPLAY_CRT:
1783 data->flags.crt = 1;
1784 break;
1785 case ACPI_VIDEO_DISPLAY_TV:
1786 data->flags.tvout = 1;
1787 break;
1788 case ACPI_VIDEO_DISPLAY_DVI:
1789 data->flags.dvi = 1;
1790 break;
1791 case ACPI_VIDEO_DISPLAY_LCD:
1792 data->flags.lcd = 1;
1793 break;
1794 default:
1795 data->flags.unknown = 1;
1796 break;
1797 }
1798 if(attribute->bios_can_detect)
1799 data->flags.bios = 1;
1800 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 acpi_video_device_bind(video, data);
1804 acpi_video_device_find_cap(data);
1805
Patrick Mochel90130262006-05-19 16:54:48 -04001806 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001807 ACPI_DEVICE_NOTIFY,
1808 acpi_video_device_notify,
1809 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001811 printk(KERN_ERR PREFIX
1812 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001813 if(data->brightness)
1814 kfree(data->brightness->levels);
1815 kfree(data->brightness);
1816 kfree(data);
1817 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001820 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001822 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 acpi_video_device_add_fs(device);
1825
Patrick Mocheld550d982006-06-27 00:41:40 -04001826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
Patrick Mocheld550d982006-06-27 00:41:40 -04001829 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832/*
1833 * Arg:
1834 * video : video bus device
1835 *
1836 * Return:
1837 * none
1838 *
1839 * Enumerate the video device list of the video bus,
1840 * bind the ids with the corresponding video devices
1841 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Len Brown4be44fc2005-08-05 00:44:28 -04001844static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001846 struct acpi_video_device *dev;
1847
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001848 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001849
1850 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001851 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001852
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001853 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856/*
1857 * Arg:
1858 * video : video bus device
1859 * device : video output device under the video
1860 * bus
1861 *
1862 * Return:
1863 * none
1864 *
1865 * Bind the ids with the corresponding video devices
1866 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869static void
Len Brown4be44fc2005-08-05 00:44:28 -04001870acpi_video_device_bind(struct acpi_video_bus *video,
1871 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001873 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001874 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001876 for (i = 0; i < video->attached_count; i++) {
1877 ids = &video->attached_array[i];
1878 if (device->device_id == (ids->value.int_val & 0xffff)) {
1879 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1881 }
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/*
1886 * Arg:
1887 * video : video bus device
1888 *
1889 * Return:
1890 * < 0 : error
1891 *
1892 * Call _DOD to enumerate all devices attached to display adapter
1893 *
Len Brown4be44fc2005-08-05 00:44:28 -04001894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1897{
Len Brown4be44fc2005-08-05 00:44:28 -04001898 int status;
1899 int count;
1900 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001901 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001902 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1903 union acpi_object *dod = NULL;
1904 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Patrick Mochel90130262006-05-19 16:54:48 -04001906 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001908 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001909 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001912 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001914 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 status = -EFAULT;
1916 goto out;
1917 }
1918
1919 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001920 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001922 active_list = kcalloc(1 + dod->package.count,
1923 sizeof(struct acpi_video_enumerated_device),
1924 GFP_KERNEL);
1925 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 status = -ENOMEM;
1927 goto out;
1928 }
1929
1930 count = 0;
1931 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001932 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001935 printk(KERN_ERR PREFIX
1936 "Invalid _DOD data in element %d\n", i);
1937 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001939
1940 active_list[count].value.int_val = obj->integer.value;
1941 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001942 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1943 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 count++;
1945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Jesper Juhl6044ec82005-11-07 01:01:32 -08001947 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001948
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001949 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001951
1952 out:
Len Brown02438d82006-06-30 03:19:10 -04001953 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001954 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Len Brown4be44fc2005-08-05 00:44:28 -04001957static int
1958acpi_video_get_next_level(struct acpi_video_device *device,
1959 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001961 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001962 max = max_below = 0;
1963 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001964 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001965 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001966 l = device->brightness->levels[i];
1967 if (abs(l - level_current) < abs(delta)) {
1968 delta = l - level_current;
1969 if (!delta)
1970 break;
1971 }
1972 }
1973 /* Ajust level_current to closest available level */
1974 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001975 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001976 l = device->brightness->levels[i];
1977 if (l < min)
1978 min = l;
1979 if (l > max)
1980 max = l;
1981 if (l < min_above && l > level_current)
1982 min_above = l;
1983 if (l > max_below && l < level_current)
1984 max_below = l;
1985 }
1986
1987 switch (event) {
1988 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1989 return (level_current < max) ? min_above : min;
1990 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1991 return (level_current < max) ? min_above : max;
1992 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1993 return (level_current > min) ? max_below : min;
1994 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1995 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1996 return 0;
1997 default:
1998 return level_current;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Zhang Ruic8890f92009-03-18 16:27:08 +08002002static int
Len Brown4be44fc2005-08-05 00:44:28 -04002003acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Matthew Wilcox27663c52008-10-10 02:22:59 -04002005 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08002006 int result = -EINVAL;
2007
Zhang Rui28c32e92009-07-13 10:33:24 +08002008 /* no warning message if acpi_backlight=vendor is used */
2009 if (!acpi_video_backlight_support())
2010 return 0;
2011
Julia Jomantaite469778c2008-06-23 22:50:42 +01002012 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08002013 goto out;
2014
2015 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05002016 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08002017 if (result)
2018 goto out;
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08002021
Zhang Rui24450c72009-03-18 16:27:10 +08002022 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08002023
Matthew Garrett36342742009-07-14 17:06:03 +01002024 if (!result)
2025 backlight_force_update(device->backlight,
2026 BACKLIGHT_UPDATE_HOTKEY);
2027
Zhang Ruic8890f92009-03-18 16:27:08 +08002028out:
2029 if (result)
2030 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2031
2032 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
2035static int
Len Brown4be44fc2005-08-05 00:44:28 -04002036acpi_video_bus_get_devices(struct acpi_video_bus *video,
2037 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Len Brown4be44fc2005-08-05 00:44:28 -04002039 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002040 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 acpi_video_device_enumerate(video);
2043
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002044 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 status = acpi_video_bus_get_one_device(dev, video);
2047 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002048 printk(KERN_WARNING PREFIX
2049 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 continue;
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002053 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Len Brown4be44fc2005-08-05 00:44:28 -04002056static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Karol Kozimor031ec772005-07-30 04:18:00 -04002058 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct acpi_video_bus *video;
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002063 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 video = device->video;
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 acpi_video_device_remove_fs(device->dev);
2068
Patrick Mochel90130262006-05-19 16:54:48 -04002069 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002070 ACPI_DEVICE_NOTIFY,
2071 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002072 if (device->backlight) {
2073 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2074 backlight_device_unregister(device->backlight);
2075 device->backlight = NULL;
2076 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002077 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002078 sysfs_remove_link(&device->dev->dev.kobj,
2079 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002080 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002081 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002082 thermal_cooling_device_unregister(device->cooling_dev);
2083 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002084 }
Luming Yu23b0f012007-05-09 21:07:05 +08002085 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002086
Patrick Mocheld550d982006-06-27 00:41:40 -04002087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Len Brown4be44fc2005-08-05 00:44:28 -04002090static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Len Brown4be44fc2005-08-05 00:44:28 -04002092 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002093 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002095 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002097 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002099 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002100 if (ACPI_FAILURE(status))
2101 printk(KERN_WARNING PREFIX
2102 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002104 if (dev->brightness) {
2105 kfree(dev->brightness->levels);
2106 kfree(dev->brightness);
2107 }
2108 list_del(&dev->entry);
2109 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002112 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002113
Patrick Mocheld550d982006-06-27 00:41:40 -04002114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
2117/* acpi_video interface */
2118
Len Brown4be44fc2005-08-05 00:44:28 -04002119static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Zhang Ruia21101c2007-09-14 11:46:22 +08002121 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Len Brown4be44fc2005-08-05 00:44:28 -04002124static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 return acpi_video_bus_DOS(video, 0, 1);
2127}
2128
Bjorn Helgaas70155582009-04-07 15:37:11 +00002129static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002131 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002132 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05002133 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Luming Yue9dab192007-08-20 18:23:53 +08002138 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002141 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002143 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002144 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 break;
2146
Julius Volz98fb8fe2007-02-20 16:38:40 +01002147 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 * connector. */
2149 acpi_video_device_enumerate(video);
2150 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002151 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002152 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 break;
2154
Len Brown4be44fc2005-08-05 00:44:28 -04002155 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002156 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002157 keycode = KEY_SWITCHVIDEOMODE;
2158 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002159 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002160 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002161 keycode = KEY_VIDEO_NEXT;
2162 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002163 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002164 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002165 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 break;
2167
2168 default:
2169 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002170 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 break;
2172 }
2173
Zhang Rui7761f632008-01-25 14:48:12 +08002174 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05002175
2176 if (keycode) {
2177 input_report_key(input, keycode, 1);
2178 input_sync(input);
2179 input_report_key(input, keycode, 0);
2180 input_sync(input);
2181 }
Luming Yue9dab192007-08-20 18:23:53 +08002182
Patrick Mocheld550d982006-06-27 00:41:40 -04002183 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Len Brown4be44fc2005-08-05 00:44:28 -04002186static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002188 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002189 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002190 struct acpi_video_bus *bus;
2191 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05002192 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002197 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002198 bus = video_device->video;
2199 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002202 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002203 if (brightness_switch_enabled)
2204 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002205 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002206 keycode = KEY_BRIGHTNESS_CYCLE;
2207 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002208 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002209 if (brightness_switch_enabled)
2210 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002211 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002212 keycode = KEY_BRIGHTNESSUP;
2213 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002214 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002215 if (brightness_switch_enabled)
2216 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002217 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002218 keycode = KEY_BRIGHTNESSDOWN;
2219 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002220 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002221 if (brightness_switch_enabled)
2222 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002223 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002224 keycode = KEY_BRIGHTNESS_ZERO;
2225 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002226 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002227 if (brightness_switch_enabled)
2228 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002229 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002230 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 break;
2232 default:
2233 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002234 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 break;
2236 }
Luming Yue9dab192007-08-20 18:23:53 +08002237
Zhang Rui7761f632008-01-25 14:48:12 +08002238 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05002239
2240 if (keycode) {
2241 input_report_key(input, keycode, 1);
2242 input_sync(input);
2243 input_report_key(input, keycode, 0);
2244 input_sync(input);
2245 }
Luming Yue9dab192007-08-20 18:23:53 +08002246
Patrick Mocheld550d982006-06-27 00:41:40 -04002247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002250static int acpi_video_resume(struct notifier_block *nb,
2251 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08002252{
2253 struct acpi_video_bus *video;
2254 struct acpi_video_device *video_device;
2255 int i;
2256
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002257 switch (val) {
2258 case PM_HIBERNATION_PREPARE:
2259 case PM_SUSPEND_PREPARE:
2260 case PM_RESTORE_PREPARE:
2261 return NOTIFY_DONE;
2262 }
Matthew Garrett863c1492008-02-04 23:31:24 -08002263
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002264 video = container_of(nb, struct acpi_video_bus, pm_nb);
2265
2266 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08002267
2268 for (i = 0; i < video->attached_count; i++) {
2269 video_device = video->attached_array[i].bind_info;
2270 if (video_device && video_device->backlight)
2271 acpi_video_set_brightness(video_device->backlight);
2272 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002273
2274 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08002275}
2276
Zhang Ruic504f8c2009-12-30 15:59:23 +08002277static acpi_status
2278acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2279 void **return_value)
2280{
2281 struct acpi_device *device = context;
2282 struct acpi_device *sibling;
2283 int result;
2284
2285 if (handle == device->handle)
2286 return AE_CTRL_TERMINATE;
2287
2288 result = acpi_bus_get_device(handle, &sibling);
2289 if (result)
2290 return AE_OK;
2291
2292 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2293 return AE_ALREADY_EXISTS;
2294
2295 return AE_OK;
2296}
2297
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002298static int instance;
2299
Len Brown4be44fc2005-08-05 00:44:28 -04002300static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002302 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002303 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002304 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002305 acpi_status status;
2306
2307 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2308 device->parent->handle, 1,
2309 acpi_video_bus_match, NULL,
2310 device, NULL);
2311 if (status == AE_ALREADY_EXISTS) {
2312 printk(KERN_WARNING FW_BUG
2313 "Duplicate ACPI video bus devices for the"
2314 " same VGA controller, please try module "
2315 "parameter \"video.allow_duplicates=1\""
2316 "if the current driver doesn't work.\n");
2317 if (!allow_duplicates)
2318 return -ENODEV;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Burman Yan36bcbec2006-12-19 12:56:11 -08002321 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002323 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Zhang Ruie6d9da12007-08-25 02:23:31 -04002325 /* a hack to fix the duplicate name "VID" problem on T61 */
2326 if (!strcmp(device->pnp.bus_id, "VID")) {
2327 if (instance)
2328 device->pnp.bus_id[3] = '0' + instance;
2329 instance ++;
2330 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002331 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2332 if (!strcmp(device->pnp.bus_id, "VGA")) {
2333 if (instance)
2334 device->pnp.bus_id[3] = '0' + instance;
2335 instance++;
2336 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002337
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002338 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2340 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002341 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002344 error = acpi_video_bus_check(video);
2345 if (error)
2346 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002348 error = acpi_video_bus_add_fs(device);
2349 if (error)
2350 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002352 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 INIT_LIST_HEAD(&video->video_device_list);
2354
2355 acpi_video_bus_get_devices(video, device);
2356 acpi_video_bus_start_devices(video);
2357
Luming Yue9dab192007-08-20 18:23:53 +08002358 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002359 if (!input) {
2360 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002361 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002362 }
Luming Yue9dab192007-08-20 18:23:53 +08002363
2364 snprintf(video->phys, sizeof(video->phys),
2365 "%s/video/input0", acpi_device_hid(video->device));
2366
2367 input->name = acpi_device_name(video->device);
2368 input->phys = video->phys;
2369 input->id.bustype = BUS_HOST;
2370 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002371 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002372 input->evbit[0] = BIT(EV_KEY);
2373 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2374 set_bit(KEY_VIDEO_NEXT, input->keybit);
2375 set_bit(KEY_VIDEO_PREV, input->keybit);
2376 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2377 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2378 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2379 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2380 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002381
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002382 error = input_register_device(input);
2383 if (error)
2384 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002387 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2388 video->flags.multihead ? "yes" : "no",
2389 video->flags.rom ? "yes" : "no",
2390 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002392 video->pm_nb.notifier_call = acpi_video_resume;
2393 video->pm_nb.priority = 0;
2394 register_pm_notifier(&video->pm_nb);
2395
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002398 err_free_input_dev:
2399 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002400 err_stop_video:
2401 acpi_video_bus_stop_devices(video);
2402 acpi_video_bus_put_devices(video);
2403 kfree(video->attached_array);
2404 acpi_video_bus_remove_fs(device);
2405 err_free_video:
2406 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002407 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002408
2409 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Len Brown4be44fc2005-08-05 00:44:28 -04002412static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Len Brown4be44fc2005-08-05 00:44:28 -04002414 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002418 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002420 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002422 unregister_pm_notifier(&video->pm_nb);
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 acpi_video_bus_put_devices(video);
2426 acpi_video_bus_remove_fs(device);
2427
Luming Yue9dab192007-08-20 18:23:53 +08002428 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002429 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 kfree(video);
2431
Patrick Mocheld550d982006-06-27 00:41:40 -04002432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Matthew Garrett74a365b2009-03-19 21:35:39 +00002435static int __init intel_opregion_present(void)
2436{
2437#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2438 struct pci_dev *dev = NULL;
2439 u32 address;
2440
2441 for_each_pci_dev(dev) {
2442 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2443 continue;
2444 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2445 continue;
2446 pci_read_config_dword(dev, 0xfc, &address);
2447 if (!address)
2448 continue;
2449 return 1;
2450 }
2451#endif
2452 return 0;
2453}
2454
2455int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Len Brown4be44fc2005-08-05 00:44:28 -04002457 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002458 if (register_count) {
2459 /*
2460 * if the function of acpi_video_register is already called,
2461 * don't register the acpi_vide_bus again and return no error.
2462 */
2463 return 0;
2464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2467 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002468 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 result = acpi_bus_register_driver(&acpi_video_bus);
2471 if (result < 0) {
2472 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002473 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475
Zhao Yakui86e437f2009-06-16 11:23:13 +08002476 /*
2477 * When the acpi_video_bus is loaded successfully, increase
2478 * the counter reference.
2479 */
2480 register_count = 1;
2481
Patrick Mocheld550d982006-06-27 00:41:40 -04002482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002484EXPORT_SYMBOL(acpi_video_register);
2485
Zhao Yakui86e437f2009-06-16 11:23:13 +08002486void acpi_video_unregister(void)
2487{
2488 if (!register_count) {
2489 /*
2490 * If the acpi video bus is already unloaded, don't
2491 * unload it again and return directly.
2492 */
2493 return;
2494 }
2495 acpi_bus_unregister_driver(&acpi_video_bus);
2496
2497 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2498
2499 register_count = 0;
2500
2501 return;
2502}
2503EXPORT_SYMBOL(acpi_video_unregister);
2504
Matthew Garrett74a365b2009-03-19 21:35:39 +00002505/*
2506 * This is kind of nasty. Hardware using Intel chipsets may require
2507 * the video opregion code to be run first in order to initialise
2508 * state before any ACPI video calls are made. To handle this we defer
2509 * registration of the video class until the opregion code has run.
2510 */
2511
2512static int __init acpi_video_init(void)
2513{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002514 dmi_check_system(video_dmi_table);
2515
Matthew Garrett74a365b2009-03-19 21:35:39 +00002516 if (intel_opregion_present())
2517 return 0;
2518
2519 return acpi_video_register();
2520}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Zhao Yakui86e437f2009-06-16 11:23:13 +08002522static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002524 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Patrick Mocheld550d982006-06-27 00:41:40 -04002526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529module_init(acpi_video_init);
2530module_exit(acpi_video_exit);