blob: 9865d46f49a8faabfe0930640b2fe3bbd10b4b89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * video.c - ACPI Video Driver ($Revision:$)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
Luming Yue9dab192007-08-20 18:23:53 +080035#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080036#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080037#include <linux/thermal.h>
Luming Yu23b0f012007-05-09 21:07:05 +080038#include <linux/video_output.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050039#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000040#include <linux/pci.h>
41#include <linux/pci_ids.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040044#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +020047#include <linux/suspend.h>
Matthew Garrette92a7162010-01-12 14:17:03 -050048#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_VIDEO_BUS_NAME "Video Bus"
54#define ACPI_VIDEO_DEVICE_NAME "Video Device"
55#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
56#define ACPI_VIDEO_NOTIFY_PROBE 0x81
57#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
58#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
59#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
60
Thomas Tuttlef4715182006-12-19 12:56:14 -080061#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
62#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
63#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
64#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
65#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Yu Luming2f3d0002006-11-11 02:40:34 +080067#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050070ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Len Brownf52fd662007-02-12 22:42:12 -050072MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050073MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_LICENSE("GPL");
75
Zhang Rui8a681a42008-01-25 14:47:49 +080076static int brightness_switch_enabled = 1;
77module_param(brightness_switch_enabled, bool, 0644);
78
Zhang Ruic504f8c2009-12-30 15:59:23 +080079/*
80 * By default, we don't allow duplicate ACPI video bus devices
81 * under the same VGA controller
82 */
83static int allow_duplicates;
84module_param(allow_duplicates, bool, 0644);
85
Zhao Yakui86e437f2009-06-16 11:23:13 +080086static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040087static int acpi_video_bus_add(struct acpi_device *device);
88static int acpi_video_bus_remove(struct acpi_device *device, int type);
Bjorn Helgaas70155582009-04-07 15:37:11 +000089static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Thomas Renninger1ba90e32007-07-23 14:44:41 +020091static const struct acpi_device_id video_device_ids[] = {
92 {ACPI_VIDEO_HID, 0},
93 {"", 0},
94};
95MODULE_DEVICE_TABLE(acpi, video_device_ids);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050098 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200100 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .ops = {
102 .add = acpi_video_bus_add,
103 .remove = acpi_video_bus_remove,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000104 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400105 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400109 u8 multihead:1; /* can switch video heads */
110 u8 rom:1; /* can retrieve a video rom */
111 u8 post:1; /* can configure the head to */
112 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400116 u8 _DOS:1; /*Enable/Disable output switching */
117 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
118 u8 _ROM:1; /*Get ROM Data */
119 u8 _GPD:1; /*Get POST Device */
120 u8 _SPD:1; /*Set POST Device */
121 u8 _VPO:1; /*Video POST Options */
122 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125struct acpi_video_device_attrib {
126 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100127 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400128 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100129 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400130 u32 bios_can_detect:1; /*BIOS can detect the device */
131 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
132 the VGA device. */
133 u32 pipe_id:3; /*For VGA multiple-head devices. */
134 u32 reserved:10; /*Must be 0 */
135 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138struct acpi_video_enumerated_device {
139 union {
140 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 } value;
143 struct acpi_video_device *bind_info;
144};
145
146struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400147 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400148 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 u8 attached_count;
151 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500154 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400155 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800156 struct input_dev *input;
157 char phys[32]; /* for input device */
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +0200158 struct notifier_block pm_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
161struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400162 u8 crt:1;
163 u8 lcd:1;
164 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500165 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 bios:1;
167 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500168 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400172 u8 _ADR:1; /*Return the unique ID */
173 u8 _BCL:1; /*Query list of brightness control levels supported */
174 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800175 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800176 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400177 u8 _DDC:1; /*Return the EDID for this device */
178 u8 _DCS:1; /*Return status of output device */
179 u8 _DGS:1; /*Query graphics state */
180 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Zhang Ruid32f6942009-03-18 16:27:12 +0800183struct acpi_video_brightness_flags {
184 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800185 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800186 u8 _BCL_use_index:1; /* levels in _BCL are index values */
187 u8 _BCM_use_index:1; /* input of _BCM is an index value */
188 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800189};
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400192 int curr;
193 int count;
194 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800195 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400199 unsigned long device_id;
200 struct acpi_video_device_flags flags;
201 struct acpi_video_device_cap cap;
202 struct list_head entry;
203 struct acpi_video_bus *video;
204 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800206 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400207 struct thermal_cooling_device *cooling_dev;
Luming Yu23b0f012007-05-09 21:07:05 +0800208 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* bus */
212static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100213static const struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700214 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400215 .open = acpi_video_bus_info_open_fs,
216 .read = seq_read,
217 .llseek = seq_lseek,
218 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100222static const struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700223 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400224 .open = acpi_video_bus_ROM_open_fs,
225 .read = seq_read,
226 .llseek = seq_lseek,
227 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
Len Brown4be44fc2005-08-05 00:44:28 -0400230static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
231 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100232static const struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700233 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400234 .open = acpi_video_bus_POST_info_open_fs,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
240static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400241static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100242 const char __user *buffer, size_t count, loff_t *data);
243static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700244 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400245 .open = acpi_video_bus_POST_open_fs,
246 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100247 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400248 .llseek = seq_lseek,
249 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400253static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100254 const char __user *buffer, size_t count, loff_t *data);
255static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700256 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400257 .open = acpi_video_bus_DOS_open_fs,
258 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100259 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400260 .llseek = seq_lseek,
261 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
264/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400265static int acpi_video_device_info_open_fs(struct inode *inode,
266 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100267static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700268 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400269 .open = acpi_video_device_info_open_fs,
270 .read = seq_read,
271 .llseek = seq_lseek,
272 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
Len Brown4be44fc2005-08-05 00:44:28 -0400275static int acpi_video_device_state_open_fs(struct inode *inode,
276 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400277static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100278 const char __user *buffer, size_t count, loff_t *data);
279static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700280 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400281 .open = acpi_video_device_state_open_fs,
282 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100283 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400284 .llseek = seq_lseek,
285 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288static int acpi_video_device_brightness_open_fs(struct inode *inode,
289 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400290static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100291 const char __user *buffer, size_t count, loff_t *data);
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700292static const struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700293 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400294 .open = acpi_video_device_brightness_open_fs,
295 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100296 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400297 .llseek = seq_lseek,
298 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301static int acpi_video_device_EDID_open_fs(struct inode *inode,
302 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100303static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700304 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400305 .open = acpi_video_device_EDID_open_fs,
306 .read = seq_read,
307 .llseek = seq_lseek,
308 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100311static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 "motherboard VGA device",
313 "PCI VGA device",
314 "AGP VGA device",
315 "UNKNOWN",
316};
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
319static void acpi_video_device_rebind(struct acpi_video_bus *video);
320static void acpi_video_device_bind(struct acpi_video_bus *video,
321 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800323static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
324 int level);
325static int acpi_video_device_lcd_get_level_current(
326 struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500327 unsigned long long *level, int init);
Len Brown4be44fc2005-08-05 00:44:28 -0400328static int acpi_video_get_next_level(struct acpi_video_device *device,
329 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800330static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400331 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800332static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400333 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800334static int acpi_video_output_get(struct output_device *od);
335static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Yu Luming2f3d0002006-11-11 02:40:34 +0800337/*backlight device sysfs support*/
338static int acpi_video_get_brightness(struct backlight_device *bd)
339{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400340 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000341 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800342 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100343 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800344
Matthew Garrett70287db2010-02-16 16:53:50 -0500345 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800346 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000347 for (i = 2; i < vd->brightness->count; i++) {
348 if (vd->brightness->levels[i] == cur_level)
349 /* The first two entries are special - see page 575
350 of the ACPI spec 3.0 */
351 return i-2;
352 }
353 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800354}
355
356static int acpi_video_set_brightness(struct backlight_device *bd)
357{
Zhang Rui24450c72009-03-18 16:27:10 +0800358 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800359 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100360 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800361
362 return acpi_video_device_lcd_set_level(vd,
363 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800364}
365
Richard Purdie599a52d2007-02-10 23:07:48 +0000366static struct backlight_ops acpi_backlight_ops = {
367 .get_brightness = acpi_video_get_brightness,
368 .update_status = acpi_video_set_brightness,
369};
370
Luming Yu23b0f012007-05-09 21:07:05 +0800371/*video output device sysfs support*/
372static int acpi_video_output_get(struct output_device *od)
373{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400374 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800375 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700376 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800377 acpi_video_device_get_state(vd, &state);
378 return (int)state;
379}
380
381static int acpi_video_output_set(struct output_device *od)
382{
383 unsigned long state = od->request_state;
384 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700385 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800386 return acpi_video_device_set_state(vd, state);
387}
388
389static struct output_properties acpi_output_properties = {
390 .set_state = acpi_video_output_set,
391 .get_status = acpi_video_output_get,
392};
Zhang Rui702ed512008-01-17 15:51:22 +0800393
394
395/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400396static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000397 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800398{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400399 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800400 struct acpi_video_device *video = acpi_driver_data(device);
401
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000402 *state = video->brightness->count - 3;
403 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800404}
405
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400406static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000407 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800408{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400409 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800410 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400411 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000412 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800413
Matthew Garrett70287db2010-02-16 16:53:50 -0500414 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
Zhang Ruic8890f92009-03-18 16:27:08 +0800415 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000416 for (offset = 2; offset < video->brightness->count; offset++)
417 if (level == video->brightness->levels[offset]) {
418 *state = video->brightness->count - offset - 1;
419 return 0;
420 }
Zhang Rui702ed512008-01-17 15:51:22 +0800421
422 return -EINVAL;
423}
424
425static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400426video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800427{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400428 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800429 struct acpi_video_device *video = acpi_driver_data(device);
430 int level;
431
432 if ( state >= video->brightness->count - 2)
433 return -EINVAL;
434
435 state = video->brightness->count - state;
436 level = video->brightness->levels[state -1];
437 return acpi_video_device_lcd_set_level(video, level);
438}
439
440static struct thermal_cooling_device_ops video_cooling_ops = {
441 .get_max_state = video_get_max_state,
442 .get_cur_state = video_get_cur_state,
443 .set_cur_state = video_set_cur_state,
444};
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/* --------------------------------------------------------------------------
447 Video Management
448 -------------------------------------------------------------------------- */
449
450/* device */
451
452static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400453acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Len Brown4be44fc2005-08-05 00:44:28 -0400455 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400456
457 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Patrick Mocheld550d982006-06-27 00:41:40 -0400459 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462static int
Len Brown4be44fc2005-08-05 00:44:28 -0400463acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400464 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Len Brown4be44fc2005-08-05 00:44:28 -0400466 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Patrick Mochel90130262006-05-19 16:54:48 -0400468 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473static int
Len Brown4be44fc2005-08-05 00:44:28 -0400474acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Len Brown4be44fc2005-08-05 00:44:28 -0400476 int status;
477 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
478 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400479 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400483 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Patrick Mocheld550d982006-06-27 00:41:40 -0400485 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static int
Len Brown4be44fc2005-08-05 00:44:28 -0400489acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
490 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Len Brown4be44fc2005-08-05 00:44:28 -0400492 int status;
493 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
494 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 *levels = NULL;
498
Patrick Mochel90130262006-05-19 16:54:48 -0400499 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400502 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500503 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400504 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 status = -EFAULT;
506 goto err;
507 }
508
509 *levels = obj;
510
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Len Brown4be44fc2005-08-05 00:44:28 -0400513 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800514 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Patrick Mocheld550d982006-06-27 00:41:40 -0400516 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519static int
Len Brown4be44fc2005-08-05 00:44:28 -0400520acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Zhang Rui24450c72009-03-18 16:27:10 +0800522 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400523 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
524 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800525 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Zhang Rui24450c72009-03-18 16:27:10 +0800529 status = acpi_evaluate_object(device->dev->handle, "_BCM",
530 &args, NULL);
531 if (ACPI_FAILURE(status)) {
532 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
533 return -EIO;
534 }
535
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400536 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800537 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800538 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800539 if (device->backlight)
540 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800541 return 0;
542 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800543
Zhang Rui24450c72009-03-18 16:27:10 +0800544 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Zhang Rui45cb50e2009-04-24 12:13:18 -0400548/*
549 * For some buggy _BQC methods, we need to add a constant value to
550 * the _BQC return value to get the actual current brightness level
551 */
552
553static int bqc_offset_aml_bug_workaround;
554static int __init video_set_bqc_offset(const struct dmi_system_id *d)
555{
556 bqc_offset_aml_bug_workaround = 9;
557 return 0;
558}
559
560static struct dmi_system_id video_dmi_table[] __initdata = {
561 /*
562 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
563 */
564 {
565 .callback = video_set_bqc_offset,
566 .ident = "Acer Aspire 5720",
567 .matches = {
568 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
569 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
570 },
571 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400572 {
573 .callback = video_set_bqc_offset,
574 .ident = "Acer Aspire 5710Z",
575 .matches = {
576 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
577 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
578 },
579 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400580 {
581 .callback = video_set_bqc_offset,
582 .ident = "eMachines E510",
583 .matches = {
584 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
585 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
586 },
587 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400588 {
589 .callback = video_set_bqc_offset,
590 .ident = "Acer Aspire 5315",
591 .matches = {
592 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
593 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
594 },
595 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800596 {
597 .callback = video_set_bqc_offset,
598 .ident = "Acer Aspire 7720",
599 .matches = {
600 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
601 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
602 },
603 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400604 {}
605};
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static int
Len Brown4be44fc2005-08-05 00:44:28 -0400608acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Garrett70287db2010-02-16 16:53:50 -0500609 unsigned long long *level, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Zhang Ruic8890f92009-03-18 16:27:08 +0800611 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800612 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800613
Zhang Ruic60d6382009-03-18 16:27:18 +0800614 if (device->cap._BQC || device->cap._BCQ) {
615 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
616
617 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800618 NULL, level);
619 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800620 if (device->brightness->flags._BQC_use_index) {
621 if (device->brightness->flags._BCL_reversed)
622 *level = device->brightness->count
623 - 3 - (*level);
624 *level = device->brightness->levels[*level + 2];
625
626 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400627 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800628 for (i = 2; i < device->brightness->count; i++)
629 if (device->brightness->levels[i] == *level) {
630 device->brightness->curr = *level;
631 return 0;
632 }
Matthew Garrett70287db2010-02-16 16:53:50 -0500633 if (!init) {
634 /*
635 * BQC returned an invalid level.
636 * Stop using it.
637 */
638 ACPI_WARNING((AE_INFO,
639 "%s returned an invalid level",
640 buf));
641 device->cap._BQC = device->cap._BCQ = 0;
642 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800643 } else {
644 /* Fixme:
645 * should we return an error or ignore this failure?
646 * dev->brightness->curr is a cached value which stores
647 * the correct current backlight level in most cases.
648 * ACPI video backlight still works w/ buggy _BQC.
649 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
650 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800651 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
652 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800653 }
654 }
655
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400656 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static int
Len Brown4be44fc2005-08-05 00:44:28 -0400661acpi_video_device_EDID(struct acpi_video_device *device,
662 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Len Brown4be44fc2005-08-05 00:44:28 -0400664 int status;
665 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
666 union acpi_object *obj;
667 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
668 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 *edid = NULL;
672
673 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (length == 128)
676 arg0.integer.value = 1;
677 else if (length == 256)
678 arg0.integer.value = 2;
679 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Patrick Mochel90130262006-05-19 16:54:48 -0400682 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200686 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (obj && obj->type == ACPI_TYPE_BUFFER)
689 *edid = obj;
690 else {
Len Brown64684632006-06-26 23:41:38 -0400691 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 status = -EFAULT;
693 kfree(obj);
694 }
695
Patrick Mocheld550d982006-06-27 00:41:40 -0400696 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/* bus */
700
701static int
Len Brown4be44fc2005-08-05 00:44:28 -0400702acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Len Brown4be44fc2005-08-05 00:44:28 -0400704 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400705 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400706 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
707 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 arg0.integer.value = option;
711
Patrick Mochel90130262006-05-19 16:54:48 -0400712 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400714 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400720acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int status;
723
Patrick Mochel90130262006-05-19 16:54:48 -0400724 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729static int
Len Brown4be44fc2005-08-05 00:44:28 -0400730acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400731 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Len Brown4be44fc2005-08-05 00:44:28 -0400733 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Patrick Mochel90130262006-05-19 16:54:48 -0400735 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *options &= 3;
737
Patrick Mocheld550d982006-06-27 00:41:40 -0400738 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741/*
742 * Arg:
743 * video : video bus device pointer
744 * bios_flag :
745 * 0. The system BIOS should NOT automatically switch(toggle)
746 * the active display output.
747 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100748 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * 2. The _DGS value should be locked.
750 * 3. The system BIOS should not automatically switch (toggle) the
751 * active display output, but instead generate the display switch
752 * event notify code.
753 * lcd_flag :
754 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100755 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100757 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * Return Value:
759 * -1 wrong arg.
760 */
761
762static int
Len Brown4be44fc2005-08-05 00:44:28 -0400763acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Lin Ming439913f2010-01-28 10:53:19 +0800765 u64 status = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400766 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
767 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Len Brown4be44fc2005-08-05 00:44:28 -0400770 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 status = -1;
772 goto Failed;
773 }
774 arg0.integer.value = (lcd_flag << 2) | bios_flag;
775 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400776 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Len Brown4be44fc2005-08-05 00:44:28 -0400778 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500783 * Simple comparison function used to sort backlight levels.
784 */
785
786static int
787acpi_video_cmp_level(const void *a, const void *b)
788{
789 return *(int *)a - *(int *)b;
790}
791
792/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * Arg:
794 * device : video output device (LCD, CRT, ..)
795 *
796 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100797 * Maximum brightness level
798 *
799 * Allocate and initialize device->brightness.
800 */
801
802static int
803acpi_video_init_brightness(struct acpi_video_device *device)
804{
805 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800806 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800807 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100808 union acpi_object *o;
809 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800810 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100811
812 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
813 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
814 "LCD brightness level\n"));
815 goto out;
816 }
817
818 if (obj->package.count < 2)
819 goto out;
820
821 br = kzalloc(sizeof(*br), GFP_KERNEL);
822 if (!br) {
823 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800824 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100825 goto out;
826 }
827
Zhang Ruid32f6942009-03-18 16:27:12 +0800828 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100829 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800830 if (!br->levels) {
831 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100832 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800833 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100834
835 for (i = 0; i < obj->package.count; i++) {
836 o = (union acpi_object *)&obj->package.elements[i];
837 if (o->type != ACPI_TYPE_INTEGER) {
838 printk(KERN_ERR PREFIX "Invalid data\n");
839 continue;
840 }
841 br->levels[count] = (u32) o->integer.value;
842
843 if (br->levels[count] > max_level)
844 max_level = br->levels[count];
845 count++;
846 }
847
Zhang Ruid32f6942009-03-18 16:27:12 +0800848 /*
849 * some buggy BIOS don't export the levels
850 * when machine is on AC/Battery in _BCL package.
851 * In this case, the first two elements in _BCL packages
852 * are also supported brightness levels that OS should take care of.
853 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800854 for (i = 2; i < count; i++) {
855 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800856 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800857 if (br->levels[i] == br->levels[1])
858 level_ac_battery++;
859 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500860
Zhang Ruid32f6942009-03-18 16:27:12 +0800861 if (level_ac_battery < 2) {
862 level_ac_battery = 2 - level_ac_battery;
863 br->flags._BCL_no_ac_battery_levels = 1;
864 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
865 br->levels[i] = br->levels[i - level_ac_battery];
866 count += level_ac_battery;
867 } else if (level_ac_battery > 2)
868 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
869
Zhang Ruid80fb992009-03-18 16:27:14 +0800870 /* Check if the _BCL package is in a reversed order */
871 if (max_level == br->levels[2]) {
872 br->flags._BCL_reversed = 1;
873 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
874 acpi_video_cmp_level, NULL);
875 } else if (max_level != br->levels[count - 1])
876 ACPI_ERROR((AE_INFO,
877 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100878
879 br->count = count;
880 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800881
882 /* Check the input/output of _BQC/_BCL/_BCM */
883 if ((max_level < 100) && (max_level <= (count - 2)))
884 br->flags._BCL_use_index = 1;
885
886 /*
887 * _BCM is always consistent with _BCL,
888 * at least for all the laptops we have ever seen.
889 */
890 br->flags._BCM_use_index = br->flags._BCL_use_index;
891
892 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400893 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800894
895 if (!device->cap._BQC)
896 goto set_level;
897
Matthew Garrett70287db2010-02-16 16:53:50 -0500898 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800899 if (result)
900 goto out_free_levels;
901
Zhang Ruie047cca2009-04-09 14:24:35 +0800902 /*
903 * Set the level to maximum and check if _BQC uses indexed value
904 */
905 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800906 if (result)
907 goto out_free_levels;
908
Matthew Garrett70287db2010-02-16 16:53:50 -0500909 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800910 if (result)
911 goto out_free_levels;
912
Zhang Ruie047cca2009-04-09 14:24:35 +0800913 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800914
Zhang Rui90c53ca2009-08-31 12:39:54 -0400915 if (!br->flags._BQC_use_index) {
916 /*
917 * Set the backlight to the initial state.
918 * On some buggy laptops, _BQC returns an uninitialized value
919 * when invoked for the first time, i.e. level_old is invalid.
920 * set the backlight to max_level in this case
921 */
922 for (i = 2; i < br->count; i++)
923 if (level_old == br->levels[i])
924 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800925 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400926 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800927
928 if (br->flags._BCL_reversed)
929 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400930 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800931
932set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400933 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800934 if (result)
935 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800936
Zhang Ruid32f6942009-03-18 16:27:12 +0800937 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
938 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100939 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800940 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100941
942out_free_levels:
943 kfree(br->levels);
944out_free:
945 kfree(br);
946out:
947 device->brightness = NULL;
948 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800949 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100950}
951
952/*
953 * Arg:
954 * device : video output device (LCD, CRT, ..)
955 *
956 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * None
958 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100959 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * device.
961 */
962
Len Brown4be44fc2005-08-05 00:44:28 -0400963static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Patrick Mochel90130262006-05-19 16:54:48 -0400967 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 device->cap._ADR = 1;
969 }
Patrick Mochel90130262006-05-19 16:54:48 -0400970 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400971 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Patrick Mochel90130262006-05-19 16:54:48 -0400973 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400974 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800976 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
977 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800978 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
979 &h_dummy1))) {
980 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
981 device->cap._BCQ = 1;
982 }
983
Patrick Mochel90130262006-05-19 16:54:48 -0400984 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400985 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Patrick Mochel90130262006-05-19 16:54:48 -0400987 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 device->cap._DCS = 1;
989 }
Patrick Mochel90130262006-05-19 16:54:48 -0400990 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 device->cap._DGS = 1;
992 }
Patrick Mochel90130262006-05-19 16:54:48 -0400993 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 device->cap._DSS = 1;
995 }
996
Zhang Rui1a7c6182009-03-18 16:27:16 +0800997 if (acpi_video_backlight_support()) {
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500998 struct backlight_properties props;
Zhang Rui702ed512008-01-17 15:51:22 +0800999 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +08001000 static int count = 0;
1001 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +08001002
1003 result = acpi_video_init_brightness(device);
1004 if (result)
1005 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -07001006 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Yu Luming2f3d0002006-11-11 02:40:34 +08001007 if (!name)
1008 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -07001009 count++;
Yu Luming2f3d0002006-11-11 02:40:34 +08001010
Matthew Garretta19a6ee2010-02-17 16:39:44 -05001011 memset(&props, 0, sizeof(struct backlight_properties));
1012 props.max_brightness = device->brightness->count - 3;
1013 device->backlight = backlight_device_register(name, NULL, device,
1014 &acpi_backlight_ops,
1015 &props);
Yu Luming2f3d0002006-11-11 02:40:34 +08001016 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +08001017 if (IS_ERR(device->backlight))
1018 return;
Zhang Rui702ed512008-01-17 15:51:22 +08001019
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02001020 /*
1021 * Save current brightness level in case we have to restore it
1022 * before acpi_video_device_lcd_set_level() is called next time.
1023 */
1024 device->backlight->props.brightness =
1025 acpi_video_get_brightness(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001026
Zhang Rui056c3082009-06-22 11:31:14 +08001027 result = sysfs_create_link(&device->backlight->dev.kobj,
1028 &device->dev->dev.kobj, "device");
1029 if (result)
1030 printk(KERN_ERR PREFIX "Create sysfs link\n");
1031
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001032 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001033 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001034 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001035 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001036 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001037 * free it.
1038 * Also, why the hell we are returning early and
1039 * not attempt to register video output if cooling
1040 * device registration failed?
1041 * -- dtor
1042 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001043 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001044 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001045 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001046
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001047 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001048 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001049 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001050 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001051 "thermal_cooling");
1052 if (result)
1053 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001054 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001055 &device->dev->dev.kobj, "device");
1056 if (result)
1057 printk(KERN_ERR PREFIX "Create sysfs link\n");
1058
Yu Luming2f3d0002006-11-11 02:40:34 +08001059 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001060
1061 if (acpi_video_display_switch_support()) {
1062
1063 if (device->cap._DCS && device->cap._DSS) {
1064 static int count;
1065 char *name;
Julia Lawallaeb834d2010-04-27 14:06:04 -07001066 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001067 if (!name)
1068 return;
Julia Lawallaeb834d2010-04-27 14:06:04 -07001069 count++;
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001070 device->output_dev = video_output_register(name,
1071 NULL, device, &acpi_output_properties);
1072 kfree(name);
1073 }
Luming Yu23b0f012007-05-09 21:07:05 +08001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/*
1078 * Arg:
1079 * device : video output device (VGA)
1080 *
1081 * Return Value:
1082 * None
1083 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001084 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086
Len Brown4be44fc2005-08-05 00:44:28 -04001087static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Len Brown4be44fc2005-08-05 00:44:28 -04001089 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Patrick Mochel90130262006-05-19 16:54:48 -04001091 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 video->cap._DOS = 1;
1093 }
Patrick Mochel90130262006-05-19 16:54:48 -04001094 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 video->cap._DOD = 1;
1096 }
Patrick Mochel90130262006-05-19 16:54:48 -04001097 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 video->cap._ROM = 1;
1099 }
Patrick Mochel90130262006-05-19 16:54:48 -04001100 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 video->cap._GPD = 1;
1102 }
Patrick Mochel90130262006-05-19 16:54:48 -04001103 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 video->cap._SPD = 1;
1105 }
Patrick Mochel90130262006-05-19 16:54:48 -04001106 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 video->cap._VPO = 1;
1108 }
1109}
1110
1111/*
1112 * Check whether the video bus device has required AML method to
1113 * support the desired features
1114 */
1115
Len Brown4be44fc2005-08-05 00:44:28 -04001116static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Len Brown4be44fc2005-08-05 00:44:28 -04001118 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001119 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001124 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001125 if (!dev)
1126 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001127 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Since there is no HID, CID and so on for VGA driver, we have
1130 * to check well known required nodes.
1131 */
1132
Julius Volz98fb8fe2007-02-20 16:38:40 +01001133 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001134 if (video->cap._DOS || video->cap._DOD) {
1135 if (!video->cap._DOS) {
1136 printk(KERN_WARNING FW_BUG
1137 "ACPI(%s) defines _DOD but not _DOS\n",
1138 acpi_device_bid(video->device));
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 video->flags.multihead = 1;
1141 status = 0;
1142 }
1143
Julius Volz98fb8fe2007-02-20 16:38:40 +01001144 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001145 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 video->flags.rom = 1;
1147 status = 0;
1148 }
1149
Julius Volz98fb8fe2007-02-20 16:38:40 +01001150 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001151 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 video->flags.post = 1;
1153 status = 0;
1154 }
1155
Patrick Mocheld550d982006-06-27 00:41:40 -04001156 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159/* --------------------------------------------------------------------------
1160 FS Interface (/proc)
1161 -------------------------------------------------------------------------- */
1162
Len Brown4be44fc2005-08-05 00:44:28 -04001163static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165/* video devices */
1166
Len Brown4be44fc2005-08-05 00:44:28 -04001167static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001169 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (!dev)
1173 goto end;
1174
1175 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1176 seq_printf(seq, "type: ");
1177 if (dev->flags.crt)
1178 seq_printf(seq, "CRT\n");
1179 else if (dev->flags.lcd)
1180 seq_printf(seq, "LCD\n");
1181 else if (dev->flags.tvout)
1182 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001183 else if (dev->flags.dvi)
1184 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 else
1186 seq_printf(seq, "UNKNOWN\n");
1187
Len Brown4be44fc2005-08-05 00:44:28 -04001188 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Len Brown4be44fc2005-08-05 00:44:28 -04001190 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static int
Len Brown4be44fc2005-08-05 00:44:28 -04001195acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 return single_open(file, acpi_video_device_info_seq_show,
1198 PDE(inode)->data);
1199}
1200
Len Brown4be44fc2005-08-05 00:44:28 -04001201static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Len Brown4be44fc2005-08-05 00:44:28 -04001203 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001204 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001205 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (!dev)
1209 goto end;
1210
1211 status = acpi_video_device_get_state(dev, &state);
1212 seq_printf(seq, "state: ");
1213 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001214 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 else
1216 seq_printf(seq, "<not supported>\n");
1217
1218 status = acpi_video_device_query(dev, &state);
1219 seq_printf(seq, "query: ");
1220 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001221 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 else
1223 seq_printf(seq, "<not supported>\n");
1224
Len Brown4be44fc2005-08-05 00:44:28 -04001225 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static int
Len Brown4be44fc2005-08-05 00:44:28 -04001230acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 return single_open(file, acpi_video_device_state_seq_show,
1233 PDE(inode)->data);
1234}
1235
1236static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001237acpi_video_device_write_state(struct file *file,
1238 const char __user * buffer,
1239 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Len Brown4be44fc2005-08-05 00:44:28 -04001241 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001242 struct seq_file *m = file->private_data;
1243 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001244 char str[12] = { 0 };
1245 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001248 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001252 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 str[count] = 0;
1255 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001256 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 status = acpi_video_device_set_state(dev, state);
1259
1260 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001261 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Patrick Mocheld550d982006-06-27 00:41:40 -04001263 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static int
Len Brown4be44fc2005-08-05 00:44:28 -04001267acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001269 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001270 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (!dev || !dev->brightness) {
1274 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001279 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 seq_printf(seq, " %d", dev->brightness->levels[i]);
1281 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1282
Patrick Mocheld550d982006-06-27 00:41:40 -04001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286static int
Len Brown4be44fc2005-08-05 00:44:28 -04001287acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 return single_open(file, acpi_video_device_brightness_seq_show,
1290 PDE(inode)->data);
1291}
1292
1293static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001294acpi_video_device_write_brightness(struct file *file,
1295 const char __user * buffer,
1296 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001298 struct seq_file *m = file->private_data;
1299 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001300 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001301 unsigned int level = 0;
1302 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001305 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001309 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 str[count] = 0;
1312 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001315 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Julius Volz98fb8fe2007-02-20 16:38:40 +01001317 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001318 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001320 if (!acpi_video_device_lcd_set_level(dev, level))
1321 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323 }
1324
Zhang Rui24450c72009-03-18 16:27:10 +08001325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Len Brown4be44fc2005-08-05 00:44:28 -04001328static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001330 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001331 int status;
1332 int i;
1333 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (!dev)
1337 goto out;
1338
Len Brown4be44fc2005-08-05 00:44:28 -04001339 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001341 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343
1344 if (ACPI_FAILURE(status)) {
1345 goto out;
1346 }
1347
1348 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1349 for (i = 0; i < edid->buffer.length; i++)
1350 seq_putc(seq, edid->buffer.pointer[i]);
1351 }
1352
Len Brown4be44fc2005-08-05 00:44:28 -04001353 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (!edid)
1355 seq_printf(seq, "<not supported>\n");
1356 else
1357 kfree(edid);
1358
Patrick Mocheld550d982006-06-27 00:41:40 -04001359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362static int
Len Brown4be44fc2005-08-05 00:44:28 -04001363acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 return single_open(file, acpi_video_device_EDID_seq_show,
1366 PDE(inode)->data);
1367}
1368
Len Brown4be44fc2005-08-05 00:44:28 -04001369static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001371 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 struct acpi_video_device *vid_dev;
1373
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001374 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001376 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001378 device_dir = proc_mkdir(acpi_device_bid(device),
1379 vid_dev->video->dir);
1380 if (!device_dir)
1381 return -ENOMEM;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001384 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001385 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001387 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001390 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001391 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001392 &acpi_video_device_state_fops,
1393 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001395 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001398 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001399 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001400 &acpi_video_device_brightness_fops,
1401 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001403 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001406 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001407 &acpi_video_device_EDID_fops,
1408 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001410 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001412 acpi_device_dir(device) = device_dir;
1413
Patrick Mocheld550d982006-06-27 00:41:40 -04001414 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001415
1416 err_remove_brightness:
1417 remove_proc_entry("brightness", device_dir);
1418 err_remove_state:
1419 remove_proc_entry("state", device_dir);
1420 err_remove_info:
1421 remove_proc_entry("info", device_dir);
1422 err_remove_dir:
1423 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1424 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Len Brown4be44fc2005-08-05 00:44:28 -04001427static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001430 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001432 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001434 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001436 device_dir = acpi_device_dir(device);
1437 if (device_dir) {
1438 remove_proc_entry("info", device_dir);
1439 remove_proc_entry("state", device_dir);
1440 remove_proc_entry("brightness", device_dir);
1441 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001442 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 acpi_device_dir(device) = NULL;
1444 }
1445
Patrick Mocheld550d982006-06-27 00:41:40 -04001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001450static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001452 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (!video)
1456 goto end;
1457
1458 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001459 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001461 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001463 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Len Brown4be44fc2005-08-05 00:44:28 -04001465 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Len Brown4be44fc2005-08-05 00:44:28 -04001469static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Len Brown4be44fc2005-08-05 00:44:28 -04001471 return single_open(file, acpi_video_bus_info_seq_show,
1472 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Len Brown4be44fc2005-08-05 00:44:28 -04001475static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001477 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 if (!video)
1481 goto end;
1482
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001483 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 seq_printf(seq, "<TODO>\n");
1485
Len Brown4be44fc2005-08-05 00:44:28 -04001486 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Len Brown4be44fc2005-08-05 00:44:28 -04001490static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1493}
1494
Len Brown4be44fc2005-08-05 00:44:28 -04001495static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001497 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001498 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001499 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 if (!video)
1503 goto end;
1504
1505 status = acpi_video_bus_POST_options(video, &options);
1506 if (ACPI_SUCCESS(status)) {
1507 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001508 printk(KERN_WARNING PREFIX
1509 "The motherboard VGA device is not listed as a possible POST device.\n");
1510 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001511 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Frank Seidel4d939152009-02-04 17:03:07 +01001513 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001514 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (options & 2)
1516 seq_printf(seq, " <PCI video>");
1517 if (options & 4)
1518 seq_printf(seq, " <AGP video>");
1519 seq_putc(seq, '\n');
1520 } else
1521 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001522 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526static int
Len Brown4be44fc2005-08-05 00:44:28 -04001527acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Len Brown4be44fc2005-08-05 00:44:28 -04001529 return single_open(file, acpi_video_bus_POST_info_seq_show,
1530 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Len Brown4be44fc2005-08-05 00:44:28 -04001533static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001535 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001536 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001537 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 if (!video)
1541 goto end;
1542
Len Brown4be44fc2005-08-05 00:44:28 -04001543 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (!ACPI_SUCCESS(status)) {
1545 seq_printf(seq, "<not supported>\n");
1546 goto end;
1547 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001548 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Len Brown4be44fc2005-08-05 00:44:28 -04001550 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Len Brown4be44fc2005-08-05 00:44:28 -04001554static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001556 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Len Brown4be44fc2005-08-05 00:44:28 -04001559 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Len Brown4be44fc2005-08-05 00:44:28 -04001564static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Len Brown4be44fc2005-08-05 00:44:28 -04001566 return single_open(file, acpi_video_bus_POST_seq_show,
1567 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Len Brown4be44fc2005-08-05 00:44:28 -04001570static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1573}
1574
1575static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001576acpi_video_bus_write_POST(struct file *file,
1577 const char __user * buffer,
1578 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Len Brown4be44fc2005-08-05 00:44:28 -04001580 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001581 struct seq_file *m = file->private_data;
1582 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001583 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001584 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001587 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001588 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 status = acpi_video_bus_POST_options(video, &options);
1591 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001595 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 str[count] = 0;
1598 opt = strtoul(str, NULL, 0);
1599 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001600 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Julius Volz98fb8fe2007-02-20 16:38:40 +01001602 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 options |= 1;
1604
1605 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001606 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001608 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 }
1611
Patrick Mocheld550d982006-06-27 00:41:40 -04001612 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001616acpi_video_bus_write_DOS(struct file *file,
1617 const char __user * buffer,
1618 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Len Brown4be44fc2005-08-05 00:44:28 -04001620 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001621 struct seq_file *m = file->private_data;
1622 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001623 char str[12] = { 0 };
1624 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001627 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001631 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 str[count] = 0;
1634 opt = strtoul(str, NULL, 0);
1635 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001636 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Len Brown4be44fc2005-08-05 00:44:28 -04001638 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001641 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Patrick Mocheld550d982006-06-27 00:41:40 -04001643 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Len Brown4be44fc2005-08-05 00:44:28 -04001646static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001648 struct acpi_video_bus *video = acpi_driver_data(device);
1649 struct proc_dir_entry *device_dir;
1650 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001652 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1653 if (!device_dir)
1654 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001657 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001658 &acpi_video_bus_info_fops,
1659 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001661 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001664 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001665 &acpi_video_bus_ROM_fops,
1666 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001668 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001671 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001672 &acpi_video_bus_POST_info_fops,
1673 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001675 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001678 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001679 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001680 &acpi_video_bus_POST_fops,
1681 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001683 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001686 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001687 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001688 &acpi_video_bus_DOS_fops,
1689 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001691 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001693 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001694 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001695
1696 err_remove_post:
1697 remove_proc_entry("POST", device_dir);
1698 err_remove_post_info:
1699 remove_proc_entry("POST_info", device_dir);
1700 err_remove_rom:
1701 remove_proc_entry("ROM", device_dir);
1702 err_remove_info:
1703 remove_proc_entry("info", device_dir);
1704 err_remove_dir:
1705 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1706 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Len Brown4be44fc2005-08-05 00:44:28 -04001709static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001711 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001713 if (device_dir) {
1714 remove_proc_entry("info", device_dir);
1715 remove_proc_entry("ROM", device_dir);
1716 remove_proc_entry("POST_info", device_dir);
1717 remove_proc_entry("POST", device_dir);
1718 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001719 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 acpi_device_dir(device) = NULL;
1721 }
1722
Patrick Mocheld550d982006-06-27 00:41:40 -04001723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726/* --------------------------------------------------------------------------
1727 Driver Interface
1728 -------------------------------------------------------------------------- */
1729
1730/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001731static struct acpi_video_device_attrib*
1732acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1733{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001734 struct acpi_video_enumerated_device *ids;
1735 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001736
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001737 for (i = 0; i < video->attached_count; i++) {
1738 ids = &video->attached_array[i];
1739 if ((ids->value.int_val & 0xffff) == device_id)
1740 return &ids->value.attrib;
1741 }
1742
Rui Zhang82cae992007-01-03 23:40:53 -05001743 return NULL;
1744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746static int
Matthew Garrette92a7162010-01-12 14:17:03 -05001747acpi_video_get_device_type(struct acpi_video_bus *video,
1748 unsigned long device_id)
1749{
1750 struct acpi_video_enumerated_device *ids;
1751 int i;
1752
1753 for (i = 0; i < video->attached_count; i++) {
1754 ids = &video->attached_array[i];
1755 if ((ids->value.int_val & 0xffff) == device_id)
1756 return ids->value.int_val;
1757 }
1758
1759 return 0;
1760}
1761
1762static int
Len Brown4be44fc2005-08-05 00:44:28 -04001763acpi_video_bus_get_one_device(struct acpi_device *device,
1764 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001766 unsigned long long device_id;
Matthew Garrette92a7162010-01-12 14:17:03 -05001767 int status, device_type;
Len Brown4be44fc2005-08-05 00:44:28 -04001768 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001769 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Len Brown4be44fc2005-08-05 00:44:28 -04001774 status =
1775 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (ACPI_SUCCESS(status)) {
1777
Burman Yan36bcbec2006-12-19 12:56:11 -08001778 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001780 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1783 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001784 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 data->device_id = device_id;
1787 data->video = video;
1788 data->dev = device;
1789
Rui Zhang82cae992007-01-03 23:40:53 -05001790 attribute = acpi_video_get_device_attr(video, device_id);
1791
1792 if((attribute != NULL) && attribute->device_id_scheme) {
1793 switch (attribute->display_type) {
1794 case ACPI_VIDEO_DISPLAY_CRT:
1795 data->flags.crt = 1;
1796 break;
1797 case ACPI_VIDEO_DISPLAY_TV:
1798 data->flags.tvout = 1;
1799 break;
1800 case ACPI_VIDEO_DISPLAY_DVI:
1801 data->flags.dvi = 1;
1802 break;
1803 case ACPI_VIDEO_DISPLAY_LCD:
1804 data->flags.lcd = 1;
1805 break;
1806 default:
1807 data->flags.unknown = 1;
1808 break;
1809 }
1810 if(attribute->bios_can_detect)
1811 data->flags.bios = 1;
Matthew Garrette92a7162010-01-12 14:17:03 -05001812 } else {
1813 /* Check for legacy IDs */
1814 device_type = acpi_video_get_device_type(video,
1815 device_id);
1816 /* Ignore bits 16 and 18-20 */
1817 switch (device_type & 0xffe2ffff) {
1818 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1819 data->flags.crt = 1;
1820 break;
1821 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1822 data->flags.lcd = 1;
1823 break;
1824 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1825 data->flags.tvout = 1;
1826 break;
1827 default:
1828 data->flags.unknown = 1;
1829 }
1830 }
Len Brown4be44fc2005-08-05 00:44:28 -04001831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 acpi_video_device_bind(video, data);
1833 acpi_video_device_find_cap(data);
1834
Patrick Mochel90130262006-05-19 16:54:48 -04001835 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001836 ACPI_DEVICE_NOTIFY,
1837 acpi_video_device_notify,
1838 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001840 printk(KERN_ERR PREFIX
1841 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001842 if(data->brightness)
1843 kfree(data->brightness->levels);
1844 kfree(data->brightness);
1845 kfree(data);
1846 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001849 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001851 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 acpi_video_device_add_fs(device);
1854
Patrick Mocheld550d982006-06-27 00:41:40 -04001855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
Patrick Mocheld550d982006-06-27 00:41:40 -04001858 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861/*
1862 * Arg:
1863 * video : video bus device
1864 *
1865 * Return:
1866 * none
1867 *
1868 * Enumerate the video device list of the video bus,
1869 * bind the ids with the corresponding video devices
1870 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Len Brown4be44fc2005-08-05 00:44:28 -04001873static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001875 struct acpi_video_device *dev;
1876
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001877 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001878
1879 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001880 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001881
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001882 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885/*
1886 * Arg:
1887 * video : video bus device
1888 * device : video output device under the video
1889 * bus
1890 *
1891 * Return:
1892 * none
1893 *
1894 * Bind the ids with the corresponding video devices
1895 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898static void
Len Brown4be44fc2005-08-05 00:44:28 -04001899acpi_video_device_bind(struct acpi_video_bus *video,
1900 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001902 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001905 for (i = 0; i < video->attached_count; i++) {
1906 ids = &video->attached_array[i];
1907 if (device->device_id == (ids->value.int_val & 0xffff)) {
1908 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1910 }
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
1914/*
1915 * Arg:
1916 * video : video bus device
1917 *
1918 * Return:
1919 * < 0 : error
1920 *
1921 * Call _DOD to enumerate all devices attached to display adapter
1922 *
Len Brown4be44fc2005-08-05 00:44:28 -04001923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1926{
Len Brown4be44fc2005-08-05 00:44:28 -04001927 int status;
1928 int count;
1929 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001930 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001931 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1932 union acpi_object *dod = NULL;
1933 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Patrick Mochel90130262006-05-19 16:54:48 -04001935 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001937 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001938 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001941 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001943 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 status = -EFAULT;
1945 goto out;
1946 }
1947
1948 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001949 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001951 active_list = kcalloc(1 + dod->package.count,
1952 sizeof(struct acpi_video_enumerated_device),
1953 GFP_KERNEL);
1954 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 status = -ENOMEM;
1956 goto out;
1957 }
1958
1959 count = 0;
1960 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001961 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001964 printk(KERN_ERR PREFIX
1965 "Invalid _DOD data in element %d\n", i);
1966 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001968
1969 active_list[count].value.int_val = obj->integer.value;
1970 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001971 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1972 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 count++;
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Jesper Juhl6044ec82005-11-07 01:01:32 -08001976 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001977
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001978 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001980
1981 out:
Len Brown02438d82006-06-30 03:19:10 -04001982 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001983 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Len Brown4be44fc2005-08-05 00:44:28 -04001986static int
1987acpi_video_get_next_level(struct acpi_video_device *device,
1988 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001990 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001991 max = max_below = 0;
1992 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001993 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001994 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001995 l = device->brightness->levels[i];
1996 if (abs(l - level_current) < abs(delta)) {
1997 delta = l - level_current;
1998 if (!delta)
1999 break;
2000 }
2001 }
2002 /* Ajust level_current to closest available level */
2003 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08002004 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08002005 l = device->brightness->levels[i];
2006 if (l < min)
2007 min = l;
2008 if (l > max)
2009 max = l;
2010 if (l < min_above && l > level_current)
2011 min_above = l;
2012 if (l > max_below && l < level_current)
2013 max_below = l;
2014 }
2015
2016 switch (event) {
2017 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
2018 return (level_current < max) ? min_above : min;
2019 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
2020 return (level_current < max) ? min_above : max;
2021 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
2022 return (level_current > min) ? max_below : min;
2023 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
2024 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
2025 return 0;
2026 default:
2027 return level_current;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Zhang Ruic8890f92009-03-18 16:27:08 +08002031static int
Len Brown4be44fc2005-08-05 00:44:28 -04002032acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Matthew Wilcox27663c52008-10-10 02:22:59 -04002034 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08002035 int result = -EINVAL;
2036
Zhang Rui28c32e92009-07-13 10:33:24 +08002037 /* no warning message if acpi_backlight=vendor is used */
2038 if (!acpi_video_backlight_support())
2039 return 0;
2040
Julia Jomantaite469778c2008-06-23 22:50:42 +01002041 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08002042 goto out;
2043
2044 result = acpi_video_device_lcd_get_level_current(device,
Matthew Garrett70287db2010-02-16 16:53:50 -05002045 &level_current, 0);
Zhang Ruic8890f92009-03-18 16:27:08 +08002046 if (result)
2047 goto out;
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08002050
Zhang Rui24450c72009-03-18 16:27:10 +08002051 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08002052
Matthew Garrett36342742009-07-14 17:06:03 +01002053 if (!result)
2054 backlight_force_update(device->backlight,
2055 BACKLIGHT_UPDATE_HOTKEY);
2056
Zhang Ruic8890f92009-03-18 16:27:08 +08002057out:
2058 if (result)
2059 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2060
2061 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Matthew Garrette92a7162010-01-12 14:17:03 -05002064int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
2065 void **edid)
2066{
2067 struct acpi_video_bus *video;
2068 struct acpi_video_device *video_device;
2069 union acpi_object *buffer = NULL;
2070 acpi_status status;
2071 int i, length;
2072
2073 if (!device || !acpi_driver_data(device))
2074 return -EINVAL;
2075
2076 video = acpi_driver_data(device);
2077
2078 for (i = 0; i < video->attached_count; i++) {
2079 video_device = video->attached_array[i].bind_info;
2080 length = 256;
2081
2082 if (!video_device)
2083 continue;
2084
2085 if (type) {
2086 switch (type) {
2087 case ACPI_VIDEO_DISPLAY_CRT:
2088 if (!video_device->flags.crt)
2089 continue;
2090 break;
2091 case ACPI_VIDEO_DISPLAY_TV:
2092 if (!video_device->flags.tvout)
2093 continue;
2094 break;
2095 case ACPI_VIDEO_DISPLAY_DVI:
2096 if (!video_device->flags.dvi)
2097 continue;
2098 break;
2099 case ACPI_VIDEO_DISPLAY_LCD:
2100 if (!video_device->flags.lcd)
2101 continue;
2102 break;
2103 }
2104 } else if (video_device->device_id != device_id) {
2105 continue;
2106 }
2107
2108 status = acpi_video_device_EDID(video_device, &buffer, length);
2109
2110 if (ACPI_FAILURE(status) || !buffer ||
2111 buffer->type != ACPI_TYPE_BUFFER) {
2112 length = 128;
2113 status = acpi_video_device_EDID(video_device, &buffer,
2114 length);
2115 if (ACPI_FAILURE(status) || !buffer ||
2116 buffer->type != ACPI_TYPE_BUFFER) {
2117 continue;
2118 }
2119 }
2120
2121 *edid = buffer->buffer.pointer;
2122 return length;
2123 }
2124
2125 return -ENODEV;
2126}
2127EXPORT_SYMBOL(acpi_video_get_edid);
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129static int
Len Brown4be44fc2005-08-05 00:44:28 -04002130acpi_video_bus_get_devices(struct acpi_video_bus *video,
2131 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Len Brown4be44fc2005-08-05 00:44:28 -04002133 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002134 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 acpi_video_device_enumerate(video);
2137
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002138 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 status = acpi_video_bus_get_one_device(dev, video);
2141 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002142 printk(KERN_WARNING PREFIX
2143 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 continue;
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Len Brown4be44fc2005-08-05 00:44:28 -04002150static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Karol Kozimor031ec772005-07-30 04:18:00 -04002152 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct acpi_video_bus *video;
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002157 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 video = device->video;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 acpi_video_device_remove_fs(device->dev);
2162
Patrick Mochel90130262006-05-19 16:54:48 -04002163 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002164 ACPI_DEVICE_NOTIFY,
2165 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002166 if (device->backlight) {
2167 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2168 backlight_device_unregister(device->backlight);
2169 device->backlight = NULL;
2170 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002171 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002172 sysfs_remove_link(&device->dev->dev.kobj,
2173 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002174 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002175 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002176 thermal_cooling_device_unregister(device->cooling_dev);
2177 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002178 }
Luming Yu23b0f012007-05-09 21:07:05 +08002179 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002180
Patrick Mocheld550d982006-06-27 00:41:40 -04002181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Len Brown4be44fc2005-08-05 00:44:28 -04002184static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Len Brown4be44fc2005-08-05 00:44:28 -04002186 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002187 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002189 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002191 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002193 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002194 if (ACPI_FAILURE(status))
2195 printk(KERN_WARNING PREFIX
2196 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002198 if (dev->brightness) {
2199 kfree(dev->brightness->levels);
2200 kfree(dev->brightness);
2201 }
2202 list_del(&dev->entry);
2203 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002206 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002207
Patrick Mocheld550d982006-06-27 00:41:40 -04002208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211/* acpi_video interface */
2212
Len Brown4be44fc2005-08-05 00:44:28 -04002213static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Zhang Ruia21101c2007-09-14 11:46:22 +08002215 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Len Brown4be44fc2005-08-05 00:44:28 -04002218static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 return acpi_video_bus_DOS(video, 0, 1);
2221}
2222
Bjorn Helgaas70155582009-04-07 15:37:11 +00002223static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002225 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002226 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05002227 int keycode = 0;
Luming Yue9dab192007-08-20 18:23:53 +08002228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002230 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Luming Yue9dab192007-08-20 18:23:53 +08002232 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002235 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002237 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002238 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
2240
Julius Volz98fb8fe2007-02-20 16:38:40 +01002241 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * connector. */
2243 acpi_video_device_enumerate(video);
2244 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002245 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002246 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
2248
Len Brown4be44fc2005-08-05 00:44:28 -04002249 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002250 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002251 keycode = KEY_SWITCHVIDEOMODE;
2252 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002253 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002254 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002255 keycode = KEY_VIDEO_NEXT;
2256 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002257 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002258 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002259 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 break;
2261
2262 default:
2263 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002264 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 }
2267
Zhang Rui7761f632008-01-25 14:48:12 +08002268 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05002269
2270 if (keycode) {
2271 input_report_key(input, keycode, 1);
2272 input_sync(input);
2273 input_report_key(input, keycode, 0);
2274 input_sync(input);
2275 }
Luming Yue9dab192007-08-20 18:23:53 +08002276
Patrick Mocheld550d982006-06-27 00:41:40 -04002277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Len Brown4be44fc2005-08-05 00:44:28 -04002280static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002282 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002283 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002284 struct acpi_video_bus *bus;
2285 struct input_dev *input;
Matthew Garrett17c452f2009-12-11 17:40:46 -05002286 int keycode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002289 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002291 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002292 bus = video_device->video;
2293 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002296 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002297 if (brightness_switch_enabled)
2298 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002299 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002300 keycode = KEY_BRIGHTNESS_CYCLE;
2301 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002302 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002303 if (brightness_switch_enabled)
2304 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002305 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002306 keycode = KEY_BRIGHTNESSUP;
2307 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002308 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002309 if (brightness_switch_enabled)
2310 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002311 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002312 keycode = KEY_BRIGHTNESSDOWN;
2313 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002314 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002315 if (brightness_switch_enabled)
2316 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002317 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002318 keycode = KEY_BRIGHTNESS_ZERO;
2319 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002320 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002321 if (brightness_switch_enabled)
2322 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002323 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002324 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 break;
2326 default:
2327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002328 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 break;
2330 }
Luming Yue9dab192007-08-20 18:23:53 +08002331
Zhang Rui7761f632008-01-25 14:48:12 +08002332 acpi_notifier_call_chain(device, event, 0);
Matthew Garrett17c452f2009-12-11 17:40:46 -05002333
2334 if (keycode) {
2335 input_report_key(input, keycode, 1);
2336 input_sync(input);
2337 input_report_key(input, keycode, 0);
2338 input_sync(input);
2339 }
Luming Yue9dab192007-08-20 18:23:53 +08002340
Patrick Mocheld550d982006-06-27 00:41:40 -04002341 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002344static int acpi_video_resume(struct notifier_block *nb,
2345 unsigned long val, void *ign)
Matthew Garrett863c1492008-02-04 23:31:24 -08002346{
2347 struct acpi_video_bus *video;
2348 struct acpi_video_device *video_device;
2349 int i;
2350
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002351 switch (val) {
2352 case PM_HIBERNATION_PREPARE:
2353 case PM_SUSPEND_PREPARE:
2354 case PM_RESTORE_PREPARE:
2355 return NOTIFY_DONE;
2356 }
Matthew Garrett863c1492008-02-04 23:31:24 -08002357
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002358 video = container_of(nb, struct acpi_video_bus, pm_nb);
2359
2360 dev_info(&video->device->dev, "Restoring backlight state\n");
Matthew Garrett863c1492008-02-04 23:31:24 -08002361
2362 for (i = 0; i < video->attached_count; i++) {
2363 video_device = video->attached_array[i].bind_info;
2364 if (video_device && video_device->backlight)
2365 acpi_video_set_brightness(video_device->backlight);
2366 }
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002367
2368 return NOTIFY_OK;
Matthew Garrett863c1492008-02-04 23:31:24 -08002369}
2370
Zhang Ruic504f8c2009-12-30 15:59:23 +08002371static acpi_status
2372acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2373 void **return_value)
2374{
2375 struct acpi_device *device = context;
2376 struct acpi_device *sibling;
2377 int result;
2378
2379 if (handle == device->handle)
2380 return AE_CTRL_TERMINATE;
2381
2382 result = acpi_bus_get_device(handle, &sibling);
2383 if (result)
2384 return AE_OK;
2385
2386 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2387 return AE_ALREADY_EXISTS;
2388
2389 return AE_OK;
2390}
2391
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002392static int instance;
2393
Len Brown4be44fc2005-08-05 00:44:28 -04002394static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002396 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002397 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002398 int error;
Zhang Ruic504f8c2009-12-30 15:59:23 +08002399 acpi_status status;
2400
2401 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2402 device->parent->handle, 1,
2403 acpi_video_bus_match, NULL,
2404 device, NULL);
2405 if (status == AE_ALREADY_EXISTS) {
2406 printk(KERN_WARNING FW_BUG
2407 "Duplicate ACPI video bus devices for the"
2408 " same VGA controller, please try module "
2409 "parameter \"video.allow_duplicates=1\""
2410 "if the current driver doesn't work.\n");
2411 if (!allow_duplicates)
2412 return -ENODEV;
2413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Burman Yan36bcbec2006-12-19 12:56:11 -08002415 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Zhang Ruie6d9da12007-08-25 02:23:31 -04002419 /* a hack to fix the duplicate name "VID" problem on T61 */
2420 if (!strcmp(device->pnp.bus_id, "VID")) {
2421 if (instance)
2422 device->pnp.bus_id[3] = '0' + instance;
2423 instance ++;
2424 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002425 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2426 if (!strcmp(device->pnp.bus_id, "VGA")) {
2427 if (instance)
2428 device->pnp.bus_id[3] = '0' + instance;
2429 instance++;
2430 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002431
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002432 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2434 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002435 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002438 error = acpi_video_bus_check(video);
2439 if (error)
2440 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002442 error = acpi_video_bus_add_fs(device);
2443 if (error)
2444 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002446 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 INIT_LIST_HEAD(&video->video_device_list);
2448
2449 acpi_video_bus_get_devices(video, device);
2450 acpi_video_bus_start_devices(video);
2451
Luming Yue9dab192007-08-20 18:23:53 +08002452 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002453 if (!input) {
2454 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002455 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002456 }
Luming Yue9dab192007-08-20 18:23:53 +08002457
2458 snprintf(video->phys, sizeof(video->phys),
2459 "%s/video/input0", acpi_device_hid(video->device));
2460
2461 input->name = acpi_device_name(video->device);
2462 input->phys = video->phys;
2463 input->id.bustype = BUS_HOST;
2464 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002465 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002466 input->evbit[0] = BIT(EV_KEY);
2467 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2468 set_bit(KEY_VIDEO_NEXT, input->keybit);
2469 set_bit(KEY_VIDEO_PREV, input->keybit);
2470 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2471 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2472 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2473 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2474 set_bit(KEY_DISPLAY_OFF, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002475
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002476 error = input_register_device(input);
2477 if (error)
2478 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002481 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2482 video->flags.multihead ? "yes" : "no",
2483 video->flags.rom ? "yes" : "no",
2484 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002486 video->pm_nb.notifier_call = acpi_video_resume;
2487 video->pm_nb.priority = 0;
2488 register_pm_notifier(&video->pm_nb);
2489
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002492 err_free_input_dev:
2493 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002494 err_stop_video:
2495 acpi_video_bus_stop_devices(video);
2496 acpi_video_bus_put_devices(video);
2497 kfree(video->attached_array);
2498 acpi_video_bus_remove_fs(device);
2499 err_free_video:
2500 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002501 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002502
2503 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Len Brown4be44fc2005-08-05 00:44:28 -04002506static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Len Brown4be44fc2005-08-05 00:44:28 -04002508 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002514 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Rafael J. Wysockiac7729d2010-04-05 01:43:51 +02002516 unregister_pm_notifier(&video->pm_nb);
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 acpi_video_bus_put_devices(video);
2520 acpi_video_bus_remove_fs(device);
2521
Luming Yue9dab192007-08-20 18:23:53 +08002522 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002523 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 kfree(video);
2525
Patrick Mocheld550d982006-06-27 00:41:40 -04002526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Matthew Garrett74a365b2009-03-19 21:35:39 +00002529static int __init intel_opregion_present(void)
2530{
2531#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2532 struct pci_dev *dev = NULL;
2533 u32 address;
2534
2535 for_each_pci_dev(dev) {
2536 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2537 continue;
2538 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2539 continue;
2540 pci_read_config_dword(dev, 0xfc, &address);
2541 if (!address)
2542 continue;
2543 return 1;
2544 }
2545#endif
2546 return 0;
2547}
2548
2549int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Len Brown4be44fc2005-08-05 00:44:28 -04002551 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002552 if (register_count) {
2553 /*
2554 * if the function of acpi_video_register is already called,
2555 * don't register the acpi_vide_bus again and return no error.
2556 */
2557 return 0;
2558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2561 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002562 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 result = acpi_bus_register_driver(&acpi_video_bus);
2565 if (result < 0) {
2566 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002567 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569
Zhao Yakui86e437f2009-06-16 11:23:13 +08002570 /*
2571 * When the acpi_video_bus is loaded successfully, increase
2572 * the counter reference.
2573 */
2574 register_count = 1;
2575
Patrick Mocheld550d982006-06-27 00:41:40 -04002576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002578EXPORT_SYMBOL(acpi_video_register);
2579
Zhao Yakui86e437f2009-06-16 11:23:13 +08002580void acpi_video_unregister(void)
2581{
2582 if (!register_count) {
2583 /*
2584 * If the acpi video bus is already unloaded, don't
2585 * unload it again and return directly.
2586 */
2587 return;
2588 }
2589 acpi_bus_unregister_driver(&acpi_video_bus);
2590
2591 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2592
2593 register_count = 0;
2594
2595 return;
2596}
2597EXPORT_SYMBOL(acpi_video_unregister);
2598
Matthew Garrett74a365b2009-03-19 21:35:39 +00002599/*
2600 * This is kind of nasty. Hardware using Intel chipsets may require
2601 * the video opregion code to be run first in order to initialise
2602 * state before any ACPI video calls are made. To handle this we defer
2603 * registration of the video class until the opregion code has run.
2604 */
2605
2606static int __init acpi_video_init(void)
2607{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002608 dmi_check_system(video_dmi_table);
2609
Matthew Garrett74a365b2009-03-19 21:35:39 +00002610 if (intel_opregion_present())
2611 return 0;
2612
2613 return acpi_video_register();
2614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Zhao Yakui86e437f2009-06-16 11:23:13 +08002616static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002618 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Patrick Mocheld550d982006-06-27 00:41:40 -04002620 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623module_init(acpi_video_init);
2624module_exit(acpi_video_exit);