blob: a0fa3946b50784ad124da40b586ce2a142ad6ea9 [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>
43
44#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;
Zhang Rui702ed512008-01-17 15:51:22 +0800203 struct thermal_cooling_device *cdev;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static 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 */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000392static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
393 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800394{
395 struct acpi_device *device = cdev->devdata;
396 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
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000402static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
403 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800404{
405 struct acpi_device *device = cdev->devdata;
406 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
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000422video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800423{
424 struct acpi_device *device = cdev->devdata;
425 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;
608
Zhang Ruic60d6382009-03-18 16:27:18 +0800609 if (device->cap._BQC || device->cap._BCQ) {
610 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
611
612 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800613 NULL, level);
614 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800615 if (device->brightness->flags._BQC_use_index) {
616 if (device->brightness->flags._BCL_reversed)
617 *level = device->brightness->count
618 - 3 - (*level);
619 *level = device->brightness->levels[*level + 2];
620
621 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400622 *level += bqc_offset_aml_bug_workaround;
Zhang Ruic8890f92009-03-18 16:27:08 +0800623 device->brightness->curr = *level;
624 return 0;
625 } else {
626 /* Fixme:
627 * should we return an error or ignore this failure?
628 * dev->brightness->curr is a cached value which stores
629 * the correct current backlight level in most cases.
630 * ACPI video backlight still works w/ buggy _BQC.
631 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
632 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800633 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
634 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800635 }
636 }
637
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400638 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static int
Len Brown4be44fc2005-08-05 00:44:28 -0400643acpi_video_device_EDID(struct acpi_video_device *device,
644 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Len Brown4be44fc2005-08-05 00:44:28 -0400646 int status;
647 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
648 union acpi_object *obj;
649 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
650 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 *edid = NULL;
654
655 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (length == 128)
658 arg0.integer.value = 1;
659 else if (length == 256)
660 arg0.integer.value = 2;
661 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400662 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Patrick Mochel90130262006-05-19 16:54:48 -0400664 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200668 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (obj && obj->type == ACPI_TYPE_BUFFER)
671 *edid = obj;
672 else {
Len Brown64684632006-06-26 23:41:38 -0400673 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 status = -EFAULT;
675 kfree(obj);
676 }
677
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/* bus */
682
683static int
Len Brown4be44fc2005-08-05 00:44:28 -0400684acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Len Brown4be44fc2005-08-05 00:44:28 -0400686 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400687 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400688 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
689 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 arg0.integer.value = option;
693
Patrick Mochel90130262006-05-19 16:54:48 -0400694 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400696 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Patrick Mocheld550d982006-06-27 00:41:40 -0400698 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400702acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int status;
705
Patrick Mochel90130262006-05-19 16:54:48 -0400706 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Patrick Mocheld550d982006-06-27 00:41:40 -0400708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static int
Len Brown4be44fc2005-08-05 00:44:28 -0400712acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400713 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Len Brown4be44fc2005-08-05 00:44:28 -0400715 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Patrick Mochel90130262006-05-19 16:54:48 -0400717 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *options &= 3;
719
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
724 * Arg:
725 * video : video bus device pointer
726 * bios_flag :
727 * 0. The system BIOS should NOT automatically switch(toggle)
728 * the active display output.
729 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100730 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * 2. The _DGS value should be locked.
732 * 3. The system BIOS should not automatically switch (toggle) the
733 * active display output, but instead generate the display switch
734 * event notify code.
735 * lcd_flag :
736 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100737 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100739 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * Return Value:
741 * -1 wrong arg.
742 */
743
744static int
Len Brown4be44fc2005-08-05 00:44:28 -0400745acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Len Brown4be44fc2005-08-05 00:44:28 -0400747 acpi_integer status = 0;
748 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
749 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Len Brown4be44fc2005-08-05 00:44:28 -0400752 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 status = -1;
754 goto Failed;
755 }
756 arg0.integer.value = (lcd_flag << 2) | bios_flag;
757 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400758 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Len Brown4be44fc2005-08-05 00:44:28 -0400760 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400761 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500765 * Simple comparison function used to sort backlight levels.
766 */
767
768static int
769acpi_video_cmp_level(const void *a, const void *b)
770{
771 return *(int *)a - *(int *)b;
772}
773
774/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * Arg:
776 * device : video output device (LCD, CRT, ..)
777 *
778 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100779 * Maximum brightness level
780 *
781 * Allocate and initialize device->brightness.
782 */
783
784static int
785acpi_video_init_brightness(struct acpi_video_device *device)
786{
787 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800788 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800789 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100790 union acpi_object *o;
791 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800792 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100793
794 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
795 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
796 "LCD brightness level\n"));
797 goto out;
798 }
799
800 if (obj->package.count < 2)
801 goto out;
802
803 br = kzalloc(sizeof(*br), GFP_KERNEL);
804 if (!br) {
805 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800806 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100807 goto out;
808 }
809
Zhang Ruid32f6942009-03-18 16:27:12 +0800810 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100811 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800812 if (!br->levels) {
813 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100814 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800815 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100816
817 for (i = 0; i < obj->package.count; i++) {
818 o = (union acpi_object *)&obj->package.elements[i];
819 if (o->type != ACPI_TYPE_INTEGER) {
820 printk(KERN_ERR PREFIX "Invalid data\n");
821 continue;
822 }
823 br->levels[count] = (u32) o->integer.value;
824
825 if (br->levels[count] > max_level)
826 max_level = br->levels[count];
827 count++;
828 }
829
Zhang Ruid32f6942009-03-18 16:27:12 +0800830 /*
831 * some buggy BIOS don't export the levels
832 * when machine is on AC/Battery in _BCL package.
833 * In this case, the first two elements in _BCL packages
834 * are also supported brightness levels that OS should take care of.
835 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800836 for (i = 2; i < count; i++) {
837 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800838 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800839 if (br->levels[i] == br->levels[1])
840 level_ac_battery++;
841 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500842
Zhang Ruid32f6942009-03-18 16:27:12 +0800843 if (level_ac_battery < 2) {
844 level_ac_battery = 2 - level_ac_battery;
845 br->flags._BCL_no_ac_battery_levels = 1;
846 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
847 br->levels[i] = br->levels[i - level_ac_battery];
848 count += level_ac_battery;
849 } else if (level_ac_battery > 2)
850 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
851
Zhang Ruid80fb992009-03-18 16:27:14 +0800852 /* Check if the _BCL package is in a reversed order */
853 if (max_level == br->levels[2]) {
854 br->flags._BCL_reversed = 1;
855 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
856 acpi_video_cmp_level, NULL);
857 } else if (max_level != br->levels[count - 1])
858 ACPI_ERROR((AE_INFO,
859 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100860
861 br->count = count;
862 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800863
864 /* Check the input/output of _BQC/_BCL/_BCM */
865 if ((max_level < 100) && (max_level <= (count - 2)))
866 br->flags._BCL_use_index = 1;
867
868 /*
869 * _BCM is always consistent with _BCL,
870 * at least for all the laptops we have ever seen.
871 */
872 br->flags._BCM_use_index = br->flags._BCL_use_index;
873
874 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Ruie047cca2009-04-09 14:24:35 +0800875 br->curr = level_old = max_level;
876
877 if (!device->cap._BQC)
878 goto set_level;
879
Zhang Rui1a7c6182009-03-18 16:27:16 +0800880 result = acpi_video_device_lcd_get_level_current(device, &level_old);
881 if (result)
882 goto out_free_levels;
883
Zhang Ruie047cca2009-04-09 14:24:35 +0800884 /*
885 * Set the level to maximum and check if _BQC uses indexed value
886 */
887 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800888 if (result)
889 goto out_free_levels;
890
891 result = acpi_video_device_lcd_get_level_current(device, &level);
892 if (result)
893 goto out_free_levels;
894
Zhang Ruie047cca2009-04-09 14:24:35 +0800895 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800896
Zhang Ruie047cca2009-04-09 14:24:35 +0800897 if (!br->flags._BQC_use_index)
898 goto set_level;
899
900 if (br->flags._BCL_reversed)
901 level_old = (br->count - 1) - level_old;
902 level_old = br->levels[level_old];
903
904set_level:
905 result = acpi_video_device_lcd_set_level(device, level_old);
906 if (result)
907 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800908
Zhang Ruid32f6942009-03-18 16:27:12 +0800909 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
910 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100911 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800912 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100913
914out_free_levels:
915 kfree(br->levels);
916out_free:
917 kfree(br);
918out:
919 device->brightness = NULL;
920 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800921 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100922}
923
924/*
925 * Arg:
926 * device : video output device (LCD, CRT, ..)
927 *
928 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * None
930 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100931 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * device.
933 */
934
Len Brown4be44fc2005-08-05 00:44:28 -0400935static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
William Lee Irwin III98934de2007-12-12 03:56:55 -0800940 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Patrick Mochel90130262006-05-19 16:54:48 -0400942 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 device->cap._ADR = 1;
944 }
Patrick Mochel90130262006-05-19 16:54:48 -0400945 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400946 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Patrick Mochel90130262006-05-19 16:54:48 -0400948 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400949 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800951 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
952 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800953 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
954 &h_dummy1))) {
955 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
956 device->cap._BCQ = 1;
957 }
958
Patrick Mochel90130262006-05-19 16:54:48 -0400959 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400960 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Patrick Mochel90130262006-05-19 16:54:48 -0400962 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 device->cap._DCS = 1;
964 }
Patrick Mochel90130262006-05-19 16:54:48 -0400965 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 device->cap._DGS = 1;
967 }
Patrick Mochel90130262006-05-19 16:54:48 -0400968 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 device->cap._DSS = 1;
970 }
971
Zhang Rui1a7c6182009-03-18 16:27:16 +0800972 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800973 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800974 static int count = 0;
975 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800976
977 result = acpi_video_init_brightness(device);
978 if (result)
979 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800980 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
981 if (!name)
982 return;
983
Yu Luming2f3d0002006-11-11 02:40:34 +0800984 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800985 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000986 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000987 device->backlight->props.max_brightness = device->brightness->count-3;
Yu Luming2f3d0002006-11-11 02:40:34 +0800988 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800989
Zhang Rui056c3082009-06-22 11:31:14 +0800990 result = sysfs_create_link(&device->backlight->dev.kobj,
991 &device->dev->dev.kobj, "device");
992 if (result)
993 printk(KERN_ERR PREFIX "Create sysfs link\n");
994
Zhang Rui702ed512008-01-17 15:51:22 +0800995 device->cdev = thermal_cooling_device_register("LCD",
996 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500997 if (IS_ERR(device->cdev))
998 return;
999
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +02001000 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1001 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +08001002 result = sysfs_create_link(&device->dev->dev.kobj,
1003 &device->cdev->device.kobj,
1004 "thermal_cooling");
1005 if (result)
1006 printk(KERN_ERR PREFIX "Create sysfs link\n");
1007 result = sysfs_create_link(&device->cdev->device.kobj,
1008 &device->dev->dev.kobj, "device");
1009 if (result)
1010 printk(KERN_ERR PREFIX "Create sysfs link\n");
1011
Yu Luming2f3d0002006-11-11 02:40:34 +08001012 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001013
1014 if (acpi_video_display_switch_support()) {
1015
1016 if (device->cap._DCS && device->cap._DSS) {
1017 static int count;
1018 char *name;
1019 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1020 if (!name)
1021 return;
1022 sprintf(name, "acpi_video%d", count++);
1023 device->output_dev = video_output_register(name,
1024 NULL, device, &acpi_output_properties);
1025 kfree(name);
1026 }
Luming Yu23b0f012007-05-09 21:07:05 +08001027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
1031 * Arg:
1032 * device : video output device (VGA)
1033 *
1034 * Return Value:
1035 * None
1036 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001037 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
1039
Len Brown4be44fc2005-08-05 00:44:28 -04001040static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Len Brown4be44fc2005-08-05 00:44:28 -04001042 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
William Lee Irwin III98934de2007-12-12 03:56:55 -08001044 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -04001045 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 video->cap._DOS = 1;
1047 }
Patrick Mochel90130262006-05-19 16:54:48 -04001048 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 video->cap._DOD = 1;
1050 }
Patrick Mochel90130262006-05-19 16:54:48 -04001051 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 video->cap._ROM = 1;
1053 }
Patrick Mochel90130262006-05-19 16:54:48 -04001054 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 video->cap._GPD = 1;
1056 }
Patrick Mochel90130262006-05-19 16:54:48 -04001057 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 video->cap._SPD = 1;
1059 }
Patrick Mochel90130262006-05-19 16:54:48 -04001060 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 video->cap._VPO = 1;
1062 }
1063}
1064
1065/*
1066 * Check whether the video bus device has required AML method to
1067 * support the desired features
1068 */
1069
Len Brown4be44fc2005-08-05 00:44:28 -04001070static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Len Brown4be44fc2005-08-05 00:44:28 -04001072 acpi_status status = -ENOENT;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001073 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001076 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001078 dev = acpi_get_pci_dev(video->device->handle);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001079 if (!dev)
1080 return -ENODEV;
Alexander Chiang1e4cffe2009-06-10 19:56:00 +00001081 pci_dev_put(dev);
Thomas Renninger22c13f92008-08-01 17:37:54 +02001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Since there is no HID, CID and so on for VGA driver, we have
1084 * to check well known required nodes.
1085 */
1086
Julius Volz98fb8fe2007-02-20 16:38:40 +01001087 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -04001088 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 video->flags.multihead = 1;
1090 status = 0;
1091 }
1092
Julius Volz98fb8fe2007-02-20 16:38:40 +01001093 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001094 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 video->flags.rom = 1;
1096 status = 0;
1097 }
1098
Julius Volz98fb8fe2007-02-20 16:38:40 +01001099 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001100 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 video->flags.post = 1;
1102 status = 0;
1103 }
1104
Patrick Mocheld550d982006-06-27 00:41:40 -04001105 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/* --------------------------------------------------------------------------
1109 FS Interface (/proc)
1110 -------------------------------------------------------------------------- */
1111
Len Brown4be44fc2005-08-05 00:44:28 -04001112static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114/* video devices */
1115
Len Brown4be44fc2005-08-05 00:44:28 -04001116static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001118 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!dev)
1122 goto end;
1123
1124 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1125 seq_printf(seq, "type: ");
1126 if (dev->flags.crt)
1127 seq_printf(seq, "CRT\n");
1128 else if (dev->flags.lcd)
1129 seq_printf(seq, "LCD\n");
1130 else if (dev->flags.tvout)
1131 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001132 else if (dev->flags.dvi)
1133 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 else
1135 seq_printf(seq, "UNKNOWN\n");
1136
Len Brown4be44fc2005-08-05 00:44:28 -04001137 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Len Brown4be44fc2005-08-05 00:44:28 -04001139 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143static int
Len Brown4be44fc2005-08-05 00:44:28 -04001144acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 return single_open(file, acpi_video_device_info_seq_show,
1147 PDE(inode)->data);
1148}
1149
Len Brown4be44fc2005-08-05 00:44:28 -04001150static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Len Brown4be44fc2005-08-05 00:44:28 -04001152 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001153 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001154 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (!dev)
1158 goto end;
1159
1160 status = acpi_video_device_get_state(dev, &state);
1161 seq_printf(seq, "state: ");
1162 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001163 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 else
1165 seq_printf(seq, "<not supported>\n");
1166
1167 status = acpi_video_device_query(dev, &state);
1168 seq_printf(seq, "query: ");
1169 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001170 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 else
1172 seq_printf(seq, "<not supported>\n");
1173
Len Brown4be44fc2005-08-05 00:44:28 -04001174 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static int
Len Brown4be44fc2005-08-05 00:44:28 -04001179acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 return single_open(file, acpi_video_device_state_seq_show,
1182 PDE(inode)->data);
1183}
1184
1185static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001186acpi_video_device_write_state(struct file *file,
1187 const char __user * buffer,
1188 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Len Brown4be44fc2005-08-05 00:44:28 -04001190 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001191 struct seq_file *m = file->private_data;
1192 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001193 char str[12] = { 0 };
1194 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001201 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 str[count] = 0;
1204 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001205 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 status = acpi_video_device_set_state(dev, state);
1208
1209 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001210 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Patrick Mocheld550d982006-06-27 00:41:40 -04001212 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static int
Len Brown4be44fc2005-08-05 00:44:28 -04001216acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001218 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001219 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if (!dev || !dev->brightness) {
1223 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
1227 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001228 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 seq_printf(seq, " %d", dev->brightness->levels[i]);
1230 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1231
Patrick Mocheld550d982006-06-27 00:41:40 -04001232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235static int
Len Brown4be44fc2005-08-05 00:44:28 -04001236acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 return single_open(file, acpi_video_device_brightness_seq_show,
1239 PDE(inode)->data);
1240}
1241
1242static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001243acpi_video_device_write_brightness(struct file *file,
1244 const char __user * buffer,
1245 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001247 struct seq_file *m = file->private_data;
1248 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001249 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001250 unsigned int level = 0;
1251 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Thomas Renninger59d399d2005-11-08 05:27:00 -05001254 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 str[count] = 0;
1261 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001264 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Julius Volz98fb8fe2007-02-20 16:38:40 +01001266 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001267 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001269 if (!acpi_video_device_lcd_set_level(dev, level))
1270 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 }
1273
Zhang Rui24450c72009-03-18 16:27:10 +08001274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Len Brown4be44fc2005-08-05 00:44:28 -04001277static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001279 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001280 int status;
1281 int i;
1282 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!dev)
1286 goto out;
1287
Len Brown4be44fc2005-08-05 00:44:28 -04001288 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001290 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
1293 if (ACPI_FAILURE(status)) {
1294 goto out;
1295 }
1296
1297 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1298 for (i = 0; i < edid->buffer.length; i++)
1299 seq_putc(seq, edid->buffer.pointer[i]);
1300 }
1301
Len Brown4be44fc2005-08-05 00:44:28 -04001302 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!edid)
1304 seq_printf(seq, "<not supported>\n");
1305 else
1306 kfree(edid);
1307
Patrick Mocheld550d982006-06-27 00:41:40 -04001308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311static int
Len Brown4be44fc2005-08-05 00:44:28 -04001312acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 return single_open(file, acpi_video_device_EDID_seq_show,
1315 PDE(inode)->data);
1316}
1317
Len Brown4be44fc2005-08-05 00:44:28 -04001318static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001320 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct acpi_video_device *vid_dev;
1322
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001323 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001325 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001327 device_dir = proc_mkdir(acpi_device_bid(device),
1328 vid_dev->video->dir);
1329 if (!device_dir)
1330 return -ENOMEM;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001333 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001334 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001336 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001339 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001340 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001341 &acpi_video_device_state_fops,
1342 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001344 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001347 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001348 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001349 &acpi_video_device_brightness_fops,
1350 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001352 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001355 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001356 &acpi_video_device_EDID_fops,
1357 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001359 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001361 acpi_device_dir(device) = device_dir;
1362
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001364
1365 err_remove_brightness:
1366 remove_proc_entry("brightness", device_dir);
1367 err_remove_state:
1368 remove_proc_entry("state", device_dir);
1369 err_remove_info:
1370 remove_proc_entry("info", device_dir);
1371 err_remove_dir:
1372 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1373 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Len Brown4be44fc2005-08-05 00:44:28 -04001376static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001379 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001381 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001385 device_dir = acpi_device_dir(device);
1386 if (device_dir) {
1387 remove_proc_entry("info", device_dir);
1388 remove_proc_entry("state", device_dir);
1389 remove_proc_entry("brightness", device_dir);
1390 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001391 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 acpi_device_dir(device) = NULL;
1393 }
1394
Patrick Mocheld550d982006-06-27 00:41:40 -04001395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001399static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001401 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (!video)
1405 goto end;
1406
1407 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001408 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001410 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001412 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Len Brown4be44fc2005-08-05 00:44:28 -04001414 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Len Brown4be44fc2005-08-05 00:44:28 -04001418static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Len Brown4be44fc2005-08-05 00:44:28 -04001420 return single_open(file, acpi_video_bus_info_seq_show,
1421 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Len Brown4be44fc2005-08-05 00:44:28 -04001424static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001426 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (!video)
1430 goto end;
1431
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001432 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 seq_printf(seq, "<TODO>\n");
1434
Len Brown4be44fc2005-08-05 00:44:28 -04001435 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Len Brown4be44fc2005-08-05 00:44:28 -04001439static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1442}
1443
Len Brown4be44fc2005-08-05 00:44:28 -04001444static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001446 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001447 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001448 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (!video)
1452 goto end;
1453
1454 status = acpi_video_bus_POST_options(video, &options);
1455 if (ACPI_SUCCESS(status)) {
1456 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001457 printk(KERN_WARNING PREFIX
1458 "The motherboard VGA device is not listed as a possible POST device.\n");
1459 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001460 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Frank Seidel4d939152009-02-04 17:03:07 +01001462 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001463 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (options & 2)
1465 seq_printf(seq, " <PCI video>");
1466 if (options & 4)
1467 seq_printf(seq, " <AGP video>");
1468 seq_putc(seq, '\n');
1469 } else
1470 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001471 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475static int
Len Brown4be44fc2005-08-05 00:44:28 -04001476acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Len Brown4be44fc2005-08-05 00:44:28 -04001478 return single_open(file, acpi_video_bus_POST_info_seq_show,
1479 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Len Brown4be44fc2005-08-05 00:44:28 -04001482static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001484 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001485 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001486 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!video)
1490 goto end;
1491
Len Brown4be44fc2005-08-05 00:44:28 -04001492 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!ACPI_SUCCESS(status)) {
1494 seq_printf(seq, "<not supported>\n");
1495 goto end;
1496 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001497 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Len Brown4be44fc2005-08-05 00:44:28 -04001499 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Len Brown4be44fc2005-08-05 00:44:28 -04001503static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001505 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Len Brown4be44fc2005-08-05 00:44:28 -04001508 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Patrick Mocheld550d982006-06-27 00:41:40 -04001510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Len Brown4be44fc2005-08-05 00:44:28 -04001513static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Len Brown4be44fc2005-08-05 00:44:28 -04001515 return single_open(file, acpi_video_bus_POST_seq_show,
1516 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Len Brown4be44fc2005-08-05 00:44:28 -04001519static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1522}
1523
1524static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001525acpi_video_bus_write_POST(struct file *file,
1526 const char __user * buffer,
1527 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Len Brown4be44fc2005-08-05 00:44:28 -04001529 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001530 struct seq_file *m = file->private_data;
1531 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001532 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001533 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001537 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 status = acpi_video_bus_POST_options(video, &options);
1540 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001544 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 str[count] = 0;
1547 opt = strtoul(str, NULL, 0);
1548 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001549 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Julius Volz98fb8fe2007-02-20 16:38:40 +01001551 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 options |= 1;
1553
1554 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001555 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001557 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 }
1560
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001565acpi_video_bus_write_DOS(struct file *file,
1566 const char __user * buffer,
1567 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Len Brown4be44fc2005-08-05 00:44:28 -04001569 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001570 struct seq_file *m = file->private_data;
1571 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001572 char str[12] = { 0 };
1573 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001580 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 str[count] = 0;
1583 opt = strtoul(str, NULL, 0);
1584 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001585 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Len Brown4be44fc2005-08-05 00:44:28 -04001587 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001590 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Patrick Mocheld550d982006-06-27 00:41:40 -04001592 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Len Brown4be44fc2005-08-05 00:44:28 -04001595static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001597 struct acpi_video_bus *video = acpi_driver_data(device);
1598 struct proc_dir_entry *device_dir;
1599 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001601 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1602 if (!device_dir)
1603 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001606 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001607 &acpi_video_bus_info_fops,
1608 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001610 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001613 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001614 &acpi_video_bus_ROM_fops,
1615 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001617 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001620 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001621 &acpi_video_bus_POST_info_fops,
1622 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001624 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001627 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001628 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001629 &acpi_video_bus_POST_fops,
1630 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001632 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001635 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001636 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001637 &acpi_video_bus_DOS_fops,
1638 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001640 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001642 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001643 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001644
1645 err_remove_post:
1646 remove_proc_entry("POST", device_dir);
1647 err_remove_post_info:
1648 remove_proc_entry("POST_info", device_dir);
1649 err_remove_rom:
1650 remove_proc_entry("ROM", device_dir);
1651 err_remove_info:
1652 remove_proc_entry("info", device_dir);
1653 err_remove_dir:
1654 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1655 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Len Brown4be44fc2005-08-05 00:44:28 -04001658static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001660 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001662 if (device_dir) {
1663 remove_proc_entry("info", device_dir);
1664 remove_proc_entry("ROM", device_dir);
1665 remove_proc_entry("POST_info", device_dir);
1666 remove_proc_entry("POST", device_dir);
1667 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001668 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 acpi_device_dir(device) = NULL;
1670 }
1671
Patrick Mocheld550d982006-06-27 00:41:40 -04001672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675/* --------------------------------------------------------------------------
1676 Driver Interface
1677 -------------------------------------------------------------------------- */
1678
1679/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001680static struct acpi_video_device_attrib*
1681acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1682{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001683 struct acpi_video_enumerated_device *ids;
1684 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001685
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001686 for (i = 0; i < video->attached_count; i++) {
1687 ids = &video->attached_array[i];
1688 if ((ids->value.int_val & 0xffff) == device_id)
1689 return &ids->value.attrib;
1690 }
1691
Rui Zhang82cae992007-01-03 23:40:53 -05001692 return NULL;
1693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695static int
Len Brown4be44fc2005-08-05 00:44:28 -04001696acpi_video_bus_get_one_device(struct acpi_device *device,
1697 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001699 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001700 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001701 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001702 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001705 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Len Brown4be44fc2005-08-05 00:44:28 -04001707 status =
1708 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (ACPI_SUCCESS(status)) {
1710
Burman Yan36bcbec2006-12-19 12:56:11 -08001711 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1716 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001717 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 data->device_id = device_id;
1720 data->video = video;
1721 data->dev = device;
1722
Rui Zhang82cae992007-01-03 23:40:53 -05001723 attribute = acpi_video_get_device_attr(video, device_id);
1724
1725 if((attribute != NULL) && attribute->device_id_scheme) {
1726 switch (attribute->display_type) {
1727 case ACPI_VIDEO_DISPLAY_CRT:
1728 data->flags.crt = 1;
1729 break;
1730 case ACPI_VIDEO_DISPLAY_TV:
1731 data->flags.tvout = 1;
1732 break;
1733 case ACPI_VIDEO_DISPLAY_DVI:
1734 data->flags.dvi = 1;
1735 break;
1736 case ACPI_VIDEO_DISPLAY_LCD:
1737 data->flags.lcd = 1;
1738 break;
1739 default:
1740 data->flags.unknown = 1;
1741 break;
1742 }
1743 if(attribute->bios_can_detect)
1744 data->flags.bios = 1;
1745 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 acpi_video_device_bind(video, data);
1749 acpi_video_device_find_cap(data);
1750
Patrick Mochel90130262006-05-19 16:54:48 -04001751 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001752 ACPI_DEVICE_NOTIFY,
1753 acpi_video_device_notify,
1754 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001756 printk(KERN_ERR PREFIX
1757 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001758 if(data->brightness)
1759 kfree(data->brightness->levels);
1760 kfree(data->brightness);
1761 kfree(data);
1762 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001765 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001767 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 acpi_video_device_add_fs(device);
1770
Patrick Mocheld550d982006-06-27 00:41:40 -04001771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Patrick Mocheld550d982006-06-27 00:41:40 -04001774 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/*
1778 * Arg:
1779 * video : video bus device
1780 *
1781 * Return:
1782 * none
1783 *
1784 * Enumerate the video device list of the video bus,
1785 * bind the ids with the corresponding video devices
1786 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001787 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Len Brown4be44fc2005-08-05 00:44:28 -04001789static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001791 struct acpi_video_device *dev;
1792
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001793 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001794
1795 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001796 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001797
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001798 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
1801/*
1802 * Arg:
1803 * video : video bus device
1804 * device : video output device under the video
1805 * bus
1806 *
1807 * Return:
1808 * none
1809 *
1810 * Bind the ids with the corresponding video devices
1811 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814static void
Len Brown4be44fc2005-08-05 00:44:28 -04001815acpi_video_device_bind(struct acpi_video_bus *video,
1816 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001818 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001819 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001821 for (i = 0; i < video->attached_count; i++) {
1822 ids = &video->attached_array[i];
1823 if (device->device_id == (ids->value.int_val & 0xffff)) {
1824 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1826 }
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830/*
1831 * Arg:
1832 * video : video bus device
1833 *
1834 * Return:
1835 * < 0 : error
1836 *
1837 * Call _DOD to enumerate all devices attached to display adapter
1838 *
Len Brown4be44fc2005-08-05 00:44:28 -04001839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1842{
Len Brown4be44fc2005-08-05 00:44:28 -04001843 int status;
1844 int count;
1845 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001846 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001847 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1848 union acpi_object *dod = NULL;
1849 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Patrick Mochel90130262006-05-19 16:54:48 -04001851 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001853 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001854 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001857 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001859 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 status = -EFAULT;
1861 goto out;
1862 }
1863
1864 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001865 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001867 active_list = kcalloc(1 + dod->package.count,
1868 sizeof(struct acpi_video_enumerated_device),
1869 GFP_KERNEL);
1870 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 status = -ENOMEM;
1872 goto out;
1873 }
1874
1875 count = 0;
1876 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001877 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001880 printk(KERN_ERR PREFIX
1881 "Invalid _DOD data in element %d\n", i);
1882 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001884
1885 active_list[count].value.int_val = obj->integer.value;
1886 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001887 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1888 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 count++;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Jesper Juhl6044ec82005-11-07 01:01:32 -08001892 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001893
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001894 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001896
1897 out:
Len Brown02438d82006-06-30 03:19:10 -04001898 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001899 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Len Brown4be44fc2005-08-05 00:44:28 -04001902static int
1903acpi_video_get_next_level(struct acpi_video_device *device,
1904 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001906 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001907 max = max_below = 0;
1908 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001909 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001910 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001911 l = device->brightness->levels[i];
1912 if (abs(l - level_current) < abs(delta)) {
1913 delta = l - level_current;
1914 if (!delta)
1915 break;
1916 }
1917 }
1918 /* Ajust level_current to closest available level */
1919 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001920 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001921 l = device->brightness->levels[i];
1922 if (l < min)
1923 min = l;
1924 if (l > max)
1925 max = l;
1926 if (l < min_above && l > level_current)
1927 min_above = l;
1928 if (l > max_below && l < level_current)
1929 max_below = l;
1930 }
1931
1932 switch (event) {
1933 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1934 return (level_current < max) ? min_above : min;
1935 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1936 return (level_current < max) ? min_above : max;
1937 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1938 return (level_current > min) ? max_below : min;
1939 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1940 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1941 return 0;
1942 default:
1943 return level_current;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Zhang Ruic8890f92009-03-18 16:27:08 +08001947static int
Len Brown4be44fc2005-08-05 00:44:28 -04001948acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001950 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001951 int result = -EINVAL;
1952
Julia Jomantaite469778c2008-06-23 22:50:42 +01001953 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001954 goto out;
1955
1956 result = acpi_video_device_lcd_get_level_current(device,
1957 &level_current);
1958 if (result)
1959 goto out;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001962
Zhang Rui24450c72009-03-18 16:27:10 +08001963 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001964
1965out:
1966 if (result)
1967 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1968
1969 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972static int
Len Brown4be44fc2005-08-05 00:44:28 -04001973acpi_video_bus_get_devices(struct acpi_video_bus *video,
1974 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Len Brown4be44fc2005-08-05 00:44:28 -04001976 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001977 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 acpi_video_device_enumerate(video);
1980
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001981 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 status = acpi_video_bus_get_one_device(dev, video);
1984 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001985 printk(KERN_WARNING PREFIX
1986 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 continue;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001990 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Len Brown4be44fc2005-08-05 00:44:28 -04001993static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Karol Kozimor031ec772005-07-30 04:18:00 -04001995 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct acpi_video_bus *video;
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002000 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 video = device->video;
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 acpi_video_device_remove_fs(device->dev);
2005
Patrick Mochel90130262006-05-19 16:54:48 -04002006 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002007 ACPI_DEVICE_NOTIFY,
2008 acpi_video_device_notify);
Zhang Rui056c3082009-06-22 11:31:14 +08002009 sysfs_remove_link(&device->backlight->dev.kobj, "device");
Richard Purdie599a52d2007-02-10 23:07:48 +00002010 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08002011 if (device->cdev) {
2012 sysfs_remove_link(&device->dev->dev.kobj,
2013 "thermal_cooling");
2014 sysfs_remove_link(&device->cdev->device.kobj,
2015 "device");
2016 thermal_cooling_device_unregister(device->cdev);
2017 device->cdev = NULL;
2018 }
Luming Yu23b0f012007-05-09 21:07:05 +08002019 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002020
Patrick Mocheld550d982006-06-27 00:41:40 -04002021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Len Brown4be44fc2005-08-05 00:44:28 -04002024static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Len Brown4be44fc2005-08-05 00:44:28 -04002026 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002027 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002029 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002031 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002033 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002034 if (ACPI_FAILURE(status))
2035 printk(KERN_WARNING PREFIX
2036 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002038 if (dev->brightness) {
2039 kfree(dev->brightness->levels);
2040 kfree(dev->brightness);
2041 }
2042 list_del(&dev->entry);
2043 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002046 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002047
Patrick Mocheld550d982006-06-27 00:41:40 -04002048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
2051/* acpi_video interface */
2052
Len Brown4be44fc2005-08-05 00:44:28 -04002053static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Zhang Ruia21101c2007-09-14 11:46:22 +08002055 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Len Brown4be44fc2005-08-05 00:44:28 -04002058static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 return acpi_video_bus_DOS(video, 0, 1);
2061}
2062
Bjorn Helgaas70155582009-04-07 15:37:11 +00002063static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002065 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002066 struct input_dev *input;
2067 int keycode;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Luming Yue9dab192007-08-20 18:23:53 +08002072 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002075 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002077 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002078 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
2080
Julius Volz98fb8fe2007-02-20 16:38:40 +01002081 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * connector. */
2083 acpi_video_device_enumerate(video);
2084 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002085 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002086 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088
Len Brown4be44fc2005-08-05 00:44:28 -04002089 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002090 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002091 keycode = KEY_SWITCHVIDEOMODE;
2092 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002093 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002094 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002095 keycode = KEY_VIDEO_NEXT;
2096 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002097 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002098 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002099 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 break;
2101
2102 default:
Luming Yue9dab192007-08-20 18:23:53 +08002103 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002105 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 break;
2107 }
2108
Zhang Rui7761f632008-01-25 14:48:12 +08002109 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002110 input_report_key(input, keycode, 1);
2111 input_sync(input);
2112 input_report_key(input, keycode, 0);
2113 input_sync(input);
2114
Patrick Mocheld550d982006-06-27 00:41:40 -04002115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Len Brown4be44fc2005-08-05 00:44:28 -04002118static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002120 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002121 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002122 struct acpi_video_bus *bus;
2123 struct input_dev *input;
2124 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002127 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002129 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002130 bus = video_device->video;
2131 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002134 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002135 if (brightness_switch_enabled)
2136 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002137 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002138 keycode = KEY_BRIGHTNESS_CYCLE;
2139 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002140 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002141 if (brightness_switch_enabled)
2142 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002143 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002144 keycode = KEY_BRIGHTNESSUP;
2145 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002146 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002147 if (brightness_switch_enabled)
2148 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002149 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002150 keycode = KEY_BRIGHTNESSDOWN;
2151 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002152 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002153 if (brightness_switch_enabled)
2154 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002155 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002156 keycode = KEY_BRIGHTNESS_ZERO;
2157 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002158 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002159 if (brightness_switch_enabled)
2160 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002161 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002162 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 break;
2164 default:
Luming Yue9dab192007-08-20 18:23:53 +08002165 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002167 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 }
Luming Yue9dab192007-08-20 18:23:53 +08002170
Zhang Rui7761f632008-01-25 14:48:12 +08002171 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002172 input_report_key(input, keycode, 1);
2173 input_sync(input);
2174 input_report_key(input, keycode, 0);
2175 input_sync(input);
2176
Patrick Mocheld550d982006-06-27 00:41:40 -04002177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Zhang Ruie6d9da12007-08-25 02:23:31 -04002180static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002181static int acpi_video_resume(struct acpi_device *device)
2182{
2183 struct acpi_video_bus *video;
2184 struct acpi_video_device *video_device;
2185 int i;
2186
2187 if (!device || !acpi_driver_data(device))
2188 return -EINVAL;
2189
2190 video = acpi_driver_data(device);
2191
2192 for (i = 0; i < video->attached_count; i++) {
2193 video_device = video->attached_array[i].bind_info;
2194 if (video_device && video_device->backlight)
2195 acpi_video_set_brightness(video_device->backlight);
2196 }
2197 return AE_OK;
2198}
2199
Len Brown4be44fc2005-08-05 00:44:28 -04002200static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002202 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002203 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002204 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Burman Yan36bcbec2006-12-19 12:56:11 -08002206 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002208 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Zhang Ruie6d9da12007-08-25 02:23:31 -04002210 /* a hack to fix the duplicate name "VID" problem on T61 */
2211 if (!strcmp(device->pnp.bus_id, "VID")) {
2212 if (instance)
2213 device->pnp.bus_id[3] = '0' + instance;
2214 instance ++;
2215 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002216 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2217 if (!strcmp(device->pnp.bus_id, "VGA")) {
2218 if (instance)
2219 device->pnp.bus_id[3] = '0' + instance;
2220 instance++;
2221 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002222
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002223 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2225 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002226 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002229 error = acpi_video_bus_check(video);
2230 if (error)
2231 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002233 error = acpi_video_bus_add_fs(device);
2234 if (error)
2235 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002237 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 INIT_LIST_HEAD(&video->video_device_list);
2239
2240 acpi_video_bus_get_devices(video, device);
2241 acpi_video_bus_start_devices(video);
2242
Luming Yue9dab192007-08-20 18:23:53 +08002243 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002244 if (!input) {
2245 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002246 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002247 }
Luming Yue9dab192007-08-20 18:23:53 +08002248
2249 snprintf(video->phys, sizeof(video->phys),
2250 "%s/video/input0", acpi_device_hid(video->device));
2251
2252 input->name = acpi_device_name(video->device);
2253 input->phys = video->phys;
2254 input->id.bustype = BUS_HOST;
2255 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002256 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002257 input->evbit[0] = BIT(EV_KEY);
2258 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2259 set_bit(KEY_VIDEO_NEXT, input->keybit);
2260 set_bit(KEY_VIDEO_PREV, input->keybit);
2261 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2262 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2263 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2264 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2265 set_bit(KEY_DISPLAY_OFF, input->keybit);
2266 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002267
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002268 error = input_register_device(input);
2269 if (error)
2270 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002273 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2274 video->flags.multihead ? "yes" : "no",
2275 video->flags.rom ? "yes" : "no",
2276 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002280 err_free_input_dev:
2281 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002282 err_stop_video:
2283 acpi_video_bus_stop_devices(video);
2284 acpi_video_bus_put_devices(video);
2285 kfree(video->attached_array);
2286 acpi_video_bus_remove_fs(device);
2287 err_free_video:
2288 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002289 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002290
2291 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Len Brown4be44fc2005-08-05 00:44:28 -04002294static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Len Brown4be44fc2005-08-05 00:44:28 -04002296 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002302 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 acpi_video_bus_put_devices(video);
2306 acpi_video_bus_remove_fs(device);
2307
Luming Yue9dab192007-08-20 18:23:53 +08002308 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002309 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 kfree(video);
2311
Patrick Mocheld550d982006-06-27 00:41:40 -04002312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Matthew Garrett74a365b2009-03-19 21:35:39 +00002315static int __init intel_opregion_present(void)
2316{
2317#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2318 struct pci_dev *dev = NULL;
2319 u32 address;
2320
2321 for_each_pci_dev(dev) {
2322 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2323 continue;
2324 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2325 continue;
2326 pci_read_config_dword(dev, 0xfc, &address);
2327 if (!address)
2328 continue;
2329 return 1;
2330 }
2331#endif
2332 return 0;
2333}
2334
2335int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Len Brown4be44fc2005-08-05 00:44:28 -04002337 int result = 0;
Zhao Yakui86e437f2009-06-16 11:23:13 +08002338 if (register_count) {
2339 /*
2340 * if the function of acpi_video_register is already called,
2341 * don't register the acpi_vide_bus again and return no error.
2342 */
2343 return 0;
2344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2347 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002348 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 result = acpi_bus_register_driver(&acpi_video_bus);
2351 if (result < 0) {
2352 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002353 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
Zhao Yakui86e437f2009-06-16 11:23:13 +08002356 /*
2357 * When the acpi_video_bus is loaded successfully, increase
2358 * the counter reference.
2359 */
2360 register_count = 1;
2361
Patrick Mocheld550d982006-06-27 00:41:40 -04002362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002364EXPORT_SYMBOL(acpi_video_register);
2365
Zhao Yakui86e437f2009-06-16 11:23:13 +08002366void acpi_video_unregister(void)
2367{
2368 if (!register_count) {
2369 /*
2370 * If the acpi video bus is already unloaded, don't
2371 * unload it again and return directly.
2372 */
2373 return;
2374 }
2375 acpi_bus_unregister_driver(&acpi_video_bus);
2376
2377 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2378
2379 register_count = 0;
2380
2381 return;
2382}
2383EXPORT_SYMBOL(acpi_video_unregister);
2384
Matthew Garrett74a365b2009-03-19 21:35:39 +00002385/*
2386 * This is kind of nasty. Hardware using Intel chipsets may require
2387 * the video opregion code to be run first in order to initialise
2388 * state before any ACPI video calls are made. To handle this we defer
2389 * registration of the video class until the opregion code has run.
2390 */
2391
2392static int __init acpi_video_init(void)
2393{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002394 dmi_check_system(video_dmi_table);
2395
Matthew Garrett74a365b2009-03-19 21:35:39 +00002396 if (intel_opregion_present())
2397 return 0;
2398
2399 return acpi_video_register();
2400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Zhao Yakui86e437f2009-06-16 11:23:13 +08002402static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Zhao Yakui86e437f2009-06-16 11:23:13 +08002404 acpi_video_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Patrick Mocheld550d982006-06-27 00:41:40 -04002406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409module_init(acpi_video_init);
2410module_exit(acpi_video_exit);