blob: ee45e7604221dc78197c861b0669512861654aed [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 Brownc2b67052007-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 Rui45cb50e2009-04-24 12:13:18 -0400581 {}
582};
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static int
Len Brown4be44fc2005-08-05 00:44:28 -0400585acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400586 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Zhang Ruic8890f92009-03-18 16:27:08 +0800588 acpi_status status = AE_OK;
589
Zhang Ruic60d6382009-03-18 16:27:18 +0800590 if (device->cap._BQC || device->cap._BCQ) {
591 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
592
593 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800594 NULL, level);
595 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800596 if (device->brightness->flags._BQC_use_index) {
597 if (device->brightness->flags._BCL_reversed)
598 *level = device->brightness->count
599 - 3 - (*level);
600 *level = device->brightness->levels[*level + 2];
601
602 }
Zhang Rui45cb50e2009-04-24 12:13:18 -0400603 *level += bqc_offset_aml_bug_workaround;
Zhang Ruic8890f92009-03-18 16:27:08 +0800604 device->brightness->curr = *level;
605 return 0;
606 } else {
607 /* Fixme:
608 * should we return an error or ignore this failure?
609 * dev->brightness->curr is a cached value which stores
610 * the correct current backlight level in most cases.
611 * ACPI video backlight still works w/ buggy _BQC.
612 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
613 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800614 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
615 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800616 }
617 }
618
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400619 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static int
Len Brown4be44fc2005-08-05 00:44:28 -0400624acpi_video_device_EDID(struct acpi_video_device *device,
625 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Len Brown4be44fc2005-08-05 00:44:28 -0400627 int status;
628 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
629 union acpi_object *obj;
630 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
631 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 *edid = NULL;
635
636 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400637 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (length == 128)
639 arg0.integer.value = 1;
640 else if (length == 256)
641 arg0.integer.value = 2;
642 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400643 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Patrick Mochel90130262006-05-19 16:54:48 -0400645 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400647 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200649 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (obj && obj->type == ACPI_TYPE_BUFFER)
652 *edid = obj;
653 else {
Len Brown64684632006-06-26 23:41:38 -0400654 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 status = -EFAULT;
656 kfree(obj);
657 }
658
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/* bus */
663
664static int
Len Brown4be44fc2005-08-05 00:44:28 -0400665acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Len Brown4be44fc2005-08-05 00:44:28 -0400667 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400668 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400669 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
670 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 arg0.integer.value = option;
674
Patrick Mochel90130262006-05-19 16:54:48 -0400675 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400677 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Patrick Mocheld550d982006-06-27 00:41:40 -0400679 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400683acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 int status;
686
Patrick Mochel90130262006-05-19 16:54:48 -0400687 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Patrick Mocheld550d982006-06-27 00:41:40 -0400689 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692static int
Len Brown4be44fc2005-08-05 00:44:28 -0400693acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400694 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Len Brown4be44fc2005-08-05 00:44:28 -0400696 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Patrick Mochel90130262006-05-19 16:54:48 -0400698 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *options &= 3;
700
Patrick Mocheld550d982006-06-27 00:41:40 -0400701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/*
705 * Arg:
706 * video : video bus device pointer
707 * bios_flag :
708 * 0. The system BIOS should NOT automatically switch(toggle)
709 * the active display output.
710 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100711 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * 2. The _DGS value should be locked.
713 * 3. The system BIOS should not automatically switch (toggle) the
714 * active display output, but instead generate the display switch
715 * event notify code.
716 * lcd_flag :
717 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100718 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100720 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Return Value:
722 * -1 wrong arg.
723 */
724
725static int
Len Brown4be44fc2005-08-05 00:44:28 -0400726acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Len Brown4be44fc2005-08-05 00:44:28 -0400728 acpi_integer status = 0;
729 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
730 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Len Brown4be44fc2005-08-05 00:44:28 -0400733 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 status = -1;
735 goto Failed;
736 }
737 arg0.integer.value = (lcd_flag << 2) | bios_flag;
738 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400739 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Len Brown4be44fc2005-08-05 00:44:28 -0400741 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400742 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500746 * Simple comparison function used to sort backlight levels.
747 */
748
749static int
750acpi_video_cmp_level(const void *a, const void *b)
751{
752 return *(int *)a - *(int *)b;
753}
754
755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * Arg:
757 * device : video output device (LCD, CRT, ..)
758 *
759 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100760 * Maximum brightness level
761 *
762 * Allocate and initialize device->brightness.
763 */
764
765static int
766acpi_video_init_brightness(struct acpi_video_device *device)
767{
768 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800769 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800770 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100771 union acpi_object *o;
772 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800773 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100774
775 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
776 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
777 "LCD brightness level\n"));
778 goto out;
779 }
780
781 if (obj->package.count < 2)
782 goto out;
783
784 br = kzalloc(sizeof(*br), GFP_KERNEL);
785 if (!br) {
786 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800787 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100788 goto out;
789 }
790
Zhang Ruid32f6942009-03-18 16:27:12 +0800791 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100792 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800793 if (!br->levels) {
794 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100795 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800796 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100797
798 for (i = 0; i < obj->package.count; i++) {
799 o = (union acpi_object *)&obj->package.elements[i];
800 if (o->type != ACPI_TYPE_INTEGER) {
801 printk(KERN_ERR PREFIX "Invalid data\n");
802 continue;
803 }
804 br->levels[count] = (u32) o->integer.value;
805
806 if (br->levels[count] > max_level)
807 max_level = br->levels[count];
808 count++;
809 }
810
Zhang Ruid32f6942009-03-18 16:27:12 +0800811 /*
812 * some buggy BIOS don't export the levels
813 * when machine is on AC/Battery in _BCL package.
814 * In this case, the first two elements in _BCL packages
815 * are also supported brightness levels that OS should take care of.
816 */
Zhang Rui90af2cf2009-04-14 11:02:18 +0800817 for (i = 2; i < count; i++) {
818 if (br->levels[i] == br->levels[0])
Zhang Ruid32f6942009-03-18 16:27:12 +0800819 level_ac_battery++;
Zhang Rui90af2cf2009-04-14 11:02:18 +0800820 if (br->levels[i] == br->levels[1])
821 level_ac_battery++;
822 }
Zhang Rui935e5f22008-12-11 16:24:52 -0500823
Zhang Ruid32f6942009-03-18 16:27:12 +0800824 if (level_ac_battery < 2) {
825 level_ac_battery = 2 - level_ac_battery;
826 br->flags._BCL_no_ac_battery_levels = 1;
827 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
828 br->levels[i] = br->levels[i - level_ac_battery];
829 count += level_ac_battery;
830 } else if (level_ac_battery > 2)
831 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
832
Zhang Ruid80fb992009-03-18 16:27:14 +0800833 /* Check if the _BCL package is in a reversed order */
834 if (max_level == br->levels[2]) {
835 br->flags._BCL_reversed = 1;
836 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
837 acpi_video_cmp_level, NULL);
838 } else if (max_level != br->levels[count - 1])
839 ACPI_ERROR((AE_INFO,
840 "Found unordered _BCL package\n"));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100841
842 br->count = count;
843 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800844
845 /* Check the input/output of _BQC/_BCL/_BCM */
846 if ((max_level < 100) && (max_level <= (count - 2)))
847 br->flags._BCL_use_index = 1;
848
849 /*
850 * _BCM is always consistent with _BCL,
851 * at least for all the laptops we have ever seen.
852 */
853 br->flags._BCM_use_index = br->flags._BCL_use_index;
854
855 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
Zhang Ruie047cca2009-04-09 14:24:35 +0800856 br->curr = level_old = max_level;
857
858 if (!device->cap._BQC)
859 goto set_level;
860
Zhang Rui1a7c6182009-03-18 16:27:16 +0800861 result = acpi_video_device_lcd_get_level_current(device, &level_old);
862 if (result)
863 goto out_free_levels;
864
Zhang Ruie047cca2009-04-09 14:24:35 +0800865 /*
866 * Set the level to maximum and check if _BQC uses indexed value
867 */
868 result = acpi_video_device_lcd_set_level(device, max_level);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800869 if (result)
870 goto out_free_levels;
871
872 result = acpi_video_device_lcd_get_level_current(device, &level);
873 if (result)
874 goto out_free_levels;
875
Zhang Ruie047cca2009-04-09 14:24:35 +0800876 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800877
Zhang Ruie047cca2009-04-09 14:24:35 +0800878 if (!br->flags._BQC_use_index)
879 goto set_level;
880
881 if (br->flags._BCL_reversed)
882 level_old = (br->count - 1) - level_old;
883 level_old = br->levels[level_old];
884
885set_level:
886 result = acpi_video_device_lcd_set_level(device, level_old);
887 if (result)
888 goto out_free_levels;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800889
Zhang Ruid32f6942009-03-18 16:27:12 +0800890 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
891 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100892 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800893 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100894
895out_free_levels:
896 kfree(br->levels);
897out_free:
898 kfree(br);
899out:
900 device->brightness = NULL;
901 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800902 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100903}
904
905/*
906 * Arg:
907 * device : video output device (LCD, CRT, ..)
908 *
909 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * None
911 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100912 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * device.
914 */
915
Len Brown4be44fc2005-08-05 00:44:28 -0400916static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
William Lee Irwin III98934de2007-12-12 03:56:55 -0800921 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Patrick Mochel90130262006-05-19 16:54:48 -0400923 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 device->cap._ADR = 1;
925 }
Patrick Mochel90130262006-05-19 16:54:48 -0400926 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400927 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Patrick Mochel90130262006-05-19 16:54:48 -0400929 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400930 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800932 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
933 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800934 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
935 &h_dummy1))) {
936 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
937 device->cap._BCQ = 1;
938 }
939
Patrick Mochel90130262006-05-19 16:54:48 -0400940 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400941 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Patrick Mochel90130262006-05-19 16:54:48 -0400943 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 device->cap._DCS = 1;
945 }
Patrick Mochel90130262006-05-19 16:54:48 -0400946 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 device->cap._DGS = 1;
948 }
Patrick Mochel90130262006-05-19 16:54:48 -0400949 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 device->cap._DSS = 1;
951 }
952
Zhang Rui1a7c6182009-03-18 16:27:16 +0800953 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800954 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800955 static int count = 0;
956 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800957
958 result = acpi_video_init_brightness(device);
959 if (result)
960 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800961 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
962 if (!name)
963 return;
964
Yu Luming2f3d0002006-11-11 02:40:34 +0800965 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800966 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000967 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000968 device->backlight->props.max_brightness = device->brightness->count-3;
Yu Luming2f3d0002006-11-11 02:40:34 +0800969 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800970
971 device->cdev = thermal_cooling_device_register("LCD",
972 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500973 if (IS_ERR(device->cdev))
974 return;
975
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200976 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
977 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800978 result = sysfs_create_link(&device->dev->dev.kobj,
979 &device->cdev->device.kobj,
980 "thermal_cooling");
981 if (result)
982 printk(KERN_ERR PREFIX "Create sysfs link\n");
983 result = sysfs_create_link(&device->cdev->device.kobj,
984 &device->dev->dev.kobj, "device");
985 if (result)
986 printk(KERN_ERR PREFIX "Create sysfs link\n");
987
Yu Luming2f3d0002006-11-11 02:40:34 +0800988 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200989
990 if (acpi_video_display_switch_support()) {
991
992 if (device->cap._DCS && device->cap._DSS) {
993 static int count;
994 char *name;
995 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
996 if (!name)
997 return;
998 sprintf(name, "acpi_video%d", count++);
999 device->output_dev = video_output_register(name,
1000 NULL, device, &acpi_output_properties);
1001 kfree(name);
1002 }
Luming Yu23b0f012007-05-09 21:07:05 +08001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/*
1007 * Arg:
1008 * device : video output device (VGA)
1009 *
1010 * Return Value:
1011 * None
1012 *
Julius Volz98fb8fe2007-02-20 16:38:40 +01001013 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
1015
Len Brown4be44fc2005-08-05 00:44:28 -04001016static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Len Brown4be44fc2005-08-05 00:44:28 -04001018 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
William Lee Irwin III98934de2007-12-12 03:56:55 -08001020 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -04001021 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 video->cap._DOS = 1;
1023 }
Patrick Mochel90130262006-05-19 16:54:48 -04001024 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 video->cap._DOD = 1;
1026 }
Patrick Mochel90130262006-05-19 16:54:48 -04001027 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 video->cap._ROM = 1;
1029 }
Patrick Mochel90130262006-05-19 16:54:48 -04001030 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 video->cap._GPD = 1;
1032 }
Patrick Mochel90130262006-05-19 16:54:48 -04001033 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 video->cap._SPD = 1;
1035 }
Patrick Mochel90130262006-05-19 16:54:48 -04001036 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 video->cap._VPO = 1;
1038 }
1039}
1040
1041/*
1042 * Check whether the video bus device has required AML method to
1043 * support the desired features
1044 */
1045
Len Brown4be44fc2005-08-05 00:44:28 -04001046static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Len Brown4be44fc2005-08-05 00:44:28 -04001048 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +02001049 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001052 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Thomas Renninger22c13f92008-08-01 17:37:54 +02001054 dev = acpi_get_physical_pci_device(video->device->handle);
1055 if (!dev)
1056 return -ENODEV;
1057 put_device(dev);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* Since there is no HID, CID and so on for VGA driver, we have
1060 * to check well known required nodes.
1061 */
1062
Julius Volz98fb8fe2007-02-20 16:38:40 +01001063 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -04001064 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 video->flags.multihead = 1;
1066 status = 0;
1067 }
1068
Julius Volz98fb8fe2007-02-20 16:38:40 +01001069 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001070 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 video->flags.rom = 1;
1072 status = 0;
1073 }
1074
Julius Volz98fb8fe2007-02-20 16:38:40 +01001075 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001076 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 video->flags.post = 1;
1078 status = 0;
1079 }
1080
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084/* --------------------------------------------------------------------------
1085 FS Interface (/proc)
1086 -------------------------------------------------------------------------- */
1087
Len Brown4be44fc2005-08-05 00:44:28 -04001088static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090/* video devices */
1091
Len Brown4be44fc2005-08-05 00:44:28 -04001092static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001094 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (!dev)
1098 goto end;
1099
1100 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1101 seq_printf(seq, "type: ");
1102 if (dev->flags.crt)
1103 seq_printf(seq, "CRT\n");
1104 else if (dev->flags.lcd)
1105 seq_printf(seq, "LCD\n");
1106 else if (dev->flags.tvout)
1107 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001108 else if (dev->flags.dvi)
1109 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 else
1111 seq_printf(seq, "UNKNOWN\n");
1112
Len Brown4be44fc2005-08-05 00:44:28 -04001113 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Len Brown4be44fc2005-08-05 00:44:28 -04001115 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119static int
Len Brown4be44fc2005-08-05 00:44:28 -04001120acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 return single_open(file, acpi_video_device_info_seq_show,
1123 PDE(inode)->data);
1124}
1125
Len Brown4be44fc2005-08-05 00:44:28 -04001126static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Len Brown4be44fc2005-08-05 00:44:28 -04001128 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001129 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001130 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 if (!dev)
1134 goto end;
1135
1136 status = acpi_video_device_get_state(dev, &state);
1137 seq_printf(seq, "state: ");
1138 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001139 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 else
1141 seq_printf(seq, "<not supported>\n");
1142
1143 status = acpi_video_device_query(dev, &state);
1144 seq_printf(seq, "query: ");
1145 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001146 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 else
1148 seq_printf(seq, "<not supported>\n");
1149
Len Brown4be44fc2005-08-05 00:44:28 -04001150 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static int
Len Brown4be44fc2005-08-05 00:44:28 -04001155acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 return single_open(file, acpi_video_device_state_seq_show,
1158 PDE(inode)->data);
1159}
1160
1161static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001162acpi_video_device_write_state(struct file *file,
1163 const char __user * buffer,
1164 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Len Brown4be44fc2005-08-05 00:44:28 -04001166 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001167 struct seq_file *m = file->private_data;
1168 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001169 char str[12] = { 0 };
1170 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001177 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 str[count] = 0;
1180 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001181 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 status = acpi_video_device_set_state(dev, state);
1184
1185 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001186 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Patrick Mocheld550d982006-06-27 00:41:40 -04001188 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static int
Len Brown4be44fc2005-08-05 00:44:28 -04001192acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001194 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001195 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (!dev || !dev->brightness) {
1199 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
1203 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001204 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 seq_printf(seq, " %d", dev->brightness->levels[i]);
1206 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1207
Patrick Mocheld550d982006-06-27 00:41:40 -04001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static int
Len Brown4be44fc2005-08-05 00:44:28 -04001212acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 return single_open(file, acpi_video_device_brightness_seq_show,
1215 PDE(inode)->data);
1216}
1217
1218static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001219acpi_video_device_write_brightness(struct file *file,
1220 const char __user * buffer,
1221 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001223 struct seq_file *m = file->private_data;
1224 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001225 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001226 unsigned int level = 0;
1227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Thomas Renninger59d399d2005-11-08 05:27:00 -05001230 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001234 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 str[count] = 0;
1237 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001240 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Julius Volz98fb8fe2007-02-20 16:38:40 +01001242 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001243 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001245 if (!acpi_video_device_lcd_set_level(dev, level))
1246 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 }
1249
Zhang Rui24450c72009-03-18 16:27:10 +08001250 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Len Brown4be44fc2005-08-05 00:44:28 -04001253static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001255 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001256 int status;
1257 int i;
1258 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (!dev)
1262 goto out;
1263
Len Brown4be44fc2005-08-05 00:44:28 -04001264 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001266 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 if (ACPI_FAILURE(status)) {
1270 goto out;
1271 }
1272
1273 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1274 for (i = 0; i < edid->buffer.length; i++)
1275 seq_putc(seq, edid->buffer.pointer[i]);
1276 }
1277
Len Brown4be44fc2005-08-05 00:44:28 -04001278 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!edid)
1280 seq_printf(seq, "<not supported>\n");
1281 else
1282 kfree(edid);
1283
Patrick Mocheld550d982006-06-27 00:41:40 -04001284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287static int
Len Brown4be44fc2005-08-05 00:44:28 -04001288acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 return single_open(file, acpi_video_device_EDID_seq_show,
1291 PDE(inode)->data);
1292}
1293
Len Brown4be44fc2005-08-05 00:44:28 -04001294static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001296 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct acpi_video_device *vid_dev;
1298
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001299 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001301 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001303 device_dir = proc_mkdir(acpi_device_bid(device),
1304 vid_dev->video->dir);
1305 if (!device_dir)
1306 return -ENOMEM;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001309 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001310 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001312 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001315 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001316 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001317 &acpi_video_device_state_fops,
1318 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_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001323 entry = proc_create_data("brightness", 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_brightness_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_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001331 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001332 &acpi_video_device_EDID_fops,
1333 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001335 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001337 acpi_device_dir(device) = device_dir;
1338
Patrick Mocheld550d982006-06-27 00:41:40 -04001339 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001340
1341 err_remove_brightness:
1342 remove_proc_entry("brightness", device_dir);
1343 err_remove_state:
1344 remove_proc_entry("state", device_dir);
1345 err_remove_info:
1346 remove_proc_entry("info", device_dir);
1347 err_remove_dir:
1348 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1349 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Len Brown4be44fc2005-08-05 00:44:28 -04001352static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001355 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001357 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001361 device_dir = acpi_device_dir(device);
1362 if (device_dir) {
1363 remove_proc_entry("info", device_dir);
1364 remove_proc_entry("state", device_dir);
1365 remove_proc_entry("brightness", device_dir);
1366 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001367 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 acpi_device_dir(device) = NULL;
1369 }
1370
Patrick Mocheld550d982006-06-27 00:41:40 -04001371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001375static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001377 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (!video)
1381 goto end;
1382
1383 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001384 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001386 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001388 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Len Brown4be44fc2005-08-05 00:44:28 -04001390 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Len Brown4be44fc2005-08-05 00:44:28 -04001394static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Len Brown4be44fc2005-08-05 00:44:28 -04001396 return single_open(file, acpi_video_bus_info_seq_show,
1397 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Len Brown4be44fc2005-08-05 00:44:28 -04001400static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001402 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (!video)
1406 goto end;
1407
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001408 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 seq_printf(seq, "<TODO>\n");
1410
Len Brown4be44fc2005-08-05 00:44:28 -04001411 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Len Brown4be44fc2005-08-05 00:44:28 -04001415static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1418}
1419
Len Brown4be44fc2005-08-05 00:44:28 -04001420static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001422 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001423 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001424 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (!video)
1428 goto end;
1429
1430 status = acpi_video_bus_POST_options(video, &options);
1431 if (ACPI_SUCCESS(status)) {
1432 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001433 printk(KERN_WARNING PREFIX
1434 "The motherboard VGA device is not listed as a possible POST device.\n");
1435 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001436 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
Frank Seidel4d939152009-02-04 17:03:07 +01001438 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001439 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (options & 2)
1441 seq_printf(seq, " <PCI video>");
1442 if (options & 4)
1443 seq_printf(seq, " <AGP video>");
1444 seq_putc(seq, '\n');
1445 } else
1446 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001447 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static int
Len Brown4be44fc2005-08-05 00:44:28 -04001452acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Len Brown4be44fc2005-08-05 00:44:28 -04001454 return single_open(file, acpi_video_bus_POST_info_seq_show,
1455 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Len Brown4be44fc2005-08-05 00:44:28 -04001458static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001460 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001461 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001462 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (!video)
1466 goto end;
1467
Len Brown4be44fc2005-08-05 00:44:28 -04001468 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (!ACPI_SUCCESS(status)) {
1470 seq_printf(seq, "<not supported>\n");
1471 goto end;
1472 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001473 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Len Brown4be44fc2005-08-05 00:44:28 -04001475 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Len Brown4be44fc2005-08-05 00:44:28 -04001479static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001481 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Len Brown4be44fc2005-08-05 00:44:28 -04001484 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Patrick Mocheld550d982006-06-27 00:41:40 -04001486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Len Brown4be44fc2005-08-05 00:44:28 -04001489static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Len Brown4be44fc2005-08-05 00:44:28 -04001491 return single_open(file, acpi_video_bus_POST_seq_show,
1492 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Len Brown4be44fc2005-08-05 00:44:28 -04001495static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1498}
1499
1500static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001501acpi_video_bus_write_POST(struct file *file,
1502 const char __user * buffer,
1503 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Len Brown4be44fc2005-08-05 00:44:28 -04001505 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001506 struct seq_file *m = file->private_data;
1507 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001508 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001509 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001513 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 status = acpi_video_bus_POST_options(video, &options);
1516 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001517 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001520 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 str[count] = 0;
1523 opt = strtoul(str, NULL, 0);
1524 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001525 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Julius Volz98fb8fe2007-02-20 16:38:40 +01001527 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 options |= 1;
1529
1530 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001531 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001533 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 }
1536
Patrick Mocheld550d982006-06-27 00:41:40 -04001537 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001541acpi_video_bus_write_DOS(struct file *file,
1542 const char __user * buffer,
1543 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Len Brown4be44fc2005-08-05 00:44:28 -04001545 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001546 struct seq_file *m = file->private_data;
1547 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001548 char str[12] = { 0 };
1549 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001553 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001556 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 str[count] = 0;
1559 opt = strtoul(str, NULL, 0);
1560 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Len Brown4be44fc2005-08-05 00:44:28 -04001563 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001566 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Patrick Mocheld550d982006-06-27 00:41:40 -04001568 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Len Brown4be44fc2005-08-05 00:44:28 -04001571static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001573 struct acpi_video_bus *video = acpi_driver_data(device);
1574 struct proc_dir_entry *device_dir;
1575 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001577 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1578 if (!device_dir)
1579 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001582 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001583 &acpi_video_bus_info_fops,
1584 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001586 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001589 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001590 &acpi_video_bus_ROM_fops,
1591 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001593 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001596 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001597 &acpi_video_bus_POST_info_fops,
1598 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001600 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001603 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001604 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001605 &acpi_video_bus_POST_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_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001611 entry = proc_create_data("DOS", 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_DOS_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001618 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001619 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001620
1621 err_remove_post:
1622 remove_proc_entry("POST", device_dir);
1623 err_remove_post_info:
1624 remove_proc_entry("POST_info", device_dir);
1625 err_remove_rom:
1626 remove_proc_entry("ROM", device_dir);
1627 err_remove_info:
1628 remove_proc_entry("info", device_dir);
1629 err_remove_dir:
1630 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1631 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
Len Brown4be44fc2005-08-05 00:44:28 -04001634static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001636 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001638 if (device_dir) {
1639 remove_proc_entry("info", device_dir);
1640 remove_proc_entry("ROM", device_dir);
1641 remove_proc_entry("POST_info", device_dir);
1642 remove_proc_entry("POST", device_dir);
1643 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001644 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 acpi_device_dir(device) = NULL;
1646 }
1647
Patrick Mocheld550d982006-06-27 00:41:40 -04001648 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/* --------------------------------------------------------------------------
1652 Driver Interface
1653 -------------------------------------------------------------------------- */
1654
1655/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001656static struct acpi_video_device_attrib*
1657acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1658{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001659 struct acpi_video_enumerated_device *ids;
1660 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001661
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001662 for (i = 0; i < video->attached_count; i++) {
1663 ids = &video->attached_array[i];
1664 if ((ids->value.int_val & 0xffff) == device_id)
1665 return &ids->value.attrib;
1666 }
1667
Rui Zhang82cae992007-01-03 23:40:53 -05001668 return NULL;
1669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671static int
Len Brown4be44fc2005-08-05 00:44:28 -04001672acpi_video_bus_get_one_device(struct acpi_device *device,
1673 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001675 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001676 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001677 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001678 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Len Brown4be44fc2005-08-05 00:44:28 -04001683 status =
1684 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (ACPI_SUCCESS(status)) {
1686
Burman Yan36bcbec2006-12-19 12:56:11 -08001687 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001689 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1692 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001693 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 data->device_id = device_id;
1696 data->video = video;
1697 data->dev = device;
1698
Rui Zhang82cae992007-01-03 23:40:53 -05001699 attribute = acpi_video_get_device_attr(video, device_id);
1700
1701 if((attribute != NULL) && attribute->device_id_scheme) {
1702 switch (attribute->display_type) {
1703 case ACPI_VIDEO_DISPLAY_CRT:
1704 data->flags.crt = 1;
1705 break;
1706 case ACPI_VIDEO_DISPLAY_TV:
1707 data->flags.tvout = 1;
1708 break;
1709 case ACPI_VIDEO_DISPLAY_DVI:
1710 data->flags.dvi = 1;
1711 break;
1712 case ACPI_VIDEO_DISPLAY_LCD:
1713 data->flags.lcd = 1;
1714 break;
1715 default:
1716 data->flags.unknown = 1;
1717 break;
1718 }
1719 if(attribute->bios_can_detect)
1720 data->flags.bios = 1;
1721 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 acpi_video_device_bind(video, data);
1725 acpi_video_device_find_cap(data);
1726
Patrick Mochel90130262006-05-19 16:54:48 -04001727 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001728 ACPI_DEVICE_NOTIFY,
1729 acpi_video_device_notify,
1730 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001732 printk(KERN_ERR PREFIX
1733 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001734 if(data->brightness)
1735 kfree(data->brightness->levels);
1736 kfree(data->brightness);
1737 kfree(data);
1738 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001741 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001743 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 acpi_video_device_add_fs(device);
1746
Patrick Mocheld550d982006-06-27 00:41:40 -04001747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
Patrick Mocheld550d982006-06-27 00:41:40 -04001750 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753/*
1754 * Arg:
1755 * video : video bus device
1756 *
1757 * Return:
1758 * none
1759 *
1760 * Enumerate the video device list of the video bus,
1761 * bind the ids with the corresponding video devices
1762 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Len Brown4be44fc2005-08-05 00:44:28 -04001765static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001767 struct acpi_video_device *dev;
1768
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001769 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001770
1771 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001772 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001773
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001774 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/*
1778 * Arg:
1779 * video : video bus device
1780 * device : video output device under the video
1781 * bus
1782 *
1783 * Return:
1784 * none
1785 *
1786 * Bind the ids with the corresponding video devices
1787 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790static void
Len Brown4be44fc2005-08-05 00:44:28 -04001791acpi_video_device_bind(struct acpi_video_bus *video,
1792 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001794 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001795 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001797 for (i = 0; i < video->attached_count; i++) {
1798 ids = &video->attached_array[i];
1799 if (device->device_id == (ids->value.int_val & 0xffff)) {
1800 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1802 }
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/*
1807 * Arg:
1808 * video : video bus device
1809 *
1810 * Return:
1811 * < 0 : error
1812 *
1813 * Call _DOD to enumerate all devices attached to display adapter
1814 *
Len Brown4be44fc2005-08-05 00:44:28 -04001815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1818{
Len Brown4be44fc2005-08-05 00:44:28 -04001819 int status;
1820 int count;
1821 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001822 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001823 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1824 union acpi_object *dod = NULL;
1825 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Patrick Mochel90130262006-05-19 16:54:48 -04001827 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001829 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001830 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001833 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001835 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 status = -EFAULT;
1837 goto out;
1838 }
1839
1840 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001841 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001843 active_list = kcalloc(1 + dod->package.count,
1844 sizeof(struct acpi_video_enumerated_device),
1845 GFP_KERNEL);
1846 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 status = -ENOMEM;
1848 goto out;
1849 }
1850
1851 count = 0;
1852 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001853 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001856 printk(KERN_ERR PREFIX
1857 "Invalid _DOD data in element %d\n", i);
1858 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001860
1861 active_list[count].value.int_val = obj->integer.value;
1862 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001863 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1864 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 count++;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Jesper Juhl6044ec82005-11-07 01:01:32 -08001868 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001869
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001870 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001872
1873 out:
Len Brown02438d82006-06-30 03:19:10 -04001874 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001875 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Len Brown4be44fc2005-08-05 00:44:28 -04001878static int
1879acpi_video_get_next_level(struct acpi_video_device *device,
1880 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001882 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001883 max = max_below = 0;
1884 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001885 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001886 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001887 l = device->brightness->levels[i];
1888 if (abs(l - level_current) < abs(delta)) {
1889 delta = l - level_current;
1890 if (!delta)
1891 break;
1892 }
1893 }
1894 /* Ajust level_current to closest available level */
1895 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001896 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001897 l = device->brightness->levels[i];
1898 if (l < min)
1899 min = l;
1900 if (l > max)
1901 max = l;
1902 if (l < min_above && l > level_current)
1903 min_above = l;
1904 if (l > max_below && l < level_current)
1905 max_below = l;
1906 }
1907
1908 switch (event) {
1909 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1910 return (level_current < max) ? min_above : min;
1911 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1912 return (level_current < max) ? min_above : max;
1913 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1914 return (level_current > min) ? max_below : min;
1915 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1916 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1917 return 0;
1918 default:
1919 return level_current;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Zhang Ruic8890f92009-03-18 16:27:08 +08001923static int
Len Brown4be44fc2005-08-05 00:44:28 -04001924acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001926 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001927 int result = -EINVAL;
1928
Julia Jomantaite469778c2008-06-23 22:50:42 +01001929 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001930 goto out;
1931
1932 result = acpi_video_device_lcd_get_level_current(device,
1933 &level_current);
1934 if (result)
1935 goto out;
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001938
Zhang Rui24450c72009-03-18 16:27:10 +08001939 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001940
1941out:
1942 if (result)
1943 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1944
1945 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948static int
Len Brown4be44fc2005-08-05 00:44:28 -04001949acpi_video_bus_get_devices(struct acpi_video_bus *video,
1950 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Len Brown4be44fc2005-08-05 00:44:28 -04001952 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001953 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 acpi_video_device_enumerate(video);
1956
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001957 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 status = acpi_video_bus_get_one_device(dev, video);
1960 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001961 printk(KERN_WARNING PREFIX
1962 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 continue;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Len Brown4be44fc2005-08-05 00:44:28 -04001969static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Karol Kozimor031ec772005-07-30 04:18:00 -04001971 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct acpi_video_bus *video;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001976 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 video = device->video;
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 acpi_video_device_remove_fs(device->dev);
1981
Patrick Mochel90130262006-05-19 16:54:48 -04001982 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001983 ACPI_DEVICE_NOTIFY,
1984 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001985 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001986 if (device->cdev) {
1987 sysfs_remove_link(&device->dev->dev.kobj,
1988 "thermal_cooling");
1989 sysfs_remove_link(&device->cdev->device.kobj,
1990 "device");
1991 thermal_cooling_device_unregister(device->cdev);
1992 device->cdev = NULL;
1993 }
Luming Yu23b0f012007-05-09 21:07:05 +08001994 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001995
Patrick Mocheld550d982006-06-27 00:41:40 -04001996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Len Brown4be44fc2005-08-05 00:44:28 -04001999static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Len Brown4be44fc2005-08-05 00:44:28 -04002001 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002002 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002004 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002006 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002008 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04002009 if (ACPI_FAILURE(status))
2010 printk(KERN_WARNING PREFIX
2011 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002013 if (dev->brightness) {
2014 kfree(dev->brightness->levels);
2015 kfree(dev->brightness);
2016 }
2017 list_del(&dev->entry);
2018 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002021 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05002022
Patrick Mocheld550d982006-06-27 00:41:40 -04002023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026/* acpi_video interface */
2027
Len Brown4be44fc2005-08-05 00:44:28 -04002028static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Zhang Ruia21101c2007-09-14 11:46:22 +08002030 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Len Brown4be44fc2005-08-05 00:44:28 -04002033static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 return acpi_video_bus_DOS(video, 0, 1);
2036}
2037
Bjorn Helgaas70155582009-04-07 15:37:11 +00002038static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Bjorn Helgaas70155582009-04-07 15:37:11 +00002040 struct acpi_video_bus *video = acpi_driver_data(device);
Luming Yue9dab192007-08-20 18:23:53 +08002041 struct input_dev *input;
2042 int keycode;
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Luming Yue9dab192007-08-20 18:23:53 +08002047 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01002050 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * most likely via hotkey. */
Len Brown14e04fb32007-08-23 15:20:26 -04002052 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002053 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 break;
2055
Julius Volz98fb8fe2007-02-20 16:38:40 +01002056 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 * connector. */
2058 acpi_video_device_enumerate(video);
2059 acpi_video_device_rebind(video);
Len Brown14e04fb32007-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
Len Brown4be44fc2005-08-05 00:44:28 -04002064 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002065 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002066 keycode = KEY_SWITCHVIDEOMODE;
2067 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002068 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002069 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002070 keycode = KEY_VIDEO_NEXT;
2071 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002072 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb32007-08-23 15:20:26 -04002073 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002074 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 break;
2076
2077 default:
Luming Yue9dab192007-08-20 18:23:53 +08002078 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002080 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 break;
2082 }
2083
Zhang Rui7761f632008-01-25 14:48:12 +08002084 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002085 input_report_key(input, keycode, 1);
2086 input_sync(input);
2087 input_report_key(input, keycode, 0);
2088 input_sync(input);
2089
Patrick Mocheld550d982006-06-27 00:41:40 -04002090 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Len Brown4be44fc2005-08-05 00:44:28 -04002093static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002095 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002096 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002097 struct acpi_video_bus *bus;
2098 struct input_dev *input;
2099 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002104 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002105 bus = video_device->video;
2106 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002109 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002110 if (brightness_switch_enabled)
2111 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002112 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002113 keycode = KEY_BRIGHTNESS_CYCLE;
2114 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002115 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002116 if (brightness_switch_enabled)
2117 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002118 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002119 keycode = KEY_BRIGHTNESSUP;
2120 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002121 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002122 if (brightness_switch_enabled)
2123 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002124 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002125 keycode = KEY_BRIGHTNESSDOWN;
2126 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002127 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002128 if (brightness_switch_enabled)
2129 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002130 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002131 keycode = KEY_BRIGHTNESS_ZERO;
2132 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002133 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002134 if (brightness_switch_enabled)
2135 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb32007-08-23 15:20:26 -04002136 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002137 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139 default:
Luming Yue9dab192007-08-20 18:23:53 +08002140 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002142 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 break;
2144 }
Luming Yue9dab192007-08-20 18:23:53 +08002145
Zhang Rui7761f632008-01-25 14:48:12 +08002146 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002147 input_report_key(input, keycode, 1);
2148 input_sync(input);
2149 input_report_key(input, keycode, 0);
2150 input_sync(input);
2151
Patrick Mocheld550d982006-06-27 00:41:40 -04002152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Zhang Ruie6d9da12007-08-25 02:23:31 -04002155static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002156static int acpi_video_resume(struct acpi_device *device)
2157{
2158 struct acpi_video_bus *video;
2159 struct acpi_video_device *video_device;
2160 int i;
2161
2162 if (!device || !acpi_driver_data(device))
2163 return -EINVAL;
2164
2165 video = acpi_driver_data(device);
2166
2167 for (i = 0; i < video->attached_count; i++) {
2168 video_device = video->attached_array[i].bind_info;
2169 if (video_device && video_device->backlight)
2170 acpi_video_set_brightness(video_device->backlight);
2171 }
2172 return AE_OK;
2173}
2174
Len Brown4be44fc2005-08-05 00:44:28 -04002175static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002177 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002178 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002179 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Burman Yan36bcbec2006-12-19 12:56:11 -08002181 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002183 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Zhang Ruie6d9da12007-08-25 02:23:31 -04002185 /* a hack to fix the duplicate name "VID" problem on T61 */
2186 if (!strcmp(device->pnp.bus_id, "VID")) {
2187 if (instance)
2188 device->pnp.bus_id[3] = '0' + instance;
2189 instance ++;
2190 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002191 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2192 if (!strcmp(device->pnp.bus_id, "VGA")) {
2193 if (instance)
2194 device->pnp.bus_id[3] = '0' + instance;
2195 instance++;
2196 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002197
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002198 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2200 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002201 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002204 error = acpi_video_bus_check(video);
2205 if (error)
2206 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002208 error = acpi_video_bus_add_fs(device);
2209 if (error)
2210 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002212 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 INIT_LIST_HEAD(&video->video_device_list);
2214
2215 acpi_video_bus_get_devices(video, device);
2216 acpi_video_bus_start_devices(video);
2217
Luming Yue9dab192007-08-20 18:23:53 +08002218 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002219 if (!input) {
2220 error = -ENOMEM;
Bjorn Helgaas70155582009-04-07 15:37:11 +00002221 goto err_stop_video;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002222 }
Luming Yue9dab192007-08-20 18:23:53 +08002223
2224 snprintf(video->phys, sizeof(video->phys),
2225 "%s/video/input0", acpi_device_hid(video->device));
2226
2227 input->name = acpi_device_name(video->device);
2228 input->phys = video->phys;
2229 input->id.bustype = BUS_HOST;
2230 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002231 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002232 input->evbit[0] = BIT(EV_KEY);
2233 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2234 set_bit(KEY_VIDEO_NEXT, input->keybit);
2235 set_bit(KEY_VIDEO_PREV, input->keybit);
2236 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2237 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2238 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2239 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2240 set_bit(KEY_DISPLAY_OFF, input->keybit);
2241 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002242
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002243 error = input_register_device(input);
2244 if (error)
2245 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002248 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2249 video->flags.multihead ? "yes" : "no",
2250 video->flags.rom ? "yes" : "no",
2251 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002255 err_free_input_dev:
2256 input_free_device(input);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002257 err_stop_video:
2258 acpi_video_bus_stop_devices(video);
2259 acpi_video_bus_put_devices(video);
2260 kfree(video->attached_array);
2261 acpi_video_bus_remove_fs(device);
2262 err_free_video:
2263 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002264 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002265
2266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Len Brown4be44fc2005-08-05 00:44:28 -04002269static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Len Brown4be44fc2005-08-05 00:44:28 -04002271 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002277 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 acpi_video_bus_stop_devices(video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 acpi_video_bus_put_devices(video);
2281 acpi_video_bus_remove_fs(device);
2282
Luming Yue9dab192007-08-20 18:23:53 +08002283 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002284 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 kfree(video);
2286
Patrick Mocheld550d982006-06-27 00:41:40 -04002287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Matthew Garrett74a365b2009-03-19 21:35:39 +00002290static int __init intel_opregion_present(void)
2291{
2292#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2293 struct pci_dev *dev = NULL;
2294 u32 address;
2295
2296 for_each_pci_dev(dev) {
2297 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2298 continue;
2299 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2300 continue;
2301 pci_read_config_dword(dev, 0xfc, &address);
2302 if (!address)
2303 continue;
2304 return 1;
2305 }
2306#endif
2307 return 0;
2308}
2309
2310int acpi_video_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Len Brown4be44fc2005-08-05 00:44:28 -04002312 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2315 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002316 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 result = acpi_bus_register_driver(&acpi_video_bus);
2319 if (result < 0) {
2320 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002321 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323
Patrick Mocheld550d982006-06-27 00:41:40 -04002324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
Matthew Garrett74a365b2009-03-19 21:35:39 +00002326EXPORT_SYMBOL(acpi_video_register);
2327
2328/*
2329 * This is kind of nasty. Hardware using Intel chipsets may require
2330 * the video opregion code to be run first in order to initialise
2331 * state before any ACPI video calls are made. To handle this we defer
2332 * registration of the video class until the opregion code has run.
2333 */
2334
2335static int __init acpi_video_init(void)
2336{
Zhang Rui45cb50e2009-04-24 12:13:18 -04002337 dmi_check_system(video_dmi_table);
2338
Matthew Garrett74a365b2009-03-19 21:35:39 +00002339 if (intel_opregion_present())
2340 return 0;
2341
2342 return acpi_video_register();
2343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Matthew Garrett3b1c1c12009-04-01 19:52:29 +01002345void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 acpi_bus_unregister_driver(&acpi_video_bus);
2349
2350 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2351
Patrick Mocheld550d982006-06-27 00:41:40 -04002352 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
Matthew Garrett3b1c1c12009-04-01 19:52:29 +01002354EXPORT_SYMBOL(acpi_video_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356module_init(acpi_video_init);
2357module_exit(acpi_video_exit);