blob: 6d897bbd3bd006b6821d30e0db11c89138fdfa4a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_VIDEO_BUS_NAME "Video Bus"
49#define ACPI_VIDEO_DEVICE_NAME "Video Device"
50#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
51#define ACPI_VIDEO_NOTIFY_PROBE 0x81
52#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
53#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
54#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
55
Thomas Tuttlef4715182006-12-19 12:56:14 -080056#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
57#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
58#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
59#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
60#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Yu Luming2f3d0002006-11-11 02:40:34 +080062#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Rui Zhang82cae992007-01-03 23:40:53 -050064#define ACPI_VIDEO_DISPLAY_CRT 1
65#define ACPI_VIDEO_DISPLAY_TV 2
66#define ACPI_VIDEO_DISPLAY_DVI 3
67#define ACPI_VIDEO_DISPLAY_LCD 4
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050070ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Len Brownf52fd662007-02-12 22:42:12 -050072MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050073MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_LICENSE("GPL");
75
Zhang Rui8a681a42008-01-25 14:47:49 +080076static int brightness_switch_enabled = 1;
77module_param(brightness_switch_enabled, bool, 0644);
78
Len Brown4be44fc2005-08-05 00:44:28 -040079static int acpi_video_bus_add(struct acpi_device *device);
80static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080081static int acpi_video_resume(struct acpi_device *device);
Bjorn Helgaas70155582009-04-07 15:37:11 +000082static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Thomas Renninger1ba90e32007-07-23 14:44:41 +020084static const struct acpi_device_id video_device_ids[] = {
85 {ACPI_VIDEO_HID, 0},
86 {"", 0},
87};
88MODULE_DEVICE_TABLE(acpi, video_device_ids);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct acpi_driver acpi_video_bus = {
Len Brownc2b6705b2007-02-12 23:33:40 -050091 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020093 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .ops = {
95 .add = acpi_video_bus_add,
96 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -080097 .resume = acpi_video_resume,
Bjorn Helgaas70155582009-04-07 15:37:11 +000098 .notify = acpi_video_bus_notify,
Len Brown4be44fc2005-08-05 00:44:28 -040099 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400103 u8 multihead:1; /* can switch video heads */
104 u8 rom:1; /* can retrieve a video rom */
105 u8 post:1; /* can configure the head to */
106 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 u8 _DOS:1; /*Enable/Disable output switching */
111 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
112 u8 _ROM:1; /*Get ROM Data */
113 u8 _GPD:1; /*Get POST Device */
114 u8 _SPD:1; /*Set POST Device */
115 u8 _VPO:1; /*Video POST Options */
116 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Len Brown4be44fc2005-08-05 00:44:28 -0400119struct acpi_video_device_attrib {
120 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100121 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400122 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100123 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400124 u32 bios_can_detect:1; /*BIOS can detect the device */
125 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
126 the VGA device. */
127 u32 pipe_id:3; /*For VGA multiple-head devices. */
128 u32 reserved:10; /*Must be 0 */
129 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132struct acpi_video_enumerated_device {
133 union {
134 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400135 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 } value;
137 struct acpi_video_device *bind_info;
138};
139
140struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400141 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400142 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400144 u8 attached_count;
145 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500148 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400149 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800150 struct input_dev *input;
151 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 crt:1;
156 u8 lcd:1;
157 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500158 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400159 u8 bios:1;
160 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500161 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 _ADR:1; /*Return the unique ID */
166 u8 _BCL:1; /*Query list of brightness control levels supported */
167 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800168 u8 _BQC:1; /* Get current brightness level */
Zhang Ruic60d6382009-03-18 16:27:18 +0800169 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400170 u8 _DDC:1; /*Return the EDID for this device */
171 u8 _DCS:1; /*Return status of output device */
172 u8 _DGS:1; /*Query graphics state */
173 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
Zhang Ruid32f6942009-03-18 16:27:12 +0800176struct acpi_video_brightness_flags {
177 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800178 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800179 u8 _BCL_use_index:1; /* levels in _BCL are index values */
180 u8 _BCM_use_index:1; /* input of _BCM is an index value */
181 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800182};
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int curr;
186 int count;
187 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800188 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400192 unsigned long device_id;
193 struct acpi_video_device_flags flags;
194 struct acpi_video_device_cap cap;
195 struct list_head entry;
196 struct acpi_video_bus *video;
197 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800199 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800200 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800201 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* bus */
205static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100206static const struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700207 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400208 .open = acpi_video_bus_info_open_fs,
209 .read = seq_read,
210 .llseek = seq_lseek,
211 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100215static const struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700216 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400217 .open = acpi_video_bus_ROM_open_fs,
218 .read = seq_read,
219 .llseek = seq_lseek,
220 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Len Brown4be44fc2005-08-05 00:44:28 -0400223static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
224 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100225static const struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700226 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400227 .open = acpi_video_bus_POST_info_open_fs,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400234static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100235 const char __user *buffer, size_t count, loff_t *data);
236static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700237 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400238 .open = acpi_video_bus_POST_open_fs,
239 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100240 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 .llseek = seq_lseek,
242 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400246static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100247 const char __user *buffer, size_t count, loff_t *data);
248static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700249 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400250 .open = acpi_video_bus_DOS_open_fs,
251 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100252 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400253 .llseek = seq_lseek,
254 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
257/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_video_device_info_open_fs(struct inode *inode,
259 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100260static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700261 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400262 .open = acpi_video_device_info_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_state_open_fs(struct inode *inode,
269 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400270static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100271 const char __user *buffer, size_t count, loff_t *data);
272static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700273 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400274 .open = acpi_video_device_state_open_fs,
275 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100276 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 .llseek = seq_lseek,
278 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Len Brown4be44fc2005-08-05 00:44:28 -0400281static int acpi_video_device_brightness_open_fs(struct inode *inode,
282 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400283static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100284 const char __user *buffer, size_t count, loff_t *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700286 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400287 .open = acpi_video_device_brightness_open_fs,
288 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100289 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400290 .llseek = seq_lseek,
291 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294static int acpi_video_device_EDID_open_fs(struct inode *inode,
295 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100296static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700297 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400298 .open = acpi_video_device_EDID_open_fs,
299 .read = seq_read,
300 .llseek = seq_lseek,
301 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100304static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 "motherboard VGA device",
306 "PCI VGA device",
307 "AGP VGA device",
308 "UNKNOWN",
309};
310
Len Brown4be44fc2005-08-05 00:44:28 -0400311static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
312static void acpi_video_device_rebind(struct acpi_video_bus *video);
313static void acpi_video_device_bind(struct acpi_video_bus *video,
314 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800316static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
317 int level);
318static int acpi_video_device_lcd_get_level_current(
319 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400320 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400321static int acpi_video_get_next_level(struct acpi_video_device *device,
322 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800323static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400324 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800325static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400326 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800327static int acpi_video_output_get(struct output_device *od);
328static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Yu Luming2f3d0002006-11-11 02:40:34 +0800330/*backlight device sysfs support*/
331static int acpi_video_get_brightness(struct backlight_device *bd)
332{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400333 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000334 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800335 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100336 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800337
338 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
339 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000340 for (i = 2; i < vd->brightness->count; i++) {
341 if (vd->brightness->levels[i] == cur_level)
342 /* The first two entries are special - see page 575
343 of the ACPI spec 3.0 */
344 return i-2;
345 }
346 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800347}
348
349static int acpi_video_set_brightness(struct backlight_device *bd)
350{
Zhang Rui24450c72009-03-18 16:27:10 +0800351 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800352 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100353 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800354
355 return acpi_video_device_lcd_set_level(vd,
356 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800357}
358
Richard Purdie599a52d2007-02-10 23:07:48 +0000359static struct backlight_ops acpi_backlight_ops = {
360 .get_brightness = acpi_video_get_brightness,
361 .update_status = acpi_video_set_brightness,
362};
363
Luming Yu23b0f012007-05-09 21:07:05 +0800364/*video output device sysfs support*/
365static int acpi_video_output_get(struct output_device *od)
366{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400367 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800368 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700369 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800370 acpi_video_device_get_state(vd, &state);
371 return (int)state;
372}
373
374static int acpi_video_output_set(struct output_device *od)
375{
376 unsigned long state = od->request_state;
377 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700378 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800379 return acpi_video_device_set_state(vd, state);
380}
381
382static struct output_properties acpi_output_properties = {
383 .set_state = acpi_video_output_set,
384 .get_status = acpi_video_output_get,
385};
Zhang Rui702ed512008-01-17 15:51:22 +0800386
387
388/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000389static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
390 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800391{
392 struct acpi_device *device = cdev->devdata;
393 struct acpi_video_device *video = acpi_driver_data(device);
394
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000395 *state = video->brightness->count - 3;
396 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800397}
398
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000399static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
400 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800401{
402 struct acpi_device *device = cdev->devdata;
403 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400404 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000405 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800406
Zhang Ruic8890f92009-03-18 16:27:08 +0800407 if (acpi_video_device_lcd_get_level_current(video, &level))
408 return -EINVAL;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000409 for (offset = 2; offset < video->brightness->count; offset++)
410 if (level == video->brightness->levels[offset]) {
411 *state = video->brightness->count - offset - 1;
412 return 0;
413 }
Zhang Rui702ed512008-01-17 15:51:22 +0800414
415 return -EINVAL;
416}
417
418static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000419video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800420{
421 struct acpi_device *device = cdev->devdata;
422 struct acpi_video_device *video = acpi_driver_data(device);
423 int level;
424
425 if ( state >= video->brightness->count - 2)
426 return -EINVAL;
427
428 state = video->brightness->count - state;
429 level = video->brightness->levels[state -1];
430 return acpi_video_device_lcd_set_level(video, level);
431}
432
433static struct thermal_cooling_device_ops video_cooling_ops = {
434 .get_max_state = video_get_max_state,
435 .get_cur_state = video_get_cur_state,
436 .set_cur_state = video_set_cur_state,
437};
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/* --------------------------------------------------------------------------
440 Video Management
441 -------------------------------------------------------------------------- */
442
443/* device */
444
445static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400446acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Len Brown4be44fc2005-08-05 00:44:28 -0400448 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400449
450 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Patrick Mocheld550d982006-06-27 00:41:40 -0400452 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455static int
Len Brown4be44fc2005-08-05 00:44:28 -0400456acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400457 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Len Brown4be44fc2005-08-05 00:44:28 -0400459 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Patrick Mochel90130262006-05-19 16:54:48 -0400461 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466static int
Len Brown4be44fc2005-08-05 00:44:28 -0400467acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Len Brown4be44fc2005-08-05 00:44:28 -0400469 int status;
470 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
471 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400472 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400476 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Patrick Mocheld550d982006-06-27 00:41:40 -0400478 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481static int
Len Brown4be44fc2005-08-05 00:44:28 -0400482acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
483 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Len Brown4be44fc2005-08-05 00:44:28 -0400485 int status;
486 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
487 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 *levels = NULL;
491
Patrick Mochel90130262006-05-19 16:54:48 -0400492 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400495 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500496 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400497 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 status = -EFAULT;
499 goto err;
500 }
501
502 *levels = obj;
503
Patrick Mocheld550d982006-06-27 00:41:40 -0400504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Len Brown4be44fc2005-08-05 00:44:28 -0400506 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800507 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Patrick Mocheld550d982006-06-27 00:41:40 -0400509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512static int
Len Brown4be44fc2005-08-05 00:44:28 -0400513acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Zhang Rui24450c72009-03-18 16:27:10 +0800515 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400516 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
517 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800518 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Zhang Rui24450c72009-03-18 16:27:10 +0800522 status = acpi_evaluate_object(device->dev->handle, "_BCM",
523 &args, NULL);
524 if (ACPI_FAILURE(status)) {
525 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
526 return -EIO;
527 }
528
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400529 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800530 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800531 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800532 if (device->backlight)
533 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800534 return 0;
535 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800536
Zhang Rui24450c72009-03-18 16:27:10 +0800537 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Zhang Rui45cb50e2009-04-24 12:13:18 -0400541/*
542 * For some buggy _BQC methods, we need to add a constant value to
543 * the _BQC return value to get the actual current brightness level
544 */
545
546static int bqc_offset_aml_bug_workaround;
547static int __init video_set_bqc_offset(const struct dmi_system_id *d)
548{
549 bqc_offset_aml_bug_workaround = 9;
550 return 0;
551}
552
553static struct dmi_system_id video_dmi_table[] __initdata = {
554 /*
555 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
556 */
557 {
558 .callback = video_set_bqc_offset,
559 .ident = "Acer Aspire 5720",
560 .matches = {
561 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
562 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
563 },
564 },
Len Brown5afc4ab2009-05-07 21:11:56 -0400565 {
566 .callback = video_set_bqc_offset,
567 .ident = "Acer Aspire 5710Z",
568 .matches = {
569 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
570 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
571 },
572 },
Zhang Rui34ac2722009-05-26 23:35:34 -0400573 {
574 .callback = video_set_bqc_offset,
575 .ident = "eMachines E510",
576 .matches = {
577 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
578 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
579 },
580 },
Zhang Rui93bcece2009-05-19 15:08:41 -0400581 {
582 .callback = video_set_bqc_offset,
583 .ident = "Acer Aspire 5315",
584 .matches = {
585 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
586 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
587 },
588 },
Zhang Rui45cb50e2009-04-24 12:13:18 -0400589 {}
590};
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static int
Len Brown4be44fc2005-08-05 00:44:28 -0400593acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400594 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Zhang Ruic8890f92009-03-18 16:27:08 +0800596 acpi_status status = AE_OK;
597
Zhang Ruic60d6382009-03-18 16:27:18 +0800598 if (device->cap._BQC || device->cap._BCQ) {
599 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
600
601 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800602 NULL, level);
603 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800604 if (device->brightness->flags._BQC_use_index) {
605 if (device->brightness->flags._BCL_reversed)
606 *level = device->brightness->count
607 - 3 - (*level);
608 *level = device->brightness->levels[*level + 2];
609
610 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400611 *level += bqc_offset_aml_bug_workaround;
Zhang Ruic8890f92009-03-18 16:27:08 +0800612 device->brightness->curr = *level;
613 return 0;
614 } else {
615 /* Fixme:
616 * should we return an error or ignore this failure?
617 * dev->brightness->curr is a cached value which stores
618 * the correct current backlight level in most cases.
619 * ACPI video backlight still works w/ buggy _BQC.
620 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
621 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800622 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
623 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800624 }
625 }
626
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400627 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static int
Len Brown4be44fc2005-08-05 00:44:28 -0400632acpi_video_device_EDID(struct acpi_video_device *device,
633 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Len Brown4be44fc2005-08-05 00:44:28 -0400635 int status;
636 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
637 union acpi_object *obj;
638 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
639 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 *edid = NULL;
643
644 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400645 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (length == 128)
647 arg0.integer.value = 1;
648 else if (length == 256)
649 arg0.integer.value = 2;
650 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400651 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Patrick Mochel90130262006-05-19 16:54:48 -0400653 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400655 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200657 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (obj && obj->type == ACPI_TYPE_BUFFER)
660 *edid = obj;
661 else {
Len Brown64684632006-06-26 23:41:38 -0400662 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 status = -EFAULT;
664 kfree(obj);
665 }
666
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/* bus */
671
672static int
Len Brown4be44fc2005-08-05 00:44:28 -0400673acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Len Brown4be44fc2005-08-05 00:44:28 -0400675 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400676 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400677 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
678 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 arg0.integer.value = option;
682
Patrick Mochel90130262006-05-19 16:54:48 -0400683 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400685 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Patrick Mocheld550d982006-06-27 00:41:40 -0400687 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400691acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int status;
694
Patrick Mochel90130262006-05-19 16:54:48 -0400695 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static int
Len Brown4be44fc2005-08-05 00:44:28 -0400701acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400702 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Len Brown4be44fc2005-08-05 00:44:28 -0400704 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Patrick Mochel90130262006-05-19 16:54:48 -0400706 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *options &= 3;
708
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
713 * Arg:
714 * video : video bus device pointer
715 * bios_flag :
716 * 0. The system BIOS should NOT automatically switch(toggle)
717 * the active display output.
718 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100719 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * 2. The _DGS value should be locked.
721 * 3. The system BIOS should not automatically switch (toggle) the
722 * active display output, but instead generate the display switch
723 * event notify code.
724 * lcd_flag :
725 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100726 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100728 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * Return Value:
730 * -1 wrong arg.
731 */
732
733static int
Len Brown4be44fc2005-08-05 00:44:28 -0400734acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Len Brown4be44fc2005-08-05 00:44:28 -0400736 acpi_integer status = 0;
737 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
738 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Len Brown4be44fc2005-08-05 00:44:28 -0400741 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 status = -1;
743 goto Failed;
744 }
745 arg0.integer.value = (lcd_flag << 2) | bios_flag;
746 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400747 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Len Brown4be44fc2005-08-05 00:44:28 -0400749 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400750 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500754 * Simple comparison function used to sort backlight levels.
755 */
756
757static int
758acpi_video_cmp_level(const void *a, const void *b)
759{
760 return *(int *)a - *(int *)b;
761}
762
763/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * Arg:
765 * device : video output device (LCD, CRT, ..)
766 *
767 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100768 * Maximum brightness level
769 *
770 * Allocate and initialize device->brightness.
771 */
772
773static int
774acpi_video_init_brightness(struct acpi_video_device *device)
775{
776 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800777 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800778 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100779 union acpi_object *o;
780 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800781 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100782
783 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
784 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
785 "LCD brightness level\n"));
786 goto out;
787 }
788
789 if (obj->package.count < 2)
790 goto out;
791
792 br = kzalloc(sizeof(*br), GFP_KERNEL);
793 if (!br) {
794 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800795 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100796 goto out;
797 }
798
Zhang Ruid32f6942009-03-18 16:27:12 +0800799 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100800 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800801 if (!br->levels) {
802 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100803 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800804 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100805
806 for (i = 0; i < obj->package.count; i++) {
807 o = (union acpi_object *)&obj->package.elements[i];
808 if (o->type != ACPI_TYPE_INTEGER) {
809 printk(KERN_ERR PREFIX "Invalid data\n");
810 continue;
811 }
812 br->levels[count] = (u32) o->integer.value;
813
814 if (br->levels[count] > max_level)
815 max_level = br->levels[count];
816 count++;
817 }
818
Zhang Ruid32f6942009-03-18 16:27:12 +0800819 /*
820 * some buggy BIOS don't export the levels
821 * when machine is on AC/Battery in _BCL package.
822 * In this case, the first two elements in _BCL packages
823 * are also supported brightness levels that OS should take care of.
824 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800825 for (i = 2; i < count; i++) {
826 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800827 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800828 if (br->levels[i] == br->levels[1])
829 level_ac_battery++;
830 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500831
Zhang Ruid32f6942009-03-18 16:27:12 +0800832 if (level_ac_battery < 2) {
833 level_ac_battery = 2 - level_ac_battery;
834 br->flags._BCL_no_ac_battery_levels = 1;
835 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
836 br->levels[i] = br->levels[i - level_ac_battery];
837 count += level_ac_battery;
838 } else if (level_ac_battery > 2)
839 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
840
Zhang Ruid80fb992009-03-18 16:27:14 +0800841 /* Check if the _BCL package is in a reversed order */
842 if (max_level == br->levels[2]) {
843 br->flags._BCL_reversed = 1;
844 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
845 acpi_video_cmp_level, NULL);
846 } else if (max_level != br->levels[count - 1])
847 ACPI_ERROR((AE_INFO,
848 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100849
850 br->count = count;
851 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800852
853 /* Check the input/output of _BQC/_BCL/_BCM */
854 if ((max_level < 100) && (max_level <= (count - 2)))
855 br->flags._BCL_use_index = 1;
856
857 /*
858 * _BCM is always consistent with _BCL,
859 * at least for all the laptops we have ever seen.
860 */
861 br->flags._BCM_use_index = br->flags._BCL_use_index;
862
863 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Ruie047cca2009-04-09 14:24:35 +0800864 br->curr = level_old = max_level;
865
866 if (!device->cap._BQC)
867 goto set_level;
868
Zhang Rui1a7c6182009-03-18 16:27:16 +0800869 result = acpi_video_device_lcd_get_level_current(device, &level_old);
870 if (result)
871 goto out_free_levels;
872
Zhang Ruie047cca2009-04-09 14:24:35 +0800873 /*
874 * Set the level to maximum and check if _BQC uses indexed value
875 */
876 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800877 if (result)
878 goto out_free_levels;
879
880 result = acpi_video_device_lcd_get_level_current(device, &level);
881 if (result)
882 goto out_free_levels;
883
Zhang Ruie047cca2009-04-09 14:24:35 +0800884 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800885
Zhang Ruie047cca2009-04-09 14:24:35 +0800886 if (!br->flags._BQC_use_index)
887 goto set_level;
888
889 if (br->flags._BCL_reversed)
890 level_old = (br->count - 1) - level_old;
891 level_old = br->levels[level_old];
892
893set_level:
894 result = acpi_video_device_lcd_set_level(device, level_old);
895 if (result)
896 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800897
Zhang Ruid32f6942009-03-18 16:27:12 +0800898 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
899 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100900 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800901 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100902
903out_free_levels:
904 kfree(br->levels);
905out_free:
906 kfree(br);
907out:
908 device->brightness = NULL;
909 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800910 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100911}
912
913/*
914 * Arg:
915 * device : video output device (LCD, CRT, ..)
916 *
917 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * None
919 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100920 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * device.
922 */
923
Len Brown4be44fc2005-08-05 00:44:28 -0400924static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
William Lee Irwin III98934de2007-12-12 03:56:55 -0800929 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Patrick Mochel90130262006-05-19 16:54:48 -0400931 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 device->cap._ADR = 1;
933 }
Patrick Mochel90130262006-05-19 16:54:48 -0400934 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400935 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Patrick Mochel90130262006-05-19 16:54:48 -0400937 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400938 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800940 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
941 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800942 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
943 &h_dummy1))) {
944 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
945 device->cap._BCQ = 1;
946 }
947
Patrick Mochel90130262006-05-19 16:54:48 -0400948 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400949 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Patrick Mochel90130262006-05-19 16:54:48 -0400951 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 device->cap._DCS = 1;
953 }
Patrick Mochel90130262006-05-19 16:54:48 -0400954 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 device->cap._DGS = 1;
956 }
Patrick Mochel90130262006-05-19 16:54:48 -0400957 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 device->cap._DSS = 1;
959 }
960
Zhang Rui1a7c6182009-03-18 16:27:16 +0800961 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800962 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800963 static int count = 0;
964 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800965
966 result = acpi_video_init_brightness(device);
967 if (result)
968 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800969 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
970 if (!name)
971 return;
972
Yu Luming2f3d0002006-11-11 02:40:34 +0800973 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800974 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000975 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000976 device->backlight->props.max_brightness = device->brightness->count-3;
Yu Luming2f3d0002006-11-11 02:40:34 +0800977 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800978
979 device->cdev = thermal_cooling_device_register("LCD",
980 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500981 if (IS_ERR(device->cdev))
982 return;
983
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200984 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
985 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800986 result = sysfs_create_link(&device->dev->dev.kobj,
987 &device->cdev->device.kobj,
988 "thermal_cooling");
989 if (result)
990 printk(KERN_ERR PREFIX "Create sysfs link\n");
991 result = sysfs_create_link(&device->cdev->device.kobj,
992 &device->dev->dev.kobj, "device");
993 if (result)
994 printk(KERN_ERR PREFIX "Create sysfs link\n");
995
Yu Luming2f3d0002006-11-11 02:40:34 +0800996 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200997
998 if (acpi_video_display_switch_support()) {
999
1000 if (device->cap._DCS && device->cap._DSS) {
1001 static int count;
1002 char *name;
1003 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1004 if (!name)
1005 return;
1006 sprintf(name, "acpi_video%d", count++);
1007 device->output_dev = video_output_register(name,
1008 NULL, device, &acpi_output_properties);
1009 kfree(name);
1010 }
Luming Yu23b0f012007-05-09 21:07:05 +08001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * Arg:
1016 * device : video output device (VGA)
1017 *
1018 * Return Value:
1019 * None
1020 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001021 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
1023
Len Brown4be44fc2005-08-05 00:44:28 -04001024static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Len Brown4be44fc2005-08-05 00:44:28 -04001026 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
William Lee Irwin III98934de2007-12-12 03:56:55 -08001028 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -04001029 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 video->cap._DOS = 1;
1031 }
Patrick Mochel90130262006-05-19 16:54:48 -04001032 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 video->cap._DOD = 1;
1034 }
Patrick Mochel90130262006-05-19 16:54:48 -04001035 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 video->cap._ROM = 1;
1037 }
Patrick Mochel90130262006-05-19 16:54:48 -04001038 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 video->cap._GPD = 1;
1040 }
Patrick Mochel90130262006-05-19 16:54:48 -04001041 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 video->cap._SPD = 1;
1043 }
Patrick Mochel90130262006-05-19 16:54:48 -04001044 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 video->cap._VPO = 1;
1046 }
1047}
1048
1049/*
1050 * Check whether the video bus device has required AML method to
1051 * support the desired features
1052 */
1053
Len Brown4be44fc2005-08-05 00:44:28 -04001054static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Len Brown4be44fc2005-08-05 00:44:28 -04001056 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +02001057 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Thomas Renninger22c13f92008-08-01 17:37:54 +02001062 dev = acpi_get_physical_pci_device(video->device->handle);
1063 if (!dev)
1064 return -ENODEV;
1065 put_device(dev);
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Since there is no HID, CID and so on for VGA driver, we have
1068 * to check well known required nodes.
1069 */
1070
Julius Volz98fb8fe2007-02-20 16:38:40 +01001071 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -04001072 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 video->flags.multihead = 1;
1074 status = 0;
1075 }
1076
Julius Volz98fb8fe2007-02-20 16:38:40 +01001077 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001078 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 video->flags.rom = 1;
1080 status = 0;
1081 }
1082
Julius Volz98fb8fe2007-02-20 16:38:40 +01001083 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001084 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 video->flags.post = 1;
1086 status = 0;
1087 }
1088
Patrick Mocheld550d982006-06-27 00:41:40 -04001089 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092/* --------------------------------------------------------------------------
1093 FS Interface (/proc)
1094 -------------------------------------------------------------------------- */
1095
Len Brown4be44fc2005-08-05 00:44:28 -04001096static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098/* video devices */
1099
Len Brown4be44fc2005-08-05 00:44:28 -04001100static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001102 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (!dev)
1106 goto end;
1107
1108 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1109 seq_printf(seq, "type: ");
1110 if (dev->flags.crt)
1111 seq_printf(seq, "CRT\n");
1112 else if (dev->flags.lcd)
1113 seq_printf(seq, "LCD\n");
1114 else if (dev->flags.tvout)
1115 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001116 else if (dev->flags.dvi)
1117 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 else
1119 seq_printf(seq, "UNKNOWN\n");
1120
Len Brown4be44fc2005-08-05 00:44:28 -04001121 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Len Brown4be44fc2005-08-05 00:44:28 -04001123 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static int
Len Brown4be44fc2005-08-05 00:44:28 -04001128acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 return single_open(file, acpi_video_device_info_seq_show,
1131 PDE(inode)->data);
1132}
1133
Len Brown4be44fc2005-08-05 00:44:28 -04001134static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Len Brown4be44fc2005-08-05 00:44:28 -04001136 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001137 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001138 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!dev)
1142 goto end;
1143
1144 status = acpi_video_device_get_state(dev, &state);
1145 seq_printf(seq, "state: ");
1146 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001147 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 else
1149 seq_printf(seq, "<not supported>\n");
1150
1151 status = acpi_video_device_query(dev, &state);
1152 seq_printf(seq, "query: ");
1153 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001154 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 else
1156 seq_printf(seq, "<not supported>\n");
1157
Len Brown4be44fc2005-08-05 00:44:28 -04001158 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static int
Len Brown4be44fc2005-08-05 00:44:28 -04001163acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 return single_open(file, acpi_video_device_state_seq_show,
1166 PDE(inode)->data);
1167}
1168
1169static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001170acpi_video_device_write_state(struct file *file,
1171 const char __user * buffer,
1172 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Len Brown4be44fc2005-08-05 00:44:28 -04001174 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001175 struct seq_file *m = file->private_data;
1176 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001177 char str[12] = { 0 };
1178 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001185 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 str[count] = 0;
1188 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001189 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 status = acpi_video_device_set_state(dev, state);
1192
1193 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001194 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Patrick Mocheld550d982006-06-27 00:41:40 -04001196 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static int
Len Brown4be44fc2005-08-05 00:44:28 -04001200acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001202 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001203 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (!dev || !dev->brightness) {
1207 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210
1211 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001212 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 seq_printf(seq, " %d", dev->brightness->levels[i]);
1214 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1215
Patrick Mocheld550d982006-06-27 00:41:40 -04001216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219static int
Len Brown4be44fc2005-08-05 00:44:28 -04001220acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 return single_open(file, acpi_video_device_brightness_seq_show,
1223 PDE(inode)->data);
1224}
1225
1226static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001227acpi_video_device_write_brightness(struct file *file,
1228 const char __user * buffer,
1229 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001231 struct seq_file *m = file->private_data;
1232 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001233 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001234 unsigned int level = 0;
1235 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Renninger59d399d2005-11-08 05:27:00 -05001238 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001242 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 str[count] = 0;
1245 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001248 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Julius Volz98fb8fe2007-02-20 16:38:40 +01001250 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001251 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001253 if (!acpi_video_device_lcd_set_level(dev, level))
1254 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
1257
Zhang Rui24450c72009-03-18 16:27:10 +08001258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Len Brown4be44fc2005-08-05 00:44:28 -04001261static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001263 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001264 int status;
1265 int i;
1266 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (!dev)
1270 goto out;
1271
Len Brown4be44fc2005-08-05 00:44:28 -04001272 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001274 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 if (ACPI_FAILURE(status)) {
1278 goto out;
1279 }
1280
1281 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1282 for (i = 0; i < edid->buffer.length; i++)
1283 seq_putc(seq, edid->buffer.pointer[i]);
1284 }
1285
Len Brown4be44fc2005-08-05 00:44:28 -04001286 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!edid)
1288 seq_printf(seq, "<not supported>\n");
1289 else
1290 kfree(edid);
1291
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295static int
Len Brown4be44fc2005-08-05 00:44:28 -04001296acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 return single_open(file, acpi_video_device_EDID_seq_show,
1299 PDE(inode)->data);
1300}
1301
Len Brown4be44fc2005-08-05 00:44:28 -04001302static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001304 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 struct acpi_video_device *vid_dev;
1306
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001307 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001309 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001311 device_dir = proc_mkdir(acpi_device_bid(device),
1312 vid_dev->video->dir);
1313 if (!device_dir)
1314 return -ENOMEM;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001317 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001318 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001320 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001323 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001324 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001325 &acpi_video_device_state_fops,
1326 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001328 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001331 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001332 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001333 &acpi_video_device_brightness_fops,
1334 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_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001339 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001340 &acpi_video_device_EDID_fops,
1341 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001343 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001345 acpi_device_dir(device) = device_dir;
1346
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001348
1349 err_remove_brightness:
1350 remove_proc_entry("brightness", device_dir);
1351 err_remove_state:
1352 remove_proc_entry("state", device_dir);
1353 err_remove_info:
1354 remove_proc_entry("info", device_dir);
1355 err_remove_dir:
1356 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1357 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Len Brown4be44fc2005-08-05 00:44:28 -04001360static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001363 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001365 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001367 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001369 device_dir = acpi_device_dir(device);
1370 if (device_dir) {
1371 remove_proc_entry("info", device_dir);
1372 remove_proc_entry("state", device_dir);
1373 remove_proc_entry("brightness", device_dir);
1374 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001375 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 acpi_device_dir(device) = NULL;
1377 }
1378
Patrick Mocheld550d982006-06-27 00:41:40 -04001379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001383static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001385 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 if (!video)
1389 goto end;
1390
1391 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001392 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001394 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001396 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Len Brown4be44fc2005-08-05 00:44:28 -04001398 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Len Brown4be44fc2005-08-05 00:44:28 -04001402static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Len Brown4be44fc2005-08-05 00:44:28 -04001404 return single_open(file, acpi_video_bus_info_seq_show,
1405 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Len Brown4be44fc2005-08-05 00:44:28 -04001408static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001410 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (!video)
1414 goto end;
1415
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001416 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 seq_printf(seq, "<TODO>\n");
1418
Len Brown4be44fc2005-08-05 00:44:28 -04001419 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Len Brown4be44fc2005-08-05 00:44:28 -04001423static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1426}
1427
Len Brown4be44fc2005-08-05 00:44:28 -04001428static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001430 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001431 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001432 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (!video)
1436 goto end;
1437
1438 status = acpi_video_bus_POST_options(video, &options);
1439 if (ACPI_SUCCESS(status)) {
1440 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001441 printk(KERN_WARNING PREFIX
1442 "The motherboard VGA device is not listed as a possible POST device.\n");
1443 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001444 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Frank Seidel4d939152009-02-04 17:03:07 +01001446 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001447 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (options & 2)
1449 seq_printf(seq, " <PCI video>");
1450 if (options & 4)
1451 seq_printf(seq, " <AGP video>");
1452 seq_putc(seq, '\n');
1453 } else
1454 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001455 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459static int
Len Brown4be44fc2005-08-05 00:44:28 -04001460acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Len Brown4be44fc2005-08-05 00:44:28 -04001462 return single_open(file, acpi_video_bus_POST_info_seq_show,
1463 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Len Brown4be44fc2005-08-05 00:44:28 -04001466static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001468 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001469 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001470 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (!video)
1474 goto end;
1475
Len Brown4be44fc2005-08-05 00:44:28 -04001476 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!ACPI_SUCCESS(status)) {
1478 seq_printf(seq, "<not supported>\n");
1479 goto end;
1480 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001481 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Len Brown4be44fc2005-08-05 00:44:28 -04001483 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
Len Brown4be44fc2005-08-05 00:44:28 -04001487static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001489 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Len Brown4be44fc2005-08-05 00:44:28 -04001492 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Patrick Mocheld550d982006-06-27 00:41:40 -04001494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Len Brown4be44fc2005-08-05 00:44:28 -04001497static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Len Brown4be44fc2005-08-05 00:44:28 -04001499 return single_open(file, acpi_video_bus_POST_seq_show,
1500 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Len Brown4be44fc2005-08-05 00:44:28 -04001503static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1506}
1507
1508static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001509acpi_video_bus_write_POST(struct file *file,
1510 const char __user * buffer,
1511 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Len Brown4be44fc2005-08-05 00:44:28 -04001513 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001514 struct seq_file *m = file->private_data;
1515 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001516 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001517 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 status = acpi_video_bus_POST_options(video, &options);
1524 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001525 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001528 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 str[count] = 0;
1531 opt = strtoul(str, NULL, 0);
1532 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001533 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Julius Volz98fb8fe2007-02-20 16:38:40 +01001535 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 options |= 1;
1537
1538 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001539 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001541 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 }
1544
Patrick Mocheld550d982006-06-27 00:41:40 -04001545 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001549acpi_video_bus_write_DOS(struct file *file,
1550 const char __user * buffer,
1551 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Len Brown4be44fc2005-08-05 00:44:28 -04001553 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001554 struct seq_file *m = file->private_data;
1555 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001556 char str[12] = { 0 };
1557 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001564 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 str[count] = 0;
1567 opt = strtoul(str, NULL, 0);
1568 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001569 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Len Brown4be44fc2005-08-05 00:44:28 -04001571 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001574 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Patrick Mocheld550d982006-06-27 00:41:40 -04001576 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Len Brown4be44fc2005-08-05 00:44:28 -04001579static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001581 struct acpi_video_bus *video = acpi_driver_data(device);
1582 struct proc_dir_entry *device_dir;
1583 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001585 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1586 if (!device_dir)
1587 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001590 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001591 &acpi_video_bus_info_fops,
1592 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001594 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001597 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001598 &acpi_video_bus_ROM_fops,
1599 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001601 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001604 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001605 &acpi_video_bus_POST_info_fops,
1606 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001608 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /* 'POST' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001611 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001612 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001613 &acpi_video_bus_POST_fops,
1614 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001616 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 /* 'DOS' [R/W] */
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001619 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001620 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001621 &acpi_video_bus_DOS_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_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001626 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001627 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001628
1629 err_remove_post:
1630 remove_proc_entry("POST", device_dir);
1631 err_remove_post_info:
1632 remove_proc_entry("POST_info", device_dir);
1633 err_remove_rom:
1634 remove_proc_entry("ROM", device_dir);
1635 err_remove_info:
1636 remove_proc_entry("info", device_dir);
1637 err_remove_dir:
1638 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1639 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Len Brown4be44fc2005-08-05 00:44:28 -04001642static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001644 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001646 if (device_dir) {
1647 remove_proc_entry("info", device_dir);
1648 remove_proc_entry("ROM", device_dir);
1649 remove_proc_entry("POST_info", device_dir);
1650 remove_proc_entry("POST", device_dir);
1651 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001652 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 acpi_device_dir(device) = NULL;
1654 }
1655
Patrick Mocheld550d982006-06-27 00:41:40 -04001656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/* --------------------------------------------------------------------------
1660 Driver Interface
1661 -------------------------------------------------------------------------- */
1662
1663/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001664static struct acpi_video_device_attrib*
1665acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1666{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001667 struct acpi_video_enumerated_device *ids;
1668 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001669
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001670 for (i = 0; i < video->attached_count; i++) {
1671 ids = &video->attached_array[i];
1672 if ((ids->value.int_val & 0xffff) == device_id)
1673 return &ids->value.attrib;
1674 }
1675
Rui Zhang82cae992007-01-03 23:40:53 -05001676 return NULL;
1677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679static int
Len Brown4be44fc2005-08-05 00:44:28 -04001680acpi_video_bus_get_one_device(struct acpi_device *device,
1681 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001683 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001684 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001685 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001686 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Len Brown4be44fc2005-08-05 00:44:28 -04001691 status =
1692 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (ACPI_SUCCESS(status)) {
1694
Burman Yan36bcbec2006-12-19 12:56:11 -08001695 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1700 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001701 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 data->device_id = device_id;
1704 data->video = video;
1705 data->dev = device;
1706
Rui Zhang82cae992007-01-03 23:40:53 -05001707 attribute = acpi_video_get_device_attr(video, device_id);
1708
1709 if((attribute != NULL) && attribute->device_id_scheme) {
1710 switch (attribute->display_type) {
1711 case ACPI_VIDEO_DISPLAY_CRT:
1712 data->flags.crt = 1;
1713 break;
1714 case ACPI_VIDEO_DISPLAY_TV:
1715 data->flags.tvout = 1;
1716 break;
1717 case ACPI_VIDEO_DISPLAY_DVI:
1718 data->flags.dvi = 1;
1719 break;
1720 case ACPI_VIDEO_DISPLAY_LCD:
1721 data->flags.lcd = 1;
1722 break;
1723 default:
1724 data->flags.unknown = 1;
1725 break;
1726 }
1727 if(attribute->bios_can_detect)
1728 data->flags.bios = 1;
1729 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 acpi_video_device_bind(video, data);
1733 acpi_video_device_find_cap(data);
1734
Patrick Mochel90130262006-05-19 16:54:48 -04001735 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001736 ACPI_DEVICE_NOTIFY,
1737 acpi_video_device_notify,
1738 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001740 printk(KERN_ERR PREFIX
1741 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001742 if(data->brightness)
1743 kfree(data->brightness->levels);
1744 kfree(data->brightness);
1745 kfree(data);
1746 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001749 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001751 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 acpi_video_device_add_fs(device);
1754
Patrick Mocheld550d982006-06-27 00:41:40 -04001755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757
Patrick Mocheld550d982006-06-27 00:41:40 -04001758 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/*
1762 * Arg:
1763 * video : video bus device
1764 *
1765 * Return:
1766 * none
1767 *
1768 * Enumerate the video device list of the video bus,
1769 * bind the ids with the corresponding video devices
1770 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Len Brown4be44fc2005-08-05 00:44:28 -04001773static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001775 struct acpi_video_device *dev;
1776
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001777 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001778
1779 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001780 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001781
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001782 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785/*
1786 * Arg:
1787 * video : video bus device
1788 * device : video output device under the video
1789 * bus
1790 *
1791 * Return:
1792 * none
1793 *
1794 * Bind the ids with the corresponding video devices
1795 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798static void
Len Brown4be44fc2005-08-05 00:44:28 -04001799acpi_video_device_bind(struct acpi_video_bus *video,
1800 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001802 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001803 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001805 for (i = 0; i < video->attached_count; i++) {
1806 ids = &video->attached_array[i];
1807 if (device->device_id == (ids->value.int_val & 0xffff)) {
1808 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1810 }
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/*
1815 * Arg:
1816 * video : video bus device
1817 *
1818 * Return:
1819 * < 0 : error
1820 *
1821 * Call _DOD to enumerate all devices attached to display adapter
1822 *
Len Brown4be44fc2005-08-05 00:44:28 -04001823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1826{
Len Brown4be44fc2005-08-05 00:44:28 -04001827 int status;
1828 int count;
1829 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001830 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001831 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1832 union acpi_object *dod = NULL;
1833 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Patrick Mochel90130262006-05-19 16:54:48 -04001835 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001837 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001838 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001841 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001843 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 status = -EFAULT;
1845 goto out;
1846 }
1847
1848 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001849 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001851 active_list = kcalloc(1 + dod->package.count,
1852 sizeof(struct acpi_video_enumerated_device),
1853 GFP_KERNEL);
1854 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 status = -ENOMEM;
1856 goto out;
1857 }
1858
1859 count = 0;
1860 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001861 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001864 printk(KERN_ERR PREFIX
1865 "Invalid _DOD data in element %d\n", i);
1866 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001868
1869 active_list[count].value.int_val = obj->integer.value;
1870 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001871 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1872 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 count++;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jesper Juhl6044ec82005-11-07 01:01:32 -08001876 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001877
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001878 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001880
1881 out:
Len Brown02438d82006-06-30 03:19:10 -04001882 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001883 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Len Brown4be44fc2005-08-05 00:44:28 -04001886static int
1887acpi_video_get_next_level(struct acpi_video_device *device,
1888 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001890 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001891 max = max_below = 0;
1892 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001893 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001894 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001895 l = device->brightness->levels[i];
1896 if (abs(l - level_current) < abs(delta)) {
1897 delta = l - level_current;
1898 if (!delta)
1899 break;
1900 }
1901 }
1902 /* Ajust level_current to closest available level */
1903 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001904 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001905 l = device->brightness->levels[i];
1906 if (l < min)
1907 min = l;
1908 if (l > max)
1909 max = l;
1910 if (l < min_above && l > level_current)
1911 min_above = l;
1912 if (l > max_below && l < level_current)
1913 max_below = l;
1914 }
1915
1916 switch (event) {
1917 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1918 return (level_current < max) ? min_above : min;
1919 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1920 return (level_current < max) ? min_above : max;
1921 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1922 return (level_current > min) ? max_below : min;
1923 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1924 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1925 return 0;
1926 default:
1927 return level_current;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Zhang Ruic8890f92009-03-18 16:27:08 +08001931static int
Len Brown4be44fc2005-08-05 00:44:28 -04001932acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001934 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001935 int result = -EINVAL;
1936
Julia Jomantaite469778c2008-06-23 22:50:42 +01001937 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001938 goto out;
1939
1940 result = acpi_video_device_lcd_get_level_current(device,
1941 &level_current);
1942 if (result)
1943 goto out;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001946
Zhang Rui24450c72009-03-18 16:27:10 +08001947 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001948
1949out:
1950 if (result)
1951 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1952
1953 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
1956static int
Len Brown4be44fc2005-08-05 00:44:28 -04001957acpi_video_bus_get_devices(struct acpi_video_bus *video,
1958 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Len Brown4be44fc2005-08-05 00:44:28 -04001960 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001961 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 acpi_video_device_enumerate(video);
1964
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001965 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 status = acpi_video_bus_get_one_device(dev, video);
1968 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001969 printk(KERN_WARNING PREFIX
1970 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 continue;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001974 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Len Brown4be44fc2005-08-05 00:44:28 -04001977static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Karol Kozimor031ec772005-07-30 04:18:00 -04001979 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct acpi_video_bus *video;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001984 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 video = device->video;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 acpi_video_device_remove_fs(device->dev);
1989
Patrick Mochel90130262006-05-19 16:54:48 -04001990 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001991 ACPI_DEVICE_NOTIFY,
1992 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001993 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001994 if (device->cdev) {
1995 sysfs_remove_link(&device->dev->dev.kobj,
1996 "thermal_cooling");
1997 sysfs_remove_link(&device->cdev->device.kobj,
1998 "device");
1999 thermal_cooling_device_unregister(device->cdev);
2000 device->cdev = NULL;
2001 }
Luming Yu23b0f012007-05-09 21:07:05 +08002002 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002003
Patrick Mocheld550d982006-06-27 00:41:40 -04002004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Len Brown4be44fc2005-08-05 00:44:28 -04002007static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Len Brown4be44fc2005-08-05 00:44:28 -04002009 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002010 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002012 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002014 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002016 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002017 if (ACPI_FAILURE(status))
2018 printk(KERN_WARNING PREFIX
2019 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002021 if (dev->brightness) {
2022 kfree(dev->brightness->levels);
2023 kfree(dev->brightness);
2024 }
2025 list_del(&dev->entry);
2026 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002029 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002030
Patrick Mocheld550d982006-06-27 00:41:40 -04002031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/* acpi_video interface */
2035
Len Brown4be44fc2005-08-05 00:44:28 -04002036static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Zhang Ruia21101c2007-09-14 11:46:22 +08002038 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Len Brown4be44fc2005-08-05 00:44:28 -04002041static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 return acpi_video_bus_DOS(video, 0, 1);
2044}
2045
Bjorn Helgaas70155582009-04-07 15:37:11 +00002046static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002048 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002049 struct input_dev *input;
2050 int keycode;
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Luming Yue9dab192007-08-20 18:23:53 +08002055 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002058 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04002060 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002061 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 break;
2063
Julius Volz98fb8fe2007-02-20 16:38:40 +01002064 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * connector. */
2066 acpi_video_device_enumerate(video);
2067 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002068 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002069 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 break;
2071
Len Brown4be44fc2005-08-05 00:44:28 -04002072 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002073 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002074 keycode = KEY_SWITCHVIDEOMODE;
2075 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002076 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002077 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002078 keycode = KEY_VIDEO_NEXT;
2079 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002080 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002081 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002082 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
2084
2085 default:
Luming Yue9dab192007-08-20 18:23:53 +08002086 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002088 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 break;
2090 }
2091
Zhang Rui7761f632008-01-25 14:48:12 +08002092 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002093 input_report_key(input, keycode, 1);
2094 input_sync(input);
2095 input_report_key(input, keycode, 0);
2096 input_sync(input);
2097
Patrick Mocheld550d982006-06-27 00:41:40 -04002098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Len Brown4be44fc2005-08-05 00:44:28 -04002101static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002103 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002104 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002105 struct acpi_video_bus *bus;
2106 struct input_dev *input;
2107 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002112 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002113 bus = video_device->video;
2114 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002117 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002118 if (brightness_switch_enabled)
2119 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002120 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002121 keycode = KEY_BRIGHTNESS_CYCLE;
2122 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002123 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002124 if (brightness_switch_enabled)
2125 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002126 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002127 keycode = KEY_BRIGHTNESSUP;
2128 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002129 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002130 if (brightness_switch_enabled)
2131 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002132 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002133 keycode = KEY_BRIGHTNESSDOWN;
2134 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002135 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002136 if (brightness_switch_enabled)
2137 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002138 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002139 keycode = KEY_BRIGHTNESS_ZERO;
2140 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002141 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002142 if (brightness_switch_enabled)
2143 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002144 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002145 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 break;
2147 default:
Luming Yue9dab192007-08-20 18:23:53 +08002148 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002150 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 break;
2152 }
Luming Yue9dab192007-08-20 18:23:53 +08002153
Zhang Rui7761f632008-01-25 14:48:12 +08002154 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002155 input_report_key(input, keycode, 1);
2156 input_sync(input);
2157 input_report_key(input, keycode, 0);
2158 input_sync(input);
2159
Patrick Mocheld550d982006-06-27 00:41:40 -04002160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Zhang Ruie6d9da12007-08-25 02:23:31 -04002163static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002164static int acpi_video_resume(struct acpi_device *device)
2165{
2166 struct acpi_video_bus *video;
2167 struct acpi_video_device *video_device;
2168 int i;
2169
2170 if (!device || !acpi_driver_data(device))
2171 return -EINVAL;
2172
2173 video = acpi_driver_data(device);
2174
2175 for (i = 0; i < video->attached_count; i++) {
2176 video_device = video->attached_array[i].bind_info;
2177 if (video_device && video_device->backlight)
2178 acpi_video_set_brightness(video_device->backlight);
2179 }
2180 return AE_OK;
2181}
2182
Len Brown4be44fc2005-08-05 00:44:28 -04002183static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002185 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002186 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002187 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Burman Yan36bcbec2006-12-19 12:56:11 -08002189 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Zhang Ruie6d9da12007-08-25 02:23:31 -04002193 /* a hack to fix the duplicate name "VID" problem on T61 */
2194 if (!strcmp(device->pnp.bus_id, "VID")) {
2195 if (instance)
2196 device->pnp.bus_id[3] = '0' + instance;
2197 instance ++;
2198 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002199 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2200 if (!strcmp(device->pnp.bus_id, "VGA")) {
2201 if (instance)
2202 device->pnp.bus_id[3] = '0' + instance;
2203 instance++;
2204 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002205
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002206 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2208 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002209 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002212 error = acpi_video_bus_check(video);
2213 if (error)
2214 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002216 error = acpi_video_bus_add_fs(device);
2217 if (error)
2218 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002220 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 INIT_LIST_HEAD(&video->video_device_list);
2222
2223 acpi_video_bus_get_devices(video, device);
2224 acpi_video_bus_start_devices(video);
2225
Luming Yue9dab192007-08-20 18:23:53 +08002226 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002227 if (!input) {
2228 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002229 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002230 }
Luming Yue9dab192007-08-20 18:23:53 +08002231
2232 snprintf(video->phys, sizeof(video->phys),
2233 "%s/video/input0", acpi_device_hid(video->device));
2234
2235 input->name = acpi_device_name(video->device);
2236 input->phys = video->phys;
2237 input->id.bustype = BUS_HOST;
2238 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002239 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002240 input->evbit[0] = BIT(EV_KEY);
2241 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2242 set_bit(KEY_VIDEO_NEXT, input->keybit);
2243 set_bit(KEY_VIDEO_PREV, input->keybit);
2244 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2245 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2246 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2247 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2248 set_bit(KEY_DISPLAY_OFF, input->keybit);
2249 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002250
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002251 error = input_register_device(input);
2252 if (error)
2253 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002256 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2257 video->flags.multihead ? "yes" : "no",
2258 video->flags.rom ? "yes" : "no",
2259 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002263 err_free_input_dev:
2264 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002265 err_stop_video:
2266 acpi_video_bus_stop_devices(video);
2267 acpi_video_bus_put_devices(video);
2268 kfree(video->attached_array);
2269 acpi_video_bus_remove_fs(device);
2270 err_free_video:
2271 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002272 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002273
2274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Len Brown4be44fc2005-08-05 00:44:28 -04002277static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Len Brown4be44fc2005-08-05 00:44:28 -04002279 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002283 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002285 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 acpi_video_bus_put_devices(video);
2289 acpi_video_bus_remove_fs(device);
2290
Luming Yue9dab192007-08-20 18:23:53 +08002291 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002292 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 kfree(video);
2294
Patrick Mocheld550d982006-06-27 00:41:40 -04002295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Matthew Garrett74a365b2009-03-19 21:35:39 +00002298static int __init intel_opregion_present(void)
2299{
2300#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2301 struct pci_dev *dev = NULL;
2302 u32 address;
2303
2304 for_each_pci_dev(dev) {
2305 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2306 continue;
2307 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2308 continue;
2309 pci_read_config_dword(dev, 0xfc, &address);
2310 if (!address)
2311 continue;
2312 return 1;
2313 }
2314#endif
2315 return 0;
2316}
2317
2318int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Len Brown4be44fc2005-08-05 00:44:28 -04002320 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2323 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002324 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 result = acpi_bus_register_driver(&acpi_video_bus);
2327 if (result < 0) {
2328 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
Patrick Mocheld550d982006-06-27 00:41:40 -04002332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002334EXPORT_SYMBOL(acpi_video_register);
2335
2336/*
2337 * This is kind of nasty. Hardware using Intel chipsets may require
2338 * the video opregion code to be run first in order to initialise
2339 * state before any ACPI video calls are made. To handle this we defer
2340 * registration of the video class until the opregion code has run.
2341 */
2342
2343static int __init acpi_video_init(void)
2344{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002345 dmi_check_system(video_dmi_table);
2346
Matthew Garrett74a365b2009-03-19 21:35:39 +00002347 if (intel_opregion_present())
2348 return 0;
2349
2350 return acpi_video_register();
2351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Matthew Garrett3b1c1c12009-04-01 19:52:29 +01002353void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 acpi_bus_unregister_driver(&acpi_video_bus);
2357
2358 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2359
Patrick Mocheld550d982006-06-27 00:41:40 -04002360 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
Matthew Garrett3b1c1c12009-04-01 19:52:29 +01002362EXPORT_SYMBOL(acpi_video_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364module_init(acpi_video_init);
2365module_exit(acpi_video_exit);