blob: 52f52b32b63fc771e219b0f20475d9e4db4f6b40 [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
171struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400172 int curr;
173 int count;
174 int *levels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400178 unsigned long device_id;
179 struct acpi_video_device_flags flags;
180 struct acpi_video_device_cap cap;
181 struct list_head entry;
182 struct acpi_video_bus *video;
183 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800185 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800186 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800187 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* bus */
191static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
192static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700193 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_video_bus_info_open_fs,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
201static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700202 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_video_bus_ROM_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
210 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_video_bus_POST_info_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
220static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700221 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400222 .open = acpi_video_bus_POST_open_fs,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
229static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700230 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400231 .open = acpi_video_bus_DOS_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400238static int acpi_video_device_info_open_fs(struct inode *inode,
239 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700241 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400242 .open = acpi_video_device_info_open_fs,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248static int acpi_video_device_state_open_fs(struct inode *inode,
249 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700251 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 .open = acpi_video_device_state_open_fs,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_video_device_brightness_open_fs(struct inode *inode,
259 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700261 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400262 .open = acpi_video_device_brightness_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_EDID_open_fs(struct inode *inode,
269 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700271 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .open = acpi_video_device_EDID_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 "motherboard VGA device",
280 "PCI VGA device",
281 "AGP VGA device",
282 "UNKNOWN",
283};
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
286static void acpi_video_device_rebind(struct acpi_video_bus *video);
287static void acpi_video_device_bind(struct acpi_video_bus *video,
288 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800290static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
291 int level);
292static int acpi_video_device_lcd_get_level_current(
293 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400294 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_video_get_next_level(struct acpi_video_device *device,
296 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800297static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400298 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800299static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400300 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800301static int acpi_video_output_get(struct output_device *od);
302static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Yu Luming2f3d0002006-11-11 02:40:34 +0800304/*backlight device sysfs support*/
305static int acpi_video_get_brightness(struct backlight_device *bd)
306{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400307 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000308 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800309 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100310 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800311
312 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
313 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000314 for (i = 2; i < vd->brightness->count; i++) {
315 if (vd->brightness->levels[i] == cur_level)
316 /* The first two entries are special - see page 575
317 of the ACPI spec 3.0 */
318 return i-2;
319 }
320 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800321}
322
323static int acpi_video_set_brightness(struct backlight_device *bd)
324{
Zhang Rui24450c72009-03-18 16:27:10 +0800325 int request_level = bd->props.brightness + 2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800326 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100327 (struct acpi_video_device *)bl_get_data(bd);
Zhang Rui24450c72009-03-18 16:27:10 +0800328
329 return acpi_video_device_lcd_set_level(vd,
330 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800331}
332
Richard Purdie599a52d2007-02-10 23:07:48 +0000333static struct backlight_ops acpi_backlight_ops = {
334 .get_brightness = acpi_video_get_brightness,
335 .update_status = acpi_video_set_brightness,
336};
337
Luming Yu23b0f012007-05-09 21:07:05 +0800338/*video output device sysfs support*/
339static int acpi_video_output_get(struct output_device *od)
340{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400341 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800342 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700343 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800344 acpi_video_device_get_state(vd, &state);
345 return (int)state;
346}
347
348static int acpi_video_output_set(struct output_device *od)
349{
350 unsigned long state = od->request_state;
351 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700352 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800353 return acpi_video_device_set_state(vd, state);
354}
355
356static struct output_properties acpi_output_properties = {
357 .set_state = acpi_video_output_set,
358 .get_status = acpi_video_output_get,
359};
Zhang Rui702ed512008-01-17 15:51:22 +0800360
361
362/* thermal cooling device callbacks */
363static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
364{
365 struct acpi_device *device = cdev->devdata;
366 struct acpi_video_device *video = acpi_driver_data(device);
367
368 return sprintf(buf, "%d\n", video->brightness->count - 3);
369}
370
371static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
372{
373 struct acpi_device *device = cdev->devdata;
374 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400375 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800376 int state;
377
Zhang Ruic8890f92009-03-18 16:27:08 +0800378 if (acpi_video_device_lcd_get_level_current(video, &level))
379 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800380 for (state = 2; state < video->brightness->count; state++)
381 if (level == video->brightness->levels[state])
382 return sprintf(buf, "%d\n",
383 video->brightness->count - state - 1);
384
385 return -EINVAL;
386}
387
388static int
389video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
390{
391 struct acpi_device *device = cdev->devdata;
392 struct acpi_video_device *video = acpi_driver_data(device);
393 int level;
394
395 if ( state >= video->brightness->count - 2)
396 return -EINVAL;
397
398 state = video->brightness->count - state;
399 level = video->brightness->levels[state -1];
400 return acpi_video_device_lcd_set_level(video, level);
401}
402
403static struct thermal_cooling_device_ops video_cooling_ops = {
404 .get_max_state = video_get_max_state,
405 .get_cur_state = video_get_cur_state,
406 .set_cur_state = video_set_cur_state,
407};
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* --------------------------------------------------------------------------
410 Video Management
411 -------------------------------------------------------------------------- */
412
413/* device */
414
415static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400416acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Len Brown4be44fc2005-08-05 00:44:28 -0400418 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400419
420 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Patrick Mocheld550d982006-06-27 00:41:40 -0400422 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static int
Len Brown4be44fc2005-08-05 00:44:28 -0400426acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400427 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Len Brown4be44fc2005-08-05 00:44:28 -0400429 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mochel90130262006-05-19 16:54:48 -0400431 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static int
Len Brown4be44fc2005-08-05 00:44:28 -0400437acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Len Brown4be44fc2005-08-05 00:44:28 -0400439 int status;
440 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
441 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400442 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400446 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Patrick Mocheld550d982006-06-27 00:41:40 -0400448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static int
Len Brown4be44fc2005-08-05 00:44:28 -0400452acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
453 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Len Brown4be44fc2005-08-05 00:44:28 -0400455 int status;
456 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
457 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 *levels = NULL;
461
Patrick Mochel90130262006-05-19 16:54:48 -0400462 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400464 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400465 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500466 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400467 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 status = -EFAULT;
469 goto err;
470 }
471
472 *levels = obj;
473
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800477 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Patrick Mocheld550d982006-06-27 00:41:40 -0400479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static int
Len Brown4be44fc2005-08-05 00:44:28 -0400483acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Zhang Rui24450c72009-03-18 16:27:10 +0800485 int status;
Len Brown4be44fc2005-08-05 00:44:28 -0400486 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
487 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800488 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Zhang Rui24450c72009-03-18 16:27:10 +0800492 status = acpi_evaluate_object(device->dev->handle, "_BCM",
493 &args, NULL);
494 if (ACPI_FAILURE(status)) {
495 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
496 return -EIO;
497 }
498
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400499 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800500 for (state = 2; state < device->brightness->count; state++)
Zhang Rui24450c72009-03-18 16:27:10 +0800501 if (level == device->brightness->levels[state]) {
Zhang Rui9e6dada2008-12-31 10:58:48 +0800502 device->backlight->props.brightness = state - 2;
Zhang Rui24450c72009-03-18 16:27:10 +0800503 return 0;
504 }
Zhang Rui9e6dada2008-12-31 10:58:48 +0800505
Zhang Rui24450c72009-03-18 16:27:10 +0800506 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510static int
Len Brown4be44fc2005-08-05 00:44:28 -0400511acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400512 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Zhang Ruic8890f92009-03-18 16:27:08 +0800514 acpi_status status = AE_OK;
515
516 if (device->cap._BQC) {
517 status = acpi_evaluate_integer(device->dev->handle, "_BQC",
518 NULL, level);
519 if (ACPI_SUCCESS(status)) {
520 device->brightness->curr = *level;
521 return 0;
522 } else {
523 /* Fixme:
524 * should we return an error or ignore this failure?
525 * dev->brightness->curr is a cached value which stores
526 * the correct current backlight level in most cases.
527 * ACPI video backlight still works w/ buggy _BQC.
528 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
529 */
530 ACPI_WARNING((AE_INFO, "Evaluating _BQC failed"));
531 device->cap._BQC = 0;
532 }
533 }
534
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400535 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539static int
Len Brown4be44fc2005-08-05 00:44:28 -0400540acpi_video_device_EDID(struct acpi_video_device *device,
541 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Len Brown4be44fc2005-08-05 00:44:28 -0400543 int status;
544 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
545 union acpi_object *obj;
546 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
547 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 *edid = NULL;
551
552 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (length == 128)
555 arg0.integer.value = 1;
556 else if (length == 256)
557 arg0.integer.value = 2;
558 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Patrick Mochel90130262006-05-19 16:54:48 -0400561 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400563 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200565 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (obj && obj->type == ACPI_TYPE_BUFFER)
568 *edid = obj;
569 else {
Len Brown64684632006-06-26 23:41:38 -0400570 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 status = -EFAULT;
572 kfree(obj);
573 }
574
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/* bus */
579
580static int
Len Brown4be44fc2005-08-05 00:44:28 -0400581acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Len Brown4be44fc2005-08-05 00:44:28 -0400583 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400584 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400585 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
586 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 arg0.integer.value = option;
590
Patrick Mochel90130262006-05-19 16:54:48 -0400591 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400593 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Patrick Mocheld550d982006-06-27 00:41:40 -0400595 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400599acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 int status;
602
Patrick Mochel90130262006-05-19 16:54:48 -0400603 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static int
Len Brown4be44fc2005-08-05 00:44:28 -0400609acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400610 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown4be44fc2005-08-05 00:44:28 -0400612 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Patrick Mochel90130262006-05-19 16:54:48 -0400614 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 *options &= 3;
616
Patrick Mocheld550d982006-06-27 00:41:40 -0400617 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
621 * Arg:
622 * video : video bus device pointer
623 * bios_flag :
624 * 0. The system BIOS should NOT automatically switch(toggle)
625 * the active display output.
626 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100627 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * 2. The _DGS value should be locked.
629 * 3. The system BIOS should not automatically switch (toggle) the
630 * active display output, but instead generate the display switch
631 * event notify code.
632 * lcd_flag :
633 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100634 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100636 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * Return Value:
638 * -1 wrong arg.
639 */
640
641static int
Len Brown4be44fc2005-08-05 00:44:28 -0400642acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Len Brown4be44fc2005-08-05 00:44:28 -0400644 acpi_integer status = 0;
645 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
646 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Len Brown4be44fc2005-08-05 00:44:28 -0400649 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 status = -1;
651 goto Failed;
652 }
653 arg0.integer.value = (lcd_flag << 2) | bios_flag;
654 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400655 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Len Brown4be44fc2005-08-05 00:44:28 -0400657 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400658 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500662 * Simple comparison function used to sort backlight levels.
663 */
664
665static int
666acpi_video_cmp_level(const void *a, const void *b)
667{
668 return *(int *)a - *(int *)b;
669}
670
671/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * Arg:
673 * device : video output device (LCD, CRT, ..)
674 *
675 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100676 * Maximum brightness level
677 *
678 * Allocate and initialize device->brightness.
679 */
680
681static int
682acpi_video_init_brightness(struct acpi_video_device *device)
683{
684 union acpi_object *obj = NULL;
685 int i, max_level = 0, count = 0;
686 union acpi_object *o;
687 struct acpi_video_device_brightness *br = NULL;
688
689 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
690 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
691 "LCD brightness level\n"));
692 goto out;
693 }
694
695 if (obj->package.count < 2)
696 goto out;
697
698 br = kzalloc(sizeof(*br), GFP_KERNEL);
699 if (!br) {
700 printk(KERN_ERR "can't allocate memory\n");
701 goto out;
702 }
703
704 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
705 GFP_KERNEL);
706 if (!br->levels)
707 goto out_free;
708
709 for (i = 0; i < obj->package.count; i++) {
710 o = (union acpi_object *)&obj->package.elements[i];
711 if (o->type != ACPI_TYPE_INTEGER) {
712 printk(KERN_ERR PREFIX "Invalid data\n");
713 continue;
714 }
715 br->levels[count] = (u32) o->integer.value;
716
717 if (br->levels[count] > max_level)
718 max_level = br->levels[count];
719 count++;
720 }
721
Zhang Rui935e5f22008-12-11 16:24:52 -0500722 /* don't sort the first two brightness levels */
723 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
724 acpi_video_cmp_level, NULL);
725
Julia Jomantaite469778c2008-06-23 22:50:42 +0100726 if (count < 2)
727 goto out_free_levels;
728
729 br->count = count;
730 device->brightness = br;
731 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
732 kfree(obj);
733 return max_level;
734
735out_free_levels:
736 kfree(br->levels);
737out_free:
738 kfree(br);
739out:
740 device->brightness = NULL;
741 kfree(obj);
742 return 0;
743}
744
745/*
746 * Arg:
747 * device : video output device (LCD, CRT, ..)
748 *
749 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * None
751 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100752 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * device.
754 */
755
Len Brown4be44fc2005-08-05 00:44:28 -0400756static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800759 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
William Lee Irwin III98934de2007-12-12 03:56:55 -0800762 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick Mochel90130262006-05-19 16:54:48 -0400764 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 device->cap._ADR = 1;
766 }
Patrick Mochel90130262006-05-19 16:54:48 -0400767 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400768 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Patrick Mochel90130262006-05-19 16:54:48 -0400770 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400771 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800773 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
774 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400775 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400776 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Patrick Mochel90130262006-05-19 16:54:48 -0400778 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 device->cap._DCS = 1;
780 }
Patrick Mochel90130262006-05-19 16:54:48 -0400781 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 device->cap._DGS = 1;
783 }
Patrick Mochel90130262006-05-19 16:54:48 -0400784 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 device->cap._DSS = 1;
786 }
787
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200788 if (acpi_video_backlight_support())
789 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Zhao Yakuic2c78902008-07-17 10:46:05 +0800791 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800792 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800793 static int count = 0;
794 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800795 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
796 if (!name)
797 return;
798
Yu Luming2f3d0002006-11-11 02:40:34 +0800799 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800800 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000801 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000802 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800803 /*
804 * If there exists the _BQC object, the _BQC object will be
805 * called to get the current backlight brightness. Otherwise
806 * the brightness will be set to the maximum.
807 */
808 if (device->cap._BQC)
809 device->backlight->props.brightness =
810 acpi_video_get_brightness(device->backlight);
811 else
812 device->backlight->props.brightness =
813 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000814 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800815 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800816
817 device->cdev = thermal_cooling_device_register("LCD",
818 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500819 if (IS_ERR(device->cdev))
820 return;
821
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200822 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
823 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800824 result = sysfs_create_link(&device->dev->dev.kobj,
825 &device->cdev->device.kobj,
826 "thermal_cooling");
827 if (result)
828 printk(KERN_ERR PREFIX "Create sysfs link\n");
829 result = sysfs_create_link(&device->cdev->device.kobj,
830 &device->dev->dev.kobj, "device");
831 if (result)
832 printk(KERN_ERR PREFIX "Create sysfs link\n");
833
Yu Luming2f3d0002006-11-11 02:40:34 +0800834 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200835
836 if (acpi_video_display_switch_support()) {
837
838 if (device->cap._DCS && device->cap._DSS) {
839 static int count;
840 char *name;
841 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
842 if (!name)
843 return;
844 sprintf(name, "acpi_video%d", count++);
845 device->output_dev = video_output_register(name,
846 NULL, device, &acpi_output_properties);
847 kfree(name);
848 }
Luming Yu23b0f012007-05-09 21:07:05 +0800849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*
853 * Arg:
854 * device : video output device (VGA)
855 *
856 * Return Value:
857 * None
858 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100859 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
861
Len Brown4be44fc2005-08-05 00:44:28 -0400862static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Len Brown4be44fc2005-08-05 00:44:28 -0400864 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
William Lee Irwin III98934de2007-12-12 03:56:55 -0800866 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400867 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 video->cap._DOS = 1;
869 }
Patrick Mochel90130262006-05-19 16:54:48 -0400870 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 video->cap._DOD = 1;
872 }
Patrick Mochel90130262006-05-19 16:54:48 -0400873 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 video->cap._ROM = 1;
875 }
Patrick Mochel90130262006-05-19 16:54:48 -0400876 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 video->cap._GPD = 1;
878 }
Patrick Mochel90130262006-05-19 16:54:48 -0400879 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 video->cap._SPD = 1;
881 }
Patrick Mochel90130262006-05-19 16:54:48 -0400882 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 video->cap._VPO = 1;
884 }
885}
886
887/*
888 * Check whether the video bus device has required AML method to
889 * support the desired features
890 */
891
Len Brown4be44fc2005-08-05 00:44:28 -0400892static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Len Brown4be44fc2005-08-05 00:44:28 -0400894 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200895 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400898 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Thomas Renninger22c13f92008-08-01 17:37:54 +0200900 dev = acpi_get_physical_pci_device(video->device->handle);
901 if (!dev)
902 return -ENODEV;
903 put_device(dev);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* Since there is no HID, CID and so on for VGA driver, we have
906 * to check well known required nodes.
907 */
908
Julius Volz98fb8fe2007-02-20 16:38:40 +0100909 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400910 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 video->flags.multihead = 1;
912 status = 0;
913 }
914
Julius Volz98fb8fe2007-02-20 16:38:40 +0100915 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400916 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 video->flags.rom = 1;
918 status = 0;
919 }
920
Julius Volz98fb8fe2007-02-20 16:38:40 +0100921 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400922 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 video->flags.post = 1;
924 status = 0;
925 }
926
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/* --------------------------------------------------------------------------
931 FS Interface (/proc)
932 -------------------------------------------------------------------------- */
933
Len Brown4be44fc2005-08-05 00:44:28 -0400934static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936/* video devices */
937
Len Brown4be44fc2005-08-05 00:44:28 -0400938static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200940 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!dev)
944 goto end;
945
946 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
947 seq_printf(seq, "type: ");
948 if (dev->flags.crt)
949 seq_printf(seq, "CRT\n");
950 else if (dev->flags.lcd)
951 seq_printf(seq, "LCD\n");
952 else if (dev->flags.tvout)
953 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500954 else if (dev->flags.dvi)
955 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 else
957 seq_printf(seq, "UNKNOWN\n");
958
Len Brown4be44fc2005-08-05 00:44:28 -0400959 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Len Brown4be44fc2005-08-05 00:44:28 -0400961 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965static int
Len Brown4be44fc2005-08-05 00:44:28 -0400966acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 return single_open(file, acpi_video_device_info_seq_show,
969 PDE(inode)->data);
970}
971
Len Brown4be44fc2005-08-05 00:44:28 -0400972static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Len Brown4be44fc2005-08-05 00:44:28 -0400974 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200975 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400976 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (!dev)
980 goto end;
981
982 status = acpi_video_device_get_state(dev, &state);
983 seq_printf(seq, "state: ");
984 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400985 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 else
987 seq_printf(seq, "<not supported>\n");
988
989 status = acpi_video_device_query(dev, &state);
990 seq_printf(seq, "query: ");
991 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400992 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 else
994 seq_printf(seq, "<not supported>\n");
995
Len Brown4be44fc2005-08-05 00:44:28 -0400996 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000static int
Len Brown4be44fc2005-08-05 00:44:28 -04001001acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 return single_open(file, acpi_video_device_state_seq_show,
1004 PDE(inode)->data);
1005}
1006
1007static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001008acpi_video_device_write_state(struct file *file,
1009 const char __user * buffer,
1010 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Len Brown4be44fc2005-08-05 00:44:28 -04001012 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001013 struct seq_file *m = file->private_data;
1014 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001015 char str[12] = { 0 };
1016 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001020 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001023 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 str[count] = 0;
1026 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001027 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 status = acpi_video_device_set_state(dev, state);
1030
1031 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001032 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick Mocheld550d982006-06-27 00:41:40 -04001034 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static int
Len Brown4be44fc2005-08-05 00:44:28 -04001038acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001040 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001041 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (!dev || !dev->brightness) {
1045 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
1049 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001050 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 seq_printf(seq, " %d", dev->brightness->levels[i]);
1052 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1053
Patrick Mocheld550d982006-06-27 00:41:40 -04001054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static int
Len Brown4be44fc2005-08-05 00:44:28 -04001058acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 return single_open(file, acpi_video_device_brightness_seq_show,
1061 PDE(inode)->data);
1062}
1063
1064static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001065acpi_video_device_write_brightness(struct file *file,
1066 const char __user * buffer,
1067 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001069 struct seq_file *m = file->private_data;
1070 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001071 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001072 unsigned int level = 0;
1073 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Thomas Renninger59d399d2005-11-08 05:27:00 -05001076 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001077 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001080 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 str[count] = 0;
1083 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001086 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Julius Volz98fb8fe2007-02-20 16:38:40 +01001088 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001089 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (level == dev->brightness->levels[i]) {
Zhang Rui24450c72009-03-18 16:27:10 +08001091 if (!acpi_video_device_lcd_set_level(dev, level))
1092 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 }
1095
Zhang Rui24450c72009-03-18 16:27:10 +08001096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Len Brown4be44fc2005-08-05 00:44:28 -04001099static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001101 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001102 int status;
1103 int i;
1104 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (!dev)
1108 goto out;
1109
Len Brown4be44fc2005-08-05 00:44:28 -04001110 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001112 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
1115 if (ACPI_FAILURE(status)) {
1116 goto out;
1117 }
1118
1119 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1120 for (i = 0; i < edid->buffer.length; i++)
1121 seq_putc(seq, edid->buffer.pointer[i]);
1122 }
1123
Len Brown4be44fc2005-08-05 00:44:28 -04001124 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (!edid)
1126 seq_printf(seq, "<not supported>\n");
1127 else
1128 kfree(edid);
1129
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static int
Len Brown4be44fc2005-08-05 00:44:28 -04001134acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 return single_open(file, acpi_video_device_EDID_seq_show,
1137 PDE(inode)->data);
1138}
1139
Len Brown4be44fc2005-08-05 00:44:28 -04001140static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001142 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct acpi_video_device *vid_dev;
1144
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001145 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001147 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001149 device_dir = proc_mkdir(acpi_device_bid(device),
1150 vid_dev->video->dir);
1151 if (!device_dir)
1152 return -ENOMEM;
1153
1154 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001157 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001158 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001160 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001163 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1164 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001165 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001166 &acpi_video_device_state_fops,
1167 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001169 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001172 acpi_video_device_brightness_fops.write =
1173 acpi_video_device_write_brightness;
1174 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001175 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001176 &acpi_video_device_brightness_fops,
1177 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001179 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001182 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001183 &acpi_video_device_EDID_fops,
1184 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001186 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001188 acpi_device_dir(device) = device_dir;
1189
Patrick Mocheld550d982006-06-27 00:41:40 -04001190 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001191
1192 err_remove_brightness:
1193 remove_proc_entry("brightness", device_dir);
1194 err_remove_state:
1195 remove_proc_entry("state", device_dir);
1196 err_remove_info:
1197 remove_proc_entry("info", device_dir);
1198 err_remove_dir:
1199 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1200 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Len Brown4be44fc2005-08-05 00:44:28 -04001203static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001206 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001208 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001210 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001212 device_dir = acpi_device_dir(device);
1213 if (device_dir) {
1214 remove_proc_entry("info", device_dir);
1215 remove_proc_entry("state", device_dir);
1216 remove_proc_entry("brightness", device_dir);
1217 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001218 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 acpi_device_dir(device) = NULL;
1220 }
1221
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001226static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001228 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (!video)
1232 goto end;
1233
1234 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001235 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001237 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001239 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Len Brown4be44fc2005-08-05 00:44:28 -04001241 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Len Brown4be44fc2005-08-05 00:44:28 -04001245static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Len Brown4be44fc2005-08-05 00:44:28 -04001247 return single_open(file, acpi_video_bus_info_seq_show,
1248 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Len Brown4be44fc2005-08-05 00:44:28 -04001251static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001253 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (!video)
1257 goto end;
1258
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001259 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 seq_printf(seq, "<TODO>\n");
1261
Len Brown4be44fc2005-08-05 00:44:28 -04001262 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Len Brown4be44fc2005-08-05 00:44:28 -04001266static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1269}
1270
Len Brown4be44fc2005-08-05 00:44:28 -04001271static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001273 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001274 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001275 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (!video)
1279 goto end;
1280
1281 status = acpi_video_bus_POST_options(video, &options);
1282 if (ACPI_SUCCESS(status)) {
1283 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001284 printk(KERN_WARNING PREFIX
1285 "The motherboard VGA device is not listed as a possible POST device.\n");
1286 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001287 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Frank Seidel4d939152009-02-04 17:03:07 +01001289 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001290 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (options & 2)
1292 seq_printf(seq, " <PCI video>");
1293 if (options & 4)
1294 seq_printf(seq, " <AGP video>");
1295 seq_putc(seq, '\n');
1296 } else
1297 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001298 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302static int
Len Brown4be44fc2005-08-05 00:44:28 -04001303acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Len Brown4be44fc2005-08-05 00:44:28 -04001305 return single_open(file, acpi_video_bus_POST_info_seq_show,
1306 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Len Brown4be44fc2005-08-05 00:44:28 -04001309static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001311 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001312 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001313 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (!video)
1317 goto end;
1318
Len Brown4be44fc2005-08-05 00:44:28 -04001319 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!ACPI_SUCCESS(status)) {
1321 seq_printf(seq, "<not supported>\n");
1322 goto end;
1323 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001324 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Len Brown4be44fc2005-08-05 00:44:28 -04001326 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Len Brown4be44fc2005-08-05 00:44:28 -04001330static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001332 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Len Brown4be44fc2005-08-05 00:44:28 -04001335 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Patrick Mocheld550d982006-06-27 00:41:40 -04001337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Len Brown4be44fc2005-08-05 00:44:28 -04001340static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Len Brown4be44fc2005-08-05 00:44:28 -04001342 return single_open(file, acpi_video_bus_POST_seq_show,
1343 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Len Brown4be44fc2005-08-05 00:44:28 -04001346static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1349}
1350
1351static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001352acpi_video_bus_write_POST(struct file *file,
1353 const char __user * buffer,
1354 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Len Brown4be44fc2005-08-05 00:44:28 -04001356 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001357 struct seq_file *m = file->private_data;
1358 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001359 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001360 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 status = acpi_video_bus_POST_options(video, &options);
1367 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001371 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 str[count] = 0;
1374 opt = strtoul(str, NULL, 0);
1375 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001376 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Julius Volz98fb8fe2007-02-20 16:38:40 +01001378 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 options |= 1;
1380
1381 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001382 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001384 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 }
1387
Patrick Mocheld550d982006-06-27 00:41:40 -04001388 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001392acpi_video_bus_write_DOS(struct file *file,
1393 const char __user * buffer,
1394 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Len Brown4be44fc2005-08-05 00:44:28 -04001396 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001397 struct seq_file *m = file->private_data;
1398 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001399 char str[12] = { 0 };
1400 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001407 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 str[count] = 0;
1410 opt = strtoul(str, NULL, 0);
1411 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001412 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Len Brown4be44fc2005-08-05 00:44:28 -04001414 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001417 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Patrick Mocheld550d982006-06-27 00:41:40 -04001419 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Len Brown4be44fc2005-08-05 00:44:28 -04001422static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001424 struct acpi_video_bus *video = acpi_driver_data(device);
1425 struct proc_dir_entry *device_dir;
1426 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001428 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1429 if (!device_dir)
1430 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001432 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001435 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001436 &acpi_video_bus_info_fops,
1437 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001439 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001442 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001443 &acpi_video_bus_ROM_fops,
1444 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001446 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001449 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001450 &acpi_video_bus_POST_info_fops,
1451 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001453 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001456 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001457 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001458 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001459 &acpi_video_bus_POST_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_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001465 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001466 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001467 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001468 &acpi_video_bus_DOS_fops,
1469 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001471 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001473 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001474 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001475
1476 err_remove_post:
1477 remove_proc_entry("POST", device_dir);
1478 err_remove_post_info:
1479 remove_proc_entry("POST_info", device_dir);
1480 err_remove_rom:
1481 remove_proc_entry("ROM", device_dir);
1482 err_remove_info:
1483 remove_proc_entry("info", device_dir);
1484 err_remove_dir:
1485 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1486 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Len Brown4be44fc2005-08-05 00:44:28 -04001489static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001491 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001493 if (device_dir) {
1494 remove_proc_entry("info", device_dir);
1495 remove_proc_entry("ROM", device_dir);
1496 remove_proc_entry("POST_info", device_dir);
1497 remove_proc_entry("POST", device_dir);
1498 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001499 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 acpi_device_dir(device) = NULL;
1501 }
1502
Patrick Mocheld550d982006-06-27 00:41:40 -04001503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506/* --------------------------------------------------------------------------
1507 Driver Interface
1508 -------------------------------------------------------------------------- */
1509
1510/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001511static struct acpi_video_device_attrib*
1512acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1513{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001514 struct acpi_video_enumerated_device *ids;
1515 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001516
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001517 for (i = 0; i < video->attached_count; i++) {
1518 ids = &video->attached_array[i];
1519 if ((ids->value.int_val & 0xffff) == device_id)
1520 return &ids->value.attrib;
1521 }
1522
Rui Zhang82cae992007-01-03 23:40:53 -05001523 return NULL;
1524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526static int
Len Brown4be44fc2005-08-05 00:44:28 -04001527acpi_video_bus_get_one_device(struct acpi_device *device,
1528 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001530 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001531 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001532 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001533 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Len Brown4be44fc2005-08-05 00:44:28 -04001538 status =
1539 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (ACPI_SUCCESS(status)) {
1541
Burman Yan36bcbec2006-12-19 12:56:11 -08001542 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001544 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1547 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001548 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 data->device_id = device_id;
1551 data->video = video;
1552 data->dev = device;
1553
Rui Zhang82cae992007-01-03 23:40:53 -05001554 attribute = acpi_video_get_device_attr(video, device_id);
1555
1556 if((attribute != NULL) && attribute->device_id_scheme) {
1557 switch (attribute->display_type) {
1558 case ACPI_VIDEO_DISPLAY_CRT:
1559 data->flags.crt = 1;
1560 break;
1561 case ACPI_VIDEO_DISPLAY_TV:
1562 data->flags.tvout = 1;
1563 break;
1564 case ACPI_VIDEO_DISPLAY_DVI:
1565 data->flags.dvi = 1;
1566 break;
1567 case ACPI_VIDEO_DISPLAY_LCD:
1568 data->flags.lcd = 1;
1569 break;
1570 default:
1571 data->flags.unknown = 1;
1572 break;
1573 }
1574 if(attribute->bios_can_detect)
1575 data->flags.bios = 1;
1576 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 acpi_video_device_bind(video, data);
1580 acpi_video_device_find_cap(data);
1581
Patrick Mochel90130262006-05-19 16:54:48 -04001582 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001583 ACPI_DEVICE_NOTIFY,
1584 acpi_video_device_notify,
1585 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001587 printk(KERN_ERR PREFIX
1588 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001589 if(data->brightness)
1590 kfree(data->brightness->levels);
1591 kfree(data->brightness);
1592 kfree(data);
1593 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001596 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001598 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 acpi_video_device_add_fs(device);
1601
Patrick Mocheld550d982006-06-27 00:41:40 -04001602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Patrick Mocheld550d982006-06-27 00:41:40 -04001605 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
1609 * Arg:
1610 * video : video bus device
1611 *
1612 * Return:
1613 * none
1614 *
1615 * Enumerate the video device list of the video bus,
1616 * bind the ids with the corresponding video devices
1617 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Len Brown4be44fc2005-08-05 00:44:28 -04001620static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001622 struct acpi_video_device *dev;
1623
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001624 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001625
1626 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001627 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001628
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001629 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
1633 * Arg:
1634 * video : video bus device
1635 * device : video output device under the video
1636 * bus
1637 *
1638 * Return:
1639 * none
1640 *
1641 * Bind the ids with the corresponding video devices
1642 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001643 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645static void
Len Brown4be44fc2005-08-05 00:44:28 -04001646acpi_video_device_bind(struct acpi_video_bus *video,
1647 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001649 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001650 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001652 for (i = 0; i < video->attached_count; i++) {
1653 ids = &video->attached_array[i];
1654 if (device->device_id == (ids->value.int_val & 0xffff)) {
1655 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1657 }
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661/*
1662 * Arg:
1663 * video : video bus device
1664 *
1665 * Return:
1666 * < 0 : error
1667 *
1668 * Call _DOD to enumerate all devices attached to display adapter
1669 *
Len Brown4be44fc2005-08-05 00:44:28 -04001670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1673{
Len Brown4be44fc2005-08-05 00:44:28 -04001674 int status;
1675 int count;
1676 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001677 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001678 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1679 union acpi_object *dod = NULL;
1680 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Patrick Mochel90130262006-05-19 16:54:48 -04001682 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001684 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001685 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001688 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001690 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 status = -EFAULT;
1692 goto out;
1693 }
1694
1695 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001696 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001698 active_list = kcalloc(1 + dod->package.count,
1699 sizeof(struct acpi_video_enumerated_device),
1700 GFP_KERNEL);
1701 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 status = -ENOMEM;
1703 goto out;
1704 }
1705
1706 count = 0;
1707 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001708 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001711 printk(KERN_ERR PREFIX
1712 "Invalid _DOD data in element %d\n", i);
1713 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001715
1716 active_list[count].value.int_val = obj->integer.value;
1717 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001718 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1719 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 count++;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Jesper Juhl6044ec82005-11-07 01:01:32 -08001723 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001724
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001725 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001727
1728 out:
Len Brown02438d82006-06-30 03:19:10 -04001729 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001730 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Len Brown4be44fc2005-08-05 00:44:28 -04001733static int
1734acpi_video_get_next_level(struct acpi_video_device *device,
1735 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001737 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001738 max = max_below = 0;
1739 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001740 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001741 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001742 l = device->brightness->levels[i];
1743 if (abs(l - level_current) < abs(delta)) {
1744 delta = l - level_current;
1745 if (!delta)
1746 break;
1747 }
1748 }
1749 /* Ajust level_current to closest available level */
1750 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001751 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001752 l = device->brightness->levels[i];
1753 if (l < min)
1754 min = l;
1755 if (l > max)
1756 max = l;
1757 if (l < min_above && l > level_current)
1758 min_above = l;
1759 if (l > max_below && l < level_current)
1760 max_below = l;
1761 }
1762
1763 switch (event) {
1764 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1765 return (level_current < max) ? min_above : min;
1766 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1767 return (level_current < max) ? min_above : max;
1768 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1769 return (level_current > min) ? max_below : min;
1770 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1771 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1772 return 0;
1773 default:
1774 return level_current;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Zhang Ruic8890f92009-03-18 16:27:08 +08001778static int
Len Brown4be44fc2005-08-05 00:44:28 -04001779acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001781 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001782 int result = -EINVAL;
1783
Julia Jomantaite469778c2008-06-23 22:50:42 +01001784 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001785 goto out;
1786
1787 result = acpi_video_device_lcd_get_level_current(device,
1788 &level_current);
1789 if (result)
1790 goto out;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001793
Zhang Rui24450c72009-03-18 16:27:10 +08001794 result = acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001795
1796out:
1797 if (result)
1798 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1799
1800 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803static int
Len Brown4be44fc2005-08-05 00:44:28 -04001804acpi_video_bus_get_devices(struct acpi_video_bus *video,
1805 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Len Brown4be44fc2005-08-05 00:44:28 -04001807 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001808 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 acpi_video_device_enumerate(video);
1811
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001812 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 status = acpi_video_bus_get_one_device(dev, video);
1815 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001816 printk(KERN_WARNING PREFIX
1817 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 continue;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001821 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Len Brown4be44fc2005-08-05 00:44:28 -04001824static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Karol Kozimor031ec772005-07-30 04:18:00 -04001826 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 struct acpi_video_bus *video;
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001831 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 video = device->video;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 acpi_video_device_remove_fs(device->dev);
1836
Patrick Mochel90130262006-05-19 16:54:48 -04001837 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001838 ACPI_DEVICE_NOTIFY,
1839 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001840 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001841 if (device->cdev) {
1842 sysfs_remove_link(&device->dev->dev.kobj,
1843 "thermal_cooling");
1844 sysfs_remove_link(&device->cdev->device.kobj,
1845 "device");
1846 thermal_cooling_device_unregister(device->cdev);
1847 device->cdev = NULL;
1848 }
Luming Yu23b0f012007-05-09 21:07:05 +08001849 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001850
Patrick Mocheld550d982006-06-27 00:41:40 -04001851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Len Brown4be44fc2005-08-05 00:44:28 -04001854static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Len Brown4be44fc2005-08-05 00:44:28 -04001856 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001857 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001859 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001861 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001863 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001864 if (ACPI_FAILURE(status))
1865 printk(KERN_WARNING PREFIX
1866 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001868 if (dev->brightness) {
1869 kfree(dev->brightness->levels);
1870 kfree(dev->brightness);
1871 }
1872 list_del(&dev->entry);
1873 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001876 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001877
Patrick Mocheld550d982006-06-27 00:41:40 -04001878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881/* acpi_video interface */
1882
Len Brown4be44fc2005-08-05 00:44:28 -04001883static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Zhang Ruia21101c2007-09-14 11:46:22 +08001885 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Len Brown4be44fc2005-08-05 00:44:28 -04001888static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 return acpi_video_bus_DOS(video, 0, 1);
1891}
1892
Len Brown4be44fc2005-08-05 00:44:28 -04001893static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001895 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001896 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001897 struct input_dev *input;
1898 int keycode;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001901 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001903 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001904 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001907 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001909 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001910 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 break;
1912
Julius Volz98fb8fe2007-02-20 16:38:40 +01001913 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 * connector. */
1915 acpi_video_device_enumerate(video);
1916 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001917 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001918 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 break;
1920
Len Brown4be44fc2005-08-05 00:44:28 -04001921 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001922 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001923 keycode = KEY_SWITCHVIDEOMODE;
1924 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001925 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001926 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001927 keycode = KEY_VIDEO_NEXT;
1928 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001929 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001930 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001931 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
1933
1934 default:
Luming Yue9dab192007-08-20 18:23:53 +08001935 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001937 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939 }
1940
Zhang Rui7761f632008-01-25 14:48:12 +08001941 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001942 input_report_key(input, keycode, 1);
1943 input_sync(input);
1944 input_report_key(input, keycode, 0);
1945 input_sync(input);
1946
Patrick Mocheld550d982006-06-27 00:41:40 -04001947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Len Brown4be44fc2005-08-05 00:44:28 -04001950static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001952 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001953 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001954 struct acpi_video_bus *bus;
1955 struct input_dev *input;
1956 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001961 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001962 bus = video_device->video;
1963 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001966 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001967 if (brightness_switch_enabled)
1968 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001969 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001970 keycode = KEY_BRIGHTNESS_CYCLE;
1971 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001972 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001973 if (brightness_switch_enabled)
1974 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001975 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001976 keycode = KEY_BRIGHTNESSUP;
1977 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001978 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001979 if (brightness_switch_enabled)
1980 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001981 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001982 keycode = KEY_BRIGHTNESSDOWN;
1983 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001984 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001985 if (brightness_switch_enabled)
1986 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001987 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001988 keycode = KEY_BRIGHTNESS_ZERO;
1989 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001990 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001991 if (brightness_switch_enabled)
1992 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001993 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001994 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996 default:
Luming Yue9dab192007-08-20 18:23:53 +08001997 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001999 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
2001 }
Luming Yue9dab192007-08-20 18:23:53 +08002002
Zhang Rui7761f632008-01-25 14:48:12 +08002003 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08002004 input_report_key(input, keycode, 1);
2005 input_sync(input);
2006 input_report_key(input, keycode, 0);
2007 input_sync(input);
2008
Patrick Mocheld550d982006-06-27 00:41:40 -04002009 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Zhang Ruie6d9da12007-08-25 02:23:31 -04002012static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002013static int acpi_video_resume(struct acpi_device *device)
2014{
2015 struct acpi_video_bus *video;
2016 struct acpi_video_device *video_device;
2017 int i;
2018
2019 if (!device || !acpi_driver_data(device))
2020 return -EINVAL;
2021
2022 video = acpi_driver_data(device);
2023
2024 for (i = 0; i < video->attached_count; i++) {
2025 video_device = video->attached_array[i].bind_info;
2026 if (video_device && video_device->backlight)
2027 acpi_video_set_brightness(video_device->backlight);
2028 }
2029 return AE_OK;
2030}
2031
Len Brown4be44fc2005-08-05 00:44:28 -04002032static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002034 acpi_status status;
2035 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002036 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002037 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Burman Yan36bcbec2006-12-19 12:56:11 -08002039 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002041 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Zhang Ruie6d9da12007-08-25 02:23:31 -04002043 /* a hack to fix the duplicate name "VID" problem on T61 */
2044 if (!strcmp(device->pnp.bus_id, "VID")) {
2045 if (instance)
2046 device->pnp.bus_id[3] = '0' + instance;
2047 instance ++;
2048 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002049 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2050 if (!strcmp(device->pnp.bus_id, "VGA")) {
2051 if (instance)
2052 device->pnp.bus_id[3] = '0' + instance;
2053 instance++;
2054 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002055
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002056 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2058 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002059 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002062 error = acpi_video_bus_check(video);
2063 if (error)
2064 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002066 error = acpi_video_bus_add_fs(device);
2067 if (error)
2068 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002070 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 INIT_LIST_HEAD(&video->video_device_list);
2072
2073 acpi_video_bus_get_devices(video, device);
2074 acpi_video_bus_start_devices(video);
2075
Patrick Mochel90130262006-05-19 16:54:48 -04002076 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002077 ACPI_DEVICE_NOTIFY,
2078 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002080 printk(KERN_ERR PREFIX
2081 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002082 error = -ENODEV;
2083 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
Luming Yue9dab192007-08-20 18:23:53 +08002086 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002087 if (!input) {
2088 error = -ENOMEM;
2089 goto err_uninstall_notify;
2090 }
Luming Yue9dab192007-08-20 18:23:53 +08002091
2092 snprintf(video->phys, sizeof(video->phys),
2093 "%s/video/input0", acpi_device_hid(video->device));
2094
2095 input->name = acpi_device_name(video->device);
2096 input->phys = video->phys;
2097 input->id.bustype = BUS_HOST;
2098 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002099 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002100 input->evbit[0] = BIT(EV_KEY);
2101 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2102 set_bit(KEY_VIDEO_NEXT, input->keybit);
2103 set_bit(KEY_VIDEO_PREV, input->keybit);
2104 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2105 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2106 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2107 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2108 set_bit(KEY_DISPLAY_OFF, input->keybit);
2109 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002110
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002111 error = input_register_device(input);
2112 if (error)
2113 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002116 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2117 video->flags.multihead ? "yes" : "no",
2118 video->flags.rom ? "yes" : "no",
2119 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002123 err_free_input_dev:
2124 input_free_device(input);
2125 err_uninstall_notify:
2126 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2127 acpi_video_bus_notify);
2128 err_stop_video:
2129 acpi_video_bus_stop_devices(video);
2130 acpi_video_bus_put_devices(video);
2131 kfree(video->attached_array);
2132 acpi_video_bus_remove_fs(device);
2133 err_free_video:
2134 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002135 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002136
2137 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Len Brown4be44fc2005-08-05 00:44:28 -04002140static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Len Brown4be44fc2005-08-05 00:44:28 -04002142 acpi_status status = 0;
2143 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002149 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 acpi_video_bus_stop_devices(video);
2152
Patrick Mochel90130262006-05-19 16:54:48 -04002153 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002154 ACPI_DEVICE_NOTIFY,
2155 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 acpi_video_bus_put_devices(video);
2158 acpi_video_bus_remove_fs(device);
2159
Luming Yue9dab192007-08-20 18:23:53 +08002160 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002161 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 kfree(video);
2163
Patrick Mocheld550d982006-06-27 00:41:40 -04002164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Len Brown4be44fc2005-08-05 00:44:28 -04002167static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Len Brown4be44fc2005-08-05 00:44:28 -04002169 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2172 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002173 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 acpi_video_dir->owner = THIS_MODULE;
2175
2176 result = acpi_bus_register_driver(&acpi_video_bus);
2177 if (result < 0) {
2178 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002179 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181
Patrick Mocheld550d982006-06-27 00:41:40 -04002182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Len Brown4be44fc2005-08-05 00:44:28 -04002185static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 acpi_bus_unregister_driver(&acpi_video_bus);
2189
2190 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2191
Patrick Mocheld550d982006-06-27 00:41:40 -04002192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195module_init(acpi_video_init);
2196module_exit(acpi_video_exit);