blob: 6a0143796772df266d6eb5c2d8498b96330070bf [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>
47
Len Browna192a952009-07-28 16:45:54 -040048#define PREFIX "ACPI: "
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_VIDEO_BUS_NAME "Video Bus"
52#define ACPI_VIDEO_DEVICE_NAME "Video Device"
53#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
54#define ACPI_VIDEO_NOTIFY_PROBE 0x81
55#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
56#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
57#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
58
Thomas Tuttlef4715182006-12-19 12:56:14 -080059#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
60#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
61#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
62#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
63#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Yu Luming2f3d0002006-11-11 02:40:34 +080065#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Rui Zhang82cae992007-01-03 23:40:53 -050067#define ACPI_VIDEO_DISPLAY_CRT 1
68#define ACPI_VIDEO_DISPLAY_TV 2
69#define ACPI_VIDEO_DISPLAY_DVI 3
70#define ACPI_VIDEO_DISPLAY_LCD 4
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050073ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Len Brownf52fd662007-02-12 22:42:12 -050075MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050076MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077MODULE_LICENSE("GPL");
78
Zhang Rui8a681a42008-01-25 14:47:49 +080079static int brightness_switch_enabled = 1;
80module_param(brightness_switch_enabled, bool, 0644);
81
Zhang Ruic504f8c2009-12-30 15:59:23 +080082/*
83 * By default, we don't allow duplicate ACPI video bus devices
84 * under the same VGA controller
85 */
86static int allow_duplicates;
87module_param(allow_duplicates, bool, 0644);
88
Zhao Yakui86e437f2009-06-16 11:23:13 +080089static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040090static int acpi_video_bus_add(struct acpi_device *device);
91static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080092static int acpi_video_resume(struct acpi_device *device);
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 Brownc2b6705b2007-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,
Matthew Garrett863c1492008-02-04 23:31:24 -0800108 .resume = acpi_video_resume,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000109 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400114 u8 multihead:1; /* can switch video heads */
115 u8 rom:1; /* can retrieve a video rom */
116 u8 post:1; /* can configure the head to */
117 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 u8 _DOS:1; /*Enable/Disable output switching */
122 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
123 u8 _ROM:1; /*Get ROM Data */
124 u8 _GPD:1; /*Get POST Device */
125 u8 _SPD:1; /*Set POST Device */
126 u8 _VPO:1; /*Video POST Options */
127 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Len Brown4be44fc2005-08-05 00:44:28 -0400130struct acpi_video_device_attrib {
131 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100132 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400133 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100134 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400135 u32 bios_can_detect:1; /*BIOS can detect the device */
136 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
137 the VGA device. */
138 u32 pipe_id:3; /*For VGA multiple-head devices. */
139 u32 reserved:10; /*Must be 0 */
140 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143struct acpi_video_enumerated_device {
144 union {
145 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } value;
148 struct acpi_video_device *bind_info;
149};
150
151struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400152 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 attached_count;
156 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500159 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400160 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800161 struct input_dev *input;
162 char phys[32]; /* for input device */
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
Zhang Rui056c3082009-06-22 11:31:14 +08001024 result = sysfs_create_link(&device->backlight->dev.kobj,
1025 &device->dev->dev.kobj, "device");
1026 if (result)
1027 printk(KERN_ERR PREFIX "Create sysfs link\n");
1028
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001029 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001030 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001031 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001032 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001033 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001034 * free it.
1035 * Also, why the hell we are returning early and
1036 * not attempt to register video output if cooling
1037 * device registration failed?
1038 * -- dtor
1039 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001040 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001041 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001042 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001043
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001044 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001045 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001046 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001047 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001048 "thermal_cooling");
1049 if (result)
1050 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001051 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001052 &device->dev->dev.kobj, "device");
1053 if (result)
1054 printk(KERN_ERR PREFIX "Create sysfs link\n");
1055
Yu Luming2f3d0002006-11-11 02:40:34 +08001056 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001057
1058 if (acpi_video_display_switch_support()) {
1059
1060 if (device->cap._DCS && device->cap._DSS) {
1061 static int count;
1062 char *name;
1063 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1064 if (!name)
1065 return;
1066 sprintf(name, "acpi_video%d", count++);
1067 device->output_dev = video_output_register(name,
1068 NULL, device, &acpi_output_properties);
1069 kfree(name);
1070 }
Luming Yu23b0f012007-05-09 21:07:05 +08001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074/*
1075 * Arg:
1076 * device : video output device (VGA)
1077 *
1078 * Return Value:
1079 * None
1080 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001081 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
1083
Len Brown4be44fc2005-08-05 00:44:28 -04001084static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Len Brown4be44fc2005-08-05 00:44:28 -04001086 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Patrick Mochel90130262006-05-19 16:54:48 -04001088 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 video->cap._DOS = 1;
1090 }
Patrick Mochel90130262006-05-19 16:54:48 -04001091 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 video->cap._DOD = 1;
1093 }
Patrick Mochel90130262006-05-19 16:54:48 -04001094 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 video->cap._ROM = 1;
1096 }
Patrick Mochel90130262006-05-19 16:54:48 -04001097 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 video->cap._GPD = 1;
1099 }
Patrick Mochel90130262006-05-19 16:54:48 -04001100 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 video->cap._SPD = 1;
1102 }
Patrick Mochel90130262006-05-19 16:54:48 -04001103 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 video->cap._VPO = 1;
1105 }
1106}
1107
1108/*
1109 * Check whether the video bus device has required AML method to
1110 * support the desired features
1111 */
1112
Len Brown4be44fc2005-08-05 00:44:28 -04001113static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Len Brown4be44fc2005-08-05 00:44:28 -04001115 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001116 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001121 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001122 if (!dev)
1123 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001124 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* Since there is no HID, CID and so on for VGA driver, we have
1127 * to check well known required nodes.
1128 */
1129
Julius Volz98fb8fe2007-02-20 16:38:40 +01001130 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001131 if (video->cap._DOS || video->cap._DOD) {
1132 if (!video->cap._DOS) {
1133 printk(KERN_WARNING FW_BUG
1134 "ACPI(%s) defines _DOD but not _DOS\n",
1135 acpi_device_bid(video->device));
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 video->flags.multihead = 1;
1138 status = 0;
1139 }
1140
Julius Volz98fb8fe2007-02-20 16:38:40 +01001141 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001142 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 video->flags.rom = 1;
1144 status = 0;
1145 }
1146
Julius Volz98fb8fe2007-02-20 16:38:40 +01001147 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001148 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 video->flags.post = 1;
1150 status = 0;
1151 }
1152
Patrick Mocheld550d982006-06-27 00:41:40 -04001153 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156/* --------------------------------------------------------------------------
1157 FS Interface (/proc)
1158 -------------------------------------------------------------------------- */
1159
Len Brown4be44fc2005-08-05 00:44:28 -04001160static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/* video devices */
1163
Len Brown4be44fc2005-08-05 00:44:28 -04001164static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001166 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (!dev)
1170 goto end;
1171
1172 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1173 seq_printf(seq, "type: ");
1174 if (dev->flags.crt)
1175 seq_printf(seq, "CRT\n");
1176 else if (dev->flags.lcd)
1177 seq_printf(seq, "LCD\n");
1178 else if (dev->flags.tvout)
1179 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001180 else if (dev->flags.dvi)
1181 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 else
1183 seq_printf(seq, "UNKNOWN\n");
1184
Len Brown4be44fc2005-08-05 00:44:28 -04001185 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Len Brown4be44fc2005-08-05 00:44:28 -04001187 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static int
Len Brown4be44fc2005-08-05 00:44:28 -04001192acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 return single_open(file, acpi_video_device_info_seq_show,
1195 PDE(inode)->data);
1196}
1197
Len Brown4be44fc2005-08-05 00:44:28 -04001198static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Len Brown4be44fc2005-08-05 00:44:28 -04001200 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001201 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001202 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 if (!dev)
1206 goto end;
1207
1208 status = acpi_video_device_get_state(dev, &state);
1209 seq_printf(seq, "state: ");
1210 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001211 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 else
1213 seq_printf(seq, "<not supported>\n");
1214
1215 status = acpi_video_device_query(dev, &state);
1216 seq_printf(seq, "query: ");
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
Len Brown4be44fc2005-08-05 00:44:28 -04001222 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static int
Len Brown4be44fc2005-08-05 00:44:28 -04001227acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 return single_open(file, acpi_video_device_state_seq_show,
1230 PDE(inode)->data);
1231}
1232
1233static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001234acpi_video_device_write_state(struct file *file,
1235 const char __user * buffer,
1236 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Len Brown4be44fc2005-08-05 00:44:28 -04001238 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001239 struct seq_file *m = file->private_data;
1240 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001241 char str[12] = { 0 };
1242 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001245 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001249 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 str[count] = 0;
1252 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001253 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 status = acpi_video_device_set_state(dev, state);
1256
1257 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Patrick Mocheld550d982006-06-27 00:41:40 -04001260 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263static int
Len Brown4be44fc2005-08-05 00:44:28 -04001264acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001266 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001267 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (!dev || !dev->brightness) {
1271 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001276 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 seq_printf(seq, " %d", dev->brightness->levels[i]);
1278 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1279
Patrick Mocheld550d982006-06-27 00:41:40 -04001280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283static int
Len Brown4be44fc2005-08-05 00:44:28 -04001284acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 return single_open(file, acpi_video_device_brightness_seq_show,
1287 PDE(inode)->data);
1288}
1289
1290static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001291acpi_video_device_write_brightness(struct file *file,
1292 const char __user * buffer,
1293 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001295 struct seq_file *m = file->private_data;
1296 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001297 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001298 unsigned int level = 0;
1299 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001302 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001306 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 str[count] = 0;
1309 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001312 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Julius Volz98fb8fe2007-02-20 16:38:40 +01001314 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001315 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001317 if (!acpi_video_device_lcd_set_level(dev, level))
1318 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
1320 }
1321
Zhang Rui24450c72009-03-18 16:27:10 +08001322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Len Brown4be44fc2005-08-05 00:44:28 -04001325static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001327 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001328 int status;
1329 int i;
1330 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (!dev)
1334 goto out;
1335
Len Brown4be44fc2005-08-05 00:44:28 -04001336 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001338 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
1341 if (ACPI_FAILURE(status)) {
1342 goto out;
1343 }
1344
1345 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1346 for (i = 0; i < edid->buffer.length; i++)
1347 seq_putc(seq, edid->buffer.pointer[i]);
1348 }
1349
Len Brown4be44fc2005-08-05 00:44:28 -04001350 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!edid)
1352 seq_printf(seq, "<not supported>\n");
1353 else
1354 kfree(edid);
1355
Patrick Mocheld550d982006-06-27 00:41:40 -04001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359static int
Len Brown4be44fc2005-08-05 00:44:28 -04001360acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 return single_open(file, acpi_video_device_EDID_seq_show,
1363 PDE(inode)->data);
1364}
1365
Len Brown4be44fc2005-08-05 00:44:28 -04001366static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001368 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct acpi_video_device *vid_dev;
1370
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001371 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001373 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001375 device_dir = proc_mkdir(acpi_device_bid(device),
1376 vid_dev->video->dir);
1377 if (!device_dir)
1378 return -ENOMEM;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001381 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001382 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001384 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001387 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001388 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001389 &acpi_video_device_state_fops,
1390 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001392 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001395 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001396 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001397 &acpi_video_device_brightness_fops,
1398 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001400 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001403 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001404 &acpi_video_device_EDID_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_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001409 acpi_device_dir(device) = device_dir;
1410
Patrick Mocheld550d982006-06-27 00:41:40 -04001411 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001412
1413 err_remove_brightness:
1414 remove_proc_entry("brightness", device_dir);
1415 err_remove_state:
1416 remove_proc_entry("state", device_dir);
1417 err_remove_info:
1418 remove_proc_entry("info", device_dir);
1419 err_remove_dir:
1420 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1421 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Len Brown4be44fc2005-08-05 00:44:28 -04001424static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001427 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001429 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001431 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001433 device_dir = acpi_device_dir(device);
1434 if (device_dir) {
1435 remove_proc_entry("info", device_dir);
1436 remove_proc_entry("state", device_dir);
1437 remove_proc_entry("brightness", device_dir);
1438 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001439 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 acpi_device_dir(device) = NULL;
1441 }
1442
Patrick Mocheld550d982006-06-27 00:41:40 -04001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001447static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001449 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (!video)
1453 goto end;
1454
1455 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001456 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001458 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001460 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Len Brown4be44fc2005-08-05 00:44:28 -04001462 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Len Brown4be44fc2005-08-05 00:44:28 -04001466static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Len Brown4be44fc2005-08-05 00:44:28 -04001468 return single_open(file, acpi_video_bus_info_seq_show,
1469 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Len Brown4be44fc2005-08-05 00:44:28 -04001472static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001474 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 if (!video)
1478 goto end;
1479
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001480 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 seq_printf(seq, "<TODO>\n");
1482
Len Brown4be44fc2005-08-05 00:44:28 -04001483 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Len Brown4be44fc2005-08-05 00:44:28 -04001487static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1490}
1491
Len Brown4be44fc2005-08-05 00:44:28 -04001492static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001494 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001495 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001496 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (!video)
1500 goto end;
1501
1502 status = acpi_video_bus_POST_options(video, &options);
1503 if (ACPI_SUCCESS(status)) {
1504 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001505 printk(KERN_WARNING PREFIX
1506 "The motherboard VGA device is not listed as a possible POST device.\n");
1507 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001508 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Frank Seidel4d939152009-02-04 17:03:07 +01001510 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001511 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (options & 2)
1513 seq_printf(seq, " <PCI video>");
1514 if (options & 4)
1515 seq_printf(seq, " <AGP video>");
1516 seq_putc(seq, '\n');
1517 } else
1518 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001519 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523static int
Len Brown4be44fc2005-08-05 00:44:28 -04001524acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Len Brown4be44fc2005-08-05 00:44:28 -04001526 return single_open(file, acpi_video_bus_POST_info_seq_show,
1527 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Len Brown4be44fc2005-08-05 00:44:28 -04001530static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001532 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001533 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001534 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 if (!video)
1538 goto end;
1539
Len Brown4be44fc2005-08-05 00:44:28 -04001540 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!ACPI_SUCCESS(status)) {
1542 seq_printf(seq, "<not supported>\n");
1543 goto end;
1544 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001545 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Len Brown4be44fc2005-08-05 00:44:28 -04001547 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Len Brown4be44fc2005-08-05 00:44:28 -04001551static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001553 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Len Brown4be44fc2005-08-05 00:44:28 -04001556 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Patrick Mocheld550d982006-06-27 00:41:40 -04001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Len Brown4be44fc2005-08-05 00:44:28 -04001561static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Len Brown4be44fc2005-08-05 00:44:28 -04001563 return single_open(file, acpi_video_bus_POST_seq_show,
1564 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Len Brown4be44fc2005-08-05 00:44:28 -04001567static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1570}
1571
1572static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001573acpi_video_bus_write_POST(struct file *file,
1574 const char __user * buffer,
1575 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Len Brown4be44fc2005-08-05 00:44:28 -04001577 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001578 struct seq_file *m = file->private_data;
1579 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001580 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001581 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001584 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001585 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 status = acpi_video_bus_POST_options(video, &options);
1588 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001592 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 str[count] = 0;
1595 opt = strtoul(str, NULL, 0);
1596 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001597 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Julius Volz98fb8fe2007-02-20 16:38:40 +01001599 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 options |= 1;
1601
1602 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001603 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001605 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 }
1608
Patrick Mocheld550d982006-06-27 00:41:40 -04001609 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001613acpi_video_bus_write_DOS(struct file *file,
1614 const char __user * buffer,
1615 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Len Brown4be44fc2005-08-05 00:44:28 -04001617 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001618 struct seq_file *m = file->private_data;
1619 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001620 char str[12] = { 0 };
1621 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001624 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001628 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 str[count] = 0;
1631 opt = strtoul(str, NULL, 0);
1632 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001633 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Len Brown4be44fc2005-08-05 00:44:28 -04001635 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001638 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Patrick Mocheld550d982006-06-27 00:41:40 -04001640 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Len Brown4be44fc2005-08-05 00:44:28 -04001643static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001645 struct acpi_video_bus *video = acpi_driver_data(device);
1646 struct proc_dir_entry *device_dir;
1647 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001649 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1650 if (!device_dir)
1651 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001654 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001655 &acpi_video_bus_info_fops,
1656 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001658 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001661 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001662 &acpi_video_bus_ROM_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_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001668 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001669 &acpi_video_bus_POST_info_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_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* 'POST' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001675 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001676 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001677 &acpi_video_bus_POST_fops,
1678 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001680 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 /* 'DOS' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001683 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001684 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001685 &acpi_video_bus_DOS_fops,
1686 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001688 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001690 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001691 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001692
1693 err_remove_post:
1694 remove_proc_entry("POST", device_dir);
1695 err_remove_post_info:
1696 remove_proc_entry("POST_info", device_dir);
1697 err_remove_rom:
1698 remove_proc_entry("ROM", device_dir);
1699 err_remove_info:
1700 remove_proc_entry("info", device_dir);
1701 err_remove_dir:
1702 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1703 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Len Brown4be44fc2005-08-05 00:44:28 -04001706static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001708 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001710 if (device_dir) {
1711 remove_proc_entry("info", device_dir);
1712 remove_proc_entry("ROM", device_dir);
1713 remove_proc_entry("POST_info", device_dir);
1714 remove_proc_entry("POST", device_dir);
1715 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001716 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 acpi_device_dir(device) = NULL;
1718 }
1719
Patrick Mocheld550d982006-06-27 00:41:40 -04001720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
1723/* --------------------------------------------------------------------------
1724 Driver Interface
1725 -------------------------------------------------------------------------- */
1726
1727/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001728static struct acpi_video_device_attrib*
1729acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1730{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001731 struct acpi_video_enumerated_device *ids;
1732 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001733
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001734 for (i = 0; i < video->attached_count; i++) {
1735 ids = &video->attached_array[i];
1736 if ((ids->value.int_val & 0xffff) == device_id)
1737 return &ids->value.attrib;
1738 }
1739
Rui Zhang82cae992007-01-03 23:40:53 -05001740 return NULL;
1741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743static int
Len Brown4be44fc2005-08-05 00:44:28 -04001744acpi_video_bus_get_one_device(struct acpi_device *device,
1745 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001747 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001748 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001749 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001750 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Len Brown4be44fc2005-08-05 00:44:28 -04001755 status =
1756 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (ACPI_SUCCESS(status)) {
1758
Burman Yan36bcbec2006-12-19 12:56:11 -08001759 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001761 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1764 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001765 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 data->device_id = device_id;
1768 data->video = video;
1769 data->dev = device;
1770
Rui Zhang82cae992007-01-03 23:40:53 -05001771 attribute = acpi_video_get_device_attr(video, device_id);
1772
1773 if((attribute != NULL) && attribute->device_id_scheme) {
1774 switch (attribute->display_type) {
1775 case ACPI_VIDEO_DISPLAY_CRT:
1776 data->flags.crt = 1;
1777 break;
1778 case ACPI_VIDEO_DISPLAY_TV:
1779 data->flags.tvout = 1;
1780 break;
1781 case ACPI_VIDEO_DISPLAY_DVI:
1782 data->flags.dvi = 1;
1783 break;
1784 case ACPI_VIDEO_DISPLAY_LCD:
1785 data->flags.lcd = 1;
1786 break;
1787 default:
1788 data->flags.unknown = 1;
1789 break;
1790 }
1791 if(attribute->bios_can_detect)
1792 data->flags.bios = 1;
1793 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 acpi_video_device_bind(video, data);
1797 acpi_video_device_find_cap(data);
1798
Patrick Mochel90130262006-05-19 16:54:48 -04001799 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001800 ACPI_DEVICE_NOTIFY,
1801 acpi_video_device_notify,
1802 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001804 printk(KERN_ERR PREFIX
1805 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001806 if(data->brightness)
1807 kfree(data->brightness->levels);
1808 kfree(data->brightness);
1809 kfree(data);
1810 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001813 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001815 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 acpi_video_device_add_fs(device);
1818
Patrick Mocheld550d982006-06-27 00:41:40 -04001819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821
Patrick Mocheld550d982006-06-27 00:41:40 -04001822 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825/*
1826 * Arg:
1827 * video : video bus device
1828 *
1829 * Return:
1830 * none
1831 *
1832 * Enumerate the video device list of the video bus,
1833 * bind the ids with the corresponding video devices
1834 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Len Brown4be44fc2005-08-05 00:44:28 -04001837static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001839 struct acpi_video_device *dev;
1840
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001841 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001842
1843 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001844 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001845
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001846 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/*
1850 * Arg:
1851 * video : video bus device
1852 * device : video output device under the video
1853 * bus
1854 *
1855 * Return:
1856 * none
1857 *
1858 * Bind the ids with the corresponding video devices
1859 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862static void
Len Brown4be44fc2005-08-05 00:44:28 -04001863acpi_video_device_bind(struct acpi_video_bus *video,
1864 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001866 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001867 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001869 for (i = 0; i < video->attached_count; i++) {
1870 ids = &video->attached_array[i];
1871 if (device->device_id == (ids->value.int_val & 0xffff)) {
1872 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1874 }
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878/*
1879 * Arg:
1880 * video : video bus device
1881 *
1882 * Return:
1883 * < 0 : error
1884 *
1885 * Call _DOD to enumerate all devices attached to display adapter
1886 *
Len Brown4be44fc2005-08-05 00:44:28 -04001887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1890{
Len Brown4be44fc2005-08-05 00:44:28 -04001891 int status;
1892 int count;
1893 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001894 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001895 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1896 union acpi_object *dod = NULL;
1897 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Patrick Mochel90130262006-05-19 16:54:48 -04001899 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001901 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001902 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001905 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001907 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 status = -EFAULT;
1909 goto out;
1910 }
1911
1912 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001913 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001915 active_list = kcalloc(1 + dod->package.count,
1916 sizeof(struct acpi_video_enumerated_device),
1917 GFP_KERNEL);
1918 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 status = -ENOMEM;
1920 goto out;
1921 }
1922
1923 count = 0;
1924 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001925 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001928 printk(KERN_ERR PREFIX
1929 "Invalid _DOD data in element %d\n", i);
1930 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001932
1933 active_list[count].value.int_val = obj->integer.value;
1934 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001935 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1936 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 count++;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Jesper Juhl6044ec82005-11-07 01:01:32 -08001940 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001941
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001942 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001944
1945 out:
Len Brown02438d82006-06-30 03:19:10 -04001946 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001947 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Len Brown4be44fc2005-08-05 00:44:28 -04001950static int
1951acpi_video_get_next_level(struct acpi_video_device *device,
1952 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001954 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001955 max = max_below = 0;
1956 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001957 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001958 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001959 l = device->brightness->levels[i];
1960 if (abs(l - level_current) < abs(delta)) {
1961 delta = l - level_current;
1962 if (!delta)
1963 break;
1964 }
1965 }
1966 /* Ajust level_current to closest available level */
1967 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001968 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001969 l = device->brightness->levels[i];
1970 if (l < min)
1971 min = l;
1972 if (l > max)
1973 max = l;
1974 if (l < min_above && l > level_current)
1975 min_above = l;
1976 if (l > max_below && l < level_current)
1977 max_below = l;
1978 }
1979
1980 switch (event) {
1981 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1982 return (level_current < max) ? min_above : min;
1983 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1984 return (level_current < max) ? min_above : max;
1985 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1986 return (level_current > min) ? max_below : min;
1987 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1988 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1989 return 0;
1990 default:
1991 return level_current;
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Zhang Ruic8890f92009-03-18 16:27:08 +08001995static int
Len Brown4be44fc2005-08-05 00:44:28 -04001996acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001998 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001999 int result = -EINVAL;
2000
Zhang Rui28c32e92009-07-13 10:33:24 +08002001 /* no warning message if acpi_backlight=vendor is used */
2002 if (!acpi_video_backlight_support())
2003 return 0;
2004
Julia Jomantaite469778c2008-06-23 22:50:42 +01002005 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08002006 goto out;
2007
2008 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05002009 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08002010 if (result)
2011 goto out;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08002014
Zhang Rui24450c72009-03-18 16:27:10 +08002015 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08002016
Matthew Garrett36342742009-07-14 17:06:03 +01002017 if (!result)
2018 backlight_force_update(device->backlight,
2019 BACKLIGHT_UPDATE_HOTKEY);
2020
Zhang Ruic8890f92009-03-18 16:27:08 +08002021out:
2022 if (result)
2023 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2024
2025 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028static int
Len Brown4be44fc2005-08-05 00:44:28 -04002029acpi_video_bus_get_devices(struct acpi_video_bus *video,
2030 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Len Brown4be44fc2005-08-05 00:44:28 -04002032 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002033 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 acpi_video_device_enumerate(video);
2036
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002037 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 status = acpi_video_bus_get_one_device(dev, video);
2040 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002041 printk(KERN_WARNING PREFIX
2042 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 continue;
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002046 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Len Brown4be44fc2005-08-05 00:44:28 -04002049static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Karol Kozimor031ec772005-07-30 04:18:00 -04002051 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct acpi_video_bus *video;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002056 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 video = device->video;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 acpi_video_device_remove_fs(device->dev);
2061
Patrick Mochel90130262006-05-19 16:54:48 -04002062 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002063 ACPI_DEVICE_NOTIFY,
2064 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002065 if (device->backlight) {
2066 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2067 backlight_device_unregister(device->backlight);
2068 device->backlight = NULL;
2069 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002070 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002071 sysfs_remove_link(&device->dev->dev.kobj,
2072 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002073 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002074 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002075 thermal_cooling_device_unregister(device->cooling_dev);
2076 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002077 }
Luming Yu23b0f012007-05-09 21:07:05 +08002078 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002079
Patrick Mocheld550d982006-06-27 00:41:40 -04002080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Len Brown4be44fc2005-08-05 00:44:28 -04002083static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Len Brown4be44fc2005-08-05 00:44:28 -04002085 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002086 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002088 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002090 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002092 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002093 if (ACPI_FAILURE(status))
2094 printk(KERN_WARNING PREFIX
2095 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002097 if (dev->brightness) {
2098 kfree(dev->brightness->levels);
2099 kfree(dev->brightness);
2100 }
2101 list_del(&dev->entry);
2102 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
2104
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002105 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002106
Patrick Mocheld550d982006-06-27 00:41:40 -04002107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110/* acpi_video interface */
2111
Len Brown4be44fc2005-08-05 00:44:28 -04002112static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Zhang Ruia21101c2007-09-14 11:46:22 +08002114 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Len Brown4be44fc2005-08-05 00:44:28 -04002117static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 return acpi_video_bus_DOS(video, 0, 1);
2120}
2121
Bjorn Helgaas70155582009-04-07 15:37:11 +00002122static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002124 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002125 struct input_dev *input;
2126 int keycode;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Luming Yue9dab192007-08-20 18:23:53 +08002131 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002134 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002136 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002137 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139
Julius Volz98fb8fe2007-02-20 16:38:40 +01002140 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * connector. */
2142 acpi_video_device_enumerate(video);
2143 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002144 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002145 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 break;
2147
Len Brown4be44fc2005-08-05 00:44:28 -04002148 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002149 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002150 keycode = KEY_SWITCHVIDEOMODE;
2151 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002152 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002153 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002154 keycode = KEY_VIDEO_NEXT;
2155 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002156 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002157 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002158 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 break;
2160
2161 default:
Luming Yue9dab192007-08-20 18:23:53 +08002162 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002164 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 break;
2166 }
2167
Zhang Rui7761f632008-01-25 14:48:12 +08002168 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002169 input_report_key(input, keycode, 1);
2170 input_sync(input);
2171 input_report_key(input, keycode, 0);
2172 input_sync(input);
2173
Patrick Mocheld550d982006-06-27 00:41:40 -04002174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Len Brown4be44fc2005-08-05 00:44:28 -04002177static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002179 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002180 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002181 struct acpi_video_bus *bus;
2182 struct input_dev *input;
2183 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002186 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002188 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002189 bus = video_device->video;
2190 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002193 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002194 if (brightness_switch_enabled)
2195 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002196 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002197 keycode = KEY_BRIGHTNESS_CYCLE;
2198 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002199 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002200 if (brightness_switch_enabled)
2201 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002202 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002203 keycode = KEY_BRIGHTNESSUP;
2204 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002205 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002206 if (brightness_switch_enabled)
2207 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002208 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002209 keycode = KEY_BRIGHTNESSDOWN;
2210 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002211 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002212 if (brightness_switch_enabled)
2213 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002214 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002215 keycode = KEY_BRIGHTNESS_ZERO;
2216 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002217 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002218 if (brightness_switch_enabled)
2219 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002220 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002221 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 default:
Luming Yue9dab192007-08-20 18:23:53 +08002224 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002226 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228 }
Luming Yue9dab192007-08-20 18:23:53 +08002229
Zhang Rui7761f632008-01-25 14:48:12 +08002230 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002231 input_report_key(input, keycode, 1);
2232 input_sync(input);
2233 input_report_key(input, keycode, 0);
2234 input_sync(input);
2235
Patrick Mocheld550d982006-06-27 00:41:40 -04002236 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Zhang Ruie6d9da12007-08-25 02:23:31 -04002239static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002240static int acpi_video_resume(struct acpi_device *device)
2241{
2242 struct acpi_video_bus *video;
2243 struct acpi_video_device *video_device;
2244 int i;
2245
2246 if (!device || !acpi_driver_data(device))
2247 return -EINVAL;
2248
2249 video = acpi_driver_data(device);
2250
2251 for (i = 0; i < video->attached_count; i++) {
2252 video_device = video->attached_array[i].bind_info;
2253 if (video_device && video_device->backlight)
2254 acpi_video_set_brightness(video_device->backlight);
2255 }
2256 return AE_OK;
2257}
2258
Zhang Ruic504f8c2009-12-30 15:59:23 +08002259static acpi_status
2260acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2261 void **return_value)
2262{
2263 struct acpi_device *device = context;
2264 struct acpi_device *sibling;
2265 int result;
2266
2267 if (handle == device->handle)
2268 return AE_CTRL_TERMINATE;
2269
2270 result = acpi_bus_get_device(handle, &sibling);
2271 if (result)
2272 return AE_OK;
2273
2274 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2275 return AE_ALREADY_EXISTS;
2276
2277 return AE_OK;
2278}
2279
Len Brown4be44fc2005-08-05 00:44:28 -04002280static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002282 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002283 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002284 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002285 acpi_status status;
2286
2287 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2288 device->parent->handle, 1,
2289 acpi_video_bus_match, NULL,
2290 device, NULL);
2291 if (status == AE_ALREADY_EXISTS) {
2292 printk(KERN_WARNING FW_BUG
2293 "Duplicate ACPI video bus devices for the"
2294 " same VGA controller, please try module "
2295 "parameter \"video.allow_duplicates=1\""
2296 "if the current driver doesn't work.\n");
2297 if (!allow_duplicates)
2298 return -ENODEV;
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Burman Yan36bcbec2006-12-19 12:56:11 -08002301 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002303 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Zhang Ruie6d9da12007-08-25 02:23:31 -04002305 /* a hack to fix the duplicate name "VID" problem on T61 */
2306 if (!strcmp(device->pnp.bus_id, "VID")) {
2307 if (instance)
2308 device->pnp.bus_id[3] = '0' + instance;
2309 instance ++;
2310 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002311 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2312 if (!strcmp(device->pnp.bus_id, "VGA")) {
2313 if (instance)
2314 device->pnp.bus_id[3] = '0' + instance;
2315 instance++;
2316 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002317
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002318 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2320 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002321 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002324 error = acpi_video_bus_check(video);
2325 if (error)
2326 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002328 error = acpi_video_bus_add_fs(device);
2329 if (error)
2330 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002332 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 INIT_LIST_HEAD(&video->video_device_list);
2334
2335 acpi_video_bus_get_devices(video, device);
2336 acpi_video_bus_start_devices(video);
2337
Luming Yue9dab192007-08-20 18:23:53 +08002338 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002339 if (!input) {
2340 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002341 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002342 }
Luming Yue9dab192007-08-20 18:23:53 +08002343
2344 snprintf(video->phys, sizeof(video->phys),
2345 "%s/video/input0", acpi_device_hid(video->device));
2346
2347 input->name = acpi_device_name(video->device);
2348 input->phys = video->phys;
2349 input->id.bustype = BUS_HOST;
2350 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002351 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002352 input->evbit[0] = BIT(EV_KEY);
2353 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2354 set_bit(KEY_VIDEO_NEXT, input->keybit);
2355 set_bit(KEY_VIDEO_PREV, input->keybit);
2356 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2357 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2358 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2359 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2360 set_bit(KEY_DISPLAY_OFF, input->keybit);
2361 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002362
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002363 error = input_register_device(input);
2364 if (error)
2365 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002368 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2369 video->flags.multihead ? "yes" : "no",
2370 video->flags.rom ? "yes" : "no",
2371 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002375 err_free_input_dev:
2376 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002377 err_stop_video:
2378 acpi_video_bus_stop_devices(video);
2379 acpi_video_bus_put_devices(video);
2380 kfree(video->attached_array);
2381 acpi_video_bus_remove_fs(device);
2382 err_free_video:
2383 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002384 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002385
2386 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Len Brown4be44fc2005-08-05 00:44:28 -04002389static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Len Brown4be44fc2005-08-05 00:44:28 -04002391 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002397 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 acpi_video_bus_put_devices(video);
2401 acpi_video_bus_remove_fs(device);
2402
Luming Yue9dab192007-08-20 18:23:53 +08002403 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002404 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 kfree(video);
2406
Patrick Mocheld550d982006-06-27 00:41:40 -04002407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Matthew Garrett74a365b2009-03-19 21:35:39 +00002410static int __init intel_opregion_present(void)
2411{
2412#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2413 struct pci_dev *dev = NULL;
2414 u32 address;
2415
2416 for_each_pci_dev(dev) {
2417 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2418 continue;
2419 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2420 continue;
2421 pci_read_config_dword(dev, 0xfc, &address);
2422 if (!address)
2423 continue;
2424 return 1;
2425 }
2426#endif
2427 return 0;
2428}
2429
2430int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Len Brown4be44fc2005-08-05 00:44:28 -04002432 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002433 if (register_count) {
2434 /*
2435 * if the function of acpi_video_register is already called,
2436 * don't register the acpi_vide_bus again and return no error.
2437 */
2438 return 0;
2439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2442 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002443 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 result = acpi_bus_register_driver(&acpi_video_bus);
2446 if (result < 0) {
2447 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002448 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450
Zhao Yakui86e437f2009-06-16 11:23:13 +08002451 /*
2452 * When the acpi_video_bus is loaded successfully, increase
2453 * the counter reference.
2454 */
2455 register_count = 1;
2456
Patrick Mocheld550d982006-06-27 00:41:40 -04002457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002459EXPORT_SYMBOL(acpi_video_register);
2460
Zhao Yakui86e437f2009-06-16 11:23:13 +08002461void acpi_video_unregister(void)
2462{
2463 if (!register_count) {
2464 /*
2465 * If the acpi video bus is already unloaded, don't
2466 * unload it again and return directly.
2467 */
2468 return;
2469 }
2470 acpi_bus_unregister_driver(&acpi_video_bus);
2471
2472 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2473
2474 register_count = 0;
2475
2476 return;
2477}
2478EXPORT_SYMBOL(acpi_video_unregister);
2479
Matthew Garrett74a365b2009-03-19 21:35:39 +00002480/*
2481 * This is kind of nasty. Hardware using Intel chipsets may require
2482 * the video opregion code to be run first in order to initialise
2483 * state before any ACPI video calls are made. To handle this we defer
2484 * registration of the video class until the opregion code has run.
2485 */
2486
2487static int __init acpi_video_init(void)
2488{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002489 dmi_check_system(video_dmi_table);
2490
Matthew Garrett74a365b2009-03-19 21:35:39 +00002491 if (intel_opregion_present())
2492 return 0;
2493
2494 return acpi_video_register();
2495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Zhao Yakui86e437f2009-06-16 11:23:13 +08002497static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002499 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Patrick Mocheld550d982006-06-27 00:41:40 -04002501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504module_init(acpi_video_init);
2505module_exit(acpi_video_exit);