blob: 3f685db1a76df61754524484ed530694342413da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * video.c - ACPI Video Driver ($Revision:$)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
Luming Yue9dab192007-08-20 18:23:53 +080035#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080036#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080037#include <linux/thermal.h>
Luming Yu23b0f012007-05-09 21:07:05 +080038#include <linux/video_output.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050039#include <linux/sort.h>
Matthew Garrett74a365b2009-03-19 21:35:39 +000040#include <linux/pci.h>
41#include <linux/pci_ids.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Len Browneb27cae2009-07-06 23:40:19 -040043#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_VIDEO_BUS_NAME "Video Bus"
51#define ACPI_VIDEO_DEVICE_NAME "Video Device"
52#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
53#define ACPI_VIDEO_NOTIFY_PROBE 0x81
54#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
55#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
56#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
57
Thomas Tuttlef4715182006-12-19 12:56:14 -080058#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
59#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
60#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
61#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
62#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yu Luming2f3d0002006-11-11 02:40:34 +080064#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Rui Zhang82cae992007-01-03 23:40:53 -050066#define ACPI_VIDEO_DISPLAY_CRT 1
67#define ACPI_VIDEO_DISPLAY_TV 2
68#define ACPI_VIDEO_DISPLAY_DVI 3
69#define ACPI_VIDEO_DISPLAY_LCD 4
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050072ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Len Brownf52fd662007-02-12 22:42:12 -050074MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050075MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076MODULE_LICENSE("GPL");
77
Zhang Rui8a681a42008-01-25 14:47:49 +080078static int brightness_switch_enabled = 1;
79module_param(brightness_switch_enabled, bool, 0644);
80
Zhao Yakui86e437f2009-06-16 11:23:13 +080081static int register_count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -040082static int acpi_video_bus_add(struct acpi_device *device);
83static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080084static int acpi_video_resume(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000085static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Thomas Renninger1ba90e32007-07-23 14:44:41 +020087static const struct acpi_device_id video_device_ids[] = {
88 {ACPI_VIDEO_HID, 0},
89 {"", 0},
90};
91MODULE_DEVICE_TABLE(acpi, video_device_ids);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050094 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020096 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .ops = {
98 .add = acpi_video_bus_add,
99 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -0800100 .resume = acpi_video_resume,
Bjorn Helgaas70155582009-04-07 15:37:11 +0000101 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400102 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u8 multihead:1; /* can switch video heads */
107 u8 rom:1; /* can retrieve a video rom */
108 u8 post:1; /* can configure the head to */
109 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400113 u8 _DOS:1; /*Enable/Disable output switching */
114 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
115 u8 _ROM:1; /*Get ROM Data */
116 u8 _GPD:1; /*Get POST Device */
117 u8 _SPD:1; /*Set POST Device */
118 u8 _VPO:1; /*Video POST Options */
119 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122struct acpi_video_device_attrib {
123 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100124 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400125 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100126 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400127 u32 bios_can_detect:1; /*BIOS can detect the device */
128 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
129 the VGA device. */
130 u32 pipe_id:3; /*For VGA multiple-head devices. */
131 u32 reserved:10; /*Must be 0 */
132 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135struct acpi_video_enumerated_device {
136 union {
137 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } value;
140 struct acpi_video_device *bind_info;
141};
142
143struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400144 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 u8 attached_count;
148 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400150 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500151 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400152 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800153 struct input_dev *input;
154 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
157struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400158 u8 crt:1;
159 u8 lcd:1;
160 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500161 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 u8 bios:1;
163 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500164 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 u8 _ADR:1; /*Return the unique ID */
169 u8 _BCL:1; /*Query list of brightness control levels supported */
170 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800171 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800172 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400173 u8 _DDC:1; /*Return the EDID for this device */
174 u8 _DCS:1; /*Return status of output device */
175 u8 _DGS:1; /*Query graphics state */
176 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Zhang Ruid32f6942009-03-18 16:27:12 +0800179struct acpi_video_brightness_flags {
180 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800181 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800182 u8 _BCL_use_index:1; /* levels in _BCL are index values */
183 u8 _BCM_use_index:1; /* input of _BCM is an index value */
184 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800185};
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 int curr;
189 int count;
190 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800191 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400195 unsigned long device_id;
196 struct acpi_video_device_flags flags;
197 struct acpi_video_device_cap cap;
198 struct list_head entry;
199 struct acpi_video_bus *video;
200 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800202 struct backlight_device *backlight;
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400203 struct thermal_cooling_device *cooling_dev;
Luming Yu23b0f012007-05-09 21:07:05 +0800204 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* bus */
208static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100209static const struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700210 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400211 .open = acpi_video_bus_info_open_fs,
212 .read = seq_read,
213 .llseek = seq_lseek,
214 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100218static const struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700219 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400220 .open = acpi_video_bus_ROM_open_fs,
221 .read = seq_read,
222 .llseek = seq_lseek,
223 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
Len Brown4be44fc2005-08-05 00:44:28 -0400226static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
227 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100228static const struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700229 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400230 .open = acpi_video_bus_POST_info_open_fs,
231 .read = seq_read,
232 .llseek = seq_lseek,
233 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400237static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100238 const char __user *buffer, size_t count, loff_t *data);
239static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700240 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 .open = acpi_video_bus_POST_open_fs,
242 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100243 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400244 .llseek = seq_lseek,
245 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400249static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100250 const char __user *buffer, size_t count, loff_t *data);
251static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700252 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400253 .open = acpi_video_bus_DOS_open_fs,
254 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100255 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400256 .llseek = seq_lseek,
257 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
260/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400261static int acpi_video_device_info_open_fs(struct inode *inode,
262 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100263static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700264 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400265 .open = acpi_video_device_info_open_fs,
266 .read = seq_read,
267 .llseek = seq_lseek,
268 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271static int acpi_video_device_state_open_fs(struct inode *inode,
272 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400273static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100274 const char __user *buffer, size_t count, loff_t *data);
275static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700276 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 .open = acpi_video_device_state_open_fs,
278 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100279 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400280 .llseek = seq_lseek,
281 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284static int acpi_video_device_brightness_open_fs(struct inode *inode,
285 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400286static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100287 const char __user *buffer, size_t count, loff_t *data);
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700288static const struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700289 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400290 .open = acpi_video_device_brightness_open_fs,
291 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100292 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400293 .llseek = seq_lseek,
294 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297static int acpi_video_device_EDID_open_fs(struct inode *inode,
298 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100299static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700300 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400301 .open = acpi_video_device_EDID_open_fs,
302 .read = seq_read,
303 .llseek = seq_lseek,
304 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100307static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 "motherboard VGA device",
309 "PCI VGA device",
310 "AGP VGA device",
311 "UNKNOWN",
312};
313
Len Brown4be44fc2005-08-05 00:44:28 -0400314static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
315static void acpi_video_device_rebind(struct acpi_video_bus *video);
316static void acpi_video_device_bind(struct acpi_video_bus *video,
317 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800319static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
320 int level);
321static int acpi_video_device_lcd_get_level_current(
322 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400323 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400324static int acpi_video_get_next_level(struct acpi_video_device *device,
325 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800326static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400327 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800328static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400329 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800330static int acpi_video_output_get(struct output_device *od);
331static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Yu Luming2f3d0002006-11-11 02:40:34 +0800333/*backlight device sysfs support*/
334static int acpi_video_get_brightness(struct backlight_device *bd)
335{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400336 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000337 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800338 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100339 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800340
341 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
342 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000343 for (i = 2; i < vd->brightness->count; i++) {
344 if (vd->brightness->levels[i] == cur_level)
345 /* The first two entries are special - see page 575
346 of the ACPI spec 3.0 */
347 return i-2;
348 }
349 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800350}
351
352static int acpi_video_set_brightness(struct backlight_device *bd)
353{
Zhang Rui24450c72009-03-18 16:27:10 +0800354 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800355 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100356 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800357
358 return acpi_video_device_lcd_set_level(vd,
359 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800360}
361
Richard Purdie599a52d2007-02-10 23:07:48 +0000362static struct backlight_ops acpi_backlight_ops = {
363 .get_brightness = acpi_video_get_brightness,
364 .update_status = acpi_video_set_brightness,
365};
366
Luming Yu23b0f012007-05-09 21:07:05 +0800367/*video output device sysfs support*/
368static int acpi_video_output_get(struct output_device *od)
369{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400370 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800371 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700372 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800373 acpi_video_device_get_state(vd, &state);
374 return (int)state;
375}
376
377static int acpi_video_output_set(struct output_device *od)
378{
379 unsigned long state = od->request_state;
380 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700381 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800382 return acpi_video_device_set_state(vd, state);
383}
384
385static struct output_properties acpi_output_properties = {
386 .set_state = acpi_video_output_set,
387 .get_status = acpi_video_output_get,
388};
Zhang Rui702ed512008-01-17 15:51:22 +0800389
390
391/* thermal cooling device callbacks */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400392static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000393 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800394{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400395 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800396 struct acpi_video_device *video = acpi_driver_data(device);
397
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000398 *state = video->brightness->count - 3;
399 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800400}
401
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400402static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000403 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800404{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400405 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800406 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400407 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000408 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800409
Zhang Ruic8890f92009-03-18 16:27:08 +0800410 if (acpi_video_device_lcd_get_level_current(video, &level))
411 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000412 for (offset = 2; offset < video->brightness->count; offset++)
413 if (level == video->brightness->levels[offset]) {
414 *state = video->brightness->count - offset - 1;
415 return 0;
416 }
Zhang Rui702ed512008-01-17 15:51:22 +0800417
418 return -EINVAL;
419}
420
421static int
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400422video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800423{
Dmitry Torokhov4a703a82009-08-29 23:03:16 -0400424 struct acpi_device *device = cooling_dev->devdata;
Zhang Rui702ed512008-01-17 15:51:22 +0800425 struct acpi_video_device *video = acpi_driver_data(device);
426 int level;
427
428 if ( state >= video->brightness->count - 2)
429 return -EINVAL;
430
431 state = video->brightness->count - state;
432 level = video->brightness->levels[state -1];
433 return acpi_video_device_lcd_set_level(video, level);
434}
435
436static struct thermal_cooling_device_ops video_cooling_ops = {
437 .get_max_state = video_get_max_state,
438 .get_cur_state = video_get_cur_state,
439 .set_cur_state = video_set_cur_state,
440};
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/* --------------------------------------------------------------------------
443 Video Management
444 -------------------------------------------------------------------------- */
445
446/* device */
447
448static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400449acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Len Brown4be44fc2005-08-05 00:44:28 -0400451 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400452
453 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Patrick Mocheld550d982006-06-27 00:41:40 -0400455 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static int
Len Brown4be44fc2005-08-05 00:44:28 -0400459acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400460 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Len Brown4be44fc2005-08-05 00:44:28 -0400462 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Patrick Mochel90130262006-05-19 16:54:48 -0400464 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469static int
Len Brown4be44fc2005-08-05 00:44:28 -0400470acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Len Brown4be44fc2005-08-05 00:44:28 -0400472 int status;
473 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
474 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400475 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400479 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Patrick Mocheld550d982006-06-27 00:41:40 -0400481 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static int
Len Brown4be44fc2005-08-05 00:44:28 -0400485acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
486 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Len Brown4be44fc2005-08-05 00:44:28 -0400488 int status;
489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
490 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 *levels = NULL;
494
Patrick Mochel90130262006-05-19 16:54:48 -0400495 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400497 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400498 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500499 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400500 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 status = -EFAULT;
502 goto err;
503 }
504
505 *levels = obj;
506
Patrick Mocheld550d982006-06-27 00:41:40 -0400507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Len Brown4be44fc2005-08-05 00:44:28 -0400509 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800510 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Patrick Mocheld550d982006-06-27 00:41:40 -0400512 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static int
Len Brown4be44fc2005-08-05 00:44:28 -0400516acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Zhang Rui24450c72009-03-18 16:27:10 +0800518 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400519 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
520 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800521 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Zhang Rui24450c72009-03-18 16:27:10 +0800525 status = acpi_evaluate_object(device->dev->handle, "_BCM",
526 &args, NULL);
527 if (ACPI_FAILURE(status)) {
528 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
529 return -EIO;
530 }
531
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400532 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800533 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800534 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800535 if (device->backlight)
536 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800537 return 0;
538 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800539
Zhang Rui24450c72009-03-18 16:27:10 +0800540 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Zhang Rui45cb50e2009-04-24 12:13:18 -0400544/*
545 * For some buggy _BQC methods, we need to add a constant value to
546 * the _BQC return value to get the actual current brightness level
547 */
548
549static int bqc_offset_aml_bug_workaround;
550static int __init video_set_bqc_offset(const struct dmi_system_id *d)
551{
552 bqc_offset_aml_bug_workaround = 9;
553 return 0;
554}
555
556static struct dmi_system_id video_dmi_table[] __initdata = {
557 /*
558 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
559 */
560 {
561 .callback = video_set_bqc_offset,
562 .ident = "Acer Aspire 5720",
563 .matches = {
564 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
565 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
566 },
567 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400568 {
569 .callback = video_set_bqc_offset,
570 .ident = "Acer Aspire 5710Z",
571 .matches = {
572 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
573 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
574 },
575 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400576 {
577 .callback = video_set_bqc_offset,
578 .ident = "eMachines E510",
579 .matches = {
580 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
581 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
582 },
583 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400584 {
585 .callback = video_set_bqc_offset,
586 .ident = "Acer Aspire 5315",
587 .matches = {
588 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
589 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
590 },
591 },
Zhang Rui152a4e62009-06-22 11:31:18 +0800592 {
593 .callback = video_set_bqc_offset,
594 .ident = "Acer Aspire 7720",
595 .matches = {
596 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
597 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
598 },
599 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400600 {}
601};
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int
Len Brown4be44fc2005-08-05 00:44:28 -0400604acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400605 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Zhang Ruic8890f92009-03-18 16:27:08 +0800607 acpi_status status = AE_OK;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800608 int i;
Zhang Ruic8890f92009-03-18 16:27:08 +0800609
Zhang Ruic60d6382009-03-18 16:27:18 +0800610 if (device->cap._BQC || device->cap._BCQ) {
611 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
612
613 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800614 NULL, level);
615 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800616 if (device->brightness->flags._BQC_use_index) {
617 if (device->brightness->flags._BCL_reversed)
618 *level = device->brightness->count
619 - 3 - (*level);
620 *level = device->brightness->levels[*level + 2];
621
622 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400623 *level += bqc_offset_aml_bug_workaround;
Vladimir Serbinenko4e231fa2009-06-24 15:17:36 +0800624 for (i = 2; i < device->brightness->count; i++)
625 if (device->brightness->levels[i] == *level) {
626 device->brightness->curr = *level;
627 return 0;
628 }
629 /* BQC returned an invalid level. Stop using it. */
630 ACPI_WARNING((AE_INFO, "%s returned an invalid level",
631 buf));
632 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800633 } else {
634 /* Fixme:
635 * should we return an error or ignore this failure?
636 * dev->brightness->curr is a cached value which stores
637 * the correct current backlight level in most cases.
638 * ACPI video backlight still works w/ buggy _BQC.
639 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
640 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800641 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
642 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800643 }
644 }
645
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400646 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static int
Len Brown4be44fc2005-08-05 00:44:28 -0400651acpi_video_device_EDID(struct acpi_video_device *device,
652 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Len Brown4be44fc2005-08-05 00:44:28 -0400654 int status;
655 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
656 union acpi_object *obj;
657 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
658 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 *edid = NULL;
662
663 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (length == 128)
666 arg0.integer.value = 1;
667 else if (length == 256)
668 arg0.integer.value = 2;
669 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400670 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Patrick Mochel90130262006-05-19 16:54:48 -0400672 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200676 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (obj && obj->type == ACPI_TYPE_BUFFER)
679 *edid = obj;
680 else {
Len Brown64684632006-06-26 23:41:38 -0400681 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 status = -EFAULT;
683 kfree(obj);
684 }
685
Patrick Mocheld550d982006-06-27 00:41:40 -0400686 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* bus */
690
691static int
Len Brown4be44fc2005-08-05 00:44:28 -0400692acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Len Brown4be44fc2005-08-05 00:44:28 -0400694 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400695 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400696 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
697 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 arg0.integer.value = option;
701
Patrick Mochel90130262006-05-19 16:54:48 -0400702 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400704 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Patrick Mocheld550d982006-06-27 00:41:40 -0400706 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400710acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 int status;
713
Patrick Mochel90130262006-05-19 16:54:48 -0400714 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
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
Len Brown4be44fc2005-08-05 00:44:28 -0400720acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400721 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Len Brown4be44fc2005-08-05 00:44:28 -0400723 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Patrick Mochel90130262006-05-19 16:54:48 -0400725 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 *options &= 3;
727
Patrick Mocheld550d982006-06-27 00:41:40 -0400728 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731/*
732 * Arg:
733 * video : video bus device pointer
734 * bios_flag :
735 * 0. The system BIOS should NOT automatically switch(toggle)
736 * the active display output.
737 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100738 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * 2. The _DGS value should be locked.
740 * 3. The system BIOS should not automatically switch (toggle) the
741 * active display output, but instead generate the display switch
742 * event notify code.
743 * lcd_flag :
744 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100745 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100747 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * Return Value:
749 * -1 wrong arg.
750 */
751
752static int
Len Brown4be44fc2005-08-05 00:44:28 -0400753acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Len Brown4be44fc2005-08-05 00:44:28 -0400755 acpi_integer status = 0;
756 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
757 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Len Brown4be44fc2005-08-05 00:44:28 -0400760 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 status = -1;
762 goto Failed;
763 }
764 arg0.integer.value = (lcd_flag << 2) | bios_flag;
765 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400766 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Len Brown4be44fc2005-08-05 00:44:28 -0400768 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400769 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500773 * Simple comparison function used to sort backlight levels.
774 */
775
776static int
777acpi_video_cmp_level(const void *a, const void *b)
778{
779 return *(int *)a - *(int *)b;
780}
781
782/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * Arg:
784 * device : video output device (LCD, CRT, ..)
785 *
786 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100787 * Maximum brightness level
788 *
789 * Allocate and initialize device->brightness.
790 */
791
792static int
793acpi_video_init_brightness(struct acpi_video_device *device)
794{
795 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800796 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800797 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100798 union acpi_object *o;
799 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800800 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100801
802 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
803 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
804 "LCD brightness level\n"));
805 goto out;
806 }
807
808 if (obj->package.count < 2)
809 goto out;
810
811 br = kzalloc(sizeof(*br), GFP_KERNEL);
812 if (!br) {
813 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800814 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100815 goto out;
816 }
817
Zhang Ruid32f6942009-03-18 16:27:12 +0800818 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100819 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800820 if (!br->levels) {
821 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100822 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800823 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100824
825 for (i = 0; i < obj->package.count; i++) {
826 o = (union acpi_object *)&obj->package.elements[i];
827 if (o->type != ACPI_TYPE_INTEGER) {
828 printk(KERN_ERR PREFIX "Invalid data\n");
829 continue;
830 }
831 br->levels[count] = (u32) o->integer.value;
832
833 if (br->levels[count] > max_level)
834 max_level = br->levels[count];
835 count++;
836 }
837
Zhang Ruid32f6942009-03-18 16:27:12 +0800838 /*
839 * some buggy BIOS don't export the levels
840 * when machine is on AC/Battery in _BCL package.
841 * In this case, the first two elements in _BCL packages
842 * are also supported brightness levels that OS should take care of.
843 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800844 for (i = 2; i < count; i++) {
845 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800846 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800847 if (br->levels[i] == br->levels[1])
848 level_ac_battery++;
849 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500850
Zhang Ruid32f6942009-03-18 16:27:12 +0800851 if (level_ac_battery < 2) {
852 level_ac_battery = 2 - level_ac_battery;
853 br->flags._BCL_no_ac_battery_levels = 1;
854 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
855 br->levels[i] = br->levels[i - level_ac_battery];
856 count += level_ac_battery;
857 } else if (level_ac_battery > 2)
858 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
859
Zhang Ruid80fb992009-03-18 16:27:14 +0800860 /* Check if the _BCL package is in a reversed order */
861 if (max_level == br->levels[2]) {
862 br->flags._BCL_reversed = 1;
863 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
864 acpi_video_cmp_level, NULL);
865 } else if (max_level != br->levels[count - 1])
866 ACPI_ERROR((AE_INFO,
867 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100868
869 br->count = count;
870 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800871
872 /* Check the input/output of _BQC/_BCL/_BCM */
873 if ((max_level < 100) && (max_level <= (count - 2)))
874 br->flags._BCL_use_index = 1;
875
876 /*
877 * _BCM is always consistent with _BCL,
878 * at least for all the laptops we have ever seen.
879 */
880 br->flags._BCM_use_index = br->flags._BCL_use_index;
881
882 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Rui90c53ca2009-08-31 12:39:54 -0400883 br->curr = level = max_level;
Zhang Ruie047cca2009-04-09 14:24:35 +0800884
885 if (!device->cap._BQC)
886 goto set_level;
887
Zhang Rui1a7c6182009-03-18 16:27:16 +0800888 result = acpi_video_device_lcd_get_level_current(device, &level_old);
889 if (result)
890 goto out_free_levels;
891
Zhang Ruie047cca2009-04-09 14:24:35 +0800892 /*
893 * Set the level to maximum and check if _BQC uses indexed value
894 */
895 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800896 if (result)
897 goto out_free_levels;
898
899 result = acpi_video_device_lcd_get_level_current(device, &level);
900 if (result)
901 goto out_free_levels;
902
Zhang Ruie047cca2009-04-09 14:24:35 +0800903 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800904
Zhang Rui90c53ca2009-08-31 12:39:54 -0400905 if (!br->flags._BQC_use_index) {
906 /*
907 * Set the backlight to the initial state.
908 * On some buggy laptops, _BQC returns an uninitialized value
909 * when invoked for the first time, i.e. level_old is invalid.
910 * set the backlight to max_level in this case
911 */
912 for (i = 2; i < br->count; i++)
913 if (level_old == br->levels[i])
914 level = level_old;
Zhang Ruie047cca2009-04-09 14:24:35 +0800915 goto set_level;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400916 }
Zhang Ruie047cca2009-04-09 14:24:35 +0800917
918 if (br->flags._BCL_reversed)
919 level_old = (br->count - 1) - level_old;
Zhang Rui90c53ca2009-08-31 12:39:54 -0400920 level = br->levels[level_old];
Zhang Ruie047cca2009-04-09 14:24:35 +0800921
922set_level:
Zhang Rui90c53ca2009-08-31 12:39:54 -0400923 result = acpi_video_device_lcd_set_level(device, level);
Zhang Ruie047cca2009-04-09 14:24:35 +0800924 if (result)
925 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800926
Zhang Ruid32f6942009-03-18 16:27:12 +0800927 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
928 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100929 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800930 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100931
932out_free_levels:
933 kfree(br->levels);
934out_free:
935 kfree(br);
936out:
937 device->brightness = NULL;
938 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800939 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100940}
941
942/*
943 * Arg:
944 * device : video output device (LCD, CRT, ..)
945 *
946 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * None
948 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100949 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * device.
951 */
952
Len Brown4be44fc2005-08-05 00:44:28 -0400953static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Patrick Mochel90130262006-05-19 16:54:48 -0400957 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 device->cap._ADR = 1;
959 }
Patrick Mochel90130262006-05-19 16:54:48 -0400960 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400961 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Patrick Mochel90130262006-05-19 16:54:48 -0400963 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400964 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800966 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
967 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800968 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
969 &h_dummy1))) {
970 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
971 device->cap._BCQ = 1;
972 }
973
Patrick Mochel90130262006-05-19 16:54:48 -0400974 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400975 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Patrick Mochel90130262006-05-19 16:54:48 -0400977 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 device->cap._DCS = 1;
979 }
Patrick Mochel90130262006-05-19 16:54:48 -0400980 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 device->cap._DGS = 1;
982 }
Patrick Mochel90130262006-05-19 16:54:48 -0400983 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 device->cap._DSS = 1;
985 }
986
Zhang Rui1a7c6182009-03-18 16:27:16 +0800987 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800988 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800989 static int count = 0;
990 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800991
992 result = acpi_video_init_brightness(device);
993 if (result)
994 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800995 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
996 if (!name)
997 return;
998
Yu Luming2f3d0002006-11-11 02:40:34 +0800999 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +08001000 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +00001001 NULL, device, &acpi_backlight_ops);
Yu Luming2f3d0002006-11-11 02:40:34 +08001002 kfree(name);
Zhang Ruie01ce792009-07-29 08:53:29 +08001003 if (IS_ERR(device->backlight))
1004 return;
1005 device->backlight->props.max_brightness = device->brightness->count-3;
Zhang Rui702ed512008-01-17 15:51:22 +08001006
Zhang Rui056c3082009-06-22 11:31:14 +08001007 result = sysfs_create_link(&device->backlight->dev.kobj,
1008 &device->dev->dev.kobj, "device");
1009 if (result)
1010 printk(KERN_ERR PREFIX "Create sysfs link\n");
1011
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001012 device->cooling_dev = thermal_cooling_device_register("LCD",
Zhang Rui702ed512008-01-17 15:51:22 +08001013 device->dev, &video_cooling_ops);
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001014 if (IS_ERR(device->cooling_dev)) {
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001015 /*
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001016 * Set cooling_dev to NULL so we don't crash trying to
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001017 * free it.
1018 * Also, why the hell we are returning early and
1019 * not attempt to register video output if cooling
1020 * device registration failed?
1021 * -- dtor
1022 */
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001023 device->cooling_dev = NULL;
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001024 return;
Dmitry Torokhov4b4fe3b2009-08-08 00:26:25 -07001025 }
Thomas Sujith43ff39f2008-02-15 18:29:18 -05001026
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001027 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001028 device->cooling_dev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001029 result = sysfs_create_link(&device->dev->dev.kobj,
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001030 &device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001031 "thermal_cooling");
1032 if (result)
1033 printk(KERN_ERR PREFIX "Create sysfs link\n");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04001034 result = sysfs_create_link(&device->cooling_dev->device.kobj,
Julia Lawall90300622008-04-11 10:09:24 +08001035 &device->dev->dev.kobj, "device");
1036 if (result)
1037 printk(KERN_ERR PREFIX "Create sysfs link\n");
1038
Yu Luming2f3d0002006-11-11 02:40:34 +08001039 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001040
1041 if (acpi_video_display_switch_support()) {
1042
1043 if (device->cap._DCS && device->cap._DSS) {
1044 static int count;
1045 char *name;
1046 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1047 if (!name)
1048 return;
1049 sprintf(name, "acpi_video%d", count++);
1050 device->output_dev = video_output_register(name,
1051 NULL, device, &acpi_output_properties);
1052 kfree(name);
1053 }
Luming Yu23b0f012007-05-09 21:07:05 +08001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/*
1058 * Arg:
1059 * device : video output device (VGA)
1060 *
1061 * Return Value:
1062 * None
1063 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001064 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
1066
Len Brown4be44fc2005-08-05 00:44:28 -04001067static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Len Brown4be44fc2005-08-05 00:44:28 -04001069 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Patrick Mochel90130262006-05-19 16:54:48 -04001071 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 video->cap._DOS = 1;
1073 }
Patrick Mochel90130262006-05-19 16:54:48 -04001074 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 video->cap._DOD = 1;
1076 }
Patrick Mochel90130262006-05-19 16:54:48 -04001077 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 video->cap._ROM = 1;
1079 }
Patrick Mochel90130262006-05-19 16:54:48 -04001080 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 video->cap._GPD = 1;
1082 }
Patrick Mochel90130262006-05-19 16:54:48 -04001083 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 video->cap._SPD = 1;
1085 }
Patrick Mochel90130262006-05-19 16:54:48 -04001086 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 video->cap._VPO = 1;
1088 }
1089}
1090
1091/*
1092 * Check whether the video bus device has required AML method to
1093 * support the desired features
1094 */
1095
Len Brown4be44fc2005-08-05 00:44:28 -04001096static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Len Brown4be44fc2005-08-05 00:44:28 -04001098 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001099 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001102 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001104 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001105 if (!dev)
1106 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001107 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Since there is no HID, CID and so on for VGA driver, we have
1110 * to check well known required nodes.
1111 */
1112
Julius Volz98fb8fe2007-02-20 16:38:40 +01001113 /* Does this device support video switching? */
Stefan Bader3a1151e2009-08-21 11:03:05 +02001114 if (video->cap._DOS || video->cap._DOD) {
1115 if (!video->cap._DOS) {
1116 printk(KERN_WARNING FW_BUG
1117 "ACPI(%s) defines _DOD but not _DOS\n",
1118 acpi_device_bid(video->device));
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 video->flags.multihead = 1;
1121 status = 0;
1122 }
1123
Julius Volz98fb8fe2007-02-20 16:38:40 +01001124 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001125 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 video->flags.rom = 1;
1127 status = 0;
1128 }
1129
Julius Volz98fb8fe2007-02-20 16:38:40 +01001130 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001131 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 video->flags.post = 1;
1133 status = 0;
1134 }
1135
Patrick Mocheld550d982006-06-27 00:41:40 -04001136 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139/* --------------------------------------------------------------------------
1140 FS Interface (/proc)
1141 -------------------------------------------------------------------------- */
1142
Len Brown4be44fc2005-08-05 00:44:28 -04001143static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145/* video devices */
1146
Len Brown4be44fc2005-08-05 00:44:28 -04001147static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001149 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (!dev)
1153 goto end;
1154
1155 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1156 seq_printf(seq, "type: ");
1157 if (dev->flags.crt)
1158 seq_printf(seq, "CRT\n");
1159 else if (dev->flags.lcd)
1160 seq_printf(seq, "LCD\n");
1161 else if (dev->flags.tvout)
1162 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001163 else if (dev->flags.dvi)
1164 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 else
1166 seq_printf(seq, "UNKNOWN\n");
1167
Len Brown4be44fc2005-08-05 00:44:28 -04001168 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Len Brown4be44fc2005-08-05 00:44:28 -04001170 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static int
Len Brown4be44fc2005-08-05 00:44:28 -04001175acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 return single_open(file, acpi_video_device_info_seq_show,
1178 PDE(inode)->data);
1179}
1180
Len Brown4be44fc2005-08-05 00:44:28 -04001181static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Len Brown4be44fc2005-08-05 00:44:28 -04001183 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001184 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001185 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (!dev)
1189 goto end;
1190
1191 status = acpi_video_device_get_state(dev, &state);
1192 seq_printf(seq, "state: ");
1193 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001194 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
1196 seq_printf(seq, "<not supported>\n");
1197
1198 status = acpi_video_device_query(dev, &state);
1199 seq_printf(seq, "query: ");
1200 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001201 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 else
1203 seq_printf(seq, "<not supported>\n");
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static int
Len Brown4be44fc2005-08-05 00:44:28 -04001210acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 return single_open(file, acpi_video_device_state_seq_show,
1213 PDE(inode)->data);
1214}
1215
1216static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001217acpi_video_device_write_state(struct file *file,
1218 const char __user * buffer,
1219 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Len Brown4be44fc2005-08-05 00:44:28 -04001221 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001222 struct seq_file *m = file->private_data;
1223 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001224 char str[12] = { 0 };
1225 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001228 if (!dev || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001232 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 str[count] = 0;
1235 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001236 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 status = acpi_video_device_set_state(dev, state);
1239
1240 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001241 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Patrick Mocheld550d982006-06-27 00:41:40 -04001243 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int
Len Brown4be44fc2005-08-05 00:44:28 -04001247acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001249 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001250 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (!dev || !dev->brightness) {
1254 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001259 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 seq_printf(seq, " %d", dev->brightness->levels[i]);
1261 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1262
Patrick Mocheld550d982006-06-27 00:41:40 -04001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static int
Len Brown4be44fc2005-08-05 00:44:28 -04001267acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 return single_open(file, acpi_video_device_brightness_seq_show,
1270 PDE(inode)->data);
1271}
1272
1273static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001274acpi_video_device_write_brightness(struct file *file,
1275 const char __user * buffer,
1276 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001278 struct seq_file *m = file->private_data;
1279 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001280 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001281 unsigned int level = 0;
1282 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001285 if (!dev || !dev->brightness || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 str[count] = 0;
1292 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001295 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Julius Volz98fb8fe2007-02-20 16:38:40 +01001297 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001298 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001300 if (!acpi_video_device_lcd_set_level(dev, level))
1301 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 }
1304
Zhang Rui24450c72009-03-18 16:27:10 +08001305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Len Brown4be44fc2005-08-05 00:44:28 -04001308static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001310 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001311 int status;
1312 int i;
1313 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (!dev)
1317 goto out;
1318
Len Brown4be44fc2005-08-05 00:44:28 -04001319 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001321 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 if (ACPI_FAILURE(status)) {
1325 goto out;
1326 }
1327
1328 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1329 for (i = 0; i < edid->buffer.length; i++)
1330 seq_putc(seq, edid->buffer.pointer[i]);
1331 }
1332
Len Brown4be44fc2005-08-05 00:44:28 -04001333 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!edid)
1335 seq_printf(seq, "<not supported>\n");
1336 else
1337 kfree(edid);
1338
Patrick Mocheld550d982006-06-27 00:41:40 -04001339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static int
Len Brown4be44fc2005-08-05 00:44:28 -04001343acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 return single_open(file, acpi_video_device_EDID_seq_show,
1346 PDE(inode)->data);
1347}
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001351 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct acpi_video_device *vid_dev;
1353
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001354 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001356 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001358 device_dir = proc_mkdir(acpi_device_bid(device),
1359 vid_dev->video->dir);
1360 if (!device_dir)
1361 return -ENOMEM;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001364 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001365 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001367 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001370 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001371 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001372 &acpi_video_device_state_fops,
1373 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001375 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001378 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001379 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001380 &acpi_video_device_brightness_fops,
1381 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001383 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001386 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001387 &acpi_video_device_EDID_fops,
1388 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001390 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001392 acpi_device_dir(device) = device_dir;
1393
Patrick Mocheld550d982006-06-27 00:41:40 -04001394 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001395
1396 err_remove_brightness:
1397 remove_proc_entry("brightness", device_dir);
1398 err_remove_state:
1399 remove_proc_entry("state", device_dir);
1400 err_remove_info:
1401 remove_proc_entry("info", device_dir);
1402 err_remove_dir:
1403 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1404 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Len Brown4be44fc2005-08-05 00:44:28 -04001407static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001410 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001412 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001414 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001416 device_dir = acpi_device_dir(device);
1417 if (device_dir) {
1418 remove_proc_entry("info", device_dir);
1419 remove_proc_entry("state", device_dir);
1420 remove_proc_entry("brightness", device_dir);
1421 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001422 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 acpi_device_dir(device) = NULL;
1424 }
1425
Patrick Mocheld550d982006-06-27 00:41:40 -04001426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001430static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001432 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (!video)
1436 goto end;
1437
1438 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001439 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001441 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001443 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Len Brown4be44fc2005-08-05 00:44:28 -04001445 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Len Brown4be44fc2005-08-05 00:44:28 -04001449static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Len Brown4be44fc2005-08-05 00:44:28 -04001451 return single_open(file, acpi_video_bus_info_seq_show,
1452 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Len Brown4be44fc2005-08-05 00:44:28 -04001455static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001457 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (!video)
1461 goto end;
1462
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001463 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 seq_printf(seq, "<TODO>\n");
1465
Len Brown4be44fc2005-08-05 00:44:28 -04001466 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Len Brown4be44fc2005-08-05 00:44:28 -04001470static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1473}
1474
Len Brown4be44fc2005-08-05 00:44:28 -04001475static int acpi_video_bus_POST_info_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;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001478 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001479 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (!video)
1483 goto end;
1484
1485 status = acpi_video_bus_POST_options(video, &options);
1486 if (ACPI_SUCCESS(status)) {
1487 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001488 printk(KERN_WARNING PREFIX
1489 "The motherboard VGA device is not listed as a possible POST device.\n");
1490 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001491 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Frank Seidel4d939152009-02-04 17:03:07 +01001493 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001494 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (options & 2)
1496 seq_printf(seq, " <PCI video>");
1497 if (options & 4)
1498 seq_printf(seq, " <AGP video>");
1499 seq_putc(seq, '\n');
1500 } else
1501 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001502 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506static int
Len Brown4be44fc2005-08-05 00:44:28 -04001507acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Len Brown4be44fc2005-08-05 00:44:28 -04001509 return single_open(file, acpi_video_bus_POST_info_seq_show,
1510 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Len Brown4be44fc2005-08-05 00:44:28 -04001513static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001515 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001516 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001517 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (!video)
1521 goto end;
1522
Len Brown4be44fc2005-08-05 00:44:28 -04001523 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (!ACPI_SUCCESS(status)) {
1525 seq_printf(seq, "<not supported>\n");
1526 goto end;
1527 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001528 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Len Brown4be44fc2005-08-05 00:44:28 -04001530 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Len Brown4be44fc2005-08-05 00:44:28 -04001534static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001536 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Len Brown4be44fc2005-08-05 00:44:28 -04001539 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Patrick Mocheld550d982006-06-27 00:41:40 -04001541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Len Brown4be44fc2005-08-05 00:44:28 -04001544static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Len Brown4be44fc2005-08-05 00:44:28 -04001546 return single_open(file, acpi_video_bus_POST_seq_show,
1547 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Len Brown4be44fc2005-08-05 00:44:28 -04001550static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1553}
1554
1555static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001556acpi_video_bus_write_POST(struct file *file,
1557 const char __user * buffer,
1558 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Len Brown4be44fc2005-08-05 00:44:28 -04001560 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001561 struct seq_file *m = file->private_data;
1562 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001563 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001564 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001567 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 status = acpi_video_bus_POST_options(video, &options);
1571 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001572 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001575 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 str[count] = 0;
1578 opt = strtoul(str, NULL, 0);
1579 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001580 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Julius Volz98fb8fe2007-02-20 16:38:40 +01001582 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 options |= 1;
1584
1585 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001586 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001588 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 }
1591
Patrick Mocheld550d982006-06-27 00:41:40 -04001592 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001596acpi_video_bus_write_DOS(struct file *file,
1597 const char __user * buffer,
1598 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Len Brown4be44fc2005-08-05 00:44:28 -04001600 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001601 struct seq_file *m = file->private_data;
1602 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001603 char str[12] = { 0 };
1604 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Arjan van de Ven52a2b112009-10-01 15:48:40 -07001607 if (!video || count >= sizeof(str))
Patrick Mocheld550d982006-06-27 00:41:40 -04001608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001611 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 str[count] = 0;
1614 opt = strtoul(str, NULL, 0);
1615 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001616 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Len Brown4be44fc2005-08-05 00:44:28 -04001618 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001621 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Patrick Mocheld550d982006-06-27 00:41:40 -04001623 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Len Brown4be44fc2005-08-05 00:44:28 -04001626static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001628 struct acpi_video_bus *video = acpi_driver_data(device);
1629 struct proc_dir_entry *device_dir;
1630 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001632 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1633 if (!device_dir)
1634 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001637 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001638 &acpi_video_bus_info_fops,
1639 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001641 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001644 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001645 &acpi_video_bus_ROM_fops,
1646 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001648 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001651 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001652 &acpi_video_bus_POST_info_fops,
1653 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001655 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001658 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001659 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001660 &acpi_video_bus_POST_fops,
1661 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001663 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001666 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001667 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001668 &acpi_video_bus_DOS_fops,
1669 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001671 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001673 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001674 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001675
1676 err_remove_post:
1677 remove_proc_entry("POST", device_dir);
1678 err_remove_post_info:
1679 remove_proc_entry("POST_info", device_dir);
1680 err_remove_rom:
1681 remove_proc_entry("ROM", device_dir);
1682 err_remove_info:
1683 remove_proc_entry("info", device_dir);
1684 err_remove_dir:
1685 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1686 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Len Brown4be44fc2005-08-05 00:44:28 -04001689static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001691 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001693 if (device_dir) {
1694 remove_proc_entry("info", device_dir);
1695 remove_proc_entry("ROM", device_dir);
1696 remove_proc_entry("POST_info", device_dir);
1697 remove_proc_entry("POST", device_dir);
1698 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001699 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 acpi_device_dir(device) = NULL;
1701 }
1702
Patrick Mocheld550d982006-06-27 00:41:40 -04001703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/* --------------------------------------------------------------------------
1707 Driver Interface
1708 -------------------------------------------------------------------------- */
1709
1710/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001711static struct acpi_video_device_attrib*
1712acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1713{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001714 struct acpi_video_enumerated_device *ids;
1715 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001716
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001717 for (i = 0; i < video->attached_count; i++) {
1718 ids = &video->attached_array[i];
1719 if ((ids->value.int_val & 0xffff) == device_id)
1720 return &ids->value.attrib;
1721 }
1722
Rui Zhang82cae992007-01-03 23:40:53 -05001723 return NULL;
1724}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726static int
Len Brown4be44fc2005-08-05 00:44:28 -04001727acpi_video_bus_get_one_device(struct acpi_device *device,
1728 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001730 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001731 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001732 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001733 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Len Brown4be44fc2005-08-05 00:44:28 -04001738 status =
1739 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (ACPI_SUCCESS(status)) {
1741
Burman Yan36bcbec2006-12-19 12:56:11 -08001742 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1747 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001748 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 data->device_id = device_id;
1751 data->video = video;
1752 data->dev = device;
1753
Rui Zhang82cae992007-01-03 23:40:53 -05001754 attribute = acpi_video_get_device_attr(video, device_id);
1755
1756 if((attribute != NULL) && attribute->device_id_scheme) {
1757 switch (attribute->display_type) {
1758 case ACPI_VIDEO_DISPLAY_CRT:
1759 data->flags.crt = 1;
1760 break;
1761 case ACPI_VIDEO_DISPLAY_TV:
1762 data->flags.tvout = 1;
1763 break;
1764 case ACPI_VIDEO_DISPLAY_DVI:
1765 data->flags.dvi = 1;
1766 break;
1767 case ACPI_VIDEO_DISPLAY_LCD:
1768 data->flags.lcd = 1;
1769 break;
1770 default:
1771 data->flags.unknown = 1;
1772 break;
1773 }
1774 if(attribute->bios_can_detect)
1775 data->flags.bios = 1;
1776 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 acpi_video_device_bind(video, data);
1780 acpi_video_device_find_cap(data);
1781
Patrick Mochel90130262006-05-19 16:54:48 -04001782 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001783 ACPI_DEVICE_NOTIFY,
1784 acpi_video_device_notify,
1785 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001787 printk(KERN_ERR PREFIX
1788 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001789 if(data->brightness)
1790 kfree(data->brightness->levels);
1791 kfree(data->brightness);
1792 kfree(data);
1793 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001796 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001798 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 acpi_video_device_add_fs(device);
1801
Patrick Mocheld550d982006-06-27 00:41:40 -04001802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Patrick Mocheld550d982006-06-27 00:41:40 -04001805 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808/*
1809 * Arg:
1810 * video : video bus device
1811 *
1812 * Return:
1813 * none
1814 *
1815 * Enumerate the video device list of the video bus,
1816 * bind the ids with the corresponding video devices
1817 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Len Brown4be44fc2005-08-05 00:44:28 -04001820static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001822 struct acpi_video_device *dev;
1823
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001824 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001825
1826 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001827 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001828
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001829 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832/*
1833 * Arg:
1834 * video : video bus device
1835 * device : video output device under the video
1836 * bus
1837 *
1838 * Return:
1839 * none
1840 *
1841 * Bind the ids with the corresponding video devices
1842 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845static void
Len Brown4be44fc2005-08-05 00:44:28 -04001846acpi_video_device_bind(struct acpi_video_bus *video,
1847 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001849 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001852 for (i = 0; i < video->attached_count; i++) {
1853 ids = &video->attached_array[i];
1854 if (device->device_id == (ids->value.int_val & 0xffff)) {
1855 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1857 }
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861/*
1862 * Arg:
1863 * video : video bus device
1864 *
1865 * Return:
1866 * < 0 : error
1867 *
1868 * Call _DOD to enumerate all devices attached to display adapter
1869 *
Len Brown4be44fc2005-08-05 00:44:28 -04001870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1873{
Len Brown4be44fc2005-08-05 00:44:28 -04001874 int status;
1875 int count;
1876 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001877 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001878 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1879 union acpi_object *dod = NULL;
1880 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Patrick Mochel90130262006-05-19 16:54:48 -04001882 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001884 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001888 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001890 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 status = -EFAULT;
1892 goto out;
1893 }
1894
1895 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001896 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001898 active_list = kcalloc(1 + dod->package.count,
1899 sizeof(struct acpi_video_enumerated_device),
1900 GFP_KERNEL);
1901 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 status = -ENOMEM;
1903 goto out;
1904 }
1905
1906 count = 0;
1907 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001908 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001911 printk(KERN_ERR PREFIX
1912 "Invalid _DOD data in element %d\n", i);
1913 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001915
1916 active_list[count].value.int_val = obj->integer.value;
1917 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001918 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1919 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 count++;
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Jesper Juhl6044ec82005-11-07 01:01:32 -08001923 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001924
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001925 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001927
1928 out:
Len Brown02438d82006-06-30 03:19:10 -04001929 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001930 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Len Brown4be44fc2005-08-05 00:44:28 -04001933static int
1934acpi_video_get_next_level(struct acpi_video_device *device,
1935 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001937 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001938 max = max_below = 0;
1939 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001940 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001941 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001942 l = device->brightness->levels[i];
1943 if (abs(l - level_current) < abs(delta)) {
1944 delta = l - level_current;
1945 if (!delta)
1946 break;
1947 }
1948 }
1949 /* Ajust level_current to closest available level */
1950 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001951 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001952 l = device->brightness->levels[i];
1953 if (l < min)
1954 min = l;
1955 if (l > max)
1956 max = l;
1957 if (l < min_above && l > level_current)
1958 min_above = l;
1959 if (l > max_below && l < level_current)
1960 max_below = l;
1961 }
1962
1963 switch (event) {
1964 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1965 return (level_current < max) ? min_above : min;
1966 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1967 return (level_current < max) ? min_above : max;
1968 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1969 return (level_current > min) ? max_below : min;
1970 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1971 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1972 return 0;
1973 default:
1974 return level_current;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Zhang Ruic8890f92009-03-18 16:27:08 +08001978static int
Len Brown4be44fc2005-08-05 00:44:28 -04001979acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001981 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001982 int result = -EINVAL;
1983
Julia Jomantaite469778c2008-06-23 22:50:42 +01001984 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001985 goto out;
1986
1987 result = acpi_video_device_lcd_get_level_current(device,
1988 &level_current);
1989 if (result)
1990 goto out;
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001993
Zhang Rui24450c72009-03-18 16:27:10 +08001994 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001995
Matthew Garrett36342742009-07-14 17:06:03 +01001996 if (!result)
1997 backlight_force_update(device->backlight,
1998 BACKLIGHT_UPDATE_HOTKEY);
1999
Zhang Ruic8890f92009-03-18 16:27:08 +08002000out:
2001 if (result)
2002 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2003
2004 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
2007static int
Len Brown4be44fc2005-08-05 00:44:28 -04002008acpi_video_bus_get_devices(struct acpi_video_bus *video,
2009 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Len Brown4be44fc2005-08-05 00:44:28 -04002011 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002012 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 acpi_video_device_enumerate(video);
2015
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002016 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 status = acpi_video_bus_get_one_device(dev, video);
2019 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002020 printk(KERN_WARNING PREFIX
2021 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 continue;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Patrick Mocheld550d982006-06-27 00:41:40 -04002025 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Len Brown4be44fc2005-08-05 00:44:28 -04002028static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Karol Kozimor031ec772005-07-30 04:18:00 -04002030 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 struct acpi_video_bus *video;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002035 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 video = device->video;
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 acpi_video_device_remove_fs(device->dev);
2040
Patrick Mochel90130262006-05-19 16:54:48 -04002041 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002042 ACPI_DEVICE_NOTIFY,
2043 acpi_video_device_notify);
Keith Packarde29b3ee2009-08-06 15:57:54 -07002044 if (device->backlight) {
2045 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2046 backlight_device_unregister(device->backlight);
2047 device->backlight = NULL;
2048 }
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002049 if (device->cooling_dev) {
Zhang Rui702ed512008-01-17 15:51:22 +08002050 sysfs_remove_link(&device->dev->dev.kobj,
2051 "thermal_cooling");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002052 sysfs_remove_link(&device->cooling_dev->device.kobj,
Zhang Rui702ed512008-01-17 15:51:22 +08002053 "device");
Dmitry Torokhov4a703a82009-08-29 23:03:16 -04002054 thermal_cooling_device_unregister(device->cooling_dev);
2055 device->cooling_dev = NULL;
Zhang Rui702ed512008-01-17 15:51:22 +08002056 }
Luming Yu23b0f012007-05-09 21:07:05 +08002057 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002058
Patrick Mocheld550d982006-06-27 00:41:40 -04002059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Len Brown4be44fc2005-08-05 00:44:28 -04002062static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Len Brown4be44fc2005-08-05 00:44:28 -04002064 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002065 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002067 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002069 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002071 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002072 if (ACPI_FAILURE(status))
2073 printk(KERN_WARNING PREFIX
2074 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002076 if (dev->brightness) {
2077 kfree(dev->brightness->levels);
2078 kfree(dev->brightness);
2079 }
2080 list_del(&dev->entry);
2081 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002084 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002085
Patrick Mocheld550d982006-06-27 00:41:40 -04002086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
2089/* acpi_video interface */
2090
Len Brown4be44fc2005-08-05 00:44:28 -04002091static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Zhang Ruia21101c2007-09-14 11:46:22 +08002093 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Len Brown4be44fc2005-08-05 00:44:28 -04002096static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 return acpi_video_bus_DOS(video, 0, 1);
2099}
2100
Bjorn Helgaas70155582009-04-07 15:37:11 +00002101static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002103 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002104 struct input_dev *input;
2105 int keycode;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Luming Yue9dab192007-08-20 18:23:53 +08002110 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002113 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002115 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002116 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 break;
2118
Julius Volz98fb8fe2007-02-20 16:38:40 +01002119 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * connector. */
2121 acpi_video_device_enumerate(video);
2122 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002123 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002124 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 break;
2126
Len Brown4be44fc2005-08-05 00:44:28 -04002127 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002128 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002129 keycode = KEY_SWITCHVIDEOMODE;
2130 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002131 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002132 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002133 keycode = KEY_VIDEO_NEXT;
2134 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002135 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002136 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002137 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139
2140 default:
Luming Yue9dab192007-08-20 18:23:53 +08002141 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002143 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
2145 }
2146
Zhang Rui7761f632008-01-25 14:48:12 +08002147 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002148 input_report_key(input, keycode, 1);
2149 input_sync(input);
2150 input_report_key(input, keycode, 0);
2151 input_sync(input);
2152
Patrick Mocheld550d982006-06-27 00:41:40 -04002153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Len Brown4be44fc2005-08-05 00:44:28 -04002156static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002158 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002159 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002160 struct acpi_video_bus *bus;
2161 struct input_dev *input;
2162 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002167 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002168 bus = video_device->video;
2169 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002172 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002173 if (brightness_switch_enabled)
2174 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002175 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002176 keycode = KEY_BRIGHTNESS_CYCLE;
2177 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002178 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002179 if (brightness_switch_enabled)
2180 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002181 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002182 keycode = KEY_BRIGHTNESSUP;
2183 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002184 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002185 if (brightness_switch_enabled)
2186 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002187 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002188 keycode = KEY_BRIGHTNESSDOWN;
2189 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002190 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002191 if (brightness_switch_enabled)
2192 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002193 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002194 keycode = KEY_BRIGHTNESS_ZERO;
2195 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002196 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002197 if (brightness_switch_enabled)
2198 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002199 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002200 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 break;
2202 default:
Luming Yue9dab192007-08-20 18:23:53 +08002203 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002205 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207 }
Luming Yue9dab192007-08-20 18:23:53 +08002208
Zhang Rui7761f632008-01-25 14:48:12 +08002209 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002210 input_report_key(input, keycode, 1);
2211 input_sync(input);
2212 input_report_key(input, keycode, 0);
2213 input_sync(input);
2214
Patrick Mocheld550d982006-06-27 00:41:40 -04002215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Zhang Ruie6d9da12007-08-25 02:23:31 -04002218static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002219static int acpi_video_resume(struct acpi_device *device)
2220{
2221 struct acpi_video_bus *video;
2222 struct acpi_video_device *video_device;
2223 int i;
2224
2225 if (!device || !acpi_driver_data(device))
2226 return -EINVAL;
2227
2228 video = acpi_driver_data(device);
2229
2230 for (i = 0; i < video->attached_count; i++) {
2231 video_device = video->attached_array[i].bind_info;
2232 if (video_device && video_device->backlight)
2233 acpi_video_set_brightness(video_device->backlight);
2234 }
2235 return AE_OK;
2236}
2237
Len Brown4be44fc2005-08-05 00:44:28 -04002238static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002240 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002241 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002242 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Burman Yan36bcbec2006-12-19 12:56:11 -08002244 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Zhang Ruie6d9da12007-08-25 02:23:31 -04002248 /* a hack to fix the duplicate name "VID" problem on T61 */
2249 if (!strcmp(device->pnp.bus_id, "VID")) {
2250 if (instance)
2251 device->pnp.bus_id[3] = '0' + instance;
2252 instance ++;
2253 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002254 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2255 if (!strcmp(device->pnp.bus_id, "VGA")) {
2256 if (instance)
2257 device->pnp.bus_id[3] = '0' + instance;
2258 instance++;
2259 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002260
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002261 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2263 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002264 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002267 error = acpi_video_bus_check(video);
2268 if (error)
2269 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002271 error = acpi_video_bus_add_fs(device);
2272 if (error)
2273 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002275 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 INIT_LIST_HEAD(&video->video_device_list);
2277
2278 acpi_video_bus_get_devices(video, device);
2279 acpi_video_bus_start_devices(video);
2280
Luming Yue9dab192007-08-20 18:23:53 +08002281 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002282 if (!input) {
2283 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002284 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002285 }
Luming Yue9dab192007-08-20 18:23:53 +08002286
2287 snprintf(video->phys, sizeof(video->phys),
2288 "%s/video/input0", acpi_device_hid(video->device));
2289
2290 input->name = acpi_device_name(video->device);
2291 input->phys = video->phys;
2292 input->id.bustype = BUS_HOST;
2293 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002294 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002295 input->evbit[0] = BIT(EV_KEY);
2296 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2297 set_bit(KEY_VIDEO_NEXT, input->keybit);
2298 set_bit(KEY_VIDEO_PREV, input->keybit);
2299 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2300 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2301 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2302 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2303 set_bit(KEY_DISPLAY_OFF, input->keybit);
2304 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002305
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002306 error = input_register_device(input);
2307 if (error)
2308 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002311 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2312 video->flags.multihead ? "yes" : "no",
2313 video->flags.rom ? "yes" : "no",
2314 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002318 err_free_input_dev:
2319 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002320 err_stop_video:
2321 acpi_video_bus_stop_devices(video);
2322 acpi_video_bus_put_devices(video);
2323 kfree(video->attached_array);
2324 acpi_video_bus_remove_fs(device);
2325 err_free_video:
2326 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002327 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002328
2329 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
Len Brown4be44fc2005-08-05 00:44:28 -04002332static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Len Brown4be44fc2005-08-05 00:44:28 -04002334 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002338 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002340 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 acpi_video_bus_put_devices(video);
2344 acpi_video_bus_remove_fs(device);
2345
Luming Yue9dab192007-08-20 18:23:53 +08002346 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002347 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 kfree(video);
2349
Patrick Mocheld550d982006-06-27 00:41:40 -04002350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Matthew Garrett74a365b2009-03-19 21:35:39 +00002353static int __init intel_opregion_present(void)
2354{
2355#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2356 struct pci_dev *dev = NULL;
2357 u32 address;
2358
2359 for_each_pci_dev(dev) {
2360 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2361 continue;
2362 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2363 continue;
2364 pci_read_config_dword(dev, 0xfc, &address);
2365 if (!address)
2366 continue;
2367 return 1;
2368 }
2369#endif
2370 return 0;
2371}
2372
2373int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Len Brown4be44fc2005-08-05 00:44:28 -04002375 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002376 if (register_count) {
2377 /*
2378 * if the function of acpi_video_register is already called,
2379 * don't register the acpi_vide_bus again and return no error.
2380 */
2381 return 0;
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2385 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002386 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 result = acpi_bus_register_driver(&acpi_video_bus);
2389 if (result < 0) {
2390 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002391 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
Zhao Yakui86e437f2009-06-16 11:23:13 +08002394 /*
2395 * When the acpi_video_bus is loaded successfully, increase
2396 * the counter reference.
2397 */
2398 register_count = 1;
2399
Patrick Mocheld550d982006-06-27 00:41:40 -04002400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002402EXPORT_SYMBOL(acpi_video_register);
2403
Zhao Yakui86e437f2009-06-16 11:23:13 +08002404void acpi_video_unregister(void)
2405{
2406 if (!register_count) {
2407 /*
2408 * If the acpi video bus is already unloaded, don't
2409 * unload it again and return directly.
2410 */
2411 return;
2412 }
2413 acpi_bus_unregister_driver(&acpi_video_bus);
2414
2415 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2416
2417 register_count = 0;
2418
2419 return;
2420}
2421EXPORT_SYMBOL(acpi_video_unregister);
2422
Matthew Garrett74a365b2009-03-19 21:35:39 +00002423/*
2424 * This is kind of nasty. Hardware using Intel chipsets may require
2425 * the video opregion code to be run first in order to initialise
2426 * state before any ACPI video calls are made. To handle this we defer
2427 * registration of the video class until the opregion code has run.
2428 */
2429
2430static int __init acpi_video_init(void)
2431{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002432 dmi_check_system(video_dmi_table);
2433
Matthew Garrett74a365b2009-03-19 21:35:39 +00002434 if (intel_opregion_present())
2435 return 0;
2436
2437 return acpi_video_register();
2438}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Zhao Yakui86e437f2009-06-16 11:23:13 +08002440static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002442 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Patrick Mocheld550d982006-06-27 00:41:40 -04002444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447module_init(acpi_video_init);
2448module_exit(acpi_video_exit);