blob: 9730ec167590c5df3a4f971c3bd361b156229bea [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 Brownc2b67052007-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 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800165 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u8 _DDC:1; /*Return the EDID for this device */
167 u8 _DCS:1; /*Return status of output device */
168 u8 _DGS:1; /*Query graphics state */
169 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
Zhang Ruid32f6942009-03-18 16:27:12 +0800172struct acpi_video_brightness_flags {
173 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800174 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800175 u8 _BCL_use_index:1; /* levels in _BCL are index values */
176 u8 _BCM_use_index:1; /* input of _BCM is an index value */
177 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400181 int curr;
182 int count;
183 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800184 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 unsigned long device_id;
189 struct acpi_video_device_flags flags;
190 struct acpi_video_device_cap cap;
191 struct list_head entry;
192 struct acpi_video_bus *video;
193 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800195 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800196 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800197 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* bus */
201static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
202static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700203 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400204 .open = acpi_video_bus_info_open_fs,
205 .read = seq_read,
206 .llseek = seq_lseek,
207 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
211static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_video_bus_ROM_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
220 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700222 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400223 .open = acpi_video_bus_POST_info_open_fs,
224 .read = seq_read,
225 .llseek = seq_lseek,
226 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
230static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700231 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400232 .open = acpi_video_bus_POST_open_fs,
233 .read = seq_read,
234 .llseek = seq_lseek,
235 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236};
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
239static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700240 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 .open = acpi_video_bus_DOS_open_fs,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
247/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400248static int acpi_video_device_info_open_fs(struct inode *inode,
249 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700251 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 .open = acpi_video_device_info_open_fs,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_video_device_state_open_fs(struct inode *inode,
259 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700261 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400262 .open = acpi_video_device_state_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_brightness_open_fs(struct inode *inode,
269 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700271 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .open = acpi_video_device_brightness_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static int acpi_video_device_EDID_open_fs(struct inode *inode,
279 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700281 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400282 .open = acpi_video_device_EDID_open_fs,
283 .read = seq_read,
284 .llseek = seq_lseek,
285 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 "motherboard VGA device",
290 "PCI VGA device",
291 "AGP VGA device",
292 "UNKNOWN",
293};
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
296static void acpi_video_device_rebind(struct acpi_video_bus *video);
297static void acpi_video_device_bind(struct acpi_video_bus *video,
298 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800300static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
301 int level);
302static int acpi_video_device_lcd_get_level_current(
303 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400304 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400305static int acpi_video_get_next_level(struct acpi_video_device *device,
306 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800307static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400308 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800309static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400310 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800311static int acpi_video_output_get(struct output_device *od);
312static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Yu Luming2f3d0002006-11-11 02:40:34 +0800314/*backlight device sysfs support*/
315static int acpi_video_get_brightness(struct backlight_device *bd)
316{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400317 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000318 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800319 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100320 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800321
322 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
323 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000324 for (i = 2; i < vd->brightness->count; i++) {
325 if (vd->brightness->levels[i] == cur_level)
326 /* The first two entries are special - see page 575
327 of the ACPI spec 3.0 */
328 return i-2;
329 }
330 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800331}
332
333static int acpi_video_set_brightness(struct backlight_device *bd)
334{
Zhang Rui24450c72009-03-18 16:27:10 +0800335 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800336 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100337 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800338
339 return acpi_video_device_lcd_set_level(vd,
340 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800341}
342
Richard Purdie599a52d2007-02-10 23:07:48 +0000343static struct backlight_ops acpi_backlight_ops = {
344 .get_brightness = acpi_video_get_brightness,
345 .update_status = acpi_video_set_brightness,
346};
347
Luming Yu23b0f012007-05-09 21:07:05 +0800348/*video output device sysfs support*/
349static int acpi_video_output_get(struct output_device *od)
350{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400351 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800352 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700353 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800354 acpi_video_device_get_state(vd, &state);
355 return (int)state;
356}
357
358static int acpi_video_output_set(struct output_device *od)
359{
360 unsigned long state = od->request_state;
361 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700362 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800363 return acpi_video_device_set_state(vd, state);
364}
365
366static struct output_properties acpi_output_properties = {
367 .set_state = acpi_video_output_set,
368 .get_status = acpi_video_output_get,
369};
Zhang Rui702ed512008-01-17 15:51:22 +0800370
371
372/* thermal cooling device callbacks */
373static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
374{
375 struct acpi_device *device = cdev->devdata;
376 struct acpi_video_device *video = acpi_driver_data(device);
377
378 return sprintf(buf, "%d\n", video->brightness->count - 3);
379}
380
381static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
382{
383 struct acpi_device *device = cdev->devdata;
384 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400385 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800386 int state;
387
Zhang Ruic8890f92009-03-18 16:27:08 +0800388 if (acpi_video_device_lcd_get_level_current(video, &level))
389 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800390 for (state = 2; state < video->brightness->count; state++)
391 if (level == video->brightness->levels[state])
392 return sprintf(buf, "%d\n",
393 video->brightness->count - state - 1);
394
395 return -EINVAL;
396}
397
398static int
399video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
400{
401 struct acpi_device *device = cdev->devdata;
402 struct acpi_video_device *video = acpi_driver_data(device);
403 int level;
404
405 if ( state >= video->brightness->count - 2)
406 return -EINVAL;
407
408 state = video->brightness->count - state;
409 level = video->brightness->levels[state -1];
410 return acpi_video_device_lcd_set_level(video, level);
411}
412
413static struct thermal_cooling_device_ops video_cooling_ops = {
414 .get_max_state = video_get_max_state,
415 .get_cur_state = video_get_cur_state,
416 .set_cur_state = video_set_cur_state,
417};
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/* --------------------------------------------------------------------------
420 Video Management
421 -------------------------------------------------------------------------- */
422
423/* device */
424
425static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400426acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Len Brown4be44fc2005-08-05 00:44:28 -0400428 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400429
430 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Patrick Mocheld550d982006-06-27 00:41:40 -0400432 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435static int
Len Brown4be44fc2005-08-05 00:44:28 -0400436acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400437 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Len Brown4be44fc2005-08-05 00:44:28 -0400439 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Patrick Mochel90130262006-05-19 16:54:48 -0400441 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static int
Len Brown4be44fc2005-08-05 00:44:28 -0400447acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Len Brown4be44fc2005-08-05 00:44:28 -0400449 int status;
450 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
451 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400452 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400456 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static int
Len Brown4be44fc2005-08-05 00:44:28 -0400462acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
463 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Len Brown4be44fc2005-08-05 00:44:28 -0400465 int status;
466 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
467 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 *levels = NULL;
471
Patrick Mochel90130262006-05-19 16:54:48 -0400472 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400475 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500476 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400477 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 status = -EFAULT;
479 goto err;
480 }
481
482 *levels = obj;
483
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Len Brown4be44fc2005-08-05 00:44:28 -0400486 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800487 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static int
Len Brown4be44fc2005-08-05 00:44:28 -0400493acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Zhang Rui24450c72009-03-18 16:27:10 +0800495 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400496 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
497 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800498 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Zhang Rui24450c72009-03-18 16:27:10 +0800502 status = acpi_evaluate_object(device->dev->handle, "_BCM",
503 &args, NULL);
504 if (ACPI_FAILURE(status)) {
505 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
506 return -EIO;
507 }
508
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400509 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800510 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800511 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800512 if (device->backlight)
513 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800514 return 0;
515 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800516
Zhang Rui24450c72009-03-18 16:27:10 +0800517 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
518 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static int
Len Brown4be44fc2005-08-05 00:44:28 -0400522acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400523 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Zhang Ruic8890f92009-03-18 16:27:08 +0800525 acpi_status status = AE_OK;
526
Zhang Ruic60d6382009-03-18 16:27:18 +0800527 if (device->cap._BQC || device->cap._BCQ) {
528 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
529
530 status = acpi_evaluate_integer(device->dev->handle, buf,
Zhang Ruic8890f92009-03-18 16:27:08 +0800531 NULL, level);
532 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800533 if (device->brightness->flags._BQC_use_index) {
534 if (device->brightness->flags._BCL_reversed)
535 *level = device->brightness->count
536 - 3 - (*level);
537 *level = device->brightness->levels[*level + 2];
538
539 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800540 device->brightness->curr = *level;
541 return 0;
542 } else {
543 /* Fixme:
544 * should we return an error or ignore this failure?
545 * dev->brightness->curr is a cached value which stores
546 * the correct current backlight level in most cases.
547 * ACPI video backlight still works w/ buggy _BQC.
548 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
549 */
Zhang Ruic60d6382009-03-18 16:27:18 +0800550 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
551 device->cap._BQC = device->cap._BCQ = 0;
Zhang Ruic8890f92009-03-18 16:27:08 +0800552 }
553 }
554
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400555 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559static int
Len Brown4be44fc2005-08-05 00:44:28 -0400560acpi_video_device_EDID(struct acpi_video_device *device,
561 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Len Brown4be44fc2005-08-05 00:44:28 -0400563 int status;
564 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
565 union acpi_object *obj;
566 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
567 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 *edid = NULL;
571
572 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (length == 128)
575 arg0.integer.value = 1;
576 else if (length == 256)
577 arg0.integer.value = 2;
578 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Patrick Mochel90130262006-05-19 16:54:48 -0400581 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200585 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (obj && obj->type == ACPI_TYPE_BUFFER)
588 *edid = obj;
589 else {
Len Brown64684632006-06-26 23:41:38 -0400590 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 status = -EFAULT;
592 kfree(obj);
593 }
594
Patrick Mocheld550d982006-06-27 00:41:40 -0400595 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/* bus */
599
600static int
Len Brown4be44fc2005-08-05 00:44:28 -0400601acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Len Brown4be44fc2005-08-05 00:44:28 -0400603 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400604 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400605 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
606 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 arg0.integer.value = option;
610
Patrick Mochel90130262006-05-19 16:54:48 -0400611 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400613 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400619acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 int status;
622
Patrick Mochel90130262006-05-19 16:54:48 -0400623 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static int
Len Brown4be44fc2005-08-05 00:44:28 -0400629acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400630 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Len Brown4be44fc2005-08-05 00:44:28 -0400632 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Patrick Mochel90130262006-05-19 16:54:48 -0400634 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *options &= 3;
636
Patrick Mocheld550d982006-06-27 00:41:40 -0400637 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640/*
641 * Arg:
642 * video : video bus device pointer
643 * bios_flag :
644 * 0. The system BIOS should NOT automatically switch(toggle)
645 * the active display output.
646 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100647 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * 2. The _DGS value should be locked.
649 * 3. The system BIOS should not automatically switch (toggle) the
650 * active display output, but instead generate the display switch
651 * event notify code.
652 * lcd_flag :
653 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100654 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100656 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Return Value:
658 * -1 wrong arg.
659 */
660
661static int
Len Brown4be44fc2005-08-05 00:44:28 -0400662acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Len Brown4be44fc2005-08-05 00:44:28 -0400664 acpi_integer status = 0;
665 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
666 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Len Brown4be44fc2005-08-05 00:44:28 -0400669 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 status = -1;
671 goto Failed;
672 }
673 arg0.integer.value = (lcd_flag << 2) | bios_flag;
674 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400675 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Len Brown4be44fc2005-08-05 00:44:28 -0400677 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500682 * Simple comparison function used to sort backlight levels.
683 */
684
685static int
686acpi_video_cmp_level(const void *a, const void *b)
687{
688 return *(int *)a - *(int *)b;
689}
690
691/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * Arg:
693 * device : video output device (LCD, CRT, ..)
694 *
695 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100696 * Maximum brightness level
697 *
698 * Allocate and initialize device->brightness.
699 */
700
701static int
702acpi_video_init_brightness(struct acpi_video_device *device)
703{
704 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800705 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800706 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100707 union acpi_object *o;
708 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800709 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100710
711 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
712 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
713 "LCD brightness level\n"));
714 goto out;
715 }
716
717 if (obj->package.count < 2)
718 goto out;
719
720 br = kzalloc(sizeof(*br), GFP_KERNEL);
721 if (!br) {
722 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800723 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100724 goto out;
725 }
726
Zhang Ruid32f6942009-03-18 16:27:12 +0800727 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100728 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800729 if (!br->levels) {
730 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100731 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800732 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100733
734 for (i = 0; i < obj->package.count; i++) {
735 o = (union acpi_object *)&obj->package.elements[i];
736 if (o->type != ACPI_TYPE_INTEGER) {
737 printk(KERN_ERR PREFIX "Invalid data\n");
738 continue;
739 }
740 br->levels[count] = (u32) o->integer.value;
741
742 if (br->levels[count] > max_level)
743 max_level = br->levels[count];
744 count++;
745 }
746
Zhang Ruid32f6942009-03-18 16:27:12 +0800747 /*
748 * some buggy BIOS don't export the levels
749 * when machine is on AC/Battery in _BCL package.
750 * In this case, the first two elements in _BCL packages
751 * are also supported brightness levels that OS should take care of.
752 */
753 for (i = 2; i < count; i++)
754 if (br->levels[i] == br->levels[0] ||
755 br->levels[i] == br->levels[1])
756 level_ac_battery++;
757
758 if (level_ac_battery < 2) {
759 level_ac_battery = 2 - level_ac_battery;
760 br->flags._BCL_no_ac_battery_levels = 1;
761 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
762 br->levels[i] = br->levels[i - level_ac_battery];
763 count += level_ac_battery;
764 } else if (level_ac_battery > 2)
765 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
766
Zhang Ruid80fb992009-03-18 16:27:14 +0800767 /* Check if the _BCL package is in a reversed order */
768 if (max_level == br->levels[2]) {
769 br->flags._BCL_reversed = 1;
770 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
771 acpi_video_cmp_level, NULL);
772 } else if (max_level != br->levels[count - 1])
773 ACPI_ERROR((AE_INFO,
774 "Found unordered _BCL package\n"));
Zhang Rui935e5f22008-12-11 16:24:52 -0500775
Julia Jomantaite469778c2008-06-23 22:50:42 +0100776 br->count = count;
777 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800778
779 /* Check the input/output of _BQC/_BCL/_BCM */
780 if ((max_level < 100) && (max_level <= (count - 2)))
781 br->flags._BCL_use_index = 1;
782
783 /*
784 * _BCM is always consistent with _BCL,
785 * at least for all the laptops we have ever seen.
786 */
787 br->flags._BCM_use_index = br->flags._BCL_use_index;
788
789 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
790 br->curr = max_level;
791 result = acpi_video_device_lcd_get_level_current(device, &level_old);
792 if (result)
793 goto out_free_levels;
794
795 result = acpi_video_device_lcd_set_level(device, br->curr);
796 if (result)
797 goto out_free_levels;
798
799 result = acpi_video_device_lcd_get_level_current(device, &level);
800 if (result)
801 goto out_free_levels;
802
803 if ((level != level_old) && !br->flags._BCM_use_index) {
804 /* Note:
805 * This piece of code does not work correctly if the current
806 * brightness levels is 0.
807 * But I guess boxes that boot with such a dark screen are rare
808 * and no more code is needed to cover this specifial case.
809 */
810
811 if (level_ac_battery != 2) {
812 /*
813 * For now, we don't support the _BCL like this:
814 * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
815 * because we may mess up the index returned by _BQC.
816 * Plus: we have not got a box like this.
817 */
818 ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
819 }
820 br->flags._BQC_use_index = 1;
821 }
822
Zhang Ruid32f6942009-03-18 16:27:12 +0800823 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
824 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100825 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800826 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100827
828out_free_levels:
829 kfree(br->levels);
830out_free:
831 kfree(br);
832out:
833 device->brightness = NULL;
834 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800835 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100836}
837
838/*
839 * Arg:
840 * device : video output device (LCD, CRT, ..)
841 *
842 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * None
844 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100845 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * device.
847 */
848
Len Brown4be44fc2005-08-05 00:44:28 -0400849static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
William Lee Irwin III98934de2007-12-12 03:56:55 -0800854 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Patrick Mochel90130262006-05-19 16:54:48 -0400856 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 device->cap._ADR = 1;
858 }
Patrick Mochel90130262006-05-19 16:54:48 -0400859 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400860 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Patrick Mochel90130262006-05-19 16:54:48 -0400862 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400863 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800865 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
866 device->cap._BQC = 1;
Zhang Ruic60d6382009-03-18 16:27:18 +0800867 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
868 &h_dummy1))) {
869 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
870 device->cap._BCQ = 1;
871 }
872
Patrick Mochel90130262006-05-19 16:54:48 -0400873 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400874 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Patrick Mochel90130262006-05-19 16:54:48 -0400876 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 device->cap._DCS = 1;
878 }
Patrick Mochel90130262006-05-19 16:54:48 -0400879 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 device->cap._DGS = 1;
881 }
Patrick Mochel90130262006-05-19 16:54:48 -0400882 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 device->cap._DSS = 1;
884 }
885
Zhang Rui1a7c6182009-03-18 16:27:16 +0800886 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800887 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800888 static int count = 0;
889 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800890
891 result = acpi_video_init_brightness(device);
892 if (result)
893 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800894 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
895 if (!name)
896 return;
897
Yu Luming2f3d0002006-11-11 02:40:34 +0800898 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800899 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000900 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000901 device->backlight->props.max_brightness = device->brightness->count-3;
Yu Luming2f3d0002006-11-11 02:40:34 +0800902 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800903
904 device->cdev = thermal_cooling_device_register("LCD",
905 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500906 if (IS_ERR(device->cdev))
907 return;
908
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200909 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
910 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800911 result = sysfs_create_link(&device->dev->dev.kobj,
912 &device->cdev->device.kobj,
913 "thermal_cooling");
914 if (result)
915 printk(KERN_ERR PREFIX "Create sysfs link\n");
916 result = sysfs_create_link(&device->cdev->device.kobj,
917 &device->dev->dev.kobj, "device");
918 if (result)
919 printk(KERN_ERR PREFIX "Create sysfs link\n");
920
Yu Luming2f3d0002006-11-11 02:40:34 +0800921 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200922
923 if (acpi_video_display_switch_support()) {
924
925 if (device->cap._DCS && device->cap._DSS) {
926 static int count;
927 char *name;
928 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
929 if (!name)
930 return;
931 sprintf(name, "acpi_video%d", count++);
932 device->output_dev = video_output_register(name,
933 NULL, device, &acpi_output_properties);
934 kfree(name);
935 }
Luming Yu23b0f012007-05-09 21:07:05 +0800936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*
940 * Arg:
941 * device : video output device (VGA)
942 *
943 * Return Value:
944 * None
945 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100946 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
948
Len Brown4be44fc2005-08-05 00:44:28 -0400949static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Len Brown4be44fc2005-08-05 00:44:28 -0400951 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
William Lee Irwin III98934de2007-12-12 03:56:55 -0800953 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400954 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 video->cap._DOS = 1;
956 }
Patrick Mochel90130262006-05-19 16:54:48 -0400957 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 video->cap._DOD = 1;
959 }
Patrick Mochel90130262006-05-19 16:54:48 -0400960 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 video->cap._ROM = 1;
962 }
Patrick Mochel90130262006-05-19 16:54:48 -0400963 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 video->cap._GPD = 1;
965 }
Patrick Mochel90130262006-05-19 16:54:48 -0400966 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 video->cap._SPD = 1;
968 }
Patrick Mochel90130262006-05-19 16:54:48 -0400969 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 video->cap._VPO = 1;
971 }
972}
973
974/*
975 * Check whether the video bus device has required AML method to
976 * support the desired features
977 */
978
Len Brown4be44fc2005-08-05 00:44:28 -0400979static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Len Brown4be44fc2005-08-05 00:44:28 -0400981 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200982 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Thomas Renninger22c13f92008-08-01 17:37:54 +0200987 dev = acpi_get_physical_pci_device(video->device->handle);
988 if (!dev)
989 return -ENODEV;
990 put_device(dev);
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Since there is no HID, CID and so on for VGA driver, we have
993 * to check well known required nodes.
994 */
995
Julius Volz98fb8fe2007-02-20 16:38:40 +0100996 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400997 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 video->flags.multihead = 1;
999 status = 0;
1000 }
1001
Julius Volz98fb8fe2007-02-20 16:38:40 +01001002 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -04001003 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 video->flags.rom = 1;
1005 status = 0;
1006 }
1007
Julius Volz98fb8fe2007-02-20 16:38:40 +01001008 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001009 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 video->flags.post = 1;
1011 status = 0;
1012 }
1013
Patrick Mocheld550d982006-06-27 00:41:40 -04001014 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/* --------------------------------------------------------------------------
1018 FS Interface (/proc)
1019 -------------------------------------------------------------------------- */
1020
Len Brown4be44fc2005-08-05 00:44:28 -04001021static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023/* video devices */
1024
Len Brown4be44fc2005-08-05 00:44:28 -04001025static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001027 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (!dev)
1031 goto end;
1032
1033 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1034 seq_printf(seq, "type: ");
1035 if (dev->flags.crt)
1036 seq_printf(seq, "CRT\n");
1037 else if (dev->flags.lcd)
1038 seq_printf(seq, "LCD\n");
1039 else if (dev->flags.tvout)
1040 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001041 else if (dev->flags.dvi)
1042 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 else
1044 seq_printf(seq, "UNKNOWN\n");
1045
Len Brown4be44fc2005-08-05 00:44:28 -04001046 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Len Brown4be44fc2005-08-05 00:44:28 -04001048 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static int
Len Brown4be44fc2005-08-05 00:44:28 -04001053acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 return single_open(file, acpi_video_device_info_seq_show,
1056 PDE(inode)->data);
1057}
1058
Len Brown4be44fc2005-08-05 00:44:28 -04001059static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Len Brown4be44fc2005-08-05 00:44:28 -04001061 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001062 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001063 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (!dev)
1067 goto end;
1068
1069 status = acpi_video_device_get_state(dev, &state);
1070 seq_printf(seq, "state: ");
1071 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001072 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 else
1074 seq_printf(seq, "<not supported>\n");
1075
1076 status = acpi_video_device_query(dev, &state);
1077 seq_printf(seq, "query: ");
1078 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001079 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 else
1081 seq_printf(seq, "<not supported>\n");
1082
Len Brown4be44fc2005-08-05 00:44:28 -04001083 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087static int
Len Brown4be44fc2005-08-05 00:44:28 -04001088acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 return single_open(file, acpi_video_device_state_seq_show,
1091 PDE(inode)->data);
1092}
1093
1094static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001095acpi_video_device_write_state(struct file *file,
1096 const char __user * buffer,
1097 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Len Brown4be44fc2005-08-05 00:44:28 -04001099 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001100 struct seq_file *m = file->private_data;
1101 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001102 char str[12] = { 0 };
1103 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001107 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 str[count] = 0;
1113 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001114 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 status = acpi_video_device_set_state(dev, state);
1117
1118 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001119 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Patrick Mocheld550d982006-06-27 00:41:40 -04001121 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124static int
Len Brown4be44fc2005-08-05 00:44:28 -04001125acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001127 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001128 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (!dev || !dev->brightness) {
1132 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001137 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 seq_printf(seq, " %d", dev->brightness->levels[i]);
1139 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1140
Patrick Mocheld550d982006-06-27 00:41:40 -04001141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static int
Len Brown4be44fc2005-08-05 00:44:28 -04001145acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 return single_open(file, acpi_video_device_brightness_seq_show,
1148 PDE(inode)->data);
1149}
1150
1151static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001152acpi_video_device_write_brightness(struct file *file,
1153 const char __user * buffer,
1154 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001156 struct seq_file *m = file->private_data;
1157 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001158 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001159 unsigned int level = 0;
1160 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Thomas Renninger59d399d2005-11-08 05:27:00 -05001163 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001167 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 str[count] = 0;
1170 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Julius Volz98fb8fe2007-02-20 16:38:40 +01001175 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001176 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001178 if (!acpi_video_device_lcd_set_level(dev, level))
1179 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 }
1182
Zhang Rui24450c72009-03-18 16:27:10 +08001183 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Len Brown4be44fc2005-08-05 00:44:28 -04001186static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001188 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001189 int status;
1190 int i;
1191 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (!dev)
1195 goto out;
1196
Len Brown4be44fc2005-08-05 00:44:28 -04001197 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001199 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202 if (ACPI_FAILURE(status)) {
1203 goto out;
1204 }
1205
1206 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1207 for (i = 0; i < edid->buffer.length; i++)
1208 seq_putc(seq, edid->buffer.pointer[i]);
1209 }
1210
Len Brown4be44fc2005-08-05 00:44:28 -04001211 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!edid)
1213 seq_printf(seq, "<not supported>\n");
1214 else
1215 kfree(edid);
1216
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static int
Len Brown4be44fc2005-08-05 00:44:28 -04001221acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 return single_open(file, acpi_video_device_EDID_seq_show,
1224 PDE(inode)->data);
1225}
1226
Len Brown4be44fc2005-08-05 00:44:28 -04001227static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001229 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct acpi_video_device *vid_dev;
1231
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001232 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001236 device_dir = proc_mkdir(acpi_device_bid(device),
1237 vid_dev->video->dir);
1238 if (!device_dir)
1239 return -ENOMEM;
1240
1241 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001244 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001245 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001247 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001250 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1251 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001252 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001253 &acpi_video_device_state_fops,
1254 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001256 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001259 acpi_video_device_brightness_fops.write =
1260 acpi_video_device_write_brightness;
1261 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001262 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001263 &acpi_video_device_brightness_fops,
1264 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001266 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001269 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001270 &acpi_video_device_EDID_fops,
1271 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001273 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001275 acpi_device_dir(device) = device_dir;
1276
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001278
1279 err_remove_brightness:
1280 remove_proc_entry("brightness", device_dir);
1281 err_remove_state:
1282 remove_proc_entry("state", device_dir);
1283 err_remove_info:
1284 remove_proc_entry("info", device_dir);
1285 err_remove_dir:
1286 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Len Brown4be44fc2005-08-05 00:44:28 -04001290static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001293 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001295 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001297 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001299 device_dir = acpi_device_dir(device);
1300 if (device_dir) {
1301 remove_proc_entry("info", device_dir);
1302 remove_proc_entry("state", device_dir);
1303 remove_proc_entry("brightness", device_dir);
1304 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001305 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 acpi_device_dir(device) = NULL;
1307 }
1308
Patrick Mocheld550d982006-06-27 00:41:40 -04001309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001313static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001315 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (!video)
1319 goto end;
1320
1321 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001322 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001324 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001326 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Len Brown4be44fc2005-08-05 00:44:28 -04001328 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Len Brown4be44fc2005-08-05 00:44:28 -04001332static int acpi_video_bus_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_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_ROM_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (!video)
1344 goto end;
1345
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001346 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 seq_printf(seq, "<TODO>\n");
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Len Brown4be44fc2005-08-05 00:44:28 -04001353static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1356}
1357
Len Brown4be44fc2005-08-05 00:44:28 -04001358static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001360 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001361 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001362 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (!video)
1366 goto end;
1367
1368 status = acpi_video_bus_POST_options(video, &options);
1369 if (ACPI_SUCCESS(status)) {
1370 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001371 printk(KERN_WARNING PREFIX
1372 "The motherboard VGA device is not listed as a possible POST device.\n");
1373 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001374 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Frank Seidel4d939152009-02-04 17:03:07 +01001376 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001377 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (options & 2)
1379 seq_printf(seq, " <PCI video>");
1380 if (options & 4)
1381 seq_printf(seq, " <AGP video>");
1382 seq_putc(seq, '\n');
1383 } else
1384 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001385 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389static int
Len Brown4be44fc2005-08-05 00:44:28 -04001390acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Len Brown4be44fc2005-08-05 00:44:28 -04001392 return single_open(file, acpi_video_bus_POST_info_seq_show,
1393 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Len Brown4be44fc2005-08-05 00:44:28 -04001396static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001398 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001399 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001400 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 if (!video)
1404 goto end;
1405
Len Brown4be44fc2005-08-05 00:44:28 -04001406 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (!ACPI_SUCCESS(status)) {
1408 seq_printf(seq, "<not supported>\n");
1409 goto end;
1410 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001411 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Len Brown4be44fc2005-08-05 00:44:28 -04001413 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Len Brown4be44fc2005-08-05 00:44:28 -04001417static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001419 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Len Brown4be44fc2005-08-05 00:44:28 -04001422 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Patrick Mocheld550d982006-06-27 00:41:40 -04001424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Len Brown4be44fc2005-08-05 00:44:28 -04001427static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Len Brown4be44fc2005-08-05 00:44:28 -04001429 return single_open(file, acpi_video_bus_POST_seq_show,
1430 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Len Brown4be44fc2005-08-05 00:44:28 -04001433static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1436}
1437
1438static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001439acpi_video_bus_write_POST(struct file *file,
1440 const char __user * buffer,
1441 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Len Brown4be44fc2005-08-05 00:44:28 -04001443 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001444 struct seq_file *m = file->private_data;
1445 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001446 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001447 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 status = acpi_video_bus_POST_options(video, &options);
1454 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001458 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 str[count] = 0;
1461 opt = strtoul(str, NULL, 0);
1462 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001463 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Julius Volz98fb8fe2007-02-20 16:38:40 +01001465 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 options |= 1;
1467
1468 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001469 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001471 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 }
1474
Patrick Mocheld550d982006-06-27 00:41:40 -04001475 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001479acpi_video_bus_write_DOS(struct file *file,
1480 const char __user * buffer,
1481 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Len Brown4be44fc2005-08-05 00:44:28 -04001483 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001484 struct seq_file *m = file->private_data;
1485 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001486 char str[12] = { 0 };
1487 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001494 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 str[count] = 0;
1497 opt = strtoul(str, NULL, 0);
1498 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001499 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Len Brown4be44fc2005-08-05 00:44:28 -04001501 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001504 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Patrick Mocheld550d982006-06-27 00:41:40 -04001506 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Len Brown4be44fc2005-08-05 00:44:28 -04001509static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001511 struct acpi_video_bus *video = acpi_driver_data(device);
1512 struct proc_dir_entry *device_dir;
1513 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001515 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1516 if (!device_dir)
1517 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001519 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001522 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001523 &acpi_video_bus_info_fops,
1524 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001526 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001529 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001530 &acpi_video_bus_ROM_fops,
1531 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001533 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001536 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001537 &acpi_video_bus_POST_info_fops,
1538 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001540 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001543 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001544 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001545 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001546 &acpi_video_bus_POST_fops,
1547 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001549 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001552 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001553 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001554 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001555 &acpi_video_bus_DOS_fops,
1556 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001558 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001560 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001561 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001562
1563 err_remove_post:
1564 remove_proc_entry("POST", device_dir);
1565 err_remove_post_info:
1566 remove_proc_entry("POST_info", device_dir);
1567 err_remove_rom:
1568 remove_proc_entry("ROM", device_dir);
1569 err_remove_info:
1570 remove_proc_entry("info", device_dir);
1571 err_remove_dir:
1572 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1573 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Len Brown4be44fc2005-08-05 00:44:28 -04001576static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001578 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001580 if (device_dir) {
1581 remove_proc_entry("info", device_dir);
1582 remove_proc_entry("ROM", device_dir);
1583 remove_proc_entry("POST_info", device_dir);
1584 remove_proc_entry("POST", device_dir);
1585 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001586 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 acpi_device_dir(device) = NULL;
1588 }
1589
Patrick Mocheld550d982006-06-27 00:41:40 -04001590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593/* --------------------------------------------------------------------------
1594 Driver Interface
1595 -------------------------------------------------------------------------- */
1596
1597/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001598static struct acpi_video_device_attrib*
1599acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1600{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001601 struct acpi_video_enumerated_device *ids;
1602 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001603
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001604 for (i = 0; i < video->attached_count; i++) {
1605 ids = &video->attached_array[i];
1606 if ((ids->value.int_val & 0xffff) == device_id)
1607 return &ids->value.attrib;
1608 }
1609
Rui Zhang82cae992007-01-03 23:40:53 -05001610 return NULL;
1611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613static int
Len Brown4be44fc2005-08-05 00:44:28 -04001614acpi_video_bus_get_one_device(struct acpi_device *device,
1615 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001617 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001618 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001619 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001620 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001623 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Len Brown4be44fc2005-08-05 00:44:28 -04001625 status =
1626 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (ACPI_SUCCESS(status)) {
1628
Burman Yan36bcbec2006-12-19 12:56:11 -08001629 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001631 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1634 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001635 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 data->device_id = device_id;
1638 data->video = video;
1639 data->dev = device;
1640
Rui Zhang82cae992007-01-03 23:40:53 -05001641 attribute = acpi_video_get_device_attr(video, device_id);
1642
1643 if((attribute != NULL) && attribute->device_id_scheme) {
1644 switch (attribute->display_type) {
1645 case ACPI_VIDEO_DISPLAY_CRT:
1646 data->flags.crt = 1;
1647 break;
1648 case ACPI_VIDEO_DISPLAY_TV:
1649 data->flags.tvout = 1;
1650 break;
1651 case ACPI_VIDEO_DISPLAY_DVI:
1652 data->flags.dvi = 1;
1653 break;
1654 case ACPI_VIDEO_DISPLAY_LCD:
1655 data->flags.lcd = 1;
1656 break;
1657 default:
1658 data->flags.unknown = 1;
1659 break;
1660 }
1661 if(attribute->bios_can_detect)
1662 data->flags.bios = 1;
1663 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 acpi_video_device_bind(video, data);
1667 acpi_video_device_find_cap(data);
1668
Patrick Mochel90130262006-05-19 16:54:48 -04001669 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001670 ACPI_DEVICE_NOTIFY,
1671 acpi_video_device_notify,
1672 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001674 printk(KERN_ERR PREFIX
1675 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001676 if(data->brightness)
1677 kfree(data->brightness->levels);
1678 kfree(data->brightness);
1679 kfree(data);
1680 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001683 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001685 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 acpi_video_device_add_fs(device);
1688
Patrick Mocheld550d982006-06-27 00:41:40 -04001689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
Patrick Mocheld550d982006-06-27 00:41:40 -04001692 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695/*
1696 * Arg:
1697 * video : video bus device
1698 *
1699 * Return:
1700 * none
1701 *
1702 * Enumerate the video device list of the video bus,
1703 * bind the ids with the corresponding video devices
1704 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Len Brown4be44fc2005-08-05 00:44:28 -04001707static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001709 struct acpi_video_device *dev;
1710
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001711 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001712
1713 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001714 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001715
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001716 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719/*
1720 * Arg:
1721 * video : video bus device
1722 * device : video output device under the video
1723 * bus
1724 *
1725 * Return:
1726 * none
1727 *
1728 * Bind the ids with the corresponding video devices
1729 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732static void
Len Brown4be44fc2005-08-05 00:44:28 -04001733acpi_video_device_bind(struct acpi_video_bus *video,
1734 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001736 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001737 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001739 for (i = 0; i < video->attached_count; i++) {
1740 ids = &video->attached_array[i];
1741 if (device->device_id == (ids->value.int_val & 0xffff)) {
1742 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1744 }
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748/*
1749 * Arg:
1750 * video : video bus device
1751 *
1752 * Return:
1753 * < 0 : error
1754 *
1755 * Call _DOD to enumerate all devices attached to display adapter
1756 *
Len Brown4be44fc2005-08-05 00:44:28 -04001757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1760{
Len Brown4be44fc2005-08-05 00:44:28 -04001761 int status;
1762 int count;
1763 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001764 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001765 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1766 union acpi_object *dod = NULL;
1767 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Patrick Mochel90130262006-05-19 16:54:48 -04001769 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001771 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001772 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001775 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001777 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 status = -EFAULT;
1779 goto out;
1780 }
1781
1782 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001783 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001785 active_list = kcalloc(1 + dod->package.count,
1786 sizeof(struct acpi_video_enumerated_device),
1787 GFP_KERNEL);
1788 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 status = -ENOMEM;
1790 goto out;
1791 }
1792
1793 count = 0;
1794 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001795 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001798 printk(KERN_ERR PREFIX
1799 "Invalid _DOD data in element %d\n", i);
1800 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001802
1803 active_list[count].value.int_val = obj->integer.value;
1804 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001805 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1806 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 count++;
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jesper Juhl6044ec82005-11-07 01:01:32 -08001810 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001811
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001812 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001814
1815 out:
Len Brown02438d82006-06-30 03:19:10 -04001816 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001817 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Len Brown4be44fc2005-08-05 00:44:28 -04001820static int
1821acpi_video_get_next_level(struct acpi_video_device *device,
1822 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001824 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001825 max = max_below = 0;
1826 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001827 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001828 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001829 l = device->brightness->levels[i];
1830 if (abs(l - level_current) < abs(delta)) {
1831 delta = l - level_current;
1832 if (!delta)
1833 break;
1834 }
1835 }
1836 /* Ajust level_current to closest available level */
1837 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001838 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001839 l = device->brightness->levels[i];
1840 if (l < min)
1841 min = l;
1842 if (l > max)
1843 max = l;
1844 if (l < min_above && l > level_current)
1845 min_above = l;
1846 if (l > max_below && l < level_current)
1847 max_below = l;
1848 }
1849
1850 switch (event) {
1851 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1852 return (level_current < max) ? min_above : min;
1853 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1854 return (level_current < max) ? min_above : max;
1855 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1856 return (level_current > min) ? max_below : min;
1857 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1858 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1859 return 0;
1860 default:
1861 return level_current;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Zhang Ruic8890f92009-03-18 16:27:08 +08001865static int
Len Brown4be44fc2005-08-05 00:44:28 -04001866acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001868 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001869 int result = -EINVAL;
1870
Julia Jomantaite469778c2008-06-23 22:50:42 +01001871 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001872 goto out;
1873
1874 result = acpi_video_device_lcd_get_level_current(device,
1875 &level_current);
1876 if (result)
1877 goto out;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001880
Zhang Rui24450c72009-03-18 16:27:10 +08001881 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001882
1883out:
1884 if (result)
1885 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1886
1887 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
1890static int
Len Brown4be44fc2005-08-05 00:44:28 -04001891acpi_video_bus_get_devices(struct acpi_video_bus *video,
1892 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Len Brown4be44fc2005-08-05 00:44:28 -04001894 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001895 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 acpi_video_device_enumerate(video);
1898
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001899 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 status = acpi_video_bus_get_one_device(dev, video);
1902 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001903 printk(KERN_WARNING PREFIX
1904 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 continue;
1906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001908 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Len Brown4be44fc2005-08-05 00:44:28 -04001911static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Karol Kozimor031ec772005-07-30 04:18:00 -04001913 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct acpi_video_bus *video;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001918 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 video = device->video;
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 acpi_video_device_remove_fs(device->dev);
1923
Patrick Mochel90130262006-05-19 16:54:48 -04001924 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001925 ACPI_DEVICE_NOTIFY,
1926 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001927 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001928 if (device->cdev) {
1929 sysfs_remove_link(&device->dev->dev.kobj,
1930 "thermal_cooling");
1931 sysfs_remove_link(&device->cdev->device.kobj,
1932 "device");
1933 thermal_cooling_device_unregister(device->cdev);
1934 device->cdev = NULL;
1935 }
Luming Yu23b0f012007-05-09 21:07:05 +08001936 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001937
Patrick Mocheld550d982006-06-27 00:41:40 -04001938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Len Brown4be44fc2005-08-05 00:44:28 -04001941static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Len Brown4be44fc2005-08-05 00:44:28 -04001943 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001944 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001946 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001948 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001950 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001951 if (ACPI_FAILURE(status))
1952 printk(KERN_WARNING PREFIX
1953 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001955 if (dev->brightness) {
1956 kfree(dev->brightness->levels);
1957 kfree(dev->brightness);
1958 }
1959 list_del(&dev->entry);
1960 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001963 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001964
Patrick Mocheld550d982006-06-27 00:41:40 -04001965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/* acpi_video interface */
1969
Len Brown4be44fc2005-08-05 00:44:28 -04001970static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Zhang Ruia21101c2007-09-14 11:46:22 +08001972 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Len Brown4be44fc2005-08-05 00:44:28 -04001975static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 return acpi_video_bus_DOS(video, 0, 1);
1978}
1979
Len Brown4be44fc2005-08-05 00:44:28 -04001980static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001982 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001983 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001984 struct input_dev *input;
1985 int keycode;
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (!video)
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;
Luming Yue9dab192007-08-20 18:23:53 +08001991 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001994 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001996 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001997 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
1999
Julius Volz98fb8fe2007-02-20 16:38:40 +01002000 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * connector. */
2002 acpi_video_device_enumerate(video);
2003 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04002004 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002005 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007
Len Brown4be44fc2005-08-05 00:44:28 -04002008 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002009 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002010 keycode = KEY_SWITCHVIDEOMODE;
2011 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002012 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002013 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002014 keycode = KEY_VIDEO_NEXT;
2015 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002016 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04002017 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002018 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 break;
2020
2021 default:
Luming Yue9dab192007-08-20 18:23:53 +08002022 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002024 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 break;
2026 }
2027
Zhang Rui7761f632008-01-25 14:48:12 +08002028 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002029 input_report_key(input, keycode, 1);
2030 input_sync(input);
2031 input_report_key(input, keycode, 0);
2032 input_sync(input);
2033
Patrick Mocheld550d982006-06-27 00:41:40 -04002034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Len Brown4be44fc2005-08-05 00:44:28 -04002037static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002039 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002040 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002041 struct acpi_video_bus *bus;
2042 struct input_dev *input;
2043 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002048 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002049 bus = video_device->video;
2050 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002053 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002054 if (brightness_switch_enabled)
2055 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002056 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002057 keycode = KEY_BRIGHTNESS_CYCLE;
2058 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002059 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002060 if (brightness_switch_enabled)
2061 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002062 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002063 keycode = KEY_BRIGHTNESSUP;
2064 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002065 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002066 if (brightness_switch_enabled)
2067 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002068 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002069 keycode = KEY_BRIGHTNESSDOWN;
2070 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002071 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002072 if (brightness_switch_enabled)
2073 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002074 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002075 keycode = KEY_BRIGHTNESS_ZERO;
2076 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002077 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002078 if (brightness_switch_enabled)
2079 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002080 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002081 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 break;
2083 default:
Luming Yue9dab192007-08-20 18:23:53 +08002084 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002086 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088 }
Luming Yue9dab192007-08-20 18:23:53 +08002089
Zhang Rui7761f632008-01-25 14:48:12 +08002090 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002091 input_report_key(input, keycode, 1);
2092 input_sync(input);
2093 input_report_key(input, keycode, 0);
2094 input_sync(input);
2095
Patrick Mocheld550d982006-06-27 00:41:40 -04002096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
Zhang Ruie6d9da12007-08-25 02:23:31 -04002099static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002100static int acpi_video_resume(struct acpi_device *device)
2101{
2102 struct acpi_video_bus *video;
2103 struct acpi_video_device *video_device;
2104 int i;
2105
2106 if (!device || !acpi_driver_data(device))
2107 return -EINVAL;
2108
2109 video = acpi_driver_data(device);
2110
2111 for (i = 0; i < video->attached_count; i++) {
2112 video_device = video->attached_array[i].bind_info;
2113 if (video_device && video_device->backlight)
2114 acpi_video_set_brightness(video_device->backlight);
2115 }
2116 return AE_OK;
2117}
2118
Len Brown4be44fc2005-08-05 00:44:28 -04002119static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002121 acpi_status status;
2122 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002123 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002124 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Burman Yan36bcbec2006-12-19 12:56:11 -08002126 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002128 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Zhang Ruie6d9da12007-08-25 02:23:31 -04002130 /* a hack to fix the duplicate name "VID" problem on T61 */
2131 if (!strcmp(device->pnp.bus_id, "VID")) {
2132 if (instance)
2133 device->pnp.bus_id[3] = '0' + instance;
2134 instance ++;
2135 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002136 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2137 if (!strcmp(device->pnp.bus_id, "VGA")) {
2138 if (instance)
2139 device->pnp.bus_id[3] = '0' + instance;
2140 instance++;
2141 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002142
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002143 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2145 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002146 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002149 error = acpi_video_bus_check(video);
2150 if (error)
2151 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002153 error = acpi_video_bus_add_fs(device);
2154 if (error)
2155 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002157 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 INIT_LIST_HEAD(&video->video_device_list);
2159
2160 acpi_video_bus_get_devices(video, device);
2161 acpi_video_bus_start_devices(video);
2162
Patrick Mochel90130262006-05-19 16:54:48 -04002163 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002164 ACPI_DEVICE_NOTIFY,
2165 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002167 printk(KERN_ERR PREFIX
2168 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002169 error = -ENODEV;
2170 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172
Luming Yue9dab192007-08-20 18:23:53 +08002173 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002174 if (!input) {
2175 error = -ENOMEM;
2176 goto err_uninstall_notify;
2177 }
Luming Yue9dab192007-08-20 18:23:53 +08002178
2179 snprintf(video->phys, sizeof(video->phys),
2180 "%s/video/input0", acpi_device_hid(video->device));
2181
2182 input->name = acpi_device_name(video->device);
2183 input->phys = video->phys;
2184 input->id.bustype = BUS_HOST;
2185 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002186 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002187 input->evbit[0] = BIT(EV_KEY);
2188 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2189 set_bit(KEY_VIDEO_NEXT, input->keybit);
2190 set_bit(KEY_VIDEO_PREV, input->keybit);
2191 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2192 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2193 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2194 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2195 set_bit(KEY_DISPLAY_OFF, input->keybit);
2196 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002197
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002198 error = input_register_device(input);
2199 if (error)
2200 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002203 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2204 video->flags.multihead ? "yes" : "no",
2205 video->flags.rom ? "yes" : "no",
2206 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002210 err_free_input_dev:
2211 input_free_device(input);
2212 err_uninstall_notify:
2213 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2214 acpi_video_bus_notify);
2215 err_stop_video:
2216 acpi_video_bus_stop_devices(video);
2217 acpi_video_bus_put_devices(video);
2218 kfree(video->attached_array);
2219 acpi_video_bus_remove_fs(device);
2220 err_free_video:
2221 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002222 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002223
2224 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Len Brown4be44fc2005-08-05 00:44:28 -04002227static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Len Brown4be44fc2005-08-05 00:44:28 -04002229 acpi_status status = 0;
2230 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002236 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 acpi_video_bus_stop_devices(video);
2239
Patrick Mochel90130262006-05-19 16:54:48 -04002240 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002241 ACPI_DEVICE_NOTIFY,
2242 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 acpi_video_bus_put_devices(video);
2245 acpi_video_bus_remove_fs(device);
2246
Luming Yue9dab192007-08-20 18:23:53 +08002247 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002248 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 kfree(video);
2250
Patrick Mocheld550d982006-06-27 00:41:40 -04002251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Len Brown4be44fc2005-08-05 00:44:28 -04002254static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Len Brown4be44fc2005-08-05 00:44:28 -04002256 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2259 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002260 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 acpi_video_dir->owner = THIS_MODULE;
2262
2263 result = acpi_bus_register_driver(&acpi_video_bus);
2264 if (result < 0) {
2265 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
Patrick Mocheld550d982006-06-27 00:41:40 -04002269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Len Brown4be44fc2005-08-05 00:44:28 -04002272static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 acpi_bus_unregister_driver(&acpi_video_bus);
2276
2277 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2278
Patrick Mocheld550d982006-06-27 00:41:40 -04002279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282module_init(acpi_video_init);
2283module_exit(acpi_video_exit);