blob: cbe6f3924a102d327cb92511255c8df0d63329b1 [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 Garrett70287db2010-02-16 16:53:50 -0500330 unsigned long long *level, int init);
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
Matthew Garrett70287db2010-02-16 16:53:50 -0500348 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800349 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
Matthew Garrett70287db2010-02-16 16:53:50 -0500417 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800418 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 Garrett70287db2010-02-16 16:53:50 -0500612 unsigned long long *level, int init)
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 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500636 if (!init) {
637 /*
638 * BQC returned an invalid level.
639 * Stop using it.
640 */
641 ACPI_WARNING((AE_INFO,
642 "%s returned an invalid level",
643 buf));
644 device->cap._BQC = device->cap._BCQ = 0;
645 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800646 } else {
647 /* Fixme:
648 * should we return an error or ignore this failure?
649 * dev->brightness->curr is a cached value which stores
650 * the correct current backlight level in most cases.
651 * ACPI video backlight still works w/ buggy _BQC.
652 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
653 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800654 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
655 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800656 }
657 }
658
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400659 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663static int
Len Brown4be44fc2005-08-05 00:44:28 -0400664acpi_video_device_EDID(struct acpi_video_device *device,
665 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Len Brown4be44fc2005-08-05 00:44:28 -0400667 int status;
668 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
669 union acpi_object *obj;
670 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
671 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 *edid = NULL;
675
676 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400677 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (length == 128)
679 arg0.integer.value = 1;
680 else if (length == 256)
681 arg0.integer.value = 2;
682 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400683 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Patrick Mochel90130262006-05-19 16:54:48 -0400685 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400687 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200689 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (obj && obj->type == ACPI_TYPE_BUFFER)
692 *edid = obj;
693 else {
Len Brown64684632006-06-26 23:41:38 -0400694 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 status = -EFAULT;
696 kfree(obj);
697 }
698
Patrick Mocheld550d982006-06-27 00:41:40 -0400699 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* bus */
703
704static int
Len Brown4be44fc2005-08-05 00:44:28 -0400705acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Len Brown4be44fc2005-08-05 00:44:28 -0400707 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400708 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400709 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
710 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 arg0.integer.value = option;
714
Patrick Mochel90130262006-05-19 16:54:48 -0400715 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400717 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
722static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400723acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
725 int status;
726
Patrick Mochel90130262006-05-19 16:54:48 -0400727 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Patrick Mocheld550d982006-06-27 00:41:40 -0400729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static int
Len Brown4be44fc2005-08-05 00:44:28 -0400733acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400734 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Len Brown4be44fc2005-08-05 00:44:28 -0400736 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Patrick Mochel90130262006-05-19 16:54:48 -0400738 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *options &= 3;
740
Patrick Mocheld550d982006-06-27 00:41:40 -0400741 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/*
745 * Arg:
746 * video : video bus device pointer
747 * bios_flag :
748 * 0. The system BIOS should NOT automatically switch(toggle)
749 * the active display output.
750 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100751 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * 2. The _DGS value should be locked.
753 * 3. The system BIOS should not automatically switch (toggle) the
754 * active display output, but instead generate the display switch
755 * event notify code.
756 * lcd_flag :
757 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100758 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100760 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * Return Value:
762 * -1 wrong arg.
763 */
764
765static int
Len Brown4be44fc2005-08-05 00:44:28 -0400766acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Lin Ming439913f2010-01-28 10:53:19 +0800768 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400769 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
770 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Len Brown4be44fc2005-08-05 00:44:28 -0400773 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 status = -1;
775 goto Failed;
776 }
777 arg0.integer.value = (lcd_flag << 2) | bios_flag;
778 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400779 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Len Brown4be44fc2005-08-05 00:44:28 -0400781 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400782 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500786 * Simple comparison function used to sort backlight levels.
787 */
788
789static int
790acpi_video_cmp_level(const void *a, const void *b)
791{
792 return *(int *)a - *(int *)b;
793}
794
795/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * Arg:
797 * device : video output device (LCD, CRT, ..)
798 *
799 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100800 * Maximum brightness level
801 *
802 * Allocate and initialize device->brightness.
803 */
804
805static int
806acpi_video_init_brightness(struct acpi_video_device *device)
807{
808 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800809 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800810 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100811 union acpi_object *o;
812 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800813 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100814
815 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
816 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
817 "LCD brightness level\n"));
818 goto out;
819 }
820
821 if (obj->package.count < 2)
822 goto out;
823
824 br = kzalloc(sizeof(*br), GFP_KERNEL);
825 if (!br) {
826 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800827 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100828 goto out;
829 }
830
Zhang Ruid32f6942009-03-18 16:27:12 +0800831 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100832 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800833 if (!br->levels) {
834 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100835 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800836 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100837
838 for (i = 0; i < obj->package.count; i++) {
839 o = (union acpi_object *)&obj->package.elements[i];
840 if (o->type != ACPI_TYPE_INTEGER) {
841 printk(KERN_ERR PREFIX "Invalid data\n");
842 continue;
843 }
844 br->levels[count] = (u32) o->integer.value;
845
846 if (br->levels[count] > max_level)
847 max_level = br->levels[count];
848 count++;
849 }
850
Zhang Ruid32f6942009-03-18 16:27:12 +0800851 /*
852 * some buggy BIOS don't export the levels
853 * when machine is on AC/Battery in _BCL package.
854 * In this case, the first two elements in _BCL packages
855 * are also supported brightness levels that OS should take care of.
856 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800857 for (i = 2; i < count; i++) {
858 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800859 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800860 if (br->levels[i] == br->levels[1])
861 level_ac_battery++;
862 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500863
Zhang Ruid32f6942009-03-18 16:27:12 +0800864 if (level_ac_battery < 2) {
865 level_ac_battery = 2 - level_ac_battery;
866 br->flags._BCL_no_ac_battery_levels = 1;
867 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
868 br->levels[i] = br->levels[i - level_ac_battery];
869 count += level_ac_battery;
870 } else if (level_ac_battery > 2)
871 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
872
Zhang Ruid80fb992009-03-18 16:27:14 +0800873 /* Check if the _BCL package is in a reversed order */
874 if (max_level == br->levels[2]) {
875 br->flags._BCL_reversed = 1;
876 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
877 acpi_video_cmp_level, NULL);
878 } else if (max_level != br->levels[count - 1])
879 ACPI_ERROR((AE_INFO,
880 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100881
882 br->count = count;
883 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800884
885 /* Check the input/output of _BQC/_BCL/_BCM */
886 if ((max_level < 100) && (max_level <= (count - 2)))
887 br->flags._BCL_use_index = 1;
888
889 /*
890 * _BCM is always consistent with _BCL,
891 * at least for all the laptops we have ever seen.
892 */
893 br->flags._BCM_use_index = br->flags._BCL_use_index;
894
895 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400896 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800897
898 if (!device->cap._BQC)
899 goto set_level;
900
Matthew Garrett70287db2010-02-16 16:53:50 -0500901 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800902 if (result)
903 goto out_free_levels;
904
Zhang Ruie047cca2009-04-09 14:24:35 +0800905 /*
906 * Set the level to maximum and check if _BQC uses indexed value
907 */
908 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800909 if (result)
910 goto out_free_levels;
911
Matthew Garrett70287db2010-02-16 16:53:50 -0500912 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800913 if (result)
914 goto out_free_levels;
915
Zhang Ruie047cca2009-04-09 14:24:35 +0800916 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800917
Zhang Rui90c53ca2009-08-31 12:39:54 -0400918 if (!br->flags._BQC_use_index) {
919 /*
920 * Set the backlight to the initial state.
921 * On some buggy laptops, _BQC returns an uninitialized value
922 * when invoked for the first time, i.e. level_old is invalid.
923 * set the backlight to max_level in this case
924 */
925 for (i = 2; i < br->count; i++)
926 if (level_old == br->levels[i])
927 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800928 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400929 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800930
931 if (br->flags._BCL_reversed)
932 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400933 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800934
935set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400936 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800937 if (result)
938 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800939
Zhang Ruid32f6942009-03-18 16:27:12 +0800940 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
941 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100942 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800943 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100944
945out_free_levels:
946 kfree(br->levels);
947out_free:
948 kfree(br);
949out:
950 device->brightness = NULL;
951 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800952 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100953}
954
955/*
956 * Arg:
957 * device : video output device (LCD, CRT, ..)
958 *
959 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * None
961 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100962 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * device.
964 */
965
Len Brown4be44fc2005-08-05 00:44:28 -0400966static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 acpi_handle h_dummy1;
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, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 device->cap._ADR = 1;
972 }
Patrick Mochel90130262006-05-19 16:54:48 -0400973 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400974 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Patrick Mochel90130262006-05-19 16:54:48 -0400976 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400977 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800979 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
980 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800981 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
982 &h_dummy1))) {
983 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
984 device->cap._BCQ = 1;
985 }
986
Patrick Mochel90130262006-05-19 16:54:48 -0400987 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400988 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Patrick Mochel90130262006-05-19 16:54:48 -0400990 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 device->cap._DCS = 1;
992 }
Patrick Mochel90130262006-05-19 16:54:48 -0400993 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 device->cap._DGS = 1;
995 }
Patrick Mochel90130262006-05-19 16:54:48 -0400996 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 device->cap._DSS = 1;
998 }
999
Zhang Rui1a7c6182009-03-18 16:27:16 +08001000 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001001 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +08001002 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +08001003 static int count = 0;
1004 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +08001005
1006 result = acpi_video_init_brightness(device);
1007 if (result)
1008 return;
Yu Luming2f3d0002006-11-11 02:40:34 +08001009 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1010 if (!name)
1011 return;
1012
Yu Luming2f3d0002006-11-11 02:40:34 +08001013 sprintf(name, "acpi_video%d", count++);
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001014 memset(&props, 0, sizeof(struct backlight_properties));
1015 props.max_brightness = device->brightness->count - 3;
1016 device->backlight = backlight_device_register(name, NULL, device,
1017 &acpi_backlight_ops,
1018 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +08001019 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +08001020 if (IS_ERR(device->backlight))
1021 return;
Zhang Rui702ed512008-01-17 15:51:22 +08001022
Zhang Rui056c3082009-06-22 11:31:14 +08001023 result = sysfs_create_link(&device->backlight->dev.kobj,
1024 &device->dev->dev.kobj, "device");
1025 if (result)
1026 printk(KERN_ERR PREFIX "Create sysfs link\n");
1027
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001028 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001029 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001030 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001031 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001032 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001033 * free it.
1034 * Also, why the hell we are returning early and
1035 * not attempt to register video output if cooling
1036 * device registration failed?
1037 * -- dtor
1038 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001039 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001040 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001041 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001042
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001043 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001044 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001045 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001046 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001047 "thermal_cooling");
1048 if (result)
1049 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001050 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001051 &device->dev->dev.kobj, "device");
1052 if (result)
1053 printk(KERN_ERR PREFIX "Create sysfs link\n");
1054
Yu Luming2f3d0002006-11-11 02:40:34 +08001055 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001056
1057 if (acpi_video_display_switch_support()) {
1058
1059 if (device->cap._DCS && device->cap._DSS) {
1060 static int count;
1061 char *name;
1062 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1063 if (!name)
1064 return;
1065 sprintf(name, "acpi_video%d", count++);
1066 device->output_dev = video_output_register(name,
1067 NULL, device, &acpi_output_properties);
1068 kfree(name);
1069 }
Luming Yu23b0f012007-05-09 21:07:05 +08001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073/*
1074 * Arg:
1075 * device : video output device (VGA)
1076 *
1077 * Return Value:
1078 * None
1079 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001080 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
1082
Len Brown4be44fc2005-08-05 00:44:28 -04001083static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Len Brown4be44fc2005-08-05 00:44:28 -04001085 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Patrick Mochel90130262006-05-19 16:54:48 -04001087 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 video->cap._DOS = 1;
1089 }
Patrick Mochel90130262006-05-19 16:54:48 -04001090 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 video->cap._DOD = 1;
1092 }
Patrick Mochel90130262006-05-19 16:54:48 -04001093 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 video->cap._ROM = 1;
1095 }
Patrick Mochel90130262006-05-19 16:54:48 -04001096 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 video->cap._GPD = 1;
1098 }
Patrick Mochel90130262006-05-19 16:54:48 -04001099 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 video->cap._SPD = 1;
1101 }
Patrick Mochel90130262006-05-19 16:54:48 -04001102 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 video->cap._VPO = 1;
1104 }
1105}
1106
1107/*
1108 * Check whether the video bus device has required AML method to
1109 * support the desired features
1110 */
1111
Len Brown4be44fc2005-08-05 00:44:28 -04001112static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Len Brown4be44fc2005-08-05 00:44:28 -04001114 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001115 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001120 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001121 if (!dev)
1122 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001123 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* Since there is no HID, CID and so on for VGA driver, we have
1126 * to check well known required nodes.
1127 */
1128
Julius Volz98fb8fe2007-02-20 16:38:40 +01001129 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001130 if (video->cap._DOS || video->cap._DOD) {
1131 if (!video->cap._DOS) {
1132 printk(KERN_WARNING FW_BUG
1133 "ACPI(%s) defines _DOD but not _DOS\n",
1134 acpi_device_bid(video->device));
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 video->flags.multihead = 1;
1137 status = 0;
1138 }
1139
Julius Volz98fb8fe2007-02-20 16:38:40 +01001140 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001141 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 video->flags.rom = 1;
1143 status = 0;
1144 }
1145
Julius Volz98fb8fe2007-02-20 16:38:40 +01001146 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001147 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 video->flags.post = 1;
1149 status = 0;
1150 }
1151
Patrick Mocheld550d982006-06-27 00:41:40 -04001152 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155/* --------------------------------------------------------------------------
1156 FS Interface (/proc)
1157 -------------------------------------------------------------------------- */
1158
Len Brown4be44fc2005-08-05 00:44:28 -04001159static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161/* video devices */
1162
Len Brown4be44fc2005-08-05 00:44:28 -04001163static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001165 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!dev)
1169 goto end;
1170
1171 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1172 seq_printf(seq, "type: ");
1173 if (dev->flags.crt)
1174 seq_printf(seq, "CRT\n");
1175 else if (dev->flags.lcd)
1176 seq_printf(seq, "LCD\n");
1177 else if (dev->flags.tvout)
1178 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001179 else if (dev->flags.dvi)
1180 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 else
1182 seq_printf(seq, "UNKNOWN\n");
1183
Len Brown4be44fc2005-08-05 00:44:28 -04001184 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Len Brown4be44fc2005-08-05 00:44:28 -04001186 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static int
Len Brown4be44fc2005-08-05 00:44:28 -04001191acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 return single_open(file, acpi_video_device_info_seq_show,
1194 PDE(inode)->data);
1195}
1196
Len Brown4be44fc2005-08-05 00:44:28 -04001197static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Len Brown4be44fc2005-08-05 00:44:28 -04001199 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001200 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001201 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (!dev)
1205 goto end;
1206
1207 status = acpi_video_device_get_state(dev, &state);
1208 seq_printf(seq, "state: ");
1209 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001210 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 else
1212 seq_printf(seq, "<not supported>\n");
1213
1214 status = acpi_video_device_query(dev, &state);
1215 seq_printf(seq, "query: ");
1216 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001217 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 else
1219 seq_printf(seq, "<not supported>\n");
1220
Len Brown4be44fc2005-08-05 00:44:28 -04001221 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static int
Len Brown4be44fc2005-08-05 00:44:28 -04001226acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 return single_open(file, acpi_video_device_state_seq_show,
1229 PDE(inode)->data);
1230}
1231
1232static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001233acpi_video_device_write_state(struct file *file,
1234 const char __user * buffer,
1235 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Len Brown4be44fc2005-08-05 00:44:28 -04001237 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001238 struct seq_file *m = file->private_data;
1239 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001240 char str[12] = { 0 };
1241 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001244 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 str[count] = 0;
1251 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001252 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 status = acpi_video_device_set_state(dev, state);
1255
1256 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001257 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Patrick Mocheld550d982006-06-27 00:41:40 -04001259 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static int
Len Brown4be44fc2005-08-05 00:44:28 -04001263acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001265 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001266 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (!dev || !dev->brightness) {
1270 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
1274 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001275 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 seq_printf(seq, " %d", dev->brightness->levels[i]);
1277 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1278
Patrick Mocheld550d982006-06-27 00:41:40 -04001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282static int
Len Brown4be44fc2005-08-05 00:44:28 -04001283acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 return single_open(file, acpi_video_device_brightness_seq_show,
1286 PDE(inode)->data);
1287}
1288
1289static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001290acpi_video_device_write_brightness(struct file *file,
1291 const char __user * buffer,
1292 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001294 struct seq_file *m = file->private_data;
1295 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001296 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001297 unsigned int level = 0;
1298 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001301 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001305 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 str[count] = 0;
1308 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001311 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Julius Volz98fb8fe2007-02-20 16:38:40 +01001313 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001314 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001316 if (!acpi_video_device_lcd_set_level(dev, level))
1317 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 }
1320
Zhang Rui24450c72009-03-18 16:27:10 +08001321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Len Brown4be44fc2005-08-05 00:44:28 -04001324static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001326 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001327 int status;
1328 int i;
1329 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (!dev)
1333 goto out;
1334
Len Brown4be44fc2005-08-05 00:44:28 -04001335 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001337 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 if (ACPI_FAILURE(status)) {
1341 goto out;
1342 }
1343
1344 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1345 for (i = 0; i < edid->buffer.length; i++)
1346 seq_putc(seq, edid->buffer.pointer[i]);
1347 }
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!edid)
1351 seq_printf(seq, "<not supported>\n");
1352 else
1353 kfree(edid);
1354
Patrick Mocheld550d982006-06-27 00:41:40 -04001355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static int
Len Brown4be44fc2005-08-05 00:44:28 -04001359acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 return single_open(file, acpi_video_device_EDID_seq_show,
1362 PDE(inode)->data);
1363}
1364
Len Brown4be44fc2005-08-05 00:44:28 -04001365static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001367 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct acpi_video_device *vid_dev;
1369
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001370 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001372 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001374 device_dir = proc_mkdir(acpi_device_bid(device),
1375 vid_dev->video->dir);
1376 if (!device_dir)
1377 return -ENOMEM;
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001380 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001381 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001383 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001386 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001387 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001388 &acpi_video_device_state_fops,
1389 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001391 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001394 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001395 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001396 &acpi_video_device_brightness_fops,
1397 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001399 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001402 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001403 &acpi_video_device_EDID_fops,
1404 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001406 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001408 acpi_device_dir(device) = device_dir;
1409
Patrick Mocheld550d982006-06-27 00:41:40 -04001410 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001411
1412 err_remove_brightness:
1413 remove_proc_entry("brightness", device_dir);
1414 err_remove_state:
1415 remove_proc_entry("state", device_dir);
1416 err_remove_info:
1417 remove_proc_entry("info", device_dir);
1418 err_remove_dir:
1419 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1420 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Len Brown4be44fc2005-08-05 00:44:28 -04001423static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001426 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001428 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001430 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001432 device_dir = acpi_device_dir(device);
1433 if (device_dir) {
1434 remove_proc_entry("info", device_dir);
1435 remove_proc_entry("state", device_dir);
1436 remove_proc_entry("brightness", device_dir);
1437 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001438 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 acpi_device_dir(device) = NULL;
1440 }
1441
Patrick Mocheld550d982006-06-27 00:41:40 -04001442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001446static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001448 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (!video)
1452 goto end;
1453
1454 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001455 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001457 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001459 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Len Brown4be44fc2005-08-05 00:44:28 -04001461 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Len Brown4be44fc2005-08-05 00:44:28 -04001465static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Len Brown4be44fc2005-08-05 00:44:28 -04001467 return single_open(file, acpi_video_bus_info_seq_show,
1468 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Len Brown4be44fc2005-08-05 00:44:28 -04001471static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001473 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 if (!video)
1477 goto end;
1478
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001479 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 seq_printf(seq, "<TODO>\n");
1481
Len Brown4be44fc2005-08-05 00:44:28 -04001482 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Len Brown4be44fc2005-08-05 00:44:28 -04001486static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1489}
1490
Len Brown4be44fc2005-08-05 00:44:28 -04001491static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001493 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001494 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001495 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (!video)
1499 goto end;
1500
1501 status = acpi_video_bus_POST_options(video, &options);
1502 if (ACPI_SUCCESS(status)) {
1503 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001504 printk(KERN_WARNING PREFIX
1505 "The motherboard VGA device is not listed as a possible POST device.\n");
1506 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001507 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
Frank Seidel4d939152009-02-04 17:03:07 +01001509 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001510 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (options & 2)
1512 seq_printf(seq, " <PCI video>");
1513 if (options & 4)
1514 seq_printf(seq, " <AGP video>");
1515 seq_putc(seq, '\n');
1516 } else
1517 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001518 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522static int
Len Brown4be44fc2005-08-05 00:44:28 -04001523acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Len Brown4be44fc2005-08-05 00:44:28 -04001525 return single_open(file, acpi_video_bus_POST_info_seq_show,
1526 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Len Brown4be44fc2005-08-05 00:44:28 -04001529static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001531 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001532 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001533 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (!video)
1537 goto end;
1538
Len Brown4be44fc2005-08-05 00:44:28 -04001539 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (!ACPI_SUCCESS(status)) {
1541 seq_printf(seq, "<not supported>\n");
1542 goto end;
1543 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001544 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Len Brown4be44fc2005-08-05 00:44:28 -04001546 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Len Brown4be44fc2005-08-05 00:44:28 -04001550static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001552 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Len Brown4be44fc2005-08-05 00:44:28 -04001555 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Patrick Mocheld550d982006-06-27 00:41:40 -04001557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Len Brown4be44fc2005-08-05 00:44:28 -04001560static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Len Brown4be44fc2005-08-05 00:44:28 -04001562 return single_open(file, acpi_video_bus_POST_seq_show,
1563 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Len Brown4be44fc2005-08-05 00:44:28 -04001566static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1569}
1570
1571static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001572acpi_video_bus_write_POST(struct file *file,
1573 const char __user * buffer,
1574 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Len Brown4be44fc2005-08-05 00:44:28 -04001576 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001577 struct seq_file *m = file->private_data;
1578 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001579 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001580 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001583 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 status = acpi_video_bus_POST_options(video, &options);
1587 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001588 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001591 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 str[count] = 0;
1594 opt = strtoul(str, NULL, 0);
1595 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001596 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Julius Volz98fb8fe2007-02-20 16:38:40 +01001598 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 options |= 1;
1600
1601 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001602 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001604 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 }
1607
Patrick Mocheld550d982006-06-27 00:41:40 -04001608 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001612acpi_video_bus_write_DOS(struct file *file,
1613 const char __user * buffer,
1614 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Len Brown4be44fc2005-08-05 00:44:28 -04001616 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001617 struct seq_file *m = file->private_data;
1618 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001619 char str[12] = { 0 };
1620 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001623 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001624 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001627 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 str[count] = 0;
1630 opt = strtoul(str, NULL, 0);
1631 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001632 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Len Brown4be44fc2005-08-05 00:44:28 -04001634 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001637 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Patrick Mocheld550d982006-06-27 00:41:40 -04001639 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Len Brown4be44fc2005-08-05 00:44:28 -04001642static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001644 struct acpi_video_bus *video = acpi_driver_data(device);
1645 struct proc_dir_entry *device_dir;
1646 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001648 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1649 if (!device_dir)
1650 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001653 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001654 &acpi_video_bus_info_fops,
1655 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001657 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001660 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001661 &acpi_video_bus_ROM_fops,
1662 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001664 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001667 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001668 &acpi_video_bus_POST_info_fops,
1669 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001671 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 /* 'POST' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001674 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001675 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001676 &acpi_video_bus_POST_fops,
1677 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001679 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* 'DOS' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001682 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001683 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001684 &acpi_video_bus_DOS_fops,
1685 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001687 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001689 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001690 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001691
1692 err_remove_post:
1693 remove_proc_entry("POST", device_dir);
1694 err_remove_post_info:
1695 remove_proc_entry("POST_info", device_dir);
1696 err_remove_rom:
1697 remove_proc_entry("ROM", device_dir);
1698 err_remove_info:
1699 remove_proc_entry("info", device_dir);
1700 err_remove_dir:
1701 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1702 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Len Brown4be44fc2005-08-05 00:44:28 -04001705static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001707 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001709 if (device_dir) {
1710 remove_proc_entry("info", device_dir);
1711 remove_proc_entry("ROM", device_dir);
1712 remove_proc_entry("POST_info", device_dir);
1713 remove_proc_entry("POST", device_dir);
1714 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001715 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 acpi_device_dir(device) = NULL;
1717 }
1718
Patrick Mocheld550d982006-06-27 00:41:40 -04001719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722/* --------------------------------------------------------------------------
1723 Driver Interface
1724 -------------------------------------------------------------------------- */
1725
1726/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001727static struct acpi_video_device_attrib*
1728acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1729{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001730 struct acpi_video_enumerated_device *ids;
1731 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001732
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001733 for (i = 0; i < video->attached_count; i++) {
1734 ids = &video->attached_array[i];
1735 if ((ids->value.int_val & 0xffff) == device_id)
1736 return &ids->value.attrib;
1737 }
1738
Rui Zhang82cae992007-01-03 23:40:53 -05001739 return NULL;
1740}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742static int
Len Brown4be44fc2005-08-05 00:44:28 -04001743acpi_video_bus_get_one_device(struct acpi_device *device,
1744 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001746 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001747 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001748 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001749 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Len Brown4be44fc2005-08-05 00:44:28 -04001754 status =
1755 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (ACPI_SUCCESS(status)) {
1757
Burman Yan36bcbec2006-12-19 12:56:11 -08001758 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1763 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001764 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 data->device_id = device_id;
1767 data->video = video;
1768 data->dev = device;
1769
Rui Zhang82cae992007-01-03 23:40:53 -05001770 attribute = acpi_video_get_device_attr(video, device_id);
1771
1772 if((attribute != NULL) && attribute->device_id_scheme) {
1773 switch (attribute->display_type) {
1774 case ACPI_VIDEO_DISPLAY_CRT:
1775 data->flags.crt = 1;
1776 break;
1777 case ACPI_VIDEO_DISPLAY_TV:
1778 data->flags.tvout = 1;
1779 break;
1780 case ACPI_VIDEO_DISPLAY_DVI:
1781 data->flags.dvi = 1;
1782 break;
1783 case ACPI_VIDEO_DISPLAY_LCD:
1784 data->flags.lcd = 1;
1785 break;
1786 default:
1787 data->flags.unknown = 1;
1788 break;
1789 }
1790 if(attribute->bios_can_detect)
1791 data->flags.bios = 1;
1792 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 acpi_video_device_bind(video, data);
1796 acpi_video_device_find_cap(data);
1797
Patrick Mochel90130262006-05-19 16:54:48 -04001798 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001799 ACPI_DEVICE_NOTIFY,
1800 acpi_video_device_notify,
1801 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001803 printk(KERN_ERR PREFIX
1804 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001805 if(data->brightness)
1806 kfree(data->brightness->levels);
1807 kfree(data->brightness);
1808 kfree(data);
1809 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001812 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001814 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 acpi_video_device_add_fs(device);
1817
Patrick Mocheld550d982006-06-27 00:41:40 -04001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
Patrick Mocheld550d982006-06-27 00:41:40 -04001821 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824/*
1825 * Arg:
1826 * video : video bus device
1827 *
1828 * Return:
1829 * none
1830 *
1831 * Enumerate the video device list of the video bus,
1832 * bind the ids with the corresponding video devices
1833 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Len Brown4be44fc2005-08-05 00:44:28 -04001836static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001838 struct acpi_video_device *dev;
1839
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001840 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001841
1842 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001843 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001844
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001845 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/*
1849 * Arg:
1850 * video : video bus device
1851 * device : video output device under the video
1852 * bus
1853 *
1854 * Return:
1855 * none
1856 *
1857 * Bind the ids with the corresponding video devices
1858 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861static void
Len Brown4be44fc2005-08-05 00:44:28 -04001862acpi_video_device_bind(struct acpi_video_bus *video,
1863 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001865 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001866 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001868 for (i = 0; i < video->attached_count; i++) {
1869 ids = &video->attached_array[i];
1870 if (device->device_id == (ids->value.int_val & 0xffff)) {
1871 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1873 }
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/*
1878 * Arg:
1879 * video : video bus device
1880 *
1881 * Return:
1882 * < 0 : error
1883 *
1884 * Call _DOD to enumerate all devices attached to display adapter
1885 *
Len Brown4be44fc2005-08-05 00:44:28 -04001886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1889{
Len Brown4be44fc2005-08-05 00:44:28 -04001890 int status;
1891 int count;
1892 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001893 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001894 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1895 union acpi_object *dod = NULL;
1896 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Patrick Mochel90130262006-05-19 16:54:48 -04001898 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001900 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001901 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001904 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001906 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 status = -EFAULT;
1908 goto out;
1909 }
1910
1911 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001912 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001914 active_list = kcalloc(1 + dod->package.count,
1915 sizeof(struct acpi_video_enumerated_device),
1916 GFP_KERNEL);
1917 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 status = -ENOMEM;
1919 goto out;
1920 }
1921
1922 count = 0;
1923 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001924 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001927 printk(KERN_ERR PREFIX
1928 "Invalid _DOD data in element %d\n", i);
1929 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001931
1932 active_list[count].value.int_val = obj->integer.value;
1933 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001934 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1935 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 count++;
1937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jesper Juhl6044ec82005-11-07 01:01:32 -08001939 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001940
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001941 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001943
1944 out:
Len Brown02438d82006-06-30 03:19:10 -04001945 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001946 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Len Brown4be44fc2005-08-05 00:44:28 -04001949static int
1950acpi_video_get_next_level(struct acpi_video_device *device,
1951 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001953 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001954 max = max_below = 0;
1955 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001956 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001957 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001958 l = device->brightness->levels[i];
1959 if (abs(l - level_current) < abs(delta)) {
1960 delta = l - level_current;
1961 if (!delta)
1962 break;
1963 }
1964 }
1965 /* Ajust level_current to closest available level */
1966 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001967 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001968 l = device->brightness->levels[i];
1969 if (l < min)
1970 min = l;
1971 if (l > max)
1972 max = l;
1973 if (l < min_above && l > level_current)
1974 min_above = l;
1975 if (l > max_below && l < level_current)
1976 max_below = l;
1977 }
1978
1979 switch (event) {
1980 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1981 return (level_current < max) ? min_above : min;
1982 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1983 return (level_current < max) ? min_above : max;
1984 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1985 return (level_current > min) ? max_below : min;
1986 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1987 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1988 return 0;
1989 default:
1990 return level_current;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Zhang Ruic8890f92009-03-18 16:27:08 +08001994static int
Len Brown4be44fc2005-08-05 00:44:28 -04001995acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001997 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001998 int result = -EINVAL;
1999
Zhang Rui28c32e92009-07-13 10:33:24 +08002000 /* no warning message if acpi_backlight=vendor is used */
2001 if (!acpi_video_backlight_support())
2002 return 0;
2003
Julia Jomantaite469778c2008-06-23 22:50:42 +01002004 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08002005 goto out;
2006
2007 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05002008 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08002009 if (result)
2010 goto out;
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08002013
Zhang Rui24450c72009-03-18 16:27:10 +08002014 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08002015
Matthew Garrett36342742009-07-14 17:06:03 +01002016 if (!result)
2017 backlight_force_update(device->backlight,
2018 BACKLIGHT_UPDATE_HOTKEY);
2019
Zhang Ruic8890f92009-03-18 16:27:08 +08002020out:
2021 if (result)
2022 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2023
2024 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027static int
Len Brown4be44fc2005-08-05 00:44:28 -04002028acpi_video_bus_get_devices(struct acpi_video_bus *video,
2029 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Len Brown4be44fc2005-08-05 00:44:28 -04002031 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002032 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 acpi_video_device_enumerate(video);
2035
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002036 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 status = acpi_video_bus_get_one_device(dev, video);
2039 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002040 printk(KERN_WARNING PREFIX
2041 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 continue;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002045 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Len Brown4be44fc2005-08-05 00:44:28 -04002048static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Karol Kozimor031ec772005-07-30 04:18:00 -04002050 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct acpi_video_bus *video;
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002055 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 video = device->video;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 acpi_video_device_remove_fs(device->dev);
2060
Patrick Mochel90130262006-05-19 16:54:48 -04002061 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002062 ACPI_DEVICE_NOTIFY,
2063 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002064 if (device->backlight) {
2065 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2066 backlight_device_unregister(device->backlight);
2067 device->backlight = NULL;
2068 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002069 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002070 sysfs_remove_link(&device->dev->dev.kobj,
2071 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002072 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002073 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002074 thermal_cooling_device_unregister(device->cooling_dev);
2075 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002076 }
Luming Yu23b0f012007-05-09 21:07:05 +08002077 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002078
Patrick Mocheld550d982006-06-27 00:41:40 -04002079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Len Brown4be44fc2005-08-05 00:44:28 -04002082static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Len Brown4be44fc2005-08-05 00:44:28 -04002084 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002085 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002087 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002089 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002091 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002092 if (ACPI_FAILURE(status))
2093 printk(KERN_WARNING PREFIX
2094 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002096 if (dev->brightness) {
2097 kfree(dev->brightness->levels);
2098 kfree(dev->brightness);
2099 }
2100 list_del(&dev->entry);
2101 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002104 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002105
Patrick Mocheld550d982006-06-27 00:41:40 -04002106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109/* acpi_video interface */
2110
Len Brown4be44fc2005-08-05 00:44:28 -04002111static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Zhang Ruia21101c2007-09-14 11:46:22 +08002113 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Len Brown4be44fc2005-08-05 00:44:28 -04002116static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 return acpi_video_bus_DOS(video, 0, 1);
2119}
2120
Bjorn Helgaas70155582009-04-07 15:37:11 +00002121static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002123 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002124 struct input_dev *input;
2125 int keycode;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002128 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Luming Yue9dab192007-08-20 18:23:53 +08002130 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002133 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002135 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002136 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
2138
Julius Volz98fb8fe2007-02-20 16:38:40 +01002139 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 * connector. */
2141 acpi_video_device_enumerate(video);
2142 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002143 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002144 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 break;
2146
Len Brown4be44fc2005-08-05 00:44:28 -04002147 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002148 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002149 keycode = KEY_SWITCHVIDEOMODE;
2150 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002151 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002152 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002153 keycode = KEY_VIDEO_NEXT;
2154 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002155 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002156 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002157 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 break;
2159
2160 default:
Luming Yue9dab192007-08-20 18:23:53 +08002161 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002163 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 break;
2165 }
2166
Zhang Rui7761f632008-01-25 14:48:12 +08002167 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002168 input_report_key(input, keycode, 1);
2169 input_sync(input);
2170 input_report_key(input, keycode, 0);
2171 input_sync(input);
2172
Patrick Mocheld550d982006-06-27 00:41:40 -04002173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Len Brown4be44fc2005-08-05 00:44:28 -04002176static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002178 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002179 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002180 struct acpi_video_bus *bus;
2181 struct input_dev *input;
2182 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002187 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002188 bus = video_device->video;
2189 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002192 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002193 if (brightness_switch_enabled)
2194 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002195 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002196 keycode = KEY_BRIGHTNESS_CYCLE;
2197 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002198 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002199 if (brightness_switch_enabled)
2200 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002201 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002202 keycode = KEY_BRIGHTNESSUP;
2203 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002204 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002205 if (brightness_switch_enabled)
2206 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002207 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002208 keycode = KEY_BRIGHTNESSDOWN;
2209 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002210 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002211 if (brightness_switch_enabled)
2212 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002213 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002214 keycode = KEY_BRIGHTNESS_ZERO;
2215 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002216 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002217 if (brightness_switch_enabled)
2218 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002219 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002220 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 break;
2222 default:
Luming Yue9dab192007-08-20 18:23:53 +08002223 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002225 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 break;
2227 }
Luming Yue9dab192007-08-20 18:23:53 +08002228
Zhang Rui7761f632008-01-25 14:48:12 +08002229 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002230 input_report_key(input, keycode, 1);
2231 input_sync(input);
2232 input_report_key(input, keycode, 0);
2233 input_sync(input);
2234
Patrick Mocheld550d982006-06-27 00:41:40 -04002235 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Zhang Ruie6d9da12007-08-25 02:23:31 -04002238static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002239static int acpi_video_resume(struct acpi_device *device)
2240{
2241 struct acpi_video_bus *video;
2242 struct acpi_video_device *video_device;
2243 int i;
2244
2245 if (!device || !acpi_driver_data(device))
2246 return -EINVAL;
2247
2248 video = acpi_driver_data(device);
2249
2250 for (i = 0; i < video->attached_count; i++) {
2251 video_device = video->attached_array[i].bind_info;
2252 if (video_device && video_device->backlight)
2253 acpi_video_set_brightness(video_device->backlight);
2254 }
2255 return AE_OK;
2256}
2257
Zhang Ruic504f8c2009-12-30 15:59:23 +08002258static acpi_status
2259acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2260 void **return_value)
2261{
2262 struct acpi_device *device = context;
2263 struct acpi_device *sibling;
2264 int result;
2265
2266 if (handle == device->handle)
2267 return AE_CTRL_TERMINATE;
2268
2269 result = acpi_bus_get_device(handle, &sibling);
2270 if (result)
2271 return AE_OK;
2272
2273 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2274 return AE_ALREADY_EXISTS;
2275
2276 return AE_OK;
2277}
2278
Len Brown4be44fc2005-08-05 00:44:28 -04002279static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002281 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002282 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002283 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002284 acpi_status status;
2285
2286 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2287 device->parent->handle, 1,
2288 acpi_video_bus_match, NULL,
2289 device, NULL);
2290 if (status == AE_ALREADY_EXISTS) {
2291 printk(KERN_WARNING FW_BUG
2292 "Duplicate ACPI video bus devices for the"
2293 " same VGA controller, please try module "
2294 "parameter \"video.allow_duplicates=1\""
2295 "if the current driver doesn't work.\n");
2296 if (!allow_duplicates)
2297 return -ENODEV;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Burman Yan36bcbec2006-12-19 12:56:11 -08002300 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002302 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Zhang Ruie6d9da12007-08-25 02:23:31 -04002304 /* a hack to fix the duplicate name "VID" problem on T61 */
2305 if (!strcmp(device->pnp.bus_id, "VID")) {
2306 if (instance)
2307 device->pnp.bus_id[3] = '0' + instance;
2308 instance ++;
2309 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002310 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2311 if (!strcmp(device->pnp.bus_id, "VGA")) {
2312 if (instance)
2313 device->pnp.bus_id[3] = '0' + instance;
2314 instance++;
2315 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002316
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002317 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2319 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002320 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002323 error = acpi_video_bus_check(video);
2324 if (error)
2325 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002327 error = acpi_video_bus_add_fs(device);
2328 if (error)
2329 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002331 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 INIT_LIST_HEAD(&video->video_device_list);
2333
2334 acpi_video_bus_get_devices(video, device);
2335 acpi_video_bus_start_devices(video);
2336
Luming Yue9dab192007-08-20 18:23:53 +08002337 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002338 if (!input) {
2339 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002340 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002341 }
Luming Yue9dab192007-08-20 18:23:53 +08002342
2343 snprintf(video->phys, sizeof(video->phys),
2344 "%s/video/input0", acpi_device_hid(video->device));
2345
2346 input->name = acpi_device_name(video->device);
2347 input->phys = video->phys;
2348 input->id.bustype = BUS_HOST;
2349 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002350 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002351 input->evbit[0] = BIT(EV_KEY);
2352 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2353 set_bit(KEY_VIDEO_NEXT, input->keybit);
2354 set_bit(KEY_VIDEO_PREV, input->keybit);
2355 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2356 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2357 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2358 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2359 set_bit(KEY_DISPLAY_OFF, input->keybit);
2360 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002361
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002362 error = input_register_device(input);
2363 if (error)
2364 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002367 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2368 video->flags.multihead ? "yes" : "no",
2369 video->flags.rom ? "yes" : "no",
2370 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002374 err_free_input_dev:
2375 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002376 err_stop_video:
2377 acpi_video_bus_stop_devices(video);
2378 acpi_video_bus_put_devices(video);
2379 kfree(video->attached_array);
2380 acpi_video_bus_remove_fs(device);
2381 err_free_video:
2382 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002383 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002384
2385 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Len Brown4be44fc2005-08-05 00:44:28 -04002388static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Len Brown4be44fc2005-08-05 00:44:28 -04002390 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002396 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 acpi_video_bus_put_devices(video);
2400 acpi_video_bus_remove_fs(device);
2401
Luming Yue9dab192007-08-20 18:23:53 +08002402 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002403 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 kfree(video);
2405
Patrick Mocheld550d982006-06-27 00:41:40 -04002406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Matthew Garrett74a365b2009-03-19 21:35:39 +00002409static int __init intel_opregion_present(void)
2410{
2411#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2412 struct pci_dev *dev = NULL;
2413 u32 address;
2414
2415 for_each_pci_dev(dev) {
2416 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2417 continue;
2418 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2419 continue;
2420 pci_read_config_dword(dev, 0xfc, &address);
2421 if (!address)
2422 continue;
2423 return 1;
2424 }
2425#endif
2426 return 0;
2427}
2428
2429int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Len Brown4be44fc2005-08-05 00:44:28 -04002431 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002432 if (register_count) {
2433 /*
2434 * if the function of acpi_video_register is already called,
2435 * don't register the acpi_vide_bus again and return no error.
2436 */
2437 return 0;
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2441 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002442 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 result = acpi_bus_register_driver(&acpi_video_bus);
2445 if (result < 0) {
2446 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002447 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449
Zhao Yakui86e437f2009-06-16 11:23:13 +08002450 /*
2451 * When the acpi_video_bus is loaded successfully, increase
2452 * the counter reference.
2453 */
2454 register_count = 1;
2455
Patrick Mocheld550d982006-06-27 00:41:40 -04002456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002458EXPORT_SYMBOL(acpi_video_register);
2459
Zhao Yakui86e437f2009-06-16 11:23:13 +08002460void acpi_video_unregister(void)
2461{
2462 if (!register_count) {
2463 /*
2464 * If the acpi video bus is already unloaded, don't
2465 * unload it again and return directly.
2466 */
2467 return;
2468 }
2469 acpi_bus_unregister_driver(&acpi_video_bus);
2470
2471 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2472
2473 register_count = 0;
2474
2475 return;
2476}
2477EXPORT_SYMBOL(acpi_video_unregister);
2478
Matthew Garrett74a365b2009-03-19 21:35:39 +00002479/*
2480 * This is kind of nasty. Hardware using Intel chipsets may require
2481 * the video opregion code to be run first in order to initialise
2482 * state before any ACPI video calls are made. To handle this we defer
2483 * registration of the video class until the opregion code has run.
2484 */
2485
2486static int __init acpi_video_init(void)
2487{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002488 dmi_check_system(video_dmi_table);
2489
Matthew Garrett74a365b2009-03-19 21:35:39 +00002490 if (intel_opregion_present())
2491 return 0;
2492
2493 return acpi_video_register();
2494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Zhao Yakui86e437f2009-06-16 11:23:13 +08002496static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002498 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Patrick Mocheld550d982006-06-27 00:41:40 -04002500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503module_init(acpi_video_init);
2504module_exit(acpi_video_exit);