blob: 1f1625c5100302507d3ffe111ff4cf4b83be0aa4 [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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
Zhang Ruid32f6942009-03-18 16:27:12 +0800171struct acpi_video_brightness_flags {
172 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
Zhang Ruid80fb992009-03-18 16:27:14 +0800173 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
Zhang Rui1a7c6182009-03-18 16:27:16 +0800174 u8 _BCL_use_index:1; /* levels in _BCL are index values */
175 u8 _BCM_use_index:1; /* input of _BCM is an index value */
176 u8 _BQC_use_index:1; /* _BQC returns an index value */
Zhang Ruid32f6942009-03-18 16:27:12 +0800177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400180 int curr;
181 int count;
182 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800183 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
186struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400187 unsigned long device_id;
188 struct acpi_video_device_flags flags;
189 struct acpi_video_device_cap cap;
190 struct list_head entry;
191 struct acpi_video_bus *video;
192 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800194 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800195 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800196 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* bus */
200static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
201static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700202 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_video_bus_info_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
209static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
210static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700211 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400212 .open = acpi_video_bus_ROM_open_fs,
213 .read = seq_read,
214 .llseek = seq_lseek,
215 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Len Brown4be44fc2005-08-05 00:44:28 -0400218static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
219 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700221 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400222 .open = acpi_video_bus_POST_info_open_fs,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
229static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700230 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400231 .open = acpi_video_bus_POST_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
238static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700239 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400240 .open = acpi_video_bus_DOS_open_fs,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400247static int acpi_video_device_info_open_fs(struct inode *inode,
248 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700250 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400251 .open = acpi_video_device_info_open_fs,
252 .read = seq_read,
253 .llseek = seq_lseek,
254 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257static int acpi_video_device_state_open_fs(struct inode *inode,
258 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700260 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400261 .open = acpi_video_device_state_open_fs,
262 .read = seq_read,
263 .llseek = seq_lseek,
264 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Len Brown4be44fc2005-08-05 00:44:28 -0400267static int acpi_video_device_brightness_open_fs(struct inode *inode,
268 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700270 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400271 .open = acpi_video_device_brightness_open_fs,
272 .read = seq_read,
273 .llseek = seq_lseek,
274 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277static int acpi_video_device_EDID_open_fs(struct inode *inode,
278 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700280 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400281 .open = acpi_video_device_EDID_open_fs,
282 .read = seq_read,
283 .llseek = seq_lseek,
284 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Len Brown4be44fc2005-08-05 00:44:28 -0400287static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "motherboard VGA device",
289 "PCI VGA device",
290 "AGP VGA device",
291 "UNKNOWN",
292};
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
295static void acpi_video_device_rebind(struct acpi_video_bus *video);
296static void acpi_video_device_bind(struct acpi_video_bus *video,
297 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800299static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
300 int level);
301static int acpi_video_device_lcd_get_level_current(
302 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400303 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400304static int acpi_video_get_next_level(struct acpi_video_device *device,
305 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800306static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400307 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800308static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400309 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800310static int acpi_video_output_get(struct output_device *od);
311static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Yu Luming2f3d0002006-11-11 02:40:34 +0800313/*backlight device sysfs support*/
314static int acpi_video_get_brightness(struct backlight_device *bd)
315{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400316 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000317 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800318 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100319 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800320
321 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
322 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000323 for (i = 2; i < vd->brightness->count; i++) {
324 if (vd->brightness->levels[i] == cur_level)
325 /* The first two entries are special - see page 575
326 of the ACPI spec 3.0 */
327 return i-2;
328 }
329 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800330}
331
332static int acpi_video_set_brightness(struct backlight_device *bd)
333{
Zhang Rui24450c72009-03-18 16:27:10 +0800334 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800335 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100336 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800337
338 return acpi_video_device_lcd_set_level(vd,
339 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800340}
341
Richard Purdie599a52d2007-02-10 23:07:48 +0000342static struct backlight_ops acpi_backlight_ops = {
343 .get_brightness = acpi_video_get_brightness,
344 .update_status = acpi_video_set_brightness,
345};
346
Luming Yu23b0f012007-05-09 21:07:05 +0800347/*video output device sysfs support*/
348static int acpi_video_output_get(struct output_device *od)
349{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400350 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800351 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700352 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800353 acpi_video_device_get_state(vd, &state);
354 return (int)state;
355}
356
357static int acpi_video_output_set(struct output_device *od)
358{
359 unsigned long state = od->request_state;
360 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700361 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800362 return acpi_video_device_set_state(vd, state);
363}
364
365static struct output_properties acpi_output_properties = {
366 .set_state = acpi_video_output_set,
367 .get_status = acpi_video_output_get,
368};
Zhang Rui702ed512008-01-17 15:51:22 +0800369
370
371/* thermal cooling device callbacks */
372static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
373{
374 struct acpi_device *device = cdev->devdata;
375 struct acpi_video_device *video = acpi_driver_data(device);
376
377 return sprintf(buf, "%d\n", video->brightness->count - 3);
378}
379
380static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
381{
382 struct acpi_device *device = cdev->devdata;
383 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400384 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800385 int state;
386
Zhang Ruic8890f92009-03-18 16:27:08 +0800387 if (acpi_video_device_lcd_get_level_current(video, &level))
388 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800389 for (state = 2; state < video->brightness->count; state++)
390 if (level == video->brightness->levels[state])
391 return sprintf(buf, "%d\n",
392 video->brightness->count - state - 1);
393
394 return -EINVAL;
395}
396
397static int
398video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
399{
400 struct acpi_device *device = cdev->devdata;
401 struct acpi_video_device *video = acpi_driver_data(device);
402 int level;
403
404 if ( state >= video->brightness->count - 2)
405 return -EINVAL;
406
407 state = video->brightness->count - state;
408 level = video->brightness->levels[state -1];
409 return acpi_video_device_lcd_set_level(video, level);
410}
411
412static struct thermal_cooling_device_ops video_cooling_ops = {
413 .get_max_state = video_get_max_state,
414 .get_cur_state = video_get_cur_state,
415 .set_cur_state = video_set_cur_state,
416};
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* --------------------------------------------------------------------------
419 Video Management
420 -------------------------------------------------------------------------- */
421
422/* device */
423
424static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400425acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Len Brown4be44fc2005-08-05 00:44:28 -0400427 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400428
429 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static int
Len Brown4be44fc2005-08-05 00:44:28 -0400435acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400436 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown4be44fc2005-08-05 00:44:28 -0400438 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Patrick Mochel90130262006-05-19 16:54:48 -0400440 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int
Len Brown4be44fc2005-08-05 00:44:28 -0400446acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Len Brown4be44fc2005-08-05 00:44:28 -0400448 int status;
449 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
450 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400451 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400455 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460static int
Len Brown4be44fc2005-08-05 00:44:28 -0400461acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
462 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Len Brown4be44fc2005-08-05 00:44:28 -0400464 int status;
465 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
466 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 *levels = NULL;
470
Patrick Mochel90130262006-05-19 16:54:48 -0400471 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400474 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500475 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400476 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 status = -EFAULT;
478 goto err;
479 }
480
481 *levels = obj;
482
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800486 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491static int
Len Brown4be44fc2005-08-05 00:44:28 -0400492acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Zhang Rui24450c72009-03-18 16:27:10 +0800494 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400495 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
496 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800497 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Zhang Rui24450c72009-03-18 16:27:10 +0800501 status = acpi_evaluate_object(device->dev->handle, "_BCM",
502 &args, NULL);
503 if (ACPI_FAILURE(status)) {
504 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
505 return -EIO;
506 }
507
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400508 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800509 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800510 if (level == device->brightness->levels[state]) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800511 if (device->backlight)
512 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800513 return 0;
514 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800515
Zhang Rui24450c72009-03-18 16:27:10 +0800516 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
517 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520static int
Len Brown4be44fc2005-08-05 00:44:28 -0400521acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400522 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Zhang Ruic8890f92009-03-18 16:27:08 +0800524 acpi_status status = AE_OK;
525
526 if (device->cap._BQC) {
527 status = acpi_evaluate_integer(device->dev->handle, "_BQC",
528 NULL, level);
529 if (ACPI_SUCCESS(status)) {
Zhang Rui1a7c6182009-03-18 16:27:16 +0800530 if (device->brightness->flags._BQC_use_index) {
531 if (device->brightness->flags._BCL_reversed)
532 *level = device->brightness->count
533 - 3 - (*level);
534 *level = device->brightness->levels[*level + 2];
535
536 }
Zhang Ruic8890f92009-03-18 16:27:08 +0800537 device->brightness->curr = *level;
538 return 0;
539 } else {
540 /* Fixme:
541 * should we return an error or ignore this failure?
542 * dev->brightness->curr is a cached value which stores
543 * the correct current backlight level in most cases.
544 * ACPI video backlight still works w/ buggy _BQC.
545 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
546 */
547 ACPI_WARNING((AE_INFO, "Evaluating _BQC failed"));
548 device->cap._BQC = 0;
549 }
550 }
551
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400552 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556static int
Len Brown4be44fc2005-08-05 00:44:28 -0400557acpi_video_device_EDID(struct acpi_video_device *device,
558 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560 int status;
561 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
562 union acpi_object *obj;
563 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
564 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 *edid = NULL;
568
569 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (length == 128)
572 arg0.integer.value = 1;
573 else if (length == 256)
574 arg0.integer.value = 2;
575 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400576 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Patrick Mochel90130262006-05-19 16:54:48 -0400578 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200582 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (obj && obj->type == ACPI_TYPE_BUFFER)
585 *edid = obj;
586 else {
Len Brown64684632006-06-26 23:41:38 -0400587 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 status = -EFAULT;
589 kfree(obj);
590 }
591
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* bus */
596
597static int
Len Brown4be44fc2005-08-05 00:44:28 -0400598acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Len Brown4be44fc2005-08-05 00:44:28 -0400600 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400601 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400602 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
603 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 arg0.integer.value = option;
607
Patrick Mochel90130262006-05-19 16:54:48 -0400608 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400610 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400616acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 int status;
619
Patrick Mochel90130262006-05-19 16:54:48 -0400620 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Patrick Mocheld550d982006-06-27 00:41:40 -0400622 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625static int
Len Brown4be44fc2005-08-05 00:44:28 -0400626acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400627 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Len Brown4be44fc2005-08-05 00:44:28 -0400629 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Patrick Mochel90130262006-05-19 16:54:48 -0400631 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 *options &= 3;
633
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * Arg:
639 * video : video bus device pointer
640 * bios_flag :
641 * 0. The system BIOS should NOT automatically switch(toggle)
642 * the active display output.
643 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100644 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * 2. The _DGS value should be locked.
646 * 3. The system BIOS should not automatically switch (toggle) the
647 * active display output, but instead generate the display switch
648 * event notify code.
649 * lcd_flag :
650 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100651 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100653 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * Return Value:
655 * -1 wrong arg.
656 */
657
658static int
Len Brown4be44fc2005-08-05 00:44:28 -0400659acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Len Brown4be44fc2005-08-05 00:44:28 -0400661 acpi_integer status = 0;
662 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
663 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Len Brown4be44fc2005-08-05 00:44:28 -0400666 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 status = -1;
668 goto Failed;
669 }
670 arg0.integer.value = (lcd_flag << 2) | bios_flag;
671 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400672 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Len Brown4be44fc2005-08-05 00:44:28 -0400674 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500679 * Simple comparison function used to sort backlight levels.
680 */
681
682static int
683acpi_video_cmp_level(const void *a, const void *b)
684{
685 return *(int *)a - *(int *)b;
686}
687
688/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * Arg:
690 * device : video output device (LCD, CRT, ..)
691 *
692 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100693 * Maximum brightness level
694 *
695 * Allocate and initialize device->brightness.
696 */
697
698static int
699acpi_video_init_brightness(struct acpi_video_device *device)
700{
701 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800702 int i, max_level = 0, count = 0, level_ac_battery = 0;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800703 unsigned long long level, level_old;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100704 union acpi_object *o;
705 struct acpi_video_device_brightness *br = NULL;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800706 int result = -EINVAL;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100707
708 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
709 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
710 "LCD brightness level\n"));
711 goto out;
712 }
713
714 if (obj->package.count < 2)
715 goto out;
716
717 br = kzalloc(sizeof(*br), GFP_KERNEL);
718 if (!br) {
719 printk(KERN_ERR "can't allocate memory\n");
Zhang Rui1a7c6182009-03-18 16:27:16 +0800720 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100721 goto out;
722 }
723
Zhang Ruid32f6942009-03-18 16:27:12 +0800724 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100725 GFP_KERNEL);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800726 if (!br->levels) {
727 result = -ENOMEM;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100728 goto out_free;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800729 }
Julia Jomantaite469778c2008-06-23 22:50:42 +0100730
731 for (i = 0; i < obj->package.count; i++) {
732 o = (union acpi_object *)&obj->package.elements[i];
733 if (o->type != ACPI_TYPE_INTEGER) {
734 printk(KERN_ERR PREFIX "Invalid data\n");
735 continue;
736 }
737 br->levels[count] = (u32) o->integer.value;
738
739 if (br->levels[count] > max_level)
740 max_level = br->levels[count];
741 count++;
742 }
743
Zhang Ruid32f6942009-03-18 16:27:12 +0800744 /*
745 * some buggy BIOS don't export the levels
746 * when machine is on AC/Battery in _BCL package.
747 * In this case, the first two elements in _BCL packages
748 * are also supported brightness levels that OS should take care of.
749 */
750 for (i = 2; i < count; i++)
751 if (br->levels[i] == br->levels[0] ||
752 br->levels[i] == br->levels[1])
753 level_ac_battery++;
754
755 if (level_ac_battery < 2) {
756 level_ac_battery = 2 - level_ac_battery;
757 br->flags._BCL_no_ac_battery_levels = 1;
758 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
759 br->levels[i] = br->levels[i - level_ac_battery];
760 count += level_ac_battery;
761 } else if (level_ac_battery > 2)
762 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
763
Zhang Ruid80fb992009-03-18 16:27:14 +0800764 /* Check if the _BCL package is in a reversed order */
765 if (max_level == br->levels[2]) {
766 br->flags._BCL_reversed = 1;
767 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
768 acpi_video_cmp_level, NULL);
769 } else if (max_level != br->levels[count - 1])
770 ACPI_ERROR((AE_INFO,
771 "Found unordered _BCL package\n"));
Zhang Rui935e5f22008-12-11 16:24:52 -0500772
Julia Jomantaite469778c2008-06-23 22:50:42 +0100773 br->count = count;
774 device->brightness = br;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800775
776 /* Check the input/output of _BQC/_BCL/_BCM */
777 if ((max_level < 100) && (max_level <= (count - 2)))
778 br->flags._BCL_use_index = 1;
779
780 /*
781 * _BCM is always consistent with _BCL,
782 * at least for all the laptops we have ever seen.
783 */
784 br->flags._BCM_use_index = br->flags._BCL_use_index;
785
786 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
787 br->curr = max_level;
788 result = acpi_video_device_lcd_get_level_current(device, &level_old);
789 if (result)
790 goto out_free_levels;
791
792 result = acpi_video_device_lcd_set_level(device, br->curr);
793 if (result)
794 goto out_free_levels;
795
796 result = acpi_video_device_lcd_get_level_current(device, &level);
797 if (result)
798 goto out_free_levels;
799
800 if ((level != level_old) && !br->flags._BCM_use_index) {
801 /* Note:
802 * This piece of code does not work correctly if the current
803 * brightness levels is 0.
804 * But I guess boxes that boot with such a dark screen are rare
805 * and no more code is needed to cover this specifial case.
806 */
807
808 if (level_ac_battery != 2) {
809 /*
810 * For now, we don't support the _BCL like this:
811 * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
812 * because we may mess up the index returned by _BQC.
813 * Plus: we have not got a box like this.
814 */
815 ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
816 }
817 br->flags._BQC_use_index = 1;
818 }
819
Zhang Ruid32f6942009-03-18 16:27:12 +0800820 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
821 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100822 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800823 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100824
825out_free_levels:
826 kfree(br->levels);
827out_free:
828 kfree(br);
829out:
830 device->brightness = NULL;
831 kfree(obj);
Zhang Rui1a7c6182009-03-18 16:27:16 +0800832 return result;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100833}
834
835/*
836 * Arg:
837 * device : video output device (LCD, CRT, ..)
838 *
839 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * None
841 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100842 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * device.
844 */
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
William Lee Irwin III98934de2007-12-12 03:56:55 -0800851 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Patrick Mochel90130262006-05-19 16:54:48 -0400853 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 device->cap._ADR = 1;
855 }
Patrick Mochel90130262006-05-19 16:54:48 -0400856 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400857 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Patrick Mochel90130262006-05-19 16:54:48 -0400859 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400860 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800862 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
863 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400864 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400865 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Patrick Mochel90130262006-05-19 16:54:48 -0400867 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 device->cap._DCS = 1;
869 }
Patrick Mochel90130262006-05-19 16:54:48 -0400870 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 device->cap._DGS = 1;
872 }
Patrick Mochel90130262006-05-19 16:54:48 -0400873 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 device->cap._DSS = 1;
875 }
876
Zhang Rui1a7c6182009-03-18 16:27:16 +0800877 if (acpi_video_backlight_support()) {
Zhang Rui702ed512008-01-17 15:51:22 +0800878 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800879 static int count = 0;
880 char *name;
Zhang Rui1a7c6182009-03-18 16:27:16 +0800881
882 result = acpi_video_init_brightness(device);
883 if (result)
884 return;
Yu Luming2f3d0002006-11-11 02:40:34 +0800885 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
886 if (!name)
887 return;
888
Yu Luming2f3d0002006-11-11 02:40:34 +0800889 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800890 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000891 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000892 device->backlight->props.max_brightness = device->brightness->count-3;
Yu Luming2f3d0002006-11-11 02:40:34 +0800893 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800894
895 device->cdev = thermal_cooling_device_register("LCD",
896 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500897 if (IS_ERR(device->cdev))
898 return;
899
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200900 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
901 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800902 result = sysfs_create_link(&device->dev->dev.kobj,
903 &device->cdev->device.kobj,
904 "thermal_cooling");
905 if (result)
906 printk(KERN_ERR PREFIX "Create sysfs link\n");
907 result = sysfs_create_link(&device->cdev->device.kobj,
908 &device->dev->dev.kobj, "device");
909 if (result)
910 printk(KERN_ERR PREFIX "Create sysfs link\n");
911
Yu Luming2f3d0002006-11-11 02:40:34 +0800912 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200913
914 if (acpi_video_display_switch_support()) {
915
916 if (device->cap._DCS && device->cap._DSS) {
917 static int count;
918 char *name;
919 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
920 if (!name)
921 return;
922 sprintf(name, "acpi_video%d", count++);
923 device->output_dev = video_output_register(name,
924 NULL, device, &acpi_output_properties);
925 kfree(name);
926 }
Luming Yu23b0f012007-05-09 21:07:05 +0800927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
931 * Arg:
932 * device : video output device (VGA)
933 *
934 * Return Value:
935 * None
936 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100937 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939
Len Brown4be44fc2005-08-05 00:44:28 -0400940static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Len Brown4be44fc2005-08-05 00:44:28 -0400942 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
William Lee Irwin III98934de2007-12-12 03:56:55 -0800944 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400945 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 video->cap._DOS = 1;
947 }
Patrick Mochel90130262006-05-19 16:54:48 -0400948 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 video->cap._DOD = 1;
950 }
Patrick Mochel90130262006-05-19 16:54:48 -0400951 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 video->cap._ROM = 1;
953 }
Patrick Mochel90130262006-05-19 16:54:48 -0400954 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 video->cap._GPD = 1;
956 }
Patrick Mochel90130262006-05-19 16:54:48 -0400957 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 video->cap._SPD = 1;
959 }
Patrick Mochel90130262006-05-19 16:54:48 -0400960 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 video->cap._VPO = 1;
962 }
963}
964
965/*
966 * Check whether the video bus device has required AML method to
967 * support the desired features
968 */
969
Len Brown4be44fc2005-08-05 00:44:28 -0400970static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Len Brown4be44fc2005-08-05 00:44:28 -0400972 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200973 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Thomas Renninger22c13f92008-08-01 17:37:54 +0200978 dev = acpi_get_physical_pci_device(video->device->handle);
979 if (!dev)
980 return -ENODEV;
981 put_device(dev);
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Since there is no HID, CID and so on for VGA driver, we have
984 * to check well known required nodes.
985 */
986
Julius Volz98fb8fe2007-02-20 16:38:40 +0100987 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400988 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 video->flags.multihead = 1;
990 status = 0;
991 }
992
Julius Volz98fb8fe2007-02-20 16:38:40 +0100993 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400994 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 video->flags.rom = 1;
996 status = 0;
997 }
998
Julius Volz98fb8fe2007-02-20 16:38:40 +0100999 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -04001000 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 video->flags.post = 1;
1002 status = 0;
1003 }
1004
Patrick Mocheld550d982006-06-27 00:41:40 -04001005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/* --------------------------------------------------------------------------
1009 FS Interface (/proc)
1010 -------------------------------------------------------------------------- */
1011
Len Brown4be44fc2005-08-05 00:44:28 -04001012static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014/* video devices */
1015
Len Brown4be44fc2005-08-05 00:44:28 -04001016static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001018 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!dev)
1022 goto end;
1023
1024 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1025 seq_printf(seq, "type: ");
1026 if (dev->flags.crt)
1027 seq_printf(seq, "CRT\n");
1028 else if (dev->flags.lcd)
1029 seq_printf(seq, "LCD\n");
1030 else if (dev->flags.tvout)
1031 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -05001032 else if (dev->flags.dvi)
1033 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 else
1035 seq_printf(seq, "UNKNOWN\n");
1036
Len Brown4be44fc2005-08-05 00:44:28 -04001037 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Len Brown4be44fc2005-08-05 00:44:28 -04001039 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static int
Len Brown4be44fc2005-08-05 00:44:28 -04001044acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 return single_open(file, acpi_video_device_info_seq_show,
1047 PDE(inode)->data);
1048}
1049
Len Brown4be44fc2005-08-05 00:44:28 -04001050static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Len Brown4be44fc2005-08-05 00:44:28 -04001052 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001053 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001054 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (!dev)
1058 goto end;
1059
1060 status = acpi_video_device_get_state(dev, &state);
1061 seq_printf(seq, "state: ");
1062 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001063 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 else
1065 seq_printf(seq, "<not supported>\n");
1066
1067 status = acpi_video_device_query(dev, &state);
1068 seq_printf(seq, "query: ");
1069 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001070 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 else
1072 seq_printf(seq, "<not supported>\n");
1073
Len Brown4be44fc2005-08-05 00:44:28 -04001074 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078static int
Len Brown4be44fc2005-08-05 00:44:28 -04001079acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 return single_open(file, acpi_video_device_state_seq_show,
1082 PDE(inode)->data);
1083}
1084
1085static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001086acpi_video_device_write_state(struct file *file,
1087 const char __user * buffer,
1088 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Len Brown4be44fc2005-08-05 00:44:28 -04001090 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001091 struct seq_file *m = file->private_data;
1092 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001093 char str[12] = { 0 };
1094 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001101 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 str[count] = 0;
1104 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001105 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 status = acpi_video_device_set_state(dev, state);
1108
1109 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Patrick Mocheld550d982006-06-27 00:41:40 -04001112 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115static int
Len Brown4be44fc2005-08-05 00:44:28 -04001116acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001118 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001119 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (!dev || !dev->brightness) {
1123 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001128 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 seq_printf(seq, " %d", dev->brightness->levels[i]);
1130 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1131
Patrick Mocheld550d982006-06-27 00:41:40 -04001132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static int
Len Brown4be44fc2005-08-05 00:44:28 -04001136acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 return single_open(file, acpi_video_device_brightness_seq_show,
1139 PDE(inode)->data);
1140}
1141
1142static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001143acpi_video_device_write_brightness(struct file *file,
1144 const char __user * buffer,
1145 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001147 struct seq_file *m = file->private_data;
1148 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001149 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001150 unsigned int level = 0;
1151 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Renninger59d399d2005-11-08 05:27:00 -05001154 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001158 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 str[count] = 0;
1161 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001164 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Julius Volz98fb8fe2007-02-20 16:38:40 +01001166 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001167 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001169 if (!acpi_video_device_lcd_set_level(dev, level))
1170 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 }
1173
Zhang Rui24450c72009-03-18 16:27:10 +08001174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Len Brown4be44fc2005-08-05 00:44:28 -04001177static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001179 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001180 int status;
1181 int i;
1182 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (!dev)
1186 goto out;
1187
Len Brown4be44fc2005-08-05 00:44:28 -04001188 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001190 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
1193 if (ACPI_FAILURE(status)) {
1194 goto out;
1195 }
1196
1197 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1198 for (i = 0; i < edid->buffer.length; i++)
1199 seq_putc(seq, edid->buffer.pointer[i]);
1200 }
1201
Len Brown4be44fc2005-08-05 00:44:28 -04001202 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (!edid)
1204 seq_printf(seq, "<not supported>\n");
1205 else
1206 kfree(edid);
1207
Patrick Mocheld550d982006-06-27 00:41:40 -04001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static int
Len Brown4be44fc2005-08-05 00:44:28 -04001212acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 return single_open(file, acpi_video_device_EDID_seq_show,
1215 PDE(inode)->data);
1216}
1217
Len Brown4be44fc2005-08-05 00:44:28 -04001218static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001220 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct acpi_video_device *vid_dev;
1222
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001223 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001225 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001227 device_dir = proc_mkdir(acpi_device_bid(device),
1228 vid_dev->video->dir);
1229 if (!device_dir)
1230 return -ENOMEM;
1231
1232 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001235 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001236 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001238 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001241 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1242 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001243 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001244 &acpi_video_device_state_fops,
1245 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_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001250 acpi_video_device_brightness_fops.write =
1251 acpi_video_device_write_brightness;
1252 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001253 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001254 &acpi_video_device_brightness_fops,
1255 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001257 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001260 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001261 &acpi_video_device_EDID_fops,
1262 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001264 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001266 acpi_device_dir(device) = device_dir;
1267
Patrick Mocheld550d982006-06-27 00:41:40 -04001268 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001269
1270 err_remove_brightness:
1271 remove_proc_entry("brightness", device_dir);
1272 err_remove_state:
1273 remove_proc_entry("state", device_dir);
1274 err_remove_info:
1275 remove_proc_entry("info", device_dir);
1276 err_remove_dir:
1277 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1278 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Len Brown4be44fc2005-08-05 00:44:28 -04001281static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001284 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001286 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001290 device_dir = acpi_device_dir(device);
1291 if (device_dir) {
1292 remove_proc_entry("info", device_dir);
1293 remove_proc_entry("state", device_dir);
1294 remove_proc_entry("brightness", device_dir);
1295 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001296 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 acpi_device_dir(device) = NULL;
1298 }
1299
Patrick Mocheld550d982006-06-27 00:41:40 -04001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001304static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001306 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (!video)
1310 goto end;
1311
1312 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001313 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001315 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001317 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Len Brown4be44fc2005-08-05 00:44:28 -04001319 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Len Brown4be44fc2005-08-05 00:44:28 -04001323static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Len Brown4be44fc2005-08-05 00:44:28 -04001325 return single_open(file, acpi_video_bus_info_seq_show,
1326 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Len Brown4be44fc2005-08-05 00:44:28 -04001329static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001331 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (!video)
1335 goto end;
1336
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001337 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 seq_printf(seq, "<TODO>\n");
1339
Len Brown4be44fc2005-08-05 00:44:28 -04001340 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Len Brown4be44fc2005-08-05 00:44:28 -04001344static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1347}
1348
Len Brown4be44fc2005-08-05 00:44:28 -04001349static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001351 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001352 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001353 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (!video)
1357 goto end;
1358
1359 status = acpi_video_bus_POST_options(video, &options);
1360 if (ACPI_SUCCESS(status)) {
1361 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001362 printk(KERN_WARNING PREFIX
1363 "The motherboard VGA device is not listed as a possible POST device.\n");
1364 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001365 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Frank Seidel4d939152009-02-04 17:03:07 +01001367 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001368 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (options & 2)
1370 seq_printf(seq, " <PCI video>");
1371 if (options & 4)
1372 seq_printf(seq, " <AGP video>");
1373 seq_putc(seq, '\n');
1374 } else
1375 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001376 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380static int
Len Brown4be44fc2005-08-05 00:44:28 -04001381acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Len Brown4be44fc2005-08-05 00:44:28 -04001383 return single_open(file, acpi_video_bus_POST_info_seq_show,
1384 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Len Brown4be44fc2005-08-05 00:44:28 -04001387static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001389 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001390 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001391 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (!video)
1395 goto end;
1396
Len Brown4be44fc2005-08-05 00:44:28 -04001397 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!ACPI_SUCCESS(status)) {
1399 seq_printf(seq, "<not supported>\n");
1400 goto end;
1401 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001402 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Len Brown4be44fc2005-08-05 00:44:28 -04001404 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Len Brown4be44fc2005-08-05 00:44:28 -04001408static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001410 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Len Brown4be44fc2005-08-05 00:44:28 -04001413 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Patrick Mocheld550d982006-06-27 00:41:40 -04001415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Len Brown4be44fc2005-08-05 00:44:28 -04001418static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Len Brown4be44fc2005-08-05 00:44:28 -04001420 return single_open(file, acpi_video_bus_POST_seq_show,
1421 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Len Brown4be44fc2005-08-05 00:44:28 -04001424static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1427}
1428
1429static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001430acpi_video_bus_write_POST(struct file *file,
1431 const char __user * buffer,
1432 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Len Brown4be44fc2005-08-05 00:44:28 -04001434 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001435 struct seq_file *m = file->private_data;
1436 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001437 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001438 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 status = acpi_video_bus_POST_options(video, &options);
1445 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001449 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 str[count] = 0;
1452 opt = strtoul(str, NULL, 0);
1453 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001454 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Julius Volz98fb8fe2007-02-20 16:38:40 +01001456 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 options |= 1;
1458
1459 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001460 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001462 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 }
1465
Patrick Mocheld550d982006-06-27 00:41:40 -04001466 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001470acpi_video_bus_write_DOS(struct file *file,
1471 const char __user * buffer,
1472 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Len Brown4be44fc2005-08-05 00:44:28 -04001474 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001475 struct seq_file *m = file->private_data;
1476 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001477 char str[12] = { 0 };
1478 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001485 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 str[count] = 0;
1488 opt = strtoul(str, NULL, 0);
1489 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001490 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Len Brown4be44fc2005-08-05 00:44:28 -04001492 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001495 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Patrick Mocheld550d982006-06-27 00:41:40 -04001497 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Len Brown4be44fc2005-08-05 00:44:28 -04001500static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001502 struct acpi_video_bus *video = acpi_driver_data(device);
1503 struct proc_dir_entry *device_dir;
1504 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001506 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1507 if (!device_dir)
1508 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001510 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001513 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001514 &acpi_video_bus_info_fops,
1515 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001517 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001520 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001521 &acpi_video_bus_ROM_fops,
1522 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001524 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001527 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001528 &acpi_video_bus_POST_info_fops,
1529 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001531 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001534 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001535 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001536 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001537 &acpi_video_bus_POST_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_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001543 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001544 entry = proc_create_data("DOS", 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_DOS_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001551 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001552 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001553
1554 err_remove_post:
1555 remove_proc_entry("POST", device_dir);
1556 err_remove_post_info:
1557 remove_proc_entry("POST_info", device_dir);
1558 err_remove_rom:
1559 remove_proc_entry("ROM", device_dir);
1560 err_remove_info:
1561 remove_proc_entry("info", device_dir);
1562 err_remove_dir:
1563 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1564 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Len Brown4be44fc2005-08-05 00:44:28 -04001567static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001569 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001571 if (device_dir) {
1572 remove_proc_entry("info", device_dir);
1573 remove_proc_entry("ROM", device_dir);
1574 remove_proc_entry("POST_info", device_dir);
1575 remove_proc_entry("POST", device_dir);
1576 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001577 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 acpi_device_dir(device) = NULL;
1579 }
1580
Patrick Mocheld550d982006-06-27 00:41:40 -04001581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584/* --------------------------------------------------------------------------
1585 Driver Interface
1586 -------------------------------------------------------------------------- */
1587
1588/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001589static struct acpi_video_device_attrib*
1590acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1591{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001592 struct acpi_video_enumerated_device *ids;
1593 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001594
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001595 for (i = 0; i < video->attached_count; i++) {
1596 ids = &video->attached_array[i];
1597 if ((ids->value.int_val & 0xffff) == device_id)
1598 return &ids->value.attrib;
1599 }
1600
Rui Zhang82cae992007-01-03 23:40:53 -05001601 return NULL;
1602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604static int
Len Brown4be44fc2005-08-05 00:44:28 -04001605acpi_video_bus_get_one_device(struct acpi_device *device,
1606 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001608 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001609 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001610 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001611 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001614 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Len Brown4be44fc2005-08-05 00:44:28 -04001616 status =
1617 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (ACPI_SUCCESS(status)) {
1619
Burman Yan36bcbec2006-12-19 12:56:11 -08001620 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001622 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1625 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001626 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 data->device_id = device_id;
1629 data->video = video;
1630 data->dev = device;
1631
Rui Zhang82cae992007-01-03 23:40:53 -05001632 attribute = acpi_video_get_device_attr(video, device_id);
1633
1634 if((attribute != NULL) && attribute->device_id_scheme) {
1635 switch (attribute->display_type) {
1636 case ACPI_VIDEO_DISPLAY_CRT:
1637 data->flags.crt = 1;
1638 break;
1639 case ACPI_VIDEO_DISPLAY_TV:
1640 data->flags.tvout = 1;
1641 break;
1642 case ACPI_VIDEO_DISPLAY_DVI:
1643 data->flags.dvi = 1;
1644 break;
1645 case ACPI_VIDEO_DISPLAY_LCD:
1646 data->flags.lcd = 1;
1647 break;
1648 default:
1649 data->flags.unknown = 1;
1650 break;
1651 }
1652 if(attribute->bios_can_detect)
1653 data->flags.bios = 1;
1654 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 acpi_video_device_bind(video, data);
1658 acpi_video_device_find_cap(data);
1659
Patrick Mochel90130262006-05-19 16:54:48 -04001660 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001661 ACPI_DEVICE_NOTIFY,
1662 acpi_video_device_notify,
1663 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001665 printk(KERN_ERR PREFIX
1666 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001667 if(data->brightness)
1668 kfree(data->brightness->levels);
1669 kfree(data->brightness);
1670 kfree(data);
1671 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001674 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001676 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 acpi_video_device_add_fs(device);
1679
Patrick Mocheld550d982006-06-27 00:41:40 -04001680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Patrick Mocheld550d982006-06-27 00:41:40 -04001683 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
1687 * Arg:
1688 * video : video bus device
1689 *
1690 * Return:
1691 * none
1692 *
1693 * Enumerate the video device list of the video bus,
1694 * bind the ids with the corresponding video devices
1695 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Len Brown4be44fc2005-08-05 00:44:28 -04001698static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001700 struct acpi_video_device *dev;
1701
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001702 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001703
1704 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001705 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001706
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001707 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710/*
1711 * Arg:
1712 * video : video bus device
1713 * device : video output device under the video
1714 * bus
1715 *
1716 * Return:
1717 * none
1718 *
1719 * Bind the ids with the corresponding video devices
1720 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723static void
Len Brown4be44fc2005-08-05 00:44:28 -04001724acpi_video_device_bind(struct acpi_video_bus *video,
1725 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001727 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001728 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001730 for (i = 0; i < video->attached_count; i++) {
1731 ids = &video->attached_array[i];
1732 if (device->device_id == (ids->value.int_val & 0xffff)) {
1733 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1735 }
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739/*
1740 * Arg:
1741 * video : video bus device
1742 *
1743 * Return:
1744 * < 0 : error
1745 *
1746 * Call _DOD to enumerate all devices attached to display adapter
1747 *
Len Brown4be44fc2005-08-05 00:44:28 -04001748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1751{
Len Brown4be44fc2005-08-05 00:44:28 -04001752 int status;
1753 int count;
1754 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001755 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001756 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1757 union acpi_object *dod = NULL;
1758 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Patrick Mochel90130262006-05-19 16:54:48 -04001760 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001762 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001763 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001766 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001768 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 status = -EFAULT;
1770 goto out;
1771 }
1772
1773 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001774 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001776 active_list = kcalloc(1 + dod->package.count,
1777 sizeof(struct acpi_video_enumerated_device),
1778 GFP_KERNEL);
1779 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 status = -ENOMEM;
1781 goto out;
1782 }
1783
1784 count = 0;
1785 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001786 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001789 printk(KERN_ERR PREFIX
1790 "Invalid _DOD data in element %d\n", i);
1791 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001793
1794 active_list[count].value.int_val = obj->integer.value;
1795 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001796 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1797 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 count++;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jesper Juhl6044ec82005-11-07 01:01:32 -08001801 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001802
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001803 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001805
1806 out:
Len Brown02438d82006-06-30 03:19:10 -04001807 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001808 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Len Brown4be44fc2005-08-05 00:44:28 -04001811static int
1812acpi_video_get_next_level(struct acpi_video_device *device,
1813 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001815 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001816 max = max_below = 0;
1817 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001818 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001819 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001820 l = device->brightness->levels[i];
1821 if (abs(l - level_current) < abs(delta)) {
1822 delta = l - level_current;
1823 if (!delta)
1824 break;
1825 }
1826 }
1827 /* Ajust level_current to closest available level */
1828 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001829 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001830 l = device->brightness->levels[i];
1831 if (l < min)
1832 min = l;
1833 if (l > max)
1834 max = l;
1835 if (l < min_above && l > level_current)
1836 min_above = l;
1837 if (l > max_below && l < level_current)
1838 max_below = l;
1839 }
1840
1841 switch (event) {
1842 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1843 return (level_current < max) ? min_above : min;
1844 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1845 return (level_current < max) ? min_above : max;
1846 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1847 return (level_current > min) ? max_below : min;
1848 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1849 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1850 return 0;
1851 default:
1852 return level_current;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Zhang Ruic8890f92009-03-18 16:27:08 +08001856static int
Len Brown4be44fc2005-08-05 00:44:28 -04001857acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001859 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001860 int result = -EINVAL;
1861
Julia Jomantaite469778c2008-06-23 22:50:42 +01001862 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001863 goto out;
1864
1865 result = acpi_video_device_lcd_get_level_current(device,
1866 &level_current);
1867 if (result)
1868 goto out;
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001871
Zhang Rui24450c72009-03-18 16:27:10 +08001872 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001873
1874out:
1875 if (result)
1876 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1877
1878 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881static int
Len Brown4be44fc2005-08-05 00:44:28 -04001882acpi_video_bus_get_devices(struct acpi_video_bus *video,
1883 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Len Brown4be44fc2005-08-05 00:44:28 -04001885 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001886 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 acpi_video_device_enumerate(video);
1889
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001890 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 status = acpi_video_bus_get_one_device(dev, video);
1893 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001894 printk(KERN_WARNING PREFIX
1895 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 continue;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001899 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Len Brown4be44fc2005-08-05 00:44:28 -04001902static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Karol Kozimor031ec772005-07-30 04:18:00 -04001904 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct acpi_video_bus *video;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001909 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 video = device->video;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 acpi_video_device_remove_fs(device->dev);
1914
Patrick Mochel90130262006-05-19 16:54:48 -04001915 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001916 ACPI_DEVICE_NOTIFY,
1917 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001918 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001919 if (device->cdev) {
1920 sysfs_remove_link(&device->dev->dev.kobj,
1921 "thermal_cooling");
1922 sysfs_remove_link(&device->cdev->device.kobj,
1923 "device");
1924 thermal_cooling_device_unregister(device->cdev);
1925 device->cdev = NULL;
1926 }
Luming Yu23b0f012007-05-09 21:07:05 +08001927 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001928
Patrick Mocheld550d982006-06-27 00:41:40 -04001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Len Brown4be44fc2005-08-05 00:44:28 -04001932static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Len Brown4be44fc2005-08-05 00:44:28 -04001934 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001935 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001937 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001939 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001941 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001942 if (ACPI_FAILURE(status))
1943 printk(KERN_WARNING PREFIX
1944 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001946 if (dev->brightness) {
1947 kfree(dev->brightness->levels);
1948 kfree(dev->brightness);
1949 }
1950 list_del(&dev->entry);
1951 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001954 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001955
Patrick Mocheld550d982006-06-27 00:41:40 -04001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
1959/* acpi_video interface */
1960
Len Brown4be44fc2005-08-05 00:44:28 -04001961static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Zhang Ruia21101c2007-09-14 11:46:22 +08001963 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Len Brown4be44fc2005-08-05 00:44:28 -04001966static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 return acpi_video_bus_DOS(video, 0, 1);
1969}
1970
Len Brown4be44fc2005-08-05 00:44:28 -04001971static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001973 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001974 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001975 struct input_dev *input;
1976 int keycode;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001979 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001981 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001982 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001985 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 * most likely via hotkey. */
Len Brown14e04fb32007-08-23 15:20:26 -04001987 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001988 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 break;
1990
Julius Volz98fb8fe2007-02-20 16:38:40 +01001991 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 * connector. */
1993 acpi_video_device_enumerate(video);
1994 acpi_video_device_rebind(video);
Len Brown14e04fb32007-08-23 15:20:26 -04001995 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001996 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998
Len Brown4be44fc2005-08-05 00:44:28 -04001999 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002000 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002001 keycode = KEY_SWITCHVIDEOMODE;
2002 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002003 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04002004 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002005 keycode = KEY_VIDEO_NEXT;
2006 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002007 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb32007-08-23 15:20:26 -04002008 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002009 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 break;
2011
2012 default:
Luming Yue9dab192007-08-20 18:23:53 +08002013 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002015 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 break;
2017 }
2018
Zhang Rui7761f632008-01-25 14:48:12 +08002019 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002020 input_report_key(input, keycode, 1);
2021 input_sync(input);
2022 input_report_key(input, keycode, 0);
2023 input_sync(input);
2024
Patrick Mocheld550d982006-06-27 00:41:40 -04002025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Len Brown4be44fc2005-08-05 00:44:28 -04002028static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002030 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04002031 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08002032 struct acpi_video_bus *bus;
2033 struct input_dev *input;
2034 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04002037 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002039 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002040 bus = video_device->video;
2041 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04002044 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002045 if (brightness_switch_enabled)
2046 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002047 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002048 keycode = KEY_BRIGHTNESS_CYCLE;
2049 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002050 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002051 if (brightness_switch_enabled)
2052 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002053 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002054 keycode = KEY_BRIGHTNESSUP;
2055 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002056 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002057 if (brightness_switch_enabled)
2058 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002059 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002060 keycode = KEY_BRIGHTNESSDOWN;
2061 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002062 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002063 if (brightness_switch_enabled)
2064 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002065 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002066 keycode = KEY_BRIGHTNESS_ZERO;
2067 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002068 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002069 if (brightness_switch_enabled)
2070 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb32007-08-23 15:20:26 -04002071 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002072 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 break;
2074 default:
Luming Yue9dab192007-08-20 18:23:53 +08002075 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002077 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 break;
2079 }
Luming Yue9dab192007-08-20 18:23:53 +08002080
Zhang Rui7761f632008-01-25 14:48:12 +08002081 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002082 input_report_key(input, keycode, 1);
2083 input_sync(input);
2084 input_report_key(input, keycode, 0);
2085 input_sync(input);
2086
Patrick Mocheld550d982006-06-27 00:41:40 -04002087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Zhang Ruie6d9da12007-08-25 02:23:31 -04002090static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002091static int acpi_video_resume(struct acpi_device *device)
2092{
2093 struct acpi_video_bus *video;
2094 struct acpi_video_device *video_device;
2095 int i;
2096
2097 if (!device || !acpi_driver_data(device))
2098 return -EINVAL;
2099
2100 video = acpi_driver_data(device);
2101
2102 for (i = 0; i < video->attached_count; i++) {
2103 video_device = video->attached_array[i].bind_info;
2104 if (video_device && video_device->backlight)
2105 acpi_video_set_brightness(video_device->backlight);
2106 }
2107 return AE_OK;
2108}
2109
Len Brown4be44fc2005-08-05 00:44:28 -04002110static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002112 acpi_status status;
2113 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002114 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002115 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Burman Yan36bcbec2006-12-19 12:56:11 -08002117 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002119 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Zhang Ruie6d9da12007-08-25 02:23:31 -04002121 /* a hack to fix the duplicate name "VID" problem on T61 */
2122 if (!strcmp(device->pnp.bus_id, "VID")) {
2123 if (instance)
2124 device->pnp.bus_id[3] = '0' + instance;
2125 instance ++;
2126 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002127 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2128 if (!strcmp(device->pnp.bus_id, "VGA")) {
2129 if (instance)
2130 device->pnp.bus_id[3] = '0' + instance;
2131 instance++;
2132 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002133
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002134 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2136 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002137 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002140 error = acpi_video_bus_check(video);
2141 if (error)
2142 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002144 error = acpi_video_bus_add_fs(device);
2145 if (error)
2146 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002148 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 INIT_LIST_HEAD(&video->video_device_list);
2150
2151 acpi_video_bus_get_devices(video, device);
2152 acpi_video_bus_start_devices(video);
2153
Patrick Mochel90130262006-05-19 16:54:48 -04002154 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002155 ACPI_DEVICE_NOTIFY,
2156 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002158 printk(KERN_ERR PREFIX
2159 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002160 error = -ENODEV;
2161 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Luming Yue9dab192007-08-20 18:23:53 +08002164 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002165 if (!input) {
2166 error = -ENOMEM;
2167 goto err_uninstall_notify;
2168 }
Luming Yue9dab192007-08-20 18:23:53 +08002169
2170 snprintf(video->phys, sizeof(video->phys),
2171 "%s/video/input0", acpi_device_hid(video->device));
2172
2173 input->name = acpi_device_name(video->device);
2174 input->phys = video->phys;
2175 input->id.bustype = BUS_HOST;
2176 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002177 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002178 input->evbit[0] = BIT(EV_KEY);
2179 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2180 set_bit(KEY_VIDEO_NEXT, input->keybit);
2181 set_bit(KEY_VIDEO_PREV, input->keybit);
2182 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2183 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2184 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2185 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2186 set_bit(KEY_DISPLAY_OFF, input->keybit);
2187 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002188
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002189 error = input_register_device(input);
2190 if (error)
2191 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002194 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2195 video->flags.multihead ? "yes" : "no",
2196 video->flags.rom ? "yes" : "no",
2197 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002201 err_free_input_dev:
2202 input_free_device(input);
2203 err_uninstall_notify:
2204 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2205 acpi_video_bus_notify);
2206 err_stop_video:
2207 acpi_video_bus_stop_devices(video);
2208 acpi_video_bus_put_devices(video);
2209 kfree(video->attached_array);
2210 acpi_video_bus_remove_fs(device);
2211 err_free_video:
2212 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002213 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002214
2215 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Len Brown4be44fc2005-08-05 00:44:28 -04002218static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Len Brown4be44fc2005-08-05 00:44:28 -04002220 acpi_status status = 0;
2221 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002227 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 acpi_video_bus_stop_devices(video);
2230
Patrick Mochel90130262006-05-19 16:54:48 -04002231 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002232 ACPI_DEVICE_NOTIFY,
2233 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 acpi_video_bus_put_devices(video);
2236 acpi_video_bus_remove_fs(device);
2237
Luming Yue9dab192007-08-20 18:23:53 +08002238 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002239 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 kfree(video);
2241
Patrick Mocheld550d982006-06-27 00:41:40 -04002242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Len Brown4be44fc2005-08-05 00:44:28 -04002245static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Len Brown4be44fc2005-08-05 00:44:28 -04002247 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2250 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002251 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 acpi_video_dir->owner = THIS_MODULE;
2253
2254 result = acpi_bus_register_driver(&acpi_video_bus);
2255 if (result < 0) {
2256 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Patrick Mocheld550d982006-06-27 00:41:40 -04002260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Len Brown4be44fc2005-08-05 00:44:28 -04002263static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 acpi_bus_unregister_driver(&acpi_video_bus);
2267
2268 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2269
Patrick Mocheld550d982006-06-27 00:41:40 -04002270 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
2273module_init(acpi_video_init);
2274module_exit(acpi_video_exit);