blob: 398b3eee37fbb00b552f16bbeca53c0d4605ed58 [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 */
173};
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400176 int curr;
177 int count;
178 int *levels;
Zhang Ruid32f6942009-03-18 16:27:12 +0800179 struct acpi_video_brightness_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
182struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400183 unsigned long device_id;
184 struct acpi_video_device_flags flags;
185 struct acpi_video_device_cap cap;
186 struct list_head entry;
187 struct acpi_video_bus *video;
188 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800190 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800191 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800192 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* bus */
196static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
197static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700198 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400199 .open = acpi_video_bus_info_open_fs,
200 .read = seq_read,
201 .llseek = seq_lseek,
202 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
205static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
206static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700207 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400208 .open = acpi_video_bus_ROM_open_fs,
209 .read = seq_read,
210 .llseek = seq_lseek,
211 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
215 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700217 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400218 .open = acpi_video_bus_POST_info_open_fs,
219 .read = seq_read,
220 .llseek = seq_lseek,
221 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
224static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
225static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700226 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400227 .open = acpi_video_bus_POST_open_fs,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
234static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700235 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400236 .open = acpi_video_bus_DOS_open_fs,
237 .read = seq_read,
238 .llseek = seq_lseek,
239 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400243static int acpi_video_device_info_open_fs(struct inode *inode,
244 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700246 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400247 .open = acpi_video_device_info_open_fs,
248 .read = seq_read,
249 .llseek = seq_lseek,
250 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
Len Brown4be44fc2005-08-05 00:44:28 -0400253static int acpi_video_device_state_open_fs(struct inode *inode,
254 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700256 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400257 .open = acpi_video_device_state_open_fs,
258 .read = seq_read,
259 .llseek = seq_lseek,
260 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
Len Brown4be44fc2005-08-05 00:44:28 -0400263static int acpi_video_device_brightness_open_fs(struct inode *inode,
264 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700266 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400267 .open = acpi_video_device_brightness_open_fs,
268 .read = seq_read,
269 .llseek = seq_lseek,
270 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273static int acpi_video_device_EDID_open_fs(struct inode *inode,
274 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700276 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 .open = acpi_video_device_EDID_open_fs,
278 .read = seq_read,
279 .llseek = seq_lseek,
280 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "motherboard VGA device",
285 "PCI VGA device",
286 "AGP VGA device",
287 "UNKNOWN",
288};
289
Len Brown4be44fc2005-08-05 00:44:28 -0400290static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
291static void acpi_video_device_rebind(struct acpi_video_bus *video);
292static void acpi_video_device_bind(struct acpi_video_bus *video,
293 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800295static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
296 int level);
297static int acpi_video_device_lcd_get_level_current(
298 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400299 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400300static int acpi_video_get_next_level(struct acpi_video_device *device,
301 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800302static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400303 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800304static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400305 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800306static int acpi_video_output_get(struct output_device *od);
307static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Yu Luming2f3d0002006-11-11 02:40:34 +0800309/*backlight device sysfs support*/
310static int acpi_video_get_brightness(struct backlight_device *bd)
311{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400312 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000313 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800314 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100315 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800316
317 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
318 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000319 for (i = 2; i < vd->brightness->count; i++) {
320 if (vd->brightness->levels[i] == cur_level)
321 /* The first two entries are special - see page 575
322 of the ACPI spec 3.0 */
323 return i-2;
324 }
325 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800326}
327
328static int acpi_video_set_brightness(struct backlight_device *bd)
329{
Zhang Rui24450c72009-03-18 16:27:10 +0800330 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800331 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100332 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800333
334 return acpi_video_device_lcd_set_level(vd,
335 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800336}
337
Richard Purdie599a52d2007-02-10 23:07:48 +0000338static struct backlight_ops acpi_backlight_ops = {
339 .get_brightness = acpi_video_get_brightness,
340 .update_status = acpi_video_set_brightness,
341};
342
Luming Yu23b0f012007-05-09 21:07:05 +0800343/*video output device sysfs support*/
344static int acpi_video_output_get(struct output_device *od)
345{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400346 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800347 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700348 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800349 acpi_video_device_get_state(vd, &state);
350 return (int)state;
351}
352
353static int acpi_video_output_set(struct output_device *od)
354{
355 unsigned long state = od->request_state;
356 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700357 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800358 return acpi_video_device_set_state(vd, state);
359}
360
361static struct output_properties acpi_output_properties = {
362 .set_state = acpi_video_output_set,
363 .get_status = acpi_video_output_get,
364};
Zhang Rui702ed512008-01-17 15:51:22 +0800365
366
367/* thermal cooling device callbacks */
368static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
369{
370 struct acpi_device *device = cdev->devdata;
371 struct acpi_video_device *video = acpi_driver_data(device);
372
373 return sprintf(buf, "%d\n", video->brightness->count - 3);
374}
375
376static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
377{
378 struct acpi_device *device = cdev->devdata;
379 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400380 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800381 int state;
382
Zhang Ruic8890f92009-03-18 16:27:08 +0800383 if (acpi_video_device_lcd_get_level_current(video, &level))
384 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800385 for (state = 2; state < video->brightness->count; state++)
386 if (level == video->brightness->levels[state])
387 return sprintf(buf, "%d\n",
388 video->brightness->count - state - 1);
389
390 return -EINVAL;
391}
392
393static int
394video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
395{
396 struct acpi_device *device = cdev->devdata;
397 struct acpi_video_device *video = acpi_driver_data(device);
398 int level;
399
400 if ( state >= video->brightness->count - 2)
401 return -EINVAL;
402
403 state = video->brightness->count - state;
404 level = video->brightness->levels[state -1];
405 return acpi_video_device_lcd_set_level(video, level);
406}
407
408static struct thermal_cooling_device_ops video_cooling_ops = {
409 .get_max_state = video_get_max_state,
410 .get_cur_state = video_get_cur_state,
411 .set_cur_state = video_set_cur_state,
412};
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* --------------------------------------------------------------------------
415 Video Management
416 -------------------------------------------------------------------------- */
417
418/* device */
419
420static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400421acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Len Brown4be44fc2005-08-05 00:44:28 -0400423 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400424
425 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430static int
Len Brown4be44fc2005-08-05 00:44:28 -0400431acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400432 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Len Brown4be44fc2005-08-05 00:44:28 -0400434 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Patrick Mochel90130262006-05-19 16:54:48 -0400436 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Patrick Mocheld550d982006-06-27 00:41:40 -0400438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441static int
Len Brown4be44fc2005-08-05 00:44:28 -0400442acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Len Brown4be44fc2005-08-05 00:44:28 -0400444 int status;
445 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
446 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400447 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400451 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Patrick Mocheld550d982006-06-27 00:41:40 -0400453 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456static int
Len Brown4be44fc2005-08-05 00:44:28 -0400457acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
458 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Len Brown4be44fc2005-08-05 00:44:28 -0400460 int status;
461 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
462 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 *levels = NULL;
466
Patrick Mochel90130262006-05-19 16:54:48 -0400467 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400469 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400470 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500471 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400472 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 status = -EFAULT;
474 goto err;
475 }
476
477 *levels = obj;
478
Patrick Mocheld550d982006-06-27 00:41:40 -0400479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Len Brown4be44fc2005-08-05 00:44:28 -0400481 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800482 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Patrick Mocheld550d982006-06-27 00:41:40 -0400484 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static int
Len Brown4be44fc2005-08-05 00:44:28 -0400488acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Zhang Rui24450c72009-03-18 16:27:10 +0800490 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400491 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
492 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800493 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Zhang Rui24450c72009-03-18 16:27:10 +0800497 status = acpi_evaluate_object(device->dev->handle, "_BCM",
498 &args, NULL);
499 if (ACPI_FAILURE(status)) {
500 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
501 return -EIO;
502 }
503
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400504 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800505 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800506 if (level == device->brightness->levels[state]) {
Zhang Rui9e6dada2008-12-31 10:58:48 +0800507 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800508 return 0;
509 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800510
Zhang Rui24450c72009-03-18 16:27:10 +0800511 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static int
Len Brown4be44fc2005-08-05 00:44:28 -0400516acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400517 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Zhang Ruic8890f92009-03-18 16:27:08 +0800519 acpi_status status = AE_OK;
520
521 if (device->cap._BQC) {
522 status = acpi_evaluate_integer(device->dev->handle, "_BQC",
523 NULL, level);
524 if (ACPI_SUCCESS(status)) {
525 device->brightness->curr = *level;
526 return 0;
527 } else {
528 /* Fixme:
529 * should we return an error or ignore this failure?
530 * dev->brightness->curr is a cached value which stores
531 * the correct current backlight level in most cases.
532 * ACPI video backlight still works w/ buggy _BQC.
533 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
534 */
535 ACPI_WARNING((AE_INFO, "Evaluating _BQC failed"));
536 device->cap._BQC = 0;
537 }
538 }
539
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400540 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544static int
Len Brown4be44fc2005-08-05 00:44:28 -0400545acpi_video_device_EDID(struct acpi_video_device *device,
546 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Len Brown4be44fc2005-08-05 00:44:28 -0400548 int status;
549 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
550 union acpi_object *obj;
551 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
552 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 *edid = NULL;
556
557 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (length == 128)
560 arg0.integer.value = 1;
561 else if (length == 256)
562 arg0.integer.value = 2;
563 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Patrick Mochel90130262006-05-19 16:54:48 -0400566 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400568 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200570 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (obj && obj->type == ACPI_TYPE_BUFFER)
573 *edid = obj;
574 else {
Len Brown64684632006-06-26 23:41:38 -0400575 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 status = -EFAULT;
577 kfree(obj);
578 }
579
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/* bus */
584
585static int
Len Brown4be44fc2005-08-05 00:44:28 -0400586acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Len Brown4be44fc2005-08-05 00:44:28 -0400588 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400589 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400590 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
591 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 arg0.integer.value = option;
595
Patrick Mochel90130262006-05-19 16:54:48 -0400596 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400598 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Patrick Mocheld550d982006-06-27 00:41:40 -0400600 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400604acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int status;
607
Patrick Mochel90130262006-05-19 16:54:48 -0400608 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Patrick Mocheld550d982006-06-27 00:41:40 -0400610 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613static int
Len Brown4be44fc2005-08-05 00:44:28 -0400614acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400615 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Len Brown4be44fc2005-08-05 00:44:28 -0400617 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Patrick Mochel90130262006-05-19 16:54:48 -0400619 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 *options &= 3;
621
Patrick Mocheld550d982006-06-27 00:41:40 -0400622 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
626 * Arg:
627 * video : video bus device pointer
628 * bios_flag :
629 * 0. The system BIOS should NOT automatically switch(toggle)
630 * the active display output.
631 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100632 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * 2. The _DGS value should be locked.
634 * 3. The system BIOS should not automatically switch (toggle) the
635 * active display output, but instead generate the display switch
636 * event notify code.
637 * lcd_flag :
638 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100639 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100641 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * Return Value:
643 * -1 wrong arg.
644 */
645
646static int
Len Brown4be44fc2005-08-05 00:44:28 -0400647acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Len Brown4be44fc2005-08-05 00:44:28 -0400649 acpi_integer status = 0;
650 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
651 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 status = -1;
656 goto Failed;
657 }
658 arg0.integer.value = (lcd_flag << 2) | bios_flag;
659 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400660 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Len Brown4be44fc2005-08-05 00:44:28 -0400662 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400663 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500667 * Simple comparison function used to sort backlight levels.
668 */
669
670static int
671acpi_video_cmp_level(const void *a, const void *b)
672{
673 return *(int *)a - *(int *)b;
674}
675
676/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * Arg:
678 * device : video output device (LCD, CRT, ..)
679 *
680 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100681 * Maximum brightness level
682 *
683 * Allocate and initialize device->brightness.
684 */
685
686static int
687acpi_video_init_brightness(struct acpi_video_device *device)
688{
689 union acpi_object *obj = NULL;
Zhang Ruid32f6942009-03-18 16:27:12 +0800690 int i, max_level = 0, count = 0, level_ac_battery = 0;
Julia Jomantaite469778c2008-06-23 22:50:42 +0100691 union acpi_object *o;
692 struct acpi_video_device_brightness *br = NULL;
693
694 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
695 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
696 "LCD brightness level\n"));
697 goto out;
698 }
699
700 if (obj->package.count < 2)
701 goto out;
702
703 br = kzalloc(sizeof(*br), GFP_KERNEL);
704 if (!br) {
705 printk(KERN_ERR "can't allocate memory\n");
706 goto out;
707 }
708
Zhang Ruid32f6942009-03-18 16:27:12 +0800709 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
Julia Jomantaite469778c2008-06-23 22:50:42 +0100710 GFP_KERNEL);
711 if (!br->levels)
712 goto out_free;
713
714 for (i = 0; i < obj->package.count; i++) {
715 o = (union acpi_object *)&obj->package.elements[i];
716 if (o->type != ACPI_TYPE_INTEGER) {
717 printk(KERN_ERR PREFIX "Invalid data\n");
718 continue;
719 }
720 br->levels[count] = (u32) o->integer.value;
721
722 if (br->levels[count] > max_level)
723 max_level = br->levels[count];
724 count++;
725 }
726
Zhang Ruid32f6942009-03-18 16:27:12 +0800727 /*
728 * some buggy BIOS don't export the levels
729 * when machine is on AC/Battery in _BCL package.
730 * In this case, the first two elements in _BCL packages
731 * are also supported brightness levels that OS should take care of.
732 */
733 for (i = 2; i < count; i++)
734 if (br->levels[i] == br->levels[0] ||
735 br->levels[i] == br->levels[1])
736 level_ac_battery++;
737
738 if (level_ac_battery < 2) {
739 level_ac_battery = 2 - level_ac_battery;
740 br->flags._BCL_no_ac_battery_levels = 1;
741 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
742 br->levels[i] = br->levels[i - level_ac_battery];
743 count += level_ac_battery;
744 } else if (level_ac_battery > 2)
745 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
746
747 /* sort all the supported brightness levels */
Zhang Rui935e5f22008-12-11 16:24:52 -0500748 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
749 acpi_video_cmp_level, NULL);
750
Julia Jomantaite469778c2008-06-23 22:50:42 +0100751 br->count = count;
752 device->brightness = br;
Zhang Ruid32f6942009-03-18 16:27:12 +0800753 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
754 "found %d brightness levels\n", count - 2));
Julia Jomantaite469778c2008-06-23 22:50:42 +0100755 kfree(obj);
756 return max_level;
757
758out_free_levels:
759 kfree(br->levels);
760out_free:
761 kfree(br);
762out:
763 device->brightness = NULL;
764 kfree(obj);
765 return 0;
766}
767
768/*
769 * Arg:
770 * device : video output device (LCD, CRT, ..)
771 *
772 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * None
774 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100775 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * device.
777 */
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800782 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
William Lee Irwin III98934de2007-12-12 03:56:55 -0800785 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Patrick Mochel90130262006-05-19 16:54:48 -0400787 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 device->cap._ADR = 1;
789 }
Patrick Mochel90130262006-05-19 16:54:48 -0400790 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400791 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Patrick Mochel90130262006-05-19 16:54:48 -0400793 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400794 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800796 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
797 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400798 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400799 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Patrick Mochel90130262006-05-19 16:54:48 -0400801 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 device->cap._DCS = 1;
803 }
Patrick Mochel90130262006-05-19 16:54:48 -0400804 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 device->cap._DGS = 1;
806 }
Patrick Mochel90130262006-05-19 16:54:48 -0400807 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 device->cap._DSS = 1;
809 }
810
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200811 if (acpi_video_backlight_support())
812 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Zhao Yakuic2c78902008-07-17 10:46:05 +0800814 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800815 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800816 static int count = 0;
817 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800818 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
819 if (!name)
820 return;
821
Yu Luming2f3d0002006-11-11 02:40:34 +0800822 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800823 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000824 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000825 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800826 /*
827 * If there exists the _BQC object, the _BQC object will be
828 * called to get the current backlight brightness. Otherwise
829 * the brightness will be set to the maximum.
830 */
831 if (device->cap._BQC)
832 device->backlight->props.brightness =
833 acpi_video_get_brightness(device->backlight);
834 else
835 device->backlight->props.brightness =
836 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000837 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800838 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800839
840 device->cdev = thermal_cooling_device_register("LCD",
841 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500842 if (IS_ERR(device->cdev))
843 return;
844
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200845 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
846 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800847 result = sysfs_create_link(&device->dev->dev.kobj,
848 &device->cdev->device.kobj,
849 "thermal_cooling");
850 if (result)
851 printk(KERN_ERR PREFIX "Create sysfs link\n");
852 result = sysfs_create_link(&device->cdev->device.kobj,
853 &device->dev->dev.kobj, "device");
854 if (result)
855 printk(KERN_ERR PREFIX "Create sysfs link\n");
856
Yu Luming2f3d0002006-11-11 02:40:34 +0800857 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200858
859 if (acpi_video_display_switch_support()) {
860
861 if (device->cap._DCS && device->cap._DSS) {
862 static int count;
863 char *name;
864 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
865 if (!name)
866 return;
867 sprintf(name, "acpi_video%d", count++);
868 device->output_dev = video_output_register(name,
869 NULL, device, &acpi_output_properties);
870 kfree(name);
871 }
Luming Yu23b0f012007-05-09 21:07:05 +0800872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
876 * Arg:
877 * device : video output device (VGA)
878 *
879 * Return Value:
880 * None
881 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100882 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
884
Len Brown4be44fc2005-08-05 00:44:28 -0400885static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Len Brown4be44fc2005-08-05 00:44:28 -0400887 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
William Lee Irwin III98934de2007-12-12 03:56:55 -0800889 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400890 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 video->cap._DOS = 1;
892 }
Patrick Mochel90130262006-05-19 16:54:48 -0400893 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 video->cap._DOD = 1;
895 }
Patrick Mochel90130262006-05-19 16:54:48 -0400896 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 video->cap._ROM = 1;
898 }
Patrick Mochel90130262006-05-19 16:54:48 -0400899 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 video->cap._GPD = 1;
901 }
Patrick Mochel90130262006-05-19 16:54:48 -0400902 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 video->cap._SPD = 1;
904 }
Patrick Mochel90130262006-05-19 16:54:48 -0400905 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 video->cap._VPO = 1;
907 }
908}
909
910/*
911 * Check whether the video bus device has required AML method to
912 * support the desired features
913 */
914
Len Brown4be44fc2005-08-05 00:44:28 -0400915static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Len Brown4be44fc2005-08-05 00:44:28 -0400917 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200918 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Thomas Renninger22c13f92008-08-01 17:37:54 +0200923 dev = acpi_get_physical_pci_device(video->device->handle);
924 if (!dev)
925 return -ENODEV;
926 put_device(dev);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* Since there is no HID, CID and so on for VGA driver, we have
929 * to check well known required nodes.
930 */
931
Julius Volz98fb8fe2007-02-20 16:38:40 +0100932 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400933 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 video->flags.multihead = 1;
935 status = 0;
936 }
937
Julius Volz98fb8fe2007-02-20 16:38:40 +0100938 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400939 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 video->flags.rom = 1;
941 status = 0;
942 }
943
Julius Volz98fb8fe2007-02-20 16:38:40 +0100944 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400945 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 video->flags.post = 1;
947 status = 0;
948 }
949
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/* --------------------------------------------------------------------------
954 FS Interface (/proc)
955 -------------------------------------------------------------------------- */
956
Len Brown4be44fc2005-08-05 00:44:28 -0400957static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959/* video devices */
960
Len Brown4be44fc2005-08-05 00:44:28 -0400961static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200963 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!dev)
967 goto end;
968
969 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
970 seq_printf(seq, "type: ");
971 if (dev->flags.crt)
972 seq_printf(seq, "CRT\n");
973 else if (dev->flags.lcd)
974 seq_printf(seq, "LCD\n");
975 else if (dev->flags.tvout)
976 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500977 else if (dev->flags.dvi)
978 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 else
980 seq_printf(seq, "UNKNOWN\n");
981
Len Brown4be44fc2005-08-05 00:44:28 -0400982 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Len Brown4be44fc2005-08-05 00:44:28 -0400984 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static int
Len Brown4be44fc2005-08-05 00:44:28 -0400989acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 return single_open(file, acpi_video_device_info_seq_show,
992 PDE(inode)->data);
993}
994
Len Brown4be44fc2005-08-05 00:44:28 -0400995static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Len Brown4be44fc2005-08-05 00:44:28 -0400997 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200998 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400999 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (!dev)
1003 goto end;
1004
1005 status = acpi_video_device_get_state(dev, &state);
1006 seq_printf(seq, "state: ");
1007 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001008 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 else
1010 seq_printf(seq, "<not supported>\n");
1011
1012 status = acpi_video_device_query(dev, &state);
1013 seq_printf(seq, "query: ");
1014 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -04001015 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 else
1017 seq_printf(seq, "<not supported>\n");
1018
Len Brown4be44fc2005-08-05 00:44:28 -04001019 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023static int
Len Brown4be44fc2005-08-05 00:44:28 -04001024acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 return single_open(file, acpi_video_device_state_seq_show,
1027 PDE(inode)->data);
1028}
1029
1030static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001031acpi_video_device_write_state(struct file *file,
1032 const char __user * buffer,
1033 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Len Brown4be44fc2005-08-05 00:44:28 -04001035 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001036 struct seq_file *m = file->private_data;
1037 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001038 char str[12] = { 0 };
1039 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001043 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001046 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 str[count] = 0;
1049 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001050 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 status = acpi_video_device_set_state(dev, state);
1053
1054 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001055 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Patrick Mocheld550d982006-06-27 00:41:40 -04001057 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060static int
Len Brown4be44fc2005-08-05 00:44:28 -04001061acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001063 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001064 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (!dev || !dev->brightness) {
1068 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001073 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 seq_printf(seq, " %d", dev->brightness->levels[i]);
1075 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1076
Patrick Mocheld550d982006-06-27 00:41:40 -04001077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static int
Len Brown4be44fc2005-08-05 00:44:28 -04001081acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 return single_open(file, acpi_video_device_brightness_seq_show,
1084 PDE(inode)->data);
1085}
1086
1087static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001088acpi_video_device_write_brightness(struct file *file,
1089 const char __user * buffer,
1090 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001092 struct seq_file *m = file->private_data;
1093 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001094 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001095 unsigned int level = 0;
1096 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Thomas Renninger59d399d2005-11-08 05:27:00 -05001099 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001100 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001103 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 str[count] = 0;
1106 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001109 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Julius Volz98fb8fe2007-02-20 16:38:40 +01001111 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001112 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001114 if (!acpi_video_device_lcd_set_level(dev, level))
1115 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117 }
1118
Zhang Rui24450c72009-03-18 16:27:10 +08001119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Len Brown4be44fc2005-08-05 00:44:28 -04001122static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001124 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001125 int status;
1126 int i;
1127 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (!dev)
1131 goto out;
1132
Len Brown4be44fc2005-08-05 00:44:28 -04001133 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001135 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
1138 if (ACPI_FAILURE(status)) {
1139 goto out;
1140 }
1141
1142 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1143 for (i = 0; i < edid->buffer.length; i++)
1144 seq_putc(seq, edid->buffer.pointer[i]);
1145 }
1146
Len Brown4be44fc2005-08-05 00:44:28 -04001147 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!edid)
1149 seq_printf(seq, "<not supported>\n");
1150 else
1151 kfree(edid);
1152
Patrick Mocheld550d982006-06-27 00:41:40 -04001153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static int
Len Brown4be44fc2005-08-05 00:44:28 -04001157acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 return single_open(file, acpi_video_device_EDID_seq_show,
1160 PDE(inode)->data);
1161}
1162
Len Brown4be44fc2005-08-05 00:44:28 -04001163static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001165 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 struct acpi_video_device *vid_dev;
1167
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001168 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001170 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001172 device_dir = proc_mkdir(acpi_device_bid(device),
1173 vid_dev->video->dir);
1174 if (!device_dir)
1175 return -ENOMEM;
1176
1177 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001180 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001181 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001183 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001186 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1187 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001188 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001189 &acpi_video_device_state_fops,
1190 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001192 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001195 acpi_video_device_brightness_fops.write =
1196 acpi_video_device_write_brightness;
1197 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001198 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001199 &acpi_video_device_brightness_fops,
1200 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001202 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001205 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001206 &acpi_video_device_EDID_fops,
1207 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001209 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001211 acpi_device_dir(device) = device_dir;
1212
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001214
1215 err_remove_brightness:
1216 remove_proc_entry("brightness", device_dir);
1217 err_remove_state:
1218 remove_proc_entry("state", device_dir);
1219 err_remove_info:
1220 remove_proc_entry("info", device_dir);
1221 err_remove_dir:
1222 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1223 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Len Brown4be44fc2005-08-05 00:44:28 -04001226static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001229 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001231 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001233 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001235 device_dir = acpi_device_dir(device);
1236 if (device_dir) {
1237 remove_proc_entry("info", device_dir);
1238 remove_proc_entry("state", device_dir);
1239 remove_proc_entry("brightness", device_dir);
1240 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001241 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 acpi_device_dir(device) = NULL;
1243 }
1244
Patrick Mocheld550d982006-06-27 00:41:40 -04001245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001249static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001251 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (!video)
1255 goto end;
1256
1257 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001258 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001260 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001262 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Len Brown4be44fc2005-08-05 00:44:28 -04001264 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Len Brown4be44fc2005-08-05 00:44:28 -04001268static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Len Brown4be44fc2005-08-05 00:44:28 -04001270 return single_open(file, acpi_video_bus_info_seq_show,
1271 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Len Brown4be44fc2005-08-05 00:44:28 -04001274static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001276 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 if (!video)
1280 goto end;
1281
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001282 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 seq_printf(seq, "<TODO>\n");
1284
Len Brown4be44fc2005-08-05 00:44:28 -04001285 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Len Brown4be44fc2005-08-05 00:44:28 -04001289static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1292}
1293
Len Brown4be44fc2005-08-05 00:44:28 -04001294static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001296 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001297 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001298 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (!video)
1302 goto end;
1303
1304 status = acpi_video_bus_POST_options(video, &options);
1305 if (ACPI_SUCCESS(status)) {
1306 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001307 printk(KERN_WARNING PREFIX
1308 "The motherboard VGA device is not listed as a possible POST device.\n");
1309 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001310 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Frank Seidel4d939152009-02-04 17:03:07 +01001312 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001313 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (options & 2)
1315 seq_printf(seq, " <PCI video>");
1316 if (options & 4)
1317 seq_printf(seq, " <AGP video>");
1318 seq_putc(seq, '\n');
1319 } else
1320 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001321 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325static int
Len Brown4be44fc2005-08-05 00:44:28 -04001326acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Len Brown4be44fc2005-08-05 00:44:28 -04001328 return single_open(file, acpi_video_bus_POST_info_seq_show,
1329 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Len Brown4be44fc2005-08-05 00:44:28 -04001332static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001334 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001335 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001336 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (!video)
1340 goto end;
1341
Len Brown4be44fc2005-08-05 00:44:28 -04001342 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!ACPI_SUCCESS(status)) {
1344 seq_printf(seq, "<not supported>\n");
1345 goto end;
1346 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001347 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Len Brown4be44fc2005-08-05 00:44:28 -04001349 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Len Brown4be44fc2005-08-05 00:44:28 -04001353static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001355 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Len Brown4be44fc2005-08-05 00:44:28 -04001358 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Patrick Mocheld550d982006-06-27 00:41:40 -04001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Len Brown4be44fc2005-08-05 00:44:28 -04001363static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Len Brown4be44fc2005-08-05 00:44:28 -04001365 return single_open(file, acpi_video_bus_POST_seq_show,
1366 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Len Brown4be44fc2005-08-05 00:44:28 -04001369static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1372}
1373
1374static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001375acpi_video_bus_write_POST(struct file *file,
1376 const char __user * buffer,
1377 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Len Brown4be44fc2005-08-05 00:44:28 -04001379 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001380 struct seq_file *m = file->private_data;
1381 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001382 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001383 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 status = acpi_video_bus_POST_options(video, &options);
1390 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001394 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 str[count] = 0;
1397 opt = strtoul(str, NULL, 0);
1398 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001399 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Julius Volz98fb8fe2007-02-20 16:38:40 +01001401 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 options |= 1;
1403
1404 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001405 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001407 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 }
1410
Patrick Mocheld550d982006-06-27 00:41:40 -04001411 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001415acpi_video_bus_write_DOS(struct file *file,
1416 const char __user * buffer,
1417 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Len Brown4be44fc2005-08-05 00:44:28 -04001419 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001420 struct seq_file *m = file->private_data;
1421 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001422 char str[12] = { 0 };
1423 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001427 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001430 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 str[count] = 0;
1433 opt = strtoul(str, NULL, 0);
1434 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001435 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Len Brown4be44fc2005-08-05 00:44:28 -04001437 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001440 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Patrick Mocheld550d982006-06-27 00:41:40 -04001442 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Len Brown4be44fc2005-08-05 00:44:28 -04001445static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001447 struct acpi_video_bus *video = acpi_driver_data(device);
1448 struct proc_dir_entry *device_dir;
1449 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001451 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1452 if (!device_dir)
1453 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001455 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001458 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001459 &acpi_video_bus_info_fops,
1460 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001462 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001465 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001466 &acpi_video_bus_ROM_fops,
1467 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001469 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001472 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001473 &acpi_video_bus_POST_info_fops,
1474 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001476 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001479 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001480 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001481 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001482 &acpi_video_bus_POST_fops,
1483 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001485 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001488 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001489 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001490 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001491 &acpi_video_bus_DOS_fops,
1492 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001494 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001496 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001497 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001498
1499 err_remove_post:
1500 remove_proc_entry("POST", device_dir);
1501 err_remove_post_info:
1502 remove_proc_entry("POST_info", device_dir);
1503 err_remove_rom:
1504 remove_proc_entry("ROM", device_dir);
1505 err_remove_info:
1506 remove_proc_entry("info", device_dir);
1507 err_remove_dir:
1508 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1509 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Len Brown4be44fc2005-08-05 00:44:28 -04001512static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001514 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001516 if (device_dir) {
1517 remove_proc_entry("info", device_dir);
1518 remove_proc_entry("ROM", device_dir);
1519 remove_proc_entry("POST_info", device_dir);
1520 remove_proc_entry("POST", device_dir);
1521 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001522 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 acpi_device_dir(device) = NULL;
1524 }
1525
Patrick Mocheld550d982006-06-27 00:41:40 -04001526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529/* --------------------------------------------------------------------------
1530 Driver Interface
1531 -------------------------------------------------------------------------- */
1532
1533/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001534static struct acpi_video_device_attrib*
1535acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1536{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001537 struct acpi_video_enumerated_device *ids;
1538 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001539
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001540 for (i = 0; i < video->attached_count; i++) {
1541 ids = &video->attached_array[i];
1542 if ((ids->value.int_val & 0xffff) == device_id)
1543 return &ids->value.attrib;
1544 }
1545
Rui Zhang82cae992007-01-03 23:40:53 -05001546 return NULL;
1547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549static int
Len Brown4be44fc2005-08-05 00:44:28 -04001550acpi_video_bus_get_one_device(struct acpi_device *device,
1551 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001553 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001554 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001555 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001556 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Len Brown4be44fc2005-08-05 00:44:28 -04001561 status =
1562 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (ACPI_SUCCESS(status)) {
1564
Burman Yan36bcbec2006-12-19 12:56:11 -08001565 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001567 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1570 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001571 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 data->device_id = device_id;
1574 data->video = video;
1575 data->dev = device;
1576
Rui Zhang82cae992007-01-03 23:40:53 -05001577 attribute = acpi_video_get_device_attr(video, device_id);
1578
1579 if((attribute != NULL) && attribute->device_id_scheme) {
1580 switch (attribute->display_type) {
1581 case ACPI_VIDEO_DISPLAY_CRT:
1582 data->flags.crt = 1;
1583 break;
1584 case ACPI_VIDEO_DISPLAY_TV:
1585 data->flags.tvout = 1;
1586 break;
1587 case ACPI_VIDEO_DISPLAY_DVI:
1588 data->flags.dvi = 1;
1589 break;
1590 case ACPI_VIDEO_DISPLAY_LCD:
1591 data->flags.lcd = 1;
1592 break;
1593 default:
1594 data->flags.unknown = 1;
1595 break;
1596 }
1597 if(attribute->bios_can_detect)
1598 data->flags.bios = 1;
1599 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 acpi_video_device_bind(video, data);
1603 acpi_video_device_find_cap(data);
1604
Patrick Mochel90130262006-05-19 16:54:48 -04001605 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001606 ACPI_DEVICE_NOTIFY,
1607 acpi_video_device_notify,
1608 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001610 printk(KERN_ERR PREFIX
1611 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001612 if(data->brightness)
1613 kfree(data->brightness->levels);
1614 kfree(data->brightness);
1615 kfree(data);
1616 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001619 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001621 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 acpi_video_device_add_fs(device);
1624
Patrick Mocheld550d982006-06-27 00:41:40 -04001625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627
Patrick Mocheld550d982006-06-27 00:41:40 -04001628 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631/*
1632 * Arg:
1633 * video : video bus device
1634 *
1635 * Return:
1636 * none
1637 *
1638 * Enumerate the video device list of the video bus,
1639 * bind the ids with the corresponding video devices
1640 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Len Brown4be44fc2005-08-05 00:44:28 -04001643static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001645 struct acpi_video_device *dev;
1646
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001647 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001648
1649 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001650 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001651
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001652 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655/*
1656 * Arg:
1657 * video : video bus device
1658 * device : video output device under the video
1659 * bus
1660 *
1661 * Return:
1662 * none
1663 *
1664 * Bind the ids with the corresponding video devices
1665 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668static void
Len Brown4be44fc2005-08-05 00:44:28 -04001669acpi_video_device_bind(struct acpi_video_bus *video,
1670 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001672 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001675 for (i = 0; i < video->attached_count; i++) {
1676 ids = &video->attached_array[i];
1677 if (device->device_id == (ids->value.int_val & 0xffff)) {
1678 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1680 }
1681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684/*
1685 * Arg:
1686 * video : video bus device
1687 *
1688 * Return:
1689 * < 0 : error
1690 *
1691 * Call _DOD to enumerate all devices attached to display adapter
1692 *
Len Brown4be44fc2005-08-05 00:44:28 -04001693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1696{
Len Brown4be44fc2005-08-05 00:44:28 -04001697 int status;
1698 int count;
1699 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001700 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001701 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1702 union acpi_object *dod = NULL;
1703 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Patrick Mochel90130262006-05-19 16:54:48 -04001705 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001707 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001711 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001713 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 status = -EFAULT;
1715 goto out;
1716 }
1717
1718 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001719 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001721 active_list = kcalloc(1 + dod->package.count,
1722 sizeof(struct acpi_video_enumerated_device),
1723 GFP_KERNEL);
1724 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 status = -ENOMEM;
1726 goto out;
1727 }
1728
1729 count = 0;
1730 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001731 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001734 printk(KERN_ERR PREFIX
1735 "Invalid _DOD data in element %d\n", i);
1736 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001738
1739 active_list[count].value.int_val = obj->integer.value;
1740 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001741 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1742 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 count++;
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Jesper Juhl6044ec82005-11-07 01:01:32 -08001746 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001747
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001748 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001750
1751 out:
Len Brown02438d82006-06-30 03:19:10 -04001752 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001753 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Len Brown4be44fc2005-08-05 00:44:28 -04001756static int
1757acpi_video_get_next_level(struct acpi_video_device *device,
1758 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001760 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001761 max = max_below = 0;
1762 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001763 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001764 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001765 l = device->brightness->levels[i];
1766 if (abs(l - level_current) < abs(delta)) {
1767 delta = l - level_current;
1768 if (!delta)
1769 break;
1770 }
1771 }
1772 /* Ajust level_current to closest available level */
1773 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001774 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001775 l = device->brightness->levels[i];
1776 if (l < min)
1777 min = l;
1778 if (l > max)
1779 max = l;
1780 if (l < min_above && l > level_current)
1781 min_above = l;
1782 if (l > max_below && l < level_current)
1783 max_below = l;
1784 }
1785
1786 switch (event) {
1787 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1788 return (level_current < max) ? min_above : min;
1789 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1790 return (level_current < max) ? min_above : max;
1791 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1792 return (level_current > min) ? max_below : min;
1793 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1794 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1795 return 0;
1796 default:
1797 return level_current;
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Zhang Ruic8890f92009-03-18 16:27:08 +08001801static int
Len Brown4be44fc2005-08-05 00:44:28 -04001802acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001804 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001805 int result = -EINVAL;
1806
Julia Jomantaite469778c2008-06-23 22:50:42 +01001807 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001808 goto out;
1809
1810 result = acpi_video_device_lcd_get_level_current(device,
1811 &level_current);
1812 if (result)
1813 goto out;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001816
Zhang Rui24450c72009-03-18 16:27:10 +08001817 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001818
1819out:
1820 if (result)
1821 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1822
1823 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
1826static int
Len Brown4be44fc2005-08-05 00:44:28 -04001827acpi_video_bus_get_devices(struct acpi_video_bus *video,
1828 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Len Brown4be44fc2005-08-05 00:44:28 -04001830 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001831 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 acpi_video_device_enumerate(video);
1834
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001835 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 status = acpi_video_bus_get_one_device(dev, video);
1838 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001839 printk(KERN_WARNING PREFIX
1840 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 continue;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001844 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Len Brown4be44fc2005-08-05 00:44:28 -04001847static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Karol Kozimor031ec772005-07-30 04:18:00 -04001849 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct acpi_video_bus *video;
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001854 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 video = device->video;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 acpi_video_device_remove_fs(device->dev);
1859
Patrick Mochel90130262006-05-19 16:54:48 -04001860 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001861 ACPI_DEVICE_NOTIFY,
1862 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001863 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001864 if (device->cdev) {
1865 sysfs_remove_link(&device->dev->dev.kobj,
1866 "thermal_cooling");
1867 sysfs_remove_link(&device->cdev->device.kobj,
1868 "device");
1869 thermal_cooling_device_unregister(device->cdev);
1870 device->cdev = NULL;
1871 }
Luming Yu23b0f012007-05-09 21:07:05 +08001872 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001873
Patrick Mocheld550d982006-06-27 00:41:40 -04001874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Len Brown4be44fc2005-08-05 00:44:28 -04001877static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Len Brown4be44fc2005-08-05 00:44:28 -04001879 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001880 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001882 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001884 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001886 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001887 if (ACPI_FAILURE(status))
1888 printk(KERN_WARNING PREFIX
1889 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001891 if (dev->brightness) {
1892 kfree(dev->brightness->levels);
1893 kfree(dev->brightness);
1894 }
1895 list_del(&dev->entry);
1896 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001899 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001900
Patrick Mocheld550d982006-06-27 00:41:40 -04001901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904/* acpi_video interface */
1905
Len Brown4be44fc2005-08-05 00:44:28 -04001906static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Zhang Ruia21101c2007-09-14 11:46:22 +08001908 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Len Brown4be44fc2005-08-05 00:44:28 -04001911static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 return acpi_video_bus_DOS(video, 0, 1);
1914}
1915
Len Brown4be44fc2005-08-05 00:44:28 -04001916static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001918 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001919 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001920 struct input_dev *input;
1921 int keycode;
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001926 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001927 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001930 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001932 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001933 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 break;
1935
Julius Volz98fb8fe2007-02-20 16:38:40 +01001936 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * connector. */
1938 acpi_video_device_enumerate(video);
1939 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001940 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001941 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943
Len Brown4be44fc2005-08-05 00:44:28 -04001944 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001945 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001946 keycode = KEY_SWITCHVIDEOMODE;
1947 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001948 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001949 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001950 keycode = KEY_VIDEO_NEXT;
1951 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001952 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001953 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001954 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956
1957 default:
Luming Yue9dab192007-08-20 18:23:53 +08001958 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001960 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 break;
1962 }
1963
Zhang Rui7761f632008-01-25 14:48:12 +08001964 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001965 input_report_key(input, keycode, 1);
1966 input_sync(input);
1967 input_report_key(input, keycode, 0);
1968 input_sync(input);
1969
Patrick Mocheld550d982006-06-27 00:41:40 -04001970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Len Brown4be44fc2005-08-05 00:44:28 -04001973static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001975 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001976 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001977 struct acpi_video_bus *bus;
1978 struct input_dev *input;
1979 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001984 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001985 bus = video_device->video;
1986 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001989 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001990 if (brightness_switch_enabled)
1991 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001992 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001993 keycode = KEY_BRIGHTNESS_CYCLE;
1994 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001995 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001996 if (brightness_switch_enabled)
1997 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001998 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001999 keycode = KEY_BRIGHTNESSUP;
2000 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002001 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08002002 if (brightness_switch_enabled)
2003 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002004 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002005 keycode = KEY_BRIGHTNESSDOWN;
2006 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002007 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08002008 if (brightness_switch_enabled)
2009 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04002010 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002011 keycode = KEY_BRIGHTNESS_ZERO;
2012 break;
Len Brown4be44fc2005-08-05 00:44:28 -04002013 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08002014 if (brightness_switch_enabled)
2015 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04002016 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002017 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 break;
2019 default:
Luming Yue9dab192007-08-20 18:23:53 +08002020 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04002022 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 break;
2024 }
Luming Yue9dab192007-08-20 18:23:53 +08002025
Zhang Rui7761f632008-01-25 14:48:12 +08002026 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002027 input_report_key(input, keycode, 1);
2028 input_sync(input);
2029 input_report_key(input, keycode, 0);
2030 input_sync(input);
2031
Patrick Mocheld550d982006-06-27 00:41:40 -04002032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Zhang Ruie6d9da12007-08-25 02:23:31 -04002035static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002036static int acpi_video_resume(struct acpi_device *device)
2037{
2038 struct acpi_video_bus *video;
2039 struct acpi_video_device *video_device;
2040 int i;
2041
2042 if (!device || !acpi_driver_data(device))
2043 return -EINVAL;
2044
2045 video = acpi_driver_data(device);
2046
2047 for (i = 0; i < video->attached_count; i++) {
2048 video_device = video->attached_array[i].bind_info;
2049 if (video_device && video_device->backlight)
2050 acpi_video_set_brightness(video_device->backlight);
2051 }
2052 return AE_OK;
2053}
2054
Len Brown4be44fc2005-08-05 00:44:28 -04002055static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002057 acpi_status status;
2058 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002059 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002060 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Burman Yan36bcbec2006-12-19 12:56:11 -08002062 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002064 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Zhang Ruie6d9da12007-08-25 02:23:31 -04002066 /* a hack to fix the duplicate name "VID" problem on T61 */
2067 if (!strcmp(device->pnp.bus_id, "VID")) {
2068 if (instance)
2069 device->pnp.bus_id[3] = '0' + instance;
2070 instance ++;
2071 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002072 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2073 if (!strcmp(device->pnp.bus_id, "VGA")) {
2074 if (instance)
2075 device->pnp.bus_id[3] = '0' + instance;
2076 instance++;
2077 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002078
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002079 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2081 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002082 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002085 error = acpi_video_bus_check(video);
2086 if (error)
2087 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002089 error = acpi_video_bus_add_fs(device);
2090 if (error)
2091 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002093 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 INIT_LIST_HEAD(&video->video_device_list);
2095
2096 acpi_video_bus_get_devices(video, device);
2097 acpi_video_bus_start_devices(video);
2098
Patrick Mochel90130262006-05-19 16:54:48 -04002099 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002100 ACPI_DEVICE_NOTIFY,
2101 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002103 printk(KERN_ERR PREFIX
2104 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002105 error = -ENODEV;
2106 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Luming Yue9dab192007-08-20 18:23:53 +08002109 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002110 if (!input) {
2111 error = -ENOMEM;
2112 goto err_uninstall_notify;
2113 }
Luming Yue9dab192007-08-20 18:23:53 +08002114
2115 snprintf(video->phys, sizeof(video->phys),
2116 "%s/video/input0", acpi_device_hid(video->device));
2117
2118 input->name = acpi_device_name(video->device);
2119 input->phys = video->phys;
2120 input->id.bustype = BUS_HOST;
2121 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002122 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002123 input->evbit[0] = BIT(EV_KEY);
2124 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2125 set_bit(KEY_VIDEO_NEXT, input->keybit);
2126 set_bit(KEY_VIDEO_PREV, input->keybit);
2127 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2128 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2129 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2130 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2131 set_bit(KEY_DISPLAY_OFF, input->keybit);
2132 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002133
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002134 error = input_register_device(input);
2135 if (error)
2136 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002139 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2140 video->flags.multihead ? "yes" : "no",
2141 video->flags.rom ? "yes" : "no",
2142 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002146 err_free_input_dev:
2147 input_free_device(input);
2148 err_uninstall_notify:
2149 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2150 acpi_video_bus_notify);
2151 err_stop_video:
2152 acpi_video_bus_stop_devices(video);
2153 acpi_video_bus_put_devices(video);
2154 kfree(video->attached_array);
2155 acpi_video_bus_remove_fs(device);
2156 err_free_video:
2157 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002158 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002159
2160 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Len Brown4be44fc2005-08-05 00:44:28 -04002163static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Len Brown4be44fc2005-08-05 00:44:28 -04002165 acpi_status status = 0;
2166 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002172 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 acpi_video_bus_stop_devices(video);
2175
Patrick Mochel90130262006-05-19 16:54:48 -04002176 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002177 ACPI_DEVICE_NOTIFY,
2178 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 acpi_video_bus_put_devices(video);
2181 acpi_video_bus_remove_fs(device);
2182
Luming Yue9dab192007-08-20 18:23:53 +08002183 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002184 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 kfree(video);
2186
Patrick Mocheld550d982006-06-27 00:41:40 -04002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Len Brown4be44fc2005-08-05 00:44:28 -04002190static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Len Brown4be44fc2005-08-05 00:44:28 -04002192 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2195 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002196 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 acpi_video_dir->owner = THIS_MODULE;
2198
2199 result = acpi_bus_register_driver(&acpi_video_bus);
2200 if (result < 0) {
2201 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002202 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204
Patrick Mocheld550d982006-06-27 00:41:40 -04002205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Len Brown4be44fc2005-08-05 00:44:28 -04002208static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 acpi_bus_unregister_driver(&acpi_video_bus);
2212
2213 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2214
Patrick Mocheld550d982006-06-27 00:41:40 -04002215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218module_init(acpi_video_init);
2219module_exit(acpi_video_exit);