blob: dc84970ce1e9acc96f7bfc90e4f4e40322cd008e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_VIDEO_BUS_NAME "Video Bus"
47#define ACPI_VIDEO_DEVICE_NAME "Video Device"
48#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
49#define ACPI_VIDEO_NOTIFY_PROBE 0x81
50#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
51#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
52#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
53
Thomas Tuttlef4715182006-12-19 12:56:14 -080054#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
55#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
56#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
57#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
58#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Yu Luming2f3d0002006-11-11 02:40:34 +080060#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Rui Zhang82cae992007-01-03 23:40:53 -050062#define ACPI_VIDEO_DISPLAY_CRT 1
63#define ACPI_VIDEO_DISPLAY_TV 2
64#define ACPI_VIDEO_DISPLAY_DVI 3
65#define ACPI_VIDEO_DISPLAY_LCD 4
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050068ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Len Brownf52fd662007-02-12 22:42:12 -050070MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050071MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_LICENSE("GPL");
73
Zhang Rui8a681a42008-01-25 14:47:49 +080074static int brightness_switch_enabled = 1;
75module_param(brightness_switch_enabled, bool, 0644);
76
Len Brown4be44fc2005-08-05 00:44:28 -040077static int acpi_video_bus_add(struct acpi_device *device);
78static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080079static int acpi_video_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Thomas Renninger1ba90e32007-07-23 14:44:41 +020081static const struct acpi_device_id video_device_ids[] = {
82 {ACPI_VIDEO_HID, 0},
83 {"", 0},
84};
85MODULE_DEVICE_TABLE(acpi, video_device_ids);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct acpi_driver acpi_video_bus = {
Len Brownc2b6705b2007-02-12 23:33:40 -050088 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020090 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .ops = {
92 .add = acpi_video_bus_add,
93 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -080094 .resume = acpi_video_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040095 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -040099 u8 multihead:1; /* can switch video heads */
100 u8 rom:1; /* can retrieve a video rom */
101 u8 post:1; /* can configure the head to */
102 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u8 _DOS:1; /*Enable/Disable output switching */
107 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
108 u8 _ROM:1; /*Get ROM Data */
109 u8 _GPD:1; /*Get POST Device */
110 u8 _SPD:1; /*Set POST Device */
111 u8 _VPO:1; /*Video POST Options */
112 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Len Brown4be44fc2005-08-05 00:44:28 -0400115struct acpi_video_device_attrib {
116 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100117 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400118 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100119 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u32 bios_can_detect:1; /*BIOS can detect the device */
121 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
122 the VGA device. */
123 u32 pipe_id:3; /*For VGA multiple-head devices. */
124 u32 reserved:10; /*Must be 0 */
125 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct acpi_video_enumerated_device {
129 union {
130 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400131 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } value;
133 struct acpi_video_device *bind_info;
134};
135
136struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400137 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 u8 attached_count;
141 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500144 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800146 struct input_dev *input;
147 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400151 u8 crt:1;
152 u8 lcd:1;
153 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500154 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 bios:1;
156 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500157 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400161 u8 _ADR:1; /*Return the unique ID */
162 u8 _BCL:1; /*Query list of brightness control levels supported */
163 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800164 u8 _BQC:1; /* Get current brightness level */
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
Zhang Ruid32f6942009-03-18 16:27:12 +0800171struct acpi_video_brightness_flags {
172 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800173 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Ruid32f6942009-03-18 16:27:12 +0800174};
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400177 int curr;
178 int count;
179 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800180 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
183struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400184 unsigned long device_id;
185 struct acpi_video_device_flags flags;
186 struct acpi_video_device_cap cap;
187 struct list_head entry;
188 struct acpi_video_bus *video;
189 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800191 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800192 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800193 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* bus */
197static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
198static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700199 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400200 .open = acpi_video_bus_info_open_fs,
201 .read = seq_read,
202 .llseek = seq_lseek,
203 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
207static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700208 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400209 .open = acpi_video_bus_ROM_open_fs,
210 .read = seq_read,
211 .llseek = seq_lseek,
212 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
216 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700218 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 .open = acpi_video_bus_POST_info_open_fs,
220 .read = seq_read,
221 .llseek = seq_lseek,
222 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
225static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
226static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700227 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 .open = acpi_video_bus_POST_open_fs,
229 .read = seq_read,
230 .llseek = seq_lseek,
231 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
235static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700236 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400237 .open = acpi_video_bus_DOS_open_fs,
238 .read = seq_read,
239 .llseek = seq_lseek,
240 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_video_device_info_open_fs(struct inode *inode,
245 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700247 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400248 .open = acpi_video_device_info_open_fs,
249 .read = seq_read,
250 .llseek = seq_lseek,
251 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254static int acpi_video_device_state_open_fs(struct inode *inode,
255 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700257 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400258 .open = acpi_video_device_state_open_fs,
259 .read = seq_read,
260 .llseek = seq_lseek,
261 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262};
263
Len Brown4be44fc2005-08-05 00:44:28 -0400264static int acpi_video_device_brightness_open_fs(struct inode *inode,
265 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700267 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400268 .open = acpi_video_device_brightness_open_fs,
269 .read = seq_read,
270 .llseek = seq_lseek,
271 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272};
273
Len Brown4be44fc2005-08-05 00:44:28 -0400274static int acpi_video_device_EDID_open_fs(struct inode *inode,
275 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700277 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400278 .open = acpi_video_device_EDID_open_fs,
279 .read = seq_read,
280 .llseek = seq_lseek,
281 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 "motherboard VGA device",
286 "PCI VGA device",
287 "AGP VGA device",
288 "UNKNOWN",
289};
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
292static void acpi_video_device_rebind(struct acpi_video_bus *video);
293static void acpi_video_device_bind(struct acpi_video_bus *video,
294 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800296static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
297 int level);
298static int acpi_video_device_lcd_get_level_current(
299 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400300 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400301static int acpi_video_get_next_level(struct acpi_video_device *device,
302 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800303static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400304 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800305static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400306 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800307static int acpi_video_output_get(struct output_device *od);
308static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Yu Luming2f3d0002006-11-11 02:40:34 +0800310/*backlight device sysfs support*/
311static int acpi_video_get_brightness(struct backlight_device *bd)
312{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400313 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000314 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800315 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100316 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800317
318 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
319 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000320 for (i = 2; i < vd->brightness->count; i++) {
321 if (vd->brightness->levels[i] == cur_level)
322 /* The first two entries are special - see page 575
323 of the ACPI spec 3.0 */
324 return i-2;
325 }
326 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800327}
328
329static int acpi_video_set_brightness(struct backlight_device *bd)
330{
Zhang Rui24450c72009-03-18 16:27:10 +0800331 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800332 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100333 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800334
335 return acpi_video_device_lcd_set_level(vd,
336 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800337}
338
Richard Purdie599a52d2007-02-10 23:07:48 +0000339static struct backlight_ops acpi_backlight_ops = {
340 .get_brightness = acpi_video_get_brightness,
341 .update_status = acpi_video_set_brightness,
342};
343
Luming Yu23b0f012007-05-09 21:07:05 +0800344/*video output device sysfs support*/
345static int acpi_video_output_get(struct output_device *od)
346{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400347 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800348 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700349 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800350 acpi_video_device_get_state(vd, &state);
351 return (int)state;
352}
353
354static int acpi_video_output_set(struct output_device *od)
355{
356 unsigned long state = od->request_state;
357 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700358 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800359 return acpi_video_device_set_state(vd, state);
360}
361
362static struct output_properties acpi_output_properties = {
363 .set_state = acpi_video_output_set,
364 .get_status = acpi_video_output_get,
365};
Zhang Rui702ed512008-01-17 15:51:22 +0800366
367
368/* thermal cooling device callbacks */
369static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
370{
371 struct acpi_device *device = cdev->devdata;
372 struct acpi_video_device *video = acpi_driver_data(device);
373
374 return sprintf(buf, "%d\n", video->brightness->count - 3);
375}
376
377static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
378{
379 struct acpi_device *device = cdev->devdata;
380 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400381 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800382 int state;
383
Zhang Ruic8890f92009-03-18 16:27:08 +0800384 if (acpi_video_device_lcd_get_level_current(video, &level))
385 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800386 for (state = 2; state < video->brightness->count; state++)
387 if (level == video->brightness->levels[state])
388 return sprintf(buf, "%d\n",
389 video->brightness->count - state - 1);
390
391 return -EINVAL;
392}
393
394static int
395video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
396{
397 struct acpi_device *device = cdev->devdata;
398 struct acpi_video_device *video = acpi_driver_data(device);
399 int level;
400
401 if ( state >= video->brightness->count - 2)
402 return -EINVAL;
403
404 state = video->brightness->count - state;
405 level = video->brightness->levels[state -1];
406 return acpi_video_device_lcd_set_level(video, level);
407}
408
409static struct thermal_cooling_device_ops video_cooling_ops = {
410 .get_max_state = video_get_max_state,
411 .get_cur_state = video_get_cur_state,
412 .set_cur_state = video_set_cur_state,
413};
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* --------------------------------------------------------------------------
416 Video Management
417 -------------------------------------------------------------------------- */
418
419/* device */
420
421static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400422acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Len Brown4be44fc2005-08-05 00:44:28 -0400424 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400425
426 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Patrick Mocheld550d982006-06-27 00:41:40 -0400428 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static int
Len Brown4be44fc2005-08-05 00:44:28 -0400432acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400433 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Patrick Mochel90130262006-05-19 16:54:48 -0400437 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Patrick Mocheld550d982006-06-27 00:41:40 -0400439 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static int
Len Brown4be44fc2005-08-05 00:44:28 -0400443acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Len Brown4be44fc2005-08-05 00:44:28 -0400445 int status;
446 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
447 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400448 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400452 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Patrick Mocheld550d982006-06-27 00:41:40 -0400454 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static int
Len Brown4be44fc2005-08-05 00:44:28 -0400458acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
459 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Len Brown4be44fc2005-08-05 00:44:28 -0400461 int status;
462 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
463 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 *levels = NULL;
467
Patrick Mochel90130262006-05-19 16:54:48 -0400468 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400471 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500472 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400473 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 status = -EFAULT;
475 goto err;
476 }
477
478 *levels = obj;
479
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800483 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Patrick Mocheld550d982006-06-27 00:41:40 -0400485 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static int
Len Brown4be44fc2005-08-05 00:44:28 -0400489acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Zhang Rui24450c72009-03-18 16:27:10 +0800491 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400492 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
493 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800494 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Zhang Rui24450c72009-03-18 16:27:10 +0800498 status = acpi_evaluate_object(device->dev->handle, "_BCM",
499 &args, NULL);
500 if (ACPI_FAILURE(status)) {
501 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
502 return -EIO;
503 }
504
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400505 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800506 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800507 if (level == device->brightness->levels[state]) {
Zhang Rui9e6dada2008-12-31 10:58:48 +0800508 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800509 return 0;
510 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800511
Zhang Rui24450c72009-03-18 16:27:10 +0800512 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
513 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516static int
Len Brown4be44fc2005-08-05 00:44:28 -0400517acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400518 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Zhang Ruic8890f92009-03-18 16:27:08 +0800520 acpi_status status = AE_OK;
521
522 if (device->cap._BQC) {
523 status = acpi_evaluate_integer(device->dev->handle, "_BQC",
524 NULL, level);
525 if (ACPI_SUCCESS(status)) {
526 device->brightness->curr = *level;
527 return 0;
528 } else {
529 /* Fixme:
530 * should we return an error or ignore this failure?
531 * dev->brightness->curr is a cached value which stores
532 * the correct current backlight level in most cases.
533 * ACPI video backlight still works w/ buggy _BQC.
534 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
535 */
536 ACPI_WARNING((AE_INFO, "Evaluating _BQC failed"));
537 device->cap._BQC = 0;
538 }
539 }
540
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400541 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static int
Len Brown4be44fc2005-08-05 00:44:28 -0400546acpi_video_device_EDID(struct acpi_video_device *device,
547 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Len Brown4be44fc2005-08-05 00:44:28 -0400549 int status;
550 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
551 union acpi_object *obj;
552 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
553 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 *edid = NULL;
557
558 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (length == 128)
561 arg0.integer.value = 1;
562 else if (length == 256)
563 arg0.integer.value = 2;
564 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Patrick Mochel90130262006-05-19 16:54:48 -0400567 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200571 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (obj && obj->type == ACPI_TYPE_BUFFER)
574 *edid = obj;
575 else {
Len Brown64684632006-06-26 23:41:38 -0400576 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 status = -EFAULT;
578 kfree(obj);
579 }
580
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/* bus */
585
586static int
Len Brown4be44fc2005-08-05 00:44:28 -0400587acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Len Brown4be44fc2005-08-05 00:44:28 -0400589 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400590 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400591 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
592 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 arg0.integer.value = option;
596
Patrick Mochel90130262006-05-19 16:54:48 -0400597 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400599 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Patrick Mocheld550d982006-06-27 00:41:40 -0400601 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400605acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int status;
608
Patrick Mochel90130262006-05-19 16:54:48 -0400609 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614static int
Len Brown4be44fc2005-08-05 00:44:28 -0400615acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400616 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Len Brown4be44fc2005-08-05 00:44:28 -0400618 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Patrick Mochel90130262006-05-19 16:54:48 -0400620 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *options &= 3;
622
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/*
627 * Arg:
628 * video : video bus device pointer
629 * bios_flag :
630 * 0. The system BIOS should NOT automatically switch(toggle)
631 * the active display output.
632 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100633 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * 2. The _DGS value should be locked.
635 * 3. The system BIOS should not automatically switch (toggle) the
636 * active display output, but instead generate the display switch
637 * event notify code.
638 * lcd_flag :
639 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100640 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100642 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * Return Value:
644 * -1 wrong arg.
645 */
646
647static int
Len Brown4be44fc2005-08-05 00:44:28 -0400648acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Len Brown4be44fc2005-08-05 00:44:28 -0400650 acpi_integer status = 0;
651 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
652 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Len Brown4be44fc2005-08-05 00:44:28 -0400655 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 status = -1;
657 goto Failed;
658 }
659 arg0.integer.value = (lcd_flag << 2) | bios_flag;
660 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400661 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Len Brown4be44fc2005-08-05 00:44:28 -0400663 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500668 * Simple comparison function used to sort backlight levels.
669 */
670
671static int
672acpi_video_cmp_level(const void *a, const void *b)
673{
674 return *(int *)a - *(int *)b;
675}
676
677/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * Arg:
679 * device : video output device (LCD, CRT, ..)
680 *
681 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100682 * Maximum brightness level
683 *
684 * Allocate and initialize device->brightness.
685 */
686
687static int
688acpi_video_init_brightness(struct acpi_video_device *device)
689{
690 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800691 int i, max_level = 0, count = 0, level_ac_battery = 0;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100692 union acpi_object *o;
693 struct acpi_video_device_brightness *br = NULL;
694
695 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
696 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
697 "LCD brightness level\n"));
698 goto out;
699 }
700
701 if (obj->package.count < 2)
702 goto out;
703
704 br = kzalloc(sizeof(*br), GFP_KERNEL);
705 if (!br) {
706 printk(KERN_ERR "can't allocate memory\n");
707 goto out;
708 }
709
Zhang Ruid32f6942009-03-18 16:27:12 +0800710 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100711 GFP_KERNEL);
712 if (!br->levels)
713 goto out_free;
714
715 for (i = 0; i < obj->package.count; i++) {
716 o = (union acpi_object *)&obj->package.elements[i];
717 if (o->type != ACPI_TYPE_INTEGER) {
718 printk(KERN_ERR PREFIX "Invalid data\n");
719 continue;
720 }
721 br->levels[count] = (u32) o->integer.value;
722
723 if (br->levels[count] > max_level)
724 max_level = br->levels[count];
725 count++;
726 }
727
Zhang Ruid32f6942009-03-18 16:27:12 +0800728 /*
729 * some buggy BIOS don't export the levels
730 * when machine is on AC/Battery in _BCL package.
731 * In this case, the first two elements in _BCL packages
732 * are also supported brightness levels that OS should take care of.
733 */
734 for (i = 2; i < count; i++)
735 if (br->levels[i] == br->levels[0] ||
736 br->levels[i] == br->levels[1])
737 level_ac_battery++;
738
739 if (level_ac_battery < 2) {
740 level_ac_battery = 2 - level_ac_battery;
741 br->flags._BCL_no_ac_battery_levels = 1;
742 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
743 br->levels[i] = br->levels[i - level_ac_battery];
744 count += level_ac_battery;
745 } else if (level_ac_battery > 2)
746 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
747
Zhang Ruid80fb992009-03-18 16:27:14 +0800748 /* Check if the _BCL package is in a reversed order */
749 if (max_level == br->levels[2]) {
750 br->flags._BCL_reversed = 1;
751 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
752 acpi_video_cmp_level, NULL);
753 } else if (max_level != br->levels[count - 1])
754 ACPI_ERROR((AE_INFO,
755 "Found unordered _BCL package\n"));
Zhang Rui935e5f22008-12-11 16:24:52 -0500756
Julia Jomantaite469778c2008-06-23 22:50:42 +0100757 br->count = count;
758 device->brightness = br;
Zhang Ruid32f6942009-03-18 16:27:12 +0800759 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
760 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100761 kfree(obj);
762 return max_level;
763
764out_free_levels:
765 kfree(br->levels);
766out_free:
767 kfree(br);
768out:
769 device->brightness = NULL;
770 kfree(obj);
771 return 0;
772}
773
774/*
775 * Arg:
776 * device : video output device (LCD, CRT, ..)
777 *
778 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * None
780 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100781 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * device.
783 */
784
Len Brown4be44fc2005-08-05 00:44:28 -0400785static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800788 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
William Lee Irwin III98934de2007-12-12 03:56:55 -0800791 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Patrick Mochel90130262006-05-19 16:54:48 -0400793 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 device->cap._ADR = 1;
795 }
Patrick Mochel90130262006-05-19 16:54:48 -0400796 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400797 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Patrick Mochel90130262006-05-19 16:54:48 -0400799 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400800 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800802 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
803 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400804 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400805 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Patrick Mochel90130262006-05-19 16:54:48 -0400807 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 device->cap._DCS = 1;
809 }
Patrick Mochel90130262006-05-19 16:54:48 -0400810 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 device->cap._DGS = 1;
812 }
Patrick Mochel90130262006-05-19 16:54:48 -0400813 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 device->cap._DSS = 1;
815 }
816
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200817 if (acpi_video_backlight_support())
818 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Zhao Yakuic2c78902008-07-17 10:46:05 +0800820 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800821 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800822 static int count = 0;
823 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800824 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
825 if (!name)
826 return;
827
Yu Luming2f3d0002006-11-11 02:40:34 +0800828 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800829 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000830 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000831 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800832 /*
833 * If there exists the _BQC object, the _BQC object will be
834 * called to get the current backlight brightness. Otherwise
835 * the brightness will be set to the maximum.
836 */
837 if (device->cap._BQC)
838 device->backlight->props.brightness =
839 acpi_video_get_brightness(device->backlight);
840 else
841 device->backlight->props.brightness =
842 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000843 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800844 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800845
846 device->cdev = thermal_cooling_device_register("LCD",
847 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500848 if (IS_ERR(device->cdev))
849 return;
850
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200851 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
852 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800853 result = sysfs_create_link(&device->dev->dev.kobj,
854 &device->cdev->device.kobj,
855 "thermal_cooling");
856 if (result)
857 printk(KERN_ERR PREFIX "Create sysfs link\n");
858 result = sysfs_create_link(&device->cdev->device.kobj,
859 &device->dev->dev.kobj, "device");
860 if (result)
861 printk(KERN_ERR PREFIX "Create sysfs link\n");
862
Yu Luming2f3d0002006-11-11 02:40:34 +0800863 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200864
865 if (acpi_video_display_switch_support()) {
866
867 if (device->cap._DCS && device->cap._DSS) {
868 static int count;
869 char *name;
870 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
871 if (!name)
872 return;
873 sprintf(name, "acpi_video%d", count++);
874 device->output_dev = video_output_register(name,
875 NULL, device, &acpi_output_properties);
876 kfree(name);
877 }
Luming Yu23b0f012007-05-09 21:07:05 +0800878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
882 * Arg:
883 * device : video output device (VGA)
884 *
885 * Return Value:
886 * None
887 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100888 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
890
Len Brown4be44fc2005-08-05 00:44:28 -0400891static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Len Brown4be44fc2005-08-05 00:44:28 -0400893 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
William Lee Irwin III98934de2007-12-12 03:56:55 -0800895 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400896 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 video->cap._DOS = 1;
898 }
Patrick Mochel90130262006-05-19 16:54:48 -0400899 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 video->cap._DOD = 1;
901 }
Patrick Mochel90130262006-05-19 16:54:48 -0400902 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 video->cap._ROM = 1;
904 }
Patrick Mochel90130262006-05-19 16:54:48 -0400905 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 video->cap._GPD = 1;
907 }
Patrick Mochel90130262006-05-19 16:54:48 -0400908 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 video->cap._SPD = 1;
910 }
Patrick Mochel90130262006-05-19 16:54:48 -0400911 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 video->cap._VPO = 1;
913 }
914}
915
916/*
917 * Check whether the video bus device has required AML method to
918 * support the desired features
919 */
920
Len Brown4be44fc2005-08-05 00:44:28 -0400921static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Len Brown4be44fc2005-08-05 00:44:28 -0400923 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200924 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Thomas Renninger22c13f92008-08-01 17:37:54 +0200929 dev = acpi_get_physical_pci_device(video->device->handle);
930 if (!dev)
931 return -ENODEV;
932 put_device(dev);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* Since there is no HID, CID and so on for VGA driver, we have
935 * to check well known required nodes.
936 */
937
Julius Volz98fb8fe2007-02-20 16:38:40 +0100938 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400939 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 video->flags.multihead = 1;
941 status = 0;
942 }
943
Julius Volz98fb8fe2007-02-20 16:38:40 +0100944 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400945 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 video->flags.rom = 1;
947 status = 0;
948 }
949
Julius Volz98fb8fe2007-02-20 16:38:40 +0100950 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400951 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 video->flags.post = 1;
953 status = 0;
954 }
955
Patrick Mocheld550d982006-06-27 00:41:40 -0400956 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/* --------------------------------------------------------------------------
960 FS Interface (/proc)
961 -------------------------------------------------------------------------- */
962
Len Brown4be44fc2005-08-05 00:44:28 -0400963static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965/* video devices */
966
Len Brown4be44fc2005-08-05 00:44:28 -0400967static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200969 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (!dev)
973 goto end;
974
975 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
976 seq_printf(seq, "type: ");
977 if (dev->flags.crt)
978 seq_printf(seq, "CRT\n");
979 else if (dev->flags.lcd)
980 seq_printf(seq, "LCD\n");
981 else if (dev->flags.tvout)
982 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500983 else if (dev->flags.dvi)
984 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 else
986 seq_printf(seq, "UNKNOWN\n");
987
Len Brown4be44fc2005-08-05 00:44:28 -0400988 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Len Brown4be44fc2005-08-05 00:44:28 -0400990 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static int
Len Brown4be44fc2005-08-05 00:44:28 -0400995acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 return single_open(file, acpi_video_device_info_seq_show,
998 PDE(inode)->data);
999}
1000
Len Brown4be44fc2005-08-05 00:44:28 -04001001static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Len Brown4be44fc2005-08-05 00:44:28 -04001003 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001004 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001005 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 if (!dev)
1009 goto end;
1010
1011 status = acpi_video_device_get_state(dev, &state);
1012 seq_printf(seq, "state: ");
1013 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001014 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 else
1016 seq_printf(seq, "<not supported>\n");
1017
1018 status = acpi_video_device_query(dev, &state);
1019 seq_printf(seq, "query: ");
1020 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001021 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 else
1023 seq_printf(seq, "<not supported>\n");
1024
Len Brown4be44fc2005-08-05 00:44:28 -04001025 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static int
Len Brown4be44fc2005-08-05 00:44:28 -04001030acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 return single_open(file, acpi_video_device_state_seq_show,
1033 PDE(inode)->data);
1034}
1035
1036static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001037acpi_video_device_write_state(struct file *file,
1038 const char __user * buffer,
1039 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Len Brown4be44fc2005-08-05 00:44:28 -04001041 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001042 struct seq_file *m = file->private_data;
1043 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001044 char str[12] = { 0 };
1045 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001049 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001052 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 str[count] = 0;
1055 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001056 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 status = acpi_video_device_set_state(dev, state);
1059
1060 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001061 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066static int
Len Brown4be44fc2005-08-05 00:44:28 -04001067acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001069 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001070 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (!dev || !dev->brightness) {
1074 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001079 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 seq_printf(seq, " %d", dev->brightness->levels[i]);
1081 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1082
Patrick Mocheld550d982006-06-27 00:41:40 -04001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086static int
Len Brown4be44fc2005-08-05 00:44:28 -04001087acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 return single_open(file, acpi_video_device_brightness_seq_show,
1090 PDE(inode)->data);
1091}
1092
1093static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001094acpi_video_device_write_brightness(struct file *file,
1095 const char __user * buffer,
1096 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001098 struct seq_file *m = file->private_data;
1099 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001100 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001101 unsigned int level = 0;
1102 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Thomas Renninger59d399d2005-11-08 05:27:00 -05001105 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 str[count] = 0;
1112 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001115 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Julius Volz98fb8fe2007-02-20 16:38:40 +01001117 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001118 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001120 if (!acpi_video_device_lcd_set_level(dev, level))
1121 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123 }
1124
Zhang Rui24450c72009-03-18 16:27:10 +08001125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Len Brown4be44fc2005-08-05 00:44:28 -04001128static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001130 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001131 int status;
1132 int i;
1133 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (!dev)
1137 goto out;
1138
Len Brown4be44fc2005-08-05 00:44:28 -04001139 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001141 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 if (ACPI_FAILURE(status)) {
1145 goto out;
1146 }
1147
1148 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1149 for (i = 0; i < edid->buffer.length; i++)
1150 seq_putc(seq, edid->buffer.pointer[i]);
1151 }
1152
Len Brown4be44fc2005-08-05 00:44:28 -04001153 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!edid)
1155 seq_printf(seq, "<not supported>\n");
1156 else
1157 kfree(edid);
1158
Patrick Mocheld550d982006-06-27 00:41:40 -04001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162static int
Len Brown4be44fc2005-08-05 00:44:28 -04001163acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 return single_open(file, acpi_video_device_EDID_seq_show,
1166 PDE(inode)->data);
1167}
1168
Len Brown4be44fc2005-08-05 00:44:28 -04001169static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001171 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 struct acpi_video_device *vid_dev;
1173
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001174 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001178 device_dir = proc_mkdir(acpi_device_bid(device),
1179 vid_dev->video->dir);
1180 if (!device_dir)
1181 return -ENOMEM;
1182
1183 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001186 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001187 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001189 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001192 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1193 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001194 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001195 &acpi_video_device_state_fops,
1196 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001198 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001201 acpi_video_device_brightness_fops.write =
1202 acpi_video_device_write_brightness;
1203 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001204 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001205 &acpi_video_device_brightness_fops,
1206 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001208 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001211 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001212 &acpi_video_device_EDID_fops,
1213 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001215 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001217 acpi_device_dir(device) = device_dir;
1218
Patrick Mocheld550d982006-06-27 00:41:40 -04001219 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001220
1221 err_remove_brightness:
1222 remove_proc_entry("brightness", device_dir);
1223 err_remove_state:
1224 remove_proc_entry("state", device_dir);
1225 err_remove_info:
1226 remove_proc_entry("info", device_dir);
1227 err_remove_dir:
1228 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1229 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
Len Brown4be44fc2005-08-05 00:44:28 -04001232static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001235 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001237 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001241 device_dir = acpi_device_dir(device);
1242 if (device_dir) {
1243 remove_proc_entry("info", device_dir);
1244 remove_proc_entry("state", device_dir);
1245 remove_proc_entry("brightness", device_dir);
1246 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001247 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 acpi_device_dir(device) = NULL;
1249 }
1250
Patrick Mocheld550d982006-06-27 00:41:40 -04001251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001255static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001257 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (!video)
1261 goto end;
1262
1263 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001264 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001266 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001268 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Len Brown4be44fc2005-08-05 00:44:28 -04001270 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Len Brown4be44fc2005-08-05 00:44:28 -04001274static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Len Brown4be44fc2005-08-05 00:44:28 -04001276 return single_open(file, acpi_video_bus_info_seq_show,
1277 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Len Brown4be44fc2005-08-05 00:44:28 -04001280static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001282 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!video)
1286 goto end;
1287
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001288 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 seq_printf(seq, "<TODO>\n");
1290
Len Brown4be44fc2005-08-05 00:44:28 -04001291 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Len Brown4be44fc2005-08-05 00:44:28 -04001295static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1298}
1299
Len Brown4be44fc2005-08-05 00:44:28 -04001300static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001302 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001303 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001304 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (!video)
1308 goto end;
1309
1310 status = acpi_video_bus_POST_options(video, &options);
1311 if (ACPI_SUCCESS(status)) {
1312 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001313 printk(KERN_WARNING PREFIX
1314 "The motherboard VGA device is not listed as a possible POST device.\n");
1315 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001316 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Frank Seidel4d939152009-02-04 17:03:07 +01001318 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001319 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (options & 2)
1321 seq_printf(seq, " <PCI video>");
1322 if (options & 4)
1323 seq_printf(seq, " <AGP video>");
1324 seq_putc(seq, '\n');
1325 } else
1326 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001327 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331static int
Len Brown4be44fc2005-08-05 00:44:28 -04001332acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Len Brown4be44fc2005-08-05 00:44:28 -04001334 return single_open(file, acpi_video_bus_POST_info_seq_show,
1335 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Len Brown4be44fc2005-08-05 00:44:28 -04001338static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001340 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001341 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001342 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (!video)
1346 goto end;
1347
Len Brown4be44fc2005-08-05 00:44:28 -04001348 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (!ACPI_SUCCESS(status)) {
1350 seq_printf(seq, "<not supported>\n");
1351 goto end;
1352 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001353 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Len Brown4be44fc2005-08-05 00:44:28 -04001355 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Len Brown4be44fc2005-08-05 00:44:28 -04001359static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001361 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Len Brown4be44fc2005-08-05 00:44:28 -04001364 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Len Brown4be44fc2005-08-05 00:44:28 -04001369static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Len Brown4be44fc2005-08-05 00:44:28 -04001371 return single_open(file, acpi_video_bus_POST_seq_show,
1372 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Len Brown4be44fc2005-08-05 00:44:28 -04001375static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1378}
1379
1380static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001381acpi_video_bus_write_POST(struct file *file,
1382 const char __user * buffer,
1383 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Len Brown4be44fc2005-08-05 00:44:28 -04001385 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001386 struct seq_file *m = file->private_data;
1387 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001388 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001389 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 status = acpi_video_bus_POST_options(video, &options);
1396 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001400 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 str[count] = 0;
1403 opt = strtoul(str, NULL, 0);
1404 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001405 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Julius Volz98fb8fe2007-02-20 16:38:40 +01001407 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 options |= 1;
1409
1410 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001411 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001413 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 }
1416
Patrick Mocheld550d982006-06-27 00:41:40 -04001417 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001421acpi_video_bus_write_DOS(struct file *file,
1422 const char __user * buffer,
1423 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Len Brown4be44fc2005-08-05 00:44:28 -04001425 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001426 struct seq_file *m = file->private_data;
1427 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001428 char str[12] = { 0 };
1429 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001436 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 str[count] = 0;
1439 opt = strtoul(str, NULL, 0);
1440 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001441 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Len Brown4be44fc2005-08-05 00:44:28 -04001443 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001446 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Patrick Mocheld550d982006-06-27 00:41:40 -04001448 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Len Brown4be44fc2005-08-05 00:44:28 -04001451static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001453 struct acpi_video_bus *video = acpi_driver_data(device);
1454 struct proc_dir_entry *device_dir;
1455 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001457 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1458 if (!device_dir)
1459 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001461 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001464 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001465 &acpi_video_bus_info_fops,
1466 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001468 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001471 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001472 &acpi_video_bus_ROM_fops,
1473 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001475 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001478 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001479 &acpi_video_bus_POST_info_fops,
1480 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001482 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001485 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001486 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001487 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001488 &acpi_video_bus_POST_fops,
1489 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001491 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001494 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001495 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001496 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001497 &acpi_video_bus_DOS_fops,
1498 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001500 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001502 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001503 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001504
1505 err_remove_post:
1506 remove_proc_entry("POST", device_dir);
1507 err_remove_post_info:
1508 remove_proc_entry("POST_info", device_dir);
1509 err_remove_rom:
1510 remove_proc_entry("ROM", device_dir);
1511 err_remove_info:
1512 remove_proc_entry("info", device_dir);
1513 err_remove_dir:
1514 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1515 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Len Brown4be44fc2005-08-05 00:44:28 -04001518static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001520 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001522 if (device_dir) {
1523 remove_proc_entry("info", device_dir);
1524 remove_proc_entry("ROM", device_dir);
1525 remove_proc_entry("POST_info", device_dir);
1526 remove_proc_entry("POST", device_dir);
1527 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001528 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 acpi_device_dir(device) = NULL;
1530 }
1531
Patrick Mocheld550d982006-06-27 00:41:40 -04001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/* --------------------------------------------------------------------------
1536 Driver Interface
1537 -------------------------------------------------------------------------- */
1538
1539/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001540static struct acpi_video_device_attrib*
1541acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1542{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001543 struct acpi_video_enumerated_device *ids;
1544 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001545
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001546 for (i = 0; i < video->attached_count; i++) {
1547 ids = &video->attached_array[i];
1548 if ((ids->value.int_val & 0xffff) == device_id)
1549 return &ids->value.attrib;
1550 }
1551
Rui Zhang82cae992007-01-03 23:40:53 -05001552 return NULL;
1553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555static int
Len Brown4be44fc2005-08-05 00:44:28 -04001556acpi_video_bus_get_one_device(struct acpi_device *device,
1557 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001559 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001560 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001561 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001562 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Len Brown4be44fc2005-08-05 00:44:28 -04001567 status =
1568 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (ACPI_SUCCESS(status)) {
1570
Burman Yan36bcbec2006-12-19 12:56:11 -08001571 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001573 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1576 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001577 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 data->device_id = device_id;
1580 data->video = video;
1581 data->dev = device;
1582
Rui Zhang82cae992007-01-03 23:40:53 -05001583 attribute = acpi_video_get_device_attr(video, device_id);
1584
1585 if((attribute != NULL) && attribute->device_id_scheme) {
1586 switch (attribute->display_type) {
1587 case ACPI_VIDEO_DISPLAY_CRT:
1588 data->flags.crt = 1;
1589 break;
1590 case ACPI_VIDEO_DISPLAY_TV:
1591 data->flags.tvout = 1;
1592 break;
1593 case ACPI_VIDEO_DISPLAY_DVI:
1594 data->flags.dvi = 1;
1595 break;
1596 case ACPI_VIDEO_DISPLAY_LCD:
1597 data->flags.lcd = 1;
1598 break;
1599 default:
1600 data->flags.unknown = 1;
1601 break;
1602 }
1603 if(attribute->bios_can_detect)
1604 data->flags.bios = 1;
1605 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 acpi_video_device_bind(video, data);
1609 acpi_video_device_find_cap(data);
1610
Patrick Mochel90130262006-05-19 16:54:48 -04001611 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001612 ACPI_DEVICE_NOTIFY,
1613 acpi_video_device_notify,
1614 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001616 printk(KERN_ERR PREFIX
1617 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001618 if(data->brightness)
1619 kfree(data->brightness->levels);
1620 kfree(data->brightness);
1621 kfree(data);
1622 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001625 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001627 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 acpi_video_device_add_fs(device);
1630
Patrick Mocheld550d982006-06-27 00:41:40 -04001631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
Patrick Mocheld550d982006-06-27 00:41:40 -04001634 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/*
1638 * Arg:
1639 * video : video bus device
1640 *
1641 * Return:
1642 * none
1643 *
1644 * Enumerate the video device list of the video bus,
1645 * bind the ids with the corresponding video devices
1646 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Len Brown4be44fc2005-08-05 00:44:28 -04001649static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001651 struct acpi_video_device *dev;
1652
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001653 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001654
1655 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001656 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001657
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001658 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661/*
1662 * Arg:
1663 * video : video bus device
1664 * device : video output device under the video
1665 * bus
1666 *
1667 * Return:
1668 * none
1669 *
1670 * Bind the ids with the corresponding video devices
1671 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674static void
Len Brown4be44fc2005-08-05 00:44:28 -04001675acpi_video_device_bind(struct acpi_video_bus *video,
1676 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001678 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001679 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001681 for (i = 0; i < video->attached_count; i++) {
1682 ids = &video->attached_array[i];
1683 if (device->device_id == (ids->value.int_val & 0xffff)) {
1684 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1686 }
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690/*
1691 * Arg:
1692 * video : video bus device
1693 *
1694 * Return:
1695 * < 0 : error
1696 *
1697 * Call _DOD to enumerate all devices attached to display adapter
1698 *
Len Brown4be44fc2005-08-05 00:44:28 -04001699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1702{
Len Brown4be44fc2005-08-05 00:44:28 -04001703 int status;
1704 int count;
1705 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001706 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001707 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1708 union acpi_object *dod = NULL;
1709 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Patrick Mochel90130262006-05-19 16:54:48 -04001711 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001713 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001714 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001717 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001719 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 status = -EFAULT;
1721 goto out;
1722 }
1723
1724 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001725 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001727 active_list = kcalloc(1 + dod->package.count,
1728 sizeof(struct acpi_video_enumerated_device),
1729 GFP_KERNEL);
1730 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 status = -ENOMEM;
1732 goto out;
1733 }
1734
1735 count = 0;
1736 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001737 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001740 printk(KERN_ERR PREFIX
1741 "Invalid _DOD data in element %d\n", i);
1742 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001744
1745 active_list[count].value.int_val = obj->integer.value;
1746 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001747 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1748 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 count++;
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jesper Juhl6044ec82005-11-07 01:01:32 -08001752 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001753
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001754 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001756
1757 out:
Len Brown02438d82006-06-30 03:19:10 -04001758 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001759 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Len Brown4be44fc2005-08-05 00:44:28 -04001762static int
1763acpi_video_get_next_level(struct acpi_video_device *device,
1764 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001766 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001767 max = max_below = 0;
1768 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001769 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001770 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001771 l = device->brightness->levels[i];
1772 if (abs(l - level_current) < abs(delta)) {
1773 delta = l - level_current;
1774 if (!delta)
1775 break;
1776 }
1777 }
1778 /* Ajust level_current to closest available level */
1779 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001780 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001781 l = device->brightness->levels[i];
1782 if (l < min)
1783 min = l;
1784 if (l > max)
1785 max = l;
1786 if (l < min_above && l > level_current)
1787 min_above = l;
1788 if (l > max_below && l < level_current)
1789 max_below = l;
1790 }
1791
1792 switch (event) {
1793 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1794 return (level_current < max) ? min_above : min;
1795 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1796 return (level_current < max) ? min_above : max;
1797 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1798 return (level_current > min) ? max_below : min;
1799 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1800 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1801 return 0;
1802 default:
1803 return level_current;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Zhang Ruic8890f92009-03-18 16:27:08 +08001807static int
Len Brown4be44fc2005-08-05 00:44:28 -04001808acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001810 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001811 int result = -EINVAL;
1812
Julia Jomantaite469778c2008-06-23 22:50:42 +01001813 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001814 goto out;
1815
1816 result = acpi_video_device_lcd_get_level_current(device,
1817 &level_current);
1818 if (result)
1819 goto out;
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001822
Zhang Rui24450c72009-03-18 16:27:10 +08001823 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001824
1825out:
1826 if (result)
1827 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1828
1829 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832static int
Len Brown4be44fc2005-08-05 00:44:28 -04001833acpi_video_bus_get_devices(struct acpi_video_bus *video,
1834 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Len Brown4be44fc2005-08-05 00:44:28 -04001836 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001837 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 acpi_video_device_enumerate(video);
1840
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001841 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 status = acpi_video_bus_get_one_device(dev, video);
1844 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001845 printk(KERN_WARNING PREFIX
1846 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 continue;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001850 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Len Brown4be44fc2005-08-05 00:44:28 -04001853static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Karol Kozimor031ec772005-07-30 04:18:00 -04001855 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct acpi_video_bus *video;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001860 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 video = device->video;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 acpi_video_device_remove_fs(device->dev);
1865
Patrick Mochel90130262006-05-19 16:54:48 -04001866 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001867 ACPI_DEVICE_NOTIFY,
1868 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001869 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001870 if (device->cdev) {
1871 sysfs_remove_link(&device->dev->dev.kobj,
1872 "thermal_cooling");
1873 sysfs_remove_link(&device->cdev->device.kobj,
1874 "device");
1875 thermal_cooling_device_unregister(device->cdev);
1876 device->cdev = NULL;
1877 }
Luming Yu23b0f012007-05-09 21:07:05 +08001878 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001879
Patrick Mocheld550d982006-06-27 00:41:40 -04001880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Len Brown4be44fc2005-08-05 00:44:28 -04001883static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Len Brown4be44fc2005-08-05 00:44:28 -04001885 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001886 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001888 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001890 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001892 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001893 if (ACPI_FAILURE(status))
1894 printk(KERN_WARNING PREFIX
1895 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001897 if (dev->brightness) {
1898 kfree(dev->brightness->levels);
1899 kfree(dev->brightness);
1900 }
1901 list_del(&dev->entry);
1902 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001905 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001906
Patrick Mocheld550d982006-06-27 00:41:40 -04001907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/* acpi_video interface */
1911
Len Brown4be44fc2005-08-05 00:44:28 -04001912static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Zhang Ruia21101c2007-09-14 11:46:22 +08001914 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Len Brown4be44fc2005-08-05 00:44:28 -04001917static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 return acpi_video_bus_DOS(video, 0, 1);
1920}
1921
Len Brown4be44fc2005-08-05 00:44:28 -04001922static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001924 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001925 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001926 struct input_dev *input;
1927 int keycode;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001930 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001932 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001933 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001936 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001938 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001939 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941
Julius Volz98fb8fe2007-02-20 16:38:40 +01001942 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * connector. */
1944 acpi_video_device_enumerate(video);
1945 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001946 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001947 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949
Len Brown4be44fc2005-08-05 00:44:28 -04001950 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001951 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001952 keycode = KEY_SWITCHVIDEOMODE;
1953 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001954 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001955 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001956 keycode = KEY_VIDEO_NEXT;
1957 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001958 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001959 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001960 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 break;
1962
1963 default:
Luming Yue9dab192007-08-20 18:23:53 +08001964 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001966 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
1968 }
1969
Zhang Rui7761f632008-01-25 14:48:12 +08001970 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001971 input_report_key(input, keycode, 1);
1972 input_sync(input);
1973 input_report_key(input, keycode, 0);
1974 input_sync(input);
1975
Patrick Mocheld550d982006-06-27 00:41:40 -04001976 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Len Brown4be44fc2005-08-05 00:44:28 -04001979static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001981 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001982 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001983 struct acpi_video_bus *bus;
1984 struct input_dev *input;
1985 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001988 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001990 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001991 bus = video_device->video;
1992 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001995 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001996 if (brightness_switch_enabled)
1997 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001998 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001999 keycode = KEY_BRIGHTNESS_CYCLE;
2000 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002001 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002002 if (brightness_switch_enabled)
2003 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002004 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002005 keycode = KEY_BRIGHTNESSUP;
2006 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002007 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002008 if (brightness_switch_enabled)
2009 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002010 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002011 keycode = KEY_BRIGHTNESSDOWN;
2012 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002013 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002014 if (brightness_switch_enabled)
2015 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002016 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002017 keycode = KEY_BRIGHTNESS_ZERO;
2018 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002019 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002020 if (brightness_switch_enabled)
2021 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002022 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002023 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025 default:
Luming Yue9dab192007-08-20 18:23:53 +08002026 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002028 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 }
Luming Yue9dab192007-08-20 18:23:53 +08002031
Zhang Rui7761f632008-01-25 14:48:12 +08002032 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002033 input_report_key(input, keycode, 1);
2034 input_sync(input);
2035 input_report_key(input, keycode, 0);
2036 input_sync(input);
2037
Patrick Mocheld550d982006-06-27 00:41:40 -04002038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Zhang Ruie6d9da12007-08-25 02:23:31 -04002041static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002042static int acpi_video_resume(struct acpi_device *device)
2043{
2044 struct acpi_video_bus *video;
2045 struct acpi_video_device *video_device;
2046 int i;
2047
2048 if (!device || !acpi_driver_data(device))
2049 return -EINVAL;
2050
2051 video = acpi_driver_data(device);
2052
2053 for (i = 0; i < video->attached_count; i++) {
2054 video_device = video->attached_array[i].bind_info;
2055 if (video_device && video_device->backlight)
2056 acpi_video_set_brightness(video_device->backlight);
2057 }
2058 return AE_OK;
2059}
2060
Len Brown4be44fc2005-08-05 00:44:28 -04002061static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002063 acpi_status status;
2064 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002065 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002066 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Burman Yan36bcbec2006-12-19 12:56:11 -08002068 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002070 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Zhang Ruie6d9da12007-08-25 02:23:31 -04002072 /* a hack to fix the duplicate name "VID" problem on T61 */
2073 if (!strcmp(device->pnp.bus_id, "VID")) {
2074 if (instance)
2075 device->pnp.bus_id[3] = '0' + instance;
2076 instance ++;
2077 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002078 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2079 if (!strcmp(device->pnp.bus_id, "VGA")) {
2080 if (instance)
2081 device->pnp.bus_id[3] = '0' + instance;
2082 instance++;
2083 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002084
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002085 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2087 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002088 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002091 error = acpi_video_bus_check(video);
2092 if (error)
2093 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002095 error = acpi_video_bus_add_fs(device);
2096 if (error)
2097 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002099 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 INIT_LIST_HEAD(&video->video_device_list);
2101
2102 acpi_video_bus_get_devices(video, device);
2103 acpi_video_bus_start_devices(video);
2104
Patrick Mochel90130262006-05-19 16:54:48 -04002105 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002106 ACPI_DEVICE_NOTIFY,
2107 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002109 printk(KERN_ERR PREFIX
2110 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002111 error = -ENODEV;
2112 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114
Luming Yue9dab192007-08-20 18:23:53 +08002115 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002116 if (!input) {
2117 error = -ENOMEM;
2118 goto err_uninstall_notify;
2119 }
Luming Yue9dab192007-08-20 18:23:53 +08002120
2121 snprintf(video->phys, sizeof(video->phys),
2122 "%s/video/input0", acpi_device_hid(video->device));
2123
2124 input->name = acpi_device_name(video->device);
2125 input->phys = video->phys;
2126 input->id.bustype = BUS_HOST;
2127 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002128 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002129 input->evbit[0] = BIT(EV_KEY);
2130 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2131 set_bit(KEY_VIDEO_NEXT, input->keybit);
2132 set_bit(KEY_VIDEO_PREV, input->keybit);
2133 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2134 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2135 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2136 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2137 set_bit(KEY_DISPLAY_OFF, input->keybit);
2138 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002139
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002140 error = input_register_device(input);
2141 if (error)
2142 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002145 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2146 video->flags.multihead ? "yes" : "no",
2147 video->flags.rom ? "yes" : "no",
2148 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002152 err_free_input_dev:
2153 input_free_device(input);
2154 err_uninstall_notify:
2155 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2156 acpi_video_bus_notify);
2157 err_stop_video:
2158 acpi_video_bus_stop_devices(video);
2159 acpi_video_bus_put_devices(video);
2160 kfree(video->attached_array);
2161 acpi_video_bus_remove_fs(device);
2162 err_free_video:
2163 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002164 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002165
2166 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Len Brown4be44fc2005-08-05 00:44:28 -04002169static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Len Brown4be44fc2005-08-05 00:44:28 -04002171 acpi_status status = 0;
2172 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002178 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 acpi_video_bus_stop_devices(video);
2181
Patrick Mochel90130262006-05-19 16:54:48 -04002182 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002183 ACPI_DEVICE_NOTIFY,
2184 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 acpi_video_bus_put_devices(video);
2187 acpi_video_bus_remove_fs(device);
2188
Luming Yue9dab192007-08-20 18:23:53 +08002189 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002190 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 kfree(video);
2192
Patrick Mocheld550d982006-06-27 00:41:40 -04002193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
Len Brown4be44fc2005-08-05 00:44:28 -04002196static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Len Brown4be44fc2005-08-05 00:44:28 -04002198 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2201 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002202 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 acpi_video_dir->owner = THIS_MODULE;
2204
2205 result = acpi_bus_register_driver(&acpi_video_bus);
2206 if (result < 0) {
2207 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002208 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210
Patrick Mocheld550d982006-06-27 00:41:40 -04002211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Len Brown4be44fc2005-08-05 00:44:28 -04002214static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 acpi_bus_unregister_driver(&acpi_video_bus);
2218
2219 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2220
Patrick Mocheld550d982006-06-27 00:41:40 -04002221 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224module_init(acpi_video_init);
2225module_exit(acpi_video_exit);