blob: b765790b32be1841d652d3e834c6013950700efc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040043#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_VIDEO_BUS_NAME "Video Bus"
51#define ACPI_VIDEO_DEVICE_NAME "Video Device"
52#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
53#define ACPI_VIDEO_NOTIFY_PROBE 0x81
54#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
55#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
56#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
57
Thomas Tuttlef4715182006-12-19 12:56:14 -080058#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
59#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
60#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
61#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
62#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yu Luming2f3d0002006-11-11 02:40:34 +080064#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Rui Zhang82cae992007-01-03 23:40:53 -050066#define ACPI_VIDEO_DISPLAY_CRT 1
67#define ACPI_VIDEO_DISPLAY_TV 2
68#define ACPI_VIDEO_DISPLAY_DVI 3
69#define ACPI_VIDEO_DISPLAY_LCD 4
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050072ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Len Brownf52fd662007-02-12 22:42:12 -050074MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050075MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_LICENSE("GPL");
77
Zhang Rui8a681a42008-01-25 14:47:49 +080078static int brightness_switch_enabled = 1;
79module_param(brightness_switch_enabled, bool, 0644);
80
Zhang Ruic504f8c2009-12-30 15:59:23 +080081/*
82 * By default, we don't allow duplicate ACPI video bus devices
83 * under the same VGA controller
84 */
85static int allow_duplicates;
86module_param(allow_duplicates, bool, 0644);
87
Zhao Yakui86e437f2009-06-16 11:23:13 +080088static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040089static int acpi_video_bus_add(struct acpi_device *device);
90static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080091static int acpi_video_resume(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000092static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Thomas Renninger1ba90e32007-07-23 14:44:41 +020094static const struct acpi_device_id video_device_ids[] = {
95 {ACPI_VIDEO_HID, 0},
96 {"", 0},
97};
98MODULE_DEVICE_TABLE(acpi, video_device_ids);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -0500101 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200103 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .ops = {
105 .add = acpi_video_bus_add,
106 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -0800107 .resume = acpi_video_resume,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000108 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400113 u8 multihead:1; /* can switch video heads */
114 u8 rom:1; /* can retrieve a video rom */
115 u8 post:1; /* can configure the head to */
116 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u8 _DOS:1; /*Enable/Disable output switching */
121 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
122 u8 _ROM:1; /*Get ROM Data */
123 u8 _GPD:1; /*Get POST Device */
124 u8 _SPD:1; /*Set POST Device */
125 u8 _VPO:1; /*Video POST Options */
126 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129struct acpi_video_device_attrib {
130 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100131 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400132 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100133 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400134 u32 bios_can_detect:1; /*BIOS can detect the device */
135 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
136 the VGA device. */
137 u32 pipe_id:3; /*For VGA multiple-head devices. */
138 u32 reserved:10; /*Must be 0 */
139 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
142struct acpi_video_enumerated_device {
143 union {
144 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } value;
147 struct acpi_video_device *bind_info;
148};
149
150struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400151 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400152 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 u8 attached_count;
155 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400157 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500158 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400159 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800160 struct input_dev *input;
161 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 crt:1;
166 u8 lcd:1;
167 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500168 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400169 u8 bios:1;
170 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500171 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
174struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400175 u8 _ADR:1; /*Return the unique ID */
176 u8 _BCL:1; /*Query list of brightness control levels supported */
177 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800178 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800179 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400180 u8 _DDC:1; /*Return the EDID for this device */
181 u8 _DCS:1; /*Return status of output device */
182 u8 _DGS:1; /*Query graphics state */
183 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Zhang Ruid32f6942009-03-18 16:27:12 +0800186struct acpi_video_brightness_flags {
187 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800188 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800189 u8 _BCL_use_index:1; /* levels in _BCL are index values */
190 u8 _BCM_use_index:1; /* input of _BCM is an index value */
191 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 int curr;
196 int count;
197 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800198 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400202 unsigned long device_id;
203 struct acpi_video_device_flags flags;
204 struct acpi_video_device_cap cap;
205 struct list_head entry;
206 struct acpi_video_bus *video;
207 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800209 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400210 struct thermal_cooling_device *cooling_dev;
Luming Yu23b0f012007-05-09 21:07:05 +0800211 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/* bus */
215static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100216static const struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700217 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400218 .open = acpi_video_bus_info_open_fs,
219 .read = seq_read,
220 .llseek = seq_lseek,
221 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100225static const struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700226 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400227 .open = acpi_video_bus_ROM_open_fs,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Len Brown4be44fc2005-08-05 00:44:28 -0400233static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
234 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100235static const struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700236 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400237 .open = acpi_video_bus_POST_info_open_fs,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400244static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100245 const char __user *buffer, size_t count, loff_t *data);
246static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700247 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400248 .open = acpi_video_bus_POST_open_fs,
249 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100250 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400251 .llseek = seq_lseek,
252 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400256static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100257 const char __user *buffer, size_t count, loff_t *data);
258static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700259 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400260 .open = acpi_video_bus_DOS_open_fs,
261 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100262 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400263 .llseek = seq_lseek,
264 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
267/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_info_open_fs(struct inode *inode,
269 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100270static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700271 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .open = acpi_video_device_info_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static int acpi_video_device_state_open_fs(struct inode *inode,
279 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400280static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100281 const char __user *buffer, size_t count, loff_t *data);
282static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700283 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400284 .open = acpi_video_device_state_open_fs,
285 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100286 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400287 .llseek = seq_lseek,
288 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291static int acpi_video_device_brightness_open_fs(struct inode *inode,
292 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400293static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100294 const char __user *buffer, size_t count, loff_t *data);
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700295static const struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700296 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400297 .open = acpi_video_device_brightness_open_fs,
298 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100299 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400300 .llseek = seq_lseek,
301 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Len Brown4be44fc2005-08-05 00:44:28 -0400304static int acpi_video_device_EDID_open_fs(struct inode *inode,
305 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100306static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700307 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400308 .open = acpi_video_device_EDID_open_fs,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100314static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 "motherboard VGA device",
316 "PCI VGA device",
317 "AGP VGA device",
318 "UNKNOWN",
319};
320
Len Brown4be44fc2005-08-05 00:44:28 -0400321static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
322static void acpi_video_device_rebind(struct acpi_video_bus *video);
323static void acpi_video_device_bind(struct acpi_video_bus *video,
324 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800326static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
327 int level);
328static int acpi_video_device_lcd_get_level_current(
329 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400330 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400331static int acpi_video_get_next_level(struct acpi_video_device *device,
332 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800333static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400334 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800335static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400336 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800337static int acpi_video_output_get(struct output_device *od);
338static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Yu Luming2f3d0002006-11-11 02:40:34 +0800340/*backlight device sysfs support*/
341static int acpi_video_get_brightness(struct backlight_device *bd)
342{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400343 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000344 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800345 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100346 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800347
348 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
349 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000350 for (i = 2; i < vd->brightness->count; i++) {
351 if (vd->brightness->levels[i] == cur_level)
352 /* The first two entries are special - see page 575
353 of the ACPI spec 3.0 */
354 return i-2;
355 }
356 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800357}
358
359static int acpi_video_set_brightness(struct backlight_device *bd)
360{
Zhang Rui24450c72009-03-18 16:27:10 +0800361 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800362 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100363 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800364
365 return acpi_video_device_lcd_set_level(vd,
366 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800367}
368
Richard Purdie599a52d2007-02-10 23:07:48 +0000369static struct backlight_ops acpi_backlight_ops = {
370 .get_brightness = acpi_video_get_brightness,
371 .update_status = acpi_video_set_brightness,
372};
373
Luming Yu23b0f012007-05-09 21:07:05 +0800374/*video output device sysfs support*/
375static int acpi_video_output_get(struct output_device *od)
376{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400377 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800378 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700379 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800380 acpi_video_device_get_state(vd, &state);
381 return (int)state;
382}
383
384static int acpi_video_output_set(struct output_device *od)
385{
386 unsigned long state = od->request_state;
387 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700388 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800389 return acpi_video_device_set_state(vd, state);
390}
391
392static struct output_properties acpi_output_properties = {
393 .set_state = acpi_video_output_set,
394 .get_status = acpi_video_output_get,
395};
Zhang Rui702ed512008-01-17 15:51:22 +0800396
397
398/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400399static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000400 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800401{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400402 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800403 struct acpi_video_device *video = acpi_driver_data(device);
404
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000405 *state = video->brightness->count - 3;
406 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800407}
408
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400409static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000410 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800411{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400412 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800413 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400414 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000415 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800416
Zhang Ruic8890f92009-03-18 16:27:08 +0800417 if (acpi_video_device_lcd_get_level_current(video, &level))
418 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000419 for (offset = 2; offset < video->brightness->count; offset++)
420 if (level == video->brightness->levels[offset]) {
421 *state = video->brightness->count - offset - 1;
422 return 0;
423 }
Zhang Rui702ed512008-01-17 15:51:22 +0800424
425 return -EINVAL;
426}
427
428static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400429video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800430{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400431 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800432 struct acpi_video_device *video = acpi_driver_data(device);
433 int level;
434
435 if ( state >= video->brightness->count - 2)
436 return -EINVAL;
437
438 state = video->brightness->count - state;
439 level = video->brightness->levels[state -1];
440 return acpi_video_device_lcd_set_level(video, level);
441}
442
443static struct thermal_cooling_device_ops video_cooling_ops = {
444 .get_max_state = video_get_max_state,
445 .get_cur_state = video_get_cur_state,
446 .set_cur_state = video_set_cur_state,
447};
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* --------------------------------------------------------------------------
450 Video Management
451 -------------------------------------------------------------------------- */
452
453/* device */
454
455static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400456acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Len Brown4be44fc2005-08-05 00:44:28 -0400458 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400459
460 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465static int
Len Brown4be44fc2005-08-05 00:44:28 -0400466acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400467 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Len Brown4be44fc2005-08-05 00:44:28 -0400469 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Patrick Mochel90130262006-05-19 16:54:48 -0400471 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476static int
Len Brown4be44fc2005-08-05 00:44:28 -0400477acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Len Brown4be44fc2005-08-05 00:44:28 -0400479 int status;
480 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
481 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400482 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400486 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491static int
Len Brown4be44fc2005-08-05 00:44:28 -0400492acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
493 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Len Brown4be44fc2005-08-05 00:44:28 -0400495 int status;
496 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
497 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 *levels = NULL;
501
Patrick Mochel90130262006-05-19 16:54:48 -0400502 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400504 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400505 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500506 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400507 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 status = -EFAULT;
509 goto err;
510 }
511
512 *levels = obj;
513
Patrick Mocheld550d982006-06-27 00:41:40 -0400514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Len Brown4be44fc2005-08-05 00:44:28 -0400516 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800517 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Patrick Mocheld550d982006-06-27 00:41:40 -0400519 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static int
Len Brown4be44fc2005-08-05 00:44:28 -0400523acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Zhang Rui24450c72009-03-18 16:27:10 +0800525 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400526 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
527 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800528 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Zhang Rui24450c72009-03-18 16:27:10 +0800532 status = acpi_evaluate_object(device->dev->handle, "_BCM",
533 &args, NULL);
534 if (ACPI_FAILURE(status)) {
535 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
536 return -EIO;
537 }
538
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400539 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800540 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800541 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800542 if (device->backlight)
543 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800544 return 0;
545 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800546
Zhang Rui24450c72009-03-18 16:27:10 +0800547 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Zhang Rui45cb50e2009-04-24 12:13:18 -0400551/*
552 * For some buggy _BQC methods, we need to add a constant value to
553 * the _BQC return value to get the actual current brightness level
554 */
555
556static int bqc_offset_aml_bug_workaround;
557static int __init video_set_bqc_offset(const struct dmi_system_id *d)
558{
559 bqc_offset_aml_bug_workaround = 9;
560 return 0;
561}
562
563static struct dmi_system_id video_dmi_table[] __initdata = {
564 /*
565 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
566 */
567 {
568 .callback = video_set_bqc_offset,
569 .ident = "Acer Aspire 5720",
570 .matches = {
571 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
572 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
573 },
574 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400575 {
576 .callback = video_set_bqc_offset,
577 .ident = "Acer Aspire 5710Z",
578 .matches = {
579 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
580 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
581 },
582 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400583 {
584 .callback = video_set_bqc_offset,
585 .ident = "eMachines E510",
586 .matches = {
587 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
588 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
589 },
590 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400591 {
592 .callback = video_set_bqc_offset,
593 .ident = "Acer Aspire 5315",
594 .matches = {
595 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
596 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
597 },
598 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800599 {
600 .callback = video_set_bqc_offset,
601 .ident = "Acer Aspire 7720",
602 .matches = {
603 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
604 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
605 },
606 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400607 {}
608};
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static int
Len Brown4be44fc2005-08-05 00:44:28 -0400611acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400612 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Zhang Ruic8890f92009-03-18 16:27:08 +0800614 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800615 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800616
Zhang Ruic60d6382009-03-18 16:27:18 +0800617 if (device->cap._BQC || device->cap._BCQ) {
618 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
619
620 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800621 NULL, level);
622 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800623 if (device->brightness->flags._BQC_use_index) {
624 if (device->brightness->flags._BCL_reversed)
625 *level = device->brightness->count
626 - 3 - (*level);
627 *level = device->brightness->levels[*level + 2];
628
629 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400630 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800631 for (i = 2; i < device->brightness->count; i++)
632 if (device->brightness->levels[i] == *level) {
633 device->brightness->curr = *level;
634 return 0;
635 }
636 /* BQC returned an invalid level. Stop using it. */
637 ACPI_WARNING((AE_INFO, "%s returned an invalid level",
638 buf));
639 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800640 } else {
641 /* Fixme:
642 * should we return an error or ignore this failure?
643 * dev->brightness->curr is a cached value which stores
644 * the correct current backlight level in most cases.
645 * ACPI video backlight still works w/ buggy _BQC.
646 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
647 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800648 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
649 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800650 }
651 }
652
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400653 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static int
Len Brown4be44fc2005-08-05 00:44:28 -0400658acpi_video_device_EDID(struct acpi_video_device *device,
659 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Len Brown4be44fc2005-08-05 00:44:28 -0400661 int status;
662 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
663 union acpi_object *obj;
664 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
665 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 *edid = NULL;
669
670 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400671 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (length == 128)
673 arg0.integer.value = 1;
674 else if (length == 256)
675 arg0.integer.value = 2;
676 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400677 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Patrick Mochel90130262006-05-19 16:54:48 -0400679 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200683 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (obj && obj->type == ACPI_TYPE_BUFFER)
686 *edid = obj;
687 else {
Len Brown64684632006-06-26 23:41:38 -0400688 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 status = -EFAULT;
690 kfree(obj);
691 }
692
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/* bus */
697
698static int
Len Brown4be44fc2005-08-05 00:44:28 -0400699acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Len Brown4be44fc2005-08-05 00:44:28 -0400701 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400702 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400703 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
704 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 arg0.integer.value = option;
708
Patrick Mochel90130262006-05-19 16:54:48 -0400709 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400711 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick Mocheld550d982006-06-27 00:41:40 -0400713 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400717acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 int status;
720
Patrick Mochel90130262006-05-19 16:54:48 -0400721 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Patrick Mocheld550d982006-06-27 00:41:40 -0400723 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726static int
Len Brown4be44fc2005-08-05 00:44:28 -0400727acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400728 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Len Brown4be44fc2005-08-05 00:44:28 -0400730 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Patrick Mochel90130262006-05-19 16:54:48 -0400732 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *options &= 3;
734
Patrick Mocheld550d982006-06-27 00:41:40 -0400735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/*
739 * Arg:
740 * video : video bus device pointer
741 * bios_flag :
742 * 0. The system BIOS should NOT automatically switch(toggle)
743 * the active display output.
744 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100745 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * 2. The _DGS value should be locked.
747 * 3. The system BIOS should not automatically switch (toggle) the
748 * active display output, but instead generate the display switch
749 * event notify code.
750 * lcd_flag :
751 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100752 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100754 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * Return Value:
756 * -1 wrong arg.
757 */
758
759static int
Len Brown4be44fc2005-08-05 00:44:28 -0400760acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Len Brown4be44fc2005-08-05 00:44:28 -0400762 acpi_integer status = 0;
763 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
764 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Len Brown4be44fc2005-08-05 00:44:28 -0400767 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 status = -1;
769 goto Failed;
770 }
771 arg0.integer.value = (lcd_flag << 2) | bios_flag;
772 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400773 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Len Brown4be44fc2005-08-05 00:44:28 -0400775 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400776 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500780 * Simple comparison function used to sort backlight levels.
781 */
782
783static int
784acpi_video_cmp_level(const void *a, const void *b)
785{
786 return *(int *)a - *(int *)b;
787}
788
789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * Arg:
791 * device : video output device (LCD, CRT, ..)
792 *
793 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100794 * Maximum brightness level
795 *
796 * Allocate and initialize device->brightness.
797 */
798
799static int
800acpi_video_init_brightness(struct acpi_video_device *device)
801{
802 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800803 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800804 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100805 union acpi_object *o;
806 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800807 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100808
809 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
810 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
811 "LCD brightness level\n"));
812 goto out;
813 }
814
815 if (obj->package.count < 2)
816 goto out;
817
818 br = kzalloc(sizeof(*br), GFP_KERNEL);
819 if (!br) {
820 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800821 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100822 goto out;
823 }
824
Zhang Ruid32f6942009-03-18 16:27:12 +0800825 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100826 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800827 if (!br->levels) {
828 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100829 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800830 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100831
832 for (i = 0; i < obj->package.count; i++) {
833 o = (union acpi_object *)&obj->package.elements[i];
834 if (o->type != ACPI_TYPE_INTEGER) {
835 printk(KERN_ERR PREFIX "Invalid data\n");
836 continue;
837 }
838 br->levels[count] = (u32) o->integer.value;
839
840 if (br->levels[count] > max_level)
841 max_level = br->levels[count];
842 count++;
843 }
844
Zhang Ruid32f6942009-03-18 16:27:12 +0800845 /*
846 * some buggy BIOS don't export the levels
847 * when machine is on AC/Battery in _BCL package.
848 * In this case, the first two elements in _BCL packages
849 * are also supported brightness levels that OS should take care of.
850 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800851 for (i = 2; i < count; i++) {
852 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800853 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800854 if (br->levels[i] == br->levels[1])
855 level_ac_battery++;
856 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500857
Zhang Ruid32f6942009-03-18 16:27:12 +0800858 if (level_ac_battery < 2) {
859 level_ac_battery = 2 - level_ac_battery;
860 br->flags._BCL_no_ac_battery_levels = 1;
861 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
862 br->levels[i] = br->levels[i - level_ac_battery];
863 count += level_ac_battery;
864 } else if (level_ac_battery > 2)
865 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
866
Zhang Ruid80fb992009-03-18 16:27:14 +0800867 /* Check if the _BCL package is in a reversed order */
868 if (max_level == br->levels[2]) {
869 br->flags._BCL_reversed = 1;
870 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
871 acpi_video_cmp_level, NULL);
872 } else if (max_level != br->levels[count - 1])
873 ACPI_ERROR((AE_INFO,
874 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100875
876 br->count = count;
877 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800878
879 /* Check the input/output of _BQC/_BCL/_BCM */
880 if ((max_level < 100) && (max_level <= (count - 2)))
881 br->flags._BCL_use_index = 1;
882
883 /*
884 * _BCM is always consistent with _BCL,
885 * at least for all the laptops we have ever seen.
886 */
887 br->flags._BCM_use_index = br->flags._BCL_use_index;
888
889 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400890 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800891
892 if (!device->cap._BQC)
893 goto set_level;
894
Zhang Rui1a7c6182009-03-18 16:27:16 +0800895 result = acpi_video_device_lcd_get_level_current(device, &level_old);
896 if (result)
897 goto out_free_levels;
898
Zhang Ruie047cca2009-04-09 14:24:35 +0800899 /*
900 * Set the level to maximum and check if _BQC uses indexed value
901 */
902 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800903 if (result)
904 goto out_free_levels;
905
906 result = acpi_video_device_lcd_get_level_current(device, &level);
907 if (result)
908 goto out_free_levels;
909
Zhang Ruie047cca2009-04-09 14:24:35 +0800910 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800911
Zhang Rui90c53ca2009-08-31 12:39:54 -0400912 if (!br->flags._BQC_use_index) {
913 /*
914 * Set the backlight to the initial state.
915 * On some buggy laptops, _BQC returns an uninitialized value
916 * when invoked for the first time, i.e. level_old is invalid.
917 * set the backlight to max_level in this case
918 */
919 for (i = 2; i < br->count; i++)
920 if (level_old == br->levels[i])
921 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800922 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400923 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800924
925 if (br->flags._BCL_reversed)
926 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400927 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800928
929set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400930 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800931 if (result)
932 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800933
Zhang Ruid32f6942009-03-18 16:27:12 +0800934 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
935 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100936 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800937 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100938
939out_free_levels:
940 kfree(br->levels);
941out_free:
942 kfree(br);
943out:
944 device->brightness = NULL;
945 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800946 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100947}
948
949/*
950 * Arg:
951 * device : video output device (LCD, CRT, ..)
952 *
953 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * None
955 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100956 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * device.
958 */
959
Len Brown4be44fc2005-08-05 00:44:28 -0400960static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Patrick Mochel90130262006-05-19 16:54:48 -0400964 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 device->cap._ADR = 1;
966 }
Patrick Mochel90130262006-05-19 16:54:48 -0400967 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400968 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Patrick Mochel90130262006-05-19 16:54:48 -0400970 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400971 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800973 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
974 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800975 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
976 &h_dummy1))) {
977 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
978 device->cap._BCQ = 1;
979 }
980
Patrick Mochel90130262006-05-19 16:54:48 -0400981 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400982 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Patrick Mochel90130262006-05-19 16:54:48 -0400984 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 device->cap._DCS = 1;
986 }
Patrick Mochel90130262006-05-19 16:54:48 -0400987 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 device->cap._DGS = 1;
989 }
Patrick Mochel90130262006-05-19 16:54:48 -0400990 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 device->cap._DSS = 1;
992 }
993
Zhang Rui1a7c6182009-03-18 16:27:16 +0800994 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800995 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800996 static int count = 0;
997 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800998
999 result = acpi_video_init_brightness(device);
1000 if (result)
1001 return;
Yu Luming2f3d0002006-11-11 02:40:34 +08001002 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1003 if (!name)
1004 return;
1005
Yu Luming2f3d0002006-11-11 02:40:34 +08001006 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +08001007 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +00001008 NULL, device, &acpi_backlight_ops);
Yu Luming2f3d0002006-11-11 02:40:34 +08001009 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +08001010 if (IS_ERR(device->backlight))
1011 return;
1012 device->backlight->props.max_brightness = device->brightness->count-3;
Zhang Rui702ed512008-01-17 15:51:22 +08001013
Zhang Rui056c3082009-06-22 11:31:14 +08001014 result = sysfs_create_link(&device->backlight->dev.kobj,
1015 &device->dev->dev.kobj, "device");
1016 if (result)
1017 printk(KERN_ERR PREFIX "Create sysfs link\n");
1018
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001019 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001020 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001021 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001022 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001023 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001024 * free it.
1025 * Also, why the hell we are returning early and
1026 * not attempt to register video output if cooling
1027 * device registration failed?
1028 * -- dtor
1029 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001030 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001031 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001032 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001033
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001034 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001035 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001036 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001037 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001038 "thermal_cooling");
1039 if (result)
1040 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001041 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001042 &device->dev->dev.kobj, "device");
1043 if (result)
1044 printk(KERN_ERR PREFIX "Create sysfs link\n");
1045
Yu Luming2f3d0002006-11-11 02:40:34 +08001046 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001047
1048 if (acpi_video_display_switch_support()) {
1049
1050 if (device->cap._DCS && device->cap._DSS) {
1051 static int count;
1052 char *name;
1053 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1054 if (!name)
1055 return;
1056 sprintf(name, "acpi_video%d", count++);
1057 device->output_dev = video_output_register(name,
1058 NULL, device, &acpi_output_properties);
1059 kfree(name);
1060 }
Luming Yu23b0f012007-05-09 21:07:05 +08001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064/*
1065 * Arg:
1066 * device : video output device (VGA)
1067 *
1068 * Return Value:
1069 * None
1070 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001071 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
1073
Len Brown4be44fc2005-08-05 00:44:28 -04001074static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Len Brown4be44fc2005-08-05 00:44:28 -04001076 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Patrick Mochel90130262006-05-19 16:54:48 -04001078 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 video->cap._DOS = 1;
1080 }
Patrick Mochel90130262006-05-19 16:54:48 -04001081 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 video->cap._DOD = 1;
1083 }
Patrick Mochel90130262006-05-19 16:54:48 -04001084 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 video->cap._ROM = 1;
1086 }
Patrick Mochel90130262006-05-19 16:54:48 -04001087 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 video->cap._GPD = 1;
1089 }
Patrick Mochel90130262006-05-19 16:54:48 -04001090 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 video->cap._SPD = 1;
1092 }
Patrick Mochel90130262006-05-19 16:54:48 -04001093 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 video->cap._VPO = 1;
1095 }
1096}
1097
1098/*
1099 * Check whether the video bus device has required AML method to
1100 * support the desired features
1101 */
1102
Len Brown4be44fc2005-08-05 00:44:28 -04001103static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Len Brown4be44fc2005-08-05 00:44:28 -04001105 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001106 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001111 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001112 if (!dev)
1113 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001114 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Since there is no HID, CID and so on for VGA driver, we have
1117 * to check well known required nodes.
1118 */
1119
Julius Volz98fb8fe2007-02-20 16:38:40 +01001120 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001121 if (video->cap._DOS || video->cap._DOD) {
1122 if (!video->cap._DOS) {
1123 printk(KERN_WARNING FW_BUG
1124 "ACPI(%s) defines _DOD but not _DOS\n",
1125 acpi_device_bid(video->device));
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 video->flags.multihead = 1;
1128 status = 0;
1129 }
1130
Julius Volz98fb8fe2007-02-20 16:38:40 +01001131 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001132 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 video->flags.rom = 1;
1134 status = 0;
1135 }
1136
Julius Volz98fb8fe2007-02-20 16:38:40 +01001137 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001138 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 video->flags.post = 1;
1140 status = 0;
1141 }
1142
Patrick Mocheld550d982006-06-27 00:41:40 -04001143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146/* --------------------------------------------------------------------------
1147 FS Interface (/proc)
1148 -------------------------------------------------------------------------- */
1149
Len Brown4be44fc2005-08-05 00:44:28 -04001150static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152/* video devices */
1153
Len Brown4be44fc2005-08-05 00:44:28 -04001154static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001156 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (!dev)
1160 goto end;
1161
1162 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1163 seq_printf(seq, "type: ");
1164 if (dev->flags.crt)
1165 seq_printf(seq, "CRT\n");
1166 else if (dev->flags.lcd)
1167 seq_printf(seq, "LCD\n");
1168 else if (dev->flags.tvout)
1169 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001170 else if (dev->flags.dvi)
1171 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 else
1173 seq_printf(seq, "UNKNOWN\n");
1174
Len Brown4be44fc2005-08-05 00:44:28 -04001175 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Len Brown4be44fc2005-08-05 00:44:28 -04001177 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static int
Len Brown4be44fc2005-08-05 00:44:28 -04001182acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 return single_open(file, acpi_video_device_info_seq_show,
1185 PDE(inode)->data);
1186}
1187
Len Brown4be44fc2005-08-05 00:44:28 -04001188static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Len Brown4be44fc2005-08-05 00:44:28 -04001190 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001191 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001192 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (!dev)
1196 goto end;
1197
1198 status = acpi_video_device_get_state(dev, &state);
1199 seq_printf(seq, "state: ");
1200 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001201 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 else
1203 seq_printf(seq, "<not supported>\n");
1204
1205 status = acpi_video_device_query(dev, &state);
1206 seq_printf(seq, "query: ");
1207 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001208 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 else
1210 seq_printf(seq, "<not supported>\n");
1211
Len Brown4be44fc2005-08-05 00:44:28 -04001212 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static int
Len Brown4be44fc2005-08-05 00:44:28 -04001217acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 return single_open(file, acpi_video_device_state_seq_show,
1220 PDE(inode)->data);
1221}
1222
1223static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001224acpi_video_device_write_state(struct file *file,
1225 const char __user * buffer,
1226 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Len Brown4be44fc2005-08-05 00:44:28 -04001228 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001229 struct seq_file *m = file->private_data;
1230 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001231 char str[12] = { 0 };
1232 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001235 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 str[count] = 0;
1242 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001243 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 status = acpi_video_device_set_state(dev, state);
1246
1247 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Patrick Mocheld550d982006-06-27 00:41:40 -04001250 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static int
Len Brown4be44fc2005-08-05 00:44:28 -04001254acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001256 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001257 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (!dev || !dev->brightness) {
1261 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
1265 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001266 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 seq_printf(seq, " %d", dev->brightness->levels[i]);
1268 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1269
Patrick Mocheld550d982006-06-27 00:41:40 -04001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273static int
Len Brown4be44fc2005-08-05 00:44:28 -04001274acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 return single_open(file, acpi_video_device_brightness_seq_show,
1277 PDE(inode)->data);
1278}
1279
1280static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001281acpi_video_device_write_brightness(struct file *file,
1282 const char __user * buffer,
1283 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001285 struct seq_file *m = file->private_data;
1286 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001287 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001288 unsigned int level = 0;
1289 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001292 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001296 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 str[count] = 0;
1299 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001302 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Julius Volz98fb8fe2007-02-20 16:38:40 +01001304 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001305 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001307 if (!acpi_video_device_lcd_set_level(dev, level))
1308 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 }
1311
Zhang Rui24450c72009-03-18 16:27:10 +08001312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Len Brown4be44fc2005-08-05 00:44:28 -04001315static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001317 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001318 int status;
1319 int i;
1320 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (!dev)
1324 goto out;
1325
Len Brown4be44fc2005-08-05 00:44:28 -04001326 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001328 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
1331 if (ACPI_FAILURE(status)) {
1332 goto out;
1333 }
1334
1335 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1336 for (i = 0; i < edid->buffer.length; i++)
1337 seq_putc(seq, edid->buffer.pointer[i]);
1338 }
1339
Len Brown4be44fc2005-08-05 00:44:28 -04001340 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!edid)
1342 seq_printf(seq, "<not supported>\n");
1343 else
1344 kfree(edid);
1345
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
1349static int
Len Brown4be44fc2005-08-05 00:44:28 -04001350acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 return single_open(file, acpi_video_device_EDID_seq_show,
1353 PDE(inode)->data);
1354}
1355
Len Brown4be44fc2005-08-05 00:44:28 -04001356static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001358 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct acpi_video_device *vid_dev;
1360
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001361 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001365 device_dir = proc_mkdir(acpi_device_bid(device),
1366 vid_dev->video->dir);
1367 if (!device_dir)
1368 return -ENOMEM;
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001371 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001372 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001374 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001377 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001378 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001379 &acpi_video_device_state_fops,
1380 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001382 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001385 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001386 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001387 &acpi_video_device_brightness_fops,
1388 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001390 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001393 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001394 &acpi_video_device_EDID_fops,
1395 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001397 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001399 acpi_device_dir(device) = device_dir;
1400
Patrick Mocheld550d982006-06-27 00:41:40 -04001401 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001402
1403 err_remove_brightness:
1404 remove_proc_entry("brightness", device_dir);
1405 err_remove_state:
1406 remove_proc_entry("state", device_dir);
1407 err_remove_info:
1408 remove_proc_entry("info", device_dir);
1409 err_remove_dir:
1410 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1411 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Len Brown4be44fc2005-08-05 00:44:28 -04001414static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001417 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001419 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001421 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001423 device_dir = acpi_device_dir(device);
1424 if (device_dir) {
1425 remove_proc_entry("info", device_dir);
1426 remove_proc_entry("state", device_dir);
1427 remove_proc_entry("brightness", device_dir);
1428 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001429 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 acpi_device_dir(device) = NULL;
1431 }
1432
Patrick Mocheld550d982006-06-27 00:41:40 -04001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001437static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001439 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 if (!video)
1443 goto end;
1444
1445 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001446 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001448 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001450 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Len Brown4be44fc2005-08-05 00:44:28 -04001452 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Len Brown4be44fc2005-08-05 00:44:28 -04001456static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Len Brown4be44fc2005-08-05 00:44:28 -04001458 return single_open(file, acpi_video_bus_info_seq_show,
1459 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Len Brown4be44fc2005-08-05 00:44:28 -04001462static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001464 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
1467 if (!video)
1468 goto end;
1469
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001470 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 seq_printf(seq, "<TODO>\n");
1472
Len Brown4be44fc2005-08-05 00:44:28 -04001473 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Len Brown4be44fc2005-08-05 00:44:28 -04001477static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1480}
1481
Len Brown4be44fc2005-08-05 00:44:28 -04001482static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001484 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001485 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001486 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!video)
1490 goto end;
1491
1492 status = acpi_video_bus_POST_options(video, &options);
1493 if (ACPI_SUCCESS(status)) {
1494 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001495 printk(KERN_WARNING PREFIX
1496 "The motherboard VGA device is not listed as a possible POST device.\n");
1497 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001498 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Frank Seidel4d939152009-02-04 17:03:07 +01001500 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001501 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (options & 2)
1503 seq_printf(seq, " <PCI video>");
1504 if (options & 4)
1505 seq_printf(seq, " <AGP video>");
1506 seq_putc(seq, '\n');
1507 } else
1508 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001509 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513static int
Len Brown4be44fc2005-08-05 00:44:28 -04001514acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Len Brown4be44fc2005-08-05 00:44:28 -04001516 return single_open(file, acpi_video_bus_POST_info_seq_show,
1517 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Len Brown4be44fc2005-08-05 00:44:28 -04001520static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001522 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001523 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001524 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (!video)
1528 goto end;
1529
Len Brown4be44fc2005-08-05 00:44:28 -04001530 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (!ACPI_SUCCESS(status)) {
1532 seq_printf(seq, "<not supported>\n");
1533 goto end;
1534 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001535 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Len Brown4be44fc2005-08-05 00:44:28 -04001537 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Len Brown4be44fc2005-08-05 00:44:28 -04001541static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001543 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Len Brown4be44fc2005-08-05 00:44:28 -04001546 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
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_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Len Brown4be44fc2005-08-05 00:44:28 -04001553 return single_open(file, acpi_video_bus_POST_seq_show,
1554 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Len Brown4be44fc2005-08-05 00:44:28 -04001557static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1560}
1561
1562static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001563acpi_video_bus_write_POST(struct file *file,
1564 const char __user * buffer,
1565 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Len Brown4be44fc2005-08-05 00:44:28 -04001567 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001568 struct seq_file *m = file->private_data;
1569 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001570 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001571 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001574 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 status = acpi_video_bus_POST_options(video, &options);
1578 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001582 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 str[count] = 0;
1585 opt = strtoul(str, NULL, 0);
1586 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001587 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Julius Volz98fb8fe2007-02-20 16:38:40 +01001589 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 options |= 1;
1591
1592 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001593 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001595 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 }
1598
Patrick Mocheld550d982006-06-27 00:41:40 -04001599 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001603acpi_video_bus_write_DOS(struct file *file,
1604 const char __user * buffer,
1605 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Len Brown4be44fc2005-08-05 00:44:28 -04001607 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001608 struct seq_file *m = file->private_data;
1609 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001610 char str[12] = { 0 };
1611 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001614 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001615 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001618 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 str[count] = 0;
1621 opt = strtoul(str, NULL, 0);
1622 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001623 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Len Brown4be44fc2005-08-05 00:44:28 -04001625 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001628 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Patrick Mocheld550d982006-06-27 00:41:40 -04001630 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Len Brown4be44fc2005-08-05 00:44:28 -04001633static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001635 struct acpi_video_bus *video = acpi_driver_data(device);
1636 struct proc_dir_entry *device_dir;
1637 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001639 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1640 if (!device_dir)
1641 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001644 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001645 &acpi_video_bus_info_fops,
1646 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001648 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001651 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001652 &acpi_video_bus_ROM_fops,
1653 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001655 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001658 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001659 &acpi_video_bus_POST_info_fops,
1660 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001662 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001665 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001666 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001667 &acpi_video_bus_POST_fops,
1668 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001670 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001673 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001674 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001675 &acpi_video_bus_DOS_fops,
1676 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001678 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001680 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001681 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001682
1683 err_remove_post:
1684 remove_proc_entry("POST", device_dir);
1685 err_remove_post_info:
1686 remove_proc_entry("POST_info", device_dir);
1687 err_remove_rom:
1688 remove_proc_entry("ROM", device_dir);
1689 err_remove_info:
1690 remove_proc_entry("info", device_dir);
1691 err_remove_dir:
1692 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Len Brown4be44fc2005-08-05 00:44:28 -04001696static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001698 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001700 if (device_dir) {
1701 remove_proc_entry("info", device_dir);
1702 remove_proc_entry("ROM", device_dir);
1703 remove_proc_entry("POST_info", device_dir);
1704 remove_proc_entry("POST", device_dir);
1705 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001706 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 acpi_device_dir(device) = NULL;
1708 }
1709
Patrick Mocheld550d982006-06-27 00:41:40 -04001710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
1713/* --------------------------------------------------------------------------
1714 Driver Interface
1715 -------------------------------------------------------------------------- */
1716
1717/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001718static struct acpi_video_device_attrib*
1719acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1720{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001721 struct acpi_video_enumerated_device *ids;
1722 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001723
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001724 for (i = 0; i < video->attached_count; i++) {
1725 ids = &video->attached_array[i];
1726 if ((ids->value.int_val & 0xffff) == device_id)
1727 return &ids->value.attrib;
1728 }
1729
Rui Zhang82cae992007-01-03 23:40:53 -05001730 return NULL;
1731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733static int
Len Brown4be44fc2005-08-05 00:44:28 -04001734acpi_video_bus_get_one_device(struct acpi_device *device,
1735 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001737 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001738 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001739 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001740 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Len Brown4be44fc2005-08-05 00:44:28 -04001745 status =
1746 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (ACPI_SUCCESS(status)) {
1748
Burman Yan36bcbec2006-12-19 12:56:11 -08001749 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001751 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1754 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001755 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 data->device_id = device_id;
1758 data->video = video;
1759 data->dev = device;
1760
Rui Zhang82cae992007-01-03 23:40:53 -05001761 attribute = acpi_video_get_device_attr(video, device_id);
1762
1763 if((attribute != NULL) && attribute->device_id_scheme) {
1764 switch (attribute->display_type) {
1765 case ACPI_VIDEO_DISPLAY_CRT:
1766 data->flags.crt = 1;
1767 break;
1768 case ACPI_VIDEO_DISPLAY_TV:
1769 data->flags.tvout = 1;
1770 break;
1771 case ACPI_VIDEO_DISPLAY_DVI:
1772 data->flags.dvi = 1;
1773 break;
1774 case ACPI_VIDEO_DISPLAY_LCD:
1775 data->flags.lcd = 1;
1776 break;
1777 default:
1778 data->flags.unknown = 1;
1779 break;
1780 }
1781 if(attribute->bios_can_detect)
1782 data->flags.bios = 1;
1783 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 acpi_video_device_bind(video, data);
1787 acpi_video_device_find_cap(data);
1788
Patrick Mochel90130262006-05-19 16:54:48 -04001789 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001790 ACPI_DEVICE_NOTIFY,
1791 acpi_video_device_notify,
1792 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001794 printk(KERN_ERR PREFIX
1795 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001796 if(data->brightness)
1797 kfree(data->brightness->levels);
1798 kfree(data->brightness);
1799 kfree(data);
1800 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001803 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001805 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 acpi_video_device_add_fs(device);
1808
Patrick Mocheld550d982006-06-27 00:41:40 -04001809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Patrick Mocheld550d982006-06-27 00:41:40 -04001812 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816 * Arg:
1817 * video : video bus device
1818 *
1819 * Return:
1820 * none
1821 *
1822 * Enumerate the video device list of the video bus,
1823 * bind the ids with the corresponding video devices
1824 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Len Brown4be44fc2005-08-05 00:44:28 -04001827static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001829 struct acpi_video_device *dev;
1830
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001831 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001832
1833 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001834 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001835
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001836 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
1839/*
1840 * Arg:
1841 * video : video bus device
1842 * device : video output device under the video
1843 * bus
1844 *
1845 * Return:
1846 * none
1847 *
1848 * Bind the ids with the corresponding video devices
1849 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852static void
Len Brown4be44fc2005-08-05 00:44:28 -04001853acpi_video_device_bind(struct acpi_video_bus *video,
1854 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001856 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001857 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001859 for (i = 0; i < video->attached_count; i++) {
1860 ids = &video->attached_array[i];
1861 if (device->device_id == (ids->value.int_val & 0xffff)) {
1862 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1864 }
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868/*
1869 * Arg:
1870 * video : video bus device
1871 *
1872 * Return:
1873 * < 0 : error
1874 *
1875 * Call _DOD to enumerate all devices attached to display adapter
1876 *
Len Brown4be44fc2005-08-05 00:44:28 -04001877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1880{
Len Brown4be44fc2005-08-05 00:44:28 -04001881 int status;
1882 int count;
1883 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001884 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001885 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1886 union acpi_object *dod = NULL;
1887 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Patrick Mochel90130262006-05-19 16:54:48 -04001889 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001891 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001892 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001895 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001897 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 status = -EFAULT;
1899 goto out;
1900 }
1901
1902 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001903 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001905 active_list = kcalloc(1 + dod->package.count,
1906 sizeof(struct acpi_video_enumerated_device),
1907 GFP_KERNEL);
1908 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 status = -ENOMEM;
1910 goto out;
1911 }
1912
1913 count = 0;
1914 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001915 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001918 printk(KERN_ERR PREFIX
1919 "Invalid _DOD data in element %d\n", i);
1920 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001922
1923 active_list[count].value.int_val = obj->integer.value;
1924 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001925 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1926 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 count++;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Jesper Juhl6044ec82005-11-07 01:01:32 -08001930 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001931
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001932 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001934
1935 out:
Len Brown02438d82006-06-30 03:19:10 -04001936 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Len Brown4be44fc2005-08-05 00:44:28 -04001940static int
1941acpi_video_get_next_level(struct acpi_video_device *device,
1942 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001944 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001945 max = max_below = 0;
1946 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001947 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001948 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001949 l = device->brightness->levels[i];
1950 if (abs(l - level_current) < abs(delta)) {
1951 delta = l - level_current;
1952 if (!delta)
1953 break;
1954 }
1955 }
1956 /* Ajust level_current to closest available level */
1957 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001958 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001959 l = device->brightness->levels[i];
1960 if (l < min)
1961 min = l;
1962 if (l > max)
1963 max = l;
1964 if (l < min_above && l > level_current)
1965 min_above = l;
1966 if (l > max_below && l < level_current)
1967 max_below = l;
1968 }
1969
1970 switch (event) {
1971 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1972 return (level_current < max) ? min_above : min;
1973 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1974 return (level_current < max) ? min_above : max;
1975 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1976 return (level_current > min) ? max_below : min;
1977 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1978 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1979 return 0;
1980 default:
1981 return level_current;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Zhang Ruic8890f92009-03-18 16:27:08 +08001985static int
Len Brown4be44fc2005-08-05 00:44:28 -04001986acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001988 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001989 int result = -EINVAL;
1990
Zhang Rui28c32e92009-07-13 10:33:24 +08001991 /* no warning message if acpi_backlight=vendor is used */
1992 if (!acpi_video_backlight_support())
1993 return 0;
1994
Julia Jomantaite469778c2008-06-23 22:50:42 +01001995 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001996 goto out;
1997
1998 result = acpi_video_device_lcd_get_level_current(device,
1999 &level_current);
2000 if (result)
2001 goto out;
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08002004
Zhang Rui24450c72009-03-18 16:27:10 +08002005 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08002006
Matthew Garrett36342742009-07-14 17:06:03 +01002007 if (!result)
2008 backlight_force_update(device->backlight,
2009 BACKLIGHT_UPDATE_HOTKEY);
2010
Zhang Ruic8890f92009-03-18 16:27:08 +08002011out:
2012 if (result)
2013 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2014
2015 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
2018static int
Len Brown4be44fc2005-08-05 00:44:28 -04002019acpi_video_bus_get_devices(struct acpi_video_bus *video,
2020 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Len Brown4be44fc2005-08-05 00:44:28 -04002022 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002023 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 acpi_video_device_enumerate(video);
2026
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002027 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 status = acpi_video_bus_get_one_device(dev, video);
2030 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002031 printk(KERN_WARNING PREFIX
2032 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 continue;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002036 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Len Brown4be44fc2005-08-05 00:44:28 -04002039static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Karol Kozimor031ec772005-07-30 04:18:00 -04002041 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct acpi_video_bus *video;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002046 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 video = device->video;
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 acpi_video_device_remove_fs(device->dev);
2051
Patrick Mochel90130262006-05-19 16:54:48 -04002052 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002053 ACPI_DEVICE_NOTIFY,
2054 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002055 if (device->backlight) {
2056 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2057 backlight_device_unregister(device->backlight);
2058 device->backlight = NULL;
2059 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002060 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002061 sysfs_remove_link(&device->dev->dev.kobj,
2062 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002063 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002064 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002065 thermal_cooling_device_unregister(device->cooling_dev);
2066 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002067 }
Luming Yu23b0f012007-05-09 21:07:05 +08002068 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002069
Patrick Mocheld550d982006-06-27 00:41:40 -04002070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Len Brown4be44fc2005-08-05 00:44:28 -04002073static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Len Brown4be44fc2005-08-05 00:44:28 -04002075 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002076 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002078 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002080 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002082 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002083 if (ACPI_FAILURE(status))
2084 printk(KERN_WARNING PREFIX
2085 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002087 if (dev->brightness) {
2088 kfree(dev->brightness->levels);
2089 kfree(dev->brightness);
2090 }
2091 list_del(&dev->entry);
2092 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002095 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002096
Patrick Mocheld550d982006-06-27 00:41:40 -04002097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
2100/* acpi_video interface */
2101
Len Brown4be44fc2005-08-05 00:44:28 -04002102static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Zhang Ruia21101c2007-09-14 11:46:22 +08002104 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Len Brown4be44fc2005-08-05 00:44:28 -04002107static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 return acpi_video_bus_DOS(video, 0, 1);
2110}
2111
Bjorn Helgaas70155582009-04-07 15:37:11 +00002112static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002114 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002115 struct input_dev *input;
2116 int keycode;
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Luming Yue9dab192007-08-20 18:23:53 +08002121 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002124 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002126 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002127 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
2129
Julius Volz98fb8fe2007-02-20 16:38:40 +01002130 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 * connector. */
2132 acpi_video_device_enumerate(video);
2133 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002134 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002135 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 break;
2137
Len Brown4be44fc2005-08-05 00:44:28 -04002138 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002139 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002140 keycode = KEY_SWITCHVIDEOMODE;
2141 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002142 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002143 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002144 keycode = KEY_VIDEO_NEXT;
2145 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002146 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002147 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002148 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 break;
2150
2151 default:
Luming Yue9dab192007-08-20 18:23:53 +08002152 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002154 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156 }
2157
Zhang Rui7761f632008-01-25 14:48:12 +08002158 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002159 input_report_key(input, keycode, 1);
2160 input_sync(input);
2161 input_report_key(input, keycode, 0);
2162 input_sync(input);
2163
Patrick Mocheld550d982006-06-27 00:41:40 -04002164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Len Brown4be44fc2005-08-05 00:44:28 -04002167static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002169 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002170 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002171 struct acpi_video_bus *bus;
2172 struct input_dev *input;
2173 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002178 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002179 bus = video_device->video;
2180 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002183 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002184 if (brightness_switch_enabled)
2185 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002186 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002187 keycode = KEY_BRIGHTNESS_CYCLE;
2188 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002189 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002190 if (brightness_switch_enabled)
2191 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002192 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002193 keycode = KEY_BRIGHTNESSUP;
2194 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002195 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002196 if (brightness_switch_enabled)
2197 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002198 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002199 keycode = KEY_BRIGHTNESSDOWN;
2200 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002201 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002202 if (brightness_switch_enabled)
2203 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002204 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002205 keycode = KEY_BRIGHTNESS_ZERO;
2206 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002207 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002208 if (brightness_switch_enabled)
2209 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002210 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002211 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 break;
2213 default:
Luming Yue9dab192007-08-20 18:23:53 +08002214 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002216 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 break;
2218 }
Luming Yue9dab192007-08-20 18:23:53 +08002219
Zhang Rui7761f632008-01-25 14:48:12 +08002220 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002221 input_report_key(input, keycode, 1);
2222 input_sync(input);
2223 input_report_key(input, keycode, 0);
2224 input_sync(input);
2225
Patrick Mocheld550d982006-06-27 00:41:40 -04002226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Zhang Ruie6d9da12007-08-25 02:23:31 -04002229static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002230static int acpi_video_resume(struct acpi_device *device)
2231{
2232 struct acpi_video_bus *video;
2233 struct acpi_video_device *video_device;
2234 int i;
2235
2236 if (!device || !acpi_driver_data(device))
2237 return -EINVAL;
2238
2239 video = acpi_driver_data(device);
2240
2241 for (i = 0; i < video->attached_count; i++) {
2242 video_device = video->attached_array[i].bind_info;
2243 if (video_device && video_device->backlight)
2244 acpi_video_set_brightness(video_device->backlight);
2245 }
2246 return AE_OK;
2247}
2248
Zhang Ruic504f8c2009-12-30 15:59:23 +08002249static acpi_status
2250acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2251 void **return_value)
2252{
2253 struct acpi_device *device = context;
2254 struct acpi_device *sibling;
2255 int result;
2256
2257 if (handle == device->handle)
2258 return AE_CTRL_TERMINATE;
2259
2260 result = acpi_bus_get_device(handle, &sibling);
2261 if (result)
2262 return AE_OK;
2263
2264 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2265 return AE_ALREADY_EXISTS;
2266
2267 return AE_OK;
2268}
2269
Len Brown4be44fc2005-08-05 00:44:28 -04002270static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002272 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002273 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002274 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002275 acpi_status status;
2276
2277 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2278 device->parent->handle, 1,
2279 acpi_video_bus_match, NULL,
2280 device, NULL);
2281 if (status == AE_ALREADY_EXISTS) {
2282 printk(KERN_WARNING FW_BUG
2283 "Duplicate ACPI video bus devices for the"
2284 " same VGA controller, please try module "
2285 "parameter \"video.allow_duplicates=1\""
2286 "if the current driver doesn't work.\n");
2287 if (!allow_duplicates)
2288 return -ENODEV;
2289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Burman Yan36bcbec2006-12-19 12:56:11 -08002291 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002293 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Zhang Ruie6d9da12007-08-25 02:23:31 -04002295 /* a hack to fix the duplicate name "VID" problem on T61 */
2296 if (!strcmp(device->pnp.bus_id, "VID")) {
2297 if (instance)
2298 device->pnp.bus_id[3] = '0' + instance;
2299 instance ++;
2300 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002301 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2302 if (!strcmp(device->pnp.bus_id, "VGA")) {
2303 if (instance)
2304 device->pnp.bus_id[3] = '0' + instance;
2305 instance++;
2306 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002307
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002308 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2310 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002311 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002314 error = acpi_video_bus_check(video);
2315 if (error)
2316 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002318 error = acpi_video_bus_add_fs(device);
2319 if (error)
2320 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002322 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 INIT_LIST_HEAD(&video->video_device_list);
2324
2325 acpi_video_bus_get_devices(video, device);
2326 acpi_video_bus_start_devices(video);
2327
Luming Yue9dab192007-08-20 18:23:53 +08002328 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002329 if (!input) {
2330 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002331 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002332 }
Luming Yue9dab192007-08-20 18:23:53 +08002333
2334 snprintf(video->phys, sizeof(video->phys),
2335 "%s/video/input0", acpi_device_hid(video->device));
2336
2337 input->name = acpi_device_name(video->device);
2338 input->phys = video->phys;
2339 input->id.bustype = BUS_HOST;
2340 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002341 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002342 input->evbit[0] = BIT(EV_KEY);
2343 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2344 set_bit(KEY_VIDEO_NEXT, input->keybit);
2345 set_bit(KEY_VIDEO_PREV, input->keybit);
2346 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2347 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2348 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2349 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2350 set_bit(KEY_DISPLAY_OFF, input->keybit);
2351 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002352
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002353 error = input_register_device(input);
2354 if (error)
2355 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002358 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2359 video->flags.multihead ? "yes" : "no",
2360 video->flags.rom ? "yes" : "no",
2361 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002365 err_free_input_dev:
2366 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002367 err_stop_video:
2368 acpi_video_bus_stop_devices(video);
2369 acpi_video_bus_put_devices(video);
2370 kfree(video->attached_array);
2371 acpi_video_bus_remove_fs(device);
2372 err_free_video:
2373 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002374 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002375
2376 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Len Brown4be44fc2005-08-05 00:44:28 -04002379static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Len Brown4be44fc2005-08-05 00:44:28 -04002381 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002387 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 acpi_video_bus_put_devices(video);
2391 acpi_video_bus_remove_fs(device);
2392
Luming Yue9dab192007-08-20 18:23:53 +08002393 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002394 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 kfree(video);
2396
Patrick Mocheld550d982006-06-27 00:41:40 -04002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Matthew Garrett74a365b2009-03-19 21:35:39 +00002400static int __init intel_opregion_present(void)
2401{
2402#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2403 struct pci_dev *dev = NULL;
2404 u32 address;
2405
2406 for_each_pci_dev(dev) {
2407 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2408 continue;
2409 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2410 continue;
2411 pci_read_config_dword(dev, 0xfc, &address);
2412 if (!address)
2413 continue;
2414 return 1;
2415 }
2416#endif
2417 return 0;
2418}
2419
2420int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Len Brown4be44fc2005-08-05 00:44:28 -04002422 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002423 if (register_count) {
2424 /*
2425 * if the function of acpi_video_register is already called,
2426 * don't register the acpi_vide_bus again and return no error.
2427 */
2428 return 0;
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2432 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002433 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 result = acpi_bus_register_driver(&acpi_video_bus);
2436 if (result < 0) {
2437 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002438 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440
Zhao Yakui86e437f2009-06-16 11:23:13 +08002441 /*
2442 * When the acpi_video_bus is loaded successfully, increase
2443 * the counter reference.
2444 */
2445 register_count = 1;
2446
Patrick Mocheld550d982006-06-27 00:41:40 -04002447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002449EXPORT_SYMBOL(acpi_video_register);
2450
Zhao Yakui86e437f2009-06-16 11:23:13 +08002451void acpi_video_unregister(void)
2452{
2453 if (!register_count) {
2454 /*
2455 * If the acpi video bus is already unloaded, don't
2456 * unload it again and return directly.
2457 */
2458 return;
2459 }
2460 acpi_bus_unregister_driver(&acpi_video_bus);
2461
2462 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2463
2464 register_count = 0;
2465
2466 return;
2467}
2468EXPORT_SYMBOL(acpi_video_unregister);
2469
Matthew Garrett74a365b2009-03-19 21:35:39 +00002470/*
2471 * This is kind of nasty. Hardware using Intel chipsets may require
2472 * the video opregion code to be run first in order to initialise
2473 * state before any ACPI video calls are made. To handle this we defer
2474 * registration of the video class until the opregion code has run.
2475 */
2476
2477static int __init acpi_video_init(void)
2478{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002479 dmi_check_system(video_dmi_table);
2480
Matthew Garrett74a365b2009-03-19 21:35:39 +00002481 if (intel_opregion_present())
2482 return 0;
2483
2484 return acpi_video_register();
2485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Zhao Yakui86e437f2009-06-16 11:23:13 +08002487static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002489 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Patrick Mocheld550d982006-06-27 00:41:40 -04002491 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494module_init(acpi_video_init);
2495module_exit(acpi_video_exit);